home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / thread / piserve / piserve.cpp < prev    next >
Text File  |  1996-10-29  |  7KB  |  221 lines

  1. /***************************************************************
  2. * FILE NAME: piserve.cpp                                       *                
  3. *                                                              *                
  4. * DESCRIPTION:                                                 *                
  5. *   IThread sample program demonstrating use of a "service     *
  6. *   thread" (as implemented by the ServiceThread class         *
  7. *   declared in service.hpp).                                  *
  8. *                                                              *                
  9. * COPYRIGHT:                                                   *                
  10. *   Licensed Materials - Property of Solution Frameworks       *                
  11. *   Copyright (C) 1996, Solution Frameworks                    *                
  12. *   All Rights Reserved                                        *                
  13. ***************************************************************/
  14. #ifndef _ITHREAD_
  15.   #include <ithread.hpp>
  16. #endif
  17. #ifndef _IFRAME_
  18.   #include <iframe.hpp>
  19. #endif
  20. #ifndef _IMLE_
  21.   #include <imle.hpp>
  22. #endif
  23. #ifndef _IMCELCV_
  24.   #include <imcelcv.hpp>
  25. #endif
  26. #ifndef _IPUSHBUT_
  27.   #include <ipushbut.hpp>
  28. #endif
  29. #ifndef _ISTATTXT_
  30.   #include <istattxt.hpp>
  31. #endif
  32. #ifndef _IENTRYFD_
  33.   #include <ientryfd.hpp>
  34. #endif
  35. #ifndef _ICMDHDR_
  36.   #include <icmdhdr.hpp>
  37. #endif
  38. #ifndef _ISTRING_
  39.   #include <istring.hpp>
  40. #endif
  41.  
  42. #include "service.hpp"
  43. #include "userevt.hpp"
  44. #include "pi.hpp"
  45.  
  46. /*----------------------- PiCalculator -------------------------
  47. | Simple class with a member function to calculate pi to       |
  48. | some number of digits and then send a user-defined event     |
  49. | to a frame window, passing the result.                       |
  50. --------------------------------------------------------------*/
  51. class PiCalculator {
  52. public: 
  53. // Construct with an MLE to be updated with the results.
  54.   PiCalculator( IMultiLineEdit &mle )
  55.     : digits( 0 ), results( mle ) {
  56.     }
  57. // This function calculates pi and updates the result window.
  58. void
  59.   calculate ( )
  60.     {
  61.     IString  result = pi( digits );
  62.     UserEvent( 0, (char*)result ).sendTo( results.handle() );
  63.     }
  64. // Use this function to set the number of digits to calculate.
  65. void
  66.   setDigits ( unsigned long numDigits ) {
  67.     digits = numDigits;
  68.   }
  69. private:
  70. unsigned long
  71.   digits;
  72. IMultiLineEdit 
  73.  &results;
  74. PiCalculator ( const PiCalculator& );
  75. operator = ( const PiCalculator& );
  76. };
  77.  
  78. /*---------------------- ResultHandler -------------------------
  79. | Handler for user-defined event that occurs when a result     |
  80. | has been calculated.  We simply add the result as the        |
  81. | last line of the associated MLE.                             |
  82. --------------------------------------------------------------*/
  83. struct ResultHandler : public UserHandler {
  84.   ResultHandler ( IMultiLineEdit &output )
  85.     : UserHandler( 0 ), output( output ) {
  86.     handleEventsFor( &output );
  87.   }   
  88.   ~ResultHandler ( ) {
  89.     stopHandlingEventsFor( &output );
  90.   }
  91. virtual Boolean
  92.   handleUserEvent ( UserEvent &event ) {
  93.     output
  94.       .addLineAsLast( (char*)event.parameter1() )
  95.       .refresh();
  96.     return false;
  97.   }
  98. private:
  99. IMultiLineEdit
  100.  &output;
  101. ResultHandler ( const ResultHandler& );
  102. operator = ( const ResultHandler& );
  103. }; // ResultHandler
  104.  
  105. /*------------------------ Controller --------------------------
  106. | This class is basically a command handler that ties          |
  107. | together the various parts of our pi calculator user         |
  108. | interface.                                                   |
  109. |                                                              |
  110. | It wires together a frame window, a button, and input and    |
  111. | output controls.  When the button is pressed, it takes       |
  112. | the input field and calculates pi to however many digits     |
  113. | is specified.  The results are written to the output         |
  114. | control (an MLE).                                            |
  115. |                                                              |
  116. | The calculations are performed by posting a request to a     |
  117. | ServiceThread data member.                                   |
  118. --------------------------------------------------------------*/
  119. struct Controller : public ICommandHandler {
  120.   Controller ( IFrameWindow   &frame,
  121.                IPushButton    &button,
  122.                IEntryField    &input,
  123.                IMultiLineEdit &output )
  124.     : frame( frame ),
  125.       button( button ),
  126.       input( input ), 
  127.       output( output ),
  128.       resultHandler( output ),
  129.       calculator( output ) {
  130.     this->handleEventsFor( &frame );
  131.   }
  132.   ~Controller ( ) {
  133.     stopHandlingEventsFor( &frame );
  134.   }
  135. virtual Boolean
  136.   command ( ICommandEvent &event ) {
  137.     Boolean
  138.       result = false;
  139.     if ( event.commandId() == button.id() ) {
  140.       unsigned long
  141.         numDigits( input.text().asUnsigned() );
  142.       output
  143.         .addLineAsLast( "Calculating pi to " 
  144.                         + 
  145.                         IString(numDigits) 
  146.                         + 
  147.                         " digits..." );
  148.       calculator.setDigits( numDigits );
  149.       serviceThread.postRequest(
  150.             new IThreadMemberFn<PiCalculator>( 
  151.                              calculator,
  152.                              PiCalculator::calculate ) );
  153.       result = true;
  154.     }
  155.     return result;
  156.   }
  157. private:
  158. IFrameWindow
  159.  &frame;
  160. IPushButton
  161.  &button;
  162. IEntryField
  163.  &input;
  164. IMultiLineEdit
  165.  &output;
  166. ResultHandler
  167.   resultHandler;
  168. PiCalculator
  169.   calculator;
  170. ServiceThread
  171.   serviceThread;
  172.   Controller( const Controller & );
  173.   operator= ( const Controller & );
  174. }; // Controller
  175.  
  176. void main ( ) {
  177.   IFrameWindow
  178.     frame( "Pi Calculator" );
  179.   IMultiLineEdit
  180.     client( 0, &frame, &frame );
  181.   frame.setClient( &client );
  182.  
  183.   IMultiCellCanvas
  184.     canvas( 0, &frame, &frame );
  185.   IPushButton
  186.     start( 1, &canvas, &canvas );
  187.   IStaticText
  188.     prompt( 2, &canvas, &canvas );
  189.   IEntryField
  190.     input( 3, &canvas, &canvas );
  191.  
  192.   frame.addExtension( &canvas, IFrameWindow::aboveClient, 38 );
  193.  
  194.   canvas
  195.     .setDefaultCell( ISize( 3, 2 ) );
  196.  
  197.   canvas
  198.     .addToCell( &start, 2, 2, 1 )
  199.     .addToCell( &prompt, 4, 2, 2 )
  200.     .addToCell( &input, 7, 2, 3 )
  201.     .setRowHeight( 1, 2 )
  202.     .setRowHeight( 2, 34, true )
  203.     .setRowHeight( 3, 2 )
  204.     .setColumnWidth( 2, 100, true )
  205.     .setColumnWidth( 4, 200, true )
  206.     .setColumnWidth( 7, 300, true );
  207.  
  208.   start.setText( "Calculate" );
  209.  
  210.   prompt.setText( "Number of digits:" );
  211.   prompt.setAlignment( IStaticText::centerRight );
  212.  
  213.   input.setText( "100" );
  214.  
  215.   Controller
  216.     controller( frame, start, input, client );
  217.  
  218.   frame.setFocus();
  219.   frame.showModally();
  220. }
  221.