convert all Imakefile LinuxDistribution to LinuxArchitecture.
[oweals/cde.git] / cde / programs / dtksh / XtCvtrs.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: XtCvtrs.c /main/3 1995/07/14 13:25:38 drk $ */
24 /*LINTLIBRARY*/
25
26 /***********************************************************
27 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
28 and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
29
30                         All Rights Reserved
31
32 Permission to use, copy, modify, and distribute this software and its 
33 documentation for any purpose and without fee is hereby granted, 
34 provided that the above copyright notice appear in all copies and that
35 both that copyright notice and this permission notice appear in 
36 supporting documentation, and that the names of Digital or MIT not be
37 used in advertising or publicity pertaining to distribution of the
38 software without specific, written prior permission.  
39
40 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
41 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
42 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
43 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
44 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
45 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
46 SOFTWARE.
47
48 ******************************************************************/
49
50 /* Conversion.c - implementations of resource type conversion procs */
51
52 #include        <X11/Xlib.h>
53 #include        <X11/Intrinsic.h>
54 #include        <X11/IntrinsicP.h>
55 #include        <X11/StringDefs.h>
56 #include        "stdio.h"
57 #include        <X11/keysym.h>
58 #include        <X11/Xlocale.h>
59 #include        "msgs.h"
60 #include        "XtCvtrs.h"
61
62
63 #define done(type, value) \
64         {                                                       \
65             if (toVal->addr != NULL) {                          \
66                 if (toVal->size < sizeof(type)) {               \
67                     toVal->size = sizeof(type);                 \
68                     return False;                               \
69                 }                                               \
70                 *(type*)(toVal->addr) = (value);                \
71             }                                                   \
72             else {                                              \
73                 static type static_val;                         \
74                 static_val = (value);                           \
75                 toVal->addr = (XPointer)&static_val;            \
76             }                                                   \
77             toVal->size = sizeof(type);                         \
78             return True;                                        \
79         }
80
81
82 static void FreePixel(
83                         XtAppContext app,
84                         XrmValuePtr toVal,
85                         XtPointer closure,
86                         XrmValuePtr args,
87                         Cardinal *num_args) ;
88
89
90
91
92 static XtConvertArgRec xtColorConvertArgs[] = {
93     {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.screen),
94      sizeof(Screen *)},
95     {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.colormap),
96      sizeof(Colormap)}
97 };
98
99
100 Boolean
101 DtkshCvtStringToPixel(
102         Display *dpy,
103         XrmValuePtr args,
104         Cardinal *num_args,
105         XrmValuePtr fromVal,
106         XrmValuePtr toVal,
107         XtPointer *closure_ret )
108 {
109     String          str = (String)fromVal->addr;
110     XColor          screenColor;
111     XColor          exactColor;
112     Screen          *screen;
113     Colormap        colormap;
114     Status          status;
115     char            *errmsg;
116
117     if (*num_args != 2) 
118         return False;
119
120     screen = *((Screen **) args[0].addr);
121     colormap = *((Colormap *) args[1].addr);
122
123     if (DtCompareISOLatin1(str, XtDefaultBackground)) {
124         *closure_ret = False;
125         done(Pixel, WhitePixelOfScreen(screen));
126     }
127     if (DtCompareISOLatin1(str, XtDefaultForeground)) {
128         *closure_ret = False;
129         done(Pixel, BlackPixelOfScreen(screen));
130     }
131
132     status = XAllocNamedColor(DisplayOfScreen(screen), colormap,
133                               (char*)str, &screenColor, &exactColor);
134     if (status == 0) {
135         String type;
136         /* Server returns a specific error code but Xlib discards it.  Ugh */
137         if (XLookupColor(DisplayOfScreen(screen), colormap, (char*)str,
138                          &exactColor, &screenColor)) {
139             *closure_ret = (char*)True;
140             done(Pixel, screenColor.pixel);
141         }
142         else {
143             /* One last chance; see if it is a pixel number */
144             char * p;
145             Pixel pixelValue;
146
147             pixelValue = strtoul(str, &p, 0);
148             if (p == str)
149             {
150                char * errbuf;
151
152                errmsg = GETMESSAGE(1, 1, 
153                         "DtkshCvtStringToPixel: The color '%s' is not defined");
154                errbuf = XtMalloc(strlen(errmsg) + strlen(str) + 10);
155                sprintf(errbuf, errmsg, str);
156                XtWarning(errbuf);
157                XtFree(errbuf);
158                *closure_ret = False;
159                return False;
160             }
161             else
162             {
163                *closure_ret = False;
164                done(Pixel, pixelValue);
165             }
166         }
167     } else {
168         *closure_ret = (char*)True;
169         done(Pixel, screenColor.pixel);
170     }
171 }
172
173 /* ARGSUSED */
174 static void
175 FreePixel(
176         XtAppContext app,
177         XrmValuePtr toVal,
178         XtPointer closure,
179         XrmValuePtr args,
180         Cardinal *num_args )
181 {
182     Screen          *screen;
183     Colormap        colormap;
184
185     if (*num_args != 2)
186         return;
187
188     screen = *((Screen **) args[0].addr);
189     colormap = *((Colormap *) args[1].addr);
190
191     if (closure) {
192         XFreeColors( DisplayOfScreen(screen), colormap,
193                      (unsigned long*)toVal->addr, 1, (unsigned long)0
194                     );
195     }
196 }
197
198 Boolean
199 DtCompareISOLatin1(
200         char *first,
201         char *second )
202 {
203     register unsigned char *ap, *bp;
204
205     for (ap = (unsigned char *) first, bp = (unsigned char *) second;
206          *ap && *bp; ap++, bp++) {
207         register unsigned char a, b;
208
209         if ((a = *ap) != (b = *bp)) {
210             /* try lowercasing and try again */
211
212             if ((a >= XK_A) && (a <= XK_Z))
213               a += (XK_a - XK_A);
214             else if ((a >= XK_Agrave) && (a <= XK_Odiaeresis))
215               a += (XK_agrave - XK_Agrave);
216             else if ((a >= XK_Ooblique) && (a <= XK_Thorn))
217               a += (XK_oslash - XK_Ooblique);
218
219             if ((b >= XK_A) && (b <= XK_Z))
220               b += (XK_a - XK_A);
221             else if ((b >= XK_Agrave) && (b <= XK_Odiaeresis))
222               b += (XK_agrave - XK_Agrave);
223             else if ((b >= XK_Ooblique) && (b <= XK_Thorn))
224               b += (XK_oslash - XK_Ooblique);
225
226             if (a != b) break;
227         }
228     }
229     return ((((int) *bp) - ((int) *ap)) == 0);
230 }
231
232 void
233 RegisterXtOverrideConverters( void )
234 {
235         XtSetTypeConverter(XtRString, XtRPixel,
236                 (XtTypeConverter)DtkshCvtStringToPixel, 
237                 xtColorConvertArgs, XtNumber(xtColorConvertArgs),
238                 XtCacheNone, FreePixel);
239 }