home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / GENERI.ZIP / GENERIC.C next >
C/C++ Source or Header  |  1992-12-26  |  18KB  |  432 lines

  1. /****************************************************
  2. **                                                 **
  3. ** Program: Generic.c                              **
  4. ** Author : Gene Backlin                           **
  5. **                                                 **
  6. ** Address: CompuServe ID 70401,1574               **
  7. **                                                 **
  8. ** Purpose: To give the User as well as the        **
  9. **          Programmer new to OS/2's Presentation  **
  10. **          Manager interface, a generic shell, to **
  11. **          see how the pieces are fit together.   **
  12. **                                                 **
  13. ** Written: 12/25/92                               **
  14. ** Revised: 12/25/92                               **
  15. **    Version 1.00 - 12/25/92                      **
  16. **       Original Version                          **
  17. **                                                 **
  18. ****************************************************/
  19. #define INCL_32
  20.  
  21. #define INCL_WIN
  22. #define INCL_GPI
  23.  
  24. #define INCL_WINHELP                            // Include IPF Header File
  25.  
  26. #include <os2.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29.  
  30. #include "generic.h"
  31.  
  32. /****************************************************
  33. *                                                   *
  34. *  Function Prototypes                              *
  35. *                                                   *
  36. ****************************************************/
  37. MRESULT EXPENTRY ClientWndProc   (HWND, ULONG, MPARAM, MPARAM);
  38. MRESULT EXPENTRY AboutDlgProc    (HWND, ULONG, MPARAM, MPARAM);
  39. MRESULT EXPENTRY GenericDlgProc  (HWND, ULONG, MPARAM, MPARAM);
  40. MRESULT EXPENTRY CenterDlg       (HWND hwnd);
  41.  
  42. /****************************************************
  43. *                                                   *
  44. *  Required IPF Structure Declarations              *
  45. *                                                   *
  46. ****************************************************/
  47. HELPINIT    hmiHelpData;                        // Help Initialization Structure
  48. HWND        hwndHelpInstance;                   // Handle to the Help Window
  49.  
  50. /****************************************************
  51. *                                                   *
  52. *  Main Function                                    *
  53. *                                                   *
  54. ****************************************************/
  55. INT main(INT argc, CHAR *argv, CHAR *envp)
  56. {
  57.    HAB     hab;                                 // Handle to Application  
  58.    HMQ     hmq;                                 // Hold the Application's Message Queue
  59.    QMSG    qmsg;                                // The actual Queue Message             
  60.  
  61.    HWND    hwndFrame;                           // Presentation Manager 
  62.    HWND    hwndClient;                          //    Window Handles
  63.    HWND    hwndMenu;                            // Menu Handle
  64.    HWND    hwndTemp;                            // Temporary Menu Handle
  65.  
  66.    SWCNTRL SwData;                              // Switch control data block
  67.    HSWITCH hSwitch;                             // Switch entry handle
  68.  
  69.    ULONG flFrameFlags =                         // Set the Frame-window creation Flags
  70.       FCF_MENU          |                       // Application Menu
  71.       FCF_TITLEBAR      |                       // Application Title
  72.       FCF_SIZEBORDER    |                       // Application Size Border
  73.       FCF_MINMAX        |                       // Minimize and Maximum Buttons
  74.       FCF_SYSMENU       |                       // Application System Menu
  75.       FCF_SHELLPOSITION    |                       // System default size and position
  76.       FCF_ICON          |                       // System default size and position
  77.       FCF_TASKLIST;                                // Add name to TaskList
  78.  
  79.    hab = WinInitialize(0);                      // Register the Application
  80.    hmq = WinCreateMsgQueue(hab, 0);             // Create the Message Queue
  81.  
  82.    if (!WinRegisterClass(                       // Register the window class
  83.       hab,                                      // Handle to application
  84.       CLIENT_CLASS,                               // Name of the Class
  85.       ClientWndProc,                               // Window procedure name
  86.       CS_SIZEREDRAW,                               // Window style
  87.       0))                                       // Extra window words
  88.          return (FALSE);                        // Terminate if Unsuccessful
  89.  
  90. /****************************************************
  91. *                                                   *
  92. *  IPF Initialization Structure                     *
  93. *                                                   *
  94. ****************************************************/
  95.    hmiHelpData.cb                      = sizeof(HELPINIT);    // size of initialization structure
  96.    hmiHelpData.ulReturnCode            = 0;            // store HM return code from init
  97.    hmiHelpData.pszTutorialName         = 0;            // no tutorial program
  98.    hmiHelpData.phtHelpTable            = (PVOID)
  99.                   (0xffff0000 | MAIN_HELPTABLE);   // help table defined in RC file
  100.    hmiHelpData.hmodAccelActionBarModule= 0;
  101.    hmiHelpData.idAccelTable            = 0;
  102.    hmiHelpData.idActionBar             = 0;
  103.    hmiHelpData.pszHelpWindowTitle      = TITLE;
  104.    hmiHelpData.hmodHelpTableModule     = 0;             // Help Table not in a DLL
  105.    hmiHelpData.fShowPanelId            = 0;            // Help Panels ID is not displayed
  106.    hmiHelpData.pszHelpLibraryName      = HELPFILE;    // Library with help panels
  107.  
  108. /****************************************************
  109. *                                                   *
  110. *  Create Instance of IPF                           *
  111. *                                                   *
  112. ****************************************************/
  113.    hwndHelpInstance =                            // Pass Anchor Block handle and address 
  114.       WinCreateHelpInstance(hab, &hmiHelpData);  // of IPF initialization structure
  115.  
  116.    if(!hwndHelpInstance)
  117.       {
  118.       WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  119.                    (PSZ) "Help Not Available",
  120.                    (PSZ) "Help Creation Error",
  121.                    1,
  122.                    MB_OK | MB_APPLMODAL | MB_MOVEABLE);
  123.       }
  124.    else
  125.       {
  126.       if(hmiHelpData.ulReturnCode)
  127.          {
  128.          WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  129.                       (PSZ) "Help NotAvailable",
  130.                       (PSZ) "Help Creation Error",
  131.                       1,
  132.                       MB_OK | MB_APPLMODAL | MB_MOVEABLE);
  133.          WinDestroyHelpInstance(hwndHelpInstance);
  134.          }
  135.       }
  136.  
  137. /****************************************************
  138. *                                                   *
  139. *  Create a Top-Level frame window with a client    *
  140. *  window that belongs to the window class          *
  141. *  CLIENT_CLASS. (see generic.h)                    *
  142. *                                                   *
  143. ****************************************************/
  144.    hwndFrame = WinCreateStdWindow(              // Create the Frame Window
  145.                       HWND_DESKTOP,                // Parent is the Desktop
  146.                       0L,                       // Don't make frame window visible
  147.                       &flFrameFlags,            // Frame Controls
  148.                       CLIENT_CLASS,                // Window class for client
  149.                       TITLE,                    // Window Title
  150.                       0L,                       // Don't make client window visible
  151.                       0,                        // Resources in application model
  152.                       ID_MENU_RESOURCE,         // Resource identifier
  153.                       &hwndClient);                // Pointer to client window handle
  154.    if (!hwndFrame)
  155.       return (FALSE);
  156.  
  157.    WinSetWindowPos(
  158.       hwndFrame,                                // Shows and activates frame
  159.       HWND_TOP,                                 // Put the window on top
  160.       55,                                       // Positon x
  161.       350,                                      // Positon y
  162.       525,                                      // New width
  163.       75,                                       // New height
  164.       SWP_SIZE      |                           // Change the size
  165.       SWP_MOVE      |                           // Move the window
  166.       SWP_ACTIVATE  |                           // Make it the active window
  167.       SWP_SHOW                                  // Make it visible
  168.       );
  169.  
  170.    hwndMenu =
  171.       WinWindowFromID( hwndFrame, FID_MENU);    // Get the Menu handle
  172.  
  173.    SwData.hwnd          = hwndFrame;            // Set frame Window handle
  174.    SwData.hwndIcon      = 0;                    // Use the default Icon
  175.    SwData.hprog;                                // Use default program handle
  176.    SwData.idProcess     = 0;                    // Use current process id
  177.    SwData.idSession     = 0;                    // Use current session id
  178.    SwData.uchVisibility    = SWL_VISIBLE;          // Make Visible
  179.    SwData.fbJump        = SWL_JUMPABLE;         // Make Jumpable via Alt+Esc
  180.    SwData.szSwtitle[0]    = '\0';                 // Use default Title Test
  181.  
  182.    hSwitch = WinAddSwitchEntry(&SwData);        // Add task manager entry
  183.  
  184. /****************************************************
  185. *                                                   *
  186. *  Associate Instance of IPF                        *
  187. *                                                   *
  188. ****************************************************/
  189.    if(hwndHelpInstance)
  190.       WinAssociateHelpInstance(hwndHelpInstance, hwndFrame);
  191.  
  192. /****************************************************
  193. *                                                   *
  194. *  Start the main message loop. Get Messages from   *
  195. *  the queue and dispatch them to the appropriate   *
  196. *  windows.                                         *
  197. *                                                   *
  198. ****************************************************/
  199.    while(WinGetMsg(hab, &qmsg, 0, 0, 0))        // Loop until WM_QUIT
  200.       WinDispatchMsg(hab, &qmsg);
  201.  
  202. /****************************************************
  203. *                                                   *
  204. *  Main Loop has terminated. Destroy all windows and*
  205. *  the message queue; then terminate the application*
  206. *                                                   *
  207. ****************************************************/
  208.    if(hwndHelpInstance)
  209.       WinDestroyHelpInstance(hwndHelpInstance);    // Destroy Help Instance
  210.  
  211.    WinDestroyWindow(hwndFrame);                 // Destroy main window
  212.    WinDestroyMsgQueue(hmq);                     // Destroy message queue
  213.    WinTerminate(hab);                           // Deregister application
  214.  
  215.    return 0;
  216. }
  217.  
  218. /****************************************************
  219. *                                                   *
  220. *  Client Window Procedure                          *
  221. *                                                   *
  222. ****************************************************/
  223. MRESULT EXPENTRY ClientWndProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  224. {
  225.    RECTL   rcl;                                 // Window rectangle
  226.    HPS     hps;                                 // Handle to the Presentation space
  227.    UCHAR   szMsg[40];                           // Standared Text Message
  228.    UCHAR   szTitle[20];                         // Title...
  229.    ULONG   rc;                                  // Status return code
  230.  
  231.   switch(msg)
  232.    {
  233.    case WM_CREATE:
  234.       WinSendMsg(                               // is just the
  235.          hwnd,                                  // About Box
  236.          WM_COMMAND,                            // Credit
  237.          (MPARAM) IDM_HEL_ABOUT,                   // Display.
  238.          0L);
  239.       break;
  240.  
  241.     case WM_ERASEBACKGROUND:
  242.       return (MRESULT)(TRUE);
  243.  
  244.     case WM_CHAR:
  245.       return (MRESULT)(TRUE);
  246.  
  247.     case WM_PAINT:
  248.       hps = WinBeginPaint(hwnd, 0, &rcl);
  249.       WinEndPaint(hps);
  250.       break;
  251.  
  252.     case WM_COMMAND:
  253.       switch (SHORT1FROMMP(mp1))
  254.         {
  255.         case IDM_DIS_GENERIC:
  256.           rc = WinDlgBox(
  257.                   HWND_DESKTOP,                 // Desktop is parent
  258.                   hwnd,                         // Current window is owner
  259.                   GenericDlgProc,               // Entry point of dialog proc.
  260.                   0,                            // Resource is in EXE
  261.                   IDD_DIS_GENERIC,              // Dialog resource identifier
  262.                   (PVOID)NULL);                 // Pointer to initialization dat
  263.           return 0;
  264.  
  265.         case IDM_HEL_ABOUT:                     // Program Credits
  266.           rc = WinDlgBox(
  267.                   HWND_DESKTOP,                 // Desktop is parent
  268.                   hwnd,                         // Current window is owner
  269.                   AboutDlgProc,                 // Entry point of dialog proc.
  270.                   0,                            // Resource is in EXE
  271.                   IDD_ABOUTBOX,                 // Dialog resource identifier
  272.                   (PVOID)NULL);                 // Pointer to initialization dat
  273.           return 0;
  274.  
  275.         case IDM_EXIT:
  276.           WinSendMsg(hwnd, WM_CLOSE, 0, 0);     // Exit the Program
  277.           return 0;
  278.  
  279.         case IDM_HELP_FOR_HELP:
  280.           if(hwndHelpInstance)
  281.             WinSendMsg(hwndHelpInstance, HM_DISPLAY_HELP, 0L, 0L);
  282.           break;
  283.  
  284.         default:
  285.           return WinDefWindowProc(hwnd, msg, mp1, mp2);
  286.         }
  287.       break;
  288.  
  289.  
  290.     case HM_ERROR:
  291.       if((hwndHelpInstance && (ULONG) mp1) == HMERR_NO_MEMORY)
  292.         {
  293.         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  294.                      (PSZ) "Help Terminated Due to Error",
  295.                      (PSZ) "Help Error",
  296.                      1,
  297.                      MB_OK | MB_APPLMODAL | MB_MOVEABLE);
  298.           WinDestroyHelpInstance(hwndHelpInstance);
  299.         }
  300.       else
  301.         {
  302.           WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  303.                      (PSZ) "Help Error Occurred",
  304.                      (PSZ) "Help Error",
  305.                      1,
  306.                      MB_OK | MB_APPLMODAL | MB_MOVEABLE);
  307.         }
  308.       break;
  309.  
  310.     case WM_CLOSE:
  311.       /*
  312.        * This is the place to put your termination routines
  313.        */
  314.  
  315.       WinPostMsg( hwnd, WM_QUIT, 0L, 0L );    // Cause termination
  316.       break;
  317.  
  318.     default:
  319.       return WinDefWindowProc(hwnd, msg, mp1, mp2); 
  320.     }
  321.   return (MRESULT)FALSE;
  322. }
  323.  
  324. /****************************************************
  325. *                                                   *
  326. *  About Window Procedure                           *
  327. *                                                   *
  328. ****************************************************/
  329. MRESULT EXPENTRY AboutDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  330. {
  331.   switch(msg)
  332.     {
  333.     case WM_INITDLG:
  334.       CenterDlg(hwnd);
  335.       return 0;
  336.  
  337.     case WM_COMMAND:
  338.       switch( SHORT1FROMMP( mp1 ) )             // Extract the command value
  339.         {
  340.         case DID_OK:
  341.           WinDismissDlg( hwnd, DID_OK );
  342.         break;
  343.  
  344.         case DID_CANCEL:
  345.           WinDismissDlg( hwnd, DID_CANCEL );
  346.         break;
  347.         }
  348.         /* no matter what the command, close the dialog */
  349.       WinDismissDlg(hwnd, TRUE);
  350.       break;
  351.  
  352.     default:
  353.       return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  354.       break;
  355.     }
  356.   return(MPVOID);
  357. }   /* AboutDlgProc() */
  358.  
  359.  
  360.  
  361. /****************************************************
  362. *                                                   *
  363. *  Generic Window Procedure                         *
  364. *                                                   *
  365. ****************************************************/
  366. MRESULT EXPENTRY GenericDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  367. {
  368.   switch(msg)
  369.     {
  370.     case WM_INITDLG:
  371.       CenterDlg(hwnd);
  372.       return 0;
  373.  
  374.     case WM_COMMAND:
  375.       switch( SHORT1FROMMP( mp1 ) )             // Extract the command value
  376.         {
  377.         case DID_OK:
  378.            WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  379.                      (PSZ) "Ok Button was Pressed",
  380.                      (PSZ) "Generic Dlg Proc",
  381.                      1,
  382.                      MB_OK | MB_APPLMODAL | MB_MOVEABLE);
  383.           WinDismissDlg( hwnd, DID_OK );
  384.         break;
  385.  
  386.         case DID_CANCEL:
  387.            WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  388.                      (PSZ) "Cancel Button was Pressed",
  389.                      (PSZ) "Generic Dlg Proc",
  390.                      1,
  391.                      MB_OK | MB_APPLMODAL | MB_MOVEABLE);
  392.           WinDismissDlg( hwnd, DID_CANCEL );
  393.         break;
  394.         }
  395.  
  396.     default:
  397.       return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  398.     }
  399.   return(MPVOID);
  400. }   /* GenericDlgProc() */
  401.  
  402. /****************************************************
  403. *                                                   *
  404. *  Center Dialog Box Procedure                      *
  405. *                                                   *
  406. ****************************************************/
  407. MRESULT EXPENTRY CenterDlg(HWND hwnd)
  408. {
  409.    RECTL rclScreen;
  410.    RECTL rclDialog;
  411.    LONG  sWidth, sHeight, sBLCx, sBLCy;
  412.    
  413.    WinQueryWindowRect(HWND_DESKTOP, &rclScreen);
  414.    WinQueryWindowRect(hwnd, &rclDialog);
  415.    
  416.    sWidth  = (LONG) (rclDialog.xRight - rclDialog.xLeft);
  417.    sHeight = (LONG) (rclDialog.yTop   - rclDialog.yBottom);
  418.    sBLCx   = ((LONG) rclScreen.xRight - sWidth)  / 2;
  419.    sBLCy   = ((LONG) rclScreen.yTop   - sHeight) / 2;
  420.    
  421.    WinSetWindowPos(
  422.       hwnd,                                    // Activates frame
  423.       HWND_TOP,                                // Put the window on top
  424.       sBLCx,                                   // Positon x
  425.       sBLCy,                                   // Positon y
  426.       0,                                       // New width
  427.       0,                                       // New height
  428.       SWP_MOVE);                               // Move the window
  429.                                                
  430.    return 0;                                   
  431. }                                              
  432.