home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / rubberband / copycmap.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.0 KB  |  131 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * This file includes the function CopyGlColormap that creates a new
  19.  * colormap for a GlxMDraw widget.  The colors specified
  20.  * by the colorInfo structure (describe in copycmap.h) are allocated
  21.  * in the colormap
  22.  * When possible, they will match the pixel value in parent,
  23.  * to avoid having incorrect colors installed in the X-based portion of
  24.  * the application.
  25.  * This function cannot be called until the widget is realized.
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <X11/Intrinsic.h>
  30. #include <X11/StringDefs.h>
  31. #include <X11/Xirisw/GlxMDraw.h>
  32. #include <X11/Xm/Xm.h>
  33. #include "copycmap.h"
  34.  
  35. CopyGlColormap(glw, colorInfo, colorInfoSize)
  36. Widget glw;
  37. struct glxcColorInfo *colorInfo;
  38. int colorInfoSize;
  39. {
  40.     XVisualInfo *visualInfo;
  41.     int depth;
  42.     int maxcolor;
  43.     Colormap colormap;
  44.     Colormap pmap;
  45.     Arg args[10];
  46.     int n;
  47.     register i;
  48.     XColor rgb_db_def;
  49.     Display *display = XtDisplay(glw);
  50.  
  51.     /* first get the visual and depth*/
  52.     n = 0;
  53.     XtSetArg(args[n], XtNvisual, &visualInfo); n++;
  54.     XtSetArg(args[n], XtNdepth, &depth); n++;
  55.     XtGetValues(glw, args, n);
  56.     maxcolor = 1<<depth;
  57.     /* Create a new colormap */
  58.     colormap = XCreateColormap(display, XtWindow(glw),
  59.                    visualInfo->visual, AllocAll);
  60.     /* Add the colormap to the window */
  61.     n = 0;
  62.     XtSetArg(args[n], XtNcolormap, colormap); n++;
  63.     XtSetValues(glw, args, n);
  64.  
  65.     /* get the parent's colormap */
  66.     n = 0;
  67.     XtSetArg(args[n], XtNcolormap, &pmap); n++;
  68.     XtGetValues(XtParent(glw), args, n);
  69.  
  70.     /* for each color in the colorInfo, determine the correct pixel value */
  71.     for (i=0; i<colorInfoSize; i++)
  72.     {
  73.     colorInfo[i].color.flags = DoRed|DoGreen|DoBlue;
  74.     switch (colorInfo[i].type)
  75.     {
  76.     case GLXC_ABSOLUTE:
  77.         colorInfo[i].color.pixel = (Pixel)colorInfo[i].value;
  78.         XQueryColor (display, pmap, &colorInfo[i].color);
  79.         break;
  80.     case GLXC_NAMED:
  81.         if (!XAllocNamedColor(display, pmap, (char *)colorInfo[i].value,
  82.                   &colorInfo[i].color, &rgb_db_def))
  83.         {
  84.         fprintf (stderr, "could not allocate %s\n", (char *)colorInfo[i].value);
  85.         exit (1);
  86.         }
  87.         break;
  88.     case GLXC_RESOURCE:
  89.         n = 0;
  90.         XtSetArg(args[n], colorInfo[i].value, &colorInfo[i].color.pixel); n++;
  91.         XtGetValues(glw, args, n);
  92.         XQueryColor (display, pmap, &colorInfo[i].color);
  93.         break;
  94.     default:
  95.         fprintf (stderr, "unknown type in colorInfo\n");
  96.         exit (1);
  97.     }
  98.     }
  99.     /* We have determined all the colors.  Loop through the colors and
  100.      * make sure that they fit into the GL colormap.  If they do not,
  101.      * choose another color that fits into the colormap.
  102.      * After this check, store the colors into the colormap,
  103.      * and save in the variables */
  104.     for (i=0; i<colorInfoSize; i++)
  105.     {
  106.     if (colorInfo[i].color.pixel >= maxcolor)
  107.     {
  108.         /* it was too high.  find another pixel to use.
  109.          * start at the highest color and work down
  110.          */
  111.         register try, check;
  112.  
  113.         for (try = maxcolor-1; try >= 0 ; try--)
  114.         {
  115.         for (check = 0; check < colorInfoSize; check++)
  116.         {
  117.             if (colorInfo[check].color.pixel == try)
  118.             break;
  119.         }
  120.         if (check == colorInfoSize) /* didn't find a match */
  121.         {
  122.             colorInfo[i].color.pixel = try;
  123.             break;
  124.         }
  125.         }
  126.     }
  127.     XStoreColor(display, colormap, &colorInfo[i].color);
  128.     *colorInfo[i].result = (Colorindex)colorInfo[i].color.pixel;
  129.     }
  130. }    
  131.