Views

1. Mouse tracking

1.1. Tracking the Mouse inside Frames

To track the mouse inside any frame, use the #trackMouseUntil: method. This method takes a block argument which it repeatedly evaluates for every occurring mouse event until the block returns false. The block takes a MacMouseTrackingEvent instance as argument and must return a boolean.

The following example tracks a single sequence of mouse down, drag, and up events in a window. Tracking stops when the first mouse up event is encountered, and all events are logged to the Transcript:

| window |
(window := MacWindowFrame new)
    position: 100@100;
    extent: 400@300;
    text: 'Mouse tracking test';
    open.

window trackMouseUntil: [:event |
    event isMouseDown ifTrue: [
        Transcript cr; 
            nextPutAll: 'mouse down at ';
            print: event position].
    event isMouseDragged ifTrue: [
        Transcript cr; 
            nextPutAll: 'mouse ';
            nextPutAll: (
                event isCommandDown 
                    ifTrue: ['command-'] 
                    ifFalse: ['']);
            nextPutAll: 'dragged to ';
            print: event position].
    event isMouseUp ifTrue: [
        Transcript cr; 
            nextPutAll: 'mouse up at ';
            print: event position].
    event isMouseUp]

Note that tracking is always done relative to the frame, even though you can press, drag, or release the mouse outside the frame. The position is reported in frame coordinates.

1.2. Tracking the Mouse inside Controls

To track the mouse inside any control, use the #trackMouseUntil: method. This method takes a block argument which it repeatedly evaluates for every occurring mouse event until the block returns false. The block takes a MacMouseTrackingEvent instance as argument and must return a boolean.

The following example tracks sequences of mouse down, drag, and up events in a drawing pane. Tracking is initiated by a mouse down event in the drawing pane and stops when the first mouse up event is encountered. All events are logged to the Transcript:

| control window |
(control := MacDrawingPane new)
    position: 10@10;
    extent: 100@100;
    when: #clicked evaluate: [:event | 
        Transcript cr;
            nextPutAll: 'mouse down at ';
            print: event position.
        control trackMouseUntil: [:event |
            event isMouseDragged ifTrue: [
                Transcript cr; 
                    nextPutAll: 'mouse ';
                    nextPutAll: (
                        event isCommandDown 
                            ifTrue: ['command-'] 
                            ifFalse: ['']);
                    nextPutAll: 'dragged to ';
                    print: event position].
            event isMouseUp ifTrue: [
                Transcript cr; 
                    nextPutAll: 'mouse up at ';
                    print: event position].
            event isMouseUp]].

(window := MacWindowFrame new)
    position: 100@100;
    extent: 400@300;
    text: 'Mouse tracking test';
    addControl: control;
    open.

Note that tracking is always done relative to the control, even though you can drag or release the mouse outside the control or the frame. The position is reported in control coordinates.

Last modified May 25, 2006.