./TilesTutorial/02.MouseEvents/Tiles/TilesModel.h:
bool ContainsPoint(const TGPoint&) const;
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);
}
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.
}
./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;
}
// 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));
}