home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / thread / threads / threads.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  4.0 KB  |  144 lines

  1. /***************************************************************
  2. * FILE NAME: threads.cpp                                       *
  3. *                                                              *
  4. * DESCRIPTION:                                                 *
  5. *   Comprehensive IThread demonstration program.               *
  6. *                                                              *
  7. * COPYRIGHT:                                                   *
  8. *   Licensed Materials - Property of Solution Frameworks       *
  9. *   Copyright (C) 1996, Solution Frameworks                    *
  10. *   All Rights Reserved                                        *
  11. ***************************************************************/
  12. #include <iframe.hpp>
  13. #include <icnr.hpp>
  14. #include <ithread.hpp>
  15. #include <icnrmhdr.hpp>
  16. #include <ipopmenu.hpp>
  17. #include <istattxt.hpp>
  18. #include <istparse.hpp>
  19. #include <isubmenu.hpp>
  20. #include <itimer.hpp>
  21.  
  22. #include "threads.h"
  23.  
  24. #include "thread.hpp"
  25.  
  26. #include "cmdroutr.hpp"
  27.  
  28. /*-------------------- ThreadsMenuHandler ----------------------
  29. | This derived ICnrMenuHandler class handles popup menu        |
  30. | requests on the main window.  If the popup is over empty     |
  31. | space, it puts up the new/exit menu.  Otherwise, it          |
  32. | requests a popup menu from the selected Thread object.       |                         
  33. --------------------------------------------------------------*/
  34. class ThreadsMenuHandler : public ICnrMenuHandler {
  35. public:
  36.   ThreadsMenuHandler ( IContainerControl &cnr ) 
  37.     : ICnrMenuHandler(),
  38.       cnr( cnr ),
  39.       pThreadObj(0),
  40.       router( cnr, *this, handleCommand ) {
  41.     this->handleEventsFor( &cnr );
  42.   }
  43. protected:
  44. virtual Boolean
  45.   makePopUpMenu ( IMenuEvent &event ) {
  46.     Boolean
  47.       result = false;
  48.  
  49.     pThreadObj = (Thread*)( this->popupMenuObject() );
  50.  
  51.     if ( pThreadObj ) {
  52.       result = pThreadObj->makePopUpMenu( event );
  53.     } else {
  54.       IPopUpMenu
  55.        *pMenu = new IPopUpMenu( THREADS_POPUP, &cnr );
  56.       pMenu->setAutoDeleteObject();
  57.       pMenu->show( event.mousePosition() );
  58.       result = true;
  59.     }
  60.  
  61.     return result;
  62.   }
  63.  
  64. private:
  65. Boolean
  66.   handleCommand( ICommandEvent &cmd ) {
  67.     Boolean
  68.       result = false;
  69.  
  70.     switch ( cmd.commandId() ) {
  71.       case THREADS_NEW:
  72.         {
  73.         new Thread( cnr );
  74.         result = true;
  75.         }
  76.         break;
  77.  
  78.       case THREADS_EXIT:
  79.         IThread::current().stopProcessingMsgs();
  80.         result = true;
  81.         break;
  82.  
  83.       default:
  84.         // Pass command to the selected Thread.
  85.         if ( pThreadObj ) {
  86.           result = pThreadObj->handleCommand( cmd );
  87.           cnr.refresh();
  88.         }
  89.         break;
  90.     }
  91.     return result;
  92.   }
  93. IContainerControl
  94.  &cnr;
  95. Thread
  96.  *pThreadObj;
  97. CommandRouter<ThreadsMenuHandler>
  98.   router;
  99. ThreadsMenuHandler ( const ThreadsMenuHandler& );
  100. operator = ( const ThreadsMenuHandler& );
  101. }; // ThreadsMenuHandler
  102.  
  103. /*--------------------------- main -----------------------------
  104. | Create the main window with container control and attach our |
  105. | menu handler.  We create the first Thread object for the     |
  106. | primary thread and go.                                       |
  107. --------------------------------------------------------------*/
  108. void main() {
  109.   IFrameWindow
  110.     frame( "IThread Sample Program, Process "
  111.            +
  112.            IApplication::current().id().asString() );
  113.  
  114.   IRectangle
  115.     initRect = IFrameWindow::nextShellRect().scaleBy( 0.66, 0.5 );
  116.  
  117.   IContainerControl
  118.     client( 0, &frame, &frame );
  119.  
  120.   ThreadsMenuHandler
  121.     menuHandler( client );
  122.  
  123.   frame
  124.     .setClient( &client )
  125.     .setIcon( THREAD1 )
  126.     .moveSizeTo( initRect )
  127.     .setFocus()
  128.     .show();
  129.  
  130.   new Thread( client, true );
  131.  
  132.   Thread::addColumnsTo( client );
  133.  
  134.   client
  135.     .showDetailsView()
  136.     .showDetailsViewTitles()
  137.     .refresh();
  138.  
  139.   IThread::current().processMsgs();
  140.  
  141.   // Destroy all the Threads.
  142.   client.deleteAllObjects();
  143. }
  144.