Annotation of pmsdk/samples/petzold/chap05/keylook.c, revision 1.1.1.1

1.1       root        1: /*----------------------------------------
                      2:    KEYLOOK.C -- Displays WM_CHAR Messages
                      3:   ----------------------------------------*/
                      4: 
                      5: #define INCL_WIN
                      6: #define INCL_GPI
                      7: 
                      8: #include <os2.h>
                      9: #include <stdio.h>
                     10: 
                     11: #define MAX_KEYS 100
                     12: 
                     13: MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
                     14: 
                     15: int main (void)
                     16:      {
                     17:      static CHAR szClientClass [] = "KeyLook" ;
                     18:      HAB         hab ;
                     19:      HMQ         hmq ;
                     20:      HWND        hwndFrame, hwndClient ;
                     21:      QMSG        qmsg ;
                     22:      ULONG       flFrameFlags = FCF_STANDARD & ~FCF_MENU ;
                     23:      ULONG       flFrameStyle = WS_VISIBLE ;
                     24: 
                     25:      hab = WinInitialize (0) ;
                     26:      hmq = WinCreateMsgQueue (hab, 0) ;
                     27: 
                     28:      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
                     29: 
                     30:      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, flFrameStyle,
                     31:                                      &flFrameFlags, szClientClass,
                     32:                                      "WM_CHAR Messages",
                     33:                                     0L, NULL, 0, &hwndClient) ;
                     34: 
                     35:      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
                     36:           WinDispatchMsg (hab, &qmsg) ;
                     37: 
                     38:      WinDestroyWindow (hwndFrame) ;
                     39:      WinDestroyMsgQueue (hmq) ;
                     40:      WinTerminate (hab) ;
                     41: 
                     42:      return 0 ;
                     43:      }
                     44: 
                     45: MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                     46:      {
                     47:      static CHAR   szHeader [] = "Scan  Rept  IN TG IC CM DK LK PD KU"
                     48:                                  " AL CO SH SC VK CH  Virt  Char" ;
                     49:      static CHAR   szUndrLn [] = "----  ----  -- -- -- -- -- -- -- --"
                     50:                                  " -- -- -- -- -- --  ----  ----" ;
                     51:      static CHAR   szFormat [] = "%4X %4dx  %2d %2d %2d %2d %2d %2d %2d %2d"
                     52:                                  " %2d %2d %2d %2d %2d %2d  %4X  %4X  %c" ;
                     53: 
                     54:      static SHORT  cxChar, cyChar, cyDesc, cxClient, cyClient, sNextKey ;
                     55:      static struct {
                     56:                    MPARAM mp1 ;
                     57:                    MPARAM mp2 ;
                     58:                    BOOL   fValid ;
                     59:                    }
                     60:                    key [MAX_KEYS] ;
                     61:      CHAR          szBuffer [80] ;
                     62:      FONTMETRICS   fm ;
                     63:      HPS           hps ;
                     64:      POINTL        ptl ;
                     65:      RECTL         rcl, rclInvalid ;
                     66:      SHORT         sKey, sIndex, sFlag ;
                     67: 
                     68:      switch (msg)
                     69:           {
                     70:           case WM_CREATE:
                     71:                hps = WinGetPS (hwnd) ;
                     72:                GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
                     73:                cxChar = (SHORT) fm.lAveCharWidth ;
                     74:                cyChar = (SHORT) fm.lMaxBaselineExt ;
                     75:                cyDesc = (SHORT) fm.lMaxDescender ;
                     76:                WinReleasePS (hps) ;
                     77:                return 0 ;
                     78: 
                     79:           case WM_SIZE:
                     80:                cxClient = SHORT1FROMMP (mp2) ;
                     81:                cyClient = SHORT2FROMMP (mp2) ;
                     82:                return 0 ;
                     83: 
                     84:           case WM_CHAR:
                     85:                key [sNextKey].mp1 = mp1 ;
                     86:                key [sNextKey].mp2 = mp2 ;
                     87:                key [sNextKey].fValid = TRUE ;
                     88: 
                     89:                sNextKey = (sNextKey + 1) % MAX_KEYS ;
                     90: 
                     91:                WinSetRect (hwnd, &rcl, 0, 0, cxClient, cyClient - 2 * cyChar) ;
                     92: 
                     93:                WinScrollWindow (hwnd, 0, cyChar, &rcl, &rcl, NULL, NULL,
                     94:                                                        SW_INVALIDATERGN) ;
                     95:                WinUpdateWindow (hwnd) ;
                     96:                return 1 ;
                     97: 
                     98:           case WM_PAINT:
                     99:                hps = WinBeginPaint (hwnd, NULL, &rclInvalid) ;
                    100:                GpiErase (hps) ;
                    101: 
                    102:                ptl.x = cxChar ;
                    103:                ptl.y = cyDesc - cyChar ;
                    104: 
                    105:                for (sKey = 0 ; sKey < MAX_KEYS ; sKey++)
                    106:                     {
                    107:                     ptl.y += cyChar ;
                    108: 
                    109:                     sIndex = (sNextKey - sKey - 1 + MAX_KEYS) % MAX_KEYS ;
                    110: 
                    111:                     if (ptl.y > rclInvalid.yTop ||
                    112:                               ptl.y > cyClient - 2 * cyChar ||
                    113:                                    !key [sIndex].fValid)
                    114:                          break ;
                    115:                          
                    116:                     mp1 = key [sIndex].mp1 ;
                    117:                     mp2 = key [sIndex].mp2 ;
                    118: 
                    119:                     sFlag = CHARMSG(&msg)->fs ;
                    120: 
                    121:                     GpiCharStringAt (hps, &ptl, 
                    122:                          (LONG) sprintf (szBuffer, szFormat,
                    123:                                    CHARMSG(&msg)->scancode,
                    124:                                    CHARMSG(&msg)->cRepeat,
                    125:                                    sFlag & KC_INVALIDCHAR ? 1 : 0,
                    126:                                    sFlag & KC_TOGGLE      ? 1 : 0,
                    127:                                    sFlag & KC_INVALIDCOMP ? 1 : 0,
                    128:                                    sFlag & KC_COMPOSITE   ? 1 : 0,
                    129:                                    sFlag & KC_DEADKEY     ? 1 : 0,
                    130:                                    sFlag & KC_LONEKEY     ? 1 : 0,
                    131:                                    sFlag & KC_PREVDOWN    ? 1 : 0,
                    132:                                    sFlag & KC_KEYUP       ? 1 : 0,
                    133:                                    sFlag & KC_ALT         ? 1 : 0,
                    134:                                    sFlag & KC_CTRL        ? 1 : 0,
                    135:                                    sFlag & KC_SHIFT       ? 1 : 0,
                    136:                                    sFlag & KC_SCANCODE    ? 1 : 0,
                    137:                                    sFlag & KC_VIRTUALKEY  ? 1 : 0,
                    138:                                    sFlag & KC_CHAR        ? 1 : 0,
                    139:                                    CHARMSG(&msg)->vkey,
                    140:                                    CHARMSG(&msg)->chr,
                    141:                                    CHARMSG(&msg)->chr),
                    142:                               szBuffer) ;
                    143:                     }
                    144:                ptl.y = cyClient - cyChar + cyDesc ;
                    145:                GpiCharStringAt (hps, &ptl, sizeof szHeader - 1L, szHeader) ;
                    146: 
                    147:                ptl.y -= cyChar ;
                    148:                GpiCharStringAt (hps, &ptl, sizeof szUndrLn - 1L, szUndrLn) ;
                    149: 
                    150:                WinEndPaint (hps) ;
                    151:                return 0 ;
                    152:           }
                    153:      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
                    154:      }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.