File:  [OS/2 SDKs] / pmsdk / samples / petzold / chap05 / keylook.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:28:13 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: pmsdk-1988, HEAD
Microsoft OS/2 SDK PM 08-08-1988

/*----------------------------------------
   KEYLOOK.C -- Displays WM_CHAR Messages
  ----------------------------------------*/

#define INCL_WIN
#define INCL_GPI

#include <os2.h>
#include <stdio.h>

#define MAX_KEYS 100

MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;

int main (void)
     {
     static CHAR szClientClass [] = "KeyLook" ;
     HAB         hab ;
     HMQ         hmq ;
     HWND        hwndFrame, hwndClient ;
     QMSG        qmsg ;
     ULONG       flFrameFlags = FCF_STANDARD & ~FCF_MENU ;
     ULONG       flFrameStyle = WS_VISIBLE ;

     hab = WinInitialize (0) ;
     hmq = WinCreateMsgQueue (hab, 0) ;

     WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;

     hwndFrame = WinCreateStdWindow (HWND_DESKTOP, flFrameStyle,
                                     &flFrameFlags, szClientClass,
                                     "WM_CHAR Messages",
				     0L, NULL, 0, &hwndClient) ;

     while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
          WinDispatchMsg (hab, &qmsg) ;

     WinDestroyWindow (hwndFrame) ;
     WinDestroyMsgQueue (hmq) ;
     WinTerminate (hab) ;

     return 0 ;
     }

MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
     {
     static CHAR   szHeader [] = "Scan  Rept  IN TG IC CM DK LK PD KU"
                                 " AL CO SH SC VK CH  Virt  Char" ;
     static CHAR   szUndrLn [] = "----  ----  -- -- -- -- -- -- -- --"
                                 " -- -- -- -- -- --  ----  ----" ;
     static CHAR   szFormat [] = "%4X %4dx  %2d %2d %2d %2d %2d %2d %2d %2d"
                                 " %2d %2d %2d %2d %2d %2d  %4X  %4X  %c" ;

     static SHORT  cxChar, cyChar, cyDesc, cxClient, cyClient, sNextKey ;
     static struct {
                   MPARAM mp1 ;
                   MPARAM mp2 ;
                   BOOL   fValid ;
                   }
                   key [MAX_KEYS] ;
     CHAR          szBuffer [80] ;
     FONTMETRICS   fm ;
     HPS           hps ;
     POINTL        ptl ;
     RECTL         rcl, rclInvalid ;
     SHORT         sKey, sIndex, sFlag ;

     switch (msg)
          {
          case WM_CREATE:
               hps = WinGetPS (hwnd) ;
               GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
               cxChar = (SHORT) fm.lAveCharWidth ;
               cyChar = (SHORT) fm.lMaxBaselineExt ;
               cyDesc = (SHORT) fm.lMaxDescender ;
               WinReleasePS (hps) ;
               return 0 ;

          case WM_SIZE:
               cxClient = SHORT1FROMMP (mp2) ;
               cyClient = SHORT2FROMMP (mp2) ;
               return 0 ;

          case WM_CHAR:
               key [sNextKey].mp1 = mp1 ;
               key [sNextKey].mp2 = mp2 ;
               key [sNextKey].fValid = TRUE ;

               sNextKey = (sNextKey + 1) % MAX_KEYS ;

               WinSetRect (hwnd, &rcl, 0, 0, cxClient, cyClient - 2 * cyChar) ;

               WinScrollWindow (hwnd, 0, cyChar, &rcl, &rcl, NULL, NULL,
                                                       SW_INVALIDATERGN) ;
               WinUpdateWindow (hwnd) ;
               return 1 ;

          case WM_PAINT:
               hps = WinBeginPaint (hwnd, NULL, &rclInvalid) ;
               GpiErase (hps) ;

               ptl.x = cxChar ;
               ptl.y = cyDesc - cyChar ;

               for (sKey = 0 ; sKey < MAX_KEYS ; sKey++)
                    {
                    ptl.y += cyChar ;

                    sIndex = (sNextKey - sKey - 1 + MAX_KEYS) % MAX_KEYS ;

                    if (ptl.y > rclInvalid.yTop ||
                              ptl.y > cyClient - 2 * cyChar ||
                                   !key [sIndex].fValid)
                         break ;
                         
                    mp1 = key [sIndex].mp1 ;
                    mp2 = key [sIndex].mp2 ;

                    sFlag = CHARMSG(&msg)->fs ;

                    GpiCharStringAt (hps, &ptl, 
                         (LONG) sprintf (szBuffer, szFormat,
                                   CHARMSG(&msg)->scancode,
                                   CHARMSG(&msg)->cRepeat,
                                   sFlag & KC_INVALIDCHAR ? 1 : 0,
                                   sFlag & KC_TOGGLE      ? 1 : 0,
                                   sFlag & KC_INVALIDCOMP ? 1 : 0,
                                   sFlag & KC_COMPOSITE   ? 1 : 0,
                                   sFlag & KC_DEADKEY     ? 1 : 0,
                                   sFlag & KC_LONEKEY     ? 1 : 0,
                                   sFlag & KC_PREVDOWN    ? 1 : 0,
                                   sFlag & KC_KEYUP       ? 1 : 0,
                                   sFlag & KC_ALT         ? 1 : 0,
                                   sFlag & KC_CTRL        ? 1 : 0,
                                   sFlag & KC_SHIFT       ? 1 : 0,
                                   sFlag & KC_SCANCODE    ? 1 : 0,
                                   sFlag & KC_VIRTUALKEY  ? 1 : 0,
                                   sFlag & KC_CHAR        ? 1 : 0,
                                   CHARMSG(&msg)->vkey,
                                   CHARMSG(&msg)->chr,
                                   CHARMSG(&msg)->chr),
                              szBuffer) ;
                    }
               ptl.y = cyClient - cyChar + cyDesc ;
               GpiCharStringAt (hps, &ptl, sizeof szHeader - 1L, szHeader) ;

               ptl.y -= cyChar ;
               GpiCharStringAt (hps, &ptl, sizeof szUndrLn - 1L, szUndrLn) ;

               WinEndPaint (hps) ;
               return 0 ;
          }
     return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
     }

unix.superglobalmegacorp.com

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