Widget Callbacks

One type of events corresponds to changes in the widget state. They are known as widget callbacks and each widget has its own set of them. There is a logical connection between the type of the widgets and their callbacks. For example, the push button widget has a callback, which is activated when it is pressed.

There are few alternative ways of registering callback procedures. First, the callbacks are widget resources whose values are the corresponding procedures, therefore they can be set as such at creation. In addition, the X Toolkit provides a way for callbacks to be set explicitly. This can be done with the predicates xtAddCallback/4 and xtAddCallbacks/3.

In xtAddCallback/4 the first argument is the widget for which the callback is registered, the second is the callback name. The third argument is the name of the procedure to be called and the last argument is some data that the programmer wants to pass to the callback procedure. In xtAddCallbacks/3 the last argument is a list of terms of type callback(Predicate,ClientData) where Predicate is the name of the procedure to be called and ClientData is the data that the programmer wants to pass to the callback procedure. If the callback is set as a widget resource then the resource value is of the same term as used in the xtAddCallbacks/3 list. For example,

        xmCreatePushButton(Shell,push_button,
          [xmNactivateCallback(callback(pressed,'Hello Quintus!'))])
     

would add a callback to the push button at creation time.

The callback procedures must be of arity 3. The first argument is the widget for which the callback was registered. The second argument is reserved for data that the programmer may want pass to the procedure and the last argument is used by the Motif widgets to pass widget specific data to the application.

Following is an extension of the first example showing how to register a widget callback using xtAddCallbacks/3:

     create_button :-
             xtAppInitialize(App,'Demo',[],Shell),
             xmCreatePushButton(Shell,push_button,
                                [xmNwidth(100), xmNheight(100)],Button),
             xtManageChild(Button),
             xtAddCallback(Button,xmNactivateCallback,pressed,
                           'Hello Quintus!'),
             xtRealizeWidget(Shell),
             xtAppMainLoop(App).
     
     pressed(Widget, ClientData, CallData) :-
             write(Widget), nl,
             write(ClientData), nl,
             write(CallData), nl.
     

To remove callbacks use xtRemoveCallback, xtRemoveCallbacks, or xtRemoveAllCallbacks.