1 /**
2  *
3  * $Id: LTCvt.c,v 1.1 2004/08/28 19:22:44 dannybackx Exp $
4  *
5  * Copyright � 1995 Free Software Foundation, Inc.
6  * Copyright � 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 LessTif Development Team
7  *
8  * This file is part of the GNU LessTif Library.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the Free
22  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  **/
25 
26 static const char rcsid[] = "$Id: LTCvt.c,v 1.1 2004/08/28 19:22:44 dannybackx Exp $";
27 
28 #include <LTconfig.h>
29 
30 #ifdef NONSTANDARD_CONVERTERS
31 
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include <XmI/XmI.h>
36 #include <XmI/HashI.h>
37 #include <XmI/ImageCacheI.h>
38 #include <Xm/XmP.h>
39 #include <Xm/ScreenP.h>
40 #if XmVERSION >= 2
41 #include <Xm/XpmP.h>
42 #else
43 #include <XmI/XmXpm.h>
44 #endif
45 
46 #include <XmI/DebugUtil.h>
47 
48 
49 static char *_search_path = NULL;
50 
51 /* Prototypes to ensure proper type for the definitions below: */
52 static Boolean
53 _XmNSECvtStringToBitmap (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
54 static Boolean
55 _XmNSECvtStringToPixmap (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
56 static Boolean
57 _XmNSECvtShapeStyleToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
58 static Boolean
59 _XmNSECvtBooleanToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
60 static Boolean
61 _XmNSECvtBoolToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
62 static Boolean
63 _XmNSECvtAttachmentToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
64 static Boolean
65 _XmNSECvtPositionToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
66 static Boolean
67 _XmNSECvtCardinalToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
68 static Boolean
69 _XmNSECvtPixelToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
70 static Boolean
71 _XmNSECvtColorToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
72 static Boolean
73 _XmNSECvtUnsignedCharToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
74 static Boolean
75 _XmNSECvtDimensionToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
76 static Boolean
77 _XmNSECvtXmStringToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
78 static Boolean
79 _XmNSECvtWidgetToString (Display*, XrmValue*, Cardinal*, XrmValue*, XrmValue*, XtPointer*);
80 
81 
82 /*
83  * _XmNSEGetPixmap is not part of the OSF/Motif API.
84  * This routine is used with the string to pixmap converter
85  * routine.  It does a lookup into the cache based on the name
86  * of the pixmap and also where the foreground, background
87  * and depth are equal to zero.
88  */
89 static Pixmap
_XmNSEGetPixmap(Screen * screen,char * fname)90 _XmNSEGetPixmap(Screen *screen, char *fname)
91 {
92     static Colormap _cmap;
93 
94 #if XmVERSION >= 2
95     XpmAttributes xpm_attrib;
96 #else
97     _LtXpmAttributes xpm_attrib;
98 #endif
99     Pixmap pmap;
100     Pixmap mask;
101 
102     Display *dpy;
103     Window w;
104 
105     char *pathname_to_pixmap = NULL;
106 
107     dpy = DisplayOfScreen(screen);
108     w = RootWindowOfScreen(screen);
109 
110     /* initialize colormap if it hasn't been done */
111     if (_cmap == (Colormap)NULL)
112     {
113 	XWindowAttributes w_attrib;
114 	XGetWindowAttributes(dpy, w, &w_attrib);
115 	_cmap = w_attrib.colormap;
116     }
117 
118     /*
119      * Set the foreground, background and depth all to zero
120      * for now.
121      */
122     pmap = XmGetPixmapByDepth(screen, fname, 0, 0, 0);
123     if (pmap != XmUNSPECIFIED_PIXMAP)
124     {
125 	return pmap;
126     }
127 
128     if (_search_path == NULL)
129     {
130 	_LTCreateSearchPath();
131     }
132 
133     /*
134      * Attempt to find pixmap in search_path
135      * if an absolute path was not given.
136      */
137     if (fname != NULL && fname[0] == '/')
138     {
139 	pathname_to_pixmap = XtNewString(fname);
140     }
141     else
142     {
143 	SubstitutionRec sub;
144 
145 	sub.match = 'B';
146 	sub.substitution = fname;
147 
148 	pathname_to_pixmap = XtResolvePathname(dpy,
149 					       "bitmaps",
150 					       NULL,
151 					       NULL,
152 					       _search_path,
153 					       &sub,
154 					       1,
155 					       NULL);
156     }
157 
158     if (pathname_to_pixmap == NULL || strlen(pathname_to_pixmap) == 0)
159     {
160 	return XmUNSPECIFIED_PIXMAP;
161     }
162 
163 #if XmVERSION >= 2
164     xpm_attrib.colormap = _cmap;
165     xpm_attrib.closeness = 40000;
166     xpm_attrib.valuemask = XpmSize | XpmReturnPixels
167 	| XpmColormap | XpmCloseness;
168 
169     if (XpmReadFileToPixmap(dpy, w, pathname_to_pixmap,
170 			       &pmap, &mask,
171 			       &xpm_attrib) == XpmSuccess)
172 #else
173     xpm_attrib.colormap = _cmap;
174     xpm_attrib.closeness = 40000;
175     xpm_attrib.valuemask = _LtXpmSize | _LtXpmReturnPixels
176 	| _LtXpmColormap | _LtXpmCloseness;
177 
178     if (_LtXpmReadFileToPixmap(dpy, w, pathname_to_pixmap,
179 			       &pmap, &mask,
180 			       &xpm_attrib) == _LtXpmSuccess)
181 #endif
182     {
183 	_LTAddPixmapToCache(fname, pmap, screen, 0, 0, 0, 0, 0, 0, 0);
184 	/* FIX ME! */
185     }
186     else
187     {
188 	/* could not find it so lets return it as unspecified */
189 	pmap = XmUNSPECIFIED_PIXMAP;
190     }
191 
192     XtFree(pathname_to_pixmap);
193 
194     return pmap;
195 }
196 
197 /*
198  * _XmNSECvtStringToPixmap is not part of the OSF/Motif API.
199  * This routine is used to convert a string to a pixmap.
200  * This is done by calling the _XmNSEGetPixmap routine with the
201  * supplied string.
202  */
203 static Boolean
_XmNSECvtStringToPixmap(Display * dpy,XrmValue * args,Cardinal * num_args,XrmValue * from,XrmValue * to,XtPointer * converter_data)204 _XmNSECvtStringToPixmap(Display *dpy,
205 		        XrmValue *args,
206 		        Cardinal *num_args,
207 		        XrmValue *from,
208 		        XrmValue *to,
209 		        XtPointer *converter_data)
210 {
211     static Pixmap _pmap;
212     Screen *screen;
213     char *name;
214 
215 	DEBUGOUT(_LtDebug(__FILE__, NULL, "_XmNSECvtStringToPixmap()\n"));
216 
217     if (_XmGetDefaultDisplay() == NULL)
218     {
219 	return False;
220     }
221 
222     if (*num_args != 1)
223     {
224 	XtWarningMsg("wrongParameters",
225 		     "cvtStringToPixmap",
226 		     "XtToolkitError",
227 		     "String to Pixmap conversion needs screen argument",
228 		     (String *)NULL,
229 		     (Cardinal *)NULL);
230     }
231 
232     /* get arguments */
233     screen = *((Screen **)args[0].addr);
234     name = (char *)from->addr;
235 
236     /* over kill check */
237     if (name == NULL
238 	|| strcmp(name, "None") == 0
239 	|| strcmp(name, "XmUNSPECIFIED_PIXMAP") == 0)
240     {
241 	_pmap = XmUNSPECIFIED_PIXMAP;
242     }
243     else
244     {
245 	_pmap = _XmNSEGetPixmap(screen, name);
246     }
247 
248     if (to->addr == NULL)
249     {
250 	to->addr = (XPointer)&_pmap;
251 	to->size = sizeof(Pixmap);
252     }
253     else
254     {
255 	if (to->size >= sizeof(Pixmap))
256 	{
257 	    *((Pixmap *)to->addr) = _pmap;
258 	    to->size = sizeof(Pixmap);
259 	}
260 	else
261 	{
262 	    XtDisplayStringConversionWarning(dpy, (char *)from->addr,
263 					     XmRPixmap);
264 	}
265     }
266 
267     return True;
268 }
269 
270 /*
271  * _XmNSECvtStringToPixmap is not part of the OSF/Motif API.
272  * This routine is used to convert a string to a pixmap.
273  * This is done by calling the _XmNSEGetPixmap routine with the
274  * supplied string.
275  */
276 static Boolean
_XmNSECvtStringToBitmap(Display * dpy,XrmValue * args,Cardinal * num_args,XrmValue * from,XrmValue * to,XtPointer * converter_data)277 _XmNSECvtStringToBitmap(Display *dpy,
278 		        XrmValue *args,
279 		        Cardinal *num_args,
280 		        XrmValue *from,
281 		        XrmValue *to,
282 		        XtPointer *converter_data)
283 {
284 	static Pixmap _pmap;
285 	Screen *screen;
286 	char *name;
287 
288 	DEBUGOUT(_LtDebug(__FILE__, NULL, "_XmNSECvtStringToBitmap(from=%s) called\n",
289 		(char *)from->addr));
290 
291 	if (_XmGetDefaultDisplay() == NULL) {
292 		return False;
293 	}
294 
295 	if (*num_args != 1) {
296 		XtWarningMsg("wrongParameters", "cvtStringToBitmap", "XtToolkitError",
297 			"String to Bitmap conversion needs screen argument",
298 			(String *)NULL,
299 			(Cardinal *)NULL);
300 	}
301 
302 	/* get arguments */
303 	screen = *((Screen **)args[0].addr);
304 	name = (char *)from->addr;
305 
306 	/* over kill check */
307 	if (name == NULL || strcmp(name, "None") == 0
308 			|| strcmp(name, "XmUNSPECIFIED_PIXMAP") == 0) {
309 		_pmap = XmUNSPECIFIED_PIXMAP;
310 	} else {
311 		fprintf(stderr, "_XmNSECvtStringToBitmap(from=%s) called\n",
312 			(char *)from->addr);
313 		_pmap = XmUNSPECIFIED_PIXMAP;		/* FIX ME */
314 
315 		return False;
316 /*		_pmap = _XmNSEGetPixmap(screen, name);	*/
317 	}
318 
319 	if (to->addr == NULL) {
320 		to->addr = (XPointer)&_pmap;
321 		to->size = sizeof(Pixmap);
322 	} else {
323 		if (to->size >= sizeof(Pixmap)) {
324 			*((Pixmap *)to->addr) = _pmap;
325 			to->size = sizeof(Pixmap);
326 		} else {
327 			XtDisplayStringConversionWarning(dpy, (char *)from->addr, XmRPixmap);
328 		}
329 	}
330 
331 	return True;
332 }
333 
334 /* Begin stuff copied from X11/Xmu/Converters.h */
335 #define XtRShapeStyle "ShapeStyle"
336 #define XtERectangle "Rectangle"
337 #define XtEOval "Oval"
338 #define XtEEllipse "Ellipse"
339 #define XtERoundedRectangle "RoundedRectangle"
340 
341 #define XmuShapeRectangle 1
342 #define XmuShapeOval 2
343 #define XmuShapeEllipse 3
344 #define XmuShapeRoundedRectangle 4
345 /* End stuff copied from X11/Xmu/Converters.h */
346 
347 #ifndef	XtCXtToolkitError
348 #define	XtCXtToolkitError	"XtToolkitError"
349 #endif
350 
351 static String XtNwrongParameters = "wrongParameters";
352 
353 typedef struct
354 {
355     int value;
356     char *name;
357     int size;
358 } ConversionMap;
359 
360 
361 static ConversionMap boolean_map[] =
362 {
363     {True, XtEtrue, sizeof(XtEtrue)},
364     {False, XtEfalse, sizeof(XtEfalse)},
365     {True, XtEon, sizeof(XtEon)},
366     {False, XtEoff, sizeof(XtEoff)},
367     {True, XtEyes, sizeof(XtEyes)},
368     {False, XtEno, sizeof(XtEno)},
369 };
370 
371 
372 static ConversionMap shape_style_map[] =
373 {
374     {XmuShapeRectangle, XtERectangle, sizeof(XtERectangle)},
375     {XmuShapeRectangle, "ShapeRectangle", sizeof("ShapeRectangle")},
376     {XmuShapeOval, XtEOval, sizeof(XtEOval)},
377     {XmuShapeOval, "ShapeOval", sizeof("ShapeOval")},
378     {XmuShapeEllipse, "ShapeEllipse", sizeof("ShapeEllipse")},
379     {XmuShapeEllipse, XtEEllipse, sizeof(XtEEllipse)},
380     {XmuShapeRoundedRectangle, "ShapeRoundedRectangle",
381      sizeof("ShapeRoundedRectangle")},
382     {XmuShapeRoundedRectangle, XtERoundedRectangle,
383      sizeof(XtERoundedRectangle)},
384 };
385 
386 
387 #define	string_convert_done(value) \
388 	{							\
389 	    if (toVal->addr != NULL) {				\
390 		if (toVal->size < size) {		        \
391 		    toVal->size = size;			        \
392 		    return False;				\
393 		}						\
394 		strcpy((char *) toVal->addr, value);	\
395 	    }							\
396 	    else {						\
397 		toVal->addr = (XPointer)value;			\
398 	    }							\
399 	    toVal->size = size; 				\
400 	    return True;					\
401 	}
402 
403 #define map_convert_done(value) \
404 	{							\
405 	    if (toVal->addr != NULL) {				\
406 		if (toVal->size < value.size) {		        \
407 		    toVal->size = value.size;		        \
408 		    return False;				\
409 		}						\
410 		strcpy((char *) toVal->addr, value.name);	\
411 	    }							\
412 	    else {						\
413 		toVal->addr = value.name;       		\
414 	    }							\
415 	    toVal->size = value.size; 				\
416 	    return True;					\
417 	}
418 
419 
420 static void
XtDisplayConversionWarning(String type,XtPointer from,String to)421 XtDisplayConversionWarning(String type, XtPointer from, String to)
422 {
423     String params[2];
424     Cardinal num_params = 2;
425 
426     params[0] = (String)from;
427     params[1] = to;
428     XtWarningMsg("conversionError", type, XtCXtToolkitError,
429 		 "Cannot convert value %d to type %s",
430 		 params, &num_params);
431 }
432 
433 
434 static Boolean
_XmNSECvtShapeStyleToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)435 _XmNSECvtShapeStyleToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
436 			    XrmValue *fromVal, XrmValue *toVal,
437 			    XtPointer *data)
438 {
439     int *i = (int *)(fromVal->addr);
440     int index;
441 
442 
443     for (index = 0; XtNumber(shape_style_map); index++)
444     {
445 	if (*i == shape_style_map[index].value)
446 	{
447 	    map_convert_done(shape_style_map[index]);
448 	}
449     }
450 
451     XtDisplayConversionWarning(XtRShapeStyle, (XtPointer)(long)*i, XtRString);
452     return False;
453 }
454 
455 
456 static Boolean
_XmNSECvtBooleanToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)457 _XmNSECvtBooleanToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
458 			 XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
459 {
460     Boolean *i = (Boolean *)(fromVal->addr);
461     int index;
462 
463 
464     for (index = 0; XtNumber(boolean_map); index++)
465     {
466 	if (*i == boolean_map[index].value)
467 	{
468 	    map_convert_done(boolean_map[index]);
469 	}
470     }
471 
472     XtDisplayConversionWarning(XtRBoolean, (XtPointer)(long)*i, XtRString);
473     return False;
474 }
475 
476 
477 static Boolean
_XmNSECvtBoolToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)478 _XmNSECvtBoolToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
479 		      XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
480 {
481     Boolean *i = (Boolean *)(fromVal->addr);
482     int index;
483 
484 
485     for (index = 0; XtNumber(boolean_map); index++)
486     {
487 	if (*i == boolean_map[index].value)
488 	{
489 	    map_convert_done(boolean_map[index]);
490 	}
491     }
492 
493     XtDisplayConversionWarning(XtRBool, (XtPointer)(long)*i, XtRString);
494     return False;
495 }
496 
497 
498 static Boolean
_XmNSECvtAttachmentToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)499 _XmNSECvtAttachmentToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
500 			  XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
501 {
502     int size;
503     const char *buffer;
504 
505     if (*numArgs != 0)
506     {
507 	XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
508 		  XtNwrongParameters, "cvtattachmentToString", XtCXtToolkitError,
509 		      "Attachment to String conversion needs no extra arguments",
510 			(String *)NULL, (Cardinal *)NULL);
511     }
512 
513     /* buffer must not be freed! */
514     buffer = _LtDebugAttachment2String(*(Position *)fromVal->addr);
515     size = strlen(buffer);
516     string_convert_done(buffer);
517 }
518 
519 
520 static Boolean
_XmNSECvtPositionToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)521 _XmNSECvtPositionToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
522 			  XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
523 {
524     int size;
525     static char buffer[32];
526 
527     if (*numArgs != 0)
528     {
529 	XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
530 		  XtNwrongParameters, "cvtPositionToString", XtCXtToolkitError,
531 		      "Position to String conversion needs no extra arguments",
532 			(String *)NULL, (Cardinal *)NULL);
533     }
534 
535     sprintf(buffer, "%d", *(Position *)fromVal->addr);
536     size = strlen(buffer);
537     string_convert_done(buffer);
538 }
539 
540 
541 static Boolean
_XmNSECvtIntToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)542 _XmNSECvtIntToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
543 		     XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
544 {
545     int size;
546     static char buffer[32];
547 
548     if (*numArgs != 0)
549     {
550 	XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
551 			XtNwrongParameters, "cvtIntToString", XtCXtToolkitError,
552 			"Int to String conversion needs no extra arguments",
553 			(String *)NULL, (Cardinal *)NULL);
554     }
555 
556     sprintf(buffer, "%d", *(int *)fromVal->addr);
557     size = strlen(buffer);
558     string_convert_done(buffer);
559 }
560 
561 
562 static Boolean
_XmNSECvtCardinalToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)563 _XmNSECvtCardinalToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
564 			  XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
565 {
566     int size;
567     static char buffer[32];
568 
569     if (*numArgs != 0)
570     {
571 	XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
572 		  XtNwrongParameters, "cvtCardinalToString", XtCXtToolkitError,
573 		      "Cardinal to String conversion needs no extra arguments",
574 			(String *)NULL, (Cardinal *)NULL);
575     }
576 
577     sprintf(buffer, "%d", *(Cardinal *)fromVal->addr);
578     size = strlen(buffer);
579     string_convert_done(buffer);
580 }
581 
582 
583 static Boolean
_XmNSECvtPixelToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)584 _XmNSECvtPixelToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
585 		       XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
586 {
587     int size;
588     static char buffer[32];
589 
590     if (*numArgs != 0)
591     {
592 	XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
593 		     XtNwrongParameters, "cvtPixelToString", XtCXtToolkitError,
594 			"Pixel to String conversion needs no extra arguments",
595 			(String *)NULL, (Cardinal *)NULL);
596     }
597 
598     sprintf(buffer, "%ld", *(Pixel *)fromVal->addr);
599     size = strlen(buffer);
600     string_convert_done(buffer);
601 }
602 
603 
604 static Boolean
_XmNSECvtColorToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)605 _XmNSECvtColorToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
606 		       XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
607 {
608     int size;
609     static char buffer[32];
610 
611     if (*numArgs != 0)
612     {
613 	XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
614 		     XtNwrongParameters, "cvtColorToString", XtCXtToolkitError,
615 			"Color to String conversion needs no extra arguments",
616 			(String *)NULL, (Cardinal *)NULL);
617     }
618 
619     sprintf(buffer, "rgb:%04hx/%04hx/%04hx", ((XColor *)fromVal->addr)->red,
620 	    ((XColor *)fromVal->addr)->green, ((XColor *)fromVal->addr)->blue);
621     size = strlen(buffer);
622     string_convert_done(buffer);
623 }
624 
625 
626 static Boolean
_XmNSECvtUnsignedCharToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)627 _XmNSECvtUnsignedCharToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
628 			      XrmValue *fromVal, XrmValue *toVal,
629 			      XtPointer *data)
630 {
631     int size;
632     static char buffer[32];
633 
634     if (*numArgs != 0)
635     {
636 	XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
637 			XtNwrongParameters, "cvtUnsignedCharToString",
638 			XtCXtToolkitError,
639 		  "UnsignedChar to String conversion needs no extra arguments",
640 			(String *)NULL, (Cardinal *)NULL);
641     }
642 
643     sprintf(buffer, "%d", *(unsigned char *)fromVal->addr);
644     size = strlen(buffer);
645     string_convert_done(buffer);
646 }
647 
648 
649 static Boolean
_XmNSECvtDimensionToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)650 _XmNSECvtDimensionToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
651 			   XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
652 {
653     int size;
654     static char buffer[32];
655 
656     if (*numArgs != 0)
657     {
658 	XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
659 		 XtNwrongParameters, "cvtDimensionToString", XtCXtToolkitError,
660 		     "Dimension to String conversion needs no extra arguments",
661 			(String *)NULL, (Cardinal *)NULL);
662     }
663 
664     sprintf(buffer, "%d", *(Dimension *)fromVal->addr);
665     size = strlen(buffer);
666     string_convert_done(buffer);
667 }
668 
669 
670 static Boolean
_XmNSECvtXmStringToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)671 _XmNSECvtXmStringToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
672 			  XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
673 {
674     int size;
675     char *s;
676 
677     DEBUGOUT(_LtDebug(__FILE__, NULL, "_XmNSECvtXmStringToString\n"));
678 
679     if (*numArgs != 0)
680     {
681 	XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
682 		 XtNwrongParameters, "cvtDimensionToString", XtCXtToolkitError,
683 		      "XmString to String conversion needs no extra arguments",
684 			(String *)NULL, (Cardinal *)NULL);
685     }
686 
687     if (XmStringGetLtoR(*(XmString *)fromVal->addr, XmFONTLIST_DEFAULT_TAG, &s))
688     {
689 	/* There's a memory leak here : nobody does free(s) */
690 	size = strlen(s);
691 	string_convert_done(s);
692     }
693     else
694        return False;		/* Conversion failed */
695 }
696 
697 
698 static Boolean
_XmNSECvtWidgetToString(Display * dpy,XrmValue * args,Cardinal * numArgs,XrmValue * fromVal,XrmValue * toVal,XtPointer * data)699 _XmNSECvtWidgetToString(Display *dpy, XrmValue *args, Cardinal *numArgs,
700 			  XrmValue *fromVal, XrmValue *toVal, XtPointer *data)
701 {
702     int size;
703     Widget w;
704     static char *s = NULL; /* amai: a small(?) memory leak - see below */
705 
706     DEBUGOUT(_LtDebug(__FILE__, NULL, "_XmNSECvtWidgetToString\n"));
707 
708     if (*numArgs != 0)
709     {
710 	XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
711 		 XtNwrongParameters, "cvtWidgetToString", XtCXtToolkitError,
712 		      "Widget to String conversion needs extra arguments",
713 			(String *)NULL, (Cardinal *)NULL);
714     }
715 
716     if (s)
717       XtFree(s);
718     w = *(Widget *)fromVal->addr;
719     s = XtNewString(XtName(w));
720     size = strlen(s);
721     string_convert_done(s);
722 }
723 
724 
725 /* this is needed for the string to pixmap converters */
726 static XtConvertArgRec args[] =
727 {
728     {
729 	XtBaseOffset,
730 	(XtPointer)XtOffsetOf(WidgetRec, core.screen),
731 	sizeof(Screen *)
732     }
733 };
734 
735 
736 /*
737  * register nonstandard extensions, if any
738  */
739 extern void
_XmRegisterNSEConverters(void)740 _XmRegisterNSEConverters(void)
741 {
742     XtSetTypeConverter(XmRString,	/* source type */
743 		       XmRPixmap,	/* target type */
744 		       _XmNSECvtStringToPixmap,	/* converter routine */
745 		       args,		/* args for converter routine */
746 		       XtNumber(args),	/* number of args for converter */
747 		       XtCacheNone,	/* caching instructions */
748 		       NULL);		/* destructor function */
749 
750     XtSetTypeConverter(XmRString,	/* source type */
751 		       XmRBitmap,	/* target type */
752 		       _XmNSECvtStringToBitmap,	/* converter routine */
753 		       args,		/* args for converter routine */
754 		       XtNumber(args),	/* number of args for converter */
755 		       XtCacheNone,	/* caching instructions */
756 		       NULL);		/* destructor function */
757 
758 #if 0
759 /* amai: see thread about DragIcon/Ishmail */
760     XtSetTypeConverter(XmRString,
761 		       XmRBitmap,
762 		       _XmNSECvtStringToBitmap,
763 		       args,
764 		       XtNumber(args),
765 		       XtCacheNone,
766 		       NULL);
767 #endif
768     XtSetTypeConverter(XmRString,
769 		       XmRXmBackgroundPixmap,
770 		       _XmNSECvtStringToPixmap,
771 		       args,
772 		       XtNumber(args),
773 		       XtCacheNone,
774 		       NULL);
775 
776     /* Converters for reverse-direction Editres below */
777     XtSetTypeConverter(XtRShapeStyle, XtRString, _XmNSECvtShapeStyleToString,
778 		       NULL, 0, XtCacheNone, NULL);
779     XtSetTypeConverter(XtRBoolean, XtRString, _XmNSECvtBooleanToString,
780 		       NULL, 0, XtCacheNone, NULL);
781     XtSetTypeConverter(XtRBool, XtRString, _XmNSECvtBoolToString,
782 		       NULL, 0, XtCacheNone, NULL);
783     XtSetTypeConverter(XtRShapeStyle, XtRString, _XmNSECvtShapeStyleToString,
784 		       NULL, 0, XtCacheNone, NULL);
785     XtSetTypeConverter(XtRBoolean, XtRString, _XmNSECvtBooleanToString,
786 		       NULL, 0, XtCacheNone, NULL);
787     XtSetTypeConverter(XtRBool, XtRString, _XmNSECvtBoolToString,
788 		       NULL, 0, XtCacheNone, NULL);
789     XtSetTypeConverter(XmRAttachment, XtRString, _XmNSECvtAttachmentToString,
790 		       NULL, 0, XtCacheNone, NULL);
791     XtSetTypeConverter(XtRPosition, XtRString, _XmNSECvtPositionToString,
792 		       NULL, 0, XtCacheNone, NULL);
793     XtSetTypeConverter(XtRInt, XtRString, _XmNSECvtIntToString,
794 		       NULL, 0, XtCacheNone, NULL);
795     XtSetTypeConverter(XtRCardinal, XtRString, _XmNSECvtCardinalToString,
796 		       NULL, 0, XtCacheNone, NULL);
797     XtSetTypeConverter(XtRPixel, XtRString, _XmNSECvtPixelToString,
798 		       NULL, 0, XtCacheNone, NULL);
799     XtSetTypeConverter(XtRColor, XtRString, _XmNSECvtColorToString,
800 		       NULL, 0, XtCacheNone, NULL);
801     XtSetTypeConverter(XtRUnsignedChar, XtRString,
802 		       _XmNSECvtUnsignedCharToString,
803 		       NULL, 0, XtCacheNone, NULL);
804     XtSetTypeConverter(XtRDimension, XtRString, _XmNSECvtDimensionToString,
805 		       NULL, 0, XtCacheNone, NULL);
806     XtSetTypeConverter(XmRXmString, XtRString, _XmNSECvtXmStringToString,
807 		       NULL, 0, XtCacheNone, NULL);
808     XtSetTypeConverter(XmRWidget, XtRString, _XmNSECvtWidgetToString,
809 		       NULL, 0, XtCacheNone, NULL);
810 }
811 
812 #endif /* NONSTANDARD_CONVERTERS */
813