--- pmsdk/samples/calc/calc.c 2018/08/09 12:28:12 1.1.1.1 +++ pmsdk/samples/calc/calc.c 2018/08/09 12:28:19 1.1.1.2 @@ -1,14 +1,21 @@ -/****************************** Module Header ******************************\ -* Module Name: calc.c - Calc application -* -* OS/2 Presentation Manager version of Calc, ported from Windows version -* -* Created by Microsoft Corporation, 1987 -* -\***************************************************************************/ - -#define INCL_WIN -#define INCL_GPI +/****************************** Module Header *********************************/ +/* */ +/* Module Name: calc.c - Calc application */ +/* */ +/* OS/2 Presentation Manager version of Calc, ported from Windows version */ +/* */ +/* Created by Microsoft Corporation, 1987 */ +/* */ +/******************************************************************************/ + +#define INCL_WININPUT +#define INCL_WINPOINTERS +#define INCL_WINMENUS +#define INCL_WINSYS +#define INCL_WINCLIPBOARD +#define INCL_GPIPRIMITIVES +#define INCL_GPIBITMAPS +#define INCL_GPILCIDS #define INCL_DEV #define INCL_ERRORS #define INCL_DOSPROCESS @@ -20,20 +27,20 @@ #include #include "calc.h" -/************* GLOBAL VARIABLES */ - -char lastkey, currkey; -char szCalcClass[] = "Calculator"; -char szTitle[30]; -char szreg1[20], szreg2[20], szmem[20], szregx[20]; -/* hope 20 is enough for kanji error string */ -char szErrorString[20], szPlusMinus[2]; -short charwidth, charheight; -int aspectx, aspecty, nchszstr; +/******************************************************************************/ +/* */ +/* GLOBAL VARIABLES */ +/* */ +/******************************************************************************/ + +CHAR chLastKey, chCurrKey; +CHAR szreg1[20], szreg2[20], szmem[20], szregx[20]; +CHAR szTitle[30], szErrorString[20], szPlusMinus[2]; +SHORT sCharWidth, sCharHeight; extern BOOL fError; -BOOL fValueInMemory = FALSE; -BOOL fMDown = FALSE; -UCHAR mScan = 0; +BOOL fValueInMemory = FALSE; +BOOL fMDown = FALSE; /* TRUE iff 'm' key depressed */ +UCHAR uchMScan = 0; /* scan code for 'm' key */ #define TOLOWER(x) ( (((x) >= 'A') && ((x) <= 'Z')) ? (x)|0x20 : (x)) #define WIDTHCONST 28 @@ -49,6 +56,8 @@ HBITMAP hbmLocal, hbmSqr; HMQ hmqCalc; HWND hwndCalc, hwndMenu; HWND hwndCalcFrame; +HPS hpsCalc; +HDC hdcCalc; HPOINTER hptrFinger; DEVOPENSTRUC dop = /* used by DevOpenDC */ @@ -56,7 +65,7 @@ DEVOPENSTRUC dop = /* used by DevOp NULL, "DISPLAY", NULL, NULL, NULL, NULL, NULL, NULL, NULL }; -static char keys[25] = /* keyboard keys */ +static char achKeys[25] = /* keyboard keys */ { '\271', '0', '.', '\261', '+', '=', '\272', '1', '2', '3', '-', 'c', @@ -65,7 +74,7 @@ static char keys[25] = /* keyboard NULL }; -static char dkeys[25] = /* 4th key is plusminus */ +static CHAR achDKeys[25] = /* 4th key is plusminus */ { ' ', '0', '.', '+', '+', '=', ' ', '1', '2', '3', '-', 'C', @@ -74,83 +83,103 @@ static char dkeys[25] = /* 4th key is NULL }; -/************* PROCEDURE DECLARATIONS */ +/******************************************************************************/ +/* */ +/* PROCEDURE DECLARATIONS */ +/* */ +/******************************************************************************/ -MPARAM EXPENTRY AboutDlgProc(HWND, USHORT, MPARAM, MPARAM); +VOID FarStrcpy( PSZ, PSZ); +MPARAM CALLBACK AboutDlgProc( HWND, USHORT, MPARAM, MPARAM); BOOL CalcInit(VOID); VOID CalcPaint( HWND, HPS); -extern BOOL CalcTextOut( HPS, INT, INT, PCH, INT); -MRESULT EXPENTRY CalcWndProc(HWND, USHORT, MPARAM, MPARAM); +VOID CalcTextOut( HPS, INT, INT, PCH, INT); +MRESULT CALLBACK CalcWndProc( HWND, USHORT, MPARAM, MPARAM); VOID cdecl main(VOID); VOID DataXCopy( VOID); VOID DataXPaste( VOID); VOID DrawNumbers( HPS); VOID Evaluate(BYTE); -BOOL FlashSqr( HPS, NPWPOINT); -VOID FlipKey(HPS, int, int); +BOOL FlashSqr( HPS, PWPOINT); +VOID FlipKey( HPS, INT, INT); VOID FrameKey( HPS, INT, INT); VOID InitCalc( VOID); BOOL InterpretChar( CHAR); -VOID ProcessKey(HWND, WPOINT *); +VOID ProcessKey( PWPOINT); BOOL PSInit( VOID); -char Translate(WPOINT *); +CHAR Translate( PWPOINT); VOID UpdateDisplay( VOID); +/******************************************************************************/ +/******************************************************************************/ +VOID CalcTextOut( hps, iX, iY, pch, iCount) + +HPS hps; +INT iX, iY; +PCH pch; +INT iCount; +{ + POINTL ptl; -/******************************************************************** - Write the appropriate number or error string to the display area - and mark memory-in-use if appropriate. - */ + ptl.x = iX; + ptl.y = iY; -VOID UpdateDisplay() + GpiSetColor( hps, CLR_BLACK); + GpiCharStringAt( hps, (PPOINTL)&ptl, (LONG)iCount, (PSZ)pch); +} + + +/******************************************************************************/ +/* Write the appropriate number or error string to the display area */ +/* and mark memory-in-use if appropriate. */ +/******************************************************************************/ +VOID +UpdateDisplay() { - int len, size; - HPS hps; RECTL rcl; - len = strlen(szreg1); - size = ((szreg1[0] == '-') ? 10 : 9); /* check for leading minus sign */ - if (strchr(szreg1, '.')) - size++; - hps = WinGetPS(hwndCalc); - - rcl.xLeft = (6 * charwidth); - rcl.yBottom = 1050 * charheight / 100; - rcl.xRight = rcl.xLeft + (12 * charwidth); - rcl.yTop = rcl.yBottom + charheight; - WinFillRect(hps, &rcl, CLR_WHITE); /* paint display area white */ - - if (fError) /* display error or number */ - { - CalcTextOut( hps, 17 * charwidth - charwidth * 5, - 1050 * charheight / 100, - (PSZ)"Error", 5); - } + rcl.xLeft = (6 * sCharWidth); + rcl.yBottom = 1050 * sCharHeight / 100; + rcl.xRight = rcl.xLeft + (12 * sCharWidth); + rcl.yTop = rcl.yBottom + (3 * sCharHeight) / 2; + + WinFillRect( hpsCalc, &rcl, CLR_WHITE); /* paint display area white */ + if( fError) + WinDrawText( hpsCalc + , -1 + , szErrorString + , &rcl + , CLR_BLACK + , CLR_WHITE + , DT_RIGHT | DT_VCENTER ); else - { - CalcTextOut(hps, 18 * charwidth - (charwidth * min(len,size)), - 1050 * charheight / 100, (PCH)szreg1, min(len, size)); - } + WinDrawText( hpsCalc + , -1 + , szreg1 + , &rcl + , CLR_BLACK + , CLR_WHITE + , DT_RIGHT | DT_VCENTER ); if (fValueInMemory) /* little black square shows mem use */ { - rcl.xLeft = (6 * charwidth); - rcl.yBottom = 1050 * charheight / 100; - rcl.xRight = rcl.xLeft + (charwidth / 2); - rcl.yTop = rcl.yBottom + (charheight / 2); - WinFillRect(hps, &rcl, CLR_BLACK); + rcl.xLeft = (6 * sCharWidth); + rcl.yBottom = 1050 * sCharHeight / 100; + rcl.xRight = rcl.xLeft + (sCharWidth / 2); + rcl.yTop = rcl.yBottom + (sCharHeight / 2); + WinFillRect( hpsCalc, &rcl, CLR_BLACK); } - WinReleasePS(hps); } -/********************************************************************** - Display helpful info - */ +/******************************************************************************/ +/* Display helpful info */ +/******************************************************************************/ +MPARAM CALLBACK +AboutDlgProc( hwnd, msg, mp1, mp2) -MPARAM EXPENTRY AboutDlgProc(hwnd, msg, mp1, mp2) -HWND hwnd; +HWND hwnd; USHORT msg; MPARAM mp1; MPARAM mp2; @@ -160,70 +189,80 @@ MPARAM mp2; WinDismissDlg(hwnd, TRUE); return(MPFROMSHORT(TRUE)); } - else return(WinDefDlgProc(hwnd, msg, mp1, mp2)); + else return(WinDefDlgProc( hwnd, msg, mp1, mp2)); } -/********************************************************************** - General initialization - */ - -BOOL CalcInit() +/******************************************************************************/ +/* General initialization */ +/******************************************************************************/ +BOOL +CalcInit() { - hab = WinInitialize(0); + hab = WinInitialize( NULL); hmqCalc = WinCreateMsgQueue( hab, 0); - - if (!WinRegisterClass( hab, szCalcClass, CalcWndProc, CS_SIZEREDRAW, 0)) + if( !hmqCalc) return(FALSE); - hptrFinger = WinLoadPointer(HWND_DESKTOP, (HMODULE)NULL, IDP_FINGER); + WinLoadString( NULL, NULL, 1, 30, (PSZ)szTitle); + WinLoadString( NULL, NULL, 2, 20, (PSZ)szErrorString); + WinLoadString( NULL, NULL, 3, 2, (PSZ)szPlusMinus); + + if (!WinRegisterClass( hab, szTitle, CalcWndProc, CS_SIZEREDRAW, 0)) + return(FALSE); - WinLoadString(NULL, NULL, 1, 30, (PSZ)szTitle); - WinLoadString(NULL, NULL, 2, 20, (PSZ)szErrorString); - WinLoadString(NULL, NULL, 3, 2, (PSZ)szPlusMinus); + hptrFinger = WinLoadPointer( HWND_DESKTOP, (HMODULE)NULL, IDP_FINGER); InitCalc(); /* arithmetic initialization */ return(TRUE); } -/********************************************************************** - main procedure - */ - -VOID cdecl main() +/******************************************************************************/ +/* main procedure */ +/******************************************************************************/ +VOID cdecl +main() { - QMSG qmsg; - ULONG ctlData; + QMSG qmsg; + ULONG ulFCF; if (!CalcInit()) { /* general initialization */ - WinAlarm(HWND_DESKTOP, 0xffff); + WinAlarm(HWND_DESKTOP, WA_ERROR); goto exit; } if (!PSInit()) { /* presentation spaces & bitmaps */ - WinAlarm(HWND_DESKTOP, 0xffff); + WinAlarm(HWND_DESKTOP, WA_ERROR); goto exit; } - ctlData = FCF_TITLEBAR | FCF_MINBUTTON | FCF_SYSMENU | FCF_MENU; - hwndCalcFrame = WinCreateStdWindow(HWND_DESKTOP, - FS_ACCELTABLE | FS_BORDER | FS_ICON, - &ctlData, szCalcClass, szTitle, 0L, - (HMODULE)NULL, IDR_CALC, - (HWND FAR *)&hwndCalc); - - WinSetWindowPos(hwndCalcFrame, (HWND)NULL, 2, 2, CXCHARS * charwidth, - CYCHARS * charheight + - (SHORT)WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR) + - (SHORT)WinQuerySysValue(HWND_DESKTOP, SV_CYMENU), - SWP_MOVE | SWP_SIZE); - WinShowWindow(hwndCalcFrame, TRUE); - WinSetFocus(HWND_DESKTOP, hwndCalc); + ulFCF = FCF_STANDARD & ~(LONG)(FCF_SIZEBORDER | FCF_MAXBUTTON); + hwndCalcFrame = WinCreateStdWindow( HWND_DESKTOP + , WS_VISIBLE | FS_BORDER + , &ulFCF + , szTitle + , NULL + , 0L + , NULL + , IDR_CALC + , &hwndCalc); + + WinSetWindowPos( hwndCalcFrame + , (HWND)NULL + , 2 + , 2 + , CXCHARS * sCharWidth + , CYCHARS * sCharHeight + + (SHORT)WinQuerySysValue( HWND_DESKTOP + , SV_CYTITLEBAR ) + + (SHORT)WinQuerySysValue( HWND_DESKTOP + , SV_CYMENU ) + , SWP_MOVE | SWP_SIZE ); - while (WinGetMsg( hab, (PQMSG)&qmsg, NULL, 0, 0)) - WinDispatchMsg( hab, (PQMSG)&qmsg); + while (WinGetMsg( hab, &qmsg, NULL, 0, 0)) + WinDispatchMsg( hab, &qmsg); exit: /* clean up */ if (hdcSqr) /* square-root bitmap */ @@ -249,27 +288,36 @@ exit: /* clean up */ } -/************************************************************************* - Calc Window Procedure - */ - -MRESULT EXPENTRY CalcWndProc(hwnd, msg, mp1, mp2) -HWND hwnd; -USHORT msg; -MPARAM mp1; -MPARAM mp2; -{ - HPS hps; - RECTL rclPaint; - WPOINT pt; - BOOL fClip; - USHORT fi; +/******************************************************************************/ +/* Calc Window Procedure */ +/******************************************************************************/ +MRESULT CALLBACK +CalcWndProc(hwnd, msg, mp1, mp2) + +HWND hwnd; +USHORT msg; +MPARAM mp1; +MPARAM mp2; +{ + HPS hps; + RECTL rclPaint; + WPOINT wpt; + BOOL fClip; + USHORT usFmtInfo; + RECTL rcl; + SIZEL sizl; switch (msg) { case WM_CREATE: - /* Set up this global first thing in case we need it elsewhere */ - hwndCalc = hwnd; + hdcCalc = WinOpenWindowDC( hwnd); + WinQueryWindowRect( hwnd, &rcl); + sizl.cx = rcl.xRight - rcl.xLeft; + sizl.cy = rcl.yTop - rcl.yBottom; + hpsCalc = GpiCreatePS( hab + , hdcCalc + , &sizl + , GPIA_ASSOC | PU_PELS ); break; case WM_DESTROY: @@ -282,10 +330,10 @@ MPARAM mp2; case WM_INITMENU: fClip = FALSE; - if (WinOpenClipbrd(NULL)) + if (WinOpenClipbrd( hab)) { - fClip = WinQueryClipbrdFmtInfo(NULL, CF_TEXT, (USHORT FAR *)&fi); - WinCloseClipbrd(NULL); + fClip = WinQueryClipbrdFmtInfo( hab, CF_TEXT, &usFmtInfo); + WinCloseClipbrd( hab); } WinSendMsg((HWND)mp2, MM_SETITEMATTR, (MPARAM) MAKELONG(CMD_PASTE, TRUE), @@ -310,30 +358,34 @@ MPARAM mp2; DataXPaste(); /* paste from clipboard */ break; case CMD_EXIT: - WinPostMsg(hwndCalcFrame, WM_QUIT, 0L, 0L); + WinPostMsg( hwndCalcFrame, WM_QUIT, 0L, 0L); break; case CMD_ABOUT: - WinDlgBox(HWND_DESKTOP, hwndCalcFrame, (PFNWP)AboutDlgProc, NULL, - 1, (PSZ)NULL); + WinDlgBox( HWND_DESKTOP + , hwndCalcFrame + , (PFNWP)AboutDlgProc + , NULL + , 1 + , (PSZ)NULL ); break; } break; case WM_MOUSEMOVE: - WinSetPointer(HWND_DESKTOP, hptrFinger); + WinSetPointer( HWND_DESKTOP, hptrFinger); break; case WM_BUTTON1DOWN: - pt.x = LOUSHORT(mp1); - pt.y = HIUSHORT(mp1); - ProcessKey(hwndCalc, &pt); + wpt.x = LOUSHORT(mp1); + wpt.y = HIUSHORT(mp1); + ProcessKey( &wpt); goto dwp; break; case WM_CHAR: if (SHORT1FROMMP(mp1) & KC_KEYUP) { - if (CHAR4FROMMP(mp1) == mScan) + if (CHAR4FROMMP(mp1) == uchMScan) fMDown = FALSE; /* 'm' key went up */ } else if (SHORT1FROMMP(mp1) & KC_CHAR) @@ -342,15 +394,15 @@ MPARAM mp2; UpdateDisplay(); else if ((CHAR1FROMMP(mp2)=='m') || (CHAR1FROMMP(mp2)=='M')) { - mScan = CHAR4FROMMP(mp1); /* save 'm' key scan code */ - fMDown = TRUE; /* 'm' key went down */ + uchMScan = CHAR4FROMMP(mp1); /* save 'm' key scan code */ + fMDown = TRUE; /* 'm' key went down */ } } break; case WM_ACTIVATE: if (HIUSHORT(mp1)) - WinSetFocus(HWND_DESKTOP, hwndCalc); + WinSetFocus( HWND_DESKTOP, hwndCalc); break; case WM_SETFOCUS: @@ -367,20 +419,20 @@ dwp: } -/************************************************************************* - translate & interpret keys (ie. locate in logical keyboard) - */ +/******************************************************************************/ +/* translate & interpret keys (ie. locate in logical keyboard) */ +/******************************************************************************/ +BOOL +InterpretChar( ch) -BOOL InterpretChar(ch) -register CHAR ch; +CHAR ch; { BOOL fDone; - CHAR *chstep; - register int n; - HPS hps; + NPCH pchStep; + INT i; fDone = FALSE; - chstep = keys; + pchStep = achKeys; switch (ch) { case 'n': @@ -415,257 +467,276 @@ register CHAR ch; } } - while (!fDone && *chstep) + while (!fDone && *pchStep) { - if (*chstep++ == ch) + if (*pchStep++ == ch) fDone = TRUE; /* char found in logical keyboard */ } if (fDone) { - lastkey = currkey; - n = chstep - keys - 1; - hps = WinGetPS(hwndCalc); - FlipKey(hps, n/6, n%6); - WinReleasePS(hps); - Evaluate(keys[n]); + chLastKey = chCurrKey; + i = pchStep - achKeys - 1; + FlipKey( hpsCalc, i/6, i%6); + Evaluate( achKeys[i]); } return (fDone); } -/************************************************************************* - briefly reverse the shading on one of the keys - */ +/******************************************************************************/ +/* briefly reverse the shading on one of the keys */ +/******************************************************************************/ +VOID +FlipKey( hps, iRow, iCol) -VOID FlipKey(hps, i, j) HPS hps; -int i,j; +INT iRow, iCol; { RECTL rcl; - rcl.xLeft = (j * 6 * charwidth) + (14 * charwidth / 10); - rcl.yBottom = (165 * charheight / 100) + (2 * i * charheight); - rcl.xRight = rcl.xLeft + (11 * charwidth / 3); - rcl.yTop = rcl.yBottom + (7 * charheight / 4); - WinInvertRect(hps, &rcl); - DosSleep(50L); - WinInvertRect(hps, &rcl); + rcl.xLeft = (iCol * 6 * sCharWidth) + (14 * sCharWidth / 10); + rcl.yBottom = (165 * sCharHeight / 100) + (2 * iRow * sCharHeight); + rcl.xRight = rcl.xLeft + (11 * sCharWidth / 3); + rcl.yTop = rcl.yBottom + (7 * sCharHeight / 4); + WinInvertRect( hps, &rcl); + DosSleep( 50L); + WinInvertRect( hps, &rcl); } -/************************************************************************* - compute whether a point is over a button and flash the button if so - */ +/******************************************************************************/ +/* compute whether a point is over a button and flash the button if so */ +/******************************************************************************/ +BOOL +FlashSqr( hps, pwpt) -BOOL FlashSqr(hps, ppt) -HPS hps; -NPWPOINT ppt; +HPS hps; +PWPOINT pwpt; { - register int i; - register int j; + INT iRow, iCol; BOOL fDone; /* find x range */ fDone = FALSE; - j = 0; - i = 3; - while (!fDone && j<6) - { - if (ppt->x < (j * 6 * charwidth) + (14 * charwidth / 10) + - (11*charwidth/3)) + iCol = 0; + iRow = 3; + while (!fDone && iCol<6) + { + if (pwpt->x < (iCol * 6 * sCharWidth) + + (14 * sCharWidth / 10) + + (11*sCharWidth/3) ) { - if (ppt->x > (j * 6 * charwidth) + (14 * charwidth / 10)) + if (pwpt->x > (iCol * 6 * sCharWidth) + (14 * sCharWidth / 10)) fDone = TRUE; else return FALSE; } else - j++; + iCol++; } if (!fDone) return FALSE; fDone = FALSE; - while (!fDone && i >= 0) + while (!fDone && iRow >= 0) { - if (ppt->y > ((165 * charheight / 100) + (2 * i * charheight))) + if (pwpt->y > ((165 * sCharHeight / 100) + (2 * iRow * sCharHeight))) { - if (ppt->y < ((165 * charheight / 100) + (2 * i * charheight) + - (7 * charheight / 4))) + if (pwpt->y < (165 * sCharHeight / 100) + + (2 * iRow * sCharHeight) + + (7 * sCharHeight / 4) ) fDone = TRUE; else return FALSE; } else - i--; + iRow--; } if (!fDone) return FALSE; - ppt->x = i; - ppt->y = j; - FlipKey(hps, i, j); + pwpt->x = iCol; + pwpt->y = iRow; + FlipKey( hps, iRow, iCol); return TRUE; } -/************************************************************************* - which key is point on? - */ +/******************************************************************************/ +/* which key is point on? */ +/******************************************************************************/ +CHAR +Translate( pwpt) -char Translate(ppt) -WPOINT *ppt; +PWPOINT pwpt; { - register int offset; - register char result; - - offset = ppt->x * 6 + ppt->y; - result = keys[offset]; - return result; + return( achKeys[ pwpt->y * 6 + pwpt->x]); } -/************************************************************************* - invoke flashing, point-to-key translation, and result-display update - */ +/******************************************************************************/ +/* invoke flashing, point-to-key translation, and result-display update */ +/******************************************************************************/ +VOID +ProcessKey( pwpt) -VOID ProcessKey(hwnd, ppt) -HWND hwnd; -WPOINT *ppt; +PWPOINT pwpt; { - HPS hps; - register BOOL fFlashed; + BOOL fFlashed; - hps = WinGetPS(hwnd); - lastkey = currkey; - fFlashed = FlashSqr(hps, ppt); - WinReleasePS(hps); + chLastKey = chCurrKey; + fFlashed = FlashSqr( hpsCalc, pwpt); if (fFlashed) - Evaluate((BYTE)Translate(ppt)); + Evaluate( (BYTE)Translate( pwpt)); UpdateDisplay(); } -/************************************************************************* - draw a blank key - */ +/******************************************************************************/ +/* draw a blank key */ +/******************************************************************************/ +VOID +FrameKey(hps, iRow, iCol) -VOID FrameKey(hps, i, j) HPS hps; -int i, j; +INT iRow, iCol; { POINTL aptl[3]; - aptl[0].x = (j * 6 * charwidth) + (14 * charwidth / 10); - aptl[0].y = (165 * charheight / 100) + (2 * i * charheight); - aptl[1].x = (11 * charwidth / 3) + (aptl[0].x); - aptl[1].y = (7 * charheight / 4) + (aptl[0].y); + aptl[0].x = (iCol * 6 * sCharWidth) + (14 * sCharWidth / 10); + aptl[0].y = (165 * sCharHeight / 100) + (2 * iRow * sCharHeight); + aptl[1].x = (11 * sCharWidth / 3) + (aptl[0].x); + aptl[1].y = (7 * sCharHeight / 4) + (aptl[0].y); aptl[2].x = 0; aptl[2].y = 0; GpiBitBlt( hps, hpsLocal, 3L, aptl, ROP_SRCCOPY, BBO_IGNORE); } -/************************************************************************* - draw the keys and fill in numbers - */ +/******************************************************************************/ +/* draw the keys and fill in numbers */ +/******************************************************************************/ +VOID +DrawNumbers(hps) -VOID DrawNumbers(hps) HPS hps; { - register int i,j; + INT iRow, iCol; /* Draw the keys and fill in the numbers we can */ - for (i = 0; i < 4; i++) + for (iRow = 0; iRow < 4; iRow++) { - for (j = 0; j < 6; j++) + for (iCol = 0; iCol < 6; iCol++) { - FrameKey(hps, i, j); - CalcTextOut(hps, - (j * 6 * charwidth) + (WIDTHCONST * charwidth / 10), - (charheight) + ( i * 2 * charheight) + (charheight), - (PSZ)((char *)dkeys + (i * 6) + j), 1); + FrameKey( hps, iRow, iCol); + CalcTextOut( hps + , (iCol * 6 * sCharWidth) + + (WIDTHCONST * sCharWidth / 10) + , (iRow + 1) * 2 * sCharHeight + , (PSZ)(achDKeys + (iRow * 6) + iCol) + , 1 ); } } } -/************************************************************************* - redraw the whole calculator - */ +/******************************************************************************/ +/* redraw the whole calculator */ +/******************************************************************************/ +VOID +CalcPaint( hwnd, hps) -VOID CalcPaint( hwnd, hps) HWND hwnd; -HPS hps; +HPS hps; { - RECTL rclDst; + RECTL rclDst; CHARBUNDLE cbnd; - register int x1, y1; + INT iX, iY; WinQueryWindowRect( hwnd, &rclDst); WinFillRect( hps, &rclDst, CLR_GREEN); DrawNumbers(hps); - CalcTextOut(hps, x1 = (11 * charwidth / 5) + 1, y1 = 2 * charheight, + CalcTextOut(hps, iX = (11 * sCharWidth / 5) + 1, iY = 2 * sCharHeight, (PSZ)"M-", 2); - CalcTextOut(hps, x1, y1 + 2 * charheight, (PSZ)"M+", 2); - CalcTextOut(hps, x1, y1 + 4 * charheight, (PSZ)"MR", 2); - CalcTextOut(hps, x1, y1 + 6 * charheight, (PSZ)"MC", 2); + CalcTextOut(hps, iX, iY + 2 * sCharHeight, (PSZ)"M+", 2); + CalcTextOut(hps, iX, iY + 4 * sCharHeight, (PSZ)"MR", 2); + CalcTextOut(hps, iX, iY + 6 * sCharHeight, (PSZ)"MC", 2); /* Draw the minus of the plus/minus button */ cbnd.usBackMixMode = FM_LEAVEALONE; GpiSetAttrs( hps, PRIM_CHAR, CBB_BACK_MIX_MODE, 0L, &cbnd); - x1 = (3 * 6 * charwidth) + (WIDTHCONST * charwidth / 10); - CalcTextOut( hps, x1, y1 - charheight / 3, (PSZ)"-", 1); + iX = (3 * 6 * sCharWidth) + (WIDTHCONST * sCharWidth / 10); + CalcTextOut( hps, iX, iY + sCharHeight / 4, (PSZ)"_", 1); /* Draw the square root bitmap */ - rclDst.xLeft = 160 * charwidth / 5; - rclDst.yBottom = 31 * charheight / 4; - rclDst.xRight = rclDst.xLeft + 2 * charwidth; - rclDst.yTop = rclDst.yBottom + (3 * charheight / 2); - WinDrawBitmap( hps, hbmSqr, NULL, (PPOINTL)&rclDst, CLR_WHITE, CLR_BLACK, DBM_STRETCH); + rclDst.xLeft = 160 * sCharWidth / 5; + rclDst.yBottom = 31 * sCharHeight / 4; + rclDst.xRight = rclDst.xLeft + 2 * sCharWidth; + rclDst.yTop = rclDst.yBottom + (3 * sCharHeight / 2); + WinDrawBitmap( hps + , hbmSqr + , NULL + , (PPOINTL)&rclDst + , CLR_WHITE + , CLR_BLACK + , DBM_STRETCH ); UpdateDisplay(); } -/************************************************************************* - initialize the bitmaps for a blank key and for the square-root sign - */ -BOOL PSInit() -{ - HPS hps; - FONTMETRICS fntmet; - POINTL ptl; - SIZEL sizl; +/******************************************************************************/ +/* initialize the bitmaps for a blank key and for the square-root sign */ +/******************************************************************************/ +BOOL +PSInit() +{ + HPS hps; + FONTMETRICS fm; + POINTL ptl; + SIZEL sizl; BITMAPINFOHEADER bmp; - POINTL aptl[4]; - LONG alCaps[2]; + POINTL aptl[4]; + LONG alCaps[2]; + /**************************************************************************/ + /* compute the units of horizontal and vertical distance based on font */ + /**************************************************************************/ hps = WinGetPS( HWND_DESKTOP); - GpiQueryFontMetrics( hps, (LONG)sizeof(FONTMETRICS), &fntmet); - charheight = (SHORT)(fntmet.lEmHeight); - charwidth = (SHORT)(fntmet.lAveCharWidth); + GpiQueryFontMetrics( hps, (LONG)sizeof(FONTMETRICS), &fm); + sCharHeight = (SHORT)(fm.lEmHeight); /* avg height of uppercase character */ + sCharWidth = (SHORT)(fm.lEmInc); /* usually 'M' increment */ WinReleasePS( hps); - hdcLocal = DevOpenDC( hab, OD_MEMORY, "*", 3L, (PDEVOPENDATA)&dop, NULL); - if( !hdcLocal) - return(FALSE); - - sizl.cx = sizl.cy = 0L; - hpsLocal = GpiCreatePS( hab, hdcLocal, &sizl, PU_PELS | GPIT_MICRO - | GPIA_ASSOC); - + /**************************************************************************/ + /* prepare the square root bitmap */ + /**************************************************************************/ hdcSqr = DevOpenDC( hab, OD_MEMORY, "*", 3L, (PDEVOPENDATA)&dop, NULL); if( !hdcSqr) return(FALSE); sizl.cx = sizl.cy = 0L; - hpsSqr = GpiCreatePS( hab, hdcSqr, &sizl, PU_PELS | GPIT_MICRO - | GPIA_ASSOC); + hpsSqr = GpiCreatePS( hab + , hdcSqr + , &sizl + , PU_PELS | GPIT_MICRO | GPIA_ASSOC ); hbmSqr = GpiLoadBitmap( hpsSqr, NULL, IDB_SQR, 0L, 0L); - bmp.cx = 11 * charwidth / 3; - bmp.cy = charheight * 2; + /**************************************************************************/ + /* prepare the bitmap of a blank key */ + /**************************************************************************/ + hdcLocal = DevOpenDC( hab, OD_MEMORY, "*", 3L, (PDEVOPENDATA)&dop, NULL); + if( !hdcLocal) + return(FALSE); + + sizl.cx = sizl.cy = 0L; + hpsLocal = GpiCreatePS( hab + , hdcLocal + , &sizl + , PU_PELS | GPIT_MICRO | GPIA_ASSOC ); + bmp.cbFix = 12; + bmp.cx = 11 * sCharWidth / 3; + bmp.cy = sCharHeight * 2; DevQueryCaps( hdcLocal, CAPS_COLOR_PLANES, 2L, alCaps); bmp.cPlanes = (USHORT)alCaps[0]; bmp.cBitCount = (USHORT)alCaps[1]; @@ -674,32 +745,37 @@ BOOL PSInit() return(FALSE); GpiSetBitmap( hpsLocal, hbmLocal); - /* Fill in the DC with CALC's background color */ aptl[0].x = aptl[0].y = 0; - aptl[1].x = 11 * charwidth / 3; - aptl[1].y = 7 * charheight / 4; + aptl[1].x = 11 * sCharWidth / 3; + aptl[1].y = 7 * sCharHeight / 4; aptl[2].x = aptl[2].y = 0; aptl[3].x = aptl[1].x; aptl[3].y = aptl[1].y; - GpiSetColor( hpsLocal, CLR_GREEN); + GpiSetColor( hpsLocal, CLR_GREEN); /* match the background to client */ GpiBitBlt( hpsLocal, NULL, 2L, aptl, ROP_PATCOPY, BBO_IGNORE); - /* Draw the rounded rect in the local PS */ + /* Draw the rounded rect */ ptl.x = 0; ptl.y = 0; GpiSetCurrentPosition( hpsLocal, &ptl); - ptl.x = (11 * charwidth / 3) - 1; - ptl.y = (7 * charheight / 4) - 1; - GpiSetColor( hpsLocal, CLR_WHITE); - GpiBox( hpsLocal, DRO_FILL, &ptl, (LONG)charwidth, - (LONG)(charheight/2)); + ptl.x = (11 * sCharWidth / 3) - 1; + ptl.y = (7 * sCharHeight / 4) - 1; + GpiSetColor( hpsLocal, CLR_WHITE); /* white interior */ + GpiBox( hpsLocal + , DRO_FILL + , &ptl + , (LONG)sCharWidth + , (LONG)(sCharHeight / 2) ); ptl.x = 0; ptl.y = 0; GpiSetCurrentPosition( hpsLocal, &ptl); - ptl.x = (11 * charwidth / 3) - 1; - ptl.y = (7 * charheight / 4) - 1; - GpiSetColor( hpsLocal, CLR_BLACK); - GpiBox( hpsLocal, DRO_OUTLINE, &ptl, (LONG)charwidth, - (LONG)(charheight/2)); + ptl.x = (11 * sCharWidth / 3) - 1; + ptl.y = (7 * sCharHeight / 4) - 1; + GpiSetColor( hpsLocal, CLR_BLACK); /* black border */ + GpiBox( hpsLocal + , DRO_OUTLINE + , &ptl + , (LONG)sCharWidth + , (LONG)(sCharHeight / 2) ); return( TRUE); }