VarGet.c revision 1477040f
1/* $Xorg: VarGet.c,v 1.4 2001/02/09 02:03:59 xorgcvs Exp $ */
2/*
3
4Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
5
6Permission is hereby granted, free of charge, to any person obtaining a
7copy of this software and associated documentation files (the "Software"),
8to deal in the Software without restriction, including without limitation
9the rights to use, copy, modify, merge, publish, distribute, sublicense,
10and/or sell copies of the Software, and to permit persons to whom the
11Software is furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice (including the next
14paragraph) shall be included in all copies or substantial portions of the
15Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23DEALINGS IN THE SOFTWARE.
24
25*/
26
27/*
28
29Copyright 1985, 1986, 1987, 1988, 1989, 1998  The Open Group
30
31Permission to use, copy, modify, distribute, and sell this software and its
32documentation for any purpose is hereby granted without fee, provided that
33the above copyright notice appear in all copies and that both that
34copyright notice and this permission notice appear in supporting
35documentation.
36
37The above copyright notice and this permission notice shall be included in
38all copies or substantial portions of the Software.
39
40THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
43OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
44AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
45CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46
47Except as contained in this notice, the name of The Open Group shall not be
48used in advertising or otherwise to promote the sale, use or other dealings
49in this Software without prior written authorization from The Open Group.
50
51*/
52/* $XFree86: xc/lib/Xt/VarGet.c,v 1.7 2001/07/29 05:01:12 tsi Exp $ */
53
54#ifdef HAVE_CONFIG_H
55#include <config.h>
56#endif
57#include "IntrinsicI.h"
58#include "VarargsI.h"
59#include "StringDefs.h"
60
61static String XtNxtGetTypedArg = "xtGetTypedArg";
62
63void
64XtVaGetSubresources(
65    Widget widget,
66    XtPointer base,
67    _Xconst char* name,
68    _Xconst char* class,
69    XtResourceList resources,
70    Cardinal num_resources,
71    ...)
72{
73    va_list                 var;
74    XtTypedArgList          args;
75    Cardinal                num_args;
76    int			    total_count, typed_count;
77    WIDGET_TO_APPCON(widget);
78
79    LOCK_APP(app);
80    va_start(var, num_resources);
81    _XtCountVaList(var, &total_count, &typed_count);
82    va_end(var);
83
84    va_start(var, num_resources);
85
86    _XtVaToTypedArgList(var, total_count, &args, &num_args);
87
88    _XtGetSubresources(widget, base, name, class, resources, num_resources,
89	NULL, 0, args, num_args);
90
91    if (num_args != 0) {
92	XtFree((XtPointer)args);
93    }
94
95    va_end(var);
96    UNLOCK_APP(app);
97}
98
99
100void
101XtVaGetApplicationResources(Widget widget, XtPointer base, XtResourceList resources, Cardinal num_resources, ...)
102{
103    va_list                 var;
104    XtTypedArgList          args;
105    Cardinal                num_args;
106    int			    total_count, typed_count;
107    WIDGET_TO_APPCON(widget);
108
109    LOCK_APP(app);
110    va_start(var,num_resources);
111    _XtCountVaList(var, &total_count, &typed_count);
112    va_end(var);
113
114    va_start(var,num_resources);
115
116    _XtVaToTypedArgList(var, total_count, &args, &num_args);
117
118    _XtGetApplicationResources(widget, base, resources, num_resources,
119	NULL, 0, args, num_args);
120
121    if (num_args != 0) {
122	XtFree((XtPointer)args);
123    }
124
125    va_end(var);
126    UNLOCK_APP(app);
127}
128
129
130static void
131GetTypedArg(
132    Widget              widget,
133    XtTypedArgList	typed_arg,
134    XtResourceList      resources,
135    Cardinal            num_resources)
136{
137    String              from_type = NULL;
138    Cardinal		from_size = 0;
139    XrmValue            from_val, to_val;
140    register Cardinal   i;
141    Arg			arg;
142    XtPointer		value;
143
144    /* note we presume that the XtResourceList to be un-compiled */
145
146    for (i = 0; i < num_resources; i++) {
147        if (StringToName(typed_arg->name) == StringToName(resources[i].resource_name)) {
148            from_type = resources[i].resource_type;
149	    from_size = resources[i].resource_size;
150            break;
151        }
152    }
153
154    if (i == num_resources) {
155	XtAppWarningMsg(XtWidgetToApplicationContext(widget),
156            "unknownType", XtNxtGetTypedArg, XtCXtToolkitError,
157            "Unable to find type of resource for conversion",
158            (String *)NULL, (Cardinal *)NULL);
159 	return;
160    }
161
162    value = ALLOCATE_LOCAL(from_size);
163    if (value == NULL) _XtAllocError(NULL);
164    XtSetArg(arg, typed_arg->name, value);
165    XtGetValues(widget, &arg, 1);
166
167    from_val.size = from_size;
168    from_val.addr = (XPointer)value;
169    to_val.addr = (XPointer)typed_arg->value;
170    to_val.size = typed_arg->size;
171
172    if (!XtConvertAndStore(widget, from_type, &from_val,
173			   typed_arg->type, &to_val)) {
174	if (to_val.size > (unsigned) typed_arg->size) {
175	    String params[2];
176	    Cardinal num_params = 2;
177	    params[0] = typed_arg->type;
178	    params[1] = XtName(widget);
179	    XtAppWarningMsg(XtWidgetToApplicationContext(widget),
180		"insufficientSpace", XtNxtGetTypedArg, XtCXtToolkitError,
181		"Insufficient space for converted type '%s' in widget '%s'",
182		params, &num_params);
183	}
184	else {
185	    String params[3];
186	    Cardinal num_params = 3;
187	    params[0] = from_type;
188	    params[1] = typed_arg->type;
189	    params[2] = XtName(widget);
190	    XtAppWarningMsg(XtWidgetToApplicationContext(widget),
191		"conversionFailed", XtNxtGetTypedArg, XtCXtToolkitError,
192		"Type conversion (%s to %s) failed for widget '%s'",
193		params, &num_params);
194	}
195    }
196    DEALLOCATE_LOCAL(value);
197}
198
199static int
200GetNestedArg(
201    Widget              widget,
202    XtTypedArgList	avlist,
203    ArgList             args,
204    XtResourceList      resources,
205    Cardinal            num_resources)
206{
207    int         count = 0;
208
209    for (; avlist->name != NULL; avlist++) {
210        if (avlist->type != NULL) {
211	    GetTypedArg(widget, avlist, resources, num_resources);
212        } else if(strcmp(avlist->name, XtVaNestedList) == 0) {
213            count += GetNestedArg(widget, (XtTypedArgList)avlist->value,
214				     args, resources, num_resources);
215        } else {
216            (args+count)->name = avlist->name;
217            (args+count)->value = avlist->value;
218            ++count;
219        }
220    }
221
222    return(count);
223}
224
225void
226XtVaGetValues(Widget widget, ...)
227{
228    va_list		var;
229    String      	attr;
230    ArgList    		args;
231    XtTypedArg		typed_arg;
232    XtResourceList      resources = (XtResourceList)NULL;
233    Cardinal            num_resources;
234    int			count, total_count, typed_count;
235    WIDGET_TO_APPCON(widget);
236
237    LOCK_APP(app);
238    va_start(var,widget);
239
240    _XtCountVaList(var, &total_count, &typed_count);
241
242    if (total_count != typed_count) {
243        args = (ArgList)__XtMalloc((unsigned)((total_count - typed_count)
244				* sizeof(Arg)));
245    }
246    else args = NULL;		/* for lint; really unused */
247    va_end(var);
248
249    va_start(var,widget);
250    for(attr = va_arg(var, String), count = 0 ; attr != NULL;
251			attr = va_arg(var, String)) {
252	if (strcmp(attr, XtVaTypedArg) == 0) {
253	    typed_arg.name = va_arg(var, String);
254	    typed_arg.type = va_arg(var, String);
255	    typed_arg.value = va_arg(var, XtArgVal);
256	    typed_arg.size = va_arg(var, int);
257
258	    if (resources == NULL) {
259		XtGetResourceList(XtClass(widget), &resources,&num_resources);
260	    }
261
262	    GetTypedArg(widget, &typed_arg, resources, num_resources);
263	} else if (strcmp(attr, XtVaNestedList) == 0) {
264	    if (resources == NULL) {
265		XtGetResourceList(XtClass(widget),&resources, &num_resources);
266	    }
267
268	    count += GetNestedArg(widget, va_arg(var, XtTypedArgList),
269				     (args+count), resources, num_resources);
270	} else {
271	    args[count].name = attr;
272	    args[count].value = va_arg(var, XtArgVal);
273	    count ++;
274	}
275    }
276    va_end(var);
277
278    if (resources != (XtResourceList)NULL) {
279	XtFree((XtPointer)resources);
280    }
281
282    if (total_count != typed_count) {
283	XtGetValues(widget, args, count);
284	XtFree((XtPointer)args);
285    }
286    UNLOCK_APP(app);
287}
288
289void
290XtVaGetSubvalues(XtPointer base,XtResourceList  resources, Cardinal num_resources, ...)
291{
292    va_list	var;
293    ArgList    	args;
294    Cardinal   	num_args;
295    int		total_count, typed_count;
296
297    va_start(var,num_resources);
298
299    _XtCountVaList(var, &total_count, &typed_count);
300
301    if (typed_count != 0) {
302	XtWarning("XtVaTypedArg is an invalid argument to XtVaGetSubvalues()\n");
303    }
304    va_end(var);
305
306    va_start(var,num_resources);
307    _XtVaToArgList((Widget)NULL, var, total_count, &args, &num_args);
308    va_end(var);
309
310    XtGetSubvalues(base, resources, num_resources, args, num_args);
311
312    if (num_args != 0) {
313        XtFree((XtPointer)args);
314    }
315}
316