2 * Copyright (C) 1999, 2000, Wei Yongming.
3 * Portions Copyright (c) 2000 Greg Haerr <greg@censoft.com>
5 * Progress bar for Microwindows win32 api.
9 ** This library is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU Library General Public
11 ** License as published by the Free Software Foundation; either
12 ** version 2 of the License, or (at your option) any later version.
14 ** This library is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** Library General Public License for more details.
19 ** You should have received a copy of the GNU Library General Public
20 ** License along with this library; if not, write to the Free
21 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26 ** Alternatively, the contents of this file may be used under the terms
27 ** of the Mozilla Public License (the "MPL License") in which case the
28 ** provisions of the MPL License are applicable instead of those above.
31 /* Copyright (C) 1999, 2000, Wei Yongming.
34 ** Originally by Zhao Jianghua.
36 ** Create date: 1999/8/29
40 ** Who When Where For What Status
41 **-----------------------------------------------------------------------------
42 ** WEI Yongming 1999/10/27 Tsinghua unsigned int Finished
43 ** WEI Yongming 1999/10/27 Tsinghua FPException fixing Finished
44 ** WEI Yongming 2000/02/24 Tsinghua Add MPL License Finished
45 ** Kevin Tseng 2000/05/24 gv port to microwin ported
46 ** Greg Haerr 2000/06/15 Utah removed floats Finished
52 #define MWINCLUDECOLORS
53 #include "windows.h" /* windef.h, winuser.h */
54 #include "wintools.h" /* Draw3dBox */
55 #include "device.h" /* GdGetTextSize */
57 #define TEST 1 /* =1 for testing*/
59 #define WIDTH_PBAR_BORDER 2
61 typedef struct _PROGRESSDATA {
65 unsigned int nStepInc;
66 } PROGRESSDATA, *PPROGRESSDATA;
68 static LRESULT CALLBACK
69 ProgressBarCtrlProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
71 int WINAPI MwRegisterProgressBarControl(HINSTANCE hInstance)
75 wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_GLOBALCLASS;
76 wc.lpfnWndProc = (WNDPROC)ProgressBarCtrlProc;
79 wc.hInstance = hInstance;
81 wc.hCursor = 0; /*LoadCursor(NULL, IDC_ARROW);*/
82 wc.hbrBackground= GetStockObject(LTGRAY_BRUSH);
83 wc.lpszMenuName = NULL;
84 wc.lpszClassName= "PROGBAR";
86 return RegisterClass(&wc);
90 FillBox(HDC hdc, int x, int y, int w, int h, COLORREF cr)
96 SetRect(&rc, x, y, x+w, y+h);
97 FastFillRect(hdc, &rc, cr);
101 GetSysCharWidth(HDC hdc)
105 GdGetTextSize(hdc->font->pfont,"X",1, &cw,&ch,&cb,MWTF_ASCII);
110 GetSysCharHeight(HDC hdc)
114 GdGetTextSize(hdc->font->pfont,"X",1, &cw,&ch,&cb,MWTF_ASCII);
118 void pbarOnDraw (HWND hwnd, HDC hdc, PROGRESSDATA* pData, BOOL fVertical,
122 unsigned int nAllPart;
123 unsigned int nNowPart;
132 if (!hdc || (pData->nMax == pData->nMin))
135 if ((pData->nMax - pData->nMin) > 5)
140 GetClientRect (hwnd, &rcClient);
141 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
143 x = rcClient.left + WIDTH_PBAR_BORDER;
144 y = rcClient.top + WIDTH_PBAR_BORDER;
145 w = (rcClient.right - rcClient.left) - (WIDTH_PBAR_BORDER << 1);
146 h = (rcClient.bottom - rcClient.top) - (WIDTH_PBAR_BORDER << 1);
148 nAllPart = (pData->nMax - pData->nMin) / step;
149 nNowPart = (pData->nPos - pData->nMin) / step;
151 whOne = h / nAllPart;
154 whOne = w / nAllPart;
159 FillBox (hdc, x, y, w, h, GetSysColor(COLOR_BTNSHADOW));
163 for (i = 0, iy = y + h - 1; i < nNowPart; ++i) {
165 if ((iy - whOne) < y)
169 FillBox (hdc, x, iy - whOne, w, whOne - 1, BLUE);
181 for (i = 0, ix = x + 1; i < nNowPart; ++i) {
183 if ((ix + whOne) > (x + w))
186 FillBox (hdc, ix, y, whOne - 1, h, BLUE);
198 /* no vertical support */
199 int d = nNowPart*100/nAllPart;
200 int maxw = GetSysCharWidth (hdc) << 2;
201 int charh = GetSysCharHeight (hdc);
206 cr = GetSysColor(COLOR_BTNSHADOW);
207 FillBox (hdc, x + ((w - maxw)>>1), y + ((h - charh) > 1), maxw,
209 FillBox (hdc, x, y, (int)((long)w*d/100L), h, BLUE);
210 SetTextColor (hdc, WHITE);
211 SetBkMode (hdc, TRANSPARENT);
212 sprintf (szText, "%d%%", d);
213 TextOut (hdc, x + ((w - GetSysCharWidth (hdc) * strlen (szText) )>>1),
214 y + ((h - GetSysCharHeight(hdc) )>>1),
215 szText, strlen(szText));
219 static void pbarNormalizeParams (const HWND pCtrl,
220 PROGRESSDATA* pData, BOOL fNotify)
222 if (pData->nPos > pData->nMax) {
224 SendMessage (GetParent ((HWND)pCtrl), WM_COMMAND,
225 (WPARAM)MAKELONG (pCtrl->id, PBN_REACHMAX), (LPARAM)pCtrl);
226 pData->nPos = pData->nMax;
229 if (pData->nPos < pData->nMin) {
231 SendMessage (GetParent ((HWND)pCtrl), WM_COMMAND,
232 (WPARAM)MAKELONG (pCtrl->id, PBN_REACHMIN), (LPARAM)pCtrl);
233 pData->nPos = pData->nMin;
237 static LRESULT CALLBACK
238 ProgressBarCtrlProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
253 if (!(pData = malloc (sizeof (PROGRESSDATA)))) {
254 fprintf(stderr, "Create progress bar control failure!\n");
262 pData->nStepInc = 100;
267 pData->nStepInc = 10;
270 pCtrl->userdata = (DWORD)pData;
274 free ((void *)(pCtrl->userdata));
281 if(GetWindowLong(hwnd, GWL_STYLE) & WS_BORDER) {
282 GetWindowRect(hwnd, &rc);
283 hdc = GetWindowDC(hwnd);
284 Draw3dBox(hdc, rc.left, rc.top, rc.right-rc.left,
285 rc.bottom-rc.top, GetSysColor(COLOR_BTNSHADOW),
286 GetSysColor(COLOR_BTNHIGHLIGHT));
291 hdc = BeginPaint (hwnd,&ps);
292 pbarOnDraw (hwnd, hdc, (PROGRESSDATA *)pCtrl->userdata,
293 hwnd->style & PBS_VERTICAL, TRUE);
294 EndPaint (hwnd, &ps);
298 pData = (PROGRESSDATA *)pCtrl->userdata;
299 pData->nMin = min (wParam, lParam);
300 pData->nMax = max (wParam, lParam);
301 if (pData->nPos > pData->nMax)
302 pData->nPos = pData->nMax;
303 if (pData->nPos < pData->nMin)
304 pData->nPos = pData->nMin;
308 pData = (PROGRESSDATA *)pCtrl->userdata;
309 pData->nStepInc = wParam;
313 pData = (PROGRESSDATA *)pCtrl->userdata;
315 if (pData->nPos == wParam)
318 fErase = (wParam < pData->nPos);
319 pData->nPos = wParam;
320 pbarNormalizeParams (pCtrl, pData, hwnd->style & PBS_NOTIFY);
321 InvalidateRect(hwnd, NULL, fErase);
325 pData = (PROGRESSDATA *)pCtrl->userdata;
330 fErase = (wParam < 0);
331 pData->nPos += wParam;
332 pbarNormalizeParams (pCtrl, pData, hwnd->style & PBS_NOTIFY);
333 InvalidateRect(hwnd, NULL, fErase);
337 pData = (PROGRESSDATA *)pCtrl->userdata;
339 if (pData->nStepInc == 0)
342 fErase = (pData->nStepInc < 0);
343 pData->nPos += pData->nStepInc;
344 pbarNormalizeParams (pCtrl, pData, hwnd->style & PBS_NOTIFY);
345 InvalidateRect(hwnd, NULL, fErase);
350 case WM_LBUTTONDBLCLK:
351 pData = (PROGRESSDATA *)pCtrl->userdata;
353 pos += pData->nStepInc;
354 if (pos > pData->nMax)
356 SendMessage(hwnd, PBM_SETPOS, pos, 0L);
360 return DefWindowProc (hwnd, message, wParam, lParam);