Programmer's Reference

Adding an event handler to a drawing area

In the following example, a button press event handler is used to detect double-clicks in a drawing area. The open method creates the widgets, and the buttonPress:clientData:event: method handles button press events.

Object subclass: #DoubleClick
  instanceVariableNames: 'clickStartTime '
  classVariableNames: ''
  poolDictionaries: 'CgConstants CwConstants '
 
open
"Create a drawing area inside a shell."
   | shell drawingArea |
 
   clickStartTime := 0.
   shell := CwTopLevelShell
      createApplicationShell: 'shell'
      argBlock: [:w | w title: 'Double-click test'].
 
   (drawingArea := shell
      createDrawingArea: 'draw'
      argBlock: [:w |
         w
            width: 100;
            height: 100])
      manageChild.
 
   drawingArea
      addEventHandler: ButtonPressMask
      receiver: self
      selector: #buttonPress:clientData:event:
      clientData: nil.
   shell realizeWidget.
 
buttonPress: widget clientData: clientData event: event
"Detect double click by checking whether the time between successive
 presses of the left mouse button is less than the system-defined
 double-click time."
   event button = 1
      ifTrue: [
         event time - clickStartTime < widget display doubleClickInterval
            ifTrue: [
               clickStartTime := 0.
               Transcript cr; show: 'DOUBLE CLICK' ]
            ifFalse: [
               clickStartTime := event time ]].
Tip:
Adding a mouse down event handler to a widget that processes mouse events internally, such as a CwPushButton, might result in unpredictable behaviour. To detect double-clicks in a CwList, use the defaultAction callback.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]