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