StrToCurs.c

Go to the documentation of this file.
00001 /* $Xorg: StrToCurs.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
00002 
00003 /*
00004 
00005 Copyright 1987, 1988, 1998  The Open Group
00006 
00007 Permission to use, copy, modify, distribute, and sell this software and its
00008 documentation for any purpose is hereby granted without fee, provided that
00009 the above copyright notice appear in all copies and that both that
00010 copyright notice and this permission notice appear in supporting
00011 documentation.
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 
00023 Except as contained in this notice, the name of The Open Group shall not be
00024 used in advertising or otherwise to promote the sale, use or other dealings
00025 in this Software without prior written authorization from The Open Group.
00026 
00027 */
00028 
00029 /***********************************************************
00030 
00031 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
00032 
00033                         All Rights Reserved
00034 
00035 Permission to use, copy, modify, and distribute this software and its 
00036 documentation for any purpose and without fee is hereby granted, 
00037 provided that the above copyright notice appear in all copies and that
00038 both that copyright notice and this permission notice appear in 
00039 supporting documentation, and that the name of Digital not be
00040 used in advertising or publicity pertaining to distribution of the
00041 software without specific, written prior permission.  
00042 
00043 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
00044 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
00045 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
00046 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
00047 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
00048 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00049 SOFTWARE.
00050 
00051 ******************************************************************/
00052 
00053 /* $XFree86: xc/lib/Xmu/StrToCurs.c,v 1.11 2002/09/19 13:21:58 tsi Exp $ */
00054 
00055 #include        <X11/Intrinsic.h>
00056 #include        <X11/StringDefs.h>
00057 #include        <X11/Xmu/Converters.h>
00058 #include        <X11/Xmu/Drawing.h>
00059 #include        <X11/Xmu/CurUtil.h>
00060 #include        <X11/Xmu/CharSet.h>
00061 
00062 #ifndef X_NOT_POSIX
00063 #include <stdlib.h>
00064 #ifdef _POSIX_SOURCE
00065 #include <limits.h>
00066 #else
00067 #define _POSIX_SOURCE
00068 #include <limits.h>
00069 #undef _POSIX_SOURCE
00070 #endif
00071 #endif /* X_NOT_POSIX */
00072 #ifndef PATH_MAX
00073 #ifdef WIN32
00074 #define PATH_MAX 512
00075 #else
00076 #include <sys/param.h>
00077 #endif
00078 #ifndef PATH_MAX
00079 #ifdef MAXPATHLEN
00080 #define PATH_MAX MAXPATHLEN
00081 #else
00082 #define PATH_MAX 1024
00083 #endif
00084 #endif
00085 #endif /* PATH_MAX */
00086 
00087 /* Kludge source to avoid encountering broken shared library linkers
00088    which insist on resolving references unused by the application,
00089    and broken object file formats that don't correctly distinguish
00090    references to procedures from references to data.
00091  */
00092 #if defined(SUNSHLIB) || defined(SVR4)
00093 #define XMU_KLUDGE
00094 #endif
00095 
00096 /*
00097  * XmuConvertStringToCursor:
00098  *
00099  * allows String to specify a standard cursor name (from cursorfont.h), a
00100  * font name and glyph index of the form "FONT fontname index [[font] index]", 
00101  * or a bitmap file name (absolute, or relative to the global resource
00102  * bitmapFilePath, class BitmapFilePath).  If the resource is not
00103  * defined, the default value is the build symbol BITMAPDIR.
00104  *
00105  * shares lots of code with XmuCvtStringToPixmap, but unfortunately
00106  * can't use it as the hotspot info is lost.
00107  *
00108  * To use, include the following in your ClassInitialize procedure:
00109 
00110 static XtConvertArgRec screenConvertArg[] = {
00111     {XtBaseOffset, (XtPointer) XtOffsetOf(WidgetRec, core.screen),
00112      sizeof(Screen *)}
00113 };
00114 
00115     XtAddConverter(XtRString, XtRCursor, XmuCvtStringToCursor,      
00116                    screenConvertArg, XtNumber(screenConvertArg));
00117  *
00118  */
00119 
00120 #define done(address, type) \
00121         { (*toVal).size = sizeof(type); (*toVal).addr = (XPointer) address; }
00122 
00123 #define FONTSPECIFIER           "FONT "
00124 
00125 /*ARGSUSED*/
00126 void
00127 XmuCvtStringToCursor(XrmValuePtr args, Cardinal *num_args,
00128                      XrmValuePtr fromVal, XrmValuePtr toVal)
00129 {
00130     static Cursor cursor;               /* static for cvt magic */
00131     char *name = (char *)fromVal->addr;
00132     Screen *screen;
00133     register int i;
00134     char maskname[PATH_MAX];
00135     Pixmap source, mask = 0;
00136     /* XXX - make fg/bg resources */
00137     static XColor bgColor = {0, 0xffff, 0xffff, 0xffff};
00138     static XColor fgColor = {0, 0, 0, 0};
00139     int xhot, yhot;
00140     int len;
00141 
00142 
00143     if (*num_args != 1)
00144      XtErrorMsg("wrongParameters","cvtStringToCursor","XtToolkitError",
00145              "String to cursor conversion needs screen argument",
00146               (String *)NULL, (Cardinal *)NULL);
00147 
00148     if (XmuCompareISOLatin1(name, "None") == 0)
00149       {
00150         cursor = None;
00151         done(&cursor, Cursor);
00152         return;
00153       }
00154 
00155     screen = *((Screen **) args[0].addr);
00156 
00157     if (0 == strncmp(FONTSPECIFIER, name, strlen(FONTSPECIFIER))) {
00158         char source_name[PATH_MAX], mask_name[PATH_MAX];
00159         int source_char, mask_char, fields;
00160         Font source_font, mask_font;
00161         XrmValue fromString, toFont;
00162         XrmValue cvtArg;
00163         Boolean success;
00164         Display *dpy = DisplayOfScreen(screen);
00165         char *strspec = NULL;
00166 #ifdef XMU_KLUDGE
00167         Cardinal num;
00168 #endif
00169 
00170         strspec = XtMalloc(strlen("FONT %s %d %s %d") + 21);
00171         sprintf(strspec, "FONT %%%lds %%d %%%lds %%d",
00172                 (unsigned long)sizeof(source_name) - 1,
00173                 (unsigned long)sizeof(mask_name) - 1);
00174         fields = sscanf(name, strspec,
00175                         source_name, &source_char,
00176                         mask_name, &mask_char);
00177         XtFree(strspec);
00178         if (fields < 2) {
00179             XtStringConversionWarning(name, XtRCursor);
00180             return;
00181         }
00182 
00183         fromString.addr = source_name;
00184         fromString.size = strlen(source_name) + 1;
00185         toFont.addr = (XPointer) &source_font;
00186         toFont.size = sizeof(Font);
00187         cvtArg.addr = (XPointer) &dpy;
00188         cvtArg.size = sizeof(Display *);
00189         /* XXX using display of screen argument as message display */
00190 #ifdef XMU_KLUDGE
00191         /* XXX Sacrifice caching */
00192         num = 1;
00193         success = XtCvtStringToFont(dpy, &cvtArg, &num, &fromString, &toFont,
00194                                     NULL);
00195 #else
00196         success = XtCallConverter(dpy, XtCvtStringToFont, &cvtArg,
00197                                   (Cardinal)1, &fromString, &toFont, NULL);
00198 #endif
00199         if (!success) {
00200             XtStringConversionWarning(name, XtRCursor);
00201             return;
00202         }
00203 
00204         switch (fields) {
00205           case 2:               /* defaulted mask font & char */
00206             mask_font = source_font;
00207             mask_char = source_char;
00208             break;
00209 
00210           case 3:               /* defaulted mask font */
00211             mask_font = source_font;
00212             mask_char = atoi(mask_name);
00213             break;
00214 
00215           case 4:               /* specified mask font & char */
00216             fromString.addr = mask_name;
00217             fromString.size = strlen(mask_name) + 1;
00218             toFont.addr = (XPointer) &mask_font;
00219             toFont.size = sizeof(Font);
00220             /* XXX using display of screen argument as message display */
00221 #ifdef XMU_KLUDGE
00222             /* XXX Sacrifice caching */
00223             num = 1;
00224             success = XtCvtStringToFont(dpy, &cvtArg, &num, &fromString,
00225                                         &toFont, NULL);
00226 #else
00227             success = XtCallConverter(dpy, XtCvtStringToFont, &cvtArg,
00228                                       (Cardinal)1, &fromString, &toFont, NULL);
00229 #endif
00230             if (!success) {
00231                 XtStringConversionWarning(name, XtRCursor);
00232                 return;
00233             }
00234         }
00235 
00236         cursor = XCreateGlyphCursor( DisplayOfScreen(screen), source_font,
00237                                      mask_font, source_char, mask_char,
00238                                      &fgColor, &bgColor );
00239         done(&cursor, Cursor);
00240         return;
00241     }
00242 
00243     i = XmuCursorNameToIndex (name);
00244     if (i != -1) {
00245         cursor = XCreateFontCursor (DisplayOfScreen(screen), i);
00246         done(&cursor, Cursor);
00247         return;
00248     }
00249 
00250     if ((source = XmuLocateBitmapFile (screen, name, 
00251                                        maskname, (sizeof maskname) - 4,
00252                                        NULL, NULL, &xhot, &yhot)) == None) {
00253         XtStringConversionWarning (name, XtRCursor);
00254         cursor = None;
00255         done(&cursor, Cursor);
00256         return;
00257     }
00258     len = strlen (maskname);
00259     for (i = 0; i < 2; i++) {
00260         strcpy (maskname + len, i == 0 ? "Mask" : "msk");
00261         if ((mask = XmuLocateBitmapFile (screen, maskname, NULL, 0, 
00262                                          NULL, NULL, NULL, NULL)) != None)
00263           break;
00264     }
00265 
00266     cursor = XCreatePixmapCursor( DisplayOfScreen(screen), source, mask,
00267                                   &fgColor, &bgColor, xhot, yhot );
00268     XFreePixmap( DisplayOfScreen(screen), source );
00269     if (mask != None) XFreePixmap( DisplayOfScreen(screen), mask );
00270 
00271     done(&cursor, Cursor);
00272 }
00273 
00274 #define new_done(type, value) \
00275         {                                                       \
00276             if (toVal->addr != NULL) {                          \
00277                 if (toVal->size < sizeof(type)) {               \
00278                     toVal->size = sizeof(type);                 \
00279                     return False;                               \
00280                 }                                               \
00281                 *(type*)(toVal->addr) = (value);                \
00282             }                                                   \
00283             else {                                              \
00284                 static type static_val;                         \
00285                 static_val = (value);                           \
00286                 toVal->addr = (XPointer)&static_val;            \
00287             }                                                   \
00288             toVal->size = sizeof(type);                         \
00289             return True;                                        \
00290         }
00291 
00292 /*      Function Name: XmuCvtStringToColorCursor
00293  *      Description: Converts a string into a colored cursor.
00294  *      Arguments: dpy
00295  *                 args - an argument list (see below).
00296  *                 num_args - number of elements in the argument list.
00297  *                 fromVal - value to convert from.
00298  *                 toVal - value to convert to.
00299  *                 data
00300  *      Returns:   True or False
00301  */
00302 
00303 /*ARGSUSED*/
00304 Boolean
00305 XmuCvtStringToColorCursor(Display *dpy, XrmValuePtr args, Cardinal *num_args,
00306                           XrmValuePtr fromVal, XrmValuePtr toVal,
00307                           XtPointer *converter_data)
00308 {
00309     Cursor cursor;
00310     Screen *screen;
00311     Pixel fg, bg;
00312     Colormap c_map;
00313     XColor colors[2];
00314     Cardinal number;
00315     XrmValue ret_val;
00316 
00317     if (*num_args != 4) {
00318         XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
00319             "wrongParameters","cvtStringToColorCursor","XmuError",
00320             "String to color cursor conversion needs four arguments",
00321             (String *)NULL, (Cardinal *)NULL);
00322         return False;
00323     }
00324 
00325     screen = *((Screen **) args[0].addr);
00326     fg = *((Pixel *) args[1].addr);
00327     bg = *((Pixel *) args[2].addr);
00328     c_map = *((Colormap *) args[3].addr);
00329 
00330     number = 1;
00331     XmuCvtStringToCursor(args, &number, fromVal, &ret_val);
00332     
00333     cursor = *((Cursor *) ret_val.addr);
00334 
00335     if (cursor == None || (fg == BlackPixelOfScreen(screen)
00336                            && bg == WhitePixelOfScreen(screen)))
00337         new_done(Cursor, cursor);
00338 
00339     colors[0].pixel = fg;
00340     colors[1].pixel = bg;
00341 
00342     XQueryColors (dpy, c_map, colors, 2);
00343     XRecolorCursor(dpy, cursor, colors, colors + 1);
00344     new_done(Cursor, cursor);
00345 }
00346 
00347     
00348     

Generated on Sun Mar 4 15:03:56 2007 for Scilab [trunk] by  doxygen 1.5.1