home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / warptlk3.zip / TOOLKIT / SAMPLES / PM / PALETTE / PALETTE.C next >
Text File  |  1995-08-24  |  42KB  |  1,020 lines

  1. /*+--------------------------------------------------------------------------+*/
  2. /*|                                                                          |*/
  3. /*| PROGRAM NAME: PALETTE                                                    |*/
  4. /*| -------------                                                            |*/
  5. /*|  A PM program demonstrating vector fonts, palette manager APIs, and IPF  |*/
  6. /*|  help.                                                                   |*/
  7. /*|                                                                          |*/
  8. /*| COPYRIGHT:                                                               |*/
  9. /*| ----------                                                               |*/
  10. /*|  Copyright (C) International Business Machines Corp., 1992               |*/
  11. /*|                                                                          |*/
  12. /*| DISCLAIMER OF WARRANTIES:                                                |*/
  13. /*| -------------------------                                                |*/
  14. /*|  The following [enclosed] code is sample code created by IBM Corporation.|*/
  15. /*|  This sample code is not part of any standard IBM product and is provided|*/
  16. /*|  to you solely for the purpose of assisting you in the development of    |*/
  17. /*|  your applications.  The code is provided "AS IS", without warranty of   |*/
  18. /*|  any kind.  IBM shall not be liable for any damages arising out of your  |*/
  19. /*|  use of the sample code, even if they have been advised of the           |*/
  20. /*|  possibility of such damages.                                            |*/
  21. /*|                                                                          |*/
  22. /*| REVISION LEVEL: 1.0                                                      |*/
  23. /*| ---------------                                                          |*/
  24. /*|                                                                          |*/
  25. /*| WHAT THIS PROGRAM DOES:                                                  |*/
  26. /*| -----------------------                                                  |*/
  27. /*|  This program displays a standard window and then draws the IBM logo     |*/
  28. /*|  in the window.  The logo is done using a vector font.  Then a palette   |*/
  29. /*|  is created and rotated in another thread.                               |*/
  30. /*|                                                                          |*/
  31. /*| WHAT THIS PROGRAM DEMONSTRATES:                                          |*/
  32. /*| -------------------------------                                          |*/
  33. /*|  This program demonstrates how to create and display a standard window,  |*/
  34. /*|  use switched menus, use a second thread for drawing, modify a palette   |*/
  35. /*|  using some basic GPI calls, and display vectored fonts.                 |*/
  36. /*|                                                                          |*/
  37. /*| APIs CALLED:                                                             |*/
  38. /*| ------------                                                             |*/
  39. /*|    WinInitialize                                                         |*/
  40. /*|    WinCreateMsgQueue                                                     |*/
  41. /*|    WinRegisterClass                                                      |*/
  42. /*|    WinCreateHelpInstance                                                 |*/
  43. /*|    WinDestroyHelpInstance                                                |*/
  44. /*|    WinCreateStdWindow                                                    |*/
  45. /*|    WinGetLastError                                                       |*/
  46. /*|    WinShowWindow                                                         |*/
  47. /*|    WinAssociateHelpInstance                                              |*/
  48. /*|    WinGetMsg                                                             |*/
  49. /*|    WinDispatchMsg                                                        |*/
  50. /*|    WinReleasePS                                                          |*/
  51. /*|    WinDestroyHelpInstance                                                |*/
  52. /*|    WinDestroyWindow                                                      |*/
  53. /*|    WinDestroyMsgQueue                                                    |*/
  54. /*|    WinTerminate                                                          |*/
  55. /*|    WinRequestMutexSem                                                    |*/
  56. /*|    WinRealizePalette                                                     |*/
  57. /*|    DosReleaseMutexSem                                                    |*/
  58. /*|    DosCreateMutexSem                                                     |*/
  59. /*|    WinOpenWindowDC                                                       |*/
  60. /*|    GpiCreatePS                                                           |*/
  61. /*|    DevQueryCaps                                                          |*/
  62. /*|    DosCreateThread                                                       |*/
  63. /*|    WinQueryWindowRect                                                    |*/
  64. /*|    WinPostMsg                                                            |*/
  65. /*|    WinWindowFromID                                                       |*/
  66. /*|    WinSetMenuItemText                                                    |*/
  67. /*|    WinInvalidateRect                                                     |*/
  68. /*|    WinDlgBox                                                             |*/
  69. /*|    WinSendMsg                                                            |*/
  70. /*|    WinBeginPaint                                                         |*/
  71. /*|    WinFillRect                                                           |*/
  72. /*|    GpiQueryFonts                                                         |*/
  73. /*|    GpiCreateLogFont                                                      |*/
  74. /*|    GpiSetCharSet                                                         |*/
  75. /*|    GpiSetCharBox                                                         |*/
  76. /*|    GpiQueryTextBox                                                       |*/
  77. /*|    GpiBeginPath                                                          |*/
  78. /*|    GpiMove                                                               |*/
  79. /*|    GpiCharString                                                         |*/
  80. /*|    GpiEndPath                                                            |*/
  81. /*|    GpiSetClipPath                                                        |*/
  82. /*|    WinEndPaint                                                           |*/
  83. /*|    WinDefWindowProc                                                      |*/
  84. /*|    WinDismissDlg                                                         |*/
  85. /*|    WinDefDlgProc                                                         |*/
  86. /*|    GpiCreatePalette                                                      |*/
  87. /*|    GpiSelectPalette                                                      |*/
  88. /*|    GpiSetColor                                                           |*/
  89. /*|    GpiLine                                                               |*/
  90. /*|    DosSleep                                                              |*/
  91. /*|    GpiAnimatePalette                                                     |*/
  92. /*|    GpiDeletePalette                                                      |*/
  93. /*|    DosExit                                                               |*/
  94. /*|    WinAlarm                                                              |*/
  95. /*|    WinMessageBox                                                         |*/
  96. /*|                                                                          |*/
  97. /*|                                                                          |*/
  98. /*| WHAT YOU NEED TO COMPILE THIS PROGRAM:                                   |*/
  99. /*| --------------------------------------                                   |*/
  100. /*|                                                                          |*/
  101. /*|  REQUIRED FILES:                                                         |*/
  102. /*|  ---------------                                                         |*/
  103. /*|                                                                          |*/
  104. /*|    PALETTE.C      - Source code                                          |*/
  105. /*|    PALETTE.MAK    - Make file for this program                           |*/
  106. /*|    PALETTE.DEP    - Dependency file for this program                     |*/
  107. /*|    PALETTE.DEF    - Module definition file                               |*/
  108. /*|    PALETTE.H      - Application header file                              |*/
  109. /*|    PALETTE.ICO    - Icon file                                            |*/
  110. /*|    PALETTE.RC     - Resource file                                        |*/
  111. /*|    PALETTE.IPF    - Documentation file for this program                  |*/
  112. /*|    PALETTE.DLG    - Dialog file                                          |*/
  113. /*|                                                                          |*/
  114. /*|    OS2.H          - Presentation Manager include file                    |*/
  115. /*|    STDLIB.H       - Standard library function declarations               |*/
  116. /*|    ...                                                                   |*/
  117. /*|                                                                          |*/
  118. /*|  REQUIRED LIBRARIES:                                                     |*/
  119. /*|  -------------------                                                     |*/
  120. /*|                                                                          |*/
  121. /*|    OS2386.LIB     - OS/2 API Library                                     |*/
  122. /*|                                                                          |*/
  123. /*|  REQUIRED PROGRAMS:                                                      |*/
  124. /*|  ------------------                                                      |*/
  125. /*|                                                                          |*/
  126. /*|    IBM C Set/2 Compiler (icc.exe    )                                    |*/
  127. /*|    IBM Linker           (link386.exe)                                    |*/
  128. /*|    Resource Compiler    (rc.exe     )                                    |*/
  129. /*|    Help Compiler        (ipfc.exe   )                                    |*/
  130. /*|                                                                          |*/
  131. /*+--------------------------------------------------------------------------+*/
  132.  
  133. /*+--------------------------------------------------------------------------+*/
  134. /*| System and library header files.                                         |*/
  135. /*+--------------------------------------------------------------------------+*/
  136.  
  137. #define INCL_WINWINDOWMGR
  138. #define INCL_WINMESSAGEMGR
  139. #define INCL_WINHELP
  140. #define INCL_WINFRAMEMGR
  141. #define INCL_WINERRORS
  142. #define INCL_WINPALETTE
  143. #define INCL_GPICONTROL
  144. #define INCL_WINMENUS
  145. #define INCL_WINDIALOGS
  146. #define INCL_GPILCIDS
  147. #define INCL_GPIPRIMITIVES
  148. #define INCL_GPIPATHS
  149. #define INCL_GPILOGCOLORTABLE
  150. #define INCL_DOSSEMAPHORES
  151. #define INCL_DOSPROCESS
  152. #define INCL_DEV
  153. #include <os2.h>
  154.  
  155. #include <string.h>
  156. #include <stdlib.h>
  157. #include <stdio.h>
  158. #include <stdarg.h>
  159. #include "palette.h"
  160.  
  161. /*
  162.  * Globals
  163.  */
  164.  
  165. HAB     hab;
  166. HPAL    hPal = NULLHANDLE;
  167. HRGN    hrgn = NULLHANDLE;
  168. HDC     hdc =  NULLHANDLE;
  169. HMQ     hmqPalette;
  170. HWND    hwndPalette;
  171. HWND    hwndPaletteFrame;
  172. CHAR    szClassName[]   = "Palette";
  173. CHAR    szMessage[]     = "Palette Manager Sample";
  174.  
  175.  
  176. /*
  177.  * Status Flags For the options menu
  178.  */
  179.  
  180. #define SLOW_VALUE    100LU
  181. #define MEDIUM_VALUE  10LU                  /* Set speed constants            */
  182. #define FAST_VALUE     0LU
  183.  
  184. BOOL  speedSlow      = FALSE;
  185. BOOL  speedMedium    = TRUE;                /* Initial Speed                  */
  186. BOOL  speedFast      = FALSE;
  187. ULONG animationSpeed = MEDIUM_VALUE;
  188.  
  189. BOOL  logoIBM        = FALSE;               /* Initial Text                   */
  190. BOOL  logoOS2        = TRUE;
  191.  
  192. /*
  193.  * Palette Drawing Thread Globals
  194.  */
  195.  
  196. static LONG    lMessageCount = 0L;
  197. static BOOL    fPaletteCaps = TRUE;    /* Palette Manager capabilities?  */
  198. static SHORT   cxClient, cyClient;     /* client window X and Y size     */
  199. static USHORT  threadRun = FALSE;      /* non-zero means draw lines      */
  200. static HMTX    hmtxPS    = NULLHANDLE; /* PS access semaphore            */
  201. static HPS     hpsBuffer = NULLHANDLE; /* Pres. space for memory buffer  */
  202.  
  203. #define PAL_COUNT 128L       /* For the purposes of this example we will be   */
  204. ULONG alTable[PAL_COUNT];    /*        using 128 palette entries.             */
  205.  
  206. #define MSG_BOX_ID     256                  /* error message box id           */
  207.  
  208.  
  209. /*
  210.  * Required IPF Structure Declarations
  211.  */
  212.  
  213. HELPINIT hmiHelpData;                       /* Help initialization structure  */
  214. HWND     hwndHelpInstance;                  /* Handle to Help window          */
  215.  
  216.  
  217. /*
  218.  * Main routine...initializes window and message queue
  219.  */
  220. int main( )
  221. {
  222.    QMSG qmsg;
  223.    ULONG ctldata;
  224.  
  225.    /*
  226.     * Create your hab, hmq, and register a class.
  227.     */
  228.  
  229.    hab = WinInitialize(0LU);
  230.  
  231.    hmqPalette = WinCreateMsgQueue(hab, 0L);
  232.  
  233.    WinRegisterClass( hab,
  234.                      (PSZ)szClassName,
  235.                      (PFNWP)PaletteWndProc,
  236.                      CS_SIZEREDRAW,
  237.                      0LU);
  238.  
  239.    /*+-----------------------------------------+*/
  240.    /*| IPF Initialization Structure            |*/
  241.    /*+-----------------------------------------+*/
  242.  
  243.    hmiHelpData.cb                       = sizeof(HELPINIT);
  244.    hmiHelpData.ulReturnCode             = 0LU;
  245.    hmiHelpData.pszTutorialName          = (PSZ)NULL;
  246.    hmiHelpData.phtHelpTable      = (PHELPTABLE)MAKELONG(MAIN_HELPTABLE, 0xFFFF);
  247.    hmiHelpData.hmodAccelActionBarModule = NULLHANDLE;
  248.    hmiHelpData.idAccelTable             = 0;
  249.    hmiHelpData.idActionBar              = 0;
  250.    hmiHelpData.pszHelpWindowTitle       = "Palette Help";
  251.    hmiHelpData.hmodHelpTableModule      = NULLHANDLE;
  252.    hmiHelpData.fShowPanelId             = CMIC_HIDE_PANEL_ID;
  253.    hmiHelpData.pszHelpLibraryName       = "PALETTE.HLP";
  254.  
  255.  
  256.    /*+------------------------------------------+*/
  257.    /*| Create Instance of IPF                   |*/
  258.    /*|                                          |*/
  259.    /*| pass Anchor Block handle and address of  |*/
  260.    /*| IPF initialization structure, and check  |*/
  261.    /*| that creation was successful.            |*/
  262.    /*+------------------------------------------+*/
  263.  
  264.    if((hwndHelpInstance = WinCreateHelpInstance(hab, &hmiHelpData))==NULLHANDLE)
  265.       mprintf("Help Creation Error");
  266.  
  267.    else if (hmiHelpData.ulReturnCode)
  268.    {
  269.       mprintf("Help Terminated Due to Error %ld",
  270.                hmiHelpData.ulReturnCode);
  271.  
  272.       WinDestroyHelpInstance(hwndHelpInstance);
  273.    }
  274.  
  275.  
  276.    /*
  277.     * Create a standard pm window and check
  278.     * that creation was successful.
  279.     */
  280.  
  281.    ctldata = FCF_STANDARD;
  282.  
  283.    hwndPaletteFrame = WinCreateStdWindow( HWND_DESKTOP,
  284.                                           WS_VISIBLE,
  285.                                           &ctldata,
  286.                                           szClassName,
  287.                                           szMessage,
  288.                                           0LU,
  289.                                           NULLHANDLE,
  290.                                           ID_WINDOW,
  291.                                           &hwndPalette );
  292.    if (hwndPaletteFrame == NULLHANDLE)
  293.    {
  294.       mprintf("WinCreateStdWindow error: 0x%04x", WinGetLastError(hab));
  295.       return 0;
  296.    }
  297.    WinShowWindow( hwndPaletteFrame, TRUE );
  298.  
  299.  
  300.    /*+-----------------------------------------+*/
  301.    /*| Associate Instance of IPF               |*/
  302.    /*|                                         |*/
  303.    /*| pass handle to Help Window and handle   |*/
  304.    /*| to frame window with which Help is      |*/
  305.    /*| to be associated.                       |*/
  306.    /*+-----------------------------------------+*/
  307.  
  308.    if (hwndHelpInstance)
  309.       WinAssociateHelpInstance(hwndHelpInstance, hwndPaletteFrame);
  310.  
  311.  
  312.    /*
  313.     * Poll messages from event queue
  314.     */
  315.  
  316.    while( WinGetMsg( hab, (PQMSG)&qmsg, (HWND)NULLHANDLE, 0LU, 0LU ) )
  317.       WinDispatchMsg( hab, (PQMSG)&qmsg );
  318.  
  319.  
  320.    /*
  321.     * Clean up
  322.     */
  323.  
  324.    if(hpsBuffer)
  325.       WinReleasePS( hpsBuffer );
  326.    if(hwndHelpInstance)
  327.       WinDestroyHelpInstance( hwndHelpInstance );
  328.    if(hwndPaletteFrame)
  329.       WinDestroyWindow( hwndPaletteFrame );
  330.    if(hmqPalette)
  331.       WinDestroyMsgQueue( hmqPalette );
  332.    WinTerminate( hab );
  333.    return 0;
  334. } /*  end of main()  */
  335.  
  336.  
  337. /*
  338.  * This routine processes WM_COMMAND, WM_PAINT.  It passes
  339.  * everything else to the Default Window Procedure.
  340.  */
  341. MRESULT PaletteWndProc(
  342.    HWND   hwnd,
  343.    ULONG  msg,
  344.    MPARAM mp1,
  345.    MPARAM mp2 )
  346. {
  347.    static  RECTL  rctlClient, rctlSize;       /* client rectl's               */
  348.    TID     tidDrawing;                        /* drawing thread identifier    */
  349.    ULONG   cclr;
  350.    HPS     hps;
  351.    HWND    hwndMenu;                          /* Handle for the Menu bar      */
  352.  
  353.  
  354.    switch (msg)
  355.    {
  356.  
  357.       /*+-----------------------------------------------------------------+*/
  358.       /*| In this example, the WinRealizePalette call is issued in        |*/
  359.       /*| response to a WM_REALIZEPALETTE. This ensures that the palette  |*/
  360.       /*| is appropriately realized for all drawing operations.           |*/
  361.       /*+-----------------------------------------------------------------+*/
  362.  
  363.    case WM_REALIZEPALETTE:
  364.          WinRequestMutexSem( hmtxPS, SEM_INDEFINITE_WAIT );
  365.          WinRealizePalette(hwnd, hpsBuffer, &cclr);
  366.          DosReleaseMutexSem( hmtxPS );
  367.       break;
  368.  
  369.  
  370.    case WM_CREATE:
  371.  
  372.    {
  373.       SIZEL sizl = { 0L, 0L };
  374.       LONG  alCaps[CAPS_PHYS_COLORS];
  375.       LONG lCount = CAPS_PHYS_COLORS;
  376.       LONG lStart = CAPS_FAMILY;
  377.  
  378.       /*+-----------------------------------------------------------------+*/
  379.       /*| The client window is being created.  Create the semaphore to    |*/
  380.       /*| control access to the presentation space.  Then create the      |*/
  381.       /*| thread that will draw the lines.                                |*/
  382.       /*+-----------------------------------------------------------------+*/
  383.  
  384.       DosCreateMutexSem( (PSZ)NULL, &hmtxPS, 0UL, FALSE );
  385.  
  386.       hdc = WinOpenWindowDC(hwnd);
  387.  
  388.       /*+-----------------------------------------------------------------+*/
  389.       /*| Create a non-cached presentation space.  We will not release    |*/
  390.       /*| this PS, as we will be Selecting a Palette to this PS and then  |*/
  391.       /*| animating the palette.  Upon releasing a PS the palette is no   |*/
  392.       /*| longer selected for obvious reasons.                            |*/
  393.       /*+-----------------------------------------------------------------+*/
  394.  
  395.       hpsBuffer = GpiCreatePS( hab,
  396.                                hdc,
  397.                                &sizl,
  398.                                PU_PELS | GPIF_DEFAULT |
  399.                                GPIT_MICRO | GPIA_ASSOC );
  400.  
  401.       DevQueryCaps( hdc, lStart, lCount, alCaps );
  402.       fPaletteCaps = alCaps[CAPS_ADDITIONAL_GRAPHICS] & CAPS_PALETTE_MANAGER;
  403.  
  404.       if ( PaletteInit() )
  405.          break;
  406.  
  407.       threadRun = TRUE;
  408.  
  409.       DosCreateThread( &tidDrawing,
  410.                        (PFNTHREAD) DrawingThread,
  411.                        0UL,
  412.                        0UL,
  413.                        8192UL );
  414.  
  415.       break;
  416.    }
  417.  
  418.    case WM_SIZE:
  419.       /*+-----------------------------------------------------------------+*/
  420.       /*| Window is being resized so get new client window size and       |*/
  421.       /*| recompute the end points for the lines.                         |*/
  422.       /*+-----------------------------------------------------------------+*/
  423.  
  424.       WinQueryWindowRect(hwnd,&rctlSize);
  425.  
  426.       cxClient = SHORT1FROMMP( mp2 );       /* Get new client window size     */
  427.       cyClient = SHORT2FROMMP( mp2 );
  428.       break;
  429.  
  430.    case WM_SHOW:
  431.       if ( (lMessageCount == 0L) && !fPaletteCaps )
  432.       {
  433.          lMessageCount +=
  434.          mprintf("Palette animation is not supported on this display.");
  435.       }
  436.       break;
  437.  
  438.    case WM_INITMENU:
  439.       switch (LOUSHORT(mp1))
  440.       {
  441.          case IDM_OPTIONS:
  442.             hwndMenu = WinWindowFromID( hwndPaletteFrame, /* Get Menu Handle  */
  443.                                         FID_MENU );
  444.             WinEnableMenuItem(hwndMenu, IDM_SPEED,     fPaletteCaps);
  445.             WinEnableMenuItem(hwndMenu, IDM_STARTSTOP, fPaletteCaps);
  446.             break;
  447.  
  448.          case IDM_SPEED:
  449.             SetPulldownState(hwndPaletteFrame, IDM_SPEEDSLOW  , speedSlow);
  450.             SetPulldownState(hwndPaletteFrame, IDM_SPEEDMEDIUM, speedMedium);
  451.             SetPulldownState(hwndPaletteFrame, IDM_SPEEDFAST  , speedFast);
  452.             break;
  453.  
  454.          case IDM_LOGO:
  455.             SetPulldownState(hwndPaletteFrame, IDM_LOGOIBM    , logoIBM);
  456.             SetPulldownState(hwndPaletteFrame, IDM_LOGOOS2    , logoOS2);
  457.             break;
  458.       }
  459.       break;
  460.  
  461.  
  462.    case WM_COMMAND:
  463.       switch (LOUSHORT(mp1))
  464.       {
  465.          case IDM_EXIT:
  466.             WinPostMsg(hwnd, WM_CLOSE, MPVOID, MPVOID);
  467.             break;
  468.  
  469.          case IDM_STARTSTOP:
  470.             hwndMenu = WinWindowFromID( hwndPaletteFrame, /* Get Menu Handle  */
  471.                                         FID_MENU );
  472.  
  473.             if ( speedSlow || speedMedium || speedFast  ) /* If running->stop */
  474.             {
  475.                speedSlow   = FALSE;
  476.                speedMedium = FALSE;                       /* Set no speed     */
  477.                speedFast   = FALSE;
  478.  
  479.                WinSetMenuItemText( hwndMenu,              /* Change Menu Text */
  480.                                    IDM_STARTSTOP,         /* ID# for old text */
  481.                                    "Start" );             /* 'Change To' text */
  482.             }
  483.             else                                     /* If stopped then start */
  484.             {
  485.                if ( animationSpeed == SLOW_VALUE )
  486.                     speedSlow   = TRUE;
  487.                if ( animationSpeed == MEDIUM_VALUE )      /* Reset proper flag */
  488.                     speedMedium = TRUE;
  489.                if ( animationSpeed == FAST_VALUE )
  490.                     speedFast   = TRUE;
  491.  
  492.                WinSetMenuItemText( hwndMenu,              /* Change Menu Text */
  493.                                    IDM_STARTSTOP,         /* ID# for old text */
  494.                                    "Stop" );              /* 'Change To' text */
  495.             }
  496.             break;
  497.  
  498.  
  499.          case IDM_SPEEDSLOW:
  500.             speedSlow   = TRUE;
  501.             speedMedium = FALSE;
  502.             speedFast   = FALSE;
  503.             animationSpeed = SLOW_VALUE;
  504.             break;
  505.  
  506.          case IDM_SPEEDMEDIUM:
  507.             speedSlow   = FALSE;
  508.             speedMedium = TRUE;
  509.             speedFast   = FALSE;
  510.             animationSpeed = MEDIUM_VALUE;
  511.             break;
  512.  
  513.          case IDM_SPEEDFAST:
  514.             speedSlow   = FALSE;
  515.             speedMedium = FALSE;
  516.             speedFast   = TRUE;
  517.             animationSpeed = FAST_VALUE;
  518.             break;
  519.  
  520.          case IDM_LOGOIBM:
  521.             logoIBM = TRUE;
  522.             logoOS2 = FALSE;
  523.             WinInvalidateRect(hwnd, &rctlSize, FALSE);
  524.             break;
  525.  
  526.          case IDM_LOGOOS2:
  527.             logoIBM = FALSE;
  528.             logoOS2 = TRUE;
  529.             WinInvalidateRect(hwnd, &rctlSize, FALSE);
  530.             break;
  531.  
  532.          case IDM_HELPABOUT:               /* DISPLAY HELP ABOUT DIALOG       */
  533.             WinDlgBox(HWND_DESKTOP,        /* MAKE THE DESKTOP THE PARENT     */
  534.                       hwnd,                /* OWNED BY THE CLIENT WINDOW      */
  535.                       (PFNWP) AboutDlgProc,/* ADDRESS OF THE DIALOG PROCEDURE */
  536.                       NULLHANDLE,          /* MODULE HANDLE                   */
  537.                       IDD_PRODUCTINFO,     /* DIALOG IDENTIFIER IN RESOURCE   */
  538.                       (PVOID)NULL);        /* INITIALIZATION DATA             */
  539.  
  540.             WinInvalidateRect(hwnd, &rctlSize, FALSE);      /* Refresh Window */
  541.             break;
  542.  
  543.          case IDM_HELPFORHELP:
  544.             if (hwndHelpInstance)
  545.             {
  546.                WinSendMsg( hwndHelpInstance, HM_DISPLAY_HELP, MPVOID, MPVOID);
  547.             }
  548.            break;
  549.  
  550.          case IDM_HELP_INDEX:
  551.             if (hwndHelpInstance)
  552.             {
  553.                WinSendMsg(hwndHelpInstance, HM_HELP_CONTENTS, MPVOID, MPVOID);
  554.             }
  555.             break;
  556.  
  557.          case IDM_EXTENDED_HELP:
  558.             if (hwndHelpInstance)
  559.             {
  560.                WinSendMsg(hwndHelpInstance, HM_DISPLAY_HELP,
  561.                MPFROMSHORT(IDM_EXTENDED_HELP), HM_RESOURCEID);
  562.             }
  563.             break;
  564.  
  565.          case IDM_KEYS_HELP:
  566.             if (hwndHelpInstance)
  567.             {
  568.                WinSendMsg( hwndHelpInstance,
  569.                            HM_DISPLAY_HELP,
  570.                            MPFROM2SHORT(IDM_KEYS_HELP, 0),
  571.                            MPFROMSHORT(HM_RESOURCEID));
  572.             }
  573.             break;
  574.  
  575.          default:
  576.             break;
  577.  
  578.       } /* end of switch within case WM_COMMAND */
  579.       break;
  580.  
  581.    case WM_PAINT:
  582.  
  583.       /*+------------------------------------------------------+*/
  584.       /*| Since there will be two threads painting on the same |*/
  585.       /*| frame we will use a semaphore to resolve contention. |*/
  586.       /*+------------------------------------------------------+*/
  587.  
  588.       WinRequestMutexSem( hmtxPS, SEM_INDEFINITE_WAIT );
  589.  
  590.       /*
  591.        * Get a cached micro presentation space.
  592.        */
  593.       hps = WinBeginPaint( hwnd, NULLHANDLE, &rctlClient );
  594.  
  595.       /*
  596.        * Fill the rectangle with the backgound color.
  597.        */
  598.       WinFillRect( hps, &rctlClient, CLR_BLACK );
  599.  
  600.       /*+-------------------------------------------------+*/
  601.       /*| The following code segment loads a vector font  |*/
  602.       /*| scales it to fit in the window and draws it     |*/
  603.       /*| vertically and horizontally centered.           |*/
  604.       /*+-------------------------------------------------+*/
  605.       {
  606.          POINTL       ptl;
  607.          SIZEF        sizfx;
  608.          FATTRS       fattrs;
  609.          ULONG        min;
  610.          LONG         cFonts, lTemp = 0L;
  611.          PFONTMETRICS pfm;
  612.          LONG         lcid = 1L;
  613.          CHAR         fontName[25],outText[25];
  614.          USHORT       codePage;
  615.  
  616.  
  617.          min = (cxClient<cyClient) ? cxClient : cyClient;
  618.          min /= 3;
  619.  
  620.          if(logoIBM)
  621.          {
  622.             strcpy(fontName,"Symbol Set");
  623.             codePage = 65400;
  624.             strcpy(outText,"Ä");
  625.          }
  626.          else
  627.          {
  628.             /*+-------------------------------------------+*/
  629.             /*| Define "Helvetica Bold" vector font,      |*/
  630.             /*| "Courier Bold" and "Times New Roman Bold" |*/
  631.             /*| are examples of other vector fonts.       |*/
  632.             /*+-------------------------------------------+*/
  633.             strcpy(fontName,"Helvetica Bold");
  634.             codePage = 850;
  635.             strcpy(outText,"OS/2");
  636.          }
  637.  
  638.          cFonts = GpiQueryFonts(hpsBuffer, QF_PUBLIC, fontName, &lTemp,
  639.              (LONG) sizeof(FONTMETRICS), (PFONTMETRICS)NULL);
  640.  
  641.          if(cFonts > 0L)
  642.          {
  643.             pfm = malloc(sizeof(FONTMETRICS) * cFonts);
  644.             GpiQueryFonts(hpsBuffer, QF_PUBLIC, fontName, &cFonts,
  645.                           (LONG) sizeof(FONTMETRICS), pfm);
  646.             fattrs.lMatch          = pfm[0].lMatch;
  647.             strcpy(fattrs.szFacename, pfm[0].szFacename);
  648.          }
  649.          else
  650.          {
  651.             fattrs.lMatch = 0L;
  652.             strcpy(fattrs.szFacename, "");
  653.          }
  654.  
  655.          fattrs.usRecordLength  = sizeof(FATTRS);
  656.          fattrs.fsSelection     = FATTR_SEL_OUTLINE;
  657.          fattrs.idRegistry      = 0;
  658.          fattrs.usCodePage      = codePage;
  659.          fattrs.lMaxBaselineExt = 0L;
  660.          fattrs.lAveCharWidth   = 0L;
  661.          fattrs.fsType          = 0;
  662.          fattrs.fsFontUse       = FATTR_FONTUSE_OUTLINE;
  663.  
  664.          GpiCreateLogFont(hpsBuffer, (PSTR8)NULL, lcid, &fattrs);
  665.          GpiSetCharSet(hpsBuffer, lcid);
  666.  
  667.          sizfx.cx = MAKEFIXED(min, 0);
  668.          sizfx.cy = MAKEFIXED(min, 0);
  669.          GpiSetCharBox(hpsBuffer, &sizfx);
  670.  
  671.          /*+------------------------------------------------+*/
  672.          /*| CALCULATE THE POINT TO START THE CENTERED TEXT |*/
  673.          /*+------------------------------------------------+*/
  674.          {
  675.             POINTL aptl[TXTBOX_COUNT];
  676.  
  677.             GpiQueryTextBox(hpsBuffer,
  678.                             strlen(outText),
  679.                             outText,
  680.                             TXTBOX_COUNT,      /* return maximum information  */
  681.                             aptl);             /* array of coordinates points */
  682.                                                /* in world coordinates.       */
  683.  
  684.  
  685.             ptl.x = (cxClient - aptl[TXTBOX_TOPRIGHT].x) / 2;
  686.             ptl.y = (cyClient - aptl[TXTBOX_TOPRIGHT].y) / 2;
  687.  
  688.          }
  689.  
  690.  
  691.          /*+-------------------------------------------+*/
  692.          /*| Now that we have our vector font and      |*/
  693.          /*| location to place on the screen, let's    |*/
  694.          /*| create a Path using the outline font.     |*/
  695.          /*+-------------------------------------------+*/
  696.  
  697.          GpiBeginPath(hpsBuffer,1L);
  698.  
  699.          GpiMove(hpsBuffer,&ptl);
  700.          GpiCharString(hpsBuffer, strlen(outText), outText);
  701.  
  702.          GpiEndPath(hpsBuffer);
  703.  
  704.          /*+-----------------------------------------+*/
  705.          /*| Next let's erase the current clip path  |*/
  706.          /*| and assign the above outline font path  |*/
  707.          /*| as a clip path to the current hps.      |*/
  708.          /*+-----------------------------------------+*/
  709.  
  710.          GpiSetClipPath(hpsBuffer,
  711.                 0L,
  712.                 FPATH_ALTERNATE);
  713.  
  714.          GpiSetClipPath(hpsBuffer,
  715.                 1L,
  716.                 FPATH_ALTERNATE | SCP_AND);
  717.  
  718.  
  719.          /*+-------------------------------------------+*/
  720.          /*| Lastly draw a series of lines which will  |*/
  721.          /*| be clipped to attached clip path.         |*/
  722.          /*| *Draw on the Char Box                     |*/
  723.          /*+-------------------------------------------+*/
  724.  
  725.          DrawPaletteLines(rctlSize,
  726.                           PAL_COUNT,
  727.                           hpsBuffer);
  728.  
  729.          if(pfm)
  730.             free(pfm);
  731.       }
  732.  
  733.  
  734.       /*
  735.        * Release the cached micro presentation space.
  736.        */
  737.       WinEndPaint( hps );
  738.  
  739.       /*+------------------------------------------------------+*/
  740.       /*| Release the semaphore, thereby allowing the other    |*/
  741.       /*| thread to paint.                                     |*/
  742.       /*+------------------------------------------------------+*/
  743.  
  744.       DosReleaseMutexSem( hmtxPS );
  745.       break;
  746.  
  747.    default:
  748.       return WinDefWindowProc(hwnd, msg, mp1, mp2);
  749.  
  750.    }
  751.    return MRFROMLONG(0L);
  752. }   /*  end of PaletteWndProc()  */
  753.  
  754.  
  755.  
  756. /*+--------------------------------------------------------------------------+*/
  757. /*| AboutDlgProc - Help About Dialog Procedure                               |*/
  758. /*+--------------------------------------------------------------------------+*/
  759.  
  760. MRESULT AboutDlgProc( HWND hwndDlg, ULONG msg, MPARAM mp1, MPARAM mp2)
  761. {
  762.    switch( msg )
  763.    {
  764.    case WM_COMMAND:          /* DISMISS DIALOG */
  765.       WinDismissDlg( hwndDlg, TRUE );
  766.       break;
  767.  
  768.    default:
  769.       return (WinDefDlgProc( hwndDlg, msg, mp1, mp2 ));
  770.    }
  771.    return MRFROMLONG(0L);
  772. }  /*  end of AboutDlgProc()  */
  773.  
  774.  
  775.  
  776. /*+--------------------------------------------------------------------------+*/
  777. /*| PaletteInit - Initializes the palette in the client window.              |*/
  778. /*+--------------------------------------------------------------------------+*/
  779. LONG PaletteInit()
  780. {
  781.    LONG    i;
  782.    LONG    ret = 0L;
  783.    ULONG   cclr;
  784.  
  785.    WinRequestMutexSem( hmtxPS, SEM_INDEFINITE_WAIT );
  786.  
  787.    /*+--------------------------------------------------------------+*/
  788.    /*| Create entry with shades of blue if animation is supported,  |*/
  789.    /*| solid blue otherwise.                                        |*/
  790.    /*+--------------------------------------------------------------+*/
  791.  
  792.    for (i=0L; (ULONG)i < PAL_COUNT; i++)
  793.    {
  794.       LONG index = 222L;    /*  use if no animation  */
  795.  
  796.       /*+----------------------------------------------------+*/
  797.       /*| INDEX = 128 POINTS EVENLY SPACED BETWEEN 1 AND 255 |*/
  798.       /*+----------------------------------------------------+*/
  799.       if(fPaletteCaps)
  800.          index = ((float) (i + 1L) * 255.0) / (float) PAL_COUNT;
  801.  
  802.       alTable[i] = PC_RESERVED * 16777216 + LONGFromRGB(0, 0, index); /* BLUE */
  803.  
  804.    }
  805.  
  806.    hPal = GpiCreatePalette( hab,
  807.                             (ULONG) LCOL_PURECOLOR,
  808.                             (ULONG) LCOLF_CONSECRGB,
  809.                             PAL_COUNT,
  810.                             alTable );
  811.  
  812.    if( hPal == NULLHANDLE || hPal == GPI_ERROR)
  813.    {
  814.       DosReleaseMutexSem( hmtxPS );
  815.       mprintf("GpiCreatePalette Error 0x%04x",ret = WinGetLastError(hab));
  816.    }
  817.  
  818.    else if(GpiSelectPalette(hpsBuffer, hPal ) == PAL_ERROR)
  819.    {
  820.       DosReleaseMutexSem( hmtxPS );
  821.       mprintf("GpiSelectPalette Error 0x%04x", ret = WinGetLastError(hab));
  822.    }
  823.  
  824.    else if( WinRealizePalette(hwndPalette,hpsBuffer,&cclr) == PAL_ERROR)
  825.    {
  826.       DosReleaseMutexSem( hmtxPS );
  827.       mprintf("WinRealizePalette Error 0x%04x", ret = WinGetLastError(hab));
  828.    }
  829.  
  830.    else
  831.    {
  832.       DosReleaseMutexSem( hmtxPS );
  833.    }
  834.  
  835.    return ret;
  836. }  /*  end of PaletteInit()  */
  837.  
  838.  
  839. /*+--------------------------------------------------------------------------+*/
  840. /*| DrawPaletteLines - Procedure that draws the lines in the passed box.     |*/
  841. /*+--------------------------------------------------------------------------+*/
  842. VOID DrawPaletteLines(RECTL  rctl,
  843.                       ULONG ulPalCount,
  844.                       HPS hps)
  845. {
  846.    LONG  color = 0L;                 /* foreground (line) color      */
  847.    POINTL ptl1,ptl2;
  848.  
  849.    ptl1.x = rctl.xLeft;
  850.    ptl1.y = rctl.yTop;
  851.    ptl2.x = rctl.xLeft;
  852.    ptl2.y = rctl.yBottom;
  853.  
  854.    while(ptl1.x <= rctl.xRight)
  855.    {
  856.       if ( (ULONG) color >= ulPalCount )
  857.           color = 0L;
  858.       else
  859.           color++;                   /* Increment foreground color   */
  860.  
  861.       ptl1.x ++;
  862.       ptl2.x ++;
  863.  
  864.       GpiSetColor( hps, color );
  865.       GpiMove( hps, &ptl1 );         /* Move to start point          */
  866.       GpiLine( hps, &ptl2 );         /* Draw new line                */
  867.     }
  868. }  /*  end of DrawPaletteLines()  */
  869.  
  870.  
  871.  
  872. /*+--------------------------------------------------------------------------+*/
  873. /*| DrawingThread - Procedure that animates the palette in the client window.|*/
  874. /*+--------------------------------------------------------------------------+*/
  875. static void DrawingThread( ULONG ulThreadArg )
  876. {
  877.    HAB   habThread2;               /* Anchor block handle for thread */
  878.    HMQ   hmqThread2;               /* Queue handle for thread        */
  879.    int   i;
  880.  
  881.    habThread2 = WinInitialize( 0LU );
  882.    hmqThread2 = WinCreateMsgQueue( habThread2, 0L );
  883.  
  884.    while ( threadRun )
  885.    {
  886.       LONG temp;
  887.  
  888.  
  889. /*+--------------------------------------------------------------------------+*/
  890. /*|     The following if clause puts this thread to sleep as long as there   |*/
  891. /*| is no speed flag set, i.e. the animation is 'Stop'ed.                    |*/
  892. /*|     The else clause continues rotating the palette.                      |*/
  893. /*+--------------------------------------------------------------------------+*/
  894.       if (!speedSlow && !speedMedium && !speedFast )
  895.       {
  896.          DosSleep(500LU);
  897.       }
  898.       else
  899.       {
  900.          /*+-----------------------------------------------------------+*/
  901.          /*| Request PS semaphore so that main thread does NOT use it. |*/
  902.          /*+-----------------------------------------------------------+*/
  903.  
  904.          WinRequestMutexSem( hmtxPS, SEM_INDEFINITE_WAIT );
  905.  
  906.          temp = alTable[ 0 ];
  907.          for(i = 0; (ULONG) i < PAL_COUNT - 1; i++)
  908.          {
  909.             alTable[ i ] = alTable[ i + 1 ];
  910.          }
  911.          alTable[ PAL_COUNT - 1 ] = temp;
  912.  
  913.          GpiAnimatePalette( hPal,
  914.                             LCOLF_CONSECRGB,
  915.                             0LU,
  916.                             PAL_COUNT,
  917.                             alTable);
  918.  
  919.          /*+-----------------------+*/
  920.          /*| Release PS semaphore. |*/
  921.          /*+-----------------------+*/
  922.  
  923.          DosReleaseMutexSem( hmtxPS );
  924.  
  925.          /*+---------------------------------------------+*/
  926.          /* Surrender rest of timeslice to other threads. */
  927.          /* Never do a DosSleep(0) as this will not give  */
  928.          /* lower priority threads a time slice           */
  929.          /*+---------------------------------------------+*/
  930.  
  931.          if(animationSpeed) DosSleep( animationSpeed );
  932.       }
  933.    }
  934.  
  935.  
  936.    /*+----------------------------------------+*/
  937.    /*| Clean up and terminate drawing thread. |*/
  938.    /*+----------------------------------------+*/
  939.  
  940.    if(hPal)
  941.       GpiDeletePalette( hPal );
  942.    if(hmqThread2)
  943.       WinDestroyMsgQueue( hmqThread2 );
  944.    WinTerminate( habThread2 );
  945.    DosExit( EXIT_THREAD, 0LU );
  946. }     /*  end of DrawingThread()  */
  947.  
  948.  
  949.  
  950. /*+--------------------------------------------------------------------------+*/
  951. /*|                                                                          |*/
  952. /*| FUNCTION NAME: mprintf                                                   |*/
  953. /*| --------------                                                           |*/
  954. /*|  A PM function for displaying a message box with a printf type format.   |*/
  955. /*|                                                                          |*/
  956. /*| EXPECTED INPUT:                                                          |*/
  957. /*| ---------------                                                          |*/
  958. /*|  See the definition of printf.                                           |*/
  959. /*|                                                                          |*/
  960. /*| EXPECTED OUTPUT:                                                         |*/
  961. /*| ----------------                                                         |*/
  962. /*|  Message Box                                                             |*/
  963. /*|                                                                          |*/
  964. /*| NOTES                                                                    |*/
  965. /*| ----------------                                                         |*/
  966. /*|  One must be careful not to call this within a paint routine, because    |*/
  967. /*|  the closing of the message box will generate another paint message.     |*/
  968. /*|                                                                          |*/
  969. /*+--------------------------------------------------------------------------+*/
  970. LONG mprintf(PSZ format, ...)
  971. {
  972.    va_list pArg;
  973.    PSZ     buffer;
  974.    LONG    lCount;
  975.  
  976.    WinAlarm( HWND_DESKTOP, WA_ERROR );
  977.  
  978.    if(!(buffer = malloc(513)))
  979.       return 0L;
  980.  
  981.    va_start(pArg, format);
  982.  
  983.    lCount = vsprintf(buffer, format, pArg);
  984.  
  985.    WinMessageBox( HWND_DESKTOP,       /* parent window handle    */
  986.                   HWND_DESKTOP,       /* owner window handle     */
  987.                   buffer,             /* pointer to message text */
  988.                   szClassName,        /* pointer to title text   */
  989.                   MSG_BOX_ID,         /* message box identifier  */
  990.                   MB_OK | MB_ERROR ); /* message box style       */
  991.  
  992.    return lCount;
  993. }   /*  end of mprintf()  */
  994.  
  995.  
  996.  
  997. /*+--------------------------------------------------------------------------+*/
  998. /*|                                                                          |*/
  999. /*| FUNCTION NAME: SetPulldownState                                          |*/
  1000. /*| --------------                                                           |*/
  1001. /*|  A PM function for setting the check mark status of a menu item.         |*/
  1002. /*|                                                                          |*/
  1003. /*| EXPECTED INPUT:                                                          |*/
  1004. /*| ---------------                                                          |*/
  1005. /*|  Handle to the window containing the menu.                               |*/
  1006. /*|  Item id within the menu to check.                                       |*/
  1007. /*|  BOOL indicating state to set:  TRUE=CHECKED FALSE=UNCHECKED             |*/
  1008. /*|                                                                          |*/
  1009. /*| EXPECTED OUTPUT:                                                         |*/
  1010. /*| ----------------                                                         |*/
  1011. /*|  None                                                                    |*/
  1012. /*+--------------------------------------------------------------------------+*/
  1013. void SetPulldownState( HWND hwnd, USHORT item, BOOL state)
  1014. {
  1015.    WinCheckMenuItem(WinWindowFromID(hwnd, FID_MENU), item, state);
  1016. } /*  end of SetPulldownState()  */
  1017. /**************************  end of palette.c   *******************************/
  1018.  
  1019.  
  1020.