1
/* $Xorg: StrToCurs.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
5
Copyright 1987, 1988, 1998 The Open Group
7
Permission to use, copy, modify, distribute, and sell this software and its
8
documentation for any purpose is hereby granted without fee, provided that
9
the above copyright notice appear in all copies and that both that
10
copyright notice and this permission notice appear in supporting
13
The above copyright notice and this permission notice shall be included in
14
all copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
Except as contained in this notice, the name of The Open Group shall not be
24
used in advertising or otherwise to promote the sale, use or other dealings
25
in this Software without prior written authorization from The Open Group.
29
/***********************************************************
31
Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
35
Permission to use, copy, modify, and distribute this software and its
36
documentation for any purpose and without fee is hereby granted,
37
provided that the above copyright notice appear in all copies and that
38
both that copyright notice and this permission notice appear in
39
supporting documentation, and that the name of Digital not be
40
used in advertising or publicity pertaining to distribution of the
41
software without specific, written prior permission.
43
DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
44
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
45
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
46
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
47
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
48
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51
******************************************************************/
53
/* $XFree86: xc/lib/Xmu/StrToCurs.c,v 1.12 2002/09/24 18:55:21 alanh Exp $ */
55
#include <X11/Intrinsic.h>
56
#include <X11/StringDefs.h>
57
#include <X11/Xmu/Converters.h>
58
#include <X11/Xmu/Drawing.h>
59
#include <X11/Xmu/CurUtil.h>
60
#include <X11/Xmu/CharSet.h>
71
#endif /* X_NOT_POSIX */
76
#include <sys/param.h>
80
#define PATH_MAX MAXPATHLEN
87
/* Kludge source to avoid encountering broken shared library linkers
88
which insist on resolving references unused by the application,
89
and broken object file formats that don't correctly distinguish
90
references to procedures from references to data.
92
#if defined(SUNSHLIB) || defined(SVR4)
97
* XmuConvertStringToCursor:
99
* allows String to specify a standard cursor name (from cursorfont.h), a
100
* font name and glyph index of the form "FONT fontname index [[font] index]",
101
* or a bitmap file name (absolute, or relative to the global resource
102
* bitmapFilePath, class BitmapFilePath). If the resource is not
103
* defined, the default value is the build symbol BITMAPDIR.
105
* shares lots of code with XmuCvtStringToPixmap, but unfortunately
106
* can't use it as the hotspot info is lost.
108
* To use, include the following in your ClassInitialize procedure:
110
static XtConvertArgRec screenConvertArg[] = {
111
{XtBaseOffset, (XtPointer) XtOffsetOf(WidgetRec, core.screen),
115
XtAddConverter(XtRString, XtRCursor, XmuCvtStringToCursor,
116
screenConvertArg, XtNumber(screenConvertArg));
120
#define done(address, type) \
121
{ (*toVal).size = sizeof(type); (*toVal).addr = (XPointer) address; }
123
#define FONTSPECIFIER "FONT "
127
XmuCvtStringToCursor(XrmValuePtr args, Cardinal *num_args,
128
XrmValuePtr fromVal, XrmValuePtr toVal)
130
static Cursor cursor; /* static for cvt magic */
131
char *name = (char *)fromVal->addr;
134
char maskname[PATH_MAX];
135
Pixmap source, mask = 0;
136
/* XXX - make fg/bg resources */
137
static XColor bgColor = {0, 0xffff, 0xffff, 0xffff};
138
static XColor fgColor = {0, 0, 0, 0};
144
XtErrorMsg("wrongParameters","cvtStringToCursor","XtToolkitError",
145
"String to cursor conversion needs screen argument",
146
(String *)NULL, (Cardinal *)NULL);
148
if (XmuCompareISOLatin1(name, "None") == 0)
151
done(&cursor, Cursor);
155
screen = *((Screen **) args[0].addr);
157
if (0 == strncmp(FONTSPECIFIER, name, strlen(FONTSPECIFIER))) {
158
char source_name[PATH_MAX], mask_name[PATH_MAX];
159
int source_char, mask_char, fields;
160
Font source_font, mask_font;
161
XrmValue fromString, toFont;
164
Display *dpy = DisplayOfScreen(screen);
165
char *strspec = NULL;
170
strspec = XtMalloc(strlen("FONT %s %d %s %d") + 21);
171
sprintf(strspec, "FONT %%%lds %%d %%%lds %%d",
172
(unsigned long)sizeof(source_name) - 1,
173
(unsigned long)sizeof(mask_name) - 1);
174
fields = sscanf(name, strspec,
175
source_name, &source_char,
176
mask_name, &mask_char);
179
XtStringConversionWarning(name, XtRCursor);
183
fromString.addr = source_name;
184
fromString.size = strlen(source_name) + 1;
185
toFont.addr = (XPointer) &source_font;
186
toFont.size = sizeof(Font);
187
cvtArg.addr = (XPointer) &dpy;
188
cvtArg.size = sizeof(Display *);
189
/* XXX using display of screen argument as message display */
191
/* XXX Sacrifice caching */
193
success = XtCvtStringToFont(dpy, &cvtArg, &num, &fromString, &toFont,
196
success = XtCallConverter(dpy, XtCvtStringToFont, &cvtArg,
197
(Cardinal)1, &fromString, &toFont, NULL);
200
XtStringConversionWarning(name, XtRCursor);
205
case 2: /* defaulted mask font & char */
206
mask_font = source_font;
207
mask_char = source_char;
210
case 3: /* defaulted mask font */
211
mask_font = source_font;
212
mask_char = atoi(mask_name);
215
case 4: /* specified mask font & char */
216
fromString.addr = mask_name;
217
fromString.size = strlen(mask_name) + 1;
218
toFont.addr = (XPointer) &mask_font;
219
toFont.size = sizeof(Font);
220
/* XXX using display of screen argument as message display */
222
/* XXX Sacrifice caching */
224
success = XtCvtStringToFont(dpy, &cvtArg, &num, &fromString,
227
success = XtCallConverter(dpy, XtCvtStringToFont, &cvtArg,
228
(Cardinal)1, &fromString, &toFont, NULL);
231
XtStringConversionWarning(name, XtRCursor);
236
cursor = XCreateGlyphCursor( DisplayOfScreen(screen), source_font,
237
mask_font, source_char, mask_char,
238
&fgColor, &bgColor );
239
done(&cursor, Cursor);
243
i = XmuCursorNameToIndex (name);
245
cursor = XCreateFontCursor (DisplayOfScreen(screen), i);
246
done(&cursor, Cursor);
250
if ((source = XmuLocateBitmapFile (screen, name,
251
maskname, (sizeof maskname) - 4,
252
NULL, NULL, &xhot, &yhot)) == None) {
253
XtStringConversionWarning (name, XtRCursor);
255
done(&cursor, Cursor);
258
len = strlen (maskname);
259
for (i = 0; i < 2; i++) {
260
strcpy (maskname + len, i == 0 ? "Mask" : "msk");
261
if ((mask = XmuLocateBitmapFile (screen, maskname, NULL, 0,
262
NULL, NULL, NULL, NULL)) != None)
266
cursor = XCreatePixmapCursor( DisplayOfScreen(screen), source, mask,
267
&fgColor, &bgColor, xhot, yhot );
268
XFreePixmap( DisplayOfScreen(screen), source );
269
if (mask != None) XFreePixmap( DisplayOfScreen(screen), mask );
271
done(&cursor, Cursor);
274
#define new_done(type, value) \
276
if (toVal->addr != NULL) { \
277
if (toVal->size < sizeof(type)) { \
278
toVal->size = sizeof(type); \
281
*(type*)(toVal->addr) = (value); \
284
static type static_val; \
285
static_val = (value); \
286
toVal->addr = (XPointer)&static_val; \
288
toVal->size = sizeof(type); \
292
/* Function Name: XmuCvtStringToColorCursor
293
* Description: Converts a string into a colored cursor.
295
* args - an argument list (see below).
296
* num_args - number of elements in the argument list.
297
* fromVal - value to convert from.
298
* toVal - value to convert to.
300
* Returns: True or False
305
XmuCvtStringToColorCursor(Display *dpy, XrmValuePtr args, Cardinal *num_args,
306
XrmValuePtr fromVal, XrmValuePtr toVal,
307
XtPointer *converter_data)
317
if (*num_args != 4) {
318
XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
319
"wrongParameters","cvtStringToColorCursor","XmuError",
320
"String to color cursor conversion needs four arguments",
321
(String *)NULL, (Cardinal *)NULL);
325
screen = *((Screen **) args[0].addr);
326
fg = *((Pixel *) args[1].addr);
327
bg = *((Pixel *) args[2].addr);
328
c_map = *((Colormap *) args[3].addr);
331
XmuCvtStringToCursor(args, &number, fromVal, &ret_val);
333
cursor = *((Cursor *) ret_val.addr);
335
if (cursor == None || (fg == BlackPixelOfScreen(screen)
336
&& bg == WhitePixelOfScreen(screen)))
337
new_done(Cursor, cursor);
339
colors[0].pixel = fg;
340
colors[1].pixel = bg;
342
XQueryColors (dpy, c_map, colors, 2);
343
XRecolorCursor(dpy, cursor, colors, colors + 1);
344
new_done(Cursor, cursor);