home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmm100.zip / PMMSAM.C < prev    next >
Text File  |  1999-03-30  |  12KB  |  340 lines

  1.  
  2. /* PMM Sample Source "C" File ***********************************************
  3.  *
  4.  * PROGRAM NAME: PMMSAM
  5.  * -------------
  6.  *  Presentation Manager Standard Window "C" Sample Program
  7.  *
  8.  * WHAT THIS PROGRAM DOES:
  9.  * -----------------------
  10.  *  This program displays a standard window containing the text "Hello".
  11.  *  The action bar contains a single choice, Options.
  12.  *  The Options pull-down contains three choices that each
  13.  *  paint a different string in the window.
  14.  *  A PMM trace session is created.  All window messages generated
  15.  *  in this program are traced and written to a file
  16.  *
  17.  * WHAT THIS PROGRAM DEMONSTRATES:
  18.  * -------------------------------
  19.  *  This program demonstrates how to create and display a standard window,
  20.  *  how to use resources defined in a resource script file,
  21.  *  and how to create and use a PMM trace session
  22.  *
  23.  * WHAT YOU NEED TO COMPILE THIS PROGRAM:
  24.  * --------------------------------------
  25.  *
  26.  *  REQUIRED FILES:
  27.  *  ---------------
  28.  *
  29.  *    PMMSAM.C       - Source code
  30.  *    PMMSAM.H       - Application header file
  31.  *    PMMSAM.MAK     - Make file for this program
  32.  *    PMMSAM.LNK     - Linker automatic response file
  33.  *    PMMSAM.DEF     - Module definition file
  34.  *    PMMSAM.RC      - Resource file
  35.  *    PMMSAM.ICO     - Icon file
  36.  *
  37.  *    OS2.H          - Presentation Manager include file
  38.  *    STRING.H       - String handling function declarations
  39.  *
  40.  *    PMMPOS2.H      - PMM header file
  41.  *    PMM.H          - PMM header file
  42.  *
  43.  * WHAT FILES NEED TO BE MODIFIED TO USE PMM IN THIS PROGRAM:
  44.  * ----------------------------------------------------------
  45.  *
  46.  *    PMMSAM.C
  47.  *    PMMSAM.MAK
  48.  *    PMMSAM.DEF
  49.  *
  50.  *  REQUIRED LIBRARIES:
  51.  *  -------------------
  52.  *
  53.  *    OS2386.LIB     - Presentation Manager OS/2 library
  54.  *    DDE4MBS.LIB    - Presentation Manager OS/2 library
  55.  *    PMM.LIB        - Presentation Manager Monitor library
  56.  *
  57.  *  REQUIRED PROGRAMS:
  58.  *  ------------------
  59.  *
  60.  *    A "C" Compiler
  61.  *    A Linker
  62.  *    A Resource Compiler
  63.  *
  64.  ***************************************************************************/
  65.  
  66. /* Conditional compilation. Activate/deactivate PMM easily */
  67. #define USE_PMM         1
  68.  
  69. #define INCL_WIN
  70. #define INCL_GPI
  71. #define INCL_DOSPROCESS
  72.  
  73. /* Include this file immediately before including "os2.h" */
  74. #if USE_PMM
  75.    #include <pmmpos2.h>
  76. #endif
  77. #include <os2.h>                        /* PM header file                */
  78. #include "pmmsam.h"                     /* Resource symbolic identifiers */
  79. /* Include the "main" PMM file */
  80. #if USE_PMM
  81.    #include <pmm.h>
  82. #endif
  83. /* Include header files as needed */
  84. #include <string.h>
  85.  
  86. #define BEEP_WARN_FREQ          60      /* Frequency of warning beep    */
  87. #define BEEP_WARN_DUR           100     /* Duration of warning beep     */
  88.  
  89. /* Function prototypes */
  90. INT main( VOID );
  91. MRESULT EXPENTRY PMMSampleWindowProc( HWND hwnd, ULONG msg,
  92.                                       MPARAM mp1, MPARAM mp2 );
  93.  
  94. /**************************************************************************
  95.  *
  96.  *  Name       : main()
  97.  *
  98.  *  Description: Initializes the process for OS/2 PM services, creates
  99.  *               a PMM trace session and process the application message
  100.  *               queue until a WM_QUIT message is received.  It then
  101.  *               destroys all created resources and terminates.
  102.  *
  103.  *  Concepts   : - obtains anchor block handle and creates message
  104.  *                 queue
  105.  *               - creates a trace session and starts tracing
  106.  *               - creates the main frame window which creates the
  107.  *                 main client window
  108.  *               - polls the message queue via Get/Dispatch Msg loop
  109.  *               - upon exiting the loop destroys the trace session,
  110.  *                 frees all allocated resources and exits
  111.  *
  112.  *  API's      :   DosBeep
  113.  *                 WinInitialize
  114.  *                 WinCreateMsgQueue
  115.  *                 PMMCreateTraceSession
  116.  *                 PMMStartTracing
  117.  *                 WinRegisterClass
  118.  *                 WinCreateStdWindow
  119.  *                 WinGetMsg
  120.  *                 WinDispatchMsg
  121.  *                 WinDestroyWindow
  122.  *                 PMMStopTracing
  123.  *                 PMMDestroyTraceSession
  124.  *                 WinDestroyMsgQueue
  125.  *                 WinTerminate
  126.  *
  127.  *  Parameters :  [none]
  128.  *
  129.  *  Return     :  zero (0) - if successful execution completed
  130.  *                non-zero - if error
  131.  *
  132.  *************************************************************************/
  133. INT main( VOID )
  134. {
  135.    HAB  hab;                            /* PM anchor block handle        */
  136.    HMQ  hmq;                            /* Message queue handle          */
  137.    QMSG qmsg;                           /* Message from message queue    */
  138.    HWND hwndFrame;                      /* Frame window handle           */
  139.    HWND hwndClient;                     /* Client area window handle     */
  140.    ULONG flCreate;                      /* Window creation control flags */
  141. #if USE_PMM
  142.    HPMMSESSION hpmms;
  143.    PMMRET pmmr;
  144. #endif
  145.  
  146.    hab = WinInitialize( 0 );
  147.    if ( !hab )
  148.    {
  149.       DosBeep( BEEP_WARN_FREQ, BEEP_WARN_DUR );
  150.       return 1; /* Error */
  151.    }
  152.  
  153.    hmq = WinCreateMsgQueue( hab, 0 );
  154.    if ( !hmq )
  155.    {
  156.       WinTerminate( hab );
  157.       DosBeep( BEEP_WARN_FREQ, BEEP_WARN_DUR );
  158.       return 2; /* Error */
  159.    }
  160.  
  161.    /* Return codes from PMM are not tested in this example,
  162.    they should be tested */
  163.  
  164.    /* Create a trace session and start tracing */
  165. #if USE_PMM
  166.    pmmr = PMMCreateTraceSession( &hpmms, hab, "pmmsam.log" );
  167.    pmmr = PMMStartTracing( hpmms );
  168. #endif
  169.  
  170.    if ( !WinRegisterClass(              /* Register window class        */
  171.         hab,                            /* Anchor block handle          */
  172.         (PSZ) "PMMSampleClass",         /* Window class name            */
  173.         (PFNWP) PMMSampleWindowProc,    /* Address of window procedure  */
  174.         CS_SIZEREDRAW,                  /* Class style                  */
  175.         0 ) )                           /* No extra window words        */
  176.    {
  177.       WinDestroyMsgQueue( hmq );
  178.       WinTerminate( hab );
  179.       DosBeep( BEEP_WARN_FREQ, BEEP_WARN_DUR );
  180.       return 3; /* Error */
  181.    }
  182.  
  183.    flCreate = FCF_STANDARD;
  184.  
  185.    if ( ( hwndFrame = WinCreateStdWindow(
  186.       HWND_DESKTOP,                     /* Desktop window is parent     */
  187.       WS_VISIBLE,                       /* STD. window styles           */
  188.       &flCreate,                        /* Frame control flags          */
  189.       "PMMSampleClass",                 /* Client window class name     */
  190.       "PMM Sample",                     /* Window text                  */
  191.       0,                                /* No special class style       */
  192.       (HMODULE) 0L,                     /* Resource is in .EXE file     */
  193.       ID_WINDOW,                        /* Frame window identifier      */
  194.       &hwndClient                       /* Client window handle         */
  195.       ) ) == 0L )
  196.    {
  197.       WinDestroyMsgQueue( hmq );
  198.       WinTerminate( hab );
  199.       DosBeep( BEEP_WARN_FREQ, BEEP_WARN_DUR );
  200.       return 4; /* Error */
  201.    }
  202.  
  203.    while( WinGetMsg( hab, &qmsg, 0L, 0, 0 ) )
  204.       WinDispatchMsg( hab, &qmsg );
  205.  
  206.    WinDestroyWindow( hwndFrame );
  207.  
  208.    /* Stop tracing and destroy the trace session */
  209. #if USE_PMM
  210.    pmmr = PMMStopTracing( hpmms );
  211.    pmmr = PMMDestroyTraceSession( hpmms );
  212. #endif
  213.  
  214.    WinDestroyMsgQueue( hmq );
  215.  
  216.    WinTerminate( hab );
  217.  
  218.    return 0; /* Success */
  219. } /* End of main */
  220.  
  221. /**************************************************************************
  222.  *
  223.  *  Name       : PMMSampleWindowProc
  224.  *
  225.  *  Description: The window procedure associated with the client area in
  226.  *               the standard frame window.  It processes all messages
  227.  *               either sent or posted to the client area, depending on
  228.  *               the message command and parameters.
  229.  *
  230.  *  Concepts   :
  231.  *
  232.  *  API's      :   WinInvalidateRegion
  233.  *                 WinPostMsg
  234.  *                 WinDefWindowProc
  235.  *                 WinBeginPaint
  236.  *                 GpiSetColor
  237.  *                 GpiSetBackColor
  238.  *                 GpiSetBackMix
  239.  *                 GpiCharStringAt
  240.  *                 WinEndPaint
  241.  *
  242.  *  Parameters :  hwnd = window handle
  243.  *                msg = message code
  244.  *                mp1 = first message parameter
  245.  *                mp2 = second message parameter
  246.  *
  247.  *  Return     :  depends on message sent
  248.  *
  249.  *************************************************************************/
  250. MRESULT EXPENTRY PMMSampleWindowProc( HWND hwnd, ULONG msg,
  251.                                       MPARAM mp1, MPARAM mp2 )
  252. {
  253.    static CHAR szString[20];
  254.  
  255.    switch( msg )
  256.    {
  257.    case WM_CREATE:
  258.       /*
  259.          Window initialization is performed here in WM_CREATE processing
  260.       */
  261.       strcpy( szString, "Hello" );
  262.       break;
  263.    case WM_COMMAND:
  264.       /*
  265.          When the user chooses option 1, 2, or 3 from the Options pull-
  266.          down, the text string is set to 1, 2, or 3, and
  267.          WinInvalidateRegion sends a WM_PAINT message.
  268.          When Exit is chosen, the application posts itself a WM_CLOSE
  269.          message.
  270.       */
  271.    {
  272.       USHORT command;                /* WM_COMMAND command value     */
  273.       command = SHORT1FROMMP(mp1);   /* Extract the command value    */
  274.       switch( command )
  275.       {
  276.       case ID_OPTION1:
  277.          strcpy( szString, "Option 1" );
  278.          WinInvalidateRegion( hwnd, 0L, FALSE );
  279.          break;
  280.       case ID_OPTION2:
  281.          strcpy( szString, "Option 2" );
  282.          WinInvalidateRegion( hwnd, 0L, FALSE );
  283.          break;
  284.       case ID_OPTION3:
  285.          strcpy( szString, "Option 3" );
  286.          WinInvalidateRegion( hwnd, 0L, FALSE );
  287.          break;
  288.       case ID_EXITPROG:
  289.          WinPostMsg( hwnd, WM_CLOSE, (MPARAM) 0, (MPARAM) 0 );
  290.          break;
  291.       default:
  292.          return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  293.       }
  294.       break;
  295.    }
  296.    case WM_ERASEBACKGROUND:
  297.       /*
  298.         Return TRUE to request PM to paint the window background
  299.         in SYSCLR_WINDOW.
  300.       */
  301.       return (MRESULT) TRUE;
  302.    case WM_PAINT:
  303.       /*
  304.         Window contents are drawn here in WM_PAINT processing
  305.       */
  306.    {
  307.       HPS    hps;                       /* Presentation Space handle    */
  308.       RECTL  rc;                        /* Rectangle coordinates        */
  309.       POINTL pt;                        /* String screen coordinates    */
  310.                                         /* Create a presentation space  */
  311.       hps = WinBeginPaint( hwnd, 0L, &rc );
  312.       pt.x = 50; pt.y = 50;             /* Set the text coordinates,    */
  313.       GpiSetColor( hps, CLR_NEUTRAL );         /* colour of the text,   */
  314.       GpiSetBackColor( hps, CLR_BACKGROUND );  /* its background and    */
  315.       GpiSetBackMix( hps, BM_OVERPAINT );      /* how it mixes,         */
  316.                                                /* and draw the string...*/
  317.       GpiCharStringAt( hps, &pt, (LONG) strlen( szString ), szString );
  318.       WinEndPaint( hps );                      /* Drawing is complete   */
  319.       break;
  320.    }
  321.    case WM_CLOSE:
  322.       /*
  323.         This is the place to put your termination routines
  324.       */
  325.       /* Cause termination */
  326.       WinPostMsg( hwnd, WM_QUIT, (MPARAM) 0, (MPARAM) 0 );
  327.       break;
  328.    default:
  329.       /*
  330.         Everything else comes here.  This call MUST exist
  331.         in your window procedure
  332.       */
  333.       return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  334.    }
  335.    return (MRESULT) 0;
  336. } /* End of PMMSampleWindowProc */
  337.  
  338. /* End of PMM Sample Source C File ****************************************/
  339.  
  340.