home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / dlgsamp / dlgsamp.c next >
Encoding:
C/C++ Source or Header  |  1990-07-06  |  29.3 KB  |  904 lines

  1. /*
  2.     DLGSAMP -- Dialog Box Sample Application
  3.     Created by Microsoft Corporation, 1989
  4. */
  5.  
  6. #define INCL_WINBUTTONS
  7. #define INCL_WINDIALOGS
  8. #define INCL_WINERRORS
  9. #define INCL_WINFRAMEMGR
  10. #define INCL_WININPUT
  11. #define INCL_WINLISTBOXES
  12. #define INCL_WINMENUS
  13. #define INCL_WINMESSAGEMGR
  14. #define INCL_WINRECTANGLES
  15. #define INCL_WINSWITCHLIST
  16. #define INCL_WINSYS
  17. #define INCL_WINWINDOWMGR
  18. #define M_I86L
  19. #include <os2.h>
  20.  
  21. #include <string.h>
  22. #include <stdlib.h>
  23.  
  24. #include "dlgsamp.h"
  25. #include "dlgsamp1.h"
  26.  
  27. /*
  28.     Function Prototypes
  29. */
  30.  
  31. VOID NEAR cdecl main(VOID);
  32.  
  33. /* Local Routines */ 
  34. VOID cdecl CenterDlgBox(HWND);
  35. VOID cdecl CheckColor(HWND, SHORT, COLOR *);
  36. VOID cdecl EnableModality(HWND, BOOL);
  37. BOOL cdecl IsIntInRange(HWND, SHORT, SHORT, SHORT, SHORT, SHORT);
  38. VOID cdecl LoadDialog(HWND, HWND, SHORT, PFNWP, BOOL);
  39. VOID cdecl MainWndCommand(HWND, USHORT, BOOL *);
  40. VOID cdecl MainWndPaint(HWND);
  41. VOID cdecl SetModality(HWND, BOOL);
  42. VOID cdecl Trace(PSZ, PSZ);
  43.  
  44. /* Window Procedures */
  45. MRESULT EXPENTRY fnwpMainWnd(HWND, USHORT, MPARAM, MPARAM);
  46. MRESULT EXPENTRY fnwpEntryFieldDlg(HWND, USHORT, MPARAM, MPARAM);
  47. MRESULT EXPENTRY fnwpAutoRadioButtonDlg(HWND, USHORT, MPARAM, MPARAM);
  48. MRESULT EXPENTRY fnwpCheckBoxDlg(HWND, USHORT, MPARAM, MPARAM);
  49. MRESULT EXPENTRY fnwpListBoxDlg(HWND, USHORT, MPARAM, MPARAM);
  50. MRESULT EXPENTRY fnwpAboutBoxDlg(HWND, USHORT, MPARAM, MPARAM);
  51.  
  52. /*
  53.     Global variables
  54. */ 
  55. COLOR colorClient = CLR_RED | CLR_BLUE;    /* Color of client area    */
  56.  
  57. CHAR  szEntryField1[10] = "";              /* Used to pass back info  */
  58. CHAR  szEntryField2[10] = "";              /* from entry fields       */
  59.                                            /* in EntryFieldDlg        */
  60.  
  61. BOOL  bModality = TRUE;                    /* Does the user want modal*/
  62.                                            /* or modeless dialogs?    */
  63. COLOR colorSave;
  64. CHAR  szSelection[LEN_LISTBOXENTRY] = "";  /* Used to pass back       */
  65.                                            /* list box item selected  */
  66.                                            /* in ListBoxDlg           */
  67.  
  68. HAB   hab;                                 /* Anchor block handle     */
  69. HWND  hwndClient;                          /* Client Window handle    */
  70. HWND  hwndFrame;                           /* Frame Window handle     */
  71. HWND  hwndModelessDlg;                     /* Modeless Dialog handle  */
  72.  
  73. /**************************************************************************
  74. *
  75. *   FUNCTION: main
  76. *
  77. *   Typical PM main function which initializes PM, creates a message queue,
  78. *   registers a window class, creates a window, gets and dispatches
  79. *   messages to its winproc until its time to quit, and then tidies up
  80. *   before terminating.
  81. *
  82. **************************************************************************/
  83.  
  84. VOID NEAR cdecl main(  )
  85. {
  86.   HMQ     hmq;                        /* Message Queue handle         */
  87.   QMSG    qmsg;                       /* Message                      */
  88.   ULONG   flCreate;
  89.  
  90.   hab   = WinInitialize( 0 );         /* Initialize PM                */
  91.   hmq   = WinCreateMsgQueue( hab, 0 );/* Create application msg queue */
  92.  
  93.   WinRegisterClass(                   /* Register Window Class        */
  94.       hab,                            /* Anchor block handle          */
  95.       "DlgSamp Class",                /* Window Class name            */
  96.       fnwpMainWnd,                    /* Address of Window Procedure  */
  97.       (ULONG) NULL,                   /* No special class style       */
  98.       0                               /* No extra window words        */
  99.       );
  100.  
  101.   flCreate = FCF_STANDARD & ~FCF_ACCELTABLE;
  102.  
  103.   hwndFrame = WinCreateStdWindow(
  104.         HWND_DESKTOP,                   /* Desktop Window is parent     */
  105.         WS_VISIBLE,                     /* Window styles                */
  106.         (PVOID)&flCreate,               /* Window creation parameters   */
  107.         "DlgSamp Class",                /* Window Class name            */
  108.         "",                             /* Window Text                  */
  109.         0L,                             /* Client style                 */
  110.         (HMODULE) NULL,                 /* Module handle                */
  111.         ID_MAINWND,                     /* Window ID                    */
  112.         (HWND FAR *)&hwndClient         /* Client Window handle         */
  113.         );
  114.  
  115.  
  116.   /* Message Loop */
  117.   while( WinGetMsg( hab, (PQMSG)&qmsg, (HWND)NULL, 0, 0 ) )
  118.     WinDispatchMsg( hab, (PQMSG)&qmsg );
  119.  
  120.   /* Cleanup code */
  121.   WinDestroyWindow( hwndFrame );
  122.   WinDestroyMsgQueue( hmq );
  123.   WinTerminate( hab );
  124. }
  125.  
  126. /***********************************************************************
  127. *
  128. *   WinProc: fnwpMainWnd
  129. *
  130. *   Controls the state of the menu, and loads various dialogs.  The
  131. *   dialogs will be modal or modeless depending on the setting of the
  132. *   Modality menuitem.
  133. *
  134. ***********************************************************************/
  135.  
  136. MRESULT  EXPENTRY fnwpMainWnd( hwnd, message, mp1, mp2 )
  137. HWND    hwnd;
  138. USHORT  message;
  139. MPARAM  mp1;
  140. MPARAM  mp2;
  141. {
  142.   USHORT   Command;   /* Command passed by WM_COMMAND */
  143.   SHORT    id;        /* ID of item selected from the list box */
  144.  
  145.   switch(message)
  146.   {
  147.     case WM_PAINT:
  148.       MainWndPaint( hwnd );  /* Invoke window painting routine */
  149.       break;
  150.     case WM_HELP:
  151. /*********************************************************************
  152. *
  153. *   This will be received when either:-
  154. *
  155. *   1. The user hits the F1 key
  156. *   2. The user clicks on the action bar item F1=Help
  157. *
  158. *********************************************************************/
  159.       WinMessageBox( HWND_DESKTOP,
  160.                      hwndFrame,
  161.                      (PSZ)"Dialog Sample Application: Help",
  162.              (PSZ)"Try out the pulldown menus, or Alt+selection",
  163.                      ID_MB,
  164.                      MB_OK );
  165.       break;
  166.     case WM_COMMAND:
  167.       Command = SHORT1FROMMP( mp1 );
  168.       MainWndCommand( hwnd, Command, &bModality );
  169.       break;
  170.     case DLGSAMP_EFCOMPLETE:
  171.       WinQueryWindowText( WinWindowFromID( hwndModelessDlg, EF_1 ),
  172.                           sizeof( szEntryField1 ), szEntryField1 );
  173.       WinQueryWindowText( WinWindowFromID( hwndModelessDlg, EF_2 ),
  174.                           sizeof( szEntryField2 ), szEntryField2 );
  175.       WinInvalidateRect( hwnd, NULL, FALSE );/* Request whole window repaint */
  176.       break;
  177.     case DLGSAMP_LBCOMPLETE:
  178.       id = SHORT1FROMMR( WinSendDlgItemMsg( hwndModelessDlg,
  179.                                             LB_1,
  180.                                             LM_QUERYSELECTION,
  181.                                             0L,
  182.                                             0L ) );
  183.       if( id == LIT_NONE )
  184.         strcpy( szSelection, "" );
  185.       else
  186.         WinSendDlgItemMsg( hwndModelessDlg,
  187.                            LB_1,
  188.                            LM_QUERYITEMTEXT,
  189.                            MPFROM2SHORT( id, sizeof( szSelection ) ),
  190.                            MPFROMP( szSelection ) );
  191.       break;
  192.     case DLGSAMP_RBCOMPLETE:
  193.     case DLGSAMP_CBCOMPLETE:
  194.       break;
  195.     case DLGSAMP_DESTROYDLG:
  196.       WinDestroyWindow( hwndModelessDlg );
  197.       EnableModality( hwndFrame, TRUE );
  198.       WinInvalidateRect( hwnd, NULL, FALSE );/* Request whole window repaint */
  199.       break;
  200.     case WM_CLOSE:
  201.       WinPostMsg( hwnd, WM_QUIT, 0L, 0L );  /* Cause termination    */
  202.       break;
  203.     default:
  204.       return WinDefWindowProc( hwnd, message, mp1, mp2 );
  205.   }
  206.   return FALSE;
  207. }
  208.  
  209. /***********************************************************************
  210. *
  211. *  DlgProc:  fnwpEntryFieldDlg
  212. *
  213. *  A dialog proc which captures and validates the contents of two
  214. *  entry fields.
  215. *
  216. ***********************************************************************/
  217.  
  218. MRESULT EXPENTRY fnwpEntryFieldDlg( hwndDlg, message, mp1, mp2 )
  219. HWND    hwndDlg;
  220. USHORT  message;
  221. MPARAM  mp1;
  222. MPARAM  mp2;
  223. {
  224.   switch (message)
  225.   {
  226.     case WM_INITDLG:
  227.       CenterDlgBox( hwndDlg );
  228.       break;
  229.     case WM_COMMAND:
  230.       switch( SHORT1FROMMP( mp1 ) )
  231.       {
  232.         case DID_OK: /* Enter key or pushbutton pressed/ selected */
  233.  
  234. /***************************************************************************
  235. *
  236. *   Validate the contents of the two entry fields
  237. *
  238. ***************************************************************************/
  239.  
  240.           if( !IsIntInRange( hwndDlg, EF_1, 1, 100, ERR_EFINVALID, ID_MB ) )
  241.             return FALSE;
  242.           if( !IsIntInRange( hwndDlg, EF_2, 1, 100, ERR_EFINVALID, ID_MB ) )
  243.             return FALSE;
  244.  
  245. /***************************************************************************
  246. *
  247. *   A modal dialog is destroyed before control is returned to the
  248. *   invoking winproc, so it must pass the contents of its Entry Fields etc.
  249. *   back to the invoking window before it returns.
  250. *
  251. *   When a modeless dialog box returns it still continues to exist. It
  252. *   could pass the contents of its Entry Fields etc. back to the
  253. *   invoking window in several ways.
  254. *
  255. *   Here a user message is posted to the invoking window to say that the
  256. *   dialog has completed. The invoking window then has an opportunity
  257. *   to extract the contents of the Entry Fields etc.
  258. *
  259. ***************************************************************************/
  260.  
  261.           if( bModality )
  262.           {
  263.             WinQueryWindowText( WinWindowFromID( hwndDlg, EF_1 ),
  264.                                 sizeof( szEntryField1),
  265.                                 szEntryField1 );
  266.             WinQueryWindowText( WinWindowFromID( hwndDlg, EF_2 ),
  267.                                 sizeof( szEntryField2),
  268.                                 szEntryField2 );
  269.           }
  270.           else
  271.             WinPostMsg( hwndClient, DLGSAMP_EFCOMPLETE, 0L, 0L );
  272.  
  273.         case DID_CANCEL:/* Escape key or CANCEL pushbutton pressed/selected */
  274.           if( bModality )
  275.             WinDismissDlg( hwndDlg,TRUE );
  276.           else
  277.             WinPostMsg( hwndClient, DLGSAMP_DESTROYDLG, 0L, 0L );
  278.           return FALSE;
  279.         default:
  280.           break;
  281.       }
  282.       break;
  283.  
  284.     default:  /* Pass all other messages to the default dialog proc */
  285.       return WinDefDlgProc( hwndDlg, message, mp1, mp2 );
  286.   }
  287.   return FALSE;
  288. }
  289.  
  290. /***********************************************************************
  291. *
  292. *  DlgProc:  fnwpAutoRadioButtonDlg
  293. *
  294. *  A dialog procedure which uses auto radio buttons to change the
  295. *  color of the Client Area window.
  296. *
  297. ***********************************************************************/
  298.  
  299. MRESULT EXPENTRY fnwpAutoRadioButtonDlg( hwndDlg, message, mp1, mp2 )
  300. HWND    hwndDlg;
  301. USHORT  message;
  302. MPARAM  mp1;
  303. MPARAM  mp2;
  304. {
  305.   switch (message)
  306.   {
  307.     case WM_INITDLG:
  308.       colorSave = colorClient;
  309.       CenterDlgBox( hwndDlg );
  310.       if ( colorClient == CLR_RED )
  311.     WinPostMsg( WinWindowFromID( hwndDlg, RB_RED ),
  312.             BM_SETCHECK,
  313.             MPFROM2SHORT( TRUE, 0 ),
  314.             0L );
  315.  
  316.       if ( colorClient == CLR_GREEN )
  317.     WinPostMsg( WinWindowFromID( hwndDlg, RB_GREEN ),
  318.             BM_SETCHECK,
  319.             MPFROM2SHORT( TRUE, 0 ),
  320.             0L );
  321.  
  322.       if ( colorClient == CLR_BLUE )
  323.         WinPostMsg( WinWindowFromID( hwndDlg, RB_BLUE ),
  324.             BM_SETCHECK,
  325.             MPFROM2SHORT( TRUE, 0 ),
  326.             0L );
  327.  
  328.       break;
  329.     case WM_CONTROL:
  330.       if( SHORT2FROMMP( mp1 ) == BN_CLICKED )
  331.         switch( SHORT1FROMMP( mp1 ) )
  332.         {
  333.           case RB_RED:
  334.             colorClient = CLR_RED;
  335.             break;
  336.           case RB_GREEN:
  337.             colorClient = CLR_GREEN;
  338.             break;
  339.           case RB_BLUE:
  340.             colorClient = CLR_BLUE;
  341.             break;
  342.           default:
  343.            return FALSE;
  344.         }
  345.         WinInvalidateRect( hwndClient, NULL, FALSE );
  346.         break;
  347.     case WM_COMMAND:
  348.       switch( SHORT1FROMMP( mp1 ) )
  349.       {
  350.         case DID_OK:     /* Enter key or pushbutton pressed/ selected        */
  351.           if( !bModality )
  352.             WinPostMsg( hwndClient, DLGSAMP_RBCOMPLETE, 0L, 0L );
  353.           break;
  354.         case DID_CANCEL: /* Escape key or CANCEL pushbutton pressed/selected */
  355.           colorClient = colorSave;
  356.           break;
  357.         default:
  358.           return WinDefDlgProc( hwndDlg, message, mp1, mp2 );
  359.       }
  360.       if( bModality )
  361.         WinDismissDlg( hwndDlg, TRUE );
  362.       else
  363.         WinPostMsg( hwndClient, DLGSAMP_DESTROYDLG, 0L, 0L );
  364.       break;
  365.  
  366.     default:  /* Pass all other messages to the default dialog proc */
  367.       return WinDefDlgProc( hwndDlg, message, mp1, mp2 );
  368.   }
  369.   return FALSE;
  370. }
  371.  
  372.  
  373. /***********************************************************************
  374. *
  375. *  DlgProc:  fnwpCheckBoxDlg
  376. *
  377. *  A dialog procedure to which use checkboxes to change the color
  378. *  of the Client Area Window.
  379. *
  380. ***********************************************************************/
  381.  
  382. MRESULT EXPENTRY fnwpCheckBoxDlg( hwndDlg, message, mp1, mp2 )
  383. HWND    hwndDlg;
  384. USHORT  message;
  385. MPARAM  mp1;
  386. MPARAM  mp2;
  387. {
  388.   switch (message)
  389.   {
  390.     case WM_INITDLG:
  391.       CenterDlgBox( hwndDlg );
  392.       colorSave = colorClient;
  393.       if( (colorClient & CLR_RED) == CLR_RED )
  394.         WinPostMsg( WinWindowFromID( hwndDlg, CB_RED ),
  395.                     BM_SETCHECK,
  396.                     MPFROM2SHORT( TRUE,0 ),
  397.                     0L );
  398.       if( (colorClient & CLR_GREEN) == CLR_GREEN )
  399.         WinPostMsg( WinWindowFromID( hwndDlg, CB_GREEN ),
  400.                     BM_SETCHECK,
  401.                     MPFROM2SHORT( TRUE,0 ),
  402.                     0L );
  403.       if( (colorClient & CLR_BLUE) == CLR_BLUE )
  404.         WinPostMsg( WinWindowFromID( hwndDlg, CB_BLUE ),
  405.                     BM_SETCHECK,
  406.                     MPFROM2SHORT( TRUE,0 ),
  407.                     0L );
  408.       break;
  409.     case WM_CONTROL:                  /* User has clicked on a checkbox */
  410.       if( SHORT2FROMMP( mp1 ) == BN_CLICKED )
  411.         CheckColor( hwndDlg, SHORT1FROMMP( mp1 ), &colorClient );
  412.       WinInvalidateRect( hwndClient, NULL, FALSE );
  413.       break;
  414.     case WM_COMMAND:
  415.       switch( SHORT1FROMMP( mp1 ) )
  416.       {
  417.         case DID_OK:     /* Enter key or pushbutton pressed/ selected */
  418.           if( !bModality )
  419.             WinPostMsg( hwndClient, DLGSAMP_CBCOMPLETE, 0L, 0L );
  420.           break;
  421.         case DID_CANCEL: /* Escape key or CANCEL pushbutton pressed/selected */
  422.           colorClient = colorSave;
  423.           break;
  424.         default:
  425.           return WinDefDlgProc( hwndDlg, message, mp1, mp2 );
  426.       }
  427.       if( bModality )
  428.         WinDismissDlg( hwndDlg, TRUE );
  429.       else
  430.         WinPostMsg( hwndClient, DLGSAMP_DESTROYDLG, 0L, 0L );
  431.       return FALSE;
  432.  
  433.     default:  /* Pass all other messages to the default dialog proc */
  434.       return WinDefDlgProc( hwndDlg, message, mp1, mp2 );
  435.   }
  436.   return FALSE;
  437. }
  438.  
  439. /***********************************************************************
  440. *
  441. *  DlgProc:  fnwpListBoxDlg
  442. *
  443. ***********************************************************************/
  444.  
  445. MRESULT EXPENTRY fnwpListBoxDlg( hwndDlg, message, mp1, mp2 )
  446. HWND    hwndDlg;
  447. USHORT  message;
  448. MPARAM  mp1;
  449. MPARAM  mp2;
  450. {
  451.   CHAR szBuffer[LEN_LISTBOXENTRY];
  452.   SHORT  i;
  453.   SHORT  id;
  454.  
  455.   switch (message)
  456.   {
  457.     case WM_INITDLG:
  458.       CenterDlgBox( hwndDlg );
  459.  
  460. /*************************************************************************
  461. *
  462. *   Initialize the listbox with a set of strings loaded from a
  463. *   resource file.
  464. *
  465. *************************************************************************/
  466.  
  467.       for ( i = 0; i < NUM_LISTBOXENTRIES; i++ )
  468.       {
  469.         WinLoadString( hab,
  470.                        (HMODULE) NULL,
  471.                        LBI_1 + i,
  472.                        LEN_LISTBOXENTRY,
  473.                        (PSZ)szBuffer
  474.                      );
  475.         WinSendDlgItemMsg( hwndDlg,
  476.                            LB_1,
  477.                            LM_INSERTITEM,
  478.                            MPFROM2SHORT( LIT_END, 0 ),
  479.                            MPFROMP( szBuffer )
  480.                          );
  481.       }
  482.       break;
  483.     case WM_COMMAND:
  484.       switch( SHORT1FROMMP( mp1 ) )
  485.       {
  486.         case DID_OK:     /* Enter key or pushbutton pressed/ selected */
  487.           if( bModality )
  488.           {
  489.  
  490. /***********************************************************************
  491. *
  492. *   Find out which item (if any) was selected and return the selected
  493. *   item text.
  494. *
  495. ***********************************************************************/
  496.  
  497.             id = SHORT1FROMMR( WinSendDlgItemMsg( hwndDlg,
  498.                                                   LB_1,
  499.                                                   LM_QUERYSELECTION,
  500.                                                   0L,
  501.                                                   0L ) );
  502.             if( id == LIT_NONE )
  503.               strcpy( szSelection, "" );
  504.             else
  505.               WinSendDlgItemMsg( hwndDlg,
  506.                                  LB_1,
  507.                                  LM_QUERYITEMTEXT,
  508.                                  MPFROM2SHORT( id, LEN_LISTBOXENTRY ),
  509.                                  MPFROMP( szSelection ) );
  510.           }
  511.           else
  512.             WinPostMsg( hwndClient, DLGSAMP_LBCOMPLETE, 0L, 0L );
  513.         case DID_CANCEL: /* Escape key or CANCEL pushbutton pressed/selected */
  514.           if( bModality )
  515.             WinDismissDlg( hwndDlg, TRUE );
  516.           else
  517.             WinPostMsg( hwndClient, DLGSAMP_DESTROYDLG, 0L, 0L );
  518.           return FALSE;
  519.         default:
  520.           break;
  521.       }
  522.       break;
  523.  
  524.     default:  /* Pass all other messages to the default dialog proc */
  525.       return WinDefDlgProc( hwndDlg, message, mp1, mp2 );
  526.   }
  527.   return FALSE;
  528. }
  529.  
  530. /*************************************************************************
  531. *
  532. *   FUNCTION : CenterDlgBox
  533. *
  534. *   Positions the dialog box in the center of the screen
  535. *
  536. *************************************************************************/
  537.  
  538. VOID cdecl CenterDlgBox( hwnd )
  539. HWND hwnd;
  540. {
  541.   SHORT ix, iy;
  542.   SHORT iwidth, idepth;
  543.   SWP   swp;
  544.  
  545.   iwidth = (SHORT)WinQuerySysValue( HWND_DESKTOP, SV_CXSCREEN );
  546.   idepth = (SHORT)WinQuerySysValue( HWND_DESKTOP, SV_CYSCREEN );
  547.   WinQueryWindowPos( hwnd, (PSWP)&swp );
  548.   ix = ( iwidth  - swp.cx ) / 2;
  549.   iy = ( idepth  - swp.cy ) / 2;
  550.   WinSetWindowPos( hwnd, HWND_TOP, ix, iy, 0, 0, SWP_MOVE );
  551. }
  552.  
  553. /***************************************************************************
  554. *
  555. *  FUNCTION: CheckColor
  556. *
  557. *  Toggle the Checked/UnChecked state of a checkbox and add/remove
  558. *  the corresponding CLR color component of the Client Area Color.
  559. *
  560. ***************************************************************************/
  561.  
  562. VOID cdecl CheckColor( hwndDlg, iDlgItem, colorClient )
  563. HWND   hwndDlg;
  564. SHORT  iDlgItem;
  565. COLOR *colorClient;
  566. {
  567.   BOOL  bChecked;
  568.   COLOR color;
  569.  
  570.   switch( iDlgItem )
  571.   {
  572.     case CB_RED:
  573.       color = CLR_RED;
  574.       break;
  575.     case CB_GREEN:
  576.       color = CLR_GREEN;
  577.       break;
  578.     case CB_BLUE:
  579.       color = CLR_BLUE;
  580.       break;
  581.     default:
  582.       return;
  583.   }
  584.  
  585.   bChecked = SHORT1FROMMR( WinSendMsg( WinWindowFromID( hwndDlg , iDlgItem ),
  586.                                        BM_QUERYCHECK,
  587.                                        0L,
  588.                                        0L ) );
  589.   WinPostMsg( WinWindowFromID( hwndDlg, iDlgItem ),
  590.               BM_SETCHECK,
  591.               MPFROM2SHORT( !bChecked, 0 ),
  592.               0L );
  593.   if( bChecked )                   /* If color previously checked */
  594.     *colorClient -= color;         /* subtract it ... else        */
  595.   else
  596.     *colorClient += color;         /* ... add it.                 */
  597. }
  598.  
  599. /**************************************************************************
  600. *
  601. *   FUNCTION: EnableModality
  602. *
  603. *   Enable or disable the Modality menuitems depending on the value
  604. *   of modal. This is done to prevent the user from altering the
  605. *   modality setting while a modeless dialog is active.
  606. *
  607. **************************************************************************/
  608.  
  609. VOID cdecl EnableModality( hwnd, bModal )
  610. HWND hwnd;
  611. BOOL bModal;
  612. {
  613.   if( bModal )
  614.   {
  615.     WinPostMsg( WinWindowFromID( hwnd, FID_MENU ),
  616.                 MM_SETITEMATTR,
  617.                 MPFROM2SHORT( MI_MODAL, TRUE ),
  618.                 MPFROM2SHORT( MIA_DISABLED, ~MIA_DISABLED ) );
  619.     WinPostMsg( WinWindowFromID( hwnd, FID_MENU ),
  620.                 MM_SETITEMATTR,
  621.                 MPFROM2SHORT( MI_MODELESS, TRUE ),
  622.                 MPFROM2SHORT( MIA_DISABLED, ~MIA_DISABLED ) );
  623.   }
  624.   else
  625.   {
  626.     WinPostMsg( WinWindowFromID( hwnd, FID_MENU ),
  627.                 MM_SETITEMATTR,
  628.                 MPFROM2SHORT( MI_MODAL, TRUE ),
  629.                 MPFROM2SHORT( MIA_DISABLED, MIA_DISABLED ) );
  630.     WinPostMsg( WinWindowFromID( hwnd, FID_MENU ),
  631.                 MM_SETITEMATTR,
  632.                 MPFROM2SHORT( MI_MODELESS, TRUE ),
  633.                 MPFROM2SHORT( MIA_DISABLED, MIA_DISABLED ) );
  634.   }
  635. }
  636.  
  637. /***************************************************************************
  638. *
  639. *  FUNCTION: IsIntInRange.
  640. *
  641. *  Checks whether the value of a dialog item is in an integer in
  642. *  a given range.
  643. *
  644. ***************************************************************************/
  645.  
  646. BOOL cdecl IsIntInRange( hwndDlg,  idEntryField,
  647.                          iLoRange, iHiRange,
  648.                          idErrMsg, idMessageBox )
  649. HWND hwndDlg;
  650. SHORT  idEntryField;
  651. SHORT  iLoRange;
  652. SHORT  iHiRange;
  653. SHORT  idErrMsg;
  654. SHORT  idMessageBox;
  655. {
  656.   SHORT ivalue;
  657.   CHAR  szErrMsg[80];
  658.  
  659. /****************************************************************************
  660. *
  661. *   Validate an entry field.
  662. *
  663. *   If validation fails leave the dialog visible, issue an error message
  664. *   using a messagebox, and when the user dismisses the messagebox,
  665. *   set the input focus to the entry field containing the error. Leave
  666. *   the contents of the entry field unchanged, and return FALSE.
  667. *
  668. *   If validation is successful return the value in ivalue and return
  669. *   TRUE.
  670. *
  671. ****************************************************************************/
  672.  
  673.   if( !WinQueryDlgItemShort( hwndDlg, idEntryField, &ivalue, TRUE ) ||
  674.       ( ivalue < iLoRange ) ||
  675.       ( ivalue > iHiRange ) )
  676.   {
  677.     WinLoadString( hab, (HMODULE) NULL, idErrMsg, sizeof( szErrMsg ), szErrMsg );
  678.     WinMessageBox( HWND_DESKTOP,
  679.                    hwndFrame,
  680.                    (PSZ)szErrMsg,
  681.                    NULL,
  682.                    idMessageBox,
  683.                    MB_OK );
  684.     WinSetFocus( HWND_DESKTOP, WinWindowFromID( hwndDlg, idEntryField ) );
  685.     return FALSE;
  686.   }
  687.   else
  688.     return TRUE;
  689. }
  690.  
  691. /***********************************************************************
  692. *
  693. *  FUNCTION: LoadDialog
  694. *
  695. *  Use the appropriate functions to put up a modal or modeless
  696. *  dialog box depending on the setting of the bModality parameter.
  697. *
  698. ***********************************************************************/
  699.  
  700. VOID cdecl LoadDialog( hwndParent, hwndOwner, idDlg, fnwpDlgProc, bModality )
  701. HWND  hwndParent;
  702. HWND  hwndOwner;
  703. SHORT idDlg;
  704. PFNWP fnwpDlgProc;
  705. BOOL  bModality;
  706. {
  707.   EnableModality( hwndOwner, FALSE ); /* Disable the Modality menu item */
  708.  
  709.   if( bModality )
  710.   {
  711.     WinDlgBox( hwndParent,        /* Parent                    */
  712.                hwndOwner,         /* Owner                     */
  713.                fnwpDlgProc,       /* Address of dialog proc    */
  714.                (HMODULE) NULL,              /* Module handle             */
  715.                idDlg,             /* Id of dialog in resource  */
  716.                NULL );            /* Initialisation data       */
  717.     EnableModality( hwndOwner, TRUE ); /* Enable the Modality menu item */
  718.   }
  719.   else
  720.   {
  721.     /*******************************************************************
  722.     *
  723.     *   Check to see if a modeless dialog is already running: if
  724.     *   so destroy it before for loading the requested dialog. Save
  725.     *   the handle of the new dialog in a global variable so that in
  726.     *   can be accessed by the WinProc that issued LoadDialog.
  727.     *
  728.     *******************************************************************/
  729.  
  730.     if( WinIsWindow( hab, hwndModelessDlg ) )
  731.       WinDestroyWindow( hwndModelessDlg );
  732.     hwndModelessDlg = WinLoadDlg( hwndParent,
  733.                                   hwndOwner,
  734.                                   fnwpDlgProc,
  735.                                   (HMODULE) NULL,
  736.                                   idDlg,
  737.                                   NULL );
  738.   }
  739. }
  740.  
  741. /*************************************************************************
  742. *
  743. *   FUNCTION: MainWndCommand
  744. *
  745. *   Take the appropriate action when a WM_COMMAND message is received by
  746. *   MainWndProc.  Issues calls which load dialogs in the prevailing state
  747. *   of modality.
  748. *
  749. *************************************************************************/
  750.  
  751. VOID cdecl MainWndCommand( hwnd, Command, bModality )
  752. HWND   hwnd;
  753. USHORT Command;
  754. BOOL   *bModality;
  755. {
  756.   USHORT idDlg;
  757.   PFNWP  pfnDlgProc;
  758.  
  759.   switch( Command )
  760.   {
  761.     case MI_MODAL:
  762.     case MI_MODELESS:
  763.       *bModality = ( Command == MI_MODAL ) ? TRUE
  764.                                            : FALSE;
  765.       SetModality( WinQueryWindow( hwnd, QW_PARENT, FALSE ), *bModality );
  766.       WinInvalidateRect( hwnd, NULL, FALSE );
  767.       return;
  768.     case MI_ENTRYFIELDEXAMPLE:
  769.       idDlg      = DLG_ENTRYFIELDEXAMPLE;
  770.       pfnDlgProc = (PFNWP)fnwpEntryFieldDlg;
  771.       break;
  772.     case MI_AUTORADIOBUTTONEXAMPLE:
  773.       idDlg      = DLG_AUTORADIOBUTTONEXAMPLE;
  774.       pfnDlgProc = (PFNWP)fnwpAutoRadioButtonDlg;
  775.       break;
  776.     case MI_CHECKBOXEXAMPLE:
  777.       idDlg      = DLG_CHECKBOXEXAMPLE;
  778.       pfnDlgProc = (PFNWP)fnwpCheckBoxDlg;
  779.       break;
  780.     case MI_LISTBOXEXAMPLE:
  781.       idDlg      = DLG_LISTBOXEXAMPLE;
  782.       pfnDlgProc = (PFNWP)fnwpListBoxDlg;
  783.       break;
  784.     case MI_ABOUTBOX:
  785.       WinDlgBox(HWND_DESKTOP, hwnd, fnwpAboutBoxDlg, (HMODULE) NULL, DLG_ABOUT, NULL);
  786.       return;
  787.     default:
  788.       return;
  789.   }
  790.   LoadDialog( HWND_DESKTOP,
  791.               hwndFrame,
  792.               idDlg,
  793.               pfnDlgProc,
  794.               *bModality );
  795.   if( *bModality )
  796.     WinInvalidateRect( hwnd, NULL, FALSE );  /* Request whole window repaint */
  797. }
  798.  
  799. /************************************************************************
  800. *
  801. *   FUNCTION: MainWndPaint
  802. *
  803. *   An unsophisticated window painting routine which simply repaints the
  804. *   entire window when a WM_PAINT message is received. In a real
  805. *   application more sophisticated techniques could be used to determine
  806. *   the minimum region needing repainting, and to paint only that
  807. *   region
  808. *
  809. ************************************************************************/
  810.  
  811. VOID cdecl MainWndPaint( hwnd )
  812. HWND hwnd;
  813. {
  814.   POINTL   pointl;
  815.   HPS      hps;                          /* Presentation space handle */
  816.   RECTL    rcl;                          /* Window rectangle          */
  817.   CHAR     string[50];
  818.  
  819.   hps = WinBeginPaint( hwnd, (HPS)NULL, (PRECTL)&rcl );
  820.   /*
  821.     Color in the background
  822.   */
  823.   switch ((int) colorClient) {
  824.     case 0:        /* (r,g,b) = (0,0,0) */
  825.         WinFillRect( hps, (PRECTL)&rcl, CLR_BLACK );
  826.         break;
  827.     case 7:        /* (r,g,b) = (1,1,1) */
  828.         WinFillRect( hps, (PRECTL)&rcl, CLR_WHITE );
  829.         break;
  830.     default:
  831.         WinFillRect( hps, (PRECTL)&rcl, colorClient );
  832.         break;
  833.   }
  834.   /*
  835.     Set the text character colors
  836.   */
  837.   GpiSetColor( hps, (colorClient == 0L) ? CLR_WHITE
  838.                                         : CLR_BLACK );
  839.   pointl.x = 10L; pointl.y = 70L;
  840.   strcpy( string, "Dialog modality    = " );
  841.   strcat( string, (bModality) ? "Modal"
  842.                               : "Modeless" );
  843.   GpiCharStringAt( hps, &pointl, (LONG)strlen( string ), (PSZ)string );
  844.   pointl.y = 50L;
  845.   strcpy( string, "Entry Field 1      = " );
  846.   strcat( string, szEntryField1 );
  847.   GpiCharStringAt( hps, &pointl, (LONG)strlen( string ), (PSZ)string );
  848.   pointl.y = 30L;
  849.   strcpy( string, "Entry Field 2      = " );
  850.   strcat( string, szEntryField2 );
  851.   GpiCharStringAt( hps, &pointl, (LONG)strlen( string ), (PSZ)string );
  852.   pointl.y = 10L;
  853.   strcpy( string, "List Box Selection = " );
  854.   strcat( string, szSelection );
  855.   GpiCharStringAt( hps, &pointl, (LONG)strlen( string ), (PSZ)string );
  856.   WinEndPaint( hps );
  857.  }
  858.  
  859. /**************************************************************************
  860. *
  861. *  FUNCTION: SetModality
  862. *
  863. *  Check or uncheck Modal and Modeless menu items as appropriate.
  864. *
  865. **************************************************************************/
  866.  
  867. VOID cdecl SetModality( hwnd, bModal )
  868. HWND hwnd;
  869. BOOL bModal;
  870. {
  871.   WinPostMsg( WinWindowFromID( hwnd, FID_MENU ),
  872.               MM_SETITEMATTR,
  873.               MPFROM2SHORT( MI_MODAL, TRUE ),
  874.               MPFROM2SHORT( MIA_CHECKED, (bModal) ? ( MIA_CHECKED)
  875.                                                   : (~MIA_CHECKED) ) );
  876.  
  877.   WinPostMsg( WinWindowFromID( hwnd, FID_MENU ),
  878.               MM_SETITEMATTR,
  879.               MPFROM2SHORT( MI_MODELESS, TRUE ),
  880.               MPFROM2SHORT( MIA_CHECKED, (bModal) ? (~MIA_CHECKED)
  881.                                                   : ( MIA_CHECKED) ) );
  882.  
  883. }
  884.  
  885. MRESULT EXPENTRY fnwpAboutBoxDlg(hDlg, msg, mp1, mp2)
  886. /*
  887.     About... dialog procedure
  888. */
  889. HWND    hDlg;
  890. USHORT    msg;
  891. MPARAM    mp1;
  892. MPARAM    mp2;
  893. {
  894.     switch(msg) {
  895.     case WM_COMMAND:
  896.         switch(COMMANDMSG(&msg)->cmd) {
  897.         case DID_OK: WinDismissDlg(hDlg, TRUE); break;
  898.         default: break;
  899.         }
  900.     default: return WinDefDlgProc(hDlg, msg, mp1, mp2);
  901.     }
  902.     return FALSE;
  903. }
  904.