1<?xml version="1.0" encoding="UTF-8" ?>
2<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN" "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
3
4<chapter id='Callbacks'>
5<title>Callbacks</title>
6<para>
7Applications and other widgets often need to register a procedure
8with a widget that gets called under certain prespecified conditions.
9For example,
10when a widget is destroyed,
11every procedure on the widget's <emphasis remap='I'>destroy_callbacks</emphasis>
12list is called to notify clients of the widget's impending doom.
13</para>
14
15<para>
16Every widget has an XtNdestroyCallbacks callback list resource.
17Widgets can define additional callback lists as they see fit.
18For example, the Pushbutton widget has a callback
19list to notify clients when the button has been activated.
20</para>
21
22<para>
23Except where otherwise noted, it is the intent that all Intrinsics
24functions may be called at any time, including from within callback
25procedures, action routines, and event handlers.
26</para>
27<sect1 id="Using_Callback_Procedure_and_Callback_List_Definitions">
28<title>Using Callback Procedure and Callback List Definitions</title>
29<para>
30Callback procedure pointers for use in callback lists are of type
31<xref linkend='XtCallbackProc' xrefstyle='select: title'/>.
32</para>
33
34<funcsynopsis id='XtCallbackProc'>
35<funcprototype>
36<funcdef>typedef void <function>(*XtCallbackProc)</function></funcdef>
37   <paramdef>Widget <parameter>w</parameter></paramdef>
38   <paramdef>XtPointer <parameter>client_data</parameter></paramdef>
39   <paramdef>XtPointer <parameter>call_data</parameter></paramdef>
40</funcprototype>
41</funcsynopsis>
42
43<variablelist>
44  <varlistentry>
45    <term>
46      <emphasis remap='I'>w</emphasis>
47    </term>
48    <listitem>
49      <para>
50Specifies the widget owning the list in which the callback is registered.
51      </para>
52    </listitem>
53  </varlistentry>
54  <varlistentry>
55    <term>
56      <emphasis remap='I'>client_data</emphasis>
57    </term>
58    <listitem>
59      <para>
60Specifies additional data supplied by the client when the procedure
61was registered.
62      </para>
63    </listitem>
64  </varlistentry>
65  <varlistentry>
66    <term>
67      <emphasis remap='I'>call_data</emphasis>
68    </term>
69    <listitem>
70      <para>
71Specifies any callback-specific data the widget wants to pass to the client.
72For example,  when Scrollbar executes its XtNthumbChanged callback list,
73it passes the new position of the thumb.
74    </para>
75  </listitem>
76  </varlistentry>
77</variablelist>
78
79<para>
80The <emphasis remap='I'>client_data</emphasis> argument provides a way for the
81client registering the callback procedure also to register client-specific data,
82for example, a pointer to additional information about the widget,
83a reason for invoking the callback, and so on.
84The <emphasis remap='I'>client_data</emphasis> value may be NULL
85if all necessary information is in the widget.
86The <emphasis remap='I'>call_data</emphasis> argument is a convenience to avoid having simple
87cases where the client could otherwise always call
88<xref linkend='XtGetValues' xrefstyle='select: title'/>
89or a widget-specific function to retrieve data from the widget.
90Widgets should generally avoid putting complex state information
91in <emphasis remap='I'>call_data</emphasis>.
92The client can use the more general data retrieval methods, if necessary.
93</para>
94
95<para>
96Whenever a client wants to pass a callback list as an argument in an
97<xref linkend='XtCreateWidget' xrefstyle='select: title'/>,
98<xref linkend='XtSetValues' xrefstyle='select: title'/>,
99or
100<xref linkend='XtGetValues' xrefstyle='select: title'/>
101call, it should specify the address
102of a NULL-terminated array of type
103<function>XtCallbackList</function>.
104</para>
105<programlisting>
106typedef struct {
107        XtCallbackProc  callback;
108        XtPointer       closure;
109} XtCallbackRec, *XtCallbackList;
110</programlisting>
111<para>
112For example, the callback list for procedures A and B with client data
113clientDataA and clientDataB, respectively, is
114</para>
115<programlisting>
116static XtCallbackRec callbacks[] = {
117        {A, (XtPointer) clientDataA},
118        {B, (XtPointer) clientDataB},
119        {(XtCallbackProc) NULL, (XtPointer) NULL}
120};
121</programlisting>
122<para>
123Although callback lists are passed by address in arglists
124and varargs lists,
125the Intrinsics recognize callback lists
126through the widget resource list and will copy the contents
127when necessary.
128Widget initialize and set_values procedures
129should not allocate memory for the callback list contents.
130The Intrinsics automatically do this,
131potentially using a different structure for their
132internal representation.
133</para>
134</sect1>
135
136<sect1 id="Identifying_Callback_Lists">
137<title>Identifying Callback Lists</title>
138<para>
139Whenever a widget contains a callback list for use by clients,
140it also exports in its public .h file the resource name of the callback list.
141Applications and client widgets never access callback list fields directly.
142Instead, they always identify the desired callback list by using the exported
143resource name.
144All the callback manipulation functions described in this chapter
145except
146<xref linkend='XtCallCallbackList' xrefstyle='select: title'/>
147check
148to see that the requested callback list is indeed implemented by the widget.
149</para>
150
151<para>
152For the Intrinsics to find and correctly handle callback lists,
153they must be declared with a resource type of
154<function>XtRCallback</function>.
155The internal representation of a callback list is
156implementation-dependent; widgets may make no assumptions about the
157value stored in this resource if it is non-NULL.  Except to compare
158the value to NULL (which is equivalent to
159<function>XtCallbackStatus</function>
160<function>XtCallbackHasNone</function>),
161access to callback list resources must be made
162through other Intrinsics procedures.
163</para>
164</sect1>
165
166<sect1 id="Adding_Callback_Procedures">
167<title>Adding Callback Procedures</title>
168<para>
169To add a callback procedure to a widget's callback list, use
170<xref linkend='XtAddCallback' xrefstyle='select: title'/>.
171</para>
172
173<funcsynopsis id='XtAddCallback'>
174<funcprototype>
175<funcdef>void <function>XtAddCallback</function></funcdef>
176   <paramdef>Widget <parameter>w</parameter></paramdef>
177   <paramdef>const char * <parameter>callback_name</parameter></paramdef>
178   <paramdef>XtCallbackProc <parameter>callback</parameter></paramdef>
179   <paramdef>XtPointer <parameter>client_data</parameter></paramdef>
180</funcprototype>
181</funcsynopsis>
182
183<variablelist>
184  <varlistentry>
185    <term>
186      <emphasis remap='I'>w</emphasis>
187    </term>
188    <listitem>
189      <para>
190Specifies the widget.  Must be of class Object or any subclass thereof.
191      </para>
192    </listitem>
193  </varlistentry>
194  <varlistentry>
195    <term>
196      <emphasis remap='I'>callback_name</emphasis>
197    </term>
198    <listitem>
199      <para>
200Specifies the callback list to which the procedure is to be appended.
201      </para>
202    </listitem>
203  </varlistentry>
204  <varlistentry>
205    <term>
206      <emphasis remap='I'>callback</emphasis>
207    </term>
208    <listitem>
209      <para>
210Specifies the callback procedure.
211      </para>
212    </listitem>
213  </varlistentry>
214  <varlistentry>
215    <term>
216      <emphasis remap='I'>client_data</emphasis>
217    </term>
218    <listitem>
219      <para>
220Specifies additional data to be passed to the specified procedure
221when it is invoked,
222or NULL.
223    </para>
224  </listitem>
225  </varlistentry>
226</variablelist>
227
228<para>
229A callback will be invoked as many times as it occurs in the callback list.
230</para>
231
232<para>
233To add a list of callback procedures to a given widget's callback list, use
234<xref linkend='XtAddCallbacks' xrefstyle='select: title'/>.
235</para>
236
237<funcsynopsis id='XtAddCallbacks'>
238<funcprototype>
239<funcdef>void <function>XtAddCallbacks</function></funcdef>
240   <paramdef>Widget <parameter>w</parameter></paramdef>
241   <paramdef>const char * <parameter>callback_name</parameter></paramdef>
242   <paramdef>XtCallbackList <parameter>callbacks</parameter></paramdef>
243</funcprototype>
244</funcsynopsis>
245
246<variablelist>
247  <varlistentry>
248    <term>
249      <emphasis remap='I'>w</emphasis>
250    </term>
251    <listitem>
252      <para>
253Specifies the widget.  Must be of class Object or any subclass thereof.
254      </para>
255    </listitem>
256  </varlistentry>
257  <varlistentry>
258    <term>
259      <emphasis remap='I'>callback_name</emphasis>
260    </term>
261    <listitem>
262      <para>
263Specifies the callback list to which the procedures are to be appended.
264      </para>
265    </listitem>
266  </varlistentry>
267  <varlistentry>
268    <term>
269      <emphasis remap='I'>callbacks</emphasis>
270    </term>
271    <listitem>
272      <para>
273Specifies the null-terminated list of callback procedures and corresponding
274client data.
275    </para>
276  </listitem>
277  </varlistentry>
278</variablelist>
279
280
281</sect1>
282
283<sect1 id="Removing_Callback_Procedures">
284<title>Removing Callback Procedures</title>
285<para>
286To delete a callback procedure from a widget's callback list, use
287<xref linkend='XtRemoveCallback' xrefstyle='select: title'/>.
288</para>
289
290<funcsynopsis id='XtRemoveCallback'>
291<funcprototype>
292<funcdef>void <function>XtRemoveCallback</function></funcdef>
293   <paramdef>Widget <parameter>w</parameter></paramdef>
294   <paramdef>const char * <parameter>callback_name</parameter></paramdef>
295   <paramdef>XtCallbackProc <parameter>callback</parameter></paramdef>
296   <paramdef>XtPointer <parameter>client_data</parameter></paramdef>
297</funcprototype>
298</funcsynopsis>
299
300<variablelist>
301  <varlistentry>
302    <term>
303      <emphasis remap='I'>w</emphasis>
304    </term>
305    <listitem>
306      <para>
307Specifies the widget.  Must be of class Object or any subclass thereof.
308      </para>
309    </listitem>
310  </varlistentry>
311  <varlistentry>
312    <term>
313      <emphasis remap='I'>callback_name</emphasis>
314    </term>
315    <listitem>
316      <para>
317Specifies the callback list from which the procedure is to be deleted.
318      </para>
319    </listitem>
320  </varlistentry>
321  <varlistentry>
322    <term>
323      <emphasis remap='I'>callback</emphasis>
324    </term>
325    <listitem>
326      <para>
327Specifies the callback procedure.
328      </para>
329    </listitem>
330  </varlistentry>
331  <varlistentry>
332    <term>
333      <emphasis remap='I'>client_data</emphasis>
334    </term>
335    <listitem>
336      <para>
337Specifies the client data to match with the registered callback entry.
338    </para>
339  </listitem>
340  </varlistentry>
341</variablelist>
342
343<para>
344The
345<xref linkend='XtRemoveCallback' xrefstyle='select: title'/>
346function removes a callback only if both the procedure and the client
347data match.
348</para>
349
350<para>
351To delete a list of callback procedures from a given widget's callback list, use
352<xref linkend='XtRemoveCallbacks' xrefstyle='select: title'/>.
353</para>
354
355<funcsynopsis id='XtRemoveCallbacks'>
356<funcprototype>
357<funcdef>void <function>XtRemoveCallbacks</function></funcdef>
358   <paramdef>Widget <parameter>w</parameter></paramdef>
359   <paramdef>const char * <parameter>callback_name</parameter></paramdef>
360   <paramdef>XtCallbackList <parameter>callbacks</parameter></paramdef>
361</funcprototype>
362</funcsynopsis>
363
364<variablelist>
365  <varlistentry>
366    <term>
367      <emphasis remap='I'>w</emphasis>
368    </term>
369    <listitem>
370      <para>
371Specifies the widget.  Must be of class Object or any subclass thereof.
372      </para>
373    </listitem>
374  </varlistentry>
375  <varlistentry>
376    <term>
377      <emphasis remap='I'>callback_name</emphasis>
378    </term>
379    <listitem>
380      <para>
381Specifies the callback list from which the procedures are to be deleted.
382      </para>
383    </listitem>
384  </varlistentry>
385  <varlistentry>
386    <term>
387      <emphasis remap='I'>callbacks</emphasis>
388    </term>
389    <listitem>
390      <para>
391Specifies the null-terminated list of callback procedures and corresponding
392client data.
393    </para>
394  </listitem>
395  </varlistentry>
396</variablelist>
397
398<para>
399To delete all callback procedures from a given widget's callback list
400and free all storage associated with the callback list, use
401<xref linkend='XtRemoveAllCallbacks' xrefstyle='select: title'/>.
402</para>
403
404<funcsynopsis id='XtRemoveAllCallbacks'>
405<funcprototype>
406<funcdef>void <function>XtRemoveAllCallbacks</function></funcdef>
407   <paramdef>Widget <parameter>w</parameter></paramdef>
408   <paramdef>const char * <parameter>callback_name</parameter></paramdef>
409</funcprototype>
410</funcsynopsis>
411
412<variablelist>
413  <varlistentry>
414    <term>
415      <emphasis remap='I'>w</emphasis>
416    </term>
417    <listitem>
418      <para>
419Specifies the widget.  Must be of class Object or any subclass thereof.
420      </para>
421    </listitem>
422  </varlistentry>
423  <varlistentry>
424    <term>
425      <emphasis remap='I'>callback_name</emphasis>
426    </term>
427    <listitem>
428      <para>
429Specifies the callback list to be cleared.
430    </para>
431  </listitem>
432  </varlistentry>
433</variablelist>
434
435
436</sect1>
437
438<sect1 id="Executing_Callback_Procedures">
439<title>Executing Callback Procedures</title>
440<para>
441To execute the procedures in a given widget's callback list,
442specifying the callback list by resource name, use
443<xref linkend='XtCallCallbacks' xrefstyle='select: title'/>.
444</para>
445
446<funcsynopsis id='XtCallCallbacks'>
447<funcprototype>
448<funcdef>void <function>XtCallCallbacks</function></funcdef>
449   <paramdef>Widget <parameter>w</parameter></paramdef>
450   <paramdef>const char * <parameter>callback_name</parameter></paramdef>
451   <paramdef>XtPointer <parameter>call_data</parameter></paramdef>
452</funcprototype>
453</funcsynopsis>
454
455<variablelist>
456  <varlistentry>
457    <term>
458      <emphasis remap='I'>w</emphasis>
459    </term>
460    <listitem>
461      <para>
462Specifies the widget.  Must be of class Object or any subclass thereof.
463      </para>
464    </listitem>
465  </varlistentry>
466  <varlistentry>
467    <term>
468      <emphasis remap='I'>callback_name</emphasis>
469    </term>
470    <listitem>
471      <para>
472Specifies the callback list to be executed.
473      </para>
474    </listitem>
475  </varlistentry>
476  <varlistentry>
477    <term>
478      <emphasis remap='I'>call_data</emphasis>
479    </term>
480    <listitem>
481      <para>
482Specifies a callback-list-specific data value to pass to each of the callback
483procedure in the list, or NULL.
484    </para>
485  </listitem>
486  </varlistentry>
487</variablelist>
488
489
490<para>
491<xref linkend='XtCallCallbacks' xrefstyle='select: title'/>
492calls each of the callback procedures in the list
493named by <emphasis remap='I'>callback_name</emphasis> in the specified widget, passing the client
494data registered with the procedure and <emphasis remap='I'>call-data</emphasis>.
495</para>
496
497<para>
498To execute the procedures in a callback list, specifying the callback
499list by address, use
500<xref linkend='XtCallCallbackList' xrefstyle='select: title'/>.
501</para>
502
503<funcsynopsis id='XtCallCallbackList'>
504<funcprototype>
505<funcdef>void <function>XtCallCallbackList</function></funcdef>
506   <paramdef>Widget <parameter>widget</parameter></paramdef>
507   <paramdef>XtCallbackList <parameter>callbacks</parameter></paramdef>
508   <paramdef>XtPointer <parameter>call_data</parameter></paramdef>
509</funcprototype>
510</funcsynopsis>
511
512<variablelist>
513  <varlistentry>
514    <term>
515      <emphasis remap='I'>widget</emphasis>
516    </term>
517    <listitem>
518      <para>
519Specifies the widget instance that contains the callback list.  Must be of class Object or any subclass thereof.
520      </para>
521    </listitem>
522  </varlistentry>
523  <varlistentry>
524    <term>
525      <emphasis remap='I'>callbacks</emphasis>
526    </term>
527    <listitem>
528      <para>
529Specifies the callback list to be executed.
530      </para>
531    </listitem>
532  </varlistentry>
533  <varlistentry>
534    <term>
535      <emphasis remap='I'>call_data</emphasis>
536    </term>
537    <listitem>
538      <para>
539Specifies a callback-list-specific data value to pass
540to each of the callback procedures in the list, or NULL.
541    </para>
542  </listitem>
543  </varlistentry>
544</variablelist>
545
546<para>
547The <emphasis remap='I'>callbacks</emphasis> parameter must specify the contents of a widget or
548object resource declared with representation type
549<function>XtRCallback</function>.
550If <emphasis remap='I'>callbacks</emphasis> is NULL,
551<xref linkend='XtCallCallbackList' xrefstyle='select: title'/>
552returns immediately; otherwise it calls each of the callback
553procedures in the list, passing the client data and <emphasis remap='I'>call_data</emphasis>.
554</para>
555</sect1>
556
557<sect1 id="Checking_the_Status_of_a_Callback_List">
558<title>Checking the Status of a Callback List</title>
559<para>
560To find out the status of a given widget's callback list, use
561<xref linkend='XtHasCallbacks' xrefstyle='select: title'/>.
562</para>
563
564<para>
565typedef enum {XtCallbackNoList, XtCallbackHasNone, XtCallbackHasSome} XtCallbackStatus;
566</para>
567
568<funcsynopsis id='XtHasCallbacks'>
569<funcprototype>
570<funcdef>XtCallbackStatus <function>XtHasCallbacks</function></funcdef>
571   <paramdef>Widget <parameter>w</parameter></paramdef>
572   <paramdef>const char * <parameter>callback_name</parameter></paramdef>
573</funcprototype>
574</funcsynopsis>
575
576<variablelist>
577  <varlistentry>
578    <term>
579      <emphasis remap='I'>w</emphasis>
580    </term>
581    <listitem>
582      <para>
583Specifies the widget.  Must be of class Object or any subclass thereof.
584      </para>
585    </listitem>
586  </varlistentry>
587  <varlistentry>
588    <term>
589      <emphasis remap='I'>callback_name</emphasis>
590    </term>
591    <listitem>
592      <para>
593Specifies the callback list to be checked.
594    </para>
595  </listitem>
596  </varlistentry>
597</variablelist>
598
599<para>
600The
601<xref linkend='XtHasCallbacks' xrefstyle='select: title'/>
602function first checks to see if the widget has a callback list identified
603by <emphasis remap='I'>callback_name</emphasis>.
604If the callback list does not exist,
605<xref linkend='XtHasCallbacks' xrefstyle='select: title'/>
606returns
607<function>XtCallbackNoList</function>.
608If the callback list exists but is empty,
609it returns
610<function>XtCallbackHasNone</function>.
611If the callback list exists and has at least one callback registered,
612it returns
613<function>XtCallbackHasSome</function>.
614</para>
615</sect1>
616</chapter>
617