home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / StrToCurs.c.orig < prev    next >
Encoding:
Text File  |  1991-07-25  |  7.8 KB  |  267 lines

  1. /* $XConsortium: StrToCurs.c,v 1.16 91/07/25 17:48:36 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/IntrinsicP.h>    /* 'cause CoreP.h needs it */
  28. #include    <X11/CoreP.h>        /* just to do XtConvert() */
  29. #include    <X11/StringDefs.h>
  30. #include    <X11/Xmu/Converters.h>
  31. #include    <X11/Xmu/Drawing.h>
  32.  
  33. #ifndef X_NOT_POSIX
  34. #ifdef _POSIX_SOURCE
  35. #include <limits.h>
  36. #else
  37. #define _POSIX_SOURCE
  38. #include <limits.h>
  39. #undef _POSIX_SOURCE
  40. #endif
  41. #endif /* X_NOT_POSIX */
  42. #ifndef PATH_MAX
  43. #include <sys/param.h>
  44. #ifdef MAXPATHLEN
  45. #define PATH_MAX MAXPATHLEN
  46. #else
  47. #define PATH_MAX 1024
  48. #endif
  49. #endif /* PATH_MAX */
  50.  
  51. /*
  52.  * XmuConvertStringToCursor:
  53.  *
  54.  * allows String to specify a standard cursor name (from cursorfont.h), a
  55.  * font name and glyph index of the form "FONT fontname index [[font] index]", 
  56.  * or a bitmap file name (absolute, or relative to the global resource
  57.  * bitmapFilePath, class BitmapFilePath).  If the resource is not
  58.  * defined, the default value is the build symbol BITMAPDIR.
  59.  *
  60.  * shares lots of code with XmuCvtStringToPixmap, but unfortunately
  61.  * can't use it as the hotspot info is lost.
  62.  *
  63.  * To use, include the following in your ClassInitialize procedure:
  64.  
  65. static XtConvertArgRec screenConvertArg[] = {
  66.     {XtBaseOffset, (XtPointer) XtOffsetOf(WidgetRec, core.screen),
  67.      sizeof(Screen *)}
  68. };
  69.  
  70.     XtAddConverter(XtRString, XtRCursor, XmuCvtStringToCursor,      
  71.            screenConvertArg, XtNumber(screenConvertArg));
  72.  *
  73.  */
  74.  
  75. #define    done(address, type) \
  76.     { (*toVal).size = sizeof(type); (*toVal).addr = (XPointer) address; }
  77.  
  78. #define FONTSPECIFIER        "FONT "
  79.  
  80. /*ARGSUSED*/
  81. void XmuCvtStringToCursor(args, num_args, fromVal, toVal)
  82.     XrmValuePtr args;
  83.     Cardinal    *num_args;
  84.     XrmValuePtr    fromVal;
  85.     XrmValuePtr    toVal;
  86. {
  87.     static Cursor cursor;        /* static for cvt magic */
  88.     char *name = (char *)fromVal->addr;
  89.     Screen *screen;
  90.     register int i;
  91.     char maskname[PATH_MAX];
  92.     Pixmap source, mask;
  93.     /* XXX - make fg/bg resources */
  94.     static XColor bgColor = {0, 0xffff, 0xffff, 0xffff};
  95.     static XColor fgColor = {0, 0, 0, 0};
  96.     int xhot, yhot;
  97.     int len;
  98.  
  99.  
  100.     if (*num_args != 1)
  101.      XtErrorMsg("wrongParameters","cvtStringToCursor","XtToolkitError",
  102.              "String to cursor conversion needs screen argument",
  103.               (String *)NULL, (Cardinal *)NULL);
  104.  
  105.     screen = *((Screen **) args[0].addr);
  106.  
  107.     if (0 == strncmp(FONTSPECIFIER, name, strlen(FONTSPECIFIER))) {
  108.     char source_name[PATH_MAX], mask_name[PATH_MAX];
  109.     int source_char, mask_char, fields;
  110.     WidgetRec widgetRec;
  111.     Font source_font, mask_font;
  112.     XrmValue fromString, toFont;
  113.  
  114.     fields = sscanf(name, "FONT %s %d %s %d",
  115.             source_name, &source_char,
  116.             mask_name, &mask_char);
  117.     if (fields < 2) {
  118.         XtStringConversionWarning(name, XtRCursor);
  119.         return;
  120.     }
  121.  
  122.     /* widgetRec is stupid; we should just use XtDirectConvert,
  123.      * but the names in Xt/Converters aren't public. */
  124.     widgetRec.core.screen = screen;
  125.     fromString.addr = source_name;
  126.     fromString.size = strlen(source_name);
  127.     XtConvert(&widgetRec, XtRString, &fromString, XtRFont, &toFont);
  128.     if (toFont.addr == NULL) {
  129.         XtStringConversionWarning(name, XtRCursor);
  130.         return;
  131.     }
  132.     source_font = *(Font*)toFont.addr;
  133.  
  134.     switch (fields) {
  135.       case 2:        /* defaulted mask font & char */
  136.         mask_font = source_font;
  137.         mask_char = source_char;
  138.         break;
  139.  
  140.       case 3:        /* defaulted mask font */
  141.         mask_font = source_font;
  142.         mask_char = atoi(mask_name);
  143.         break;
  144.  
  145.       case 4:        /* specified mask font & char */
  146.         fromString.addr = mask_name;
  147.         fromString.size = strlen(mask_name);
  148.         XtConvert(&widgetRec, XtRString, &fromString, XtRFont, &toFont);
  149.         if (toFont.addr == NULL) {
  150.         XtStringConversionWarning(name, XtRCursor);
  151.         return;
  152.         }
  153.         mask_font = *(Font*)toFont.addr;
  154.     }
  155.  
  156.     cursor = XCreateGlyphCursor( DisplayOfScreen(screen), source_font,
  157.                      mask_font, source_char, mask_char,
  158.                      &fgColor, &bgColor );
  159.     done(&cursor, Cursor);
  160.     return;
  161.     }
  162.  
  163.     i = XmuCursorNameToIndex (name);
  164.     if (i != -1) {
  165.     cursor = XCreateFontCursor (DisplayOfScreen(screen), i);
  166.     done(&cursor, Cursor);
  167.     return;
  168.     }
  169.  
  170.     if ((source = XmuLocateBitmapFile (screen, name, 
  171.                        maskname, (sizeof maskname) - 4,
  172.                        NULL, NULL, &xhot, &yhot)) == None) {
  173.     XtStringConversionWarning (name, XtRCursor);
  174.     }
  175.     len = strlen (maskname);
  176.     for (i = 0; i < 2; i++) {
  177.     strcpy (maskname + len, i == 0 ? "Mask" : "msk");
  178.     if ((mask = XmuLocateBitmapFile (screen, maskname, NULL, 0, 
  179.                      NULL, NULL, NULL, NULL)) != None)
  180.       break;
  181.     }
  182.  
  183.     cursor = XCreatePixmapCursor( DisplayOfScreen(screen), source, mask,
  184.                   &fgColor, &bgColor, xhot, yhot );
  185.     XFreePixmap( DisplayOfScreen(screen), source );
  186.     if (mask != None) XFreePixmap( DisplayOfScreen(screen), mask );
  187.  
  188.     done(&cursor, Cursor);
  189. }
  190.  
  191. #define    new_done(type, value) \
  192.     {                            \
  193.         if (toVal->addr != NULL) {                \
  194.         if (toVal->size < sizeof(type)) {        \
  195.             toVal->size = sizeof(type);            \
  196.             return False;                \
  197.         }                        \
  198.         *(type*)(toVal->addr) = (value);        \
  199.         }                            \
  200.         else {                        \
  201.         static type static_val;                \
  202.         static_val = (value);                \
  203.         toVal->addr = (XPointer)&static_val;        \
  204.         }                            \
  205.         toVal->size = sizeof(type);                \
  206.         return True;                    \
  207.     }
  208.  
  209. /*    Function Name: XmuCvtStringToColorCursor
  210.  *    Description: Converts a string into a colored cursor.
  211.  *    Arguments: dpy
  212.  *           args - an argument list (see below).
  213.  *                 num_args - number of elements in the argument list.
  214.  *                 fromVal - value to convert from.
  215.  *                 toVal - value to convert to.
  216.  *           data
  217.  *    Returns:   True or False
  218.  */
  219.  
  220. /*ARGSUSED*/
  221. Boolean
  222. XmuCvtStringToColorCursor(dpy, args, num_args, fromVal, toVal, converter_data)
  223.     Display     *dpy;
  224.     XrmValuePtr args;
  225.     Cardinal    *num_args;
  226.     XrmValuePtr    fromVal;
  227.     XrmValuePtr    toVal;
  228.     XtPointer   *converter_data;    /* unused */
  229. {
  230.     Cursor cursor;
  231.     Pixel fg, bg;
  232.     Colormap c_map;
  233.     XColor colors[2];
  234.     Cardinal number;
  235.     XrmValue ret_val;
  236.  
  237.     if (*num_args != 4) {
  238.     XtAppWarningMsg(XtDisplayToApplicationContext(dpy),
  239.         "wrongParameters","cvtStringToColorCursor","XmuError",
  240.             "String to color cursor conversion needs four arguments",
  241.         (String *)NULL, (Cardinal *)NULL);
  242.     return False;
  243.     }
  244.  
  245.     fg = *((Pixel *) args[1].addr);
  246.     bg = *((Pixel *) args[2].addr);
  247.     c_map = *((Colormap *) args[3].addr);
  248.  
  249.     number = 1;
  250.     XmuCvtStringToCursor(args, &number, fromVal, &ret_val);
  251.     
  252.     cursor = *((Cursor *) ret_val.addr);
  253.  
  254.     if (cursor == None)
  255.     new_done(Cursor, cursor);
  256.  
  257.     colors[0].pixel = fg;
  258.     colors[1].pixel = bg;
  259.  
  260.     XQueryColors (dpy, c_map, colors, 2);
  261.     XRecolorCursor(dpy, cursor, colors, colors + 1);
  262.     new_done(Cursor, cursor);
  263. }
  264.  
  265.     
  266.     
  267.