Programmer Reference : Common Widgets : Overview of Common Widgets user interface concepts : Example of using an event handler and a callback
Example of using an event handler and a callback
In the following example, a small graphics application interface is created. The widget tree created by the code is illustrated on the right.
The code to create this tree is similar to that for the example on page Example code to create a widget tree, but in this example event and callback handler code (bold text) has been added. This code registers the event and callback handlers just after the widgets are created.
When the push-button widget is pressed (that is, when the user clicks mouse button 1 while the mouse pointer is over the push-button widget), the pressed:clientData:callData: method is run. When the mouse is moved in the drawing area with button 1 held down, the button1Move:clientData:event: method is run.
| shell main form drawArea button |
shell := CwTopLevelShell
createApplicationShell: 'shell'
argBlock: [:w |
w title: 'Graphics Example'].
main := shell
createMainWindow: 'main'
argBlock: nil.
main manageChild.
form := main
createForm: 'form'
argBlock: nil.
form manageChild.
drawArea := form
createDrawingArea: 'drawing'
argBlock: [:w |
w
width: 300;
height: 300;
borderWidth: 1].
"After the drawing area is created, an event handler is added to the widget
The method specifies that the application shall be notified of Button1Motion
events (button 1 is down and the mouse is moving) within the drawing area.
When mouse events occur, the button1Move:clientData:event message is sent
to self (the application)."
drawArea
addEventHandler: Button1MotionMask
receiver:self
selector: #button1Move:clientData:event:
clientData:nil.
drawArea manageChild.
button := form
createPushButton: '1'
argBlock: nil.
"Here a callback is added to the newly created push-button widget.
The method specifies that the application shall be notified of
activate callback (the button is pressed and released). When the
button is activated, the pressed:clientData:callData: message is
sent to self."
button
addCallback:XmNactivateCallback
receiver: self
selector: #pressed:clientData:callData:
clientData:nil.
button manageChild.
drawArea
setValuesBlock: [:w |
w
topAttachment: XmATTACHFORM;
topOffset: 2;
bottomAttachment: XmATTACHFORM;
bottomOffset: 2;
leftAttachment: XmATTACHWIDGET;
leftWidget: button;
leftOffset: 2;
rightAttachment: XmATTACHFORM;
rightOffset: 2].
button
setValuesBlock: [:w |
w
topAttachment: XmATTACHFORM;
topOffset: 2;
leftAttachment: XmATTACHFORM;
leftOffset: 2].
shell realizeWidget.
 

Button pressed
 
 
Last modified date: 04/18/2020