home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / VisCmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-08  |  5.8 KB  |  168 lines

  1. /* $XConsortium: VisCmap.c,v 1.10 89/10/08 15:05:47 rws Exp $ 
  2.  * 
  3.  * Copyright 1989 by the Massachusetts Institute of Technology
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose and without fee is hereby granted, provided 
  7.  * that the above copyright notice appear in all copies and that both that 
  8.  * copyright notice and this permission notice appear in supporting 
  9.  * documentation, and that the name of M.I.T. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific, 
  11.  * written prior permission. M.I.T. makes no representations about the 
  12.  * suitability of this software for any purpose.  It is provided "as is"
  13.  * without express or implied warranty.
  14.  *
  15.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  17.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  20.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  * Author:  Donna Converse, MIT X Consortium
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <math.h>
  27. #include <X11/Xlib.h>
  28. #include <X11/Xatom.h>
  29. #include <X11/Xutil.h>
  30. #include <X11/Xmu/StdCmap.h>
  31.  
  32. /*
  33.  * To create all of the appropriate standard colormaps for a given visual on
  34.  * a given screen, use XmuVisualStandardColormaps.
  35.  * 
  36.  * Define all appropriate standard colormap properties for the given visual.
  37.  * If replace is true, any previous definition will be removed.
  38.  * If retain is true, new properties will be retained for the duration of
  39.  * the server session.  Return 0 on failure, non-zero on success.
  40.  * On failure, no new properties will be defined, and, old ones may have
  41.  * been removed if replace was True.
  42.  *
  43.  * Not all standard colormaps are meaningful to all visual classes.  This
  44.  * routine will check and define the following properties for the following
  45.  * classes, provided that the size of the colormap is not too small.
  46.  *
  47.  *    DirectColor and PseudoColor
  48.  *        RGB_DEFAULT_MAP
  49.  *        RGB_BEST_MAP
  50.  *        RGB_RED_MAP
  51.  *        RGB_GREEN_MAP
  52.  *         RGB_BLUE_MAP
  53.  *          RGB_GRAY_MAP
  54.  *
  55.  *    TrueColor and StaticColor
  56.  *        RGB_BEST_MAP
  57.  *
  58.  *    GrayScale and StaticGray
  59.  *        RGB_GRAY_MAP
  60.  */
  61.  
  62. Status XmuVisualStandardColormaps(dpy, screen, visualid, depth, replace,
  63.                   retain)
  64.     Display        *dpy;        /* specifies server connection */
  65.     int            screen;        /* specifies screen number */
  66.     VisualID        visualid;    /* specifies the visual */
  67.     unsigned int    depth;        /* specifies the visual */
  68.     Bool        replace;    /* specifies whether to replace */
  69.     Bool        retain;        /* specifies whether to retain */
  70. {
  71.     Status        status;
  72.     int            n;
  73.     long        vinfo_mask;
  74.     XVisualInfo        vinfo_template, *vinfo;
  75.         
  76.     vinfo_template.screen = screen;
  77.     vinfo_template.visualid = visualid;
  78.     vinfo_template.depth = depth;
  79.     vinfo_mask = VisualScreenMask | VisualIDMask | VisualDepthMask;
  80.     if ((vinfo = XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &n)) == NULL)
  81.     return 0;
  82.  
  83.     if (vinfo->colormap_size <= 2) {
  84.     /* Monochrome visuals have no standard maps; considered successful */
  85.     XFree((char *) vinfo);
  86.     return 1;
  87.     }
  88.  
  89.     switch (vinfo->class)
  90.     {
  91.       case PseudoColor:
  92.       case DirectColor:
  93.     status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  94.                        XA_RGB_DEFAULT_MAP, replace,retain);
  95.     if (!status) break;
  96.  
  97.     status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  98.                        XA_RGB_GRAY_MAP, replace, retain);
  99.     if (!status) {
  100.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  101.         break;
  102.     }
  103.  
  104.     status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  105.                        XA_RGB_RED_MAP, replace, retain);
  106.     if (!status) {
  107.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  108.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
  109.         break;
  110.     }
  111.  
  112.     status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  113.                        XA_RGB_GREEN_MAP, replace, retain);
  114.     if (!status) {
  115.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  116.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
  117.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_RED_MAP);
  118.         break;
  119.     }
  120.  
  121.     status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  122.                        XA_RGB_BLUE_MAP, replace, retain);
  123.     if (!status) {
  124.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  125.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
  126.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_RED_MAP);
  127.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_GREEN_MAP);
  128.         break;
  129.     }
  130.     /* fall through */
  131.  
  132.       case StaticColor:
  133.       case TrueColor:
  134.  
  135.     status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  136.                        XA_RGB_BEST_MAP, replace, retain);
  137.     if (!status && (vinfo->class == PseudoColor || 
  138.             vinfo->class == DirectColor)) {
  139.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  140.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_GRAY_MAP);
  141.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_RED_MAP);
  142.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_GREEN_MAP);
  143.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_BLUE_MAP);
  144.     }
  145.     break;
  146.     /* the end for PseudoColor, DirectColor, StaticColor, and TrueColor */
  147.  
  148.       case GrayScale:
  149.     status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  150.                        XA_RGB_DEFAULT_MAP, replace,
  151.                        retain);
  152.     if (! status) break;
  153.     /* fall through */
  154.  
  155.       case StaticGray:
  156.  
  157.     status = XmuLookupStandardColormap(dpy, screen, visualid, depth,
  158.                        XA_RGB_GRAY_MAP, replace, retain);
  159.     if (! status && vinfo->class == GrayScale) {
  160.         XmuDeleteStandardColormap(dpy, screen, XA_RGB_DEFAULT_MAP);
  161.         break;
  162.     }
  163.     }
  164.  
  165.     XFree((char *) vinfo);
  166.     return status;
  167. }
  168.