2 * Copyright (c) 1998 by The XFree86 Project, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
17 * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * Except as contained in this notice, the name of the XFree86 Project shall
23 * not be used in advertising or otherwise to promote the sale, use or other
24 * dealings in this Software without prior written authorization from the
32 #include <X11/IntrinsicP.h>
33 #include <X11/StringDefs.h>
34 #include <X11/Xmu/CharSet.h>
35 #include <X11/Xmu/SysUtil.h>
36 #include <X11/Xaw/Simple.h>
37 #include <X11/Xaw/XawInit.h>
45 #define done(type, value) \
47 if (toVal->addr != NULL) \
49 if (toVal->size < sizeof(type)) \
51 toVal->size = sizeof(type); \
54 *(type *)(toVal->addr) = (value); \
58 static type static_val; \
60 static_val = (value); \
61 toVal->addr = (XPointer)&static_val; \
63 toVal->size = sizeof(type); \
67 #define string_done(value) \
69 if (toVal->addr != NULL) \
71 if (toVal->size < size) \
76 strcpy((char *)toVal->addr, (value)); \
79 toVal->addr = (XPointer)(value); \
87 static Boolean _XawCvtAtomToString(Display*, XrmValue*, Cardinal*,
88 XrmValue*, XrmValue*, XtPointer*);
89 static Boolean _XawCvtBooleanToString(Display*, XrmValue*, Cardinal*,
90 XrmValue*, XrmValue*, XtPointer*);
91 static Boolean _XawCvtBoolToString(Display*, XrmValue*, Cardinal*,
92 XrmValue*, XrmValue*, XtPointer*);
93 static Boolean _XawCvtCARD32ToString(Display*, XrmValue*, Cardinal*,
94 XrmValue*, XrmValue*, XtPointer*);
95 static Boolean _XawCvtCardinalToString(Display*, XrmValue*, Cardinal*,
96 XrmValue*, XrmValue*, XtPointer*);
97 static Boolean _XawCvtDimensionToString(Display*, XrmValue*, Cardinal*,
98 XrmValue*, XrmValue*, XtPointer*);
99 static Boolean _XawCvtDisplayListToString(Display*, XrmValue*, Cardinal*,
100 XrmValue*, XrmValue*, XtPointer*);
101 static Boolean _XawCvtFontStructToString(Display*, XrmValue*, Cardinal*,
102 XrmValue*, XrmValue*, XtPointer*);
103 static Boolean _XawCvtIntToString(Display*, XrmValue*, Cardinal*,
104 XrmValue*, XrmValue*, XtPointer*);
105 static Boolean _XawCvtPixelToString(Display*, XrmValue*, Cardinal*,
106 XrmValue*, XrmValue*, XtPointer*);
107 static Boolean _XawCvtPixmapToString(Display*, XrmValue*, Cardinal*,
108 XrmValue*, XrmValue*, XtPointer*);
109 static Boolean _XawCvtShortToString(Display*, XrmValue*, Cardinal*,
110 XrmValue*, XrmValue*, XtPointer*);
111 static Boolean _XawCvtPositionToString(Display*, XrmValue*, Cardinal*,
112 XrmValue*, XrmValue*, XtPointer*);
113 static Boolean _XawCvtStringToDisplayList(Display*, XrmValue*, Cardinal*,
114 XrmValue*, XrmValue*, XtPointer*);
115 static Boolean _XawCvtStringToPixmap(Display*, XrmValue*, Cardinal*,
116 XrmValue*, XrmValue*, XtPointer*);
117 static Boolean _XawCvtUnsignedCharToString(Display*, XrmValue*, Cardinal*,
118 XrmValue*, XrmValue*, XtPointer*);
119 static void TypeToStringNoArgsWarning(Display*, String);
124 static XtConvertArgRec PixelArgs[] = {
125 {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.colormap),
129 static XtConvertArgRec DLArgs[] = {
130 {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.screen),
132 {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.colormap),
134 {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.depth),
139 static String XtCToolkitError = "ToolkitError";
140 static String XtNconversionError = "conversionError";
143 static String XtNwrongParameters = "wrongParameters";
149 XawInitializeDefaultConverters(void)
151 static Boolean first_time = True;
153 if (first_time == False)
158 /* Replace with more apropriate converters */
159 XtSetTypeConverter(XtRCallback, XtRString, _XawCvtCARD32ToString,
160 NULL, 0, XtCacheNone, NULL);
161 XtSetTypeConverter(XtRColormap, XtRString, _XawCvtCARD32ToString,
162 NULL, 0, XtCacheNone, NULL);
163 XtSetTypeConverter(XtRFunction, XtRString, _XawCvtCARD32ToString,
164 NULL, 0, XtCacheNone, NULL);
165 XtSetTypeConverter(XtRPointer, XtRString, _XawCvtCARD32ToString,
166 NULL, 0, XtCacheNone, NULL);
167 XtSetTypeConverter(XtRScreen, XtRString, _XawCvtCARD32ToString,
168 NULL, 0, XtCacheNone, NULL);
169 XtSetTypeConverter(XtRStringArray, XtRString, _XawCvtCARD32ToString,
170 NULL, 0, XtCacheNone, NULL);
171 XtSetTypeConverter(XtRVisual, XtRString, _XawCvtCARD32ToString,
172 NULL, 0, XtCacheNone, NULL);
173 XtSetTypeConverter(XtRWidget, XtRString, _XawCvtCARD32ToString,
174 NULL, 0, XtCacheNone, NULL);
175 XtSetTypeConverter(XtRWidgetList, XtRString, _XawCvtCARD32ToString,
176 NULL, 0, XtCacheNone, NULL);
177 XtSetTypeConverter(XtRWindow, XtRString, _XawCvtCARD32ToString,
178 NULL, 0, XtCacheNone, NULL);
180 XtSetTypeConverter(XtRAtom, XtRString, _XawCvtAtomToString,
181 NULL, 0, XtCacheNone, NULL);
182 XtSetTypeConverter(XtRBool, XtRString, _XawCvtBoolToString,
183 NULL, 0, XtCacheNone, NULL);
184 XtSetTypeConverter(XtRBoolean, XtRString, _XawCvtBooleanToString,
185 NULL, 0, XtCacheNone, NULL);
186 XtSetTypeConverter(XtRCardinal, XtRString, _XawCvtCardinalToString,
187 NULL, 0, XtCacheNone, NULL);
188 XtSetTypeConverter(XtRDimension, XtRString, _XawCvtDimensionToString,
189 NULL, 0, XtCacheNone, NULL);
190 XtSetTypeConverter(XawRDisplayList, XtRString, _XawCvtDisplayListToString,
191 NULL, 0, XtCacheNone, NULL);
192 XtSetTypeConverter(XtRFontStruct, XtRString, _XawCvtFontStructToString,
193 NULL, 0, XtCacheNone, NULL);
194 XtSetTypeConverter(XtRInt, XtRString, _XawCvtIntToString,
195 NULL, 0, XtCacheNone, NULL);
196 XtSetTypeConverter(XtRPixel, XtRString, _XawCvtPixelToString,
197 &PixelArgs[0], XtNumber(PixelArgs), XtCacheNone, NULL);
198 XtSetTypeConverter(XtRPixmap, XtRString, _XawCvtPixmapToString,
199 &DLArgs[0], XtNumber(DLArgs), XtCacheNone, NULL);
200 XtSetTypeConverter(XtRPosition, XtRString, _XawCvtPositionToString,
201 NULL, 0, XtCacheNone, NULL);
202 XtSetTypeConverter(XtRShort, XtRString, _XawCvtShortToString,
203 NULL, 0, XtCacheNone, NULL);
204 XtSetTypeConverter(XtRString, XawRDisplayList, _XawCvtStringToDisplayList,
205 &DLArgs[0], XtNumber(DLArgs), XtCacheAll, NULL);
206 XtSetTypeConverter(XtRString, XtRPixmap, _XawCvtStringToPixmap,
207 &DLArgs[0], XtNumber(DLArgs), XtCacheAll, NULL);
208 XtSetTypeConverter(XtRUnsignedChar, XtRString, _XawCvtUnsignedCharToString,
209 NULL, 0, XtCacheNone, NULL);
214 XawTypeToStringWarning(Display *dpy, String type)
220 XmuSnprintf(fname, sizeof(fname), "cvt%sToString", type);
224 XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
225 XtNconversionError, fname, XtCToolkitError,
226 "Cannot convert %s to String",
227 params, &num_params);
232 TypeToStringNoArgsWarning(Display *dpy, String type)
238 XmuSnprintf(fname, sizeof(fname), "cvt%sToString", type);
242 XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
243 XtNconversionError, fname,
245 "%s to String conversion needs no extra arguments",
246 params, &num_params);
251 _XawCvtBooleanToString(Display *dpy, XrmValue *args, Cardinal *num_args,
252 XrmValue *fromVal, XrmValue *toVal,
253 XtPointer *converter_data)
255 static char buffer[6];
259 TypeToStringNoArgsWarning(dpy, XtRBoolean);
261 XmuSnprintf(buffer, sizeof(buffer), "%s",
262 *(Boolean *)fromVal->addr ? XtEtrue : XtEfalse);
263 size = strlen(buffer) + 1;
270 _XawCvtBoolToString(Display *dpy, XrmValue *args, Cardinal *num_args,
271 XrmValue *fromVal, XrmValue *toVal,
272 XtPointer *converter_data)
274 static char buffer[6];
278 TypeToStringNoArgsWarning(dpy, XtRBool);
280 XmuSnprintf(buffer, sizeof(buffer), "%s",
281 *(Bool *)fromVal->addr ? XtEtrue : XtEfalse);
282 size = strlen(buffer) + 1;
289 _XawCvtPositionToString(Display *dpy, XrmValue *args, Cardinal *num_args,
290 XrmValue *fromVal, XrmValue *toVal,
291 XtPointer *converter_data)
293 static char buffer[7];
297 TypeToStringNoArgsWarning(dpy, XtRPosition);
299 XmuSnprintf(buffer, sizeof(buffer), "%d", *(Position *)fromVal->addr);
300 size = strlen(buffer) + 1;
307 _XawCvtShortToString(Display *dpy, XrmValue *args, Cardinal *num_args,
308 XrmValue *fromVal, XrmValue *toVal,
309 XtPointer *converter_data)
311 static char buffer[7];
315 TypeToStringNoArgsWarning(dpy, XtRShort);
317 XmuSnprintf(buffer, sizeof(buffer), "%d", *(short *)fromVal->addr);
318 size = strlen(buffer) + 1;
325 _XawCvtDimensionToString(Display *dpy, XrmValue *args, Cardinal *num_args,
326 XrmValue *fromVal, XrmValue *toVal,
327 XtPointer *converter_data)
329 static char buffer[6];
333 TypeToStringNoArgsWarning(dpy, XtRDimension);
335 XmuSnprintf(buffer, sizeof(buffer), "%u", *(Dimension *)fromVal->addr);
336 size = strlen(buffer) + 1;
343 _XawCvtCARD32ToString(Display *dpy, XrmValue *args, Cardinal *num_args,
344 XrmValue *fromVal, XrmValue *toVal,
345 XtPointer *converter_data)
347 static char buffer[11];
351 TypeToStringNoArgsWarning(dpy, "CARD32");
353 XmuSnprintf(buffer, sizeof(buffer), "0x%08hx", *(int *)fromVal->addr);
354 size = strlen(buffer) + 1;
361 _XawCvtIntToString(Display *dpy, XrmValue *args, Cardinal *num_args,
362 XrmValue *fromVal, XrmValue *toVal,
363 XtPointer *converter_data)
365 static char buffer[12];
369 TypeToStringNoArgsWarning(dpy, XtRInt);
371 XmuSnprintf(buffer, sizeof(buffer), "%d", *(int *)fromVal->addr);
372 size = strlen(buffer) + 1;
379 _XawCvtCardinalToString(Display *dpy, XrmValue *args, Cardinal *num_args,
380 XrmValue *fromVal, XrmValue *toVal,
381 XtPointer *converter_data)
383 static char buffer[11];
387 TypeToStringNoArgsWarning(dpy, XtRCardinal);
389 XmuSnprintf(buffer, sizeof(buffer), "%u", *(Cardinal *)fromVal->addr);
390 size = strlen(buffer) + 1;
397 _XawCvtAtomToString(Display *dpy, XrmValue *args, Cardinal *num_args,
398 XrmValue *fromVal, XrmValue *toVal,
399 XtPointer *converter_data)
401 static char *buffer = NULL;
402 static char *nullatom = "NULL";
407 TypeToStringNoArgsWarning(dpy, XtRAtom);
409 if (buffer && buffer != nullatom)
412 atom = *(Atom *)fromVal[0].addr;
415 else if ((buffer = XGetAtomName(dpy, *(Atom *)fromVal[0].addr)) == NULL)
417 XawTypeToStringWarning(dpy, XtRAtom);
419 toVal->size = sizeof(String);
423 size = strlen(buffer) + 1;
430 _XawCvtPixelToString(Display *dpy, XrmValue *args, Cardinal *num_args,
431 XrmValue *fromVal, XrmValue *toVal,
432 XtPointer *converter_data)
434 static char buffer[19];
441 XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
442 XtNwrongParameters, "cvtPixelToString",
444 "Pixel to String conversion needs colormap argument",
449 colormap = *(Colormap *)args[0].addr;
450 color.pixel = *(Pixel *)fromVal->addr;
453 * If we know the visual type, we can calculate the xcolor
454 * without asking Xlib.
456 XQueryColor(dpy, colormap, &color);
457 XmuSnprintf(buffer, sizeof(buffer), "rgb:%04hx/%04hx/%04hx",
458 color.red, color.green, color.blue);
459 size = strlen(buffer) + 1;
466 _XawCvtFontStructToString(Display *dpy, XrmValue *args, Cardinal *num_args,
467 XrmValue *fromVal, XrmValue *toVal,
468 XtPointer *converter_data)
470 static char buffer[128];
476 TypeToStringNoArgsWarning(dpy, XtRFontStruct);
478 if ((atom = XInternAtom(dpy, "FONT", True)) == None)
483 if (XGetFontProperty(*(XFontStruct **)fromVal->addr, atom, &value))
485 char *tmp = XGetAtomName(dpy, value);
489 XmuSnprintf(buffer, sizeof(buffer), "%s", tmp);
501 XawTypeToStringWarning(dpy, XtRFontStruct);
508 _XawCvtUnsignedCharToString(Display *dpy, XrmValue *args, Cardinal *num_args,
509 XrmValue *fromVal, XrmValue *toVal,
510 XtPointer *converter_data)
512 static char buffer[4];
516 TypeToStringNoArgsWarning(dpy, XtRUnsignedChar);
518 XmuSnprintf(buffer, sizeof(buffer), "%u",
519 *(unsigned char *)fromVal->addr);
520 size = strlen(buffer) + 1;
527 _XawCvtStringToDisplayList(Display *dpy, XrmValue *args, Cardinal *num_args,
528 XrmValue *fromVal, XrmValue *toVal,
529 XtPointer *converter_data)
531 XawDisplayList *dlist;
539 XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
540 XtNwrongParameters, "cvtStringToDisplayList",
542 "String to DisplayList conversion needs screen, "
543 "colormap, and depth arguments",
548 screen = *(Screen **)args[0].addr;
549 colormap = *(Colormap *)args[1].addr;
550 depth = *(int *)args[2].addr;
552 commands = (String)(fromVal[0].addr);
554 dlist = XawCreateDisplayList(commands, screen, colormap, depth);
558 XtDisplayStringConversionWarning(dpy, (String)fromVal->addr,
561 toVal->size = sizeof(XawDisplayList*);
565 done(XawDisplayList*, dlist);
570 _XawCvtDisplayListToString(Display *dpy, XrmValue *args, Cardinal *num_args,
571 XrmValue *fromVal, XrmValue *toVal,
572 XtPointer *converter_data)
578 TypeToStringNoArgsWarning(dpy, XawRDisplayList);
580 buffer = XawDisplayListString(*(XawDisplayList **)(fromVal[0].addr));
581 size = strlen(buffer) + 1;
588 _XawCvtStringToPixmap(Display *dpy, XrmValue *args, Cardinal *num_args,
589 XrmValue *fromVal, XrmValue *toVal,
590 XtPointer *converter_data)
592 XawPixmap *xaw_pixmap;
601 XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
602 XtNwrongParameters, "cvtStringToPixmap",
604 "String to Pixmap conversion needs screen, "
605 "colormap, and depth arguments",
610 screen = *(Screen **)args[0].addr;
611 colormap = *(Colormap *)args[1].addr;
612 depth = *(int *)args[2].addr;
614 name = (String)(fromVal[0].addr);
616 if (XmuCompareISOLatin1(name, "None") == 0)
618 else if (XmuCompareISOLatin1(name, "ParentRelative") == 0)
619 pixmap = ParentRelative;
620 else if (XmuCompareISOLatin1(name, "XtUnspecifiedPixmap") == 0)
621 pixmap = XtUnspecifiedPixmap;
624 xaw_pixmap = XawLoadPixmap(name, screen, colormap, depth);
627 XtDisplayStringConversionWarning(dpy, (String)fromVal->addr,
629 toVal->addr = (XtPointer)XtUnspecifiedPixmap;
630 toVal->size = sizeof(Pixmap);
634 pixmap = xaw_pixmap->pixmap;
637 done(Pixmap, pixmap);
642 _XawCvtPixmapToString(Display *dpy, XrmValue *args, Cardinal *num_args,
643 XrmValue *fromVal, XrmValue *toVal,
644 XtPointer *converter_data)
646 XawPixmap *xaw_pixmap;
651 String buffer = NULL;
656 XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
657 XtNwrongParameters, "cvtPixmapToString",
659 "Pixmap to String conversion needs screen, "
660 "colormap, and depth arguments",
665 screen = *(Screen **)args[0].addr;
666 colormap = *(Colormap *)args[1].addr;
667 depth = *(int *)args[2].addr;
669 pixmap = *(Pixmap *)(fromVal[0].addr);
677 buffer = "ParentRelative";
679 case XtUnspecifiedPixmap:
680 buffer = "XtUnspecifiedPixmap";
683 xaw_pixmap = XawPixmapFromXPixmap(pixmap, screen, colormap, depth);
685 buffer = xaw_pixmap->name;
690 /* Bad Pixmap or Pixmap was not loaded by XawLoadPixmap() */
691 return (_XawCvtCARD32ToString(dpy, args, num_args, fromVal, toVal,
694 size = strlen(buffer) + 1;