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