home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MONTE.ZIP / PMTHREAD / APP.C next >
Text File  |  1993-02-22  |  5KB  |  170 lines

  1. /* app.c:
  2. A sample PM application showing use of an object window operated by
  3. thread 2 for time-consuming tasks.
  4. */
  5. // os2 includes
  6. #define INCL_DOSPROCESS
  7. #define INCL_WIN
  8. #include <os2.h>
  9. // c includes
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <assert.h>
  14. // app includes
  15. #include "def.h"
  16. #include "app.h"
  17. #include "pmassert.h"
  18.  
  19. // ----------------------------------------------------------------------
  20. // main entry point for thread 1
  21. int main ( void )
  22. {
  23.   APIRET   rc;
  24.   BOOL     fSuccess;
  25.   HAB      hab;
  26.   HMQ      hmq;
  27.   HWND     hwndClient;
  28.   HWND     hwndFrame;
  29.   QMSG     qmsg;
  30.   ULONG    flCreate;
  31.   PGLOBALS pg;
  32.  
  33.   // PM application init
  34.   hab = WinInitialize( 0 );
  35.   hmq = WinCreateMsgQueue( hab, 0 );
  36.   assert( hmq );
  37.  
  38.   // register client window class
  39.   // with 4 bytes of window words to hold a pointer to globals
  40.   fSuccess = WinRegisterClass( hab, APP_CLASS_CLIENT, (PFNWP)ClientWinProc,
  41.                       CS_SIZEREDRAW | CS_CLIPCHILDREN, sizeof( PGLOBALS ) );
  42.   pmassert( hab, fSuccess );
  43.  
  44.   flCreate = FCF_SYSMENU | FCF_SIZEBORDER    | FCF_TITLEBAR |
  45.              FCF_MINMAX  | FCF_SHELLPOSITION | FCF_TASKLIST |
  46.              FCF_MENU    | FCF_ICON;
  47.  
  48.   // standard window create; returns after WM_CREATE processing finishes
  49.   hwndFrame = WinCreateStdWindow( HWND_DESKTOP, WS_VISIBLE, &flCreate,
  50.                APP_CLASS_CLIENT, APP_TITLE, 0, 0, ID_APP, &hwndClient );
  51.   pmassert( hab, hwndFrame );
  52.   pmassert( hab, hwndClient );
  53.   pg = (PGLOBALS) WinQueryWindowULong( hwndClient, QWL_USER );
  54.  
  55.   // dispatch user input messages
  56.   while( WinGetMsg( hab, &qmsg, 0, 0, 0 ))
  57.   {
  58.     WinDispatchMsg( hab, &qmsg );
  59.   }
  60.  
  61.   // wrap up
  62.   WinDestroyWindow ( hwndFrame );
  63.   WinDestroyMsgQueue ( hmq );
  64.   WinTerminate ( hab );
  65.  
  66.   rc = DosWaitThread( &pg->tidObject, DCWW_WAIT );
  67.   assert( rc == 0 );
  68.  
  69.   // exit the process
  70.   return 0;
  71. }
  72.  
  73. // ----------------------------------------------------------------------
  74. // client window procedure
  75. MRESULT EXPENTRY ClientWinProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  76. {
  77.   HAB           hab;
  78.   HPS           hps;
  79.   PGLOBALS      pg;
  80.   RECTL         rectl;
  81.   ULONG         ulWork;
  82.  
  83.   switch( msg ) {
  84.   case WM_CLOSE:
  85.     pg = (PGLOBALS) WinQueryWindowULong( hwnd, QWL_USER );
  86.     WinPostMsg( pg->hwndObject, WM_QUIT, 0, 0 );
  87.     return (MRESULT) 0;
  88.  
  89.   case WM_COMMAND:
  90.     pg = (PGLOBALS) WinQueryWindowULong( hwnd, QWL_USER );
  91.     switch( SHORT1FROMMP( mp1 )) {
  92.     case IDM_SLEEP:
  93.       // disable client for this lengthy task
  94.       WinSendMsg( hwnd, WM_USER_DISABLE_CLIENT, 0L, 0L );
  95.       // tell object window to perform lengthy task
  96.       WinPostMsg( pg->hwndObject, WM_USER_SLEEP, (MPARAM)hwnd, 0L );
  97.       // wait for ack
  98.       break;
  99.     }
  100.     return (MRESULT) 0;
  101.  
  102.   case WM_CREATE:
  103.     hab = WinQueryAnchorBlock( hwnd );
  104.     // allocate global memory; see GLOBALS struct in app.h
  105.     pg = (PGLOBALS) malloc( sizeof( GLOBALS ));
  106.     pmassert( hab, pg );
  107.     // initialize globals to zero
  108.     memset( pg, 0, sizeof( GLOBALS ));
  109.     // store globals pointer into client window words
  110.     WinSetWindowULong( hwnd, QWL_USER, (ULONG) pg );
  111.     // disable client until object window initializes
  112.     WinSendMsg( hwnd, WM_USER_DISABLE_CLIENT, 0, 0 );
  113.     // initialize globals with important data
  114.     pg->hab          = hab;
  115.     pg->hwndClient   = hwnd;
  116.     pg->hwndFrame    = WinQueryWindow( hwnd, QW_PARENT );
  117.     pg->hwndTitlebar = WinWindowFromID( pg->hwndFrame, FID_TITLEBAR );
  118.     pg->hwndMenubar  = WinWindowFromID( pg->hwndFrame, FID_MENU );
  119.     // create thread 2 for object window; pass pointer to globals
  120.     pg->tidObject = _beginthread( threadmain, NULL, LEN_STACK, (void *)pg );
  121.     pmassert( hab, pg->tidObject );
  122.     return (MRESULT) 0;
  123.  
  124.   case WM_MOUSEMOVE:
  125.     // if busy, display the wait pointer, else the arrow pointer
  126.     pg = (PGLOBALS) WinQueryWindowULong( hwnd, QWL_USER );
  127.     ulWork = pg->fBusy ? SPTR_WAIT : SPTR_ARROW;
  128.     WinSetPointer(HWND_DESKTOP,WinQuerySysPointer(HWND_DESKTOP,ulWork,FALSE));
  129.     return (MRESULT) TRUE;
  130.  
  131.   case WM_PAINT:
  132.     hps = WinBeginPaint( hwnd, 0, &rectl );
  133.     WinFillRect( hps, &rectl, SYSCLR_WINDOW );
  134.     WinEndPaint( hps );
  135.     return (MRESULT) 0;
  136.  
  137.   case WM_USER_ACK:
  138.     // object window has completed which task?
  139.     pg = (PGLOBALS) WinQueryWindowULong( hwnd, QWL_USER );
  140.     switch( (ULONG) mp1 ) {
  141.     case WM_USER_SLEEP:
  142.       WinMessageBox( HWND_DESKTOP, pg->hwndFrame, "Done sleeping.",
  143.                        APP_TITLE, 0, MB_CANCEL);
  144.       break;
  145.     }
  146.     WinSendMsg( hwnd, WM_USER_ENABLE_CLIENT, 0, 0 );
  147.     return (MRESULT) 0;
  148.  
  149.   case WM_USER_DISABLE_CLIENT:
  150.     // this message sent by client
  151.     // disable all but the frame window
  152.     pg = (PGLOBALS) WinQueryWindowULong( hwnd, QWL_USER );
  153.     pg->fBusy = TRUE;
  154.     WinEnableWindow( pg->hwndClient, FALSE );
  155.     WinEnableWindow( pg->hwndMenubar, FALSE );
  156.     return (MRESULT) 0;
  157.  
  158.   case WM_USER_ENABLE_CLIENT:
  159.     // this message sent by client
  160.     // enable client and menu
  161.     pg = (PGLOBALS) WinQueryWindowULong( hwnd, QWL_USER );
  162.     WinEnableWindow( pg->hwndClient, TRUE );
  163.     WinEnableWindow( pg->hwndMenubar, TRUE );
  164.     pg->fBusy = FALSE;
  165.     return (MRESULT) 0;
  166.   }
  167.   // default
  168.   return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  169. }
  170.