OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [MW_0_8_9PRE7/] [mw/] [src/] [mwin/] [winlib/] [progbar.c] - Diff between revs 674 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 674 Rev 1765
/*
/*
 * Copyright (C) 1999, 2000, Wei Yongming.
 * Copyright (C) 1999, 2000, Wei Yongming.
 * Portions Copyright (c) 2000 Greg Haerr <greg@censoft.com>
 * Portions Copyright (c) 2000 Greg Haerr <greg@censoft.com>
 *
 *
 * Progress bar for Microwindows win32 api.
 * Progress bar for Microwindows win32 api.
 */
 */
 
 
/*
/*
**  This library is free software; you can redistribute it and/or
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Library General Public
**  modify it under the terms of the GNU Library General Public
**  License as published by the Free Software Foundation; either
**  License as published by the Free Software Foundation; either
**  version 2 of the License, or (at your option) any later version.
**  version 2 of the License, or (at your option) any later version.
**
**
**  This library is distributed in the hope that it will be useful,
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Library General Public License for more details.
**  Library General Public License for more details.
**
**
**  You should have received a copy of the GNU Library General Public
**  You should have received a copy of the GNU Library General Public
**  License along with this library; if not, write to the Free
**  License along with this library; if not, write to the Free
**  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
**  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
**  MA 02111-1307, USA
**  MA 02111-1307, USA
*/
*/
 
 
/*
/*
**  Alternatively, the contents of this file may be used under the terms
**  Alternatively, the contents of this file may be used under the terms
**  of the Mozilla Public License (the "MPL License") in which case the
**  of the Mozilla Public License (the "MPL License") in which case the
**  provisions of the MPL License are applicable instead of those above.
**  provisions of the MPL License are applicable instead of those above.
*/
*/
 
 
/* Copyright (C) 1999, 2000, Wei Yongming.
/* Copyright (C) 1999, 2000, Wei Yongming.
**
**
** Note:
** Note:
**   Originally by Zhao Jianghua.
**   Originally by Zhao Jianghua.
**
**
** Create date: 1999/8/29
** Create date: 1999/8/29
**
**
** Modify records:
** Modify records:
**
**
**  Who             When        Where       For What                Status
**  Who             When        Where       For What                Status
**-----------------------------------------------------------------------------
**-----------------------------------------------------------------------------
**  WEI Yongming    1999/10/27  Tsinghua    unsigned int            Finished
**  WEI Yongming    1999/10/27  Tsinghua    unsigned int            Finished
**  WEI Yongming    1999/10/27  Tsinghua    FPException fixing      Finished
**  WEI Yongming    1999/10/27  Tsinghua    FPException fixing      Finished
**  WEI Yongming    2000/02/24  Tsinghua    Add MPL License         Finished
**  WEI Yongming    2000/02/24  Tsinghua    Add MPL License         Finished
**  Kevin Tseng     2000/05/24  gv          port to microwin        ported
**  Kevin Tseng     2000/05/24  gv          port to microwin        ported
**  Greg Haerr      2000/06/15  Utah        removed floats          Finished
**  Greg Haerr      2000/06/15  Utah        removed floats          Finished
*/
*/
 
 
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#define MWINCLUDECOLORS
#define MWINCLUDECOLORS
#include "windows.h"    /* windef.h, winuser.h */
#include "windows.h"    /* windef.h, winuser.h */
#include "wintools.h"   /* Draw3dBox */
#include "wintools.h"   /* Draw3dBox */
#include "device.h"     /* GdGetTextSize */
#include "device.h"     /* GdGetTextSize */
 
 
#define TEST    1       /* =1 for testing*/
#define TEST    1       /* =1 for testing*/
 
 
#define  WIDTH_PBAR_BORDER  2 
#define  WIDTH_PBAR_BORDER  2 
 
 
typedef  struct _PROGRESSDATA {
typedef  struct _PROGRESSDATA {
    unsigned int nMin;
    unsigned int nMin;
    unsigned int nMax;
    unsigned int nMax;
    unsigned int nPos;
    unsigned int nPos;
    unsigned int nStepInc;
    unsigned int nStepInc;
} PROGRESSDATA, *PPROGRESSDATA;
} PROGRESSDATA, *PPROGRESSDATA;
 
 
static LRESULT CALLBACK
static LRESULT CALLBACK
ProgressBarCtrlProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
ProgressBarCtrlProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
 
 
int WINAPI MwRegisterProgressBarControl(HINSTANCE hInstance)
int WINAPI MwRegisterProgressBarControl(HINSTANCE hInstance)
{
{
        WNDCLASS        wc;
        WNDCLASS        wc;
 
 
        wc.style        = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_GLOBALCLASS;
        wc.style        = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_GLOBALCLASS;
        wc.lpfnWndProc  = (WNDPROC)ProgressBarCtrlProc;
        wc.lpfnWndProc  = (WNDPROC)ProgressBarCtrlProc;
        wc.cbClsExtra   = 0;
        wc.cbClsExtra   = 0;
        wc.cbWndExtra   = 0;
        wc.cbWndExtra   = 0;
        wc.hInstance    = hInstance;
        wc.hInstance    = hInstance;
        wc.hIcon        = NULL;
        wc.hIcon        = NULL;
        wc.hCursor      = 0; /*LoadCursor(NULL, IDC_ARROW);*/
        wc.hCursor      = 0; /*LoadCursor(NULL, IDC_ARROW);*/
        wc.hbrBackground= GetStockObject(LTGRAY_BRUSH);
        wc.hbrBackground= GetStockObject(LTGRAY_BRUSH);
        wc.lpszMenuName = NULL;
        wc.lpszMenuName = NULL;
        wc.lpszClassName= "PROGBAR";
        wc.lpszClassName= "PROGBAR";
 
 
        return RegisterClass(&wc);
        return RegisterClass(&wc);
}
}
 
 
static void
static void
FillBox(HDC hdc, int x, int y, int w, int h, COLORREF cr)
FillBox(HDC hdc, int x, int y, int w, int h, COLORREF cr)
{
{
        RECT    rc;
        RECT    rc;
 
 
        if (w <= 0)
        if (w <= 0)
                return;
                return;
        SetRect(&rc, x, y, x+w, y+h);
        SetRect(&rc, x, y, x+w, y+h);
        FastFillRect(hdc, &rc, cr);
        FastFillRect(hdc, &rc, cr);
}
}
 
 
static int
static int
GetSysCharWidth(HDC hdc)
GetSysCharWidth(HDC hdc)
{
{
        int     cw, ch, cb;
        int     cw, ch, cb;
 
 
        GdGetTextSize(hdc->font->pfont,"X",1, &cw,&ch,&cb,MWTF_ASCII);
        GdGetTextSize(hdc->font->pfont,"X",1, &cw,&ch,&cb,MWTF_ASCII);
        return cw;
        return cw;
}
}
 
 
static int
static int
GetSysCharHeight(HDC hdc)
GetSysCharHeight(HDC hdc)
{
{
        int     cw, ch, cb;
        int     cw, ch, cb;
 
 
        GdGetTextSize(hdc->font->pfont,"X",1, &cw,&ch,&cb,MWTF_ASCII);
        GdGetTextSize(hdc->font->pfont,"X",1, &cw,&ch,&cb,MWTF_ASCII);
        return ch;
        return ch;
}
}
 
 
void pbarOnDraw (HWND hwnd, HDC hdc, PROGRESSDATA* pData, BOOL fVertical,
void pbarOnDraw (HWND hwnd, HDC hdc, PROGRESSDATA* pData, BOOL fVertical,
        BOOL fErase)
        BOOL fErase)
{
{
    int     x, y, w, h;
    int     x, y, w, h;
    unsigned int     nAllPart;
    unsigned int     nAllPart;
    unsigned int     nNowPart;
    unsigned int     nNowPart;
    int     whOne, nRem;
    int     whOne, nRem;
    int     ix, iy;
    int     ix, iy;
    int     i;
    int     i;
    int     step;
    int     step;
    COLORREF cr;
    COLORREF cr;
    RECT    rcClient;
    RECT    rcClient;
    char    szText[8];
    char    szText[8];
 
 
    if (!hdc || (pData->nMax == pData->nMin))
    if (!hdc || (pData->nMax == pData->nMin))
        return;
        return;
 
 
    if ((pData->nMax - pData->nMin) > 5)
    if ((pData->nMax - pData->nMin) > 5)
        step = 5;
        step = 5;
    else
    else
        step = 1;
        step = 1;
 
 
    GetClientRect (hwnd, &rcClient);
    GetClientRect (hwnd, &rcClient);
    SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
    SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
 
 
    x = rcClient.left + WIDTH_PBAR_BORDER;
    x = rcClient.left + WIDTH_PBAR_BORDER;
    y = rcClient.top + WIDTH_PBAR_BORDER;
    y = rcClient.top + WIDTH_PBAR_BORDER;
    w = (rcClient.right - rcClient.left) - (WIDTH_PBAR_BORDER << 1);
    w = (rcClient.right - rcClient.left) - (WIDTH_PBAR_BORDER << 1);
    h = (rcClient.bottom - rcClient.top) - (WIDTH_PBAR_BORDER << 1);
    h = (rcClient.bottom - rcClient.top) - (WIDTH_PBAR_BORDER << 1);
 
 
    nAllPart = (pData->nMax - pData->nMin) / step;
    nAllPart = (pData->nMax - pData->nMin) / step;
    nNowPart = (pData->nPos - pData->nMin) / step;
    nNowPart = (pData->nPos - pData->nMin) / step;
    if (fVertical) {
    if (fVertical) {
        whOne = h / nAllPart;
        whOne = h / nAllPart;
        nRem = h % nAllPart;
        nRem = h % nAllPart;
    } else {
    } else {
        whOne = w / nAllPart;
        whOne = w / nAllPart;
        nRem = w % nAllPart;
        nRem = w % nAllPart;
    }
    }
 
 
    if (fErase)
    if (fErase)
        FillBox (hdc, x, y, w, h, GetSysColor(COLOR_BTNSHADOW));
        FillBox (hdc, x, y, w, h, GetSysColor(COLOR_BTNSHADOW));
 
 
    if(whOne >= 4) {
    if(whOne >= 4) {
        if (fVertical) {
        if (fVertical) {
            for (i = 0, iy = y + h - 1; i < nNowPart; ++i) {
            for (i = 0, iy = y + h - 1; i < nNowPart; ++i) {
#if 0
#if 0
                if ((iy - whOne) < y)
                if ((iy - whOne) < y)
                    whOne = iy - y;
                    whOne = iy - y;
#endif
#endif
 
 
                FillBox (hdc, x, iy - whOne, w, whOne - 1, BLUE);
                FillBox (hdc, x, iy - whOne, w, whOne - 1, BLUE);
 
 
                iy -= whOne + 1;
                iy -= whOne + 1;
#if 0
#if 0
                if(nRem > 0) {
                if(nRem > 0) {
                    iy --;
                    iy --;
                    nRem --;
                    nRem --;
                }
                }
#endif
#endif
            }
            }
        }
        }
        else {
        else {
            for (i = 0, ix = x + 1; i < nNowPart; ++i) {
            for (i = 0, ix = x + 1; i < nNowPart; ++i) {
#if 0
#if 0
                if ((ix + whOne) > (x + w))
                if ((ix + whOne) > (x + w))
                    whOne = x + w - ix;
                    whOne = x + w - ix;
#endif
#endif
                FillBox (hdc, ix, y, whOne - 1, h, BLUE);
                FillBox (hdc, ix, y, whOne - 1, h, BLUE);
                ix += whOne + 1;
                ix += whOne + 1;
#if 0
#if 0
                if(nRem > 0) {
                if(nRem > 0) {
                    ix ++;
                    ix ++;
                    nRem --;
                    nRem --;
                }
                }
#endif
#endif
            }
            }
        }
        }
    }
    }
    else {
    else {
        /* no vertical support */
        /* no vertical support */
        int d = nNowPart*100/nAllPart;
        int d = nNowPart*100/nAllPart;
        int maxw = GetSysCharWidth (hdc) << 2;
        int maxw = GetSysCharWidth (hdc) << 2;
        int charh = GetSysCharHeight (hdc);
        int charh = GetSysCharHeight (hdc);
 
 
        if (d > 50)
        if (d > 50)
            cr = BLUE;
            cr = BLUE;
        else
        else
            cr = GetSysColor(COLOR_BTNSHADOW);
            cr = GetSysColor(COLOR_BTNSHADOW);
        FillBox (hdc, x + ((w - maxw)>>1), y + ((h - charh) > 1), maxw,
        FillBox (hdc, x + ((w - maxw)>>1), y + ((h - charh) > 1), maxw,
                charh - 1, cr);
                charh - 1, cr);
        FillBox (hdc, x, y, (int)((long)w*d/100L), h, BLUE);
        FillBox (hdc, x, y, (int)((long)w*d/100L), h, BLUE);
        SetTextColor (hdc, WHITE);
        SetTextColor (hdc, WHITE);
        SetBkMode (hdc, TRANSPARENT);
        SetBkMode (hdc, TRANSPARENT);
        sprintf (szText, "%d%%", d);
        sprintf (szText, "%d%%", d);
        TextOut (hdc, x + ((w - GetSysCharWidth (hdc) * strlen (szText) )>>1),
        TextOut (hdc, x + ((w - GetSysCharWidth (hdc) * strlen (szText) )>>1),
                      y + ((h - GetSysCharHeight(hdc) )>>1),
                      y + ((h - GetSysCharHeight(hdc) )>>1),
                      szText, strlen(szText));
                      szText, strlen(szText));
    }
    }
}
}
 
 
static void pbarNormalizeParams (const HWND pCtrl,
static void pbarNormalizeParams (const HWND pCtrl,
                PROGRESSDATA* pData, BOOL fNotify)
                PROGRESSDATA* pData, BOOL fNotify)
{
{
    if (pData->nPos > pData->nMax) {
    if (pData->nPos > pData->nMax) {
        if (fNotify)
        if (fNotify)
            SendMessage (GetParent ((HWND)pCtrl), WM_COMMAND,
            SendMessage (GetParent ((HWND)pCtrl), WM_COMMAND,
                (WPARAM)MAKELONG (pCtrl->id, PBN_REACHMAX), (LPARAM)pCtrl);
                (WPARAM)MAKELONG (pCtrl->id, PBN_REACHMAX), (LPARAM)pCtrl);
        pData->nPos = pData->nMax;
        pData->nPos = pData->nMax;
    }
    }
 
 
    if (pData->nPos < pData->nMin) {
    if (pData->nPos < pData->nMin) {
        if (fNotify)
        if (fNotify)
            SendMessage (GetParent ((HWND)pCtrl), WM_COMMAND,
            SendMessage (GetParent ((HWND)pCtrl), WM_COMMAND,
                (WPARAM)MAKELONG (pCtrl->id, PBN_REACHMIN), (LPARAM)pCtrl);
                (WPARAM)MAKELONG (pCtrl->id, PBN_REACHMIN), (LPARAM)pCtrl);
        pData->nPos = pData->nMin;
        pData->nPos = pData->nMin;
    }
    }
}
}
 
 
static LRESULT CALLBACK
static LRESULT CALLBACK
ProgressBarCtrlProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
ProgressBarCtrlProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
{
    HDC           hdc;
    HDC           hdc;
    HWND          pCtrl;
    HWND          pCtrl;
    PROGRESSDATA* pData;
    PROGRESSDATA* pData;
    BOOL          fErase;
    BOOL          fErase;
    RECT        rc;
    RECT        rc;
    PAINTSTRUCT ps;
    PAINTSTRUCT ps;
    int         pos;
    int         pos;
 
 
    pCtrl = hwnd;
    pCtrl = hwnd;
 
 
    switch (message)
    switch (message)
    {
    {
        case WM_CREATE:
        case WM_CREATE:
            if (!(pData = malloc (sizeof (PROGRESSDATA)))) {
            if (!(pData = malloc (sizeof (PROGRESSDATA)))) {
                fprintf(stderr, "Create progress bar control failure!\n");
                fprintf(stderr, "Create progress bar control failure!\n");
                return -1;
                return -1;
            }
            }
 
 
#if TEST
#if TEST
            pData->nMax     = 1000;
            pData->nMax     = 1000;
            pData->nMin     = 0;
            pData->nMin     = 0;
            pData->nPos     = 500;
            pData->nPos     = 500;
            pData->nStepInc = 100;
            pData->nStepInc = 100;
#else
#else
            pData->nMax     = 100;
            pData->nMax     = 100;
            pData->nMin     = 0;
            pData->nMin     = 0;
            pData->nPos     = 0;
            pData->nPos     = 0;
            pData->nStepInc = 10;
            pData->nStepInc = 10;
#endif
#endif
 
 
            pCtrl->userdata = (DWORD)pData;
            pCtrl->userdata = (DWORD)pData;
        break;
        break;
 
 
        case WM_DESTROY:
        case WM_DESTROY:
            free ((void *)(pCtrl->userdata));
            free ((void *)(pCtrl->userdata));
        break;
        break;
 
 
        case WM_GETDLGCODE:
        case WM_GETDLGCODE:
            return DLGC_STATIC;
            return DLGC_STATIC;
 
 
        case WM_NCPAINT:
        case WM_NCPAINT:
                if(GetWindowLong(hwnd, GWL_STYLE) & WS_BORDER) {
                if(GetWindowLong(hwnd, GWL_STYLE) & WS_BORDER) {
                        GetWindowRect(hwnd, &rc);
                        GetWindowRect(hwnd, &rc);
                        hdc = GetWindowDC(hwnd);
                        hdc = GetWindowDC(hwnd);
                        Draw3dBox(hdc, rc.left, rc.top, rc.right-rc.left,
                        Draw3dBox(hdc, rc.left, rc.top, rc.right-rc.left,
                                rc.bottom-rc.top, GetSysColor(COLOR_BTNSHADOW),
                                rc.bottom-rc.top, GetSysColor(COLOR_BTNSHADOW),
                                GetSysColor(COLOR_BTNHIGHLIGHT));
                                GetSysColor(COLOR_BTNHIGHLIGHT));
                }
                }
            break;
            break;
 
 
        case WM_PAINT:
        case WM_PAINT:
            hdc = BeginPaint (hwnd,&ps);
            hdc = BeginPaint (hwnd,&ps);
            pbarOnDraw (hwnd, hdc, (PROGRESSDATA *)pCtrl->userdata,
            pbarOnDraw (hwnd, hdc, (PROGRESSDATA *)pCtrl->userdata,
                            hwnd->style & PBS_VERTICAL, TRUE);
                            hwnd->style & PBS_VERTICAL, TRUE);
            EndPaint (hwnd, &ps);
            EndPaint (hwnd, &ps);
            break;
            break;
 
 
        case PBM_SETRANGE:
        case PBM_SETRANGE:
            pData = (PROGRESSDATA *)pCtrl->userdata;
            pData = (PROGRESSDATA *)pCtrl->userdata;
            pData->nMin = min (wParam, lParam);
            pData->nMin = min (wParam, lParam);
            pData->nMax = max (wParam, lParam);
            pData->nMax = max (wParam, lParam);
            if (pData->nPos > pData->nMax)
            if (pData->nPos > pData->nMax)
                pData->nPos = pData->nMax;
                pData->nPos = pData->nMax;
            if (pData->nPos < pData->nMin)
            if (pData->nPos < pData->nMin)
                pData->nPos = pData->nMin;
                pData->nPos = pData->nMin;
        break;
        break;
 
 
        case PBM_SETSTEP:
        case PBM_SETSTEP:
            pData = (PROGRESSDATA *)pCtrl->userdata;
            pData = (PROGRESSDATA *)pCtrl->userdata;
            pData->nStepInc = wParam;
            pData->nStepInc = wParam;
        break;
        break;
 
 
        case PBM_SETPOS:
        case PBM_SETPOS:
            pData = (PROGRESSDATA *)pCtrl->userdata;
            pData = (PROGRESSDATA *)pCtrl->userdata;
 
 
            if (pData->nPos == wParam)
            if (pData->nPos == wParam)
                break;
                break;
 
 
            fErase = (wParam < pData->nPos);
            fErase = (wParam < pData->nPos);
            pData->nPos = wParam;
            pData->nPos = wParam;
            pbarNormalizeParams (pCtrl, pData, hwnd->style & PBS_NOTIFY);
            pbarNormalizeParams (pCtrl, pData, hwnd->style & PBS_NOTIFY);
            InvalidateRect(hwnd, NULL, fErase);
            InvalidateRect(hwnd, NULL, fErase);
        break;
        break;
 
 
        case PBM_DELTAPOS:
        case PBM_DELTAPOS:
            pData = (PROGRESSDATA *)pCtrl->userdata;
            pData = (PROGRESSDATA *)pCtrl->userdata;
 
 
            if (wParam == 0)
            if (wParam == 0)
                break;
                break;
 
 
            fErase = (wParam < 0);
            fErase = (wParam < 0);
            pData->nPos += wParam;
            pData->nPos += wParam;
            pbarNormalizeParams (pCtrl, pData, hwnd->style & PBS_NOTIFY);
            pbarNormalizeParams (pCtrl, pData, hwnd->style & PBS_NOTIFY);
            InvalidateRect(hwnd, NULL, fErase);
            InvalidateRect(hwnd, NULL, fErase);
        break;
        break;
 
 
        case PBM_STEPIT:
        case PBM_STEPIT:
            pData = (PROGRESSDATA *)pCtrl->userdata;
            pData = (PROGRESSDATA *)pCtrl->userdata;
 
 
            if (pData->nStepInc == 0)
            if (pData->nStepInc == 0)
                break;
                break;
 
 
            fErase = (pData->nStepInc < 0);
            fErase = (pData->nStepInc < 0);
            pData->nPos += pData->nStepInc;
            pData->nPos += pData->nStepInc;
            pbarNormalizeParams (pCtrl, pData, hwnd->style & PBS_NOTIFY);
            pbarNormalizeParams (pCtrl, pData, hwnd->style & PBS_NOTIFY);
            InvalidateRect(hwnd, NULL, fErase);
            InvalidateRect(hwnd, NULL, fErase);
        break;
        break;
 
 
#if TEST
#if TEST
        case WM_LBUTTONDOWN:
        case WM_LBUTTONDOWN:
        case WM_LBUTTONDBLCLK:
        case WM_LBUTTONDBLCLK:
                pData = (PROGRESSDATA *)pCtrl->userdata;
                pData = (PROGRESSDATA *)pCtrl->userdata;
                pos = pData->nPos;
                pos = pData->nPos;
                pos += pData->nStepInc;
                pos += pData->nStepInc;
                if (pos > pData->nMax)
                if (pos > pData->nMax)
                        pos = pData->nMin;
                        pos = pData->nMin;
                SendMessage(hwnd, PBM_SETPOS, pos, 0L);
                SendMessage(hwnd, PBM_SETPOS, pos, 0L);
                break;
                break;
#endif
#endif
        default:
        default:
                return DefWindowProc (hwnd, message, wParam, lParam);
                return DefWindowProc (hwnd, message, wParam, lParam);
    }
    }
    return 0;
    return 0;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.