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

  1. //*********************************************************
  2. // Reusable Commands - Processing Mouse Pointer Movement
  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 <iapp.hpp>
  9. #include <icoordsy.hpp>
  10. #include <idrawcv.hpp>
  11. #include <iframe.hpp>
  12. #include <igbundle.hpp>
  13. #include <igelipse.hpp>
  14. #include <iglist.hpp>
  15. #include <igpyline.hpp>
  16. #include <igrafctx.hpp>
  17. #include <igrect.hpp>
  18. #include <ihandle.hpp>
  19. #include <imoushdr.hpp>
  20. #include <iptarray.hpp>
  21. #include <istattxt.hpp>
  22. #include <ivport.hpp>
  23. #include <icconst.h>
  24.  
  25. #define ID_CIRCLE     1
  26. #define ID_SQUARE     2
  27. #define ID_RECTANGLE  3
  28. #define ID_TRIANGLE   4
  29. #define ID_ELLIPSE    5
  30. #define ID_PENTAGON   6
  31.  
  32. class ShapeWindow : public IFrameWindow {
  33. public:
  34.   ShapeWindow ( );
  35.  ~ShapeWindow ( );
  36. protected:
  37. Boolean
  38.   processMouseMovement ( IMouseEvent& event );
  39. private:
  40.   ShapeWindow ( const ShapeWindow& );
  41. ShapeWindow
  42.  &operator=   ( const ShapeWindow& );
  43. IStaticText
  44.   instructions;
  45. IViewPort
  46.   viewPort;
  47. IDrawingCanvas
  48.   shapeArea;
  49. IGraphicContext
  50.  *gc;
  51. IGList
  52.   graphicList;
  53. IMouseConnectionTo< ShapeWindow >
  54.   mouseConnection;
  55. }; // ShapeWindow
  56.  
  57. void main ( )
  58. {
  59.   ICoordinateSystem::setApplicationOrientation
  60.                         ( ICoordinateSystem::originLowerLeft );
  61.  
  62.   ShapeWindow
  63.     frame;
  64.   frame
  65.    .setFocus()
  66.    .show();
  67.   IApplication::current().run();
  68. }
  69.  
  70. ShapeWindow::ShapeWindow ( )
  71.   : IFrameWindow( "Mouse Movement Example"),
  72.     instructions( 1, this, this ),
  73.     viewPort( IC_FRAME_CLIENT_ID, this, this ),
  74.     shapeArea( 1, &viewPort, &viewPort ),
  75.     gc( 0 ),
  76.     graphicList(),
  77.     mouseConnection( *this, 0, ShapeWindow::processMouseMovement, 0 )
  78. {
  79.   instructions
  80.    .setText( "Move the mouse pointer over a shape." );
  81.   mouseConnection
  82.    .handleEventsFor( &shapeArea );
  83.  
  84.   // Create a graphic context for the shapeArea window.  We do
  85.   // this once to avoid a performance hit during mouse-movement
  86.   // processing.
  87.   gc = new IGraphicContext( shapeArea.presSpace() );
  88.   shapeArea
  89.    .setGraphicContext( gc );
  90.  
  91.   // Set up the shapes to draw in the shapeArea window.
  92.   IGraphicBundle
  93.     bundle;
  94.   bundle
  95.    .setBackgroundMixMode( IGraphicBundle::backOverPaint )
  96.    .setDrawOperation( IGraphicBundle::fillAndFrame )
  97.    .setPenWidth( 3 )
  98.    .setPenJoiningStyle( IGraphicBundle::miter )
  99.    .setPenColor( IColor::black )
  100.    .setFillColor( IColor::green );
  101.  
  102.   // Create a circle.
  103.   IGEllipse
  104.    *circle = new IGEllipse( IPoint( 110, 110 ), 90 );
  105.   (*circle)
  106.    .setGraphicBundle( bundle )
  107.    .setId( ID_CIRCLE );
  108.  
  109.   // Create a rectangle.
  110.   IGRectangle
  111.    *rectangle = new IGRectangle( IRectangle( IPoint( 170, 20 ),
  112.                                              ISize( 170, 100 ) ) );
  113.   bundle
  114.    .setFillColor( IColor::red );
  115.   (*rectangle)
  116.    .setGraphicBundle( bundle )
  117.    .setId( ID_RECTANGLE );
  118.  
  119.   // Create a square.
  120.   IGRectangle
  121.    *square = new IGRectangle( IRectangle( IPoint( 15, 190 ),
  122.                                           ISize( 160, 160 ) ) );
  123.   bundle
  124.    .setFillColor( IColor::yellow );
  125.   (*square)
  126.    .setGraphicBundle( bundle )
  127.    .setId( ID_SQUARE );
  128.  
  129.   // Create a triangle.
  130.   IPoint trianglePoints[] =
  131.     { IPoint( 320, 150 ), IPoint( 230, 370 ), IPoint( 360, 350 ) };
  132.   IGPolygon
  133.    *triangle = new IGPolygon( IPointArray( 3, trianglePoints ) );
  134.   bundle
  135.    .setFillColor( IColor::pink );
  136.   (*triangle)
  137.    .setGraphicBundle( bundle )
  138.    .setId( ID_TRIANGLE );
  139.  
  140.   // Create an ellipse.
  141.   IGEllipse
  142.    *ellipse = new IGEllipse( IPoint( 200, 320 ), 60, 30 );
  143.   bundle
  144.    .setFillColor( IColor::blue );
  145.   (*ellipse)
  146.    .setGraphicBundle( bundle )
  147.    .setId( ID_ELLIPSE );
  148.  
  149.   // Create a pentagon.
  150.   IPoint pentagonPoints[] =
  151.     { IPoint( 160, 200 ), IPoint( 220, 140 ), IPoint( 280, 200 ),
  152.       IPoint( 260, 270 ), IPoint( 180, 270 ) };
  153.   IGPolygon
  154.    *pentagon = new IGPolygon( IPointArray( 5, pentagonPoints ) );
  155.   bundle
  156.    .setFillColor( IColor::cyan );
  157.   (*pentagon)
  158.    .setGraphicBundle( bundle )
  159.    .setId( ID_PENTAGON );
  160.  
  161.   // Add all shapes to the list that the drawing canvas uses.
  162.   graphicList
  163.    .addAsLast( *square )
  164.    .addAsLast( *circle )
  165.    .addAsLast( *rectangle )
  166.    .addAsLast( *triangle )
  167.    .addAsLast( *ellipse )
  168.    .addAsLast( *pentagon );
  169.   shapeArea
  170.    .setGraphicList( &graphicList );
  171.  
  172.   (*this)
  173.    .setClient( &viewPort )
  174.    .addExtension( &instructions, IFrameWindow::belowClient );
  175. }
  176.  
  177. ShapeWindow::~ShapeWindow ( )
  178. {
  179.   // Clean up the graphic list that the shapeArea window uses.
  180.   unsigned long
  181.     count = graphicList.numberOfGraphics();
  182.   while ( count-- )
  183.   {
  184.     IGraphic
  185.      *graphic = graphicList.firstGraphic();
  186.     graphicList
  187.      .removeFirst();
  188.     delete graphic;
  189.   }
  190.  
  191.   // Clean up the graphic context that the shapeArea window uses.
  192.   if ( shapeArea.isValid() )
  193.   {
  194.      shapeArea
  195.       .releasePresSpace( gc->handle() );
  196.   }
  197. #ifdef IC_PM
  198.   else
  199.   {
  200.      IWindow::desktopWindow()->releasePresSpace( gc->handle() );
  201.   }
  202. #endif
  203.   delete gc;
  204. }
  205.  
  206. // This is the function that the IMouseConnectionTo<> object calls.
  207. IBase::Boolean ShapeWindow::processMouseMovement ( IMouseEvent& event )
  208. {
  209.   if ( event.windowUnderPointer() ==
  210.          event.dispatchingWindow()->handle() )
  211.   {      // The mouse pointer is over the drawing canvas.
  212.      IGraphic
  213.       *graphicUnderMousePointer =
  214.           graphicList.topGraphicUnderPoint( event.mousePosition(), *gc );
  215.      char
  216.       *infoText = "Move the mouse pointer over a shape.";
  217.  
  218.      // Test if the mouse pointer is over a shape.
  219.      if ( graphicUnderMousePointer )
  220.      {        // Now figure out which shape.
  221.         switch ( graphicUnderMousePointer->id() )
  222.         {
  223.           case ID_CIRCLE:
  224.             infoText = "Circle";
  225.             break;
  226.           case ID_SQUARE:
  227.             infoText = "Square";
  228.             break;
  229.           case ID_RECTANGLE:
  230.             infoText = "Rectangle";
  231.             break;
  232.           case ID_TRIANGLE:
  233.             infoText = "Triangle";
  234.             break;
  235.           case ID_ELLIPSE:
  236.             infoText = "Ellipse";
  237.             break;
  238.           case ID_PENTAGON:
  239.             infoText = "Pentagon";
  240.             break;
  241.           default:
  242.             break;
  243.         }
  244.      }
  245.  
  246.      instructions
  247.       .setText( infoText );
  248.   }
  249.   return false;
  250. }
  251.