home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / BOXES.ZIP / BOXES2.ARC / BOXES2.C < prev    next >
Text File  |  1988-04-19  |  7KB  |  162 lines

  1. /*----------------------------------------------------------------------
  2.    BOXES2.C -- OS/2 PM program that draws nested boxes in client window
  3.                (c) 1988, Ziff Communications Company
  4.                PC Magazine * Charles Petzold, 4/88
  5.   ----------------------------------------------------------------------*/
  6.  
  7. #define INCL_WIN
  8. #include <os2.h>
  9. #include <stddef.h>
  10. #include "boxes2.h"
  11.  
  12. #define NUMBOXES  50     /* number of boxes to draw    */
  13. #define INCREMENT 20     /* rotate each 1/20th of side */
  14.  
  15. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  16.  
  17. int main (void)
  18.      {
  19.      static CHAR szClientClass [] = "Boxes2" ;
  20.      HAB         hab ;
  21.      HMQ         hmq ;
  22.      HWND        hwndFrame, hwndClient ;
  23.      QMSG        qmsg ;
  24.  
  25.      hab = WinInitialize (0) ;
  26.      hmq = WinCreateMsgQueue (hab, 0) ;
  27.  
  28.      WinRegisterClass (
  29.                     hab,                /* Anchor block handle             */
  30.                     szClientClass,      /* Name of class being registered  */
  31.                     ClientWndProc,      /* Window procedure for class      */
  32.                     CS_SIZEREDRAW,      /* Class style                     */
  33.                     0) ;                /* Extra bytes to reserve          */
  34.  
  35.      hwndFrame = WinCreateStdWindow (
  36.                     HWND_DESKTOP,       /* Parent window handle            */
  37.                     WS_VISIBLE          /* Style of frame window           */
  38.                          | FS_SIZEBORDER
  39.                          | FS_TITLEBAR
  40.                          | FS_SYSMENU
  41.                          | FS_MINMAX
  42.                          | FS_MENU,
  43.                     szClientClass,      /* Client window class name        */
  44.                     szClientClass,      /* Title bar text                  */
  45.                     0L,                 /* Style of client window          */
  46.                     NULL,               /* Module handle for resources     */
  47.                     ID_MAINMENU,        /* ID of resources                 */
  48.                     &hwndClient) ;      /* Pointer to client window handle */
  49.  
  50.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  51.           WinDispatchMsg (hab, &qmsg) ;
  52.  
  53.      WinDestroyWindow (hwndFrame) ;
  54.      WinDestroyMsgQueue (hmq) ;
  55.      WinTerminate (hab) ;
  56.  
  57.      return 0 ;
  58.      }
  59.  
  60. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  61.      {
  62.      static LONG  alColors [] = { CLR_BLACK, CLR_BLUE, CLR_RED,    CLR_PINK,
  63.                                   CLR_GREEN, CLR_CYAN, CLR_YELLOW, CLR_WHITE,
  64.                                   CLR_DARKGRAY, CLR_DARKBLUE,  CLR_DARKRED,
  65.                                   CLR_DARKPINK, CLR_DARKGREEN, CLR_DARKCYAN,
  66.                                   CLR_BROWN,    CLR_LIGHTGRAY } ;
  67.  
  68.      static SHORT xClient, yClient, idBackground = IDM_BACKGROUND + 7,
  69.                                     idForeground = IDM_FOREGROUND ;
  70.      HPS          hps;
  71.      HWND         hwndMenu ;
  72.      POINTL       aptl[5] ;
  73.      SHORT        sRep, sSide ;
  74.  
  75.      switch (msg)
  76.           {
  77.           case WM_COMMAND:
  78.  
  79.                hwndMenu = WinWindowFromID (
  80.                               WinQueryWindow (hwnd, QW_PARENT, FALSE),
  81.                               FID_MENU) ;
  82.  
  83.                if (COMMANDMSG(&msg)->cmd >= IDM_BACKGROUND &&
  84.                    COMMANDMSG(&msg)->cmd <= IDM_BACKGROUND + 15)
  85.                     {
  86.                     WinSendMsg (hwndMenu, MM_SETITEMATTR,
  87.                                    MPFROM2SHORT (idBackground, TRUE),
  88.                                    MPFROM2SHORT (MIA_CHECKED, 0)) ;
  89.  
  90.                     idBackground = COMMANDMSG(&msg)->cmd ;
  91.  
  92.                     WinSendMsg (hwndMenu, MM_SETITEMATTR,
  93.                                    MPFROM2SHORT (idBackground, TRUE),
  94.                                    MPFROM2SHORT (MIA_CHECKED, MIA_CHECKED)) ;
  95.                     }
  96.  
  97.                else if (COMMANDMSG(&msg)->cmd >= IDM_FOREGROUND &&
  98.                         COMMANDMSG(&msg)->cmd <= IDM_FOREGROUND + 15)
  99.                     {
  100.                     WinSendMsg (hwndMenu, MM_SETITEMATTR,
  101.                                    MPFROM2SHORT (idForeground, TRUE),
  102.                                    MPFROM2SHORT (MIA_CHECKED, 0)) ;
  103.  
  104.                     idForeground = COMMANDMSG(&msg)->cmd ;
  105.  
  106.                     WinSendMsg (hwndMenu, MM_SETITEMATTR,
  107.                                    MPFROM2SHORT (idForeground, TRUE),
  108.                                    MPFROM2SHORT (MIA_CHECKED, MIA_CHECKED)) ;
  109.                     }
  110.                else
  111.                     break ;
  112.  
  113.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  114.                break ;
  115.  
  116.           case WM_SIZE:
  117.                xClient = SHORT1FROMMP (mp2) ;
  118.                yClient = SHORT2FROMMP (mp2) ;
  119.                break ;
  120.  
  121.           case WM_PAINT:
  122.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  123.  
  124.                aptl[0].x = aptl[1].x = 0 ;
  125.                aptl[2].x = aptl[3].x = xClient - 1 ;
  126.                aptl[0].y = aptl[3].y = 0 ;
  127.                aptl[1].y = aptl[2].y = yClient - 1 ;
  128.  
  129.                GpiSetColor (hps, alColors[idBackground - IDM_BACKGROUND]) ;
  130.  
  131.                GpiMove (hps, aptl) ;
  132.                GpiBox (hps, DRO_FILL, aptl + 2, 0L, 0L) ;
  133.  
  134.                GpiSetColor (hps, alColors[idForeground - IDM_FOREGROUND]) ;
  135.  
  136.                for (sRep = 0 ; sRep < NUMBOXES ; sRep ++)
  137.                     {
  138.                     aptl[4] = aptl[0] ;
  139.  
  140.                     GpiMove (hps, aptl) ;
  141.                     GpiPolyLine (hps, 4L, aptl + 1) ;
  142.  
  143.                     for (sSide = 0 ; sSide < 4 ; sSide++)
  144.                          {
  145.                          aptl[sSide].x = ((INCREMENT - 1) * aptl[sSide].x +
  146.                                              aptl[sSide + 1].x +
  147.                                                   INCREMENT / 2) / INCREMENT ;
  148.  
  149.                          aptl[sSide].y = ((INCREMENT - 1) * aptl[sSide].y +
  150.                                              aptl[sSide + 1].y +
  151.                                                   INCREMENT / 2) / INCREMENT ;
  152.                          }
  153.                     }
  154.                WinEndPaint (hps) ;
  155.                break ;
  156.  
  157.           default:
  158.                return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  159.           }
  160.      return FALSE ;
  161.      }
  162.