home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk11 / petzold / chap09 / checker2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-20  |  6.4 KB  |  189 lines

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