Annotation of pmsdk/samples/petzold/chap06/checker2.c, revision 1.1.1.1

1.1       root        1: /*-------------------------------------------------------------------
                      2:    CHECKER2.C -- Mouse Hit-Test Demo Program with Keyboard Interface
                      3:   -------------------------------------------------------------------*/
                      4: 
                      5: #define INCL_WIN
                      6: #define INCL_GPI
                      7: 
                      8: #include <os2.h>
                      9: #include <stddef.h>
                     10: #include <stdlib.h>
                     11: 
                     12: #define DIVISIONS 5
                     13: 
                     14: MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
                     15: 
                     16: int main (void)
                     17:      {
                     18:      static CHAR szClientClass [] = "Checker2" ;
                     19:      HAB         hab ;
                     20:      HMQ         hmq ;
                     21:      HWND        hwndFrame, hwndClient ;
                     22:      QMSG        qmsg ;
                     23:      ULONG       flFrameFlags = FCF_STANDARD & ~FCF_MENU ;
                     24:      ULONG       flFrameStyle = WS_VISIBLE ;
                     25: 
                     26:      hab = WinInitialize (0) ;
                     27:      hmq = WinCreateMsgQueue (hab, 0) ;
                     28: 
                     29:      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
                     30: 
                     31:      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, flFrameStyle,
                     32:                                      &flFrameFlags, szClientClass,
                     33:                                      "Mouse Hit-Test Program No. 2",
                     34:                                      0L, NULL, 0, &hwndClient) ;
                     35: 
                     36:      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
                     37:           WinDispatchMsg (hab, &qmsg) ;
                     38: 
                     39:      WinDestroyWindow (hwndFrame) ;
                     40:      WinDestroyMsgQueue (hmq) ;
                     41:      WinTerminate (hab) ;
                     42:      
                     43:      return 0 ;
                     44:      }
                     45: 
                     46: VOID DrawLine (HPS hps, LONG x1, LONG y1, LONG x2, LONG y2)
                     47:      {
                     48:      POINTL ptl ;
                     49: 
                     50:      ptl.x = x1 ;  ptl.y = y1 ;  GpiMove (hps, &ptl) ;
                     51:      ptl.x = x2 ;  ptl.y = y2 ;  GpiLine (hps, &ptl) ;
                     52:      }
                     53: 
                     54: MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                     55:      {
                     56:      static BOOL  bBlockState [DIVISIONS] [DIVISIONS] ;
                     57:      static SHORT xBlock, yBlock ;
                     58:      HPS          hps ;
                     59:      POINTL       ptl ;
                     60:      RECTL        rcl ;
                     61:      SHORT        x, y ;
                     62: 
                     63:      switch (msg)
                     64:           {
                     65:           case WM_SIZE:
                     66:                xBlock = SHORT1FROMMP (mp2) / DIVISIONS ;
                     67:                yBlock = SHORT2FROMMP (mp2) / DIVISIONS ;
                     68:                return 0 ;
                     69: 
                     70:           case WM_BUTTON1DOWN:
                     71:           case WM_BUTTON1DBLCLK:
                     72:                if (xBlock > 0 && yBlock > 0)
                     73:                     {
                     74:                     x = MOUSEMSG(&msg)->x / xBlock ;
                     75:                     y = MOUSEMSG(&msg)->y / yBlock ;
                     76: 
                     77:                     if (x < DIVISIONS && y < DIVISIONS)
                     78:                          {
                     79:                          bBlockState [x][y] = !bBlockState [x][y] ;
                     80: 
                     81:                          rcl.xRight = xBlock + (rcl.xLeft   = x * xBlock) ;
                     82:                          rcl.yTop   = yBlock + (rcl.yBottom = y * yBlock) ;
                     83: 
                     84:                          WinInvalidateRect (hwnd, &rcl, FALSE) ;
                     85:                          }
                     86:                     else
                     87:                          WinAlarm (HWND_DESKTOP, WA_WARNING) ;
                     88:                     }
                     89:                else
                     90:                     WinAlarm (HWND_DESKTOP, WA_WARNING) ;
                     91: 
                     92:                return 1 ;
                     93: 
                     94:           case WM_SETFOCUS:
                     95:                if (0L == WinQuerySysValue (HWND_DESKTOP, SV_CMOUSEBUTTONS))
                     96:                     WinShowPointer (HWND_DESKTOP,
                     97:                                     SHORT1FROMMP (mp2) ? TRUE : FALSE) ;
                     98:                return 0 ;
                     99: 
                    100:           case WM_CHAR:
                    101:                if (xBlock == 0 || yBlock == 0)
                    102:                     break ;
                    103: 
                    104:                if (CHARMSG(&msg)->fs & KC_KEYUP)
                    105:                     break ;
                    106: 
                    107:                if (!(CHARMSG(&msg)->fs & KC_VIRTUALKEY))
                    108:                     break ;
                    109: 
                    110:                WinQueryPointerPos (HWND_DESKTOP, &ptl) ;
                    111:                WinMapWindowPoints (HWND_DESKTOP, hwnd, &ptl, 1) ;
                    112: 
                    113:                x = max (0, min (DIVISIONS - 1, (SHORT) ptl.x / xBlock)) ;
                    114:                y = max (0, min (DIVISIONS - 1, (SHORT) ptl.y / yBlock)) ;
                    115: 
                    116:                switch (CHARMSG(&msg)->vkey)
                    117:                     {
                    118:                     case VK_LEFT:
                    119:                          x-- ;
                    120:                          break ;
                    121: 
                    122:                     case VK_RIGHT:
                    123:                          x++ ;
                    124:                          break ;
                    125: 
                    126:                     case VK_DOWN:
                    127:                          y-- ;
                    128:                          break ;
                    129: 
                    130:                     case VK_UP:
                    131:                          y++ ;
                    132:                          break ;
                    133: 
                    134:                     case VK_HOME:
                    135:                          x = 0 ;
                    136:                          y = DIVISIONS - 1 ;
                    137:                          break ;
                    138: 
                    139:                     case VK_END:
                    140:                          x = DIVISIONS - 1 ;
                    141:                          y = 0 ;
                    142:                          break ;
                    143: 
                    144:                     case VK_NEWLINE:
                    145:                    case VK_ENTER:
                    146:                     case VK_SPACE:
                    147:                          WinSendMsg (hwnd, WM_BUTTON1DOWN, 
                    148:                               MPFROM2SHORT (x * xBlock, y * yBlock), NULL) ;
                    149:                          break ;
                    150: 
                    151:                     default:
                    152:                          return 1 ;
                    153:                     }
                    154:                x = (x + DIVISIONS) % DIVISIONS ;
                    155:                y = (y + DIVISIONS) % DIVISIONS ;
                    156: 
                    157:                ptl.x = x * xBlock + xBlock / 2 ;
                    158:                ptl.y = y * yBlock + yBlock / 2 ;
                    159: 
                    160:                WinMapWindowPoints (hwnd, HWND_DESKTOP, &ptl, 1) ;
                    161:                WinSetPointerPos (HWND_DESKTOP, (SHORT) ptl.x, (SHORT) ptl.y) ;
                    162:                return 1 ;
                    163: 
                    164:           case WM_PAINT:
                    165:                hps = WinBeginPaint (hwnd, NULL, NULL) ;
                    166: 
                    167:                GpiErase (hps) ;
                    168: 
                    169:                if (xBlock > 0 && yBlock > 0)
                    170:                     for (x = 0 ; x < DIVISIONS ; x++)
                    171:                          for (y = 0 ; y < DIVISIONS ; y++)
                    172:                               {
                    173:                               rcl.xRight = xBlock + (rcl.xLeft   = x * xBlock);
                    174:                               rcl.yTop   = yBlock + (rcl.yBottom = y * yBlock);
                    175: 
                    176:                               WinDrawBorder (hps, &rcl, 1, 1,
                    177:                                              CLR_NEUTRAL, CLR_BACKGROUND,
                    178:                                              DB_STANDARD | DB_INTERIOR) ;
                    179: 
                    180:                               if (bBlockState [x][y])
                    181:                                    {
                    182:                                    DrawLine (hps, rcl.xLeft,  rcl.yBottom,
                    183:                                                   rcl.xRight, rcl.yTop) ;
                    184: 
                    185:                                    DrawLine (hps, rcl.xLeft,  rcl.yTop,
                    186:                                                   rcl.xRight, rcl.yBottom) ;
                    187:                                    }
                    188:                               }
                    189:                WinEndPaint (hps) ;
                    190:                return 0 ;
                    191:           }
                    192:      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
                    193:      }

unix.superglobalmegacorp.com

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