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

  1. /*******************************************************************************
  2. * FILE NAME: pithread.cpp                                                      *
  3. *                                                                              *
  4. * DESCRIPTION:                                                                 *
  5. *   IThread sample program demonstrating thread creation using an              *
  6. *   interesting (albeit worthless) function.                                   *
  7. *                                                                              *
  8. * COPYRIGHT:                                                                   *
  9. *   Licensed Materials - Property of Solution Frameworks                       *
  10. *   Copyright (C) 1996, Solution Frameworks                                    *
  11. *   All Rights Reserved                                                        *
  12. *******************************************************************************/
  13. #ifndef _ITHREAD_
  14.   #include <ithread.hpp>
  15. #endif
  16. #ifndef _IFRAME_
  17.   #include <iframe.hpp>
  18. #endif
  19. #ifndef _IMLE_
  20.   #include <imle.hpp>
  21. #endif
  22. #ifndef _IMCELCV_
  23.   #include <imcelcv.hpp>
  24. #endif
  25. #ifndef _IPUSHBUT_
  26.   #include <ipushbut.hpp>
  27. #endif
  28. #ifndef _ISTATTXT_
  29.   #include <istattxt.hpp>
  30. #endif
  31. #ifndef _IENTRYFD_
  32.   #include <ientryfd.hpp>
  33. #endif
  34. #ifndef _ICMDHDR_
  35.   #include <icmdhdr.hpp>
  36. #endif
  37. #ifndef _ISTRING_
  38.   #include <istring.hpp>
  39. #endif
  40.  
  41. #include "userevt.hpp"
  42. #include "pi.hpp"
  43.  
  44. /*----------------------- PiOnAThread --------------------------
  45. | This simple IThreadFn derivative calculates pi to some       |
  46. | arbitrary number of digits.  The results are sent as a       |
  47. | user-defined event to some window.                           |
  48. --------------------------------------------------------------*/
  49. class PiOnAThread : public IThreadFn {
  50. public:
  51.   PiOnAThread ( unsigned digits,
  52.                 const IWindowHandle &window )
  53.     : arg( digits ),
  54.       win( window )
  55.     {
  56.     }
  57. virtual void
  58.   run ( )
  59.     {
  60.     IString
  61.       result = pi( arg );
  62.     UserEvent( 0, (char*)result ).sendTo( win );
  63.     }
  64. private:
  65. unsigned
  66.   arg;
  67. IWindowHandle
  68.   win;
  69. PiOnAThread( const PiOnAThread& );
  70. operator = ( const PiOnAThread& );
  71. };
  72.  
  73. /*---------------------- ResultHandler -------------------------
  74. | This user-define event handler accepts the result string     |
  75. | and adds this text to an MLE control.                        |
  76. --------------------------------------------------------------*/
  77. struct ResultHandler : public UserHandler {
  78.   ResultHandler ( IMultiLineEdit &output )
  79.     : UserHandler( 0 ), output( output ) {
  80.     handleEventsFor( &output );
  81.   }   
  82.   ~ResultHandler ( ) {
  83.     stopHandlingEventsFor( &output );
  84.   }
  85. virtual Boolean
  86.   handleUserEvent ( UserEvent &event ) {
  87.     output
  88.       .addLineAsLast( (char*)event.parameter1() )
  89.       .refresh();
  90.     return false;
  91.   }
  92. private:
  93. IMultiLineEdit
  94.  &output;
  95. ResultHandler ( const ResultHandler& );
  96. operator = ( const ResultHandler& );
  97. }; // ResultHandler
  98.  
  99. struct Controller : public ICommandHandler {
  100.   Controller ( IFrameWindow   &frame,
  101.                IPushButton    &button,
  102.                IEntryField    &input,
  103.                IMultiLineEdit &output )
  104.     : frame( frame ),
  105.       button( button ),
  106.       input( input ), 
  107.       output( output ),
  108.       resultHandler( output ) {
  109.     this->handleEventsFor( &frame );
  110.   }
  111.   ~Controller ( ) {
  112.     stopHandlingEventsFor( &frame );
  113.   }
  114. virtual Boolean
  115.   command ( ICommandEvent &event ) {
  116.     Boolean
  117.       result = false;
  118.     if ( event.commandId() == button.id() ) {
  119.       unsigned long
  120.         numDigits( input.text().asUnsigned() );
  121.       output
  122.         .addLineAsLast( "Calculating pi to " 
  123.                         + 
  124.                         IString(numDigits) 
  125.                         + 
  126.                         " digits..." )
  127.         .refresh();
  128.       IThread
  129.         calculatePi;
  130.       calculatePi.start( new PiOnAThread(
  131.                                (unsigned)input.text().asInt(),
  132.                                output.handle() ) );
  133.       result = true;
  134.     }
  135.     return result;
  136.   }
  137. private:
  138. IFrameWindow
  139.  &frame;
  140. IPushButton
  141.  &button;
  142. IEntryField
  143.  &input;
  144. IMultiLineEdit
  145.  &output;
  146. ResultHandler
  147.   resultHandler;
  148.   Controller( const Controller & );
  149.   operator= ( const Controller & );
  150. }; // Controller
  151.  
  152. void main ( ) {
  153.   IFrameWindow
  154.     frame( "Pi Calculator" );
  155.   IMultiLineEdit
  156.     client( 0, &frame, &frame );
  157.   frame.setClient( &client );
  158.  
  159.   IMultiCellCanvas
  160.     canvas( 0, &frame, &frame );
  161.   IPushButton
  162.     start( 1, &canvas, &canvas );
  163.   IStaticText
  164.     prompt( 2, &canvas, &canvas );
  165.   IEntryField
  166.     input( 3, &canvas, &canvas );
  167.  
  168.   frame.addExtension( &canvas, IFrameWindow::aboveClient, 38 );
  169.  
  170.   canvas
  171.     .setDefaultCell( ISize( 3, 2 ) );
  172.  
  173.   canvas
  174.     .addToCell( &start, 2, 2, 1 )
  175.     .addToCell( &prompt, 4, 2, 2 )
  176.     .addToCell( &input, 7, 2, 3 )
  177.     .setRowHeight( 1, 2 )
  178.     .setRowHeight( 2, 34, true )
  179.     .setRowHeight( 3, 2 )
  180.     .setColumnWidth( 2, 100, true )
  181.     .setColumnWidth( 4, 200, true )
  182.     .setColumnWidth( 7, 300, true );
  183.  
  184.   start.setText( "Calculate" );
  185.  
  186.   prompt.setText( "Number of digits:" );
  187.   prompt.setAlignment( IStaticText::centerRight );
  188.  
  189.   input.setText( "100" );
  190.  
  191.   Controller
  192.     controller( frame, start, input, client );
  193.  
  194.   frame.setFocus();
  195.   frame.showModally();
  196. }
  197.