home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1988 / 15 / boxes3 / boxes3.c < prev    next >
Text File  |  1988-05-14  |  10KB  |  271 lines

  1. /*----------------------------------------------------------------------
  2.    BOXES3.C -- OS/2 PM program that draws nested boxes in client window
  3.                (c) 1988, Ziff Communications Company
  4.                PC Magazine * Charles Petzold, 5/88
  5.   ----------------------------------------------------------------------*/
  6.  
  7. #define INCL_WIN
  8. #include <os2.h>
  9. #include <stddef.h>
  10. #include "boxes3.h"
  11.  
  12. SHORT usNumBoxes  = 50 ;      /* number of boxes to draw (initially)    */
  13. SHORT usIncrement = 20 ;      /* rotate each 1/20th of side (initially) */
  14.  
  15. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  16. MRESULT EXPENTRY AboutDlgProc  (HWND, USHORT, MPARAM, MPARAM) ;
  17. MRESULT EXPENTRY NumIncDlgProc (HWND, USHORT, MPARAM, MPARAM) ;
  18.  
  19. int main (void)
  20.      {
  21.      static CHAR szClientClass [] = "Boxes3" ;
  22.      HAB         hab ;
  23.      HMQ         hmq ;
  24.      HWND        hwndFrame, hwndClient ;
  25.      QMSG        qmsg ;
  26.  
  27.      hab = WinInitialize (0) ;
  28.      hmq = WinCreateMsgQueue (hab, 0) ;
  29.  
  30.      WinRegisterClass (
  31.                     hab,                /* Anchor block handle             */
  32.                     szClientClass,      /* Name of class being registered  */
  33.                     ClientWndProc,      /* Window procedure for class      */
  34.                     CS_SIZEREDRAW,      /* Class style                     */
  35.                     0) ;                /* Extra bytes to reserve          */
  36.  
  37.      hwndFrame = WinCreateStdWindow (
  38.                     HWND_DESKTOP,       /* Parent window handle            */
  39.                     WS_VISIBLE          /* Style of frame window           */
  40.                          | FS_SIZEBORDER
  41.                          | FS_TITLEBAR
  42.                          | FS_SYSMENU
  43.                          | FS_MINMAX
  44.                          | FS_MENU,
  45.                     szClientClass,      /* Client window class name        */
  46.                     szClientClass,      /* Title bar text                  */
  47.                     0L,                 /* Style of client window          */
  48.                     NULL,               /* Module handle for resources     */
  49.                     ID_MAINMENU,        /* ID of resources                 */
  50.                     &hwndClient) ;      /* Pointer to client window handle */
  51.  
  52.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  53.           WinDispatchMsg (hab, &qmsg) ;
  54.  
  55.      WinDestroyWindow (hwndFrame) ;
  56.      WinDestroyMsgQueue (hmq) ;
  57.      WinTerminate (hab) ;
  58.  
  59.      return 0 ;
  60.      }
  61.  
  62.           /*-------------------------
  63.              Client window procedure
  64.             -------------------------*/
  65.  
  66. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  67.      {
  68.      static LONG  alColors [] = { CLR_BLACK, CLR_BLUE, CLR_RED,    CLR_PINK,
  69.                                   CLR_GREEN, CLR_CYAN, CLR_YELLOW, CLR_WHITE,
  70.                                   CLR_DARKGRAY, CLR_DARKBLUE,  CLR_DARKRED,
  71.                                   CLR_DARKPINK, CLR_DARKGREEN, CLR_DARKCYAN,
  72.                                   CLR_BROWN,    CLR_LIGHTGRAY } ;
  73.  
  74.      static SHORT xClient, yClient, idBackground = IDM_BACKGROUND + 7,
  75.                                     idForeground = IDM_FOREGROUND ;
  76.      HPS          hps;
  77.      HWND         hwndMenu ;
  78.      POINTL       aptl[5] ;
  79.      SHORT        sRep, sSide ;
  80.  
  81.      switch (msg)
  82.           {
  83.           case WM_COMMAND:
  84.                hwndMenu = WinWindowFromID (
  85.                               WinQueryWindow (hwnd, QW_PARENT, FALSE),
  86.                               FID_MENU) ;
  87.  
  88.                if (COMMANDMSG(&msg)->cmd >= IDM_BACKGROUND &&
  89.                    COMMANDMSG(&msg)->cmd <= IDM_BACKGROUND + 15)
  90.                     {
  91.                     WinSendMsg (hwndMenu, MM_SETITEMATTR,
  92.                                    MPFROM2SHORT (idBackground, TRUE),
  93.                                    MPFROM2SHORT (MIA_CHECKED, 0)) ;
  94.  
  95.                     idBackground = COMMANDMSG(&msg)->cmd ;
  96.  
  97.                     WinSendMsg (hwndMenu, MM_SETITEMATTR,
  98.                                    MPFROM2SHORT (idBackground, TRUE),
  99.                                    MPFROM2SHORT (MIA_CHECKED, MIA_CHECKED)) ;
  100.  
  101.                     WinInvalidateRect (hwnd, NULL, FALSE) ;
  102.                     }
  103.  
  104.                else if (COMMANDMSG(&msg)->cmd >= IDM_FOREGROUND &&
  105.                         COMMANDMSG(&msg)->cmd <= IDM_FOREGROUND + 15)
  106.                     {
  107.                     WinSendMsg (hwndMenu, MM_SETITEMATTR,
  108.                                    MPFROM2SHORT (idForeground, TRUE),
  109.                                    MPFROM2SHORT (MIA_CHECKED, 0)) ;
  110.  
  111.                     idForeground = COMMANDMSG(&msg)->cmd ;
  112.  
  113.                     WinSendMsg (hwndMenu, MM_SETITEMATTR,
  114.                                    MPFROM2SHORT (idForeground, TRUE),
  115.                                    MPFROM2SHORT (MIA_CHECKED, MIA_CHECKED)) ;
  116.  
  117.                     WinInvalidateRect (hwnd, NULL, FALSE) ;
  118.                     }
  119.  
  120.                else switch (COMMANDMSG(&msg)->cmd)
  121.                     {
  122.                     case IDM_ABOUT:
  123.                          WinDlgBox (HWND_DESKTOP, hwnd, AboutDlgProc,
  124.                                         NULL, IDD_ABOUTBOX, NULL) ;
  125.                          break ;
  126.  
  127.                     case IDM_NUMINC:
  128.                          if (WinDlgBox (HWND_DESKTOP, hwnd, NumIncDlgProc,
  129.                                         NULL, IDD_NUMINCBOX, NULL))
  130.  
  131.                               WinInvalidateRect (hwnd, NULL, FALSE) ;
  132.                          break ;
  133.  
  134.                     default:
  135.                          break ;
  136.                     }
  137.                break ;
  138.  
  139.           case WM_SIZE:
  140.                xClient = SHORT1FROMMP (mp2) ;
  141.                yClient = SHORT2FROMMP (mp2) ;
  142.                break ;
  143.  
  144.           case WM_PAINT:
  145.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  146.  
  147.                aptl[0].x = aptl[1].x = 0 ;
  148.                aptl[2].x = aptl[3].x = xClient - 1 ;
  149.                aptl[0].y = aptl[3].y = 0 ;
  150.                aptl[1].y = aptl[2].y = yClient - 1 ;
  151.  
  152.                GpiSetColor (hps, alColors[idBackground - IDM_BACKGROUND]) ;
  153.  
  154.                GpiMove (hps, aptl) ;
  155.                GpiBox (hps, DRO_FILL, aptl + 2, 0L, 0L) ;
  156.  
  157.                GpiSetColor (hps, alColors[idForeground - IDM_FOREGROUND]) ;
  158.  
  159.                for (sRep = 0 ; sRep < usNumBoxes ; sRep ++)
  160.                     {
  161.                     aptl[4] = aptl[0] ;
  162.  
  163.                     GpiMove (hps, aptl) ;
  164.                     GpiPolyLine (hps, 4L, aptl + 1) ;
  165.  
  166.                     for (sSide = 0 ; sSide < 4 ; sSide++)
  167.                          {
  168.                          aptl[sSide].x = ((usIncrement - 1) * aptl[sSide].x +
  169.                                              aptl[sSide + 1].x +
  170.                                              usIncrement / 2) / usIncrement ;
  171.  
  172.                          aptl[sSide].y = ((usIncrement - 1) * aptl[sSide].y +
  173.                                              aptl[sSide + 1].y +
  174.                                              usIncrement / 2) / usIncrement ;
  175.                          }
  176.                     }
  177.                WinEndPaint (hps) ;
  178.                break ;
  179.  
  180.           default:
  181.                return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  182.           }
  183.      return FALSE ;
  184.      }
  185.  
  186.           /*----------------------------
  187.              About box dialog procedure
  188.             ----------------------------*/
  189.  
  190. MRESULT EXPENTRY AboutDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  191.      {
  192.      switch (msg)
  193.           {
  194.           case WM_COMMAND:
  195.                switch (COMMANDMSG(&msg)->cmd)
  196.                     {
  197.                     case DID_OK:
  198.                     case DID_CANCEL:
  199.                          WinDismissDlg (hwnd, TRUE) ;
  200.                          break ;
  201.  
  202.                     default:
  203.                          break ;
  204.                     }
  205.                break ;
  206.  
  207.           default:
  208.                return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  209.           }
  210.      return FALSE ;
  211.      }
  212.  
  213.           /*---------------------------------------
  214.              Number and increment dialog procedure
  215.             ---------------------------------------*/
  216.  
  217. MRESULT EXPENTRY NumIncDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  218.      {
  219.      static USHORT usLocalNumBoxes, usLocalIncrement ;
  220.  
  221.      switch (msg)
  222.           {
  223.           case WM_INITDLG:
  224.                WinSendDlgItemMsg (hwnd, IDD_NUMBOXES, EM_SETTEXTLIMIT,
  225.                                   MPFROMSHORT(2), NULL) ;
  226.  
  227.                WinSendDlgItemMsg (hwnd, IDD_INCREMENT, EM_SETTEXTLIMIT,
  228.                                   MPFROMSHORT(2), NULL) ;
  229.  
  230.                WinSetDlgItemShort (hwnd, IDD_NUMBOXES,  usNumBoxes,  FALSE) ;
  231.                WinSetDlgItemShort (hwnd, IDD_INCREMENT, usIncrement, FALSE) ;
  232.                break ;
  233.  
  234.           case WM_COMMAND:
  235.                switch (COMMANDMSG(&msg)->cmd)
  236.                     {
  237.                     case DID_OK:
  238.                          if (WinQueryDlgItemShort (hwnd, IDD_NUMBOXES,
  239.                                         &usLocalNumBoxes, FALSE) &&
  240.  
  241.                                    WinQueryDlgItemShort (hwnd, IDD_INCREMENT,
  242.                                         &usLocalIncrement, FALSE) &&
  243.  
  244.                                    usLocalNumBoxes > 0 && usLocalIncrement > 0)
  245.                               {
  246.                               usNumBoxes  = usLocalNumBoxes ;
  247.                               usIncrement = usLocalIncrement ;
  248.  
  249.                               WinDismissDlg (hwnd, TRUE) ;
  250.                               }
  251.                          else
  252.                               WinMessageBox (HWND_DESKTOP, hwnd, 
  253.                                    "Values must be greater than zero.",
  254.                                    "BOXES3", 0, MB_ICONHAND | MB_OK) ;
  255.                          break ;
  256.  
  257.                     case DID_CANCEL:
  258.                          WinDismissDlg (hwnd, FALSE) ;
  259.                          break ;
  260.  
  261.                     default:
  262.                          break ;
  263.                     }
  264.                break ;
  265.  
  266.           default:
  267.                return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  268.           }
  269.      return FALSE ;
  270.      }
  271.