Next Previous Up Top Contents Index

7.2 A tour of gadgets

7.2.7 Assigning callbacks to gadgets

To make gadgets actually do something, you have to assign them callback functions. A callback is a function that is invoked when a particular event occurs on a gadget, such as pressing a button. When the user presses a button, the appropriate callback method is invoked and some behavior, defined by you, occurs. It is the main way of providing your applications with some kind of interactive functionality. Most classes of gadget have a number of different callbacks available. Like gadget properties, different classes of gadget can have different callback types available.

The most common type of callback is the activate callback. This is the callback that is invoked whenever a general instance <action-gadget> is activated: for instance, if a push button is clicked. All the gadget classes you have seen so far are general instances of <action-gadget>.

The following code creates a push button that has an activate callback defined:

make(<push-button>, 
      label: "Hello", 
      activate-callback: method (button) 
                           notify-user("Pressed button!",
                                       owner: button) 
                         end)));

The notify-user function is a useful function that lets you display a message in a dialog.

Now when you click on the button, a notification pops up saying "Pressed button!"

Figure 7.23 Simple behavior of notify-user

Two callbacks are unique to general instances of <value-gadget>: the value-changing and the value-changed callbacks. The value-changing callback is invoked as the gadget value of the gadget changes, and the value-changed callback is invoked when the value has changed, and is passed back to the gadget.

In practice, a value-changing callback is of most use in a gadget whose value you need to monitor constantly, such as a <value-range-gadget>. A value-changed callback is of most use when the user enters a value explicitly and returns it to the application, for instance by clicking on a button or pressing RETURN.

In a text field, for example, a value-changing callback would be invoked whenever a character is typed in the text field, whereas a value-changed callback would be invoked once the user had finished typing and had returned the value to the gadget. For a text field, the value-changed callback is usually more useful than the value-changing callback.

contain(make(<text-field>, 
             value-changed-callback: 
                method (gadget) 
                notify-user
                   ("Changed to %=", gadget-value(gadget))
                end));


Building Applications Using DUIM - 26 May 1999

Next Previous Up Top Contents Index