home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pentlk11.zip / IMAGES / PENTLKT2.IMG / unc.dsk / HWX_SAMP.C < prev    next >
C/C++ Source or Header  |  1993-06-22  |  27KB  |  612 lines

  1. /*****************************************************************************
  2. *                                                                            *
  3. *  File Name   : HWX_SAMP.C                                                  *
  4. *                                                                            *
  5. *  Description : Main calling routine for the HWX_SAMP.EXE                   *
  6. *                                                                            *
  7. *  Function:  This file contains the code for the main thread for the        *
  8. *             HWX_SAMP.EXE.                                                  *
  9. *                                                                            *
  10. *  Copyright (C) 1993 IBM Corporation                                        *
  11. *                                                                            *
  12. *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is           *
  13. *      sample code created by IBM Corporation. This sample code is not       *
  14. *      part of any standard or IBM product and is provided to you solely     *
  15. *      for  the purpose of assisting you in the development of your          *
  16. *      applications.  The code is provided "AS IS", without                  *
  17. *      warranty of any kind.  IBM shall not be liable for any damages        *
  18. *      arising out of your use of the sample code, even if they have been    *
  19. *      advised of the possibility of such damages.                           *
  20. *                                                                            *
  21. *****************************************************************************/
  22.  
  23. /*****************************************************************************
  24. * Defines                                                                    *
  25. *****************************************************************************/
  26. #define  INCL_PM
  27. #define  INCL_WINLOAD
  28. #define  INCL_DOSMODULEMGR
  29.  
  30. /*****************************************************************************
  31. * System includes                                                            *
  32. *****************************************************************************/
  33. #include <os2.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36.  
  37. /*****************************************************************************
  38. * Application includes                                                       *
  39. *****************************************************************************/
  40. #include <penpm.h>
  41. #include "hwx_samp.h"
  42.  
  43. /*****************************************************************************
  44. * Function prototypes                                                        *
  45. *****************************************************************************/
  46. MRESULT EXPENTRY  wpMain                     ( HWND, ULONG, MPARAM, MPARAM );
  47. MRESULT EXPENTRY  wpStatus                   ( HWND, ULONG, MPARAM, MPARAM );
  48. MRESULT EXPENTRY  wpChild                    ( HWND, ULONG, MPARAM, MPARAM );
  49. ULONG             CreateTestWindow           ( HWND, ULONG );
  50. VOID              DebugMessage               ( char* );
  51.  
  52. /*****************************************************************************
  53. * Global Variables                                                           *
  54. *****************************************************************************/
  55. HAB            hab;
  56. HMQ            hmq;
  57. HMODULE        hmodule;
  58.  
  59. /*****************************************************************************
  60. * Main entry point for the application                                       *
  61. *****************************************************************************/
  62. int main ( VOID )
  63.    {
  64.    ULONG          rc;
  65.    HAB            hab;
  66.    HWND           hframe;
  67.    HWND           hWndStatus;
  68.    HMQ            hmq;
  69.    QMSG           qmsg;
  70.    HWND           hClient;
  71.    ULONG          ulStyles;
  72.    ULONG          ulCreateFlags;
  73.  
  74.    /**************************************************************************
  75.    * Initialize PM                                                           *
  76.    **************************************************************************/
  77.    hab = WinInitialize ( 0 );
  78.    if ( !hab )
  79.       {
  80.       printf ( "Error: WinInitialize failed!\n" );
  81.       return ( 0 );
  82.       };
  83.  
  84.    /**************************************************************************
  85.    * Create the message queue for this thread                                *
  86.    **************************************************************************/
  87.    hmq = WinCreateMsgQueue ( hab, 100 );
  88.    if ( !hmq )
  89.       {
  90.       printf         ( "Error: WinCreateMsgQueue failed!\n" );
  91.       WinTerminate   ( hab );
  92.       return         ( 0 );
  93.       };
  94.  
  95.    /**************************************************************************
  96.    * Register the private class for the main window                          *
  97.    **************************************************************************/
  98.    rc  = WinRegisterClass ( hab,
  99.                             MAINWINCLASS,
  100.                             wpMain,
  101.                             0l,
  102.                             0 );
  103.    if ( !rc )
  104.       {
  105.       printf             ( "Error: WinRegisterClass Failed!\n" );
  106.       WinDestroyMsgQueue ( hmq );
  107.       WinTerminate       ( hab );
  108.       return             ( rc );
  109.       };
  110.  
  111.    rc  = WinRegisterClass ( hab,
  112.                             "MYSTATUSBOX",
  113.                             wpStatus,
  114.                             CS_CLIPSIBLINGS,
  115.                             0 );
  116.    if ( !rc )
  117.       {
  118.       printf             ( "Error: WinRegisterClass Failed!\n" );
  119.       WinDestroyMsgQueue ( hmq );
  120.       WinTerminate       ( hab );
  121.       return             ( rc );
  122.       };
  123.  
  124.    rc  = WinRegisterClass ( hab,
  125.                             "MYCHILDBOX",
  126.                             wpChild,
  127.                             0L,
  128.                             0 );
  129.    if ( !rc )
  130.       {
  131.       printf             ( "Error: WinRegisterClass Failed!\n" );
  132.       WinDestroyMsgQueue ( hmq );
  133.       WinTerminate       ( hab );
  134.       return             ( rc );
  135.       };
  136.  
  137.    /**************************************************************************
  138.    * Create the main window.                                                 *
  139.    **************************************************************************/
  140.    ulStyles      = WS_VISIBLE          |
  141.                    WS_CLIPCHILDREN     ;
  142.  
  143.    ulCreateFlags = FCF_TITLEBAR        |
  144.                    FCF_SIZEBORDER      |
  145.                    FCF_MINMAX          |
  146.                    FCF_SYSMENU         |
  147.                    FCF_SHELLPOSITION   |
  148.                    FCF_TASKLIST        |
  149.                    FCF_MENU            |
  150.                    FCF_ICON            ;
  151.                                                  /***************************/
  152.    hframe = WinCreateStdWindow ( HWND_DESKTOP,   /* parent window handle    */
  153.                                  ulStyles,       /* frame style             */
  154.                                  &ulCreateFlags, /* frame create flags      */
  155.                                  MAINWINCLASS,   /* class for client window */
  156.                                  MAINWINNAME,    /* title bar text          */
  157.                                  ulStyles,       /* client style            */
  158.                                  NULLHANDLE,     /* resources identifier    */
  159.                                  ID_MAIN_RES,    /* frame window identifier */
  160.                                  &hClient );     /* client window handle    */
  161.                                                  /***************************/
  162.    if ( !hframe )
  163.       {
  164.       WinDestroyMsgQueue ( hmq );
  165.       WinTerminate       ( hab );
  166.       return             ( rc );
  167.       };
  168.  
  169.    /**************************************************************************
  170.    * Message Dispatching loop                                                *
  171.    **************************************************************************/
  172.    while ( WinGetMsg ( hab, &qmsg, 0, 0, 0 ) )
  173.       {
  174.       WinDispatchMsg ( hab, &qmsg );
  175.       };
  176.  
  177.    /**************************************************************************
  178.    * Cleanup. Destroy window. Destroy message queue. Terminate application   *
  179.    **************************************************************************/
  180.    WinDestroyWindow   ( hframe );
  181.    WinDestroyMsgQueue ( hmq );
  182.    WinTerminate       ( hab );
  183.  
  184.    /**************************************************************************
  185.    * All is well. Return  0 to command line handler                          *
  186.    **************************************************************************/
  187.    return ( 0 );
  188.    }
  189.  
  190. /*****************************************************************************
  191. * Window Procedure for Main Window                                           *
  192. *****************************************************************************/
  193. MRESULT EXPENTRY  wpStatus ( HWND     hWnd,
  194.                              ULONG    ulMsg,
  195.                              MPARAM   mp1,
  196.                              MPARAM   mp2   )
  197.    {
  198.    RECTL    rcl;
  199.    HPS      hps;
  200.    ULONG    rc;
  201.    HWND     hWndStatus;
  202.    POINTL   points[4];
  203.    LONG     backgroundcolor;
  204.  
  205.    switch ( ulMsg )
  206.       {
  207.       /***********************************************************************
  208.       * Initialization for main application window.                          *
  209.       ***********************************************************************/
  210.       case  WM_CREATE:
  211.             backgroundcolor = SYSCLR_MENU;
  212.             WinSetPresParam ( hWnd,
  213.                               PP_BACKGROUNDCOLORINDEX,
  214.                               sizeof(LONG),
  215.                               (PVOID) &backgroundcolor );
  216.             WinQueryWindowRect   ( hWnd, &rcl );
  217.  
  218.             /*****************************************************************
  219.             * Create the status line at the bottom of the window.            *
  220.             *****************************************************************/
  221.             hWndStatus  =  WinCreateWindow ( hWnd,
  222.                                              WC_STATIC,
  223.                                              "",
  224.                                              SS_TEXT         |
  225.                                              DT_VCENTER      |
  226.                                              DT_LEFT         |
  227.                                              WS_VISIBLE,
  228.                                              1,
  229.                                              1,
  230.                                              rcl.xRight - 2,
  231.                                              STATUS_BAR_HEIGHT,
  232.                                              hWnd,
  233.                                              HWND_TOP,
  234.                                              ID_STATUS_TEXT,
  235.                                              NULL,
  236.                                              NULL );
  237.             if ( !hWndStatus )
  238.                {
  239.                DebugMessage ( "Status Line creation failed" );
  240.                WinPostMsg ( hWnd, WM_CLOSE, 0, 0 );
  241.                return( (MRESULT) FALSE );
  242.                };
  243.  
  244.             WinSetPresParam ( hWndStatus,
  245.                               PP_BACKGROUNDCOLORINDEX,
  246.                               sizeof(LONG),
  247.                               (PVOID) &backgroundcolor );
  248.             WinShowWindow    ( hWndStatus, TRUE );
  249.             break;
  250.  
  251.       /***********************************************************************
  252.       * Paint the background in the same color as the menu.                  *
  253.       ***********************************************************************/
  254.       case  WM_PAINT:
  255.             hps = WinBeginPaint  ( hWnd, NULLHANDLE, (PRECTL)NULL );
  256.             WinQueryWindowRect   ( hWnd, &rcl );
  257.             WinFillRect          ( hps,  &rcl, SYSCLR_MENU );
  258.             points[0].x = 0;
  259.             points[0].y = STATUS_BAR_HEIGHT + 1;
  260.             points[1].x = rcl.xRight-1;
  261.             points[1].y = STATUS_BAR_HEIGHT + 1;
  262.             points[2].x = rcl.xRight-1;
  263.             points[2].y = 0;
  264.             points[3].x = 0;
  265.             points[3].y = 0;
  266.             GpiSetColor ( hps, CLR_WHITE );
  267.             GpiMove ( hps,
  268.                       &points[3] );
  269.             GpiPolyLine( hps,
  270.                          2,
  271.                          &points[0] );
  272.             GpiSetColor ( hps, CLR_BLACK );
  273.             GpiPolyLine( hps,
  274.                          2,
  275.                          &points[2] );
  276.             WinEndPaint          ( hps );
  277.             return ( (MRESULT) TRUE );
  278.             break;
  279.  
  280.       /***********************************************************************
  281.       * Resize the status line to fill the bottom of the window.             *
  282.       ***********************************************************************/
  283.       case  WM_SIZE:
  284.             WinQueryWindowRect   ( hWnd, &rcl );
  285.             WinSetWindowPos  ( WinWindowFromID ( hWnd, ID_STATUS_TEXT ),
  286.                                HWND_TOP,
  287.                                1,
  288.                                1,
  289.                                rcl.xRight-2,
  290.                                STATUS_BAR_HEIGHT,
  291.                                SWP_MOVE      |
  292.                                  SWP_SIZE    |
  293.                                  SWP_SHOW );
  294.             break;
  295.  
  296.       default:
  297.             break;
  298.       };
  299.    return( (MRESULT) WinDefWindowProc ( hWnd, ulMsg, mp1, mp2 )  );
  300.    };
  301.  
  302. /*****************************************************************************
  303. * Window Procedure for Main Window                                           *
  304. *****************************************************************************/
  305. MRESULT EXPENTRY  wpChild ( HWND     hWnd,
  306.                             ULONG    ulMsg,
  307.                             MPARAM   mp1,
  308.                             MPARAM   mp2   )
  309.    {
  310.    RECTL    rcl;
  311.    HPS      hps;
  312.  
  313.    switch ( ulMsg )
  314.       {
  315.       /***********************************************************************
  316.       * Paint the background in the same color as the menu.                  *
  317.       ***********************************************************************/
  318.       case  WM_PAINT:
  319.             hps = WinBeginPaint  ( hWnd, NULLHANDLE, (PRECTL)NULL );
  320.             WinQueryWindowRect   ( hWnd, &rcl );
  321.             WinFillRect          ( hps,  &rcl, SYSCLR_MENU );
  322.             WinEndPaint          ( hps );
  323.             return ( (MRESULT) TRUE );
  324.             break;
  325.  
  326.       default:
  327.             break;
  328.       };
  329.    return( (MRESULT) WinDefWindowProc ( hWnd, ulMsg, mp1, mp2 )  );
  330.    };
  331.  
  332. /*****************************************************************************
  333. * Window Procedure for Main Window                                           *
  334. *****************************************************************************/
  335. MRESULT EXPENTRY  wpMain (  HWND     hWnd,
  336.                             ULONG    ulMsg,
  337.                             MPARAM   mp1,
  338.                             MPARAM   mp2   )
  339.    {
  340.    RECTL    rcl;
  341.    HPS      hps;
  342.    ULONG    rc;
  343.    CHAR     pszObjNameBuf[100];
  344.    LONG     backgroundcolor;
  345.    HWND     hWndStatus;
  346.    HWND     hWndChild;
  347.  
  348.    switch ( ulMsg )
  349.       {
  350.       /***********************************************************************
  351.       * Initialization for main application window.                          *
  352.       ***********************************************************************/
  353.       case  WM_CREATE:
  354.             if( WrtWaitActive( WRT_IMMEDIATE_RETURN ) )
  355.                {
  356.                DebugMessage ( "Pen for OS/2 is not active" );
  357.                WinSendMsg ( hWnd, WM_CLOSE, 0, 0 );
  358.                };
  359.  
  360.             backgroundcolor = SYSCLR_MENU;
  361.             WinSetPresParam ( hWnd,
  362.                               PP_BACKGROUNDCOLORINDEX,
  363.                               sizeof(LONG),
  364.                               (PVOID) &backgroundcolor );
  365.  
  366.             WinShowWindow    ( hWnd, TRUE );
  367.             WinQueryWindowRect   ( hWnd, &rcl );
  368.  
  369.             /*****************************************************************
  370.             * Load the hwxprocs.dll. This dll contains all of the window     *
  371.             * procedures for the child windows.                              *
  372.             *****************************************************************/
  373.             rc = DosLoadModule ( pszObjNameBuf,
  374.                                  sizeof(pszObjNameBuf),
  375.                                  "hwxprocs",
  376.                                  &hmodule );
  377.             if ( rc )
  378.                {
  379.                DebugMessage ( "could not load module" );
  380.                WinPostMsg ( hWnd, WM_CLOSE, 0, 0 );
  381.                return ( (MRESULT) FALSE );
  382.                };
  383.  
  384.             /*****************************************************************
  385.             * Create the status line at the bottom of the window.            *
  386.             *****************************************************************/
  387.             hWndStatus  =  WinCreateWindow ( hWnd,
  388.                                              "MYSTATUSBOX",
  389.                                              "",
  390.                                              0L,
  391.                                              0,
  392.                                              0,
  393.                                              rcl.xRight,
  394.                                              STATUS_BAR_HEIGHT+2,
  395.                                              hWnd,
  396.                                              HWND_TOP,
  397.                                              ID_STATUS_BOX,
  398.                                              NULL,
  399.                                              NULL );
  400.             if ( !hWndStatus )
  401.                {
  402.                DebugMessage ( "Status window creation failed" );
  403.                WinPostMsg ( hWnd, WM_CLOSE, 0, 0 );
  404.                return ( (MRESULT) FALSE );
  405.                };
  406.  
  407.             /*****************************************************************
  408.             * Create the status line at the bottom of the window.            *
  409.             *****************************************************************/
  410.             hWndChild   =  WinCreateWindow ( hWnd,
  411.                                              "MYCHILDBOX",
  412.                                              "",
  413.                                              0L,
  414.                                              0,
  415.                                              STATUS_BAR_HEIGHT+2,
  416.                                              rcl.xRight,
  417.                                              rcl.yTop-STATUS_BAR_HEIGHT-2,
  418.                                              hWnd,
  419.                                              HWND_TOP,
  420.                                              ID_CHILD_BOX,
  421.                                              NULL,
  422.                                              NULL );
  423.             if ( !hWndChild )
  424.                {
  425.                DebugMessage ( "Child window creation failed" );
  426.                WinPostMsg ( hWnd, WM_CLOSE, 0, 0 );
  427.                return ( (MRESULT) FALSE );
  428.                };
  429.  
  430.             break;
  431.  
  432.       /***********************************************************************
  433.       * Paint the background in the same color as the menu.                  *
  434.       ***********************************************************************/
  435.       case  WM_PAINT:
  436.             hps = WinBeginPaint  ( hWnd, NULLHANDLE, (PRECTL)NULL );
  437.             WinQueryWindowRect   ( hWnd, &rcl );
  438.             WinFillRect          ( hps,  &rcl, SYSCLR_MENU );
  439.             WinEndPaint          ( hps );
  440.             return ( (MRESULT) TRUE );
  441.             break;
  442.  
  443.       /***********************************************************************
  444.       * Messages from the menu                                               *
  445.       ***********************************************************************/
  446.       case  WM_COMMAND:
  447.             if ( LONGFROMMP(mp1) == ID_QUIT )
  448.                WinPostMsg ( hWnd, WM_CLOSE, 0, 0 );
  449.             else
  450.                CreateTestWindow ( hWnd, LONGFROMMP(mp1) );
  451.             break;
  452.  
  453.       /***********************************************************************
  454.       * Resize the status line to fill the bottom of the window.             *
  455.       ***********************************************************************/
  456.       case  WM_SIZE:
  457.             WinQueryWindowRect   ( hWnd, &rcl );
  458.             WinSetWindowPos  ( WinWindowFromID ( hWnd, ID_STATUS_BOX ),
  459.                                HWND_TOP,
  460.                                0,
  461.                                0,
  462.                                rcl.xRight,
  463.                                STATUS_BAR_HEIGHT+2,
  464.                                SWP_MOVE      |
  465.                                  SWP_SIZE    |
  466.                                  SWP_SHOW );
  467.             WinSetWindowPos  ( WinWindowFromID ( hWnd, ID_CHILD_BOX ),
  468.                                HWND_TOP,
  469.                                0,
  470.                                STATUS_BAR_HEIGHT+2,
  471.                                rcl.xRight,
  472.                                rcl.yTop-STATUS_BAR_HEIGHT-2,
  473.                                SWP_MOVE      |
  474.                                  SWP_SIZE    |
  475.                                  SWP_SHOW );
  476.             break;
  477.  
  478.       default:
  479.             break;
  480.       };
  481.    return( (MRESULT) WinDefWindowProc ( hWnd, ulMsg, mp1, mp2 )  );
  482.    };
  483.  
  484. /*****************************************************************************
  485. * Debug messages                                                             *
  486. *****************************************************************************/
  487. VOID DebugMessage ( char dmessage[50] )
  488.    {
  489.    WinMessageBox ( HWND_DESKTOP,
  490.                    HWND_DESKTOP,
  491.                    dmessage,
  492.                    "Debug Message",
  493.                    0,
  494.                    MB_OK            |
  495.                    MB_ERROR         |
  496.                    MB_SYSTEMMODAL   |
  497.                    MB_MOVEABLE );
  498.    }
  499.  
  500. /*****************************************************************************
  501. * Create the test window                                                     *
  502. *****************************************************************************/
  503. ULONG CreateTestWindow ( HWND hWnd, ULONG ID )
  504.    {
  505.    ULONG          rc;
  506.    PFNWP          pWndProc;
  507.    HWND           hframe;
  508.    HWND           hClient;
  509.    HWND           hStatus;
  510.    ULONG          ulStyles;
  511.    ULONG          ulCreateFlags;
  512.    CHAR           myname[32];
  513.    PFN            pProcAddr;
  514.    CHAR           pszObjNameBuf[100];
  515.    RECTL          rcl;
  516.  
  517.  
  518.    /**************************************************************************
  519.    * Load the window procedure same from the string table. The ID of the     *
  520.    * string is the same as the ID as the menu item selected.                 *
  521.    **************************************************************************/
  522.    rc = WinLoadString ( hab, NULLHANDLE, ID, sizeof(myname), myname );
  523.    if ( !rc )
  524.       {
  525.       DebugMessage ( "Could not load string" );
  526.       return ( -1 );
  527.       };
  528.  
  529.    /**************************************************************************
  530.    * Get a pointer to the window procedure for the selected menu item        *
  531.    **************************************************************************/
  532.    rc = DosQueryProcAddr ( hmodule,
  533.                            0L,
  534.                            myname,
  535.                            &pProcAddr);
  536.    if ( rc )
  537.       {
  538.       DebugMessage ( "Could not load procedure" );
  539.       return ( -1 );
  540.       };
  541.  
  542.    /**************************************************************************
  543.    * Register the window class for the application window running in this    *
  544.    * thread                                                                  *
  545.    **************************************************************************/
  546.    rc = WinRegisterClass ( hab,
  547.                            myname,
  548.                            (PFNWP) pProcAddr,
  549.                            0L,
  550.                            sizeof(HWND) );
  551.    if ( !rc )
  552.       {
  553.       DebugMessage ( "Error: WinRegisterProcedure for TestWindow! ");
  554.       return ( -1 );
  555.       };
  556.  
  557.    /**************************************************************************
  558.    * Create the application window for the handwriting control               *
  559.    **************************************************************************/
  560.    ulStyles      = WS_VISIBLE;
  561.    ulCreateFlags = FCF_TITLEBAR        |
  562.                    FCF_SIZEBORDER      |
  563.                    FCF_MINMAX          |
  564.                    FCF_SYSMENU         |
  565.                    FCF_MENU            |
  566.                    FCF_ICON            ;
  567.  
  568.    hframe  = WinCreateStdWindow ( WinWindowFromID ( hWnd, ID_CHILD_BOX ),
  569.                                   ulStyles,
  570.                                   &ulCreateFlags,
  571.                                   myname,
  572.                                   myname,
  573.                                   ulStyles,
  574.                                   NULLHANDLE,
  575.                                   ID_TEST_RES,
  576.                                   &hClient );
  577.  
  578.    if ( !hframe )
  579.       {
  580.       DebugMessage ( "Error: WinCreateWindow for TestWindow! ");
  581.       return             ( -1 );
  582.       };
  583.  
  584.    /*************************************************************************
  585.    * Set the window handle for the status window in the window words for    *
  586.    * child window to update the status text.                                *
  587.    *************************************************************************/
  588.    hStatus = WinWindowFromID ( WinWindowFromID ( hWnd,
  589.                                                  ID_STATUS_BOX ),
  590.                                ID_STATUS_TEXT );
  591.  
  592.    WinPostMsg ( hClient, WM_INITMYWINDOW, (MPARAM) hStatus, 0 );
  593.  
  594.    /*************************************************************************
  595.    * Set the window position for the new window                             *
  596.    *************************************************************************/
  597.    WinQueryWindowRect ( WinWindowFromID ( hWnd, ID_CHILD_BOX ), &rcl );
  598.    WinSetWindowPos  ( hframe,
  599.                       HWND_TOP,
  600.                       10,
  601.                       rcl.yTop-210,
  602.                       250,
  603.                       200,
  604.                       SWP_ACTIVATE | SWP_MOVE | SWP_SIZE | SWP_SHOW );
  605.  
  606.    /**************************************************************************
  607.    * All is well. Return  0 to command line handler                          *
  608.    **************************************************************************/
  609.    return ( 0 );
  610.    }
  611.  
  612.