Callback.c revision a3bd7f05
1444c061aSmrg/***********************************************************
2249c3046SmrgCopyright (c) 1993, Oracle and/or its affiliates. All rights reserved.
31477040fSmrg
41477040fSmrgPermission is hereby granted, free of charge, to any person obtaining a
51477040fSmrgcopy of this software and associated documentation files (the "Software"),
61477040fSmrgto deal in the Software without restriction, including without limitation
71477040fSmrgthe rights to use, copy, modify, merge, publish, distribute, sublicense,
81477040fSmrgand/or sell copies of the Software, and to permit persons to whom the
91477040fSmrgSoftware is furnished to do so, subject to the following conditions:
101477040fSmrg
111477040fSmrgThe above copyright notice and this permission notice (including the next
121477040fSmrgparagraph) shall be included in all copies or substantial portions of the
131477040fSmrgSoftware.
141477040fSmrg
151477040fSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161477040fSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171477040fSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181477040fSmrgTHE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191477040fSmrgLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201477040fSmrgFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
211477040fSmrgDEALINGS IN THE SOFTWARE.
221477040fSmrg
231477040fSmrgCopyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
24444c061aSmrg
25444c061aSmrg                        All Rights Reserved
26444c061aSmrg
27444c061aSmrgPermission to use, copy, modify, and distribute this software and its
28444c061aSmrgdocumentation for any purpose and without fee is hereby granted,
29444c061aSmrgprovided that the above copyright notice appear in all copies and that
30444c061aSmrgboth that copyright notice and this permission notice appear in
311477040fSmrgsupporting documentation, and that the name of Digital not be
32444c061aSmrgused in advertising or publicity pertaining to distribution of the
33444c061aSmrgsoftware without specific, written prior permission.
34444c061aSmrg
35444c061aSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
36444c061aSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
37444c061aSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
38444c061aSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
39444c061aSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
40444c061aSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
41444c061aSmrgSOFTWARE.
42444c061aSmrg
43444c061aSmrg******************************************************************/
44444c061aSmrg
45444c061aSmrg/*
46444c061aSmrg
47444c061aSmrgCopyright 1987, 1988, 1998  The Open Group
48444c061aSmrg
49444c061aSmrgPermission to use, copy, modify, distribute, and sell this software and its
50444c061aSmrgdocumentation for any purpose is hereby granted without fee, provided that
51444c061aSmrgthe above copyright notice appear in all copies and that both that
52444c061aSmrgcopyright notice and this permission notice appear in supporting
53444c061aSmrgdocumentation.
54444c061aSmrg
55444c061aSmrgThe above copyright notice and this permission notice shall be included in
56444c061aSmrgall copies or substantial portions of the Software.
57444c061aSmrg
58444c061aSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59444c061aSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60444c061aSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
61444c061aSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
62444c061aSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
63444c061aSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
64444c061aSmrg
65444c061aSmrgExcept as contained in this notice, the name of The Open Group shall not be
66444c061aSmrgused in advertising or otherwise to promote the sale, use or other dealings
67444c061aSmrgin this Software without prior written authorization from The Open Group.
68444c061aSmrg
69444c061aSmrg*/
70444c061aSmrg
71444c061aSmrg#ifdef HAVE_CONFIG_H
72444c061aSmrg#include <config.h>
73444c061aSmrg#endif
74444c061aSmrg#include "IntrinsicI.h"
75444c061aSmrg
760568f49bSmrgstatic _Xconst _XtString XtNinvalidCallbackList = "invalidCallbackList";
770568f49bSmrgstatic _Xconst _XtString XtNxtAddCallback = "xtAddCallback";
780568f49bSmrgstatic _Xconst _XtString XtNxtRemoveCallback = "xtRemoveCallback";
790568f49bSmrgstatic _Xconst _XtString XtNxtRemoveAllCallback = "xtRemoveAllCallback";
800568f49bSmrgstatic _Xconst _XtString XtNxtCallCallback = "xtCallCallback";
81444c061aSmrg
82444c061aSmrg/* However it doesn't contain a final NULL record */
83444c061aSmrg#define ToList(p) ((XtCallbackList) ((p)+1))
84444c061aSmrg
85a3bd7f05Smrgstatic InternalCallbackList *
86a3bd7f05SmrgFetchInternalList(Widget widget,
87a3bd7f05Smrg                  _Xconst char *name)
88444c061aSmrg{
89444c061aSmrg    XrmQuark quark;
90444c061aSmrg    int n;
91444c061aSmrg    CallbackTable offsets;
92a3bd7f05Smrg    InternalCallbackList *retval = NULL;
93444c061aSmrg
94444c061aSmrg    quark = StringToQuark(name);
95444c061aSmrg    LOCK_PROCESS;
96444c061aSmrg    offsets = (CallbackTable)
97a3bd7f05Smrg        widget->core.widget_class->core_class.callback_private;
98a3bd7f05Smrg
99a3bd7f05Smrg    for (n = (int) (long) *(offsets++); --n >= 0; offsets++)
100a3bd7f05Smrg        if (quark == (*offsets)->xrm_name) {
101a3bd7f05Smrg            retval = (InternalCallbackList *)
102a3bd7f05Smrg                ((char *) widget - (*offsets)->xrm_offset - 1);
103a3bd7f05Smrg            break;
104a3bd7f05Smrg        }
105444c061aSmrg    UNLOCK_PROCESS;
106444c061aSmrg    return retval;
107444c061aSmrg}
108444c061aSmrg
109a3bd7f05Smrgvoid
110a3bd7f05Smrg_XtAddCallback(InternalCallbackList *callbacks,
111a3bd7f05Smrg               XtCallbackProc callback,
112a3bd7f05Smrg               XtPointer closure)
113444c061aSmrg{
114444c061aSmrg    register InternalCallbackList icl;
115444c061aSmrg    register XtCallbackList cl;
116444c061aSmrg    register int count;
117444c061aSmrg
118444c061aSmrg    icl = *callbacks;
119444c061aSmrg    count = icl ? icl->count : 0;
120444c061aSmrg
121444c061aSmrg    if (icl && icl->call_state) {
122a3bd7f05Smrg        icl->call_state |= _XtCBFreeAfterCalling;
123a3bd7f05Smrg        icl = (InternalCallbackList)
124a3bd7f05Smrg            __XtMalloc((Cardinal) (sizeof(InternalCallbackRec) +
125a3bd7f05Smrg                                   sizeof(XtCallbackRec) * (size_t) (count +
126a3bd7f05Smrg                                                                     1)));
127a3bd7f05Smrg        (void) memmove((char *) ToList(icl), (char *) ToList(*callbacks),
128a3bd7f05Smrg                       sizeof(XtCallbackRec) * (size_t) count);
129a3bd7f05Smrg    }
130a3bd7f05Smrg    else {
131a3bd7f05Smrg        icl = (InternalCallbackList)
132a3bd7f05Smrg            XtRealloc((char *) icl, (Cardinal) (sizeof(InternalCallbackRec) +
133a3bd7f05Smrg                                                sizeof(XtCallbackRec) *
134a3bd7f05Smrg                                                (size_t) (count + 1)));
135444c061aSmrg    }
136444c061aSmrg    *callbacks = icl;
1370568f49bSmrg    icl->count = (unsigned short) (count + 1);
138444c061aSmrg    icl->is_padded = 0;
139444c061aSmrg    icl->call_state = 0;
140444c061aSmrg    cl = ToList(icl) + count;
141444c061aSmrg    cl->callback = callback;
142444c061aSmrg    cl->closure = closure;
143a3bd7f05Smrg}                               /* _XtAddCallback */
144444c061aSmrg
145a3bd7f05Smrgvoid
146a3bd7f05Smrg_XtAddCallbackOnce(register InternalCallbackList *callbacks,
147a3bd7f05Smrg                   XtCallbackProc callback,
148a3bd7f05Smrg                   XtPointer closure)
149444c061aSmrg{
150444c061aSmrg    register XtCallbackList cl = ToList(*callbacks);
151444c061aSmrg    register int i;
152444c061aSmrg
153a3bd7f05Smrg    for (i = (*callbacks)->count; --i >= 0; cl++)
154a3bd7f05Smrg        if (cl->callback == callback && cl->closure == closure)
155a3bd7f05Smrg            return;
156444c061aSmrg
157444c061aSmrg    _XtAddCallback(callbacks, callback, closure);
158a3bd7f05Smrg}                               /* _XtAddCallbackOnce */
159a3bd7f05Smrg
160a3bd7f05Smrgvoid
161a3bd7f05SmrgXtAddCallback(Widget widget,
162a3bd7f05Smrg              _Xconst char *name,
163a3bd7f05Smrg              XtCallbackProc callback,
164a3bd7f05Smrg              XtPointer closure)
165444c061aSmrg{
166444c061aSmrg    InternalCallbackList *callbacks;
167444c061aSmrg    XtAppContext app = XtWidgetToApplicationContext(widget);
168444c061aSmrg
169444c061aSmrg    LOCK_APP(app);
170444c061aSmrg    callbacks = FetchInternalList(widget, name);
171444c061aSmrg    if (!callbacks) {
172a3bd7f05Smrg        XtAppWarningMsg(app,
173a3bd7f05Smrg                        XtNinvalidCallbackList, XtNxtAddCallback,
174a3bd7f05Smrg                        XtCXtToolkitError,
175a3bd7f05Smrg                        "Cannot find callback list in XtAddCallback", NULL,
176a3bd7f05Smrg                        NULL);
177a3bd7f05Smrg        UNLOCK_APP(app);
178a3bd7f05Smrg        return;
179444c061aSmrg    }
180444c061aSmrg    _XtAddCallback(callbacks, callback, closure);
181444c061aSmrg    if (!_XtIsHookObject(widget)) {
1820568f49bSmrg        Widget hookobj = XtHooksOfDisplay(XtDisplayOfObject(widget));
183444c061aSmrg
184a3bd7f05Smrg        if (XtHasCallbacks(hookobj, XtNchangeHook) == XtCallbackHasSome) {
185a3bd7f05Smrg            XtChangeHookDataRec call_data;
186a3bd7f05Smrg
187a3bd7f05Smrg            call_data.type = XtHaddCallback;
188a3bd7f05Smrg            call_data.widget = widget;
189a3bd7f05Smrg            call_data.event_data = (XtPointer) name;
190a3bd7f05Smrg            XtCallCallbackList(hookobj,
191a3bd7f05Smrg                               ((HookObject) hookobj)->hooks.
192a3bd7f05Smrg                               changehook_callbacks, (XtPointer) &call_data);
193a3bd7f05Smrg        }
194444c061aSmrg    }
195444c061aSmrg    UNLOCK_APP(app);
196a3bd7f05Smrg}                               /* XtAddCallback */
197444c061aSmrg
198a3bd7f05Smrgstatic void
199a3bd7f05SmrgAddCallbacks(Widget widget _X_UNUSED,
200a3bd7f05Smrg             InternalCallbackList *callbacks,
201a3bd7f05Smrg             XtCallbackList newcallbacks)
202444c061aSmrg{
203444c061aSmrg    register InternalCallbackList icl;
204444c061aSmrg    register int i, j;
205444c061aSmrg    register XtCallbackList cl;
206444c061aSmrg
207444c061aSmrg    icl = *callbacks;
208444c061aSmrg    i = icl ? icl->count : 0;
209a3bd7f05Smrg    for (j = 0, cl = newcallbacks; cl->callback; cl++, j++);
210444c061aSmrg    if (icl && icl->call_state) {
211a3bd7f05Smrg        icl->call_state |= _XtCBFreeAfterCalling;
212a3bd7f05Smrg        icl = (InternalCallbackList)
213a3bd7f05Smrg            __XtMalloc((Cardinal)
214a3bd7f05Smrg                       (sizeof(InternalCallbackRec) +
215a3bd7f05Smrg                        sizeof(XtCallbackRec) * (size_t) (i + j)));
216a3bd7f05Smrg        (void) memmove((char *) ToList(*callbacks), (char *) ToList(icl),
217a3bd7f05Smrg                       sizeof(XtCallbackRec) * (size_t) i);
218a3bd7f05Smrg    }
219a3bd7f05Smrg    else {
220a3bd7f05Smrg        icl = (InternalCallbackList) XtRealloc((char *) icl,
221a3bd7f05Smrg                                               (Cardinal) (sizeof
222a3bd7f05Smrg                                                           (InternalCallbackRec)
223a3bd7f05Smrg                                                           +
224a3bd7f05Smrg                                                           sizeof(XtCallbackRec)
225a3bd7f05Smrg                                                           * (size_t) (i + j)));
226444c061aSmrg    }
227444c061aSmrg    *callbacks = icl;
228a3bd7f05Smrg    icl->count = (unsigned short) (i + j);
229444c061aSmrg    icl->is_padded = 0;
230444c061aSmrg    icl->call_state = 0;
231a3bd7f05Smrg    for (cl = ToList(icl) + i; --j >= 0;)
232a3bd7f05Smrg        *cl++ = *newcallbacks++;
233a3bd7f05Smrg}                               /* AddCallbacks */
234a3bd7f05Smrg
235a3bd7f05Smrgvoid
236a3bd7f05SmrgXtAddCallbacks(Widget widget,
237a3bd7f05Smrg               _Xconst char *name,
238a3bd7f05Smrg               XtCallbackList xtcallbacks)
239444c061aSmrg{
240a3bd7f05Smrg    InternalCallbackList *callbacks;
241444c061aSmrg    Widget hookobj;
242444c061aSmrg    XtAppContext app = XtWidgetToApplicationContext(widget);
243444c061aSmrg
244444c061aSmrg    LOCK_APP(app);
245444c061aSmrg    callbacks = FetchInternalList(widget, name);
246444c061aSmrg    if (!callbacks) {
247a3bd7f05Smrg        XtAppWarningMsg(app,
248a3bd7f05Smrg                        XtNinvalidCallbackList, XtNxtAddCallback,
249a3bd7f05Smrg                        XtCXtToolkitError,
250a3bd7f05Smrg                        "Cannot find callback list in XtAddCallbacks", NULL,
251a3bd7f05Smrg                        NULL);
252a3bd7f05Smrg        UNLOCK_APP(app);
253a3bd7f05Smrg        return;
254444c061aSmrg    }
255444c061aSmrg    AddCallbacks(widget, callbacks, xtcallbacks);
256444c061aSmrg    hookobj = XtHooksOfDisplay(XtDisplayOfObject(widget));
257444c061aSmrg    if (XtHasCallbacks(hookobj, XtNchangeHook) == XtCallbackHasSome) {
258a3bd7f05Smrg        XtChangeHookDataRec call_data;
259a3bd7f05Smrg
260a3bd7f05Smrg        call_data.type = XtHaddCallbacks;
261a3bd7f05Smrg        call_data.widget = widget;
262a3bd7f05Smrg        call_data.event_data = (XtPointer) name;
263a3bd7f05Smrg        XtCallCallbackList(hookobj,
264a3bd7f05Smrg                           ((HookObject) hookobj)->hooks.changehook_callbacks,
265a3bd7f05Smrg                           (XtPointer) &call_data);
266444c061aSmrg    }
267444c061aSmrg    UNLOCK_APP(app);
268a3bd7f05Smrg}                               /* XtAddCallbacks */
269444c061aSmrg
270a3bd7f05Smrgvoid
271a3bd7f05Smrg_XtRemoveCallback(InternalCallbackList *callbacks,
272a3bd7f05Smrg                  XtCallbackProc callback,
273a3bd7f05Smrg                  XtPointer closure)
274444c061aSmrg{
275444c061aSmrg    register InternalCallbackList icl;
276444c061aSmrg    register int i, j;
277444c061aSmrg    register XtCallbackList cl, ncl, ocl;
278444c061aSmrg
279444c061aSmrg    icl = *callbacks;
280a3bd7f05Smrg    if (!icl)
281a3bd7f05Smrg        return;
282444c061aSmrg
283444c061aSmrg    cl = ToList(icl);
284a3bd7f05Smrg    for (i = icl->count; --i >= 0; cl++) {
285a3bd7f05Smrg        if (cl->callback == callback && cl->closure == closure) {
286a3bd7f05Smrg            if (icl->call_state) {
287a3bd7f05Smrg                icl->call_state |= _XtCBFreeAfterCalling;
288a3bd7f05Smrg                if (icl->count == 1) {
289a3bd7f05Smrg                    *callbacks = NULL;
290a3bd7f05Smrg                }
291a3bd7f05Smrg                else {
292a3bd7f05Smrg                    j = icl->count - i - 1;
293a3bd7f05Smrg                    ocl = ToList(icl);
294a3bd7f05Smrg                    icl = (InternalCallbackList)
295a3bd7f05Smrg                        __XtMalloc((Cardinal) (sizeof(InternalCallbackRec) +
296a3bd7f05Smrg                                               sizeof(XtCallbackRec) *
297a3bd7f05Smrg                                               (size_t) (i + j)));
298a3bd7f05Smrg                    icl->count = (unsigned short) (i + j);
299a3bd7f05Smrg                    icl->is_padded = 0;
300a3bd7f05Smrg                    icl->call_state = 0;
301a3bd7f05Smrg                    ncl = ToList(icl);
302a3bd7f05Smrg                    while (--j >= 0)
303a3bd7f05Smrg                        *ncl++ = *ocl++;
304a3bd7f05Smrg                    while (--i >= 0)
305a3bd7f05Smrg                        *ncl++ = *++cl;
306a3bd7f05Smrg                    *callbacks = icl;
307a3bd7f05Smrg                }
308a3bd7f05Smrg            }
309a3bd7f05Smrg            else {
310a3bd7f05Smrg                if (--icl->count) {
311a3bd7f05Smrg                    ncl = cl + 1;
312a3bd7f05Smrg                    while (--i >= 0)
313a3bd7f05Smrg                        *cl++ = *ncl++;
314a3bd7f05Smrg                    icl = (InternalCallbackList)
315a3bd7f05Smrg                        XtRealloc((char *) icl,
316a3bd7f05Smrg                                  (Cardinal) (sizeof(InternalCallbackRec)
317a3bd7f05Smrg                                              +
318a3bd7f05Smrg                                              sizeof(XtCallbackRec) *
319a3bd7f05Smrg                                              icl->count));
320a3bd7f05Smrg                    icl->is_padded = 0;
321a3bd7f05Smrg                    *callbacks = icl;
322a3bd7f05Smrg                }
323a3bd7f05Smrg                else {
324a3bd7f05Smrg                    XtFree((char *) icl);
325a3bd7f05Smrg                    *callbacks = NULL;
326a3bd7f05Smrg                }
327a3bd7f05Smrg            }
328a3bd7f05Smrg            return;
329a3bd7f05Smrg        }
330a3bd7f05Smrg    }
331a3bd7f05Smrg}                               /* _XtRemoveCallback */
332a3bd7f05Smrg
333a3bd7f05Smrgvoid
334a3bd7f05SmrgXtRemoveCallback(Widget widget,
335a3bd7f05Smrg                 _Xconst char *name,
336a3bd7f05Smrg                 XtCallbackProc callback,
337a3bd7f05Smrg                 XtPointer closure)
338444c061aSmrg{
339444c061aSmrg    InternalCallbackList *callbacks;
340444c061aSmrg    Widget hookobj;
341444c061aSmrg    XtAppContext app = XtWidgetToApplicationContext(widget);
342444c061aSmrg
343444c061aSmrg    LOCK_APP(app);
344444c061aSmrg    callbacks = FetchInternalList(widget, name);
345444c061aSmrg    if (!callbacks) {
346a3bd7f05Smrg        XtAppWarningMsg(app,
347a3bd7f05Smrg                        XtNinvalidCallbackList, XtNxtRemoveCallback,
348a3bd7f05Smrg                        XtCXtToolkitError,
349a3bd7f05Smrg                        "Cannot find callback list in XtRemoveCallback", NULL,
350a3bd7f05Smrg                        NULL);
351a3bd7f05Smrg        UNLOCK_APP(app);
352a3bd7f05Smrg        return;
353444c061aSmrg    }
354444c061aSmrg    _XtRemoveCallback(callbacks, callback, closure);
355444c061aSmrg    hookobj = XtHooksOfDisplay(XtDisplayOfObject(widget));
356444c061aSmrg    if (XtHasCallbacks(hookobj, XtNchangeHook) == XtCallbackHasSome) {
357a3bd7f05Smrg        XtChangeHookDataRec call_data;
358a3bd7f05Smrg
359a3bd7f05Smrg        call_data.type = XtHremoveCallback;
360a3bd7f05Smrg        call_data.widget = widget;
361a3bd7f05Smrg        call_data.event_data = (XtPointer) name;
362a3bd7f05Smrg        XtCallCallbackList(hookobj,
363a3bd7f05Smrg                           ((HookObject) hookobj)->hooks.changehook_callbacks,
364a3bd7f05Smrg                           (XtPointer) &call_data);
365444c061aSmrg    }
366444c061aSmrg    UNLOCK_APP(app);
367a3bd7f05Smrg}                               /* XtRemoveCallback */
368444c061aSmrg
369a3bd7f05Smrgvoid
370a3bd7f05SmrgXtRemoveCallbacks(Widget widget,
371a3bd7f05Smrg                  _Xconst char *name,
372a3bd7f05Smrg                  XtCallbackList xtcallbacks)
373444c061aSmrg{
374444c061aSmrg    InternalCallbackList *callbacks;
375444c061aSmrg    Widget hookobj;
376444c061aSmrg    int i;
377444c061aSmrg    InternalCallbackList icl;
378444c061aSmrg    XtCallbackList cl, ccl, rcl;
379444c061aSmrg    XtAppContext app = XtWidgetToApplicationContext(widget);
380444c061aSmrg
381444c061aSmrg    LOCK_APP(app);
382444c061aSmrg    callbacks = FetchInternalList(widget, name);
383444c061aSmrg    if (!callbacks) {
384a3bd7f05Smrg        XtAppWarningMsg(app,
385a3bd7f05Smrg                        XtNinvalidCallbackList, XtNxtRemoveCallback,
386a3bd7f05Smrg                        XtCXtToolkitError,
387a3bd7f05Smrg                        "Cannot find callback list in XtRemoveCallbacks", NULL,
388a3bd7f05Smrg                        NULL);
389a3bd7f05Smrg        UNLOCK_APP(app);
390a3bd7f05Smrg        return;
391444c061aSmrg    }
392444c061aSmrg
393444c061aSmrg    icl = *callbacks;
394444c061aSmrg    if (!icl) {
395a3bd7f05Smrg        UNLOCK_APP(app);
396a3bd7f05Smrg        return;
397444c061aSmrg    }
398444c061aSmrg
399444c061aSmrg    i = icl->count;
400444c061aSmrg    cl = ToList(icl);
401444c061aSmrg    if (icl->call_state) {
402a3bd7f05Smrg        icl->call_state |= _XtCBFreeAfterCalling;
403a3bd7f05Smrg        icl =
404a3bd7f05Smrg            (InternalCallbackList)
405a3bd7f05Smrg            __XtMalloc((Cardinal)
406a3bd7f05Smrg                       (sizeof(InternalCallbackRec) +
407a3bd7f05Smrg                        sizeof(XtCallbackRec) * (size_t) i));
408a3bd7f05Smrg        icl->count = (unsigned short) i;
409a3bd7f05Smrg        icl->call_state = 0;
410444c061aSmrg    }
411444c061aSmrg    ccl = ToList(icl);
412444c061aSmrg    while (--i >= 0) {
413a3bd7f05Smrg        *ccl++ = *cl;
414a3bd7f05Smrg        for (rcl = xtcallbacks; rcl->callback; rcl++) {
415a3bd7f05Smrg            if (cl->callback == rcl->callback && cl->closure == rcl->closure) {
416a3bd7f05Smrg                ccl--;
417a3bd7f05Smrg                icl->count--;
418a3bd7f05Smrg                break;
419a3bd7f05Smrg            }
420a3bd7f05Smrg        }
421a3bd7f05Smrg        cl++;
422444c061aSmrg    }
423444c061aSmrg    if (icl->count) {
424a3bd7f05Smrg        icl = (InternalCallbackList)
425a3bd7f05Smrg            XtRealloc((char *) icl, (Cardinal) (sizeof(InternalCallbackRec) +
426a3bd7f05Smrg                                                sizeof(XtCallbackRec) *
427a3bd7f05Smrg                                                icl->count));
428a3bd7f05Smrg        icl->is_padded = 0;
429a3bd7f05Smrg        *callbacks = icl;
430a3bd7f05Smrg    }
431a3bd7f05Smrg    else {
432a3bd7f05Smrg        XtFree((char *) icl);
433a3bd7f05Smrg        *callbacks = NULL;
434444c061aSmrg    }
435444c061aSmrg    hookobj = XtHooksOfDisplay(XtDisplayOfObject(widget));
436444c061aSmrg    if (XtHasCallbacks(hookobj, XtNchangeHook) == XtCallbackHasSome) {
437a3bd7f05Smrg        XtChangeHookDataRec call_data;
438a3bd7f05Smrg
439a3bd7f05Smrg        call_data.type = XtHremoveCallbacks;
440a3bd7f05Smrg        call_data.widget = widget;
441a3bd7f05Smrg        call_data.event_data = (XtPointer) name;
442a3bd7f05Smrg        XtCallCallbackList(hookobj,
443a3bd7f05Smrg                           ((HookObject) hookobj)->hooks.changehook_callbacks,
444a3bd7f05Smrg                           (XtPointer) &call_data);
445444c061aSmrg    }
446444c061aSmrg    UNLOCK_APP(app);
447a3bd7f05Smrg}                               /* XtRemoveCallbacks */
448444c061aSmrg
449a3bd7f05Smrgvoid
450a3bd7f05Smrg_XtRemoveAllCallbacks(InternalCallbackList *callbacks)
451444c061aSmrg{
452444c061aSmrg    register InternalCallbackList icl = *callbacks;
453444c061aSmrg
454444c061aSmrg    if (icl) {
455a3bd7f05Smrg        if (icl->call_state)
456a3bd7f05Smrg            icl->call_state |= _XtCBFreeAfterCalling;
457a3bd7f05Smrg        else
458a3bd7f05Smrg            XtFree((char *) icl);
459a3bd7f05Smrg        *callbacks = NULL;
460444c061aSmrg    }
461a3bd7f05Smrg}                               /* _XtRemoveAllCallbacks */
462444c061aSmrg
463a3bd7f05Smrgvoid
464a3bd7f05SmrgXtRemoveAllCallbacks(Widget widget, _Xconst char *name)
465444c061aSmrg{
466444c061aSmrg    InternalCallbackList *callbacks;
467444c061aSmrg    Widget hookobj;
468444c061aSmrg    XtAppContext app = XtWidgetToApplicationContext(widget);
469444c061aSmrg
470444c061aSmrg    LOCK_APP(app);
471444c061aSmrg    callbacks = FetchInternalList(widget, name);
472444c061aSmrg    if (!callbacks) {
473a3bd7f05Smrg        XtAppWarningMsg(app,
474a3bd7f05Smrg                        XtNinvalidCallbackList, XtNxtRemoveAllCallback,
475a3bd7f05Smrg                        XtCXtToolkitError,
476a3bd7f05Smrg                        "Cannot find callback list in XtRemoveAllCallbacks",
477a3bd7f05Smrg                        NULL, NULL);
478a3bd7f05Smrg        UNLOCK_APP(app);
479a3bd7f05Smrg        return;
480444c061aSmrg    }
481444c061aSmrg    _XtRemoveAllCallbacks(callbacks);
482444c061aSmrg    hookobj = XtHooksOfDisplay(XtDisplayOfObject(widget));
483444c061aSmrg    if (XtHasCallbacks(hookobj, XtNchangeHook) == XtCallbackHasSome) {
484a3bd7f05Smrg        XtChangeHookDataRec call_data;
485a3bd7f05Smrg
486a3bd7f05Smrg        call_data.type = XtHremoveAllCallbacks;
487a3bd7f05Smrg        call_data.widget = widget;
488a3bd7f05Smrg        call_data.event_data = (XtPointer) name;
489a3bd7f05Smrg        XtCallCallbackList(hookobj,
490a3bd7f05Smrg                           ((HookObject) hookobj)->hooks.changehook_callbacks,
491a3bd7f05Smrg                           (XtPointer) &call_data);
492444c061aSmrg    }
493444c061aSmrg    UNLOCK_APP(app);
494a3bd7f05Smrg}                               /* XtRemoveAllCallbacks */
495444c061aSmrg
496a3bd7f05SmrgInternalCallbackList
497a3bd7f05Smrg_XtCompileCallbackList(XtCallbackList xtcallbacks)
498444c061aSmrg{
499444c061aSmrg    register int n;
500444c061aSmrg    register XtCallbackList xtcl, cl;
501444c061aSmrg    register InternalCallbackList callbacks;
502444c061aSmrg
503a3bd7f05Smrg    for (n = 0, xtcl = xtcallbacks; xtcl->callback; n++, xtcl++) {
504a3bd7f05Smrg    };
505a3bd7f05Smrg    if (n == 0)
506a3bd7f05Smrg        return (InternalCallbackList) NULL;
507444c061aSmrg
508a3bd7f05Smrg    callbacks =
509a3bd7f05Smrg        (InternalCallbackList)
510a3bd7f05Smrg        __XtMalloc((Cardinal)
511a3bd7f05Smrg                   (sizeof(InternalCallbackRec) +
512a3bd7f05Smrg                    sizeof(XtCallbackRec) * (size_t) n));
5130568f49bSmrg    callbacks->count = (unsigned short) n;
514444c061aSmrg    callbacks->is_padded = 0;
515444c061aSmrg    callbacks->call_state = 0;
516444c061aSmrg    cl = ToList(callbacks);
517444c061aSmrg    while (--n >= 0)
518a3bd7f05Smrg        *cl++ = *xtcallbacks++;
519a3bd7f05Smrg    return (callbacks);
520a3bd7f05Smrg}                               /* _XtCompileCallbackList */
521444c061aSmrg
522a3bd7f05SmrgXtCallbackList
523a3bd7f05Smrg_XtGetCallbackList(InternalCallbackList *callbacks)
524444c061aSmrg{
5250568f49bSmrg    int i;
5260568f49bSmrg    InternalCallbackList icl;
5270568f49bSmrg    XtCallbackList cl;
528444c061aSmrg
529444c061aSmrg    icl = *callbacks;
530444c061aSmrg    if (!icl) {
531a3bd7f05Smrg        static XtCallbackRec emptyList[1] = { {NULL, NULL} };
532a3bd7f05Smrg        return (XtCallbackList) emptyList;
533444c061aSmrg    }
534444c061aSmrg    if (icl->is_padded)
535a3bd7f05Smrg        return ToList(icl);
536444c061aSmrg    i = icl->count;
537444c061aSmrg    if (icl->call_state) {
5380568f49bSmrg        XtCallbackList ocl;
539a3bd7f05Smrg
540a3bd7f05Smrg        icl->call_state |= _XtCBFreeAfterCalling;
541a3bd7f05Smrg        ocl = ToList(icl);
542a3bd7f05Smrg        icl = (InternalCallbackList)
543a3bd7f05Smrg            __XtMalloc((Cardinal)
544a3bd7f05Smrg                       (sizeof(InternalCallbackRec) +
545a3bd7f05Smrg                        sizeof(XtCallbackRec) * (size_t) (i + 1)));
546a3bd7f05Smrg        icl->count = (unsigned short) i;
547a3bd7f05Smrg        icl->call_state = 0;
548a3bd7f05Smrg        cl = ToList(icl);
549a3bd7f05Smrg        while (--i >= 0)
550a3bd7f05Smrg            *cl++ = *ocl++;
551a3bd7f05Smrg    }
552a3bd7f05Smrg    else {
553a3bd7f05Smrg        icl = (InternalCallbackList)
554a3bd7f05Smrg            XtRealloc((char *) icl, (Cardinal) (sizeof(InternalCallbackRec)
555a3bd7f05Smrg                                                + sizeof(XtCallbackRec)
556a3bd7f05Smrg                                                * (size_t) (i + 1)));
557a3bd7f05Smrg        cl = ToList(icl) + i;
558444c061aSmrg    }
559444c061aSmrg    icl->is_padded = 1;
560444c061aSmrg    cl->callback = (XtCallbackProc) NULL;
561444c061aSmrg    cl->closure = NULL;
562444c061aSmrg    *callbacks = icl;
563444c061aSmrg    return ToList(icl);
564444c061aSmrg}
565444c061aSmrg
566a3bd7f05Smrgvoid
567a3bd7f05SmrgXtCallCallbacks(Widget widget,
568a3bd7f05Smrg                _Xconst char *name,
569a3bd7f05Smrg                XtPointer call_data)
570444c061aSmrg{
571444c061aSmrg    InternalCallbackList *callbacks;
572444c061aSmrg    InternalCallbackList icl;
573444c061aSmrg    XtCallbackList cl;
574444c061aSmrg    int i;
575444c061aSmrg    char ostate;
576444c061aSmrg    XtAppContext app = XtWidgetToApplicationContext(widget);
577444c061aSmrg
578444c061aSmrg    LOCK_APP(app);
579444c061aSmrg    callbacks = FetchInternalList(widget, name);
580444c061aSmrg    if (!callbacks) {
581a3bd7f05Smrg        XtAppWarningMsg(app,
582a3bd7f05Smrg                        XtNinvalidCallbackList, XtNxtCallCallback,
583a3bd7f05Smrg                        XtCXtToolkitError,
584a3bd7f05Smrg                        "Cannot find callback list in XtCallCallbacks", NULL,
585a3bd7f05Smrg                        NULL);
586a3bd7f05Smrg        UNLOCK_APP(app);
587a3bd7f05Smrg        return;
588444c061aSmrg    }
589444c061aSmrg
590444c061aSmrg    icl = *callbacks;
591444c061aSmrg    if (!icl) {
592a3bd7f05Smrg        UNLOCK_APP(app);
593a3bd7f05Smrg        return;
594444c061aSmrg    }
595444c061aSmrg    cl = ToList(icl);
596444c061aSmrg    if (icl->count == 1) {
597a3bd7f05Smrg        (*cl->callback) (widget, cl->closure, call_data);
598a3bd7f05Smrg        UNLOCK_APP(app);
599a3bd7f05Smrg        return;
600444c061aSmrg    }
601444c061aSmrg    ostate = icl->call_state;
602444c061aSmrg    icl->call_state = _XtCBCalling;
603444c061aSmrg    for (i = icl->count; --i >= 0; cl++)
604a3bd7f05Smrg        (*cl->callback) (widget, cl->closure, call_data);
605444c061aSmrg    if (ostate)
606a3bd7f05Smrg        icl->call_state |= ostate;
607444c061aSmrg    else if (icl->call_state & _XtCBFreeAfterCalling)
608a3bd7f05Smrg        XtFree((char *) icl);
609444c061aSmrg    else
610a3bd7f05Smrg        icl->call_state = ostate;
611444c061aSmrg    UNLOCK_APP(app);
612a3bd7f05Smrg}                               /* XtCallCallbacks */
613444c061aSmrg
614a3bd7f05SmrgXtCallbackStatus
615a3bd7f05SmrgXtHasCallbacks(Widget widget,
616a3bd7f05Smrg               _Xconst char *callback_name)
617444c061aSmrg{
618444c061aSmrg    InternalCallbackList *callbacks;
619444c061aSmrg    XtCallbackStatus retval = XtCallbackHasSome;
620a3bd7f05Smrg
621444c061aSmrg    WIDGET_TO_APPCON(widget);
622444c061aSmrg
623444c061aSmrg    LOCK_APP(app);
624444c061aSmrg    callbacks = FetchInternalList(widget, callback_name);
625444c061aSmrg    if (!callbacks)
626a3bd7f05Smrg        retval = XtCallbackNoList;
627444c061aSmrg    else if (!*callbacks)
628a3bd7f05Smrg        retval = XtCallbackHasNone;
629444c061aSmrg    UNLOCK_APP(app);
630444c061aSmrg    return retval;
631a3bd7f05Smrg}                               /* XtHasCallbacks */
632444c061aSmrg
633a3bd7f05Smrgvoid
634a3bd7f05SmrgXtCallCallbackList(Widget widget,
635a3bd7f05Smrg                   XtCallbackList callbacks,
636a3bd7f05Smrg                   XtPointer call_data)
637444c061aSmrg{
638444c061aSmrg    register InternalCallbackList icl;
639444c061aSmrg    register XtCallbackList cl;
640444c061aSmrg    register int i;
641444c061aSmrg    char ostate;
642a3bd7f05Smrg
643444c061aSmrg    WIDGET_TO_APPCON(widget);
644444c061aSmrg
645444c061aSmrg    LOCK_APP(app);
646444c061aSmrg    if (!callbacks) {
647a3bd7f05Smrg        UNLOCK_APP(app);
648a3bd7f05Smrg        return;
649444c061aSmrg    }
650a3bd7f05Smrg    icl = (InternalCallbackList) callbacks;
651444c061aSmrg    cl = ToList(icl);
652444c061aSmrg    if (icl->count == 1) {
653a3bd7f05Smrg        (*cl->callback) (widget, cl->closure, call_data);
654a3bd7f05Smrg        UNLOCK_APP(app);
655a3bd7f05Smrg        return;
656444c061aSmrg    }
657444c061aSmrg    ostate = icl->call_state;
658444c061aSmrg    icl->call_state = _XtCBCalling;
659444c061aSmrg    for (i = icl->count; --i >= 0; cl++)
660a3bd7f05Smrg        (*cl->callback) (widget, cl->closure, call_data);
661444c061aSmrg    if (ostate)
662a3bd7f05Smrg        icl->call_state |= ostate;
663444c061aSmrg    else if (icl->call_state & _XtCBFreeAfterCalling)
664a3bd7f05Smrg        XtFree((char *) icl);
665444c061aSmrg    else
666a3bd7f05Smrg        icl->call_state = 0;
667444c061aSmrg    UNLOCK_APP(app);
668a3bd7f05Smrg}                               /* XtCallCallbackList */
669444c061aSmrg
670a3bd7f05Smrgvoid
671a3bd7f05Smrg_XtPeekCallback(Widget widget _X_UNUSED,
672a3bd7f05Smrg                XtCallbackList callbacks,
673a3bd7f05Smrg                XtCallbackProc *callback,
674a3bd7f05Smrg                XtPointer *closure)
675444c061aSmrg{
676444c061aSmrg    register InternalCallbackList icl = (InternalCallbackList) callbacks;
677444c061aSmrg    register XtCallbackList cl;
678444c061aSmrg
679444c061aSmrg    if (!callbacks) {
680a3bd7f05Smrg        *callback = (XtCallbackProc) NULL;
681a3bd7f05Smrg        return;
682444c061aSmrg    }
683444c061aSmrg    cl = ToList(icl);
684444c061aSmrg    *callback = cl->callback;
685444c061aSmrg    *closure = cl->closure;
686444c061aSmrg    return;
687444c061aSmrg}
688444c061aSmrg
689a3bd7f05Smrgvoid
690a3bd7f05Smrg_XtCallConditionalCallbackList(Widget widget,
691a3bd7f05Smrg                               XtCallbackList callbacks,
692a3bd7f05Smrg                               XtPointer call_data,
693a3bd7f05Smrg                               _XtConditionProc cond_proc)
694444c061aSmrg{
695444c061aSmrg    register InternalCallbackList icl;
696444c061aSmrg    register XtCallbackList cl;
697444c061aSmrg    register int i;
698444c061aSmrg    char ostate;
699a3bd7f05Smrg
700444c061aSmrg    WIDGET_TO_APPCON(widget);
701444c061aSmrg
702444c061aSmrg    LOCK_APP(app);
703444c061aSmrg    if (!callbacks) {
704a3bd7f05Smrg        UNLOCK_APP(app);
705a3bd7f05Smrg        return;
706444c061aSmrg    }
707a3bd7f05Smrg    icl = (InternalCallbackList) callbacks;
708444c061aSmrg    cl = ToList(icl);
709444c061aSmrg    if (icl->count == 1) {
710a3bd7f05Smrg        (*cl->callback) (widget, cl->closure, call_data);
711a3bd7f05Smrg        (void) (*cond_proc) (call_data);
712a3bd7f05Smrg        UNLOCK_APP(app);
713a3bd7f05Smrg        return;
714444c061aSmrg    }
715444c061aSmrg    ostate = icl->call_state;
716444c061aSmrg    icl->call_state = _XtCBCalling;
717444c061aSmrg    for (i = icl->count; --i >= 0; cl++) {
718a3bd7f05Smrg        (*cl->callback) (widget, cl->closure, call_data);
719a3bd7f05Smrg        if (!(*cond_proc) (call_data))
720a3bd7f05Smrg            break;
721444c061aSmrg    }
722444c061aSmrg    if (ostate)
723a3bd7f05Smrg        icl->call_state |= ostate;
724444c061aSmrg    else if (icl->call_state & _XtCBFreeAfterCalling)
725a3bd7f05Smrg        XtFree((char *) icl);
726444c061aSmrg    else
727a3bd7f05Smrg        icl->call_state = 0;
728444c061aSmrg    UNLOCK_APP(app);
729444c061aSmrg}
730