Mixing in MMouseEventHandler

You add mouse-event handling capability to a view simply by mixing in the class MMouseEventHandler. MMouseEventHandler provides functions for dealing with different types of mouse events--each function has a default implementation that does nothing. Views that derive from MMouseEventHandler override only the functions of interest. TTilesView implements only the MouseDown function.


This diagram shows TTilesView and the functions it inherits from MMouseEventHandler:


The framework calls the MouseDown function whenever a mouse-down event occurs within the area of a TTilesView. The function takes a reference to a TMouseDownEvent, which it uses to determine the location of the mouse down.

The TTilesView::MouseDown function identifies which, if any, tile within the model was clicked on. To do this, the MouseDown function needs to access the data in the model, so it locks the model before accessing the data and releases the model when it is finished. Like the DrawContents function, the MouseDown function can use a TModelPointerTo<AModel> to accomplish this easily.

At this stage in developing the Tiles program, the MouseDown function will only identify whether a specific tile was clicked on. In Step 3 the function will be extended to select the clicked-on tile and issue a command accordingly. To handle the mouse click, the MouseDown function:

  1. Gets the position of the mouse click (as a TGPoint) from the TMouseDownEvent.
  2. Creates a smart pointer to handle locking and releasing the model.
  3. Determines whether any tile in the model contains the point at which the mouse was clicked. To make it easier to determine whether a tile contains a particular point, the TTile class has an additional function, ContainsPoint, that takes a TGPoint and returns a Boolean value indicating whether the tile contains that point.
You can compile and run the Tiles project in the tutorial directory
$TaligentRoot/TaligentSamples/Supported/Apps/TilesTutorial/02.MouseEvents/Tiles and launch a Tiles document using TilesTutorialApp to see the mouse event handling. Make sure that a previous version of a Tiles document is not running when you compile this version. When you click on a tile, the index of that tile is displayed on your AIX console. See "Launching a document" on page 21 for directions on how to compile a project and launch a document.


[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