8.2 Overall Mechanism

In A brief introduction to Inspectors, we explained how to set properties for an individual morph instance to handle a specific event. In this case, one property informed Cuis-Smalltalk we were interested in a given event (#handlesMouseDown), a second property defined the behavior with a block of code to be executed each time this event occurred.

Alternatively, to handle events in a given morph class, we define the behavior with instance methods. In the Morph class, observe the method categories event and event handling testing.

The method category event handling testing lists methods returning a Boolean to indicate if the instance should be notified about certain events. Let’s take a look at one of these methods, handlesMouseDown:, its comment is worth reading:

Morph>>handlesMouseDown: aMouseButtonEvent
"Do I want to receive mouseButton messages ?
- #mouseButton1Down:localPosition:
- #mouseButton1Up:localPosition:
- #mouseButton2Down:localPosition:
- #mouseButton2Up:localPosition:
- #mouseButton3Down:localPosition:
- #mouseButton3Up:localPosition:
- #mouseMove:localPosition:
- #mouseButton2Activity
NOTE: The default response is false. Subclasses that implement these
messages directly should override this one to return true.
Implementors could query the argument, and only answer true for (for
example) button 2 up only."
"Use a property test to allow individual instances to dynamically
specify this."

   ^ self hasProperty: #'handlesMouseDown:'

As defined by default, this method and the other handlers check to see if an instance has defined a property with the same name as the standard method. So each individual instance can add its own behavior.

In a morph class where we want all instances to handle mouse down events, we just override the appropriate method to return true:

MyMorph>>>>handlesMouseDown: aMouseButtonEvent
   ^ true

Now in the events method category for class Morph, we find the handlers listed in the comment above. A ScrollBar, a kind of Morph to represent a list’s position control, scrolls its list contents when a mouse button 1 is pressed:

ScrollBar>>mouseButton1Down: aMouseBtnEvent position: eventPosition
"Update visual feedback"
   self setNextDirectionFromEvent: aMouseBtnEvent.
   self scrollByPage

To discover other events available for your morph, explore with the System Browser as described above.