home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dialog.zip / DIALOG.C next >
C/C++ Source or Header  |  1998-04-20  |  19KB  |  496 lines

  1. /*************************************************************************
  2. *
  3. *  File Name   : Dialog.c
  4. *
  5. *  Description : This program demonstrates how to associate a pop-up
  6. *                window with a standard window.  The pop-up window
  7. *                is defined as a dialog template in a resource file.
  8. *
  9. *  Concepts    : This program displays a standard window.  The action bar
  10. *                contains a single choice:  Options.  The Options
  11. *                pull-down contains a choice that displays a pop-up
  12. *                window which contains an entry field, and OK and Cancel
  13. *                pushbuttons.  When the user types a place name in the
  14. *                entry field and selects OK, the pop-up window
  15. *                disappears, and the place name is displayed at the end
  16. *                of the text "You live in".
  17. *
  18. *  API's       :
  19. *  -------------
  20. *  GpiCharStringAt       WinDestroyWindow    WinInvalidateRegion
  21. *  GpiSetColor           WinDismissDlg       WinLoadString
  22. *  WinBeginPaint         WinDispatchMsg      WinPostMsg
  23. *  WinCreateMsgQueue     WinDlgBox           WinQueryWindowText
  24. *  WinCreateStdWindow    WinEndPaint         WinRegisterClass
  25. *  WinDefDlgProc         WinFillRect         WinSendMsg
  26. *  WinDefWindowProc      WinGetMsg           WinTerminate
  27. *  WinDestroyMsgQueue    WinInitialize       WinWindowFromID
  28. *
  29. *  Copyright (C) 1992 IBM Corporation
  30. *
  31. *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  32. *      sample code created by IBM Corporation. This sample code is not
  33. *      part of any standard or IBM product and is provided to you solely
  34. *      for  the purpose of assisting you in the development of your
  35. *      applications.  The code is provided "AS IS", without
  36. *      warranty of any kind.  IBM shall not be liable for any damages
  37. *      arising out of your use of the sample code, even if they have been
  38. *      advised of the possibility of such damages.                                                    *
  39. *
  40. ************************************************************************/
  41.  
  42. #define INCL_WINSYS                     /* Selectively include          */
  43. #define INCL_WINFRAMEMGR                /* relevant parts of            */
  44. #define INCL_WINMENUS                   /* the PM header file           */
  45. #define INCL_WINDIALOGS
  46. #define INCL_GPI
  47.  
  48. #include <os2.h>
  49. #include <string.h>
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include "dialog.h"                     /* Resource symbolic identifiers*/
  53.  
  54. #define LENGTH_STRING  33               /* Length of string             */
  55. #define RETURN_ERROR    1               /* return value for exit        */
  56. #define BEEP_WARN_FREQ      60          /* frequency of warning beep    */
  57. #define BEEP_WARN_DUR      100          /* duration of warning beep     */
  58. #define MAXITEMS      8                 /* numer of menu items in FCF_SYSMENU*/
  59.  
  60. /* Function prototypes                                                  */
  61. int main(VOID);
  62. MRESULT EXPENTRY MyWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  63. MRESULT EXPENTRY MyDlgProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  64. MRESULT EXPENTRY ProdInfoDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  65. VOID DisplayMessage( PCH str );
  66. VOID SetSysMenu(HWND hwnd);
  67.                                         /* Define parameters by type    */
  68. HAB   hab;                              /* Anchor block handle          */
  69. HMQ   hmq;                              /* Message queue handle         */
  70. HWND  hwndClient;                       /* Client window handle         */
  71. ULONG flCreate = FCF_STANDARD;          /* Frame control flag           */
  72. BOOL  bComplete = FALSE;                /* Switch for first time through*/
  73. HWND  hwndFrame;                        /* Frame window handle          */
  74. CHAR  szLocation[LENGTH_STRING] = "";   /* To hold text entered by user */
  75. CHAR  szResponse[LENGTH_STRING] = "";   /* To hold "You live in"        */
  76.  
  77. /****************************************************************
  78.  *  Name:   main()
  79.  *
  80.  *  Description: Entry point of program.
  81.  *
  82.  *  Concepts: Obtains anchor block handle and creates message
  83.  *            queue.  Calls the initialization routine.
  84.  *            Creates the main frame window which creates the
  85.  *            main client window.  Polls the message queue
  86.  *            via Get/Dispatch Msg loop.  Upon exiting the
  87.  *            loop, exits.
  88.  *
  89.  *  API's   :  WinInitilize
  90.  *             DosBeep
  91.  *             WinCreateMsgQueue
  92.  *             WinTerminate
  93.  *             WinRegisterClass
  94.  *             WinCreateStdWindow
  95.  *             WinSetWindowText
  96.  *             WinGetMsg
  97.  *             WinDispatchMsg
  98.  *             WinDestroyWindow
  99.  *             WinDestroyMsgQueue
  100.  *
  101.  *  Parameters: None
  102.  *
  103.  *  Returns:
  104.  *          1 - if sucessful execution completed
  105.  *          0 - if error
  106. \****************************************************************/
  107. int main(VOID)
  108. {
  109.   QMSG  qmsg;
  110.   BOOL success;
  111.  
  112.   hab = WinInitialize( 0 );
  113.   if(!hab)
  114.   {
  115.     DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  116.     exit(RETURN_ERROR);
  117.   }
  118.  
  119.   hmq = WinCreateMsgQueue( hab, 0 );
  120.   if(!hmq)
  121.   {
  122.     DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  123.     WinTerminate(hab);
  124.     exit(RETURN_ERROR);
  125.   }
  126.  
  127.   success = WinDlgBox( HWND_DESKTOP,    /* Place anywhere on desktop    */
  128.                        HWND_DESKTOP,    /* Owned by desk top            */
  129.                        (PFNWP)ProdInfoDlgProc,   /* Addr. of procedure  */
  130.                        (HMODULE)0,      /* Module handle                */
  131.                        ID_PRODINFO,     /* Dialog identifier in resource*/
  132.                        NULL);           /* Initialization data          */
  133.   if (!success)
  134.   {
  135.     DisplayMessage("Could Not Register Class");
  136.     exit(RETURN_ERROR);
  137.   }
  138.  
  139.   success = WinRegisterClass(           /* Register window class        */
  140.        hab,                             /* Anchor block handle          */
  141.        "MyWindow",                      /* Window class name            */
  142.        MyWindowProc,                    /* Address of window procedure  */
  143.        CS_SIZEREDRAW,                   /* Class style                  */
  144.        0                                /* No extra window words        */
  145.        );
  146.   if (!success)
  147.   {
  148.     DisplayMessage("Could Not Register Class");
  149.     exit(RETURN_ERROR);
  150.   }
  151.  
  152.   hwndFrame = WinCreateStdWindow(
  153.         HWND_DESKTOP,                   /* Desktop window is parent     */
  154.         WS_VISIBLE,                     /* Frame style                  */
  155.         &flCreate,                      /* Frame control flag           */
  156.         "MyWindow",                     /* Window class name            */
  157.         "",                             /* Window title                 */
  158.         0L,                             /* Client style of VISIBLE      */
  159.         (HMODULE)NULL,                  /* Resource is in .EXE file     */
  160.         ID_MAINWND,                     /* Frame window identifier      */
  161.         &hwndClient);                   /* Client window handle         */
  162.  
  163.   if (!hwndFrame)
  164.   {
  165.     DisplayMessage("Could Not Create Main Window");
  166.     exit(RETURN_ERROR);
  167.   }
  168.  
  169.   WinSetWindowText(hwndFrame, "Dialog - Sample Program");
  170.  
  171. /*
  172.  * Get and dispatch messages from the application message queue
  173.  * until WinGetMsg returns FALSE, indicating a WM_QUIT message.
  174.  */
  175.   while( WinGetMsg( hab, &qmsg, (HWND)NULL, 0, 0 ) )
  176.     WinDispatchMsg( hab, &qmsg );
  177.  
  178.   WinDestroyWindow( hwndFrame );        /* Tidy up...                   */
  179.   WinDestroyMsgQueue( hmq );            /* and                          */
  180.   WinTerminate( hab );                  /* terminate the application    */
  181.   return(0);
  182. }
  183. /***********************  End of main procedure  ************************/
  184.  
  185. /****************************************************************
  186.  *  Name:   MyWndProc
  187.  *
  188.  *  Description : Window procedure for the main clent window.
  189.  *
  190.  *  Concepts : Processes the messages sent to the main client
  191.  *             window.
  192.  *
  193.  *  API's : WinLoadString
  194.  *          WinDlgBox
  195.  *          WinInvalidateRegion
  196.  *          WinPostMsg
  197.  *          WinDefDlgProc
  198.  *          WinBeginPaint
  199.  *          WinFillRect
  200.  *          GpiCharStringAt
  201.  *          GpiSetColor
  202.  *          WinEndPaint
  203.  *
  204.  * Parameters   : hwnd - Window handle to which message is addressed
  205.  *                msg - Message type
  206.  *                mp1 - First message parameter
  207.  *                mp2 - Second message parameter
  208.  *
  209.  *  Returns:  Return values are determined by each message
  210.  *
  211.  ****************************************************************/
  212. MRESULT EXPENTRY MyWindowProc ( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  213. {
  214.   switch( msg )
  215.   {
  216.     case WM_CREATE:
  217.       /*
  218.        * Window initialization - load "You live in" string from resource
  219.        */
  220.       WinLoadString( hab,
  221.                      (HMODULE)NULL,        /* Resource is in .EXE file  */
  222.                      ID_RESPONSE,          /* String identifier         */
  223.                      sizeof( szResponse ), /* Size of buffer            */
  224.                      (PSZ)szResponse);     /* Buffer                    */
  225.       break;
  226.     case WM_COMMAND:
  227.       /*
  228.        * When the user chooses "Where..." from the Options pull-down,
  229.        * the application displays a dialog box. WinDlgBox calls the
  230.        * dialog procedure below. WinDlgBox does not return until the
  231.        * dialog procedure returns.
  232.        * WinInvalidateRegion sends a WM_PAINT message.
  233.        */
  234.       switch ( SHORT1FROMMP( mp1 ) )
  235.       {
  236.         case ID_WHERE:
  237.  
  238.          WinDlgBox( HWND_DESKTOP,       /* Place anywhere on desktop    */
  239.                      hwndFrame,         /* Owned by frame               */
  240.                      MyDlgProc,         /* Address of dialog procedure  */
  241.                      (HMODULE)0,        /* Module handle                */
  242.                      ID_MYDIALOG,       /* Dialog identifier in resource*/
  243.                      NULL);             /* Initialization data          */
  244.  
  245.           WinInvalidateRegion( hwnd, NULLHANDLE, FALSE ); /* Force a repaint */
  246.  
  247.           break;
  248.         case ID_EXITPROGRAM:
  249.  
  250.           WinPostMsg( hwnd, WM_CLOSE, NULL, NULL);
  251.  
  252.           break;
  253.         default:
  254.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  255.       }
  256.       break;
  257.     case WM_PAINT:
  258.       /*
  259.        * Window contents are drawn here. First time through, bComplete
  260.        * is FALSE, so window is simply filled with SYSCLR_WINDOW.
  261.        * On subsequent passes, if bComplete has been set to TRUE in the
  262.        * dialog procedure, GpiCharStringAt draws the text.
  263.        */
  264.       {
  265.        HPS    hps;                      /* Presentation space handle    */
  266.        POINTL pt;                       /* String screen coordinates    */
  267.        RECTL  rc;                       /* Window rectangle             */
  268.  
  269.         hps = WinBeginPaint( hwnd, (HPS)NULL, &rc );
  270.         WinFillRect( hps, &rc, SYSCLR_WINDOW );
  271.         if( bComplete)
  272.         {
  273.           GpiSetColor( hps, CLR_NEUTRAL ); /* Text color same as PM       */
  274.           pt.x = 50L; pt.y = 50L;         /* Coordinates of "You live in" */
  275.           GpiCharStringAt( hps, &pt, (LONG)strlen( szResponse ), szResponse );
  276.           pt.x = 50L; pt.y = 30L;         /* Coordinates of location      */
  277.           GpiCharStringAt( hps, &pt, (LONG)strlen( szLocation ), szLocation );
  278.         }
  279.         WinEndPaint( hps );               /* Drawing is complete          */
  280.       }
  281.       break;
  282.     case WM_CLOSE:
  283.       WinPostMsg( hwnd, WM_QUIT, NULL, NULL);     /* Cause termination    */
  284.       break;
  285.     default:
  286.       /*
  287.        * Must have WinDefWindowProc in your window procedure
  288.        */
  289.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  290.   }
  291.   return (MRESULT) FALSE;
  292. }
  293. /*********************** End of window procedure  ***********************/
  294.  
  295. /*********************************************************************
  296.  *  Name : MyDlgProc
  297.  *
  298.  *  Description : Processes all messages sent to the dialog.
  299.  *
  300.  *  Concepts : Called for each message sent to the dialog box.
  301.  *             A switch statement branches to the routines to be
  302.  *             performed for each message processed.  Any messages
  303.  *             not specifically process are passed to the default
  304.  *             window procedure WinDefDlgProc().
  305.  *
  306.  *  API's : WinQueryWindowText
  307.  *          WinDismissDlg
  308.  *
  309.  * Parameters   : hwnd - Window handle to which message is addressed
  310.  *                msg - Message type
  311.  *                mp1 - First message parameter
  312.  *                mp2 - Second message parameter
  313.  *
  314.  *  Returns : Dependent upon message sent
  315.  *
  316.  ****************************************************************/
  317. MRESULT EXPENTRY MyDlgProc( HWND hwndDlg, ULONG msg, MPARAM mp1, MPARAM mp2 )
  318. {
  319.   switch ( msg )
  320.   {
  321.     case WM_INITDLG:
  322.        SetSysMenu(hwndDlg);             /* remove grayed items from sys menu*/
  323.        break;
  324.  
  325.     case WM_COMMAND:                    /* Posted by pushbutton or key  */
  326.       /*
  327.        * PM sends a WM_COMMAND message when the user presses either
  328.        * the Enter or Escape pushbuttons.
  329.        */
  330.       switch( SHORT1FROMMP( mp1 ) )     /* Extract the command value    */
  331.       {
  332.         case DID_OK:                    /* The Enter pushbutton or key. */
  333.                                         /* WinQueryWindowText copies the*/
  334.                                         /* contents of the entry field  */
  335.                                         /* into szLocation.             */
  336.           WinQueryWindowText( WinWindowFromID( hwndDlg, ID_ENTRYFIELD ),
  337.                               LENGTH_STRING,
  338.                               szLocation );
  339.           bComplete = TRUE;             /* Set switch so strings are    */
  340.                                         /* drawn in WM_PAINT processing.*/
  341.         case DID_CANCEL:         /* The Cancel pushbutton or Escape key */
  342.           WinDismissDlg( hwndDlg, TRUE );  /* Removes the dialog box    */
  343.           return (MRESULT) FALSE;
  344.         default:
  345.           break;
  346.       }
  347.       break;
  348.     default:
  349.       /*
  350.        * Any event messages that the dialog procedure has not processed
  351.        * come here and are processed by WinDefDlgProc.
  352.        * This call MUST exist in your dialog procedure.
  353.        */
  354.       return WinDefDlgProc( hwndDlg, msg, mp1, mp2 );
  355.   }
  356.   return (MRESULT) FALSE;
  357. }
  358. /********************** End of dialog procedure  ************************/
  359.  
  360. /*********************************************************************
  361.  *  Name : ProdInfoDlgProc
  362.  *
  363.  *  Description : Processes all messages sent to the Product
  364.  *                Information dialog box.
  365.  *
  366.  *  Concepts : Called for each message sent to the Product
  367.  *             Information dialog box.  The Product
  368.  *             Information box only has a button control so
  369.  *             this routine only processes WM_COMMAND
  370.  *             messages.  Any WM_COMMAND posted must have come
  371.  *             from the Ok button so we dismiss the dialog
  372.  *             upon receiving it.
  373.  *
  374.  *  API's : WinDismissDlg
  375.  *          WinDefDlgProc
  376.  *
  377.  * Parameters   : hwnd - Window handle to which message is addressed
  378.  *                msg - Message type
  379.  *                mp1 - First message parameter
  380.  *                mp2 - Second message parameter
  381.  *
  382.  *  Returns : Dependent upon message sent
  383.  *
  384.  ****************************************************************/
  385. MRESULT EXPENTRY ProdInfoDlgProc( HWND hwnd, ULONG msg, MPARAM mp1,
  386.                                   MPARAM mp2)
  387. {
  388.    switch(msg)
  389.    {
  390.       case WM_INITDLG:
  391.          SetSysMenu(hwnd);              /* remove grayed items from sys menu*/
  392.          break;
  393.  
  394.       case WM_COMMAND:
  395.          /*
  396.           * No matter what the command, close the dialog
  397.           */
  398.          WinDismissDlg(hwnd, TRUE);
  399.          break;
  400.  
  401.       default:
  402.          return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  403.          break;
  404.    }
  405.    return (MRESULT)0;
  406. }
  407. /********************** End ofProdinfo dialog procedure ****************/
  408.  
  409. /****************************************************************
  410.  *  Name:   DisplayMessage
  411.  *
  412.  *  Description : Displays error messages.
  413.  *
  414.  *  Concepts : Displays a message box containing an error
  415.  *             message each time called.
  416.  *
  417.  *  API's : WinMessageBoxNon
  418.  *
  419.  * Parameters   : str - error message to display
  420.  *
  421.  *  Returns:  Void
  422.  *
  423.  ****************************************************************/
  424. VOID DisplayMessage(PCH str)
  425. {
  426.   /*
  427.    * This routine calls WinMessageBox to dsplay error messages to the
  428.    * user.
  429.    */
  430.  
  431.   WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (PCH) str,
  432.      (PCH) "Error Messages For The Dialog Sample", ID_MESSAGEBOX,
  433.      MB_OK | MB_APPLMODAL | MB_MOVEABLE | MB_ERROR);
  434. }
  435. /******************* End of DisplayMessage procedure ********************/
  436. /***********************************************************
  437.  * Name         : SetSysMenu
  438.  *
  439.  * Description  : Procedure to remove grayed items from
  440.  *                all dialog window system menus.
  441.  *
  442.  * Concepts     : This routine obtains the handle for the system
  443.  *                menu and sends a message to delete the item
  444.  *                from the System Menu using the pre-defined
  445.  *                menuitem value. The item is deleted only if
  446.  *                it is disabled.
  447.  *
  448.  *                It is recommended that the system menu not
  449.  *                be altered by the programmer.
  450.  *
  451.  * API's        : WinWindowFromID, WinSendMsg.
  452.  *
  453.  * Parameters   : None
  454.  *
  455.  * Return       : None
  456.  *
  457.  **************************************************************/
  458. VOID SetSysMenu(HWND hwndDlg)
  459. {
  460.   HWND  hwndSysMenu;
  461.   MENUITEM menuItem;
  462.   SHORT sItemCount;
  463.   SHORT sMenu[MAXITEMS] = {SC_HIDE,
  464.                            SC_RESTORE,
  465.                            SC_SIZE,
  466.                            SC_MOVE,
  467.                            SC_MINIMIZE,
  468.                            SC_MAXIMIZE,
  469.                            SC_CLOSE,
  470.                            SC_TASKMANAGER};
  471.  
  472.  
  473.   /* Obtain the system menu handle  */
  474.   hwndSysMenu = WinWindowFromID(hwndDlg,
  475.                                 FID_SYSMENU);
  476.  
  477.   for (sItemCount = 0;sItemCount < MAXITEMS ;sItemCount ++ )
  478.   {
  479.      /* Obtain the MENUITEM structure for the menu item */
  480.      WinSendMsg(hwndSysMenu,
  481.                 MM_QUERYITEM,
  482.                 MPFROM2SHORT(sMenu[sItemCount],TRUE),
  483.                 MPFROMP(&menuItem));
  484.  
  485.      /* If the menu item is disabled - delete it. */
  486.      if (menuItem.afAttribute == MIA_DISABLED) 
  487.      {
  488.         WinSendMsg(hwndSysMenu,
  489.                    MM_DELETEITEM,
  490.                    MPFROM2SHORT(sMenu[sItemCount],TRUE),
  491.                    NULL);
  492.      }
  493.   }
  494. }
  495. /******************* End of SetSysMenu() procedure ********************/
  496.