Imported Upstream version 1.1.1
[platform/upstream/libXmu.git] / src / StrToWidg.c
1 /*
2
3 Copyright 1994, 1998  The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
24
25 */
26
27 /*
28  * XmuCvtStringToWidget
29  *
30  * static XtConvertArgRec parentCvtArgs[] = {
31  *   {XtBaseOffset, (XtPointer)XtOffset(Widget, core.parent), sizeof(Widget)},
32  * };
33  *
34  * matches the string against the name of the immediate children (normal
35  * or popup) of the parent.  If none match, compares string to classname
36  * & returns first match.  Case is significant.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41 #include <X11/IntrinsicP.h>
42 #include <X11/StringDefs.h>
43 #include <X11/ObjectP.h>
44 #include <X11/Xmu/Converters.h>
45
46 #define done(address, type) \
47 {                                               \
48   toVal->size = sizeof(type);                   \
49   toVal->addr = (XPointer)address;              \
50           return; \
51 }
52
53 /*ARGSUSED*/
54 void
55 XmuCvtStringToWidget(XrmValuePtr args, Cardinal *num_args,
56                      XrmValuePtr fromVal, XrmValuePtr toVal)
57 {
58     static Widget widget, *widgetP, parent;
59     XrmName name = XrmStringToName(fromVal->addr);
60     Cardinal i;
61
62     if (*num_args != 1)
63     {
64         i = 0;
65         XtErrorMsg("wrongParameters", "cvtStringToWidget", "xtToolkitError",
66                    "StringToWidget conversion needs parent arg", NULL, &i);
67     }
68
69     parent = *(Widget*)args[0].addr;
70     /* try to match names of normal children */
71   if (XtIsComposite(parent))
72     {
73         i = ((CompositeWidget)parent)->composite.num_children;
74         for (widgetP = ((CompositeWidget)parent)->composite.children;
75            i; i--, widgetP++)
76         if ((*widgetP)->core.xrm_name == name)
77           {
78                 widget = *widgetP;
79                 done(&widget, Widget);
80             }
81         }
82
83     /* try to match names of popup children */
84     i = parent->core.num_popups;
85   for (widgetP = parent->core.popup_list; i; i--, widgetP++)
86     if ((*widgetP)->core.xrm_name == name)
87       {
88             widget = *widgetP;
89             done(&widget, Widget);
90         }
91
92     /* try to match classes of normal children */
93   if (XtIsComposite(parent))
94     {
95         i = ((CompositeWidget)parent)->composite.num_children;
96         for (widgetP = ((CompositeWidget)parent)->composite.children;
97            i; i--, widgetP++)
98         if ((*widgetP)->core.widget_class->core_class.xrm_class == name)
99           {
100                 widget = *widgetP;
101                 done(&widget, Widget);
102             }
103         }
104
105     /* try to match classes of popup children */
106     i = parent->core.num_popups;
107   for (widgetP = parent->core.popup_list; i; i--, widgetP++)
108     if ((*widgetP)->core.widget_class->core_class.xrm_class == name)
109       {
110             widget = *widgetP;
111             done(&widget, Widget);
112         }
113
114     XtStringConversionWarning(fromVal->addr, XtRWidget);
115     toVal->addr = NULL;
116     toVal->size = 0;
117 }
118
119 #undef done
120
121 #define newDone(type, value) \
122         {                                                       \
123             if (toVal->addr != NULL) {                          \
124                 if (toVal->size < sizeof(type)) {               \
125                     toVal->size = sizeof(type);                 \
126                     return False;                               \
127                 }                                               \
128                 *(type*)(toVal->addr) = (value);                \
129             }                                                   \
130             else {                                              \
131                 static type static_val;                         \
132                 static_val = (value);                           \
133                 toVal->addr = (XtPointer)&static_val;           \
134             }                                                   \
135             toVal->size = sizeof(type);                         \
136             return True;                                        \
137         }
138
139
140 /*ARGSUSED*/
141 Boolean
142 XmuNewCvtStringToWidget(Display *dpy, XrmValue *args, Cardinal *num_args,
143                         XrmValue *fromVal, XrmValue *toVal,
144                         XtPointer *converter_data)
145 {
146     Widget *widgetP, parent;
147     XrmName name = XrmStringToName(fromVal->addr);
148     int i;
149
150     if (*num_args != 1)
151         XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
152                         "wrongParameters","cvtStringToWidget","xtToolkitError",
153                         "String To Widget conversion needs parent argument",
154                         (String *)NULL, (Cardinal *)NULL);
155
156     parent = *(Widget*)args[0].addr;
157     /* try to match names of normal children */
158   if (XtIsComposite(parent))
159     {
160         i = ((CompositeWidget)parent)->composite.num_children;
161         for (widgetP = ((CompositeWidget)parent)->composite.children;
162            i; i--, widgetP++)
163             if ((*widgetP)->core.xrm_name == name)
164                 newDone(Widget, *widgetP);
165         }
166
167     /* try to match names of popup children */
168     i = parent->core.num_popups;
169   for (widgetP = parent->core.popup_list; i; i--, widgetP++)
170         if ((*widgetP)->core.xrm_name == name)
171             newDone(Widget, *widgetP);
172
173     /* try to match classes of normal children */
174   if (XtIsComposite(parent))
175     {
176         i = ((CompositeWidget)parent)->composite.num_children;
177         for (widgetP = ((CompositeWidget)parent)->composite.children;
178            i; i--, widgetP++)
179             if ((*widgetP)->core.widget_class->core_class.xrm_class == name)
180                 newDone(Widget, *widgetP);
181         }
182
183     /* try to match classes of popup children */
184     i = parent->core.num_popups;
185   for (widgetP = parent->core.popup_list; i; i--, widgetP++)
186         if ((*widgetP)->core.widget_class->core_class.xrm_class == name)
187             newDone(Widget, *widgetP);
188
189     XtDisplayStringConversionWarning(dpy, (String)fromVal->addr, XtRWidget);
190   return (False);
191 }
192
193 /*ARGSUSED*/
194 Boolean
195 XmuCvtWidgetToString(Display *dpy, XrmValuePtr args, Cardinal *num_args,
196                      XrmValuePtr fromVal, XrmValuePtr toVal,
197                      XtPointer *data)
198 {
199   static String buffer;
200   Cardinal size;
201   Widget widget;
202
203   widget = *(Widget *)fromVal->addr;
204
205   if (widget)
206     buffer = XrmQuarkToString(widget->core.xrm_name);
207   else
208     buffer = "(null)";
209
210   size = strlen(buffer) + 1;
211   if (toVal->addr != NULL)
212     {
213       if (toVal->size < size)
214         {
215           toVal->size = size;
216           return (False);
217         }
218       strcpy((char *)toVal->addr, buffer);
219     }
220   else
221     toVal->addr = (XPointer)buffer;
222   toVal->size = sizeof(String);
223
224   return (True);
225 }