home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / StrToCurs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  8.7 KB  |  301 lines

  1. /* $XConsortium: StrToCurs.c,v 1.19 92/03/19 15:22:29 converse Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  5. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the names of Digital or MIT not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. #include    <X11/Intrinsic.h>
  28. #include    <X11/StringDefs.h>
  29. #include    <X11/Xmu/Converters.h>
  30. #include    <X11/Xmu/Drawing.h>
  31.  
  32. #ifndef X_NOT_POSIX
  33. #ifdef _POSIX_SOURCE
  34. #include <limits.h>
  35. #else
  36. #define _POSIX_SOURCE
  37. #include <limits.h>
  38. #undef _POSIX_SOURCE
  39. #endif
  40. #endif /* X_NOT_POSIX */
  41. #ifndef PATH_MAX
  42. #include <sys/param.h>
  43. #ifndef PATH_MAX
  44. #ifdef MAXPATHLEN
  45. #define PATH_MAX MAXPATHLEN
  46. #else
  47. #define PATH_MAX 1024
  48. #endif
  49. #endif
  50. #endif /* PATH_MAX */
  51.  
  52. /* Kludge source to avoid encountering broken shared library linkers
  53.    which insist on resolving references unused by the application,
  54.    and broken object file formats that don't correctly distinguish
  55.    references to procedures from references to data.
  56.  */
  57. #if defined(SUNSHLIB) || defined(SVR4)
  58. #define XMU_KLUDGE
  59. #endif
  60.  
  61. /*
  62.  * XmuConvertStringToCursor:
  63.  *
  64.  * allows String to specify a standard cursor name (from cursorfont.h), a
  65.  * font name and glyph index of the form "FONT fontname index [[font] index]", 
  66.  * or a bitmap file name (absolute, or relative to the global resource
  67.  * bitmapFilePath, class BitmapFilePath).  If the resource is not
  68.  * defined, the default value is the build symbol BITMAPDIR.
  69.  *
  70.  * shares lots of code with XmuCvtStringToPixmap, but unfortunately
  71.  * can't use it as the hotspot info is lost.
  72.  *
  73.  * To use, include the following in your ClassInitialize procedure:
  74.  
  75. static XtConvertArgRec screenConvertArg[] = {
  76.     {XtBaseOffset, (XtPointer) XtOffsetOf(WidgetRec, core.screen),
  77.      sizeof(Screen *)}
  78. };
  79.  
  80.     XtAddConverter(XtRString, XtRCursor, XmuCvtStringToCursor,      
  81.            screenConvertArg, XtNumber(screenConvertArg));
  82.  *
  83.  */
  84.  
  85. #define    done(address, type) \
  86.     { (*toVal).size = sizeof(type); (*toVal).addr = (XPointer) address; }
  87.  
  88. #define FONTSPECIFIER        "FONT "
  89.  
  90. /*ARGSUSED*/
  91. void XmuCvtStringToCursor(args, num_args, fromVal, toVal)
  92.     XrmValuePtr args;
  93.     Cardinal    *num_args;
  94.     XrmValuePtr    fromVal;
  95.     XrmValuePtr    toVal;
  96. {
  97.     static Cursor cursor;        /* static for cvt magic */
  98.     char *name = (char *)fromVal->addr;
  99.     Screen *screen;
  100.     register int i;
  101.     char maskname[PATH_MAX];
  102.     Pixmap source, mask;
  103.     /* XXX - make fg/bg resources */
  104.     static XColor bgColor = {0, 0xffff, 0xffff, 0xffff};
  105.     static XColor fgColor = {0, 0, 0, 0};
  106.     int xhot, yhot;
  107.     int len;
  108.  
  109.  
  110.     if (*num_args != 1)
  111.      XtErrorMsg("wrongParameters","cvtStringToCursor","XtToolkitError",
  112.              "String to cursor conversion needs screen argument",
  113.               (String *)NULL, (Cardinal *)NULL);
  114.  
  115.     screen = *((Screen **) args[0].addr);
  116.  
  117.     if (0 == strncmp(FONTSPECIFIER, name, strlen(FONTSPECIFIER))) {
  118.     char source_name[PATH_MAX], mask_name[PATH_MAX];
  119.     int source_char, mask_char, fields;
  120.     Font source_font, mask_font;
  121.     XrmValue fromString, toFont;
  122.     XrmValue cvtArg;
  123.     Boolean success;
  124.     Display *dpy = DisplayOfScreen(screen);
  125. #ifdef XMU_KLUDGE
  126.     Cardinal num;
  127. #endif
  128.  
  129.     fields = sscanf(name, "FONT %s %d %s %d",
  130.             source_name, &source_char,
  131.             mask_name, &mask_char);
  132.     if (fields < 2) {
  133.         XtStringConversionWarning(name, XtRCursor);
  134.         return;
  135.     }
  136.  
  137.     fromString.addr = source_name;
  138.     fromString.size = strlen(source_name) + 1;
  139.     toFont.addr = (XPointer) &source_font;
  140.     toFont.size = sizeof(Font);
  141.     cvtArg.addr = (XPointer) &dpy;
  142.     cvtArg.size = sizeof(Display *);
  143.     /* XXX using display of screen argument as message display */
  144. #ifdef XMU_KLUDGE
  145.     /* XXX Sacrifice caching */
  146.     num = 1;
  147.     success = XtCvtStringToFont(dpy, &cvtArg, &num, &fromString, &toFont,
  148.                     NULL);
  149. #else
  150.     success = XtCallConverter(dpy, XtCvtStringToFont, &cvtArg,
  151.                   (Cardinal)1, &fromString, &toFont, NULL);
  152. #endif
  153.     if (!success) {
  154.         XtStringConversionWarning(name, XtRCursor);
  155.         return;
  156.     }
  157.  
  158.     switch (fields) {
  159.       case 2:        /* defaulted mask font & char */
  160.         mask_font = source_font;
  161.         mask_char = source_char;
  162.         break;
  163.  
  164.       case 3:        /* defaulted mask font */
  165.         mask_font = source_font;
  166.         mask_char = atoi(mask_name);
  167.         break;
  168.  
  169.       case 4:        /* specified mask font & char */
  170.         fromString.addr = mask_name;
  171.         fromString.size = strlen(mask_name) + 1;
  172.         toFont.addr = (XPointer) &mask_font;
  173.         toFont.size = sizeof(Font);
  174.         /* XXX using display of screen argument as message display */
  175. #ifdef XMU_KLUDGE
  176.         /* XXX Sacrifice caching */
  177.         num = 1;
  178.         success = XtCvtStringToFont(dpy, &cvtArg, &num, &fromString,
  179.                     &toFont, NULL);
  180. #else
  181.         success = XtCallConverter(dpy, XtCvtStringToFont, &cvtArg,
  182.                       (Cardinal)1, &fromString, &toFont, NULL);
  183. #endif
  184.         if (!success) {
  185.         XtStringConversionWarning(name, XtRCursor);
  186.         return;
  187.         }
  188.     }
  189.  
  190.     cursor = XCreateGlyphCursor( DisplayOfScreen(screen), source_font,
  191.                      mask_font, source_char, mask_char,
  192.                      &fgColor, &bgColor );
  193.     done(&cursor, Cursor);
  194.     return;
  195.     }
  196.  
  197.     i = XmuCursorNameToIndex (name);
  198.     if (i != -1) {
  199.     cursor = XCreateFontCursor (DisplayOfScreen(screen), i);
  200.     done(&cursor, Cursor);
  201.     return;
  202.     }
  203.  
  204.     if ((source = XmuLocateBitmapFile (screen, name, 
  205.                        maskname, (sizeof maskname) - 4,
  206.                        NULL, NULL, &xhot, &yhot)) == None) {
  207.     XtStringConversionWarning (name, XtRCursor);
  208.     }
  209.     len = strlen (maskname);
  210.     for (i = 0; i < 2; i++) {
  211.     strcpy (maskname + len, i == 0 ? "Mask" : "msk");
  212.     if ((mask = XmuLocateBitmapFile (screen, maskname, NULL, 0, 
  213.                      NULL, NULL, NULL, NULL)) != None)
  214.       break;
  215.     }
  216.  
  217.     cursor = XCreatePixmapCursor( DisplayOfScreen(screen), source, mask,
  218.                   &fgColor, &bgColor, xhot, yhot );
  219.     XFreePixmap( DisplayOfScreen(screen), source );
  220.     if (mask != None) XFreePixmap( DisplayOfScreen(screen), mask );
  221.  
  222.     done(&cursor, Cursor);
  223. }
  224.  
  225. #define    new_done(type, value) \
  226.     {                            \
  227.         if (toVal->addr != NULL) {                \
  228.         if (toVal->size < sizeof(type)) {        \
  229.             toVal->size = sizeof(type);            \
  230.             return False;                \
  231.         }                        \
  232.         *(type*)(toVal->addr) = (value);        \
  233.         }                            \
  234.         else {                        \
  235.         static type static_val;                \
  236.         static_val = (value);                \
  237.         toVal->addr = (XPointer)&static_val;        \
  238.         }                            \
  239.         toVal->size = sizeof(type);                \
  240.         return True;                    \
  241.     }
  242.  
  243. /*    Function Name: XmuCvtStringToColorCursor
  244.  *    Description: Converts a string into a colored cursor.
  245.  *    Arguments: dpy
  246.  *           args - an argument list (see below).
  247.  *                 num_args - number of elements in the argument list.
  248.  *                 fromVal - value to convert from.
  249.  *                 toVal - value to convert to.
  250.  *           data
  251.  *    Returns:   True or False
  252.  */
  253.  
  254. /*ARGSUSED*/
  255. Boolean
  256. XmuCvtStringToColorCursor(dpy, args, num_args, fromVal, toVal, converter_data)
  257.     Display     *dpy;
  258.     XrmValuePtr args;
  259.     Cardinal    *num_args;
  260.     XrmValuePtr    fromVal;
  261.     XrmValuePtr    toVal;
  262.     XtPointer   *converter_data;    /* unused */
  263. {
  264.     Cursor cursor;
  265.     Pixel fg, bg;
  266.     Colormap c_map;
  267.     XColor colors[2];
  268.     Cardinal number;
  269.     XrmValue ret_val;
  270.  
  271.     if (*num_args != 4) {
  272.     XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
  273.         "wrongParameters","cvtStringToColorCursor","XmuError",
  274.             "String to color cursor conversion needs four arguments",
  275.         (String *)NULL, (Cardinal *)NULL);
  276.     return False;
  277.     }
  278.  
  279.     fg = *((Pixel *) args[1].addr);
  280.     bg = *((Pixel *) args[2].addr);
  281.     c_map = *((Colormap *) args[3].addr);
  282.  
  283.     number = 1;
  284.     XmuCvtStringToCursor(args, &number, fromVal, &ret_val);
  285.     
  286.     cursor = *((Cursor *) ret_val.addr);
  287.  
  288.     if (cursor == None)
  289.     new_done(Cursor, cursor);
  290.  
  291.     colors[0].pixel = fg;
  292.     colors[1].pixel = bg;
  293.  
  294.     XQueryColors (dpy, c_map, colors, 2);
  295.     XRecolorCursor(dpy, cursor, colors, colors + 1);
  296.     new_done(Cursor, cursor);
  297. }
  298.  
  299.     
  300.     
  301.