]> Pileus Git - ~andy/gtk/blobdiff - gdk/x11/gdkcolor-x11.c
doc comment fixes.
[~andy/gtk] / gdk / x11 / gdkcolor-x11.c
index 13b99e0ba0b3eb36d1ec0189f0b9c16a492fbd4c..57c84a95265f8c1cfbe1928c130720a71fa16106 100644 (file)
 
 #include "gdkcolor.h"
 #include "gdkinternals.h"
+#include "gdkx.h"
 #include "gdkprivate-x11.h"
+#include "gdkscreen-x11.h"
 
 typedef struct _GdkColormapPrivateX11  GdkColormapPrivateX11;
 
 struct _GdkColormapPrivateX11
 {
+  GdkScreen *screen;
   Colormap xcolormap;
   Display *xdisplay;
   gint private_val;
@@ -41,6 +44,8 @@ struct _GdkColormapPrivateX11
   GHashTable *hash;
   GdkColorInfo *info;
   time_t last_sync_time;
+
+  guint foreign : 1;
 };
 
 #define GDK_COLORMAP_PRIVATE_DATA(cmap) ((GdkColormapPrivateX11 *) GDK_COLORMAP (cmap)->windowing_data)
@@ -50,6 +55,10 @@ static gint     gdk_colormap_match_color (GdkColormap *cmap,
                                          const gchar *available);
 static void     gdk_colormap_add         (GdkColormap *cmap);
 static void     gdk_colormap_remove      (GdkColormap *cmap);
+
+static GdkColormap *gdk_colormap_lookup   (GdkScreen   *screen,
+                                          Colormap     xcolormap);
+
 static guint    gdk_colormap_hash        (Colormap    *cmap);
 static gboolean gdk_colormap_equal       (Colormap    *a,
                                          Colormap    *b);
@@ -62,8 +71,6 @@ static void gdk_colormap_finalize   (GObject              *object);
 
 static gpointer parent_class = NULL;
 
-static GHashTable *colormap_hash = NULL;
-
 GType
 gdk_colormap_get_type (void)
 {
@@ -101,7 +108,7 @@ gdk_colormap_init (GdkColormap *colormap)
 
   colormap->windowing_data = private;
   
-  private->xdisplay = gdk_display;
+  private->screen = NULL;
   private->hash = NULL;
   private->last_sync_time = 0;
   private->info = NULL;
@@ -128,7 +135,8 @@ gdk_colormap_finalize (GObject *object)
 
   gdk_colormap_remove (colormap);
 
-  XFreeColormap (private->xdisplay, private->xcolormap);
+  if (!private->screen->closed)
+    XFreeColormap (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap);
 
   if (private->hash)
     g_hash_table_destroy (private->hash);
@@ -139,13 +147,26 @@ gdk_colormap_finalize (GObject *object)
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
+/**
+ * gdk_colormap_new:
+ * @visual: a #GdkVisual.
+ * @allocate: if %TRUE, the newly created colormap will be
+ * a private colormap, and all colors in it will be
+ * allocated for the applications use.
+ * 
+ * Creates a new colormap for the given visual.
+ * 
+ * Return value: the new #GdkColormap.
+ **/
 GdkColormap*
 gdk_colormap_new (GdkVisual *visual,
-                 gboolean   private_cmap)
+                 gboolean   allocate)
 {
   GdkColormap *colormap;
   GdkColormapPrivateX11 *private;
   Visual *xvisual;
+  Display *xdisplay;
+  Window xrootwin;
   int size;
   int i;
 
@@ -155,12 +176,15 @@ gdk_colormap_new (GdkVisual *visual,
   
   g_return_val_if_fail (visual != NULL, NULL);
 
-  colormap = g_object_new (gdk_colormap_get_type (), NULL);
+  colormap = g_object_new (GDK_TYPE_COLORMAP, NULL);
   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
   colormap->visual = visual;
+  private->screen = gdk_visual_get_screen (visual);
   
   xvisual = ((GdkVisualPrivate*) visual)->xvisual;
+  xdisplay = GDK_SCREEN_XDISPLAY (private->screen);
+  xrootwin = GDK_SCREEN_XROOTWIN (private->screen);
 
   colormap->size = visual->colormap_size;
 
@@ -174,21 +198,21 @@ gdk_colormap_new (GdkVisual *visual,
       private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
                                        (GEqualFunc) gdk_color_equal);
       
-      private->private_val = private_cmap;
-      private->xcolormap = XCreateColormap (private->xdisplay, _gdk_root_window,
-                                           xvisual, (private_cmap) ? (AllocAll) : (AllocNone));
+      private->private_val = allocate;
+      private->xcolormap = XCreateColormap (xdisplay, xrootwin,
+                                           xvisual, (allocate) ? (AllocAll) : (AllocNone));
 
-      if (private_cmap)
+      if (allocate)
        {
          XColor *default_colors;
 
-         default_colors = g_new (XColor, colormap->size);
+         default_colors = g_new (XColor, colormap->size);
 
          for (i = 0; i < colormap->size; i++)
            default_colors[i].pixel = i;
-
-         XQueryColors (private->xdisplay,
-                       DefaultColormap (private->xdisplay, _gdk_screen),
+         
+         XQueryColors (xdisplay,
+                       DefaultColormapOfScreen (GDK_SCREEN_X11 (private->screen)->xscreen),
                        default_colors, colormap->size);
 
          for (i = 0; i < colormap->size; i++)
@@ -207,7 +231,7 @@ gdk_colormap_new (GdkVisual *visual,
 
     case GDK_VISUAL_DIRECT_COLOR:
       private->private_val = TRUE;
-      private->xcolormap = XCreateColormap (private->xdisplay, _gdk_root_window,
+      private->xcolormap = XCreateColormap (xdisplay, xrootwin,
                                            xvisual, AllocAll);
       colormap->colors = g_new (GdkColor, colormap->size);
 
@@ -229,7 +253,7 @@ gdk_colormap_new (GdkVisual *visual,
     case GDK_VISUAL_STATIC_GRAY:
     case GDK_VISUAL_STATIC_COLOR:
       private->private_val = FALSE;
-      private->xcolormap = XCreateColormap (private->xdisplay, _gdk_root_window,
+      private->xcolormap = XCreateColormap (xdisplay, xrootwin,
                                            xvisual, AllocNone);
       
       colormap->colors = g_new (GdkColor, colormap->size);
@@ -238,7 +262,7 @@ gdk_colormap_new (GdkVisual *visual,
       
     case GDK_VISUAL_TRUE_COLOR:
       private->private_val = FALSE;
-      private->xcolormap = XCreateColormap (private->xdisplay, _gdk_root_window,
+      private->xcolormap = XCreateColormap (xdisplay, xrootwin,
                                            xvisual, AllocNone);
       break;
     }
@@ -248,25 +272,13 @@ gdk_colormap_new (GdkVisual *visual,
   return colormap;
 }
 
-#define MIN_SYNC_TIME 2
-
 static void
-gdk_colormap_sync (GdkColormap *colormap,
-                  gboolean     force)
+gdk_colormap_sync_palette (GdkColormap *colormap)
 {
-  time_t current_time;
   GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
   XColor *xpalette;
   gint nlookup;
   gint i;
-  
-  g_return_if_fail (GDK_IS_COLORMAP (colormap));
-
-  current_time = time (NULL);
-  if (!force && ((current_time - private->last_sync_time) < MIN_SYNC_TIME))
-    return;
-
-  private->last_sync_time = current_time;
 
   nlookup = 0;
   xpalette = g_new (XColor, colormap->size);
@@ -282,8 +294,9 @@ gdk_colormap_sync (GdkColormap *colormap,
          nlookup++;
        }
     }
-  
-  XQueryColors (gdk_display, private->xcolormap, xpalette, nlookup);
+
+  XQueryColors (GDK_SCREEN_XDISPLAY (private->screen),
+               private->xcolormap, xpalette, nlookup);
   
   for (i = 0; i < nlookup; i++)
     {
@@ -296,61 +309,152 @@ gdk_colormap_sync (GdkColormap *colormap,
   
   g_free (xpalette);
 }
-                  
 
-GdkColormap*
-gdk_colormap_get_system (void)
+static void
+gdk_colormap_sync_direct_color (GdkColormap *colormap)
 {
-  static GdkColormap *colormap = NULL;
-  GdkColormapPrivateX11 *private;
+  GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+  GdkVisual *visual = colormap->visual;
+  XColor *xpalette;
+  gint i;
 
-  if (!colormap)
+  xpalette = g_new (XColor, colormap->size);
+  
+  for (i = 0; i < colormap->size; i++)
     {
-      colormap = g_object_new (gdk_colormap_get_type (), NULL);
-      private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+      xpalette[i].pixel =
+       (((i << visual->red_shift)   & visual->red_mask)   |
+        ((i << visual->green_shift) & visual->green_mask) |
+        ((i << visual->blue_shift)  & visual->blue_mask));
+    }
 
-      private->xdisplay = gdk_display;
-      private->xcolormap = DefaultColormap (gdk_display, _gdk_screen);
-      colormap->visual = gdk_visual_get_system ();
-      private->private_val = FALSE;
+  XQueryColors (GDK_SCREEN_XDISPLAY (private->screen),
+               private->xcolormap, xpalette, colormap->size);
+  
+  for (i = 0; i < colormap->size; i++)
+    {
+      colormap->colors[i].pixel = xpalette[i].pixel;
+      colormap->colors[i].red = xpalette[i].red;
+      colormap->colors[i].green = xpalette[i].green;
+      colormap->colors[i].blue = xpalette[i].blue;
+    }
+  
+  g_free (xpalette);
+}
 
-      private->hash = NULL;
-      private->last_sync_time = 0;
-      private->info = NULL;
+#define MIN_SYNC_TIME 2
 
-      colormap->colors = NULL;
-      colormap->size = colormap->visual->colormap_size;
+static void
+gdk_colormap_sync (GdkColormap *colormap,
+                  gboolean     force)
+{
+  time_t current_time;
+  GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
-      switch (colormap->visual->type)
-       {
-       case GDK_VISUAL_GRAYSCALE:
-       case GDK_VISUAL_PSEUDO_COLOR:
-         private->info = g_new0 (GdkColorInfo, colormap->size);
-         private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
-                                           (GEqualFunc) gdk_color_equal);
-         /* Fall through */
-       case GDK_VISUAL_STATIC_GRAY:
-       case GDK_VISUAL_STATIC_COLOR:
-         colormap->colors = g_new (GdkColor, colormap->size);
-         gdk_colormap_sync (colormap, TRUE);
-         
-       case GDK_VISUAL_DIRECT_COLOR:
-       case GDK_VISUAL_TRUE_COLOR:
-         break;
-       }
+  g_return_if_fail (GDK_IS_COLORMAP (colormap));
 
-      gdk_colormap_add (colormap);
-    }
+  if (private->screen->closed)
+    return;
+
+  current_time = time (NULL);
+  if (!force && ((current_time - private->last_sync_time) < MIN_SYNC_TIME))
+    return;
+
+  private->last_sync_time = current_time;
+
+  if (colormap->visual->type == GDK_VISUAL_DIRECT_COLOR)
+    gdk_colormap_sync_direct_color (colormap);
+  else
+    gdk_colormap_sync_palette (colormap);
+}
+                  
+/**
+ * gdk_screen_get_system_colormap:
+ * @screen: a #GdkScreen
+ *
+ * Gets the system's default colormap for @screen
+ *
+ * Returns: the default colormap for @screen.
+ */
+GdkColormap *
+gdk_screen_get_system_colormap (GdkScreen *screen)
+{
+  GdkColormap *colormap = NULL;
+  GdkColormapPrivateX11 *private;
+  GdkScreenX11 *screen_x11;
+
+  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
+  screen_x11 = GDK_SCREEN_X11 (screen);
+
+  if (screen_x11->system_colormap)
+    return screen_x11->system_colormap;
 
+  colormap = g_object_new (GDK_TYPE_COLORMAP, NULL);
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+  private->screen = screen;
+  colormap->visual = gdk_screen_get_system_visual (screen);
+  
+  private->xcolormap = DefaultColormapOfScreen (screen_x11->xscreen);
+  private->private_val = FALSE;
+
+  private->hash = NULL;
+  private->last_sync_time = 0;
+  private->info = NULL;
+
+  colormap->colors = NULL;
+  colormap->size = colormap->visual->colormap_size;
+
+  switch (colormap->visual->type)
+    {
+    case GDK_VISUAL_GRAYSCALE:
+    case GDK_VISUAL_PSEUDO_COLOR:
+      private->info = g_new0 (GdkColorInfo, colormap->size);
+      private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
+                                       (GEqualFunc) gdk_color_equal);
+      /* Fall through */
+    case GDK_VISUAL_STATIC_GRAY:
+    case GDK_VISUAL_STATIC_COLOR:
+    case GDK_VISUAL_DIRECT_COLOR:
+      colormap->colors = g_new (GdkColor, colormap->size);
+      gdk_colormap_sync (colormap, TRUE);
+      
+    case GDK_VISUAL_TRUE_COLOR:
+      break;
+    }
+  
+  gdk_colormap_add (colormap);
+  screen_x11->system_colormap = colormap;
+  
   return colormap;
 }
 
+/**
+ * gdk_colormap_get_system_size:
+ * 
+ * Returns the size of the system's default colormap.
+ * (See the description of struct #GdkColormap for an
+ * explanation of the size of a colormap.)
+ * 
+ * Return value: the size of the system's default colormap.
+ **/
 gint
 gdk_colormap_get_system_size (void)
 {
-  return DisplayCells (gdk_display, _gdk_screen);
+  return DisplayCells (GDK_SCREEN_XDISPLAY (gdk_screen_get_default()),
+                      GDK_SCREEN_X11 (gdk_screen_get_default())->screen_num);
 }
 
+/**
+ * gdk_colormap_change:
+ * @colormap: a #GdkColormap.
+ * @ncolors: the number of colors to change.
+ * 
+ * Changes the value of the first @ncolors in a private colormap
+ * to match the values in the <structfield>colors</structfield>
+ * array in the colormap. This function is obsolete and
+ * should not be used. See gdk_color_change().
+ **/
 void
 gdk_colormap_change (GdkColormap *colormap,
                     gint         ncolors)
@@ -358,6 +462,7 @@ gdk_colormap_change (GdkColormap *colormap,
   GdkColormapPrivateX11 *private;
   GdkVisual *visual;
   XColor *palette;
+  Display *xdisplay;
   gint shift;
   int max_colors;
   int size;
@@ -365,9 +470,14 @@ gdk_colormap_change (GdkColormap *colormap,
 
   g_return_if_fail (GDK_IS_COLORMAP (colormap));
 
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+  if (private->screen->closed)
+    return;
+
+  xdisplay = GDK_SCREEN_XDISPLAY (private->screen);
   palette = g_new (XColor, ncolors);
 
-  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
   switch (colormap->visual->type)
     {
     case GDK_VISUAL_GRAYSCALE:
@@ -381,7 +491,7 @@ gdk_colormap_change (GdkColormap *colormap,
          palette[i].flags = DoRed | DoGreen | DoBlue;
        }
 
-      XStoreColors (private->xdisplay, private->xcolormap, palette, ncolors);
+      XStoreColors (xdisplay, private->xcolormap, palette, ncolors);
       break;
 
     case GDK_VISUAL_DIRECT_COLOR:
@@ -398,7 +508,7 @@ gdk_colormap_change (GdkColormap *colormap,
          palette[i].flags = DoRed;
        }
 
-      XStoreColors (private->xdisplay, private->xcolormap, palette, size);
+      XStoreColors (xdisplay, private->xcolormap, palette, size);
 
       shift = visual->green_shift;
       max_colors = 1 << visual->green_prec;
@@ -411,7 +521,7 @@ gdk_colormap_change (GdkColormap *colormap,
          palette[i].flags = DoGreen;
        }
 
-      XStoreColors (private->xdisplay, private->xcolormap, palette, size);
+      XStoreColors (xdisplay, private->xcolormap, palette, size);
 
       shift = visual->blue_shift;
       max_colors = 1 << visual->blue_prec;
@@ -424,7 +534,7 @@ gdk_colormap_change (GdkColormap *colormap,
          palette[i].flags = DoBlue;
        }
 
-      XStoreColors (private->xdisplay, private->xcolormap, palette, size);
+      XStoreColors (xdisplay, private->xcolormap, palette, size);
       break;
 
     default:
@@ -434,6 +544,24 @@ gdk_colormap_change (GdkColormap *colormap,
   g_free (palette);
 }
 
+/**
+ * gdk_colors_alloc:
+ * @colormap: a #GdkColormap.
+ * @contiguous: if %TRUE, the colors should be allocated
+ *    in contiguous color cells.
+ * @planes: an array in which to store the plane masks.
+ * @nplanes: the number of planes to allocate. (Or zero,
+ *    to indicate that the color allocation should not be planar.)
+ * @pixels: an array into which to store allocated pixel values.
+ * @npixels: the number of pixels in each plane to allocate.
+ * 
+ * Allocates colors from a colormap. This function
+ * is obsolete. See gdk_colormap_alloc_colors().
+ * For full documentation of the fields, see 
+ * the Xlib documentation for <function>XAllocColorCells()</function>.
+ * 
+ * Return value: 
+ **/
 gboolean
 gdk_colors_alloc (GdkColormap   *colormap,
                  gboolean       contiguous,
@@ -450,9 +578,12 @@ gdk_colors_alloc (GdkColormap   *colormap,
 
   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
-  return_val = XAllocColorCells (private->xdisplay, private->xcolormap,
-                                contiguous, planes, nplanes, pixels, npixels);
+  if (private->screen->closed)
+    return FALSE;
 
+  return_val = XAllocColorCells (GDK_SCREEN_XDISPLAY (private->screen),
+                                private->xcolormap,contiguous, planes,
+                                nplanes, pixels, npixels);
   if (return_val)
     {
       for (i=0; i<npixels; i++)
@@ -465,48 +596,34 @@ gdk_colors_alloc (GdkColormap   *colormap,
   return return_val != 0;
 }
 
-gboolean
-gdk_color_parse (const gchar *spec,
-                GdkColor *color)
-{
-  Colormap xcolormap;
-  XColor xcolor;
-  gboolean return_val;
-
-  g_return_val_if_fail (spec != NULL, FALSE);
-  g_return_val_if_fail (color != NULL, FALSE);
-
-  xcolormap = DefaultColormap (gdk_display, _gdk_screen);
-
-  if (XParseColor (gdk_display, xcolormap, spec, &xcolor))
-    {
-      return_val = TRUE;
-      color->red = xcolor.red;
-      color->green = xcolor.green;
-      color->blue = xcolor.blue;
-    }
-  else
-    return_val = FALSE;
-
-  return return_val;
-}
-
 /* This is almost identical to gdk_colormap_free_colors.
  * Keep them in sync!
  */
+
+
+/**
+ * gdk_colors_free:
+ * @colormap: a #GdkColormap.
+ * @pixels: the pixel values of the colors to free.
+ * @npixels: the number of values in @pixels.
+ * @planes: the plane masks for all planes to free, OR'd together.
+ * 
+ * Frees colors allocated with gdk_colors_alloc(). This
+ * function is obsolete. See gdk_colormap_free_colors().
+ **/
 void
 gdk_colors_free (GdkColormap *colormap,
-                gulong      *in_pixels,
-                gint         in_npixels,
+                gulong      *pixels,
+                gint         npixels,
                 gulong       planes)
 {
   GdkColormapPrivateX11 *private;
-  gulong *pixels;
-  gint npixels = 0;
+  gulong *pixels_to_free;
+  gint npixels_to_free = 0;
   gint i;
 
   g_return_if_fail (GDK_IS_COLORMAP (colormap));
-  g_return_if_fail (in_pixels != NULL);
+  g_return_if_fail (pixels != NULL);
 
   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
@@ -514,11 +631,11 @@ gdk_colors_free (GdkColormap *colormap,
       (colormap->visual->type != GDK_VISUAL_GRAYSCALE))
     return;
   
-  pixels = g_new (gulong, in_npixels);
+  pixels_to_free = g_new (gulong, npixels);
 
-  for (i=0; i<in_npixels; i++)
+  for (i=0; i<npixels; i++)
     {
-      gulong pixel = in_pixels[i];
+      gulong pixel = pixels[i];
       
       if (private->info[pixel].ref_count)
        {
@@ -526,7 +643,7 @@ gdk_colors_free (GdkColormap *colormap,
 
          if (private->info[pixel].ref_count == 0)
            {
-             pixels[npixels++] = pixel;
+             pixels_to_free[npixels_to_free++] = pixel;
              if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
                g_hash_table_remove (private->hash, &colormap->colors[pixel]);
              private->info[pixel].flags = 0;
@@ -534,15 +651,24 @@ gdk_colors_free (GdkColormap *colormap,
        }
     }
 
-  if (npixels)
-    XFreeColors (private->xdisplay, private->xcolormap,
-                pixels, npixels, planes);
-  g_free (pixels);
+  if (npixels_to_free && !private->screen->closed)
+    XFreeColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,
+                pixels_to_free, npixels_to_free, planes);
+  g_free (pixels_to_free);
 }
 
 /* This is almost identical to gdk_colors_free.
  * Keep them in sync!
  */
+
+/**
+ * gdk_colormap_free_colors:
+ * @colormap: a #GdkColormap.
+ * @colors: the colors to free.
+ * @ncolors: the number of colors in @colors.
+ * 
+ * Frees previously allocated colors.
+ **/
 void
 gdk_colormap_free_colors (GdkColormap *colormap,
                          GdkColor    *colors,
@@ -582,8 +708,8 @@ gdk_colormap_free_colors (GdkColormap *colormap,
        }
     }
 
-  if (npixels)
-    XFreeColors (private->xdisplay, private->xcolormap,
+  if (npixels && !private->screen->closed)
+    XFreeColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,
                 pixels, npixels, 0);
 
   g_free (pixels);
@@ -612,7 +738,7 @@ gdk_colormap_alloc1 (GdkColormap *colormap,
   xcolor.pixel = color->pixel;
   xcolor.flags = DoRed | DoGreen | DoBlue;
 
-  if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
+  if (XAllocColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor))
     {
       ret->pixel = xcolor.pixel;
       ret->red = xcolor.red;
@@ -623,7 +749,7 @@ gdk_colormap_alloc1 (GdkColormap *colormap,
        {
          if (private->info[ret->pixel].ref_count) /* got a duplicate */
            {
-             XFreeColors (private->xdisplay, private->xcolormap,
+             XFreeColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,
                           &xcolor.pixel, 1, 0);
            }
          else
@@ -685,7 +811,7 @@ gdk_colormap_alloc_colors_writeable (GdkColormap *colormap,
       pixels = g_new (gulong, ncolors);
       /* Allocation of a writeable color cells */
       
-      status =  XAllocColorCells (private->xdisplay, private->xcolormap,
+      status =  XAllocColorCells (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,
                                  FALSE, NULL, 0, pixels, ncolors);
       if (status)
        {
@@ -748,7 +874,8 @@ gdk_colormap_alloc_colors_private (GdkColormap *colormap,
        }
     }
   
-  XStoreColors (private->xdisplay, private->xcolormap, store, nstore);
+  XStoreColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,
+               store, nstore);
   g_free (store);
 
   if (nremaining > 0 && best_match)
@@ -815,7 +942,7 @@ gdk_colormap_alloc_colors_shared (GdkColormap *colormap,
       gchar *available = g_new (gchar, colormap->size);
       for (i = 0; i < colormap->size; i++)
        available[i] = ((private->info[i].ref_count == 0) ||
-                       !(private->info[i].flags && GDK_COLOR_WRITEABLE));
+                       !(private->info[i].flags & GDK_COLOR_WRITEABLE));
       gdk_colormap_sync (colormap, FALSE);
       
       while (nremaining > 0)
@@ -921,10 +1048,50 @@ gdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap,
     return 0;
 }
 
+/**
+ * gdk_colormap_alloc_colors:
+ * @colormap: a #GdkColormap.
+ * @colors: The color values to allocate. On return, the pixel
+ *    values for allocated colors will be filled in.
+ * @ncolors: The number of colors in @colors.
+ * @writeable: If %TRUE, the colors are allocated writeable
+ *    (their values can later be changed using gdk_color_change()).
+ *    Writeable colors cannot be shared between applications.
+ * @best_match: If %TRUE, GDK will attempt to do matching against
+ *    existing colors if the colors cannot be allocated as requested.
+ * @success: An array of length @ncolors. On return, this
+ *   indicates whether the corresponding color in @colors was
+ *   sucessfully allocated or not.
+ * 
+ * Allocates colors from a colormap.
+ * 
+ * Return value: The number of colors that were not sucessfully 
+ * allocated.
+ **/
 gint
 gdk_colormap_alloc_colors (GdkColormap *colormap,
                           GdkColor    *colors,
                           gint         ncolors,
+/**
+ * gdk_colormap_alloc_colors:
+ * @colormap: a #GdkColormap.
+ * @colors: The color values to allocate. On return, the pixel
+ *    values for allocated colors will be filled in.
+ * @ncolors: The number of colors in @colors.
+ * @writeable: If %TRUE, the colors are allocated writeable
+ *    (their values can later be changed using gdk_color_change()).
+ *    Writeable colors cannot be shared between applications.
+ * @best_match: If %TRUE, GDK will attempt to do matching against
+ *    existing colors if the colors cannot be allocated as requested.
+ * @success: An array of length @ncolors. On return, this
+ *   indicates whether the corresponding color in @colors was
+ *   sucessfully allocated or not.
+ * 
+ * Allocates colors from a colormap.
+ * 
+ * Return value: The number of colors that were not sucessfully 
+ * allocated.
+ **/
                           gboolean     writeable,
                           gboolean     best_match,
                           gboolean    *success)
@@ -935,11 +1102,14 @@ gdk_colormap_alloc_colors (GdkColormap *colormap,
   gint nremaining = 0;
   XColor xcolor;
 
-  g_return_val_if_fail (GDK_IS_COLORMAP (colormap), FALSE);
-  g_return_val_if_fail (colors != NULL, FALSE);
+  g_return_val_if_fail (GDK_IS_COLORMAP (colormap), ncolors);
+  g_return_val_if_fail (colors != NULL, ncolors);
 
   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
+  if (private->screen->closed)
+    return ncolors;
+
   for (i=0; i<ncolors; i++)
     {
       success[i] = FALSE;
@@ -969,7 +1139,6 @@ gdk_colormap_alloc_colors (GdkColormap *colormap,
          success[i] = TRUE;
        }
       break;
-
     case GDK_VISUAL_STATIC_GRAY:
     case GDK_VISUAL_STATIC_COLOR:
       for (i=0; i<ncolors; i++)
@@ -980,7 +1149,7 @@ gdk_colormap_alloc_colors (GdkColormap *colormap,
          xcolor.pixel = colors[i].pixel;
          xcolor.flags = DoRed | DoGreen | DoBlue;
 
-         if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
+         if (XAllocColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor))
            {
              colors[i].pixel = xcolor.pixel;
              success[i] = TRUE;
@@ -993,6 +1162,24 @@ gdk_colormap_alloc_colors (GdkColormap *colormap,
   return nremaining;
 }
 
+/**
+ * gdk_colormap_query_color:
+ * @colormap: a #GdkColormap
+ * @pixel: pixel value in hardware display format
+ * @result: #GdkColor with red, green, blue fields initialized
+ * 
+ * Locates the RGB color in @colormap corresponding to the given
+ * hardware pixel @pixel. @pixel must be a valid pixel in the
+ * colormap; it's a programmer error to call this function with a
+ * pixel which is not in the colormap. Hardware pixels are normally
+ * obtained from gdk_colormap_alloc_colors(), or from a #GdkImage. (A
+ * #GdkImage contains image data in hardware format, a #GdkPixbuf
+ * contains image data in a canonical 24-bit RGB format.)
+ *
+ * This function is rarely useful; it's used for example to
+ * implement the eyedropper feature in #GtkColorSelection.
+ * 
+ **/
 void
 gdk_colormap_query_color (GdkColormap *colormap,
                          gulong       pixel,
@@ -1021,12 +1208,18 @@ gdk_colormap_query_color (GdkColormap *colormap,
     break;
   case GDK_VISUAL_STATIC_COLOR:
     xcolor.pixel = pixel;
-    XQueryColor (private->xdisplay, private->xcolormap, &xcolor);
-    result->red = xcolor.red;
-    result->green = xcolor.green;
-    result->blue =  xcolor.blue;
+    if (!private->screen->closed)
+      {
+       XQueryColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor);
+       result->red = xcolor.red;
+       result->green = xcolor.green;
+       result->blue =  xcolor.blue;
+      }
+    else
+      result->red = result->green = result->blue = 0;
     break;
   case GDK_VISUAL_PSEUDO_COLOR:
+    g_return_if_fail (pixel < colormap->size);
     result->red = colormap->colors[pixel].red;
     result->green = colormap->colors[pixel].green;
     result->blue = colormap->colors[pixel].blue;
@@ -1037,6 +1230,21 @@ gdk_colormap_query_color (GdkColormap *colormap,
   }
 }
 
+/**
+ * gdk_color_change:
+ * @colormap: a #GdkColormap.
+ * @color: a #GdkColor, with the color to change
+ * in the <structfield>pixel</structfield> field,
+ * and the new value in the remaining fields.
+ * 
+ * Changes the value of a color that has already
+ * been allocated. If @colormap is not a private
+ * colormap, then the color must have been allocated
+ * using gdk_colormap_alloc_colors() with the 
+ * @writeable set to %TRUE.
+ * 
+ * Return value: %TRUE if the color was successfully changed.
+ **/
 gboolean
 gdk_color_change (GdkColormap *colormap,
                  GdkColor    *color)
@@ -1054,68 +1262,118 @@ gdk_color_change (GdkColormap *colormap,
   xcolor.flags = DoRed | DoGreen | DoBlue;
 
   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
-  XStoreColor (private->xdisplay, private->xcolormap, &xcolor);
+  if (!private->screen->closed)
+    XStoreColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor);
 
   return TRUE;
 }
 
-/* XXX: Do not use this function until it is fixed. An X Colormap
- *      is useless unless we also have the visual.
- */
-GdkColormap*
-gdkx_colormap_get (Colormap xcolormap)
+/**
+ * gdk_x11_colormap_foreign_new:
+ * @visual: a #GdkVisual
+ * @xcolormap: The XID of a colormap with visual @visual
+ * 
+ * If xcolormap refers to a colormap previously known to GTK+,
+ * returns a new reference to the existing #GdkColormap object,
+ * otherwise creates a new GdkColormap object and returns that
+ *
+ * Return value: the #GdkColormap object for @xcolormap.
+ *   Free with g_object_unref(). Note that for colormap created
+ *   with gdk_x11_colormap_foreign_new(), unref'ing the last
+ *   reference to the object will only free the #GdkColoramp
+ *   object and not call XFreeColormap()
+ **/
+GdkColormap *
+gdk_x11_colormap_foreign_new (GdkVisual *visual,
+                             Colormap   xcolormap)
 {
   GdkColormap *colormap;
+  GdkScreen *screen;
   GdkColormapPrivateX11 *private;
+  
+  g_return_val_if_fail (GDK_IS_VISUAL (visual), NULL);
+  g_return_val_if_fail (xcolormap != None, NULL);
 
-  colormap = gdk_colormap_lookup (xcolormap);
-  if (colormap)
-    return colormap;
+  screen = gdk_visual_get_screen (visual);
+  
+  if (xcolormap == DefaultColormap (GDK_SCREEN_XDISPLAY (screen),
+                                   GDK_SCREEN_XNUMBER (screen)));
+    return g_object_ref (gdk_screen_get_system_colormap (screen));
 
-  if (xcolormap == DefaultColormap (gdk_display, _gdk_screen))
-    return gdk_colormap_get_system ();
+  colormap = gdk_colormap_lookup (screen, xcolormap);
+  if (colormap)
+    return g_object_ref (colormap);
 
-  colormap = g_object_new (gdk_colormap_get_type (), NULL);
+  colormap = g_object_new (GDK_TYPE_COLORMAP, NULL);
   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
-  private->xdisplay = gdk_display;
-  private->xcolormap = xcolormap;
-  colormap->visual = NULL;
-  private->private_val = TRUE;
-
-  /* To do the following safely, we would have to have some way of finding
-   * out what the size or visual of the given colormap is. It seems
-   * X doesn't allow this
-   */
+  colormap->visual = visual;
 
-#if 0
-  for (i = 0; i < 256; i++)
-    {
-      xpalette[i].pixel = i;
-      xpalette[i].red = 0;
-      xpalette[i].green = 0;
-      xpalette[i].blue = 0;
-    }
+  private->screen = screen;
+  private->xcolormap = xcolormap;
+  private->xdisplay = GDK_SCREEN_XDISPLAY (screen);
+  private->private_val = FALSE;
 
-  XQueryColors (gdk_display, private->xcolormap, xpalette, 256);
+  colormap->size = visual->colormap_size;
 
-  for (i = 0; i < 256; i++)
+  switch (colormap->visual->type)
     {
-      colormap->colors[i].pixel = xpalette[i].pixel;
-      colormap->colors[i].red = xpalette[i].red;
-      colormap->colors[i].green = xpalette[i].green;
-      colormap->colors[i].blue = xpalette[i].blue;
+    case GDK_VISUAL_GRAYSCALE:
+    case GDK_VISUAL_PSEUDO_COLOR:
+      private->info = g_new0 (GdkColorInfo, colormap->size);
+      private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
+                                       (GEqualFunc) gdk_color_equal);
+      /* Fall through */
+    case GDK_VISUAL_STATIC_GRAY:
+    case GDK_VISUAL_STATIC_COLOR:
+    case GDK_VISUAL_DIRECT_COLOR:
+      colormap->colors = g_new (GdkColor, colormap->size);
+      gdk_colormap_sync (colormap, TRUE);
+      
+    case GDK_VISUAL_TRUE_COLOR:
+      break;
     }
-#endif
-
-  colormap->colors = NULL;
-  colormap->size = 0;
 
   gdk_colormap_add (colormap);
 
   return colormap;
+  
 }
 
+/**
+ * gdkx_colormap_get:
+ * @xcolormap: the XID of a colormap for the default screen.
+ * 
+ * Returns a #GdkColormap corresponding to a X colormap;
+ * this function only works if the colormap is already
+ * known to GTK+ (a colormap created by GTK+ or the default
+ * colormap for the screen), since GTK+ 
+ *
+ * Always use gdk_x11_colormap_foreign_new() instead.
+ *
+ * Return value: the existing #GdkColormap object if it was
+ *  already known to GTK+, otherwise warns and return
+ *  %NULL.
+ **/
+GdkColormap*
+gdkx_colormap_get (Colormap xcolormap)
+{
+  GdkScreen *screen = gdk_screen_get_default ();
+  GdkColormap *colormap;
+
+  if (xcolormap == DefaultColormap (GDK_SCREEN_XDISPLAY (screen),
+                                   GDK_SCREEN_XNUMBER (screen)));
+    return g_object_ref (gdk_screen_get_system_colormap (screen));
+
+  colormap = gdk_colormap_lookup (screen, xcolormap);
+  if (colormap)
+    return g_object_ref (colormap);
+
+  g_warning ("Colormap passed to gdkx_colormap_get\n"
+            "does not previously exist");
+
+  return NULL;
+}
 
 static gint
 gdk_colormap_match_color (GdkColormap *cmap,
@@ -1156,50 +1414,51 @@ gdk_colormap_match_color (GdkColormap *cmap,
 }
 
 
-GdkColormap*
-gdk_colormap_lookup (Colormap xcolormap)
+static GdkColormap*
+gdk_colormap_lookup (GdkScreen *screen,
+                    Colormap   xcolormap)
 {
-  GdkColormap *cmap;
+  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
 
-  if (!colormap_hash)
+  if (screen_x11->colormap_hash)
+    return g_hash_table_lookup (screen_x11->colormap_hash, &xcolormap);
+  else
     return NULL;
-
-  cmap = g_hash_table_lookup (colormap_hash, &xcolormap);
-  return cmap;
 }
 
 static void
 gdk_colormap_add (GdkColormap *cmap)
 {
+  GdkScreenX11 *screen_x11;
   GdkColormapPrivateX11 *private;
 
-  if (!colormap_hash)
-    colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
-                                     (GEqualFunc) gdk_colormap_equal);
-
   private = GDK_COLORMAP_PRIVATE_DATA (cmap);
+  screen_x11 = GDK_SCREEN_X11 (private->screen);
+
+  if (!screen_x11->colormap_hash)
+    screen_x11->colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
+                                                 (GEqualFunc) gdk_colormap_equal);
 
-  g_hash_table_insert (colormap_hash, &private->xcolormap, cmap);
+  g_hash_table_insert (screen_x11->colormap_hash, &private->xcolormap, cmap);
 }
 
 static void
 gdk_colormap_remove (GdkColormap *cmap)
 {
+  GdkScreenX11 *screen_x11;
   GdkColormapPrivateX11 *private;
 
-  if (!colormap_hash)
-    colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
-                                     (GEqualFunc) gdk_colormap_equal);
-
   private = GDK_COLORMAP_PRIVATE_DATA (cmap);
+  screen_x11 = GDK_SCREEN_X11 (private->screen);
 
-  g_hash_table_remove (colormap_hash, &private->xcolormap);
+  if (screen_x11->colormap_hash)
+    g_hash_table_remove (screen_x11->colormap_hash, &private->xcolormap);
 }
 
 static guint
-gdk_colormap_hash (Colormap *cmap)
+gdk_colormap_hash (Colormap *colormap)
 {
-  return *cmap;
+  return *colormap;
 }
 
 static gboolean
@@ -1209,6 +1468,14 @@ gdk_colormap_equal (Colormap *a,
   return (*a == *b);
 }
 
+/**
+ * gdk_x11_colormap_get_xdisplay:
+ * @colormap: a #GdkColormap.
+ * 
+ * Returns the display of a #GdkColormap.
+ * 
+ * Return value: an Xlib <type>Display*</type>.
+ **/
 Display *
 gdk_x11_colormap_get_xdisplay (GdkColormap *colormap)
 {
@@ -1221,6 +1488,14 @@ gdk_x11_colormap_get_xdisplay (GdkColormap *colormap)
   return private->xdisplay;
 }
 
+/**
+ * gdk_x11_colormap_get_xcolormap:
+ * @colormap:  a #GdkColormap.
+ * 
+ * Returns the X colormap belonging to a #GdkColormap.
+ * 
+ * Return value: an Xlib <type>Colormap</type>.
+ **/
 Colormap
 gdk_x11_colormap_get_xcolormap (GdkColormap *colormap)
 {
@@ -1230,5 +1505,24 @@ gdk_x11_colormap_get_xcolormap (GdkColormap *colormap)
 
   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
-  return private->xcolormap;
+  if (private->screen->closed)
+    return None;
+  else
+    return private->xcolormap;
+}
+
+/**
+ * gdk_colormap_get_screen:
+ * @cmap: a #GdkColormap
+ * 
+ * Gets the screen for which this colormap was created.
+ * 
+ * Return value: the screen for which this colormap was created.
+ **/
+GdkScreen *
+gdk_colormap_get_screen (GdkColormap *cmap)
+{
+  g_return_val_if_fail (GDK_IS_COLORMAP (cmap), NULL);
+
+  return  GDK_COLORMAP_PRIVATE_DATA (cmap)->screen;
 }