Code walkthrough

This section contains code listings for the additions you need to make to the Tiles program to provide mouse-event handling:

TTile::ContainsPoint

TTile::ContainsPoint is declared in the file
./TilesTutorial/02.MouseEvents/Tiles/TilesModel.h:

    bool    ContainsPoint(const TGPoint&) const;
The function is const because the caller (generally the view) only requires read-only access to determine whether the tile contains a particular point.

The implementation of the TTile::ContainsPoint function simply calls the TGRect::Contains function on the TTile data member representing the bounds of the tile. TTile::ContainsPoint is implemented in the file
./TilesTutorial/02.MouseEvents/Tiles/TilesModel.C:

      bool
      TTile::ContainsPoint(const TGPoint& point) const
      {
          return fBounds.Contains(point);
      }

Mixing in MMouseEventHandler

To implement mouse-event handling, TTilesView now also mixes in MMouseEventHandler and overrides the inherited MouseDown function. The HandleAfterConnectionToViewRoot function also requests the view to convert mouse events to the view's local coordinate system instead of global coordinates.

The new code in the TTilesView header, declared in the file
./TilesTutorial/02.MouseEvents/Tiles/TilesView.h, is listed in bold below.

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      // Mixes in MMouseEventHandler and overrides MouseDown.
      class TTilesView : public TDocumentComponentView, public MMouseEventHandler {
      public:
          // TTilesView public constructor, destructor, and macro call are unchanged.
      
          virtual bool    MouseDown(TMouseDownEvent&);
          
      protected:
          // Adds new initialization routines to this function.
          virtual void    HandleAfterConnectionToViewRoot();
          virtual void    HandleBeforeDisconnectionFromViewRoot();
          // TTilesView drawing functions and protected constructors unchanged.
      }
TTilesView::MouseDown is implemented in the file
./TilesTutorial/02.MouseEvents/Tiles/TilesView.C:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      // Return value indicates whether the event was handled.
      bool
      TTilesView::MouseDown (TMouseDownEvent& mouseDownEvent)
      {
          TileIndex whichTile; 
          
          // Gets the position of the mouse down.
          TGPoint mousePoint = mouseDownEvent.GetEventPosition();
      
          {
              // Creates a smart pointer to the model.
              const TModelPointerTo<TTilesModel> model(GetModelReference());
              
      
              // Determines which tile, if any, was clicked on.
              for (whichTile = model->GetNumTiles() - 1; whichTile >= 0; whichTile--) {
                  if (model->GetTileForReading(whichTile)->ContainsPoint(mousePoint)) {
                      break;
                  }
              }
              
      
              // Prints index of selected tile to the console.
              if (whichTile >= 0) {
                  ::qprintf("The user clicked on tile %d.\n", whichTile);
              }
          }
      
          // The model is unlocked at the end of the scope.
          return TRUE;
      }
To request that mouse events be returned in local coordinates, HandleAfterConnectionToViewRoot calls the inherited MMouseEventHandler function SetCoordinateView. The new call is shown in bold below:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      void
      TTilesView::HandleAfterConnectionToViewRoot()
      {
          TDocumentComponentView::HandleAfterConnectionToViewRoot ();
          BuildTilesGraphicsMapToModel();
          SetAllocatedArea(TGArea(TGRect(TGPoint(0,0), TGPoint(200, 100))));
          
          // Takes a references to the local view. TViewHandle is a light-weight reference
          // to a view that can be passed between threads.
          SetCoordinateView(TViewHandle(*this));
      }

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker