[2] | 1 | /*
|
---|
| 2 | * This file is (C) Chris Wohlgemuth 2001/2002
|
---|
| 3 | */
|
---|
| 4 | /*
|
---|
| 5 | * This program is free software; you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation; either version 2, or (at your option)
|
---|
| 8 | * any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program; see the file COPYING. If not, write to
|
---|
| 17 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | #define INCL_WIN
|
---|
| 21 | #define INCL_GPI
|
---|
| 22 | #include <os2.h>
|
---|
| 23 | #include <stdlib.h>
|
---|
| 24 | #include <string.h>
|
---|
| 25 | #include <stdio.h>
|
---|
| 26 |
|
---|
| 27 | /* Window ULONG for the percent bar */
|
---|
| 28 | #define QWL_PERCENT 4 /* The location in the window words to store the percent value */
|
---|
| 29 | #define QWL_TEXTPTR 8 /* The ptr to our percent bar text */
|
---|
| 30 |
|
---|
| 31 | /*
|
---|
| 32 | * Paint the percent bar and print the label if necessary.
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | VOID paintPercent(int iPercent, HWND hwnd, HPS hps)
|
---|
| 36 | {
|
---|
| 37 | POINTL ptlText, aptlText[TXTBOX_COUNT];
|
---|
| 38 | RECTL rcl, rcl2;
|
---|
| 39 | BOOL bVertical=FALSE;
|
---|
| 40 | CHAR * ptrChr=NULL;
|
---|
| 41 |
|
---|
| 42 | WinQueryWindowRect(hwnd, &rcl);
|
---|
| 43 | /* Check if it's a vertical percent bar */
|
---|
| 44 | if(rcl.xRight<rcl.yTop)
|
---|
| 45 | bVertical=TRUE;
|
---|
| 46 | else
|
---|
| 47 | bVertical=FALSE;
|
---|
| 48 |
|
---|
| 49 | GpiCreateLogColorTable(hps, 0, LCOLF_RGB, 0, 0, NULL);
|
---|
| 50 |
|
---|
| 51 | /* Draw the bar border */
|
---|
| 52 | WinDrawBorder(hps, &rcl, 1,1,0,0, 0x800);
|
---|
| 53 |
|
---|
| 54 | rcl.xLeft = 1;
|
---|
| 55 | rcl.xRight -= 1;
|
---|
| 56 | rcl.yBottom = 1;
|
---|
| 57 | rcl.yTop -= 1;
|
---|
| 58 |
|
---|
| 59 | if((ptrChr=(char*)WinQueryWindowPtr(hwnd,QWL_TEXTPTR))!=NULLHANDLE)
|
---|
| 60 | {
|
---|
| 61 | /* Text size */
|
---|
| 62 | GpiQueryTextBox(hps, strlen(ptrChr), ptrChr,
|
---|
| 63 | TXTBOX_COUNT, (PPOINTL)&aptlText);
|
---|
| 64 |
|
---|
| 65 | ptlText.x = rcl.xLeft+(((rcl.xRight-rcl.xLeft)
|
---|
| 66 | -(aptlText[TXTBOX_BOTTOMRIGHT].x-aptlText[TXTBOX_BOTTOMLEFT].x))/2);
|
---|
| 67 | ptlText.y = 3 + rcl.yBottom+(((rcl.yTop-rcl.yBottom)
|
---|
| 68 | -(aptlText[TXTBOX_TOPLEFT].y-aptlText[TXTBOX_BOTTOMLEFT].y))/2);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | if(!bVertical) {
|
---|
| 72 | rcl2.xLeft = rcl.xLeft;
|
---|
| 73 | rcl2.xRight = (rcl.xRight-rcl.xLeft)*iPercent/100;
|
---|
| 74 | rcl2.yBottom = rcl.yBottom;
|
---|
| 75 | rcl2.yTop = rcl.yTop-1;
|
---|
| 76 | rcl.xLeft=rcl2.xRight+1;
|
---|
| 77 | }
|
---|
| 78 | else {
|
---|
| 79 | rcl2.xLeft = rcl.xLeft;
|
---|
| 80 | rcl2.xRight = rcl.xRight-1;
|
---|
| 81 | rcl2.yBottom = rcl.yBottom;
|
---|
| 82 | rcl2.yTop = (rcl.yTop-rcl.yBottom)*iPercent/100;
|
---|
| 83 | rcl.yBottom=rcl2.yTop+1;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /* Background */
|
---|
| 87 | WinFillRect(hps, &rcl,
|
---|
| 88 | WinQuerySysColor(HWND_DESKTOP, SYSCLR_DIALOGBACKGROUND, 0));
|
---|
| 89 |
|
---|
| 90 | /* Percentbar */
|
---|
| 91 | if ((rcl2.xRight > rcl2.xLeft && !bVertical)||(rcl2.yTop > rcl2.yBottom && bVertical)) {
|
---|
| 92 | ULONG ulBG;
|
---|
| 93 |
|
---|
| 94 | /* Find color. This color is the background color set within DrDialog */
|
---|
| 95 | if(!WinQueryPresParam(hwnd, PP_BACKGROUNDCOLOR, PP_BACKGROUNDCOLORINDEX, NULL, sizeof(ulBG),
|
---|
| 96 | &ulBG, QPF_ID2COLORINDEX|QPF_NOINHERIT ))
|
---|
| 97 | ulBG=0x002020ff;
|
---|
| 98 | GpiSetColor(hps,ulBG );
|
---|
| 99 |
|
---|
| 100 | rcl2.yBottom+=1;
|
---|
| 101 | rcl2.xLeft+=1;
|
---|
| 102 |
|
---|
| 103 | WinFillRect(hps, &rcl2, ulBG);
|
---|
| 104 | WinDrawBorder(hps, &rcl2, 1,1,0,0, 0x400);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /* now print the percentage */
|
---|
| 108 | if(ptrChr!=NULLHANDLE)
|
---|
| 109 | {
|
---|
| 110 | ULONG ulFG;
|
---|
| 111 |
|
---|
| 112 | /* Find color. This color is the foreground color set within DrDialog */
|
---|
| 113 | if(!WinQueryPresParam(hwnd, PP_FOREGROUNDCOLOR, PP_FOREGROUNDCOLORINDEX, NULL, sizeof(ulFG),
|
---|
| 114 | &ulFG, QPF_ID2COLORINDEX|QPF_NOINHERIT ))
|
---|
| 115 | ulFG=WinQuerySysColor(HWND_DESKTOP, SYSCLR_BUTTONDEFAULT, 0);
|
---|
| 116 | GpiSetColor(hps,ulFG );
|
---|
| 117 | GpiMove(hps, &ptlText);
|
---|
| 118 | GpiCharString(hps, strlen(ptrChr), ptrChr);
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | /*
|
---|
| 124 | * This is the window proc for the percentbar control
|
---|
| 125 | */
|
---|
| 126 |
|
---|
| 127 | MRESULT EXPENTRY percentBarProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
|
---|
| 128 | {
|
---|
| 129 | MRESULT mrc;
|
---|
| 130 | HPS hps;
|
---|
| 131 | PWNDPARAMS pwp;
|
---|
| 132 | int iPercent;
|
---|
| 133 |
|
---|
| 134 | switch(msg) {
|
---|
| 135 |
|
---|
| 136 | case WM_SETWINDOWPARAMS:
|
---|
| 137 | {
|
---|
| 138 | pwp=(PWNDPARAMS)mp1;
|
---|
| 139 |
|
---|
| 140 | if(pwp->fsStatus==WPM_TEXT) {
|
---|
| 141 | /* The text changed */
|
---|
| 142 | char *ptr;
|
---|
| 143 | char *ptr2;
|
---|
| 144 |
|
---|
| 145 | /* Get the current percent value for the control */
|
---|
| 146 | iPercent=atol(pwp->pszText);
|
---|
| 147 | if(iPercent>100)
|
---|
| 148 | iPercent=100;
|
---|
| 149 | if(iPercent<0)
|
---|
| 150 | iPercent=0;
|
---|
| 151 |
|
---|
| 152 | /* Check if there is some text for the bar */
|
---|
| 153 | if((ptr=strchr(pwp->pszText, '#'))!=NULLHANDLE) {
|
---|
| 154 | /* Everything after the '#' is treated as the label */
|
---|
| 155 | if((ptr2=(char*)WinQueryWindowPtr(hwnd,QWL_TEXTPTR))!=NULLHANDLE)
|
---|
| 156 | free(ptr2); /* Free the old text */
|
---|
| 157 | WinSetWindowPtr(hwnd,QWL_TEXTPTR, NULLHANDLE);
|
---|
| 158 | if(*(ptr++)!=0) {
|
---|
| 159 | /* There's additional text to print */
|
---|
| 160 | if((ptr2=calloc(strlen(ptr)+1, sizeof(char)))!=NULLHANDLE) {
|
---|
| 161 | strcpy(ptr2,ptr);
|
---|
| 162 | WinSetWindowPtr(hwnd,QWL_TEXTPTR,ptr2);
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | mrc = WinDefWindowProc(hwnd, msg, mp1, mp2);
|
---|
| 167 | WinSetWindowULong(hwnd, QWL_PERCENT,iPercent);
|
---|
| 168 | WinInvalidateRect(hwnd, NULLHANDLE,TRUE);
|
---|
| 169 | return mrc;
|
---|
| 170 | }
|
---|
| 171 | break;
|
---|
| 172 | }
|
---|
| 173 | case WM_DESTROY:
|
---|
| 174 | {
|
---|
| 175 | char *ptrText;
|
---|
| 176 | /* Free the memory allocated for the text */
|
---|
| 177 | if((ptrText=(char*)WinQueryWindowPtr(hwnd,QWL_TEXTPTR))!=NULLHANDLE)
|
---|
| 178 | free(ptrText);
|
---|
| 179 | break;
|
---|
| 180 | }
|
---|
| 181 | case WM_PRESPARAMCHANGED:
|
---|
| 182 | /* The color or the font has changed */
|
---|
| 183 | /* Force a repaint of the percent bar */
|
---|
| 184 | mrc = WinDefWindowProc(hwnd, msg, mp1, mp2);
|
---|
| 185 | if(LONGFROMMP(mp1)==PP_FOREGROUNDCOLOR)
|
---|
| 186 | WinInvalidateRect(hwnd, NULLHANDLE,TRUE);
|
---|
| 187 | else if(LONGFROMMP(mp1)==PP_BACKGROUNDCOLOR)
|
---|
| 188 | WinInvalidateRect(hwnd, NULLHANDLE,TRUE);
|
---|
| 189 | else if(LONGFROMMP(mp1)==PP_FONTNAMESIZE)
|
---|
| 190 | WinInvalidateRect(hwnd, NULLHANDLE,TRUE);
|
---|
| 191 | return mrc;
|
---|
| 192 | case WM_PAINT:
|
---|
| 193 | {
|
---|
| 194 | hps=WinBeginPaint(hwnd, NULLHANDLE, NULLHANDLE);
|
---|
| 195 | paintPercent(WinQueryWindowULong(hwnd,QWL_PERCENT), hwnd, hps);
|
---|
| 196 | WinEndPaint(hps);
|
---|
| 197 | return (MRESULT) FALSE;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | default:
|
---|
| 201 | break;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | mrc = WinDefWindowProc(hwnd, msg, mp1, mp2);
|
---|
| 205 | return (mrc);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 | BOOL percentRegisterBarClass(void)
|
---|
| 210 | {
|
---|
| 211 | return WinRegisterClass(WinQueryAnchorBlock(HWND_DESKTOP),"CW_PERCENTBAR", percentBarProc,0L,12);
|
---|
| 212 | }
|
---|