Copyright © Taligent, Inc. 1995 1 class THelloWorld : public TDocumentComponentView, public MMouseEventHandler 2 { 3 public: 4 THelloWorld( TGUIBundle* b ) : 5 TDocumentComponentView( b ), 6 { 7 CheckForInitialize(); 8 SetAllocatedArea( TGRect( 0,0, 1000, 1000 ) ); 9 10 TFillBundle* blackBundle = new TFillBundle( TRGBColor( 0, 0, 0 ) ); 11 12 TGPolygon blackPolygonGeometry; 13 blackPolygonGeometry.Append( TGPoint( 100, 60 ) ); 14 blackPolygonGeometry.Append( TGPoint( 140, 115 ) ); 15 blackPolygonGeometry.Append( TGPoint( 60, 115 ) ); 16 fGraphic = new TPolygon( blackPolygonGeometry, blackBundle ); 17 } 18 19 ~THelloWorld() 20 { 21 22 CheckForFinalize( ); 23 delete fGraphic; 24 } 25 void 26 Initialize() 27 { 28 TDocumentComponentView::Initialize(); 29 } 30 void DrawContents( TGrafPort& thePort ) const 31 { 32 TGArea area; 33 GetAllocatedArea( area ); 34 TFillBundle bundle( new TRGBColor( 1,1,1 ) ); 35 thePort.Draw ( area.GetBounds(), bundle ); 36 37 fGraphic->Draw( thePort ); 38 } 39 Boolean 40 MouseDown( TMouseDownEvent& mouseDown ) 41 { 42 TGPoint selection( mouseDown.GetEventPosition() ); 43 GlobalToLocal( selection ); 44 45 TGRect bounds( fGraphic->GetGeometricBounds() ); 46 47 if ( bounds.Contains( selection ) ) 48 qprintf( "Selected\n" ); 49 else 50 qprintf( "not selected\n" ); 51 52 Invalidate(); 53 54 return( TRUE); 55 } 56 VersionDeclarationsMacro( THelloWorld ); 57 58 private: 59 THelloWorld( conts THellowWorld& ); 60 TPolygon* fGraphic;
The addition of MMouseEventHandler means that the graphic must be accessible to the MouseDown function.
Lines 10 through 16 and Lines 58 through 60: Instead of creating and drawing the polygon in DrawContents, a private field is added to THelloWorld of type TPolygon. The THelloWorld constructor creates the polygon and stores it in fGraphic.
Line 23: A line is added to the destructor to delete fGraphic.
The Invalidate function redraws the view after changes are made. If you adapted this function to change the color of the fill, the Invalidate function redraws the View with the new fill color.
MouseDown function
Lines 40 through 55: The View system calls the MouseDown function when a mouse down event is detected. The mouseDown parameter contains the TGPoint where the mouse down event occurred in global coordinates. The code gets the TGPoint from mouseDown, converts it to local coordinates, gets the bounds of the polygon and tests to see whether the TGPoint is contained within the bounds of the polygon. Since the bounds is a rectangle and the polygon is not rectangular, there are points close to, but outside of, the polygon that return True because they are still within the polygon bounds. Changes to DrawContents
Lines 30 through 38: To draw the polygon, you call the Draw function on fGraphic.
[Contents]
[Previous]
[Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Generated with WebMaker