home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / info / prgramer / edmi / issue_1 / ugpm / zoom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-21  |  6.9 KB  |  198 lines

  1. /* ZOOM.C -- Program to demonstrate palette animation */
  2. /* Version 1.46 */
  3. /* This program is Copyright (c) 1993 by Raja Thiagarajan. However, you
  4.    may freely use it for any non-commercial purpose. You can contact me
  5.    at sthiagar@bronze.ucs.indiana.edu */
  6.  
  7. #include <stdlib.h>      /* include atol () */
  8.  
  9. #define INCL_GPI
  10. #define INCL_WIN
  11. #include <os2.h>
  12.  
  13. MRESULT EXPENTRY ClientWinProc (HWND hWnd, USHORT msg, MPARAM mp1, MPARAM mp2);
  14.  
  15. static LONG hasPalMan;      /* Whether the Palette Manager is available */
  16. static LONG shadesToShow;   /* How many squares to draw */
  17.  
  18. #define MAX_SHADES 16
  19. static ULONG tbl[MAX_SHADES];  /* Table of RGB values */
  20.  
  21. static HAB hab;                /* This program's hab ... */
  22. static HDC hdc;                /* ... hdc ... */
  23. static HPS hps;                /* ... and hps. Note that we do NOT use
  24.                                   a cached micro-ps! */
  25. static HPAL hpal;              /* Handle to the palette we'll use */
  26.  
  27. INT main (INT argc, CHAR * argv [])
  28. {
  29.    HMQ   hmq;         /* The usual bunch of variables for PM programs */
  30.    QMSG  qmsg;
  31.    HWND  hwnd,
  32.          hwndClient;
  33.  
  34.    ULONG createFlags  =  FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER
  35.                                       | FCF_MINMAX | FCF_SHELLPOSITION
  36.                                       |  FCF_TASKLIST;
  37.  
  38. #define clientClass "zoom"
  39.  
  40.    {
  41.       LONG sColors; /* How many colors are available */
  42.       HPS hpsScr;   /* These temporary vars are */
  43.       HDC hdcScr;   /*   used to query the screen driver */
  44.       hpsScr = WinGetPS (HWND_DESKTOP);
  45.       hdcScr = GpiQueryDevice (hpsScr);
  46.       DevQueryCaps (hdcScr, CAPS_COLORS, 1L, &sColors);
  47.       DevQueryCaps (hdcScr, CAPS_ADDITIONAL_GRAPHICS, 1, &hasPalMan);
  48.       hasPalMan &= CAPS_PALETTE_MANAGER;
  49.       shadesToShow = sColors;
  50. #define MAX_SHADES 16
  51.       if (shadesToShow > MAX_SHADES)
  52.         shadesToShow = MAX_SHADES;
  53.    }
  54.  
  55.    hab = WinInitialize (0);  /* initialize PM usage */
  56.  
  57.    if (hasPalMan) {
  58. /* Use 65536 for Red, 256 for Green, 1 for Blue */
  59. #define WHITE 65793 /* White = Red + Green + Blue */
  60.       ULONG  primary = WHITE;
  61.       INT    j;
  62.       if (argc > 1) {
  63.          primary = atol (argv [1]);
  64.          if ((primary < 1) || (primary > WHITE)) {
  65.             primary = WHITE;
  66.          }
  67.       }
  68.  
  69.       for (j = 0; j < shadesToShow; j++) {
  70.          tbl [j] = PC_RESERVED * 16777216 + primary * j * (256 / MAX_SHADES);
  71.       }
  72.       hpal = GpiCreatePalette (hab, 0L, LCOLF_CONSECRGB, shadesToShow, tbl);
  73.    }
  74.  
  75.    hmq = WinCreateMsgQueue (hab, 0);   /* create message queue */
  76.  
  77.    WinRegisterClass (hab, clientClass, (PFNWP) ClientWinProc, CS_SIZEREDRAW, 0);
  78.  
  79.       /* Create standard window and client */
  80.       hwnd = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE, &createFlags,
  81.                                  clientClass, "Zoom In", 0L,
  82.                                  0UL, 0, &hwndClient);
  83.    if (!hasPalMan) {
  84.       /* If there isn't a palette manager, give up now */
  85.       WinMessageBox (HWND_DESKTOP, hwndClient,
  86.                      "ZOOM cannot run without palette manager",
  87.                      "ZOOM: FATAL ERROR", 0, MB_OK | MB_ERROR);
  88.    } else {
  89.  
  90. #define TIMER_ID 1
  91.  
  92.       if (!WinStartTimer (hab, hwndClient, TIMER_ID, 100L)) {
  93.          WinMessageBox (HWND_DESKTOP, hwndClient,
  94.                         "ZOOM cannot allocate a timer. Please free a timer and try again",
  95.                         "ZOOM: FATAL ERROR", 0, MB_OK | MB_ERROR);
  96.       } else {
  97.          while (WinGetMsg (hab, &qmsg, NULLHANDLE, 0, 0)) /* msg dispatch loop */
  98.             WinDispatchMsg (hab, &qmsg);
  99.  
  100.          WinStopTimer (hab, hwndClient, TIMER_ID);
  101.  
  102.          GpiDeletePalette (hpal);
  103.       }
  104.    }
  105.  
  106.    WinDestroyWindow (hwnd);           /* destroy frame window */
  107.    WinDestroyMsgQueue (hmq);          /* destroy message queue */
  108.    WinTerminate (hab);                /* terminate PM usage */
  109.  
  110.    return 0;
  111. }
  112.  
  113. MRESULT EXPENTRY ClientWinProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  114. {
  115.    static USHORT  cWid, cHi;
  116.    static BOOL    firstPaint = TRUE; /* Whether this is the first response
  117.                                         to WM_PAINT */
  118.      
  119.    switch (msg)
  120.       {
  121.          case WM_CREATE:
  122.             {
  123.                SIZEL sizl;
  124.                sizl.cx = sizl.cy = 0;
  125.                hdc = WinOpenWindowDC (hwnd);
  126.                hps = GpiCreatePS (hab, hdc, &sizl, PU_PELS | GPIF_DEFAULT
  127.                         | GPIT_MICRO | GPIA_ASSOC);
  128.                GpiSelectPalette (hps, hpal);
  129.             }
  130.             return (MRESULT) FALSE;
  131.          case WM_DESTROY:
  132.             GpiSelectPalette (hps, NULLHANDLE);
  133.             GpiDestroyPS (hps);
  134.             return (MRESULT) FALSE;
  135.          case WM_TIMER:
  136.             {
  137.                INT j;
  138.                ULONG tmp = tbl [0];
  139.                for (j = 0; j < (shadesToShow - 1); j++) {
  140.                   tbl [j] = tbl [j + 1];
  141.                }
  142.                tbl [shadesToShow - 1] = tmp;
  143.                GpiAnimatePalette (hpal, LCOLF_CONSECRGB, 0, shadesToShow, tbl);
  144.                {
  145.                   ULONG palSize = shadesToShow;
  146.                   WinRealizePalette (hwnd, hps, &palSize);
  147.                }
  148.             }
  149.             return (MRESULT) FALSE;
  150.          case WM_ERASEBACKGROUND:
  151.             return (MRESULT) TRUE;
  152.          case WM_REALIZEPALETTE:
  153.             {
  154.                ULONG palSize = shadesToShow;
  155.                if (WinRealizePalette (hwnd, hps, &palSize)) {
  156.                   WinInvalidateRect (hwnd, NULL, FALSE);
  157.                }
  158.             }
  159.             return (MRESULT) FALSE;
  160.          case WM_PAINT:
  161.             {
  162.                POINTL ptl, p2;
  163.                LONG   j;
  164.  
  165.                WinBeginPaint (hwnd, hps, NULL);
  166.                if (hasPalMan) {
  167.                   if (firstPaint) {
  168.                      ULONG palSize = shadesToShow;
  169.                      WinRealizePalette (hwnd, hps, &palSize);
  170.                      firstPaint = FALSE;
  171.                   }
  172.                   ptl.x = ptl.y = 0;
  173.                   p2.x = cWid;
  174.                   p2.y = cHi;
  175.                   for (j = 0; j < shadesToShow; j++) {
  176.                      GpiSetColor (hps, j);
  177.                      GpiMove (hps, &ptl);
  178.                      GpiBox (hps, DRO_FILL, &p2, 0L, 0L);
  179.                      ptl.x += cWid / shadesToShow / 2;
  180.                      ptl.y += cHi / shadesToShow / 2;
  181.                      p2.x -= cWid / shadesToShow / 2;
  182.                      p2.y -= cHi / shadesToShow / 2;
  183.                   }
  184.                }
  185.                WinEndPaint (hps);
  186.                return (MRESULT) FALSE;
  187.             }
  188.          case WM_SIZE:
  189.             cWid = SHORT1FROMMP (mp2);
  190.             cHi = SHORT2FROMMP (mp2);
  191.             return (MRESULT) FALSE;
  192.          default:
  193.             return (WinDefWindowProc (hwnd, msg, mp1, mp2));
  194.             break;
  195.       }  /*end switch*/
  196.    return (MRESULT) FALSE;
  197. }
  198.