home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / genhdrs / painthdr / painthdr.cpp < prev   
Text File  |  1996-10-29  |  5KB  |  189 lines

  1. //*********************************************************
  2. // Reusable Handlers - Paint Handler
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc.
  6. // All Rights Reserved.
  7. //*********************************************************
  8. #include <iframe.hpp>
  9. #include <igbundle.hpp>
  10. #include <igline.hpp>
  11. #include <igrafctx.hpp>
  12. #include <igrect.hpp>
  13. #include <igregion.hpp>
  14. #include <ihandle.hpp>
  15. #include <imcelcv.hpp>
  16. #include <ipainhdr.hpp>
  17. #include <ipushbut.hpp>
  18. #include <istattxt.hpp>
  19. #include <istring.hpp>
  20.  
  21. class InvertHalfPaintHandler : public IPaintHandler {
  22. protected:
  23. virtual Boolean
  24.   paintWindow ( IPaintEvent& event );
  25. }; // InvertHalfPaintHandler
  26.  
  27. IBase::Boolean
  28.   InvertHalfPaintHandler::paintWindow ( IPaintEvent& event )
  29. {
  30.   // This paint handler paints the left half of a window by
  31.   // inverting its contents.
  32.   IWindow
  33.    *windowToPaint = event.dispatchingWindow();
  34.   IRectangle
  35.     leftHalf( windowToPaint->rect()
  36.                .moveTo( IPoint() )
  37.                .scaleBy( 0.5, 1 ) );
  38.   IRectangle
  39.     paintArea( event.rect() & leftHalf );
  40.  
  41.   // First let the window paint its contents.
  42.   (*windowToPaint)
  43.    .dispatchRemainingHandlers( event, true );
  44.  
  45.   // See if any part of the left half needs painting.
  46.   if ( paintArea.size() > ISize() )
  47.   {      // Something needs painting.
  48.      // Reset the clip region, in case the window cleared
  49.      // it as part of its painting.
  50.      IGraphicContext
  51.        gc( event.presSpaceHandle() );
  52.      IGRegion
  53.        paintRegion( gc, paintArea );
  54.      gc
  55.       .setClipRegion( paintRegion );
  56.  
  57.      // Do the XOR paint now.
  58.      IGraphicBundle
  59.        bundle( gc );
  60.      bundle
  61.       .setDrawOperation( IGraphicBundle::fill )
  62.       .setMixMode( IGraphicBundle::xor )
  63.       .setFillColor( IColor::yellow );
  64.      IGRectangle
  65.        grect( paintArea );
  66.      grect
  67.       .setGraphicBundle( bundle );
  68.      grect
  69.       .drawOn( gc );
  70.   }
  71.   return true;
  72. }
  73.  
  74. class XWindowPaintHandler : public IPaintHandler {
  75. public:
  76. protected:
  77. virtual Boolean
  78.   paintWindow ( IPaintEvent& event );
  79. };  // XWindowPaintHandler
  80.  
  81. IBase::Boolean
  82.   XWindowPaintHandler::paintWindow ( IPaintEvent& event )
  83. {
  84.   IWindow
  85.    *windowToPaint = event.dispatchingWindow();
  86.  
  87.   // Save the clip region in case the window clears it
  88.   // as part of its painting.
  89.   IGraphicContext
  90.     gc( event.presSpaceHandle() );
  91.   IRegionHandle
  92.     origClipRegion( gc.clipRegion() );
  93.  
  94.   // Let the window paint its contents.
  95.   (*windowToPaint)
  96.    .dispatchRemainingHandlers( event, true );
  97.  
  98.   // Reset the clip region in case the window cleared it
  99.   // as part of its painting.
  100.   gc
  101.    .setClipRegion( origClipRegion );
  102.  
  103.   // Now draw the two diagonal red lines.
  104.   IRectangle
  105.     windowRect( windowToPaint->rect().moveTo( IPoint() ) );
  106.   IGLine
  107.     diag1( windowRect.bottomLeft(), windowRect.topRight() ),
  108.     diag2( windowRect.bottomRight(), windowRect.topLeft() );
  109.   IGraphicBundle
  110.     bundle( gc );
  111.   bundle
  112.    .setMixMode( IGraphicBundle::overPaint )
  113.    .setPenType( IGraphicBundle::solid )
  114.    .setPenEndingStyle( IGraphicBundle::rounded )
  115.    .setPenWidth( 2 )
  116.    .setPenColor( IColor::red );
  117.   diag1
  118.    .setGraphicBundle( bundle )
  119.    .drawOn( gc );
  120.   diag2
  121.    .setGraphicBundle( bundle )
  122.    .drawOn( gc );
  123.  
  124.   return true;
  125. }
  126.  
  127. void main ( )
  128. {
  129.   IFrameWindow
  130.     frame( "Paint Handler Example" );
  131.  
  132.   // Create the client area.
  133.   IMultiCellCanvas
  134.     client( IC_FRAME_CLIENT_ID, &frame, &frame );
  135.   frame
  136.    .setClient( &client );
  137.  
  138.   IStaticText
  139.     text1( 1, &client, &client ),
  140.     text2( 2, &client, &client );
  141.   text1
  142.    .setText( "Standard static text" );
  143.   text2
  144.    .setText( "Static text with a paint handler" );
  145.  
  146.   IPushButton
  147.     button1( 3, &client, &client ),
  148.     button2( 4, &client, &client );
  149.   button1
  150.    .setText( "Standard push button" )
  151.    .enableTabStop()
  152.    .enableGroup();
  153.   button2
  154.    .setText( "Push button with a paint handler" )
  155.    .enableTabStop();
  156.  
  157.   client
  158.    .addToCell( &text1,   2, 2 )
  159.    .addToCell( &text2,   2, 4 )
  160.    .addToCell( &button1, 2, 6 )
  161.    .addToCell( &button2, 2, 8 )
  162.    .setColumnWidth( 2, 10, true )
  163.    .setColumnWidth( 3, 10 )
  164.    .setRowHeight( 2, 10, true )
  165.    .setRowHeight( 4, 10, true )
  166.    .setRowHeight( 6, 10, true )
  167.    .setRowHeight( 8, 10, true )
  168.    .setRowHeight( 9, 10 );
  169.  
  170.   // Construct and attach the paint handlers.
  171.   InvertHalfPaintHandler
  172.     invertPaintHandler;
  173.   invertPaintHandler
  174.    .handleEventsFor( &button2 );
  175.   XWindowPaintHandler
  176.     xWindowPaintHandler;
  177.   xWindowPaintHandler
  178.    .handleEventsFor( &text2 );
  179.  
  180.   // Show the window now.
  181.   frame
  182.    .moveSizeToClient( IRectangle( IPoint( 50, 50 ),
  183.                                   ISize( client.minimumSize() ) ) )
  184.    .setFocus()
  185.    .show();
  186.  
  187.   IApplication::current().run();
  188. }
  189.