summaryrefslogtreecommitdiff
path: root/xc/lib/Xt/Destroy.c
blob: a7633600fd48c0b018bb29dc9d97da825728d28e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* $TOG: Destroy.c /main/49 1998/02/06 13:21:18 kaleb $ */

/***********************************************************
Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts
Copyright 1993 by Sun Microsystems, Inc. Mountain View, CA.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Digital or Sun not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

/*

Copyright 1987, 1988, 1994, 1998  The Open Group

All Rights Reserved.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

*/

#include "IntrinsicI.h"

struct _DestroyRec {
    int dispatch_level;
    Widget widget;
};

static void Recursive(widget, proc)
    Widget       widget;
    XtWidgetProc proc;
{
    register int    i;
    CompositePart   *cwp;

    /* Recurse down normal children */
    if (XtIsComposite(widget)) {
	cwp = &(((CompositeWidget) widget)->composite);
	for (i = 0; i < cwp->num_children; i++) {
	    Recursive(cwp->children[i], proc);
	}
    }

    /* Recurse down popup children */
    if (XtIsWidget(widget)) {
	for (i = 0; i < widget->core.num_popups; i++) {
	    Recursive(widget->core.popup_list[i], proc);
	}
    }

    /* Finally, apply procedure to this widget */
    (*proc) (widget);  
} /* Recursive */

static void Phase1Destroy (widget)
    Widget    widget;
{
    Widget hookobj = XtHooksOfDisplay(XtDisplayOfObject(widget));

    widget->core.being_destroyed = TRUE;
    if (XtHasCallbacks(hookobj, XtNdestroyHook) == XtCallbackHasSome) {
	XtDestroyHookDataRec call_data;

	call_data.type = XtHdestroy;
	call_data.widget = widget;
	XtCallCallbackList(hookobj, 
		((HookObject)hookobj)->hooks.destroyhook_callbacks, 
		(XtPointer) &call_data);
    }
} /* Phase1Destroy */

static void Phase2Callbacks(widget)
    Widget    widget;
{
    if (widget->core.destroy_callbacks != NULL) {
	XtCallCallbackList(widget,
			   widget->core.destroy_callbacks, (XtPointer) NULL);
    }
} /* Phase2Callbacks */

static void Phase2Destroy(widget)
    register Widget widget;
{
    register WidgetClass	    class;
    register ConstraintWidgetClass  cwClass;
    ObjectClassExtension	    ext;

    /* Call constraint destroy procedures */
    if (XtParent(widget) != NULL && !XtIsShell(widget) && XtIsConstraint(XtParent(widget))) {
	LOCK_PROCESS;
	cwClass = (ConstraintWidgetClass)XtParent(widget)->core.widget_class;
	UNLOCK_PROCESS;
	for (;;) {
	    XtWidgetProc destroy;

	    LOCK_PROCESS;
	    destroy = cwClass->constraint_class.destroy;
	    UNLOCK_PROCESS;
	    if (destroy)
		(*destroy) (widget);
            if (cwClass == (ConstraintWidgetClass)constraintWidgetClass) break;
	    LOCK_PROCESS;
	    cwClass = (ConstraintWidgetClass) cwClass->core_class.superclass;
	    UNLOCK_PROCESS;
	}
    }

    /* Call widget destroy procedures */
    LOCK_PROCESS;
    for (class = widget->core.widget_class;
	 class != NULL; 
	 class = class->core_class.superclass) {
	XtWidgetProc destroy;

	destroy = class->core_class.destroy;
	UNLOCK_PROCESS;
	if (destroy)
	    (*destroy) (widget);
	LOCK_PROCESS;
    }

    /* Call widget deallocate procedure */
    ext = (ObjectClassExtension)
	XtGetClassExtension(widget->core.widget_class, 
			    XtOffsetOf(CoreClassPart, extension),
			    NULLQUARK, XtObjectExtensionVersion, 
			    sizeof(ObjectClassExtensionRec));
    if (ext && ext->deallocate) {
	XtDeallocateProc deallocate;
	deallocate = ext->deallocate;
	UNLOCK_PROCESS;
	(*deallocate)(widget, NULL);
    } else {
	UNLOCK_PROCESS;
	XtFree((char *)widget);
    }
} /* Phase2Destroy */

static Boolean IsDescendant(widget, root)
    register Widget widget, root;
{
    while ((widget = XtParent(widget)) != root) {
	if (widget == NULL) return False;
    }
    return True;
}

static void XtPhase2Destroy (widget)
    register Widget widget;
{
    Display	    *display;
    Window	    window;
    Widget          parent;
    XtAppContext    app = XtWidgetToApplicationContext(widget);
    Widget	    outerInPhase2Destroy = app->in_phase2_destroy;
    int		    starting_count = app->destroy_count;
    Boolean	    isPopup = False;

    /* invalidate focus trace cache for this display */
    _XtGetPerDisplay(XtDisplayOfObject(widget))->pdi.traceDepth = 0;

    parent = widget->core.parent;

    if (parent && XtIsWidget(parent) && parent->core.num_popups) {
	int i;
	for (i = 0; i < parent->core.num_popups; i++) {
	    if (parent->core.popup_list[i] == widget) {
		isPopup = True;
		break;
	    }
	}
    }

    if (!isPopup && parent && XtIsComposite(parent)) {
	XtWidgetProc delete_child;

	LOCK_PROCESS;
	delete_child =
	    ((CompositeWidgetClass) parent->core.widget_class)->
		composite_class.delete_child;
	UNLOCK_PROCESS;
        if (XtIsRectObj(widget)) {
       	    XtUnmanageChild(widget);
        }
	if (delete_child == NULL) {
	    String param;
	    Cardinal num_params = 1;

	    LOCK_PROCESS;
	    param = parent->core.widget_class->core_class.class_name;
	    UNLOCK_PROCESS;
	    XtAppWarningMsg(XtWidgetToApplicationContext(widget),
		"invalidProcedure","deleteChild",XtCXtToolkitError,
		"null delete_child procedure for class %s in XtDestroy",
		&param, &num_params);
	} else {
	    (*delete_child) (widget);
	}
    }

    /* widget is freed in Phase2Destroy, so retrieve window now.
     * Shells destroy their own windows, to prevent window leaks in
     * popups; this test is practical only when XtIsShell() is cheap.
     */
    if (XtIsShell(widget) || !XtIsWidget(widget)) {
	window = 0;
#ifdef lint
	display = 0;
#endif
    }
    else {
	display = XtDisplay(widget);
	window = widget->core.window;
    }

    Recursive(widget, Phase2Callbacks);
    if (app->destroy_count > starting_count) {
	int i = starting_count;
	while (i < app->destroy_count) {

	    DestroyRec * dr = app->destroy_list + i;
	    if (IsDescendant(dr->widget, widget)) {
		Widget descendant = dr->widget;
		register int j;
		app->destroy_count--;
		for (j = app->destroy_count - i; --j >= 0; dr++)
		    *dr = *(dr + 1);
		XtPhase2Destroy(descendant);
	    }
	    else i++;
	}
    }

    app->in_phase2_destroy = widget;
    Recursive(widget, Phase2Destroy);
    app->in_phase2_destroy = outerInPhase2Destroy;

    if (isPopup) {
	int i;
	for (i = 0; i < parent->core.num_popups; i++)
	    if (parent->core.popup_list[i] == widget) {
		parent->core.num_popups--;
		while (i < parent->core.num_popups) {
		    parent->core.popup_list[i] = parent->core.popup_list[i+1];
		    i++;
		}
		break;
	    }
    }

    /* %%% the following parent test hides a more serious problem,
       but it avoids breaking those who depended on the old bug
       until we have time to fix it properly. */

    if (window && (parent == NULL || !parent->core.being_destroyed))
	XDestroyWindow(display, window);
} /* XtPhase2Destroy */


void _XtDoPhase2Destroy(app, dispatch_level)
    XtAppContext app;
    int dispatch_level;
{
    /* Phase 2 must occur in fifo order.  List is not necessarily
     * contiguous in dispatch_level.
     */

    int i = 0;
    while (i < app->destroy_count) {

	/* XtPhase2Destroy can result in calls to XtDestroyWidget,
	 * and these could cause app->destroy_list to be reallocated.
	 */

	DestroyRec* dr = app->destroy_list + i;
	if (dr->dispatch_level >= dispatch_level)  {
	    Widget w = dr->widget;
	    register int j;
	    app->destroy_count--;
	    for (j = app->destroy_count - i; --j >=0; dr++)
		*dr = *(dr + 1);
	    XtPhase2Destroy(w);
	}
	else i++;
    }
}


void XtDestroyWidget (widget)
    Widget    widget;
{
    XtAppContext app;
    DestroyRec *dr, *dr2;

    app = XtWidgetToApplicationContext(widget);
    LOCK_APP(app);
    if (widget->core.being_destroyed) {
	UNLOCK_APP(app);
	return;
    }
    Recursive(widget, Phase1Destroy);

    if (app->in_phase2_destroy &&
	IsDescendant(widget, app->in_phase2_destroy))
    {
	XtPhase2Destroy(widget);
	UNLOCK_APP(app);
	return;
    }

    if (app->destroy_count == app->destroy_list_size) {
	app->destroy_list_size += 10;
	app->destroy_list = (DestroyRec*)
	    XtRealloc( (char*)app->destroy_list,
		       (unsigned)sizeof(DestroyRec)*app->destroy_list_size
		      );
    }
    dr = app->destroy_list + app->destroy_count++;
    dr->dispatch_level = app->dispatch_level;
    dr->widget = widget;

    if (app->dispatch_level > 1) {
	int i;
	for (i = app->destroy_count - 1; i;) {
	    /* this handles only one case of nesting difficulties */
 	    dr = app->destroy_list + (--i);
 	    if (dr->dispatch_level < app->dispatch_level &&
 		IsDescendant(dr->widget, widget)) {
 	        dr2 = app->destroy_list + (app->destroy_count-1);
 		dr2->dispatch_level = dr->dispatch_level;
  		break;
  	    }
  	}
    }

    if (_XtSafeToDestroy(app)) {
	app->dispatch_level = 1; /* avoid nested _XtDoPhase2Destroy */
	_XtDoPhase2Destroy(app, 0);
	app->dispatch_level = 0;
    }
    UNLOCK_APP(app);
	
} /* XtDestroyWidget */