Composite.c revision fdf6a26f
1/***********************************************************
2Copyright (c) 1993, Oracle and/or its affiliates.
3
4Permission is hereby granted, free of charge, to any person obtaining a
5copy of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation
7the rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the
9Software is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice (including the next
12paragraph) shall be included in all copies or substantial portions of the
13Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21DEALINGS IN THE SOFTWARE.
22
23Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
24
25                        All Rights Reserved
26
27Permission to use, copy, modify, and distribute this software and its
28documentation for any purpose and without fee is hereby granted,
29provided that the above copyright notice appear in all copies and that
30both that copyright notice and this permission notice appear in
31supporting documentation, and that the name of Digital not be
32used in advertising or publicity pertaining to distribution of the
33software without specific, written prior permission.
34
35DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
36ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
37DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
38ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
39WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
40ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
41SOFTWARE.
42
43******************************************************************/
44
45/*
46
47Copyright 1987, 1988, 1998  The Open Group
48
49Permission to use, copy, modify, distribute, and sell this software and its
50documentation for any purpose is hereby granted without fee, provided that
51the above copyright notice appear in all copies and that both that
52copyright notice and this permission notice appear in supporting
53documentation.
54
55The above copyright notice and this permission notice shall be included in
56all copies or substantial portions of the Software.
57
58THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
61OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
62AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
63CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
64
65Except as contained in this notice, the name of The Open Group shall not be
66used in advertising or otherwise to promote the sale, use or other dealings
67in this Software without prior written authorization from The Open Group.
68
69*/
70
71#ifdef HAVE_CONFIG_H
72#include <config.h>
73#endif
74#include "IntrinsicI.h"
75#include "StringDefs.h"
76
77static XtResource resources[] = {
78    {XtNchildren, XtCReadOnly, XtRWidgetList, sizeof(WidgetList),
79     XtOffsetOf(CompositeRec, composite.children), XtRImmediate, NULL},
80    {XtNnumChildren, XtCReadOnly, XtRCardinal, sizeof(Cardinal),
81     XtOffsetOf(CompositeRec, composite.num_children), XtRImmediate, NULL},
82    {XtNinsertPosition, XtCInsertPosition, XtRFunction, sizeof(XtOrderProc),
83     XtOffsetOf(CompositeRec, composite.insert_position), XtRImmediate, NULL},
84};
85
86static void CompositeClassPartInitialize(WidgetClass);
87static void CompositeInitialize(Widget, Widget, ArgList, Cardinal *);
88static void CompositeInsertChild(Widget);
89static void CompositeDeleteChild(Widget);
90static void CompositeDestroy(Widget);
91
92externaldef(compositeclassrec)
93CompositeClassRec compositeClassRec = {
94    {
95    /******* CorePart *******/
96     /* superclass           */ &widgetClassRec,
97     /* class_name           */ "Composite",
98     /* widget_size          */ sizeof(CompositeRec),
99     /* class_initialize     */ NULL,
100     /* class_part_initialize */ CompositeClassPartInitialize,
101     /* class_inited         */ FALSE,
102     /* initialize           */ CompositeInitialize,
103     /* initialize_hook      */ NULL,
104     /* realize              */ XtInheritRealize,
105     /* actions              */ NULL,
106     /* num_actions          */ 0,
107     /* resources            */ resources,
108     /* num_resources        */ XtNumber(resources),
109     /* xrm_class            */ NULLQUARK,
110     /* compress_motion      */ FALSE,
111     /* compress_exposure    */ TRUE,
112     /* compress_enterleave  */ FALSE,
113     /* visible_interest     */ FALSE,
114     /* destroy              */ CompositeDestroy,
115     /* resize               */ NULL,
116     /* expose               */ NULL,
117     /* set_values           */ NULL,
118     /* set_values_hook      */ NULL,
119     /* set_values_almost    */ XtInheritSetValuesAlmost,
120     /* get_values_hook      */ NULL,
121     /* accept_focus         */ NULL,
122     /* version              */ XtVersion,
123     /* callback_offsets     */ NULL,
124     /* tm_table             */ NULL,
125     /* query_geometry       */ NULL,
126     /* display_accelerator  */ NULL,
127     /* extension            */ NULL
128     }
129    ,
130    {
131    /**** CompositePart *****/
132     /* geometry_handler     */ NULL,
133     /* change_managed       */ NULL,
134     /* insert_child         */ CompositeInsertChild,
135     /* delete_child         */ CompositeDeleteChild,
136     /* extension            */ NULL
137     }
138};
139
140externaldef(compositewidgetclass)
141WidgetClass compositeWidgetClass = (WidgetClass) &compositeClassRec;
142
143static void
144InheritAllowsChangeManagedSet(WidgetClass widget_class)
145{
146    CompositeWidgetClass cc = (CompositeWidgetClass) widget_class;
147    CompositeClassExtension ext, super_ext;
148
149    ext = (CompositeClassExtension)
150        XtGetClassExtension(widget_class,
151                            XtOffsetOf(CompositeClassRec,
152                                       composite_class.extension), NULLQUARK,
153                            1L, 0);
154
155    if (ext && ext->version == XtCompositeExtensionVersion)
156        return;
157
158    super_ext = (CompositeClassExtension)
159        XtGetClassExtension(cc->core_class.superclass,
160                            XtOffsetOf(CompositeClassRec,
161                                       composite_class.extension), NULLQUARK,
162                            1L, 0);
163
164    LOCK_PROCESS;
165    if (super_ext && super_ext->version == XtCompositeExtensionVersion &&
166        super_ext->record_size == sizeof(CompositeClassExtensionRec) &&
167        super_ext->allows_change_managed_set) {
168        CompositeClassExtension new_ext;
169
170        new_ext = (CompositeClassExtension)
171            __XtCalloc(1, sizeof(CompositeClassExtensionRec));
172
173        /* Be careful to inherit only what is appropriate */
174        new_ext->next_extension = cc->composite_class.extension;
175        new_ext->record_type = NULLQUARK;
176        new_ext->version = XtCompositeExtensionVersion;
177        new_ext->record_size = sizeof(CompositeClassExtensionRec);
178        new_ext->accepts_objects = (ext ? ext->accepts_objects : False);
179        new_ext->allows_change_managed_set = True;
180        cc->composite_class.extension = (XtPointer) new_ext;
181    }
182    UNLOCK_PROCESS;
183}
184
185static void
186CompositeClassPartInitialize(WidgetClass myWidgetClass)
187{
188    register CompositePartPtr wcPtr;
189    register CompositePartPtr superPtr = NULL;
190
191    wcPtr = (CompositePartPtr)
192        &(((CompositeWidgetClass) myWidgetClass)->composite_class);
193
194    if (myWidgetClass != compositeWidgetClass) {
195        /* don't compute possible bogus pointer */
196        superPtr =
197            (CompositePartPtr) &
198            (((CompositeWidgetClass) myWidgetClass->core_class.superclass)->
199             composite_class);
200
201        LOCK_PROCESS;
202        if (wcPtr->geometry_manager == XtInheritGeometryManager) {
203            wcPtr->geometry_manager = superPtr->geometry_manager;
204        }
205
206        if (wcPtr->change_managed == XtInheritChangeManaged) {
207            wcPtr->change_managed = superPtr->change_managed;
208            InheritAllowsChangeManagedSet(myWidgetClass);
209        }
210
211        if (wcPtr->insert_child == XtInheritInsertChild) {
212            wcPtr->insert_child = superPtr->insert_child;
213        }
214
215        if (wcPtr->delete_child == XtInheritDeleteChild) {
216            wcPtr->delete_child = superPtr->delete_child;
217        }
218        UNLOCK_PROCESS;
219    }
220}
221
222static void
223CompositeDestroy(Widget w)
224{
225    register CompositeWidget cw = (CompositeWidget) w;
226
227    XtFree((char *) cw->composite.children);
228}
229
230static void
231CompositeInsertChild(Widget w)
232{
233    register Cardinal position;
234    register Cardinal i;
235    register CompositeWidget cw;
236    register WidgetList children;
237
238    cw = (CompositeWidget) w->core.parent;
239    children = cw->composite.children;
240
241    if (cw->composite.insert_position != NULL)
242        position = (*(cw->composite.insert_position)) (w);
243    else
244        position = cw->composite.num_children;
245
246    if (cw->composite.num_children == cw->composite.num_slots) {
247        /* Allocate more space */
248        cw->composite.num_slots += (cw->composite.num_slots / 2) + 2;
249        cw->composite.children = children =
250            XtReallocArray(children, cw->composite.num_slots,
251                           (Cardinal) sizeof(Widget));
252    }
253    /* Ripple children up one space from "position" */
254    for (i = cw->composite.num_children; i > position; i--) {
255        children[i] = children[i - 1];
256    }
257    children[position] = w;
258    cw->composite.num_children++;
259}
260
261static void
262CompositeDeleteChild(Widget w)
263{
264    register Cardinal position;
265    register Cardinal i;
266    register CompositeWidget cw;
267
268    cw = (CompositeWidget) w->core.parent;
269
270    for (position = 0; position < cw->composite.num_children; position++) {
271        if (cw->composite.children[position] == w) {
272            break;
273        }
274    }
275    if (position == cw->composite.num_children)
276        return;
277
278    /* Ripple children down one space from "position" */
279    cw->composite.num_children--;
280    for (i = position; i < cw->composite.num_children; i++) {
281        cw->composite.children[i] = cw->composite.children[i + 1];
282    }
283}
284
285static void
286CompositeInitialize(Widget requested_widget _X_UNUSED,
287                    Widget new_widget,
288                    ArgList args _X_UNUSED,
289                    Cardinal *num_args _X_UNUSED)
290{
291    register CompositeWidget cw;
292
293    cw = (CompositeWidget) new_widget;
294    cw->composite.num_children = 0;
295    cw->composite.children = NULL;
296    cw->composite.num_slots = 0;
297}
298