Object.c revision 249c3046
1/***********************************************************
2Copyright (c) 1993, Oracle and/or its affiliates. All rights reserved.
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, 1994, 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        {XtNdestroyCallback, XtCCallback, XtRCallback,sizeof(XtPointer),
79         XtOffsetOf(ObjectRec,object.destroy_callbacks),
80	 XtRCallback, (XtPointer)NULL}
81    };
82
83static void ObjectClassPartInitialize(WidgetClass);
84static Boolean ObjectSetValues(Widget, Widget, Widget, ArgList, Cardinal *);
85static void ObjectDestroy(Widget);
86
87externaldef(objectclassrec) ObjectClassRec objectClassRec = {
88  {
89    /* superclass	  */	NULL,
90    /* class_name	  */	"Object",
91    /* widget_size	  */	sizeof(ObjectRec),
92    /* class_initialize   */    NULL,
93    /* class_part_initialize*/	ObjectClassPartInitialize,
94    /* class_inited       */	FALSE,
95    /* initialize	  */	NULL,
96    /* initialize_hook    */	NULL,
97    /* pad                */    NULL,
98    /* pad		  */	NULL,
99    /* pad       	  */	0,
100    /* resources	  */	resources,
101    /* num_resources	  */    XtNumber(resources),
102    /* xrm_class	  */	NULLQUARK,
103    /* pad                */    FALSE,
104    /* pad                */    FALSE,
105    /* pad                */    FALSE,
106    /* pad                */    FALSE,
107    /* destroy		  */	ObjectDestroy,
108    /* pad		  */	NULL,
109    /* pad		  */	NULL,
110    /* set_values	  */	ObjectSetValues,
111    /* set_values_hook    */	NULL,
112    /* pad                */    NULL,
113    /* get_values_hook    */	NULL,
114    /* pad                */    NULL,
115    /* version		  */	XtVersion,
116    /* callback_offsets   */    NULL,
117    /* pad                */    NULL,
118    /* pad                */    NULL,
119    /* pad                */    NULL,
120    /* extension	    */  NULL
121}
122};
123
124externaldef(objectClass) WidgetClass objectClass
125                          = (WidgetClass)&objectClassRec;
126
127/*
128 * Start of object routines.
129 */
130
131
132static void ConstructCallbackOffsets(
133    WidgetClass widgetClass)
134{
135    static XrmQuark QCallback = NULLQUARK;
136    register int i;
137    register int tableSize;
138    register CallbackTable newTable;
139    register CallbackTable superTable;
140    register XrmResourceList resourceList;
141    ObjectClass objectClass = (ObjectClass)widgetClass;
142
143    /*
144      This function builds an array of pointers to the resource
145      structures which describe the callbacks for this widget class.
146      This array is special in that the 0th entry is the number of
147      callback pointers.
148     */
149
150    if (QCallback == NULLQUARK)
151	QCallback = XrmPermStringToQuark(XtRCallback);
152
153    if (objectClass->object_class.superclass != NULL) {
154	superTable = (CallbackTable)
155	    ((ObjectClass) objectClass->object_class.superclass)->
156		object_class.callback_private;
157	tableSize = (int)(long) superTable[0];
158    } else {
159	superTable = (CallbackTable) NULL;
160	tableSize = 0;
161    }
162
163    /* Count the number of callbacks */
164    resourceList = (XrmResourceList) objectClass->object_class.resources;
165    for (i = objectClass->object_class.num_resources; --i >= 0; resourceList++)
166	if (resourceList->xrm_type == QCallback)
167	    tableSize++;
168
169    /*
170     * Allocate and load the table.  Make sure that the new callback
171     * offsets occur in the table ahead of the superclass callback
172     * offsets so that resource overrides work.
173     */
174    newTable = (CallbackTable)
175	__XtMalloc(sizeof(XrmResource *) * (tableSize + 1));
176
177    newTable[0] = (XrmResource *)(long) tableSize;
178
179    if (superTable)
180	tableSize -= (int)(long) superTable[0];
181    resourceList = (XrmResourceList) objectClass->object_class.resources;
182    for (i=1; tableSize > 0; resourceList++)
183	if (resourceList->xrm_type == QCallback) {
184	    newTable[i++] = resourceList;
185	    tableSize--;
186	}
187
188    if (superTable)
189	for (tableSize = (int)(long) *superTable++;
190	    --tableSize >= 0; superTable++)
191	    newTable[i++] = *superTable;
192
193    objectClass->object_class.callback_private = (XtPointer) newTable;
194}
195
196static void InheritObjectExtensionMethods(
197    WidgetClass widget_class)
198{
199    ObjectClass oc = (ObjectClass) widget_class;
200    ObjectClassExtension ext, super_ext = NULL;
201
202    ext = (ObjectClassExtension)
203	XtGetClassExtension(widget_class,
204		    XtOffsetOf(ObjectClassRec, object_class.extension),
205			    NULLQUARK, XtObjectExtensionVersion,
206			    sizeof(ObjectClassExtensionRec));
207
208    if (oc->object_class.superclass)
209	super_ext = (ObjectClassExtension)
210	    XtGetClassExtension(oc->object_class.superclass,
211			XtOffsetOf(ObjectClassRec, object_class.extension),
212				NULLQUARK, XtObjectExtensionVersion,
213				sizeof(ObjectClassExtensionRec));
214    LOCK_PROCESS;
215    if (ext) {
216	if (ext->allocate == XtInheritAllocate)
217	    ext->allocate = (super_ext ? super_ext->allocate : NULL);
218	if (ext->deallocate == XtInheritDeallocate)
219	    ext->deallocate = (super_ext ? super_ext->deallocate : NULL);
220    } else if (super_ext) {
221	/* Be careful to inherit only what is appropriate */
222	ext = (ObjectClassExtension)
223	    __XtCalloc(1, sizeof(ObjectClassExtensionRec));
224	ext->next_extension = oc->object_class.extension;
225	ext->record_type = NULLQUARK;
226	ext->version = XtObjectExtensionVersion;
227	ext->record_size = sizeof(ObjectClassExtensionRec);
228	ext->allocate = super_ext->allocate;
229	ext->deallocate = super_ext->deallocate;
230	oc->object_class.extension = (XtPointer) ext;
231    }
232    UNLOCK_PROCESS;
233}
234
235static void ObjectClassPartInitialize(
236    register WidgetClass wc)
237{
238   ObjectClass oc = (ObjectClass)wc;
239
240   oc->object_class.xrm_class =
241       XrmPermStringToQuark(oc->object_class.class_name);
242
243   if (oc->object_class.resources)
244       _XtCompileResourceList(oc->object_class.resources,
245			      oc->object_class.num_resources);
246
247   ConstructCallbackOffsets(wc);
248   _XtResourceDependencies(wc);
249   InheritObjectExtensionMethods(wc);
250}
251
252
253/*ARGSUSED*/
254static Boolean ObjectSetValues(
255    Widget	old,
256    Widget	request,
257    Widget	widget,
258    ArgList	args,
259    Cardinal *	num_args)
260{
261    register CallbackTable offsets;
262    register int i;
263    register InternalCallbackList *ol, *nl;
264
265    LOCK_PROCESS;
266    /* Compile any callback lists into internal form */
267    offsets = (CallbackTable) XtClass(widget)->core_class.callback_private;
268
269    for (i= (int)(long) *(offsets++); --i >= 0; offsets++) {
270	ol = (InternalCallbackList *)
271	    ((char *) old - (*offsets)->xrm_offset - 1);
272	nl = (InternalCallbackList *)
273	    ((char *) widget - (*offsets)->xrm_offset - 1);
274	if (*ol != *nl) {
275	    if (*ol != NULL)
276		XtFree((char *) *ol);
277	    if (*nl != NULL)
278		*nl = _XtCompileCallbackList((XtCallbackList) *nl);
279	}
280    }
281    UNLOCK_PROCESS;
282    return False;
283}
284
285
286static void ObjectDestroy (
287    register Widget    widget)
288{
289    register CallbackTable offsets;
290    register int i;
291    register InternalCallbackList cl;
292
293    /* Remove all callbacks associated with widget */
294    LOCK_PROCESS;
295    offsets = (CallbackTable)
296	widget->core.widget_class->core_class.callback_private;
297
298    for (i = (int)(long) *(offsets++); --i >= 0; offsets++) {
299	cl = *(InternalCallbackList *)
300	    ((char *) widget - (*offsets)->xrm_offset - 1);
301	if (cl) XtFree((char *) cl);
302    }
303    UNLOCK_PROCESS;
304} /* ObjectDestroy */
305