home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / StrToBmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-09  |  3.9 KB  |  124 lines

  1. /* static char rcsid[] = "$XConsortium: StrToBmap.c,v 1.11 91/03/09 17:08:48 rws Exp $"; */
  2.  
  3.  
  4. /***********************************************************
  5. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  6. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  7.  
  8.                         All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the names of Digital or MIT not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25.  
  26. ******************************************************************/
  27.  
  28. #include    <X11/Intrinsic.h>
  29. #include    <X11/StringDefs.h>
  30. #include    <X11/Xmu/Converters.h>
  31. #include    <X11/Xmu/Drawing.h>
  32.  
  33.  
  34. /*
  35.  * XmuConvertStringToBitmap:
  36.  *
  37.  * creates a depth-1 Pixmap suitable for window manager icons.
  38.  * "string" represents a bitmap(1) filename which may be absolute,
  39.  * or relative to the global resource bitmapFilePath, class
  40.  * BitmapFilePath.  If the resource is not defined, the default
  41.  * value is the build symbol BITMAPDIR.
  42.  *
  43.  * shares lots of code with XmuConvertStringToCursor.  
  44.  *
  45.  * To use, include the following in your ClassInitialize procedure:
  46.  
  47. static XtConvertArgRec screenConvertArg[] = {
  48.     {XtBaseOffset, (caddr_t) XtOffset(Widget, core.screen), sizeof(Screen *)}
  49. };
  50.  
  51.     XtAddConverter("String", "Bitmap", XmuCvtStringToBitmap,
  52.            screenConvertArg, XtNumber(screenConvertArg));
  53.  *
  54.  */
  55.  
  56. #define    done(address, type) \
  57.     { (*toVal).size = sizeof(type); (*toVal).addr = (caddr_t) address; }
  58.  
  59.  
  60. /*ARGSUSED*/
  61. void XmuCvtStringToBitmap(args, num_args, fromVal, toVal)
  62.     XrmValuePtr args;
  63.     Cardinal    *num_args;
  64.     XrmValuePtr    fromVal;
  65.     XrmValuePtr    toVal;
  66. {
  67.     static Pixmap pixmap;        /* static for cvt magic */
  68.     char *name = (char *)fromVal->addr;
  69.     Screen *screen;
  70.     Display *dpy;
  71.     XrmDatabase db;
  72.     String fn;
  73.     unsigned int width, height;
  74.     int xhot, yhot;
  75.     unsigned char *data;
  76.  
  77.     if (*num_args != 1)
  78.      XtErrorMsg("wrongParameters","cvtStringToBitmap","XtToolkitError",
  79.              "String to pixmap conversion needs screen argument",
  80.               (String *)NULL, (Cardinal *)NULL);
  81.  
  82.     if (strcmp(name, "None") == 0) {
  83.     pixmap = None;
  84.     done(&pixmap, Pixmap);
  85.     return;
  86.     }
  87.  
  88.     if (strcmp(name, "ParentRelative") == 0) {
  89.     pixmap = ParentRelative;
  90.     done(&pixmap, Pixmap);
  91.     return;
  92.     }
  93.  
  94.     screen = *((Screen **) args[0].addr);
  95.     pixmap = XmuLocateBitmapFile (screen, name,
  96.                   NULL, 0, NULL, NULL, NULL, NULL);
  97.     if (pixmap == None) {
  98.     dpy = DisplayOfScreen(screen);
  99.     db = XrmGetDatabase(dpy);
  100.     XrmSetDatabase(dpy, XtScreenDatabase(screen));
  101.     fn = XtResolvePathname(dpy, "bitmaps", name, "", NULL, NULL, 0, NULL);
  102.     if (!fn)
  103.         fn = XtResolvePathname(dpy, "", name, ".xbm", NULL, NULL, 0, NULL);
  104.     XrmSetDatabase(dpy, db);
  105.     if (fn &&
  106.         XmuReadBitmapDataFromFile (fn, &width, &height, &data,
  107.                        &xhot, &yhot) == BitmapSuccess) {
  108.         pixmap = XCreatePixmapFromBitmapData (dpy,
  109.                           RootWindowOfScreen(screen),
  110.                           (char *) data, width, height,
  111.                           1, 0, 1);
  112.         XFree ((char *)data);
  113.     }
  114.     }
  115.  
  116.     if (pixmap != None) {
  117.     done (&pixmap, Pixmap);
  118.     } else {
  119.     XtStringConversionWarning (name, "Pixmap");
  120.     return;
  121.     }
  122. }
  123.  
  124.