home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / thread2.zip / APP.C next >
Text File  |  1993-06-28  |  6KB  |  177 lines

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