1444c061aSmrg/*
2444c061aSmrg
3fdf6a26fSmrgCopyright (c) 1993, Oracle and/or its affiliates.
41477040fSmrg
51477040fSmrgPermission is hereby granted, free of charge, to any person obtaining a
61477040fSmrgcopy of this software and associated documentation files (the "Software"),
71477040fSmrgto deal in the Software without restriction, including without limitation
81477040fSmrgthe rights to use, copy, modify, merge, publish, distribute, sublicense,
91477040fSmrgand/or sell copies of the Software, and to permit persons to whom the
101477040fSmrgSoftware is furnished to do so, subject to the following conditions:
111477040fSmrg
121477040fSmrgThe above copyright notice and this permission notice (including the next
131477040fSmrgparagraph) shall be included in all copies or substantial portions of the
141477040fSmrgSoftware.
151477040fSmrg
161477040fSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171477040fSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181477040fSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
191477040fSmrgTHE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201477040fSmrgLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
211477040fSmrgFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
221477040fSmrgDEALINGS IN THE SOFTWARE.
23444c061aSmrg
24444c061aSmrg*/
25444c061aSmrg
26444c061aSmrg/*
27444c061aSmrg
28444c061aSmrgCopyright 1985, 1986, 1987, 1988, 1989, 1998  The Open Group
29444c061aSmrg
30444c061aSmrgPermission to use, copy, modify, distribute, and sell this software and its
31444c061aSmrgdocumentation for any purpose is hereby granted without fee, provided that
32444c061aSmrgthe above copyright notice appear in all copies and that both that
33444c061aSmrgcopyright notice and this permission notice appear in supporting
34444c061aSmrgdocumentation.
35444c061aSmrg
36444c061aSmrgThe above copyright notice and this permission notice shall be included in
37444c061aSmrgall copies or substantial portions of the Software.
38444c061aSmrg
39444c061aSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40444c061aSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41444c061aSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
42444c061aSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
43444c061aSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44444c061aSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45444c061aSmrg
46444c061aSmrgExcept as contained in this notice, the name of The Open Group shall not be
47444c061aSmrgused in advertising or otherwise to promote the sale, use or other dealings
48444c061aSmrgin this Software without prior written authorization from The Open Group.
49444c061aSmrg
50444c061aSmrg*/
51444c061aSmrg
52444c061aSmrg#ifdef HAVE_CONFIG_H
53444c061aSmrg#include <config.h>
54444c061aSmrg#endif
55444c061aSmrg#include "IntrinsicI.h"
56444c061aSmrg#include "VarargsI.h"
57444c061aSmrg#include "StringDefs.h"
58444c061aSmrg
59444c061aSmrgstatic String XtNxtGetTypedArg = "xtGetTypedArg";
60444c061aSmrg
61444c061aSmrgvoid
62a3bd7f05SmrgXtVaGetSubresources(Widget widget,
63a3bd7f05Smrg                    XtPointer base,
64a3bd7f05Smrg                    _Xconst char *name,
65a3bd7f05Smrg                    _Xconst char *class,
66a3bd7f05Smrg                    XtResourceList resources,
67a3bd7f05Smrg                    Cardinal num_resources,
68a3bd7f05Smrg                    ...)
69444c061aSmrg{
70a3bd7f05Smrg    va_list var;
71a3bd7f05Smrg    XtTypedArgList args;
72a3bd7f05Smrg    Cardinal num_args;
73a3bd7f05Smrg    int total_count, typed_count;
74a3bd7f05Smrg
75444c061aSmrg    WIDGET_TO_APPCON(widget);
76444c061aSmrg
77444c061aSmrg    LOCK_APP(app);
78444c061aSmrg    va_start(var, num_resources);
79444c061aSmrg    _XtCountVaList(var, &total_count, &typed_count);
80444c061aSmrg    va_end(var);
81444c061aSmrg
82444c061aSmrg    va_start(var, num_resources);
83444c061aSmrg
84444c061aSmrg    _XtVaToTypedArgList(var, total_count, &args, &num_args);
85444c061aSmrg
86444c061aSmrg    _XtGetSubresources(widget, base, name, class, resources, num_resources,
87a3bd7f05Smrg                       NULL, 0, args, num_args);
88444c061aSmrg
89a3bd7f05Smrg    XtFree((XtPointer) args);
90444c061aSmrg
91444c061aSmrg    va_end(var);
92444c061aSmrg    UNLOCK_APP(app);
93444c061aSmrg}
94444c061aSmrg
95444c061aSmrgvoid
96a3bd7f05SmrgXtVaGetApplicationResources(Widget widget,
97a3bd7f05Smrg                            XtPointer base,
98a3bd7f05Smrg                            XtResourceList resources,
99a3bd7f05Smrg                            Cardinal num_resources,
100a3bd7f05Smrg                            ...)
101444c061aSmrg{
102a3bd7f05Smrg    va_list var;
103a3bd7f05Smrg    XtTypedArgList args;
104a3bd7f05Smrg    Cardinal num_args;
105a3bd7f05Smrg    int total_count, typed_count;
106a3bd7f05Smrg
107444c061aSmrg    WIDGET_TO_APPCON(widget);
108444c061aSmrg
109444c061aSmrg    LOCK_APP(app);
110a3bd7f05Smrg    va_start(var, num_resources);
111444c061aSmrg    _XtCountVaList(var, &total_count, &typed_count);
112444c061aSmrg    va_end(var);
113444c061aSmrg
114a3bd7f05Smrg    va_start(var, num_resources);
115444c061aSmrg
116444c061aSmrg    _XtVaToTypedArgList(var, total_count, &args, &num_args);
117444c061aSmrg
118444c061aSmrg    _XtGetApplicationResources(widget, base, resources, num_resources,
119a3bd7f05Smrg                               NULL, 0, args, num_args);
120444c061aSmrg
121a3bd7f05Smrg    XtFree((XtPointer) args);
122444c061aSmrg
123444c061aSmrg    va_end(var);
124444c061aSmrg    UNLOCK_APP(app);
125444c061aSmrg}
126444c061aSmrg
127444c061aSmrgstatic void
128a3bd7f05SmrgGetTypedArg(Widget widget,
129a3bd7f05Smrg            XtTypedArgList typed_arg,
130a3bd7f05Smrg            XtResourceList resources,
131a3bd7f05Smrg            Cardinal num_resources)
132444c061aSmrg{
133a3bd7f05Smrg    String from_type = NULL;
134a3bd7f05Smrg    Cardinal from_size = 0;
135a3bd7f05Smrg    XrmValue from_val, to_val;
136a3bd7f05Smrg    register Cardinal i;
137a3bd7f05Smrg    Arg arg;
138a3bd7f05Smrg    XtPointer value;
139444c061aSmrg
140444c061aSmrg    /* note we presume that the XtResourceList to be un-compiled */
141444c061aSmrg
142444c061aSmrg    for (i = 0; i < num_resources; i++) {
143a3bd7f05Smrg        if (StringToName(typed_arg->name) ==
144a3bd7f05Smrg            StringToName(resources[i].resource_name)) {
145444c061aSmrg            from_type = resources[i].resource_type;
146a3bd7f05Smrg            from_size = resources[i].resource_size;
147444c061aSmrg            break;
148444c061aSmrg        }
149444c061aSmrg    }
150444c061aSmrg
151444c061aSmrg    if (i == num_resources) {
152a3bd7f05Smrg        XtAppWarningMsg(XtWidgetToApplicationContext(widget),
153a3bd7f05Smrg                        "unknownType", XtNxtGetTypedArg, XtCXtToolkitError,
154a3bd7f05Smrg                        "Unable to find type of resource for conversion",
155a3bd7f05Smrg                        NULL, NULL);
156a3bd7f05Smrg        return;
157444c061aSmrg    }
158444c061aSmrg
159444c061aSmrg    value = ALLOCATE_LOCAL(from_size);
160a3bd7f05Smrg    if (value == NULL)
161a3bd7f05Smrg        _XtAllocError(NULL);
162444c061aSmrg    XtSetArg(arg, typed_arg->name, value);
163444c061aSmrg    XtGetValues(widget, &arg, 1);
164444c061aSmrg
165444c061aSmrg    from_val.size = from_size;
166a3bd7f05Smrg    from_val.addr = (XPointer) value;
167a3bd7f05Smrg    to_val.addr = (XPointer) typed_arg->value;
1680568f49bSmrg    to_val.size = (unsigned) typed_arg->size;
169444c061aSmrg
170444c061aSmrg    if (!XtConvertAndStore(widget, from_type, &from_val,
171a3bd7f05Smrg                           typed_arg->type, &to_val)) {
172a3bd7f05Smrg        if (to_val.size > (unsigned) typed_arg->size) {
173a3bd7f05Smrg            String params[2];
174a3bd7f05Smrg            Cardinal num_params = 2;
175a3bd7f05Smrg
176a3bd7f05Smrg            params[0] = typed_arg->type;
177a3bd7f05Smrg            params[1] = XtName(widget);
178a3bd7f05Smrg            XtAppWarningMsg(XtWidgetToApplicationContext(widget),
179a3bd7f05Smrg                            "insufficientSpace", XtNxtGetTypedArg,
180a3bd7f05Smrg                            XtCXtToolkitError,
181a3bd7f05Smrg                            "Insufficient space for converted type '%s' in widget '%s'",
182a3bd7f05Smrg                            params, &num_params);
183a3bd7f05Smrg        }
184a3bd7f05Smrg        else {
185a3bd7f05Smrg            String params[3];
186a3bd7f05Smrg            Cardinal num_params = 3;
187a3bd7f05Smrg
188a3bd7f05Smrg            params[0] = from_type;
189a3bd7f05Smrg            params[1] = typed_arg->type;
190a3bd7f05Smrg            params[2] = XtName(widget);
191a3bd7f05Smrg            XtAppWarningMsg(XtWidgetToApplicationContext(widget),
192a3bd7f05Smrg                            "conversionFailed", XtNxtGetTypedArg,
193a3bd7f05Smrg                            XtCXtToolkitError,
194a3bd7f05Smrg                            "Type conversion (%s to %s) failed for widget '%s'",
195a3bd7f05Smrg                            params, &num_params);
196a3bd7f05Smrg        }
197444c061aSmrg    }
198444c061aSmrg    DEALLOCATE_LOCAL(value);
199444c061aSmrg}
200444c061aSmrg
201444c061aSmrgstatic int
202a3bd7f05SmrgGetNestedArg(Widget widget,
203a3bd7f05Smrg             XtTypedArgList avlist,
204a3bd7f05Smrg             ArgList args,
205a3bd7f05Smrg             XtResourceList resources,
206a3bd7f05Smrg             Cardinal num_resources)
207444c061aSmrg{
208a3bd7f05Smrg    int count = 0;
209444c061aSmrg
210444c061aSmrg    for (; avlist->name != NULL; avlist++) {
211444c061aSmrg        if (avlist->type != NULL) {
212a3bd7f05Smrg            GetTypedArg(widget, avlist, resources, num_resources);
213a3bd7f05Smrg        }
214a3bd7f05Smrg        else if (strcmp(avlist->name, XtVaNestedList) == 0) {
215a3bd7f05Smrg            count += GetNestedArg(widget, (XtTypedArgList) avlist->value,
216a3bd7f05Smrg                                  args, resources, num_resources);
217a3bd7f05Smrg        }
218a3bd7f05Smrg        else {
219a3bd7f05Smrg            (args + count)->name = avlist->name;
220a3bd7f05Smrg            (args + count)->value = avlist->value;
221444c061aSmrg            ++count;
222444c061aSmrg        }
223444c061aSmrg    }
224444c061aSmrg
225a3bd7f05Smrg    return (count);
226444c061aSmrg}
227444c061aSmrg
228444c061aSmrgvoid
229444c061aSmrgXtVaGetValues(Widget widget, ...)
230444c061aSmrg{
231a3bd7f05Smrg    va_list var;
232a3bd7f05Smrg    String attr;
233a3bd7f05Smrg    ArgList args;
234a3bd7f05Smrg    XtTypedArg typed_arg;
235a3bd7f05Smrg    XtResourceList resources = (XtResourceList) NULL;
236a3bd7f05Smrg    Cardinal num_resources;
237a3bd7f05Smrg    int count, total_count, typed_count;
238a3bd7f05Smrg
239444c061aSmrg    WIDGET_TO_APPCON(widget);
240444c061aSmrg
241444c061aSmrg    LOCK_APP(app);
242a3bd7f05Smrg    va_start(var, widget);
243444c061aSmrg
244444c061aSmrg    _XtCountVaList(var, &total_count, &typed_count);
245444c061aSmrg
246444c061aSmrg    if (total_count != typed_count) {
247a3bd7f05Smrg        size_t limit = (size_t) (total_count - typed_count);
248a3bd7f05Smrg
249fdf6a26fSmrg        args = XtMallocArray((Cardinal) limit, (Cardinal) sizeof(Arg));
250444c061aSmrg    }
251a3bd7f05Smrg    else
252a3bd7f05Smrg        args = NULL;            /* for lint; really unused */
253444c061aSmrg    va_end(var);
254444c061aSmrg
2550568f49bSmrg    if (args != NULL) {
256a3bd7f05Smrg        va_start(var, widget);
257a3bd7f05Smrg        for (attr = va_arg(var, String), count = 0; attr != NULL;
258a3bd7f05Smrg             attr = va_arg(var, String)) {
259a3bd7f05Smrg            if (strcmp(attr, XtVaTypedArg) == 0) {
260a3bd7f05Smrg                typed_arg.name = va_arg(var, String);
261a3bd7f05Smrg                typed_arg.type = va_arg(var, String);
262a3bd7f05Smrg                typed_arg.value = va_arg(var, XtArgVal);
263a3bd7f05Smrg                typed_arg.size = va_arg(var, int);
264a3bd7f05Smrg
265a3bd7f05Smrg                if (resources == NULL) {
266a3bd7f05Smrg                    XtGetResourceList(XtClass(widget), &resources,
267a3bd7f05Smrg                                      &num_resources);
268a3bd7f05Smrg                }
269a3bd7f05Smrg
270a3bd7f05Smrg                GetTypedArg(widget, &typed_arg, resources, num_resources);
271a3bd7f05Smrg            }
272a3bd7f05Smrg            else if (strcmp(attr, XtVaNestedList) == 0) {
273a3bd7f05Smrg                if (resources == NULL) {
274a3bd7f05Smrg                    XtGetResourceList(XtClass(widget), &resources,
275a3bd7f05Smrg                                      &num_resources);
276a3bd7f05Smrg                }
277a3bd7f05Smrg
278a3bd7f05Smrg                count += GetNestedArg(widget, va_arg(var, XtTypedArgList),
279a3bd7f05Smrg                                      (args + count), resources, num_resources);
280a3bd7f05Smrg            }
281a3bd7f05Smrg            else {
282a3bd7f05Smrg                args[count].name = attr;
283a3bd7f05Smrg                args[count].value = va_arg(var, XtArgVal);
284a3bd7f05Smrg                count++;
285a3bd7f05Smrg            }
286a3bd7f05Smrg        }
287a3bd7f05Smrg        va_end(var);
288444c061aSmrg    }
289444c061aSmrg
290a3bd7f05Smrg    XtFree((XtPointer) resources);
291444c061aSmrg
2920568f49bSmrg    if (args != NULL) {
293a3bd7f05Smrg        XtGetValues(widget, args, (Cardinal) count);
294a3bd7f05Smrg        XtFree((XtPointer) args);
295444c061aSmrg    }
296444c061aSmrg    UNLOCK_APP(app);
297444c061aSmrg}
298444c061aSmrg
299444c061aSmrgvoid
300a3bd7f05SmrgXtVaGetSubvalues(XtPointer base,
301a3bd7f05Smrg                 XtResourceList resources,
302a3bd7f05Smrg                 Cardinal num_resources,
303a3bd7f05Smrg                 ...)
304444c061aSmrg{
305a3bd7f05Smrg    va_list var;
306a3bd7f05Smrg    ArgList args;
307a3bd7f05Smrg    Cardinal num_args;
308a3bd7f05Smrg    int total_count, typed_count;
309444c061aSmrg
310a3bd7f05Smrg    va_start(var, num_resources);
311444c061aSmrg
312444c061aSmrg    _XtCountVaList(var, &total_count, &typed_count);
313444c061aSmrg
314444c061aSmrg    if (typed_count != 0) {
315a3bd7f05Smrg        XtWarning
316a3bd7f05Smrg            ("XtVaTypedArg is an invalid argument to XtVaGetSubvalues()\n");
317444c061aSmrg    }
318444c061aSmrg    va_end(var);
319444c061aSmrg
320a3bd7f05Smrg    va_start(var, num_resources);
321a3bd7f05Smrg    _XtVaToArgList((Widget) NULL, var, total_count, &args, &num_args);
322444c061aSmrg    va_end(var);
323444c061aSmrg
324444c061aSmrg    XtGetSubvalues(base, resources, num_resources, args, num_args);
325444c061aSmrg
326a3bd7f05Smrg    XtFree((XtPointer) args);
327444c061aSmrg}
328