]> Pileus Git - ~andy/gtk/blobdiff - gdk/x11/gdkcolor-x11.c
Always select for property notify for maintaining window state.
[~andy/gtk] / gdk / x11 / gdkcolor-x11.c
index f84f570255b4fd409c0d66e8803184ea972e038a..3e2d202fe4c63a5dc3018e150d0bfaf2aef63860 100644 (file)
  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
+
+/*
+ * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
+ */
+
 #include <time.h>
-#include <X11/Xlib.h>
-#include "gdk.h"
-#include "gdkprivate.h"
+
+#include "gdkcolor.h"
+#include "gdkinternals.h"
 #include "gdkx.h"
+#include "gdkprivate-x11.h"
+#include "gdkscreen-x11.h"
+
+typedef struct _GdkColormapPrivateX11  GdkColormapPrivateX11;
+
+struct _GdkColormapPrivateX11
+{
+  Colormap xcolormap;
+  Display *xdisplay;
+  gint private_val;
+
+  GHashTable *hash;
+  GdkColorInfo *info;
+  time_t last_sync_time;
+};
 
+#define GDK_COLORMAP_PRIVATE_DATA(cmap) ((GdkColormapPrivateX11 *) GDK_COLORMAP (cmap)->windowing_data)
 
-static gint  gdk_colormap_match_color (GdkColormap *cmap,
-                                      GdkColor    *color,
-                                      const gchar *available);
-static void  gdk_colormap_add         (GdkColormap *cmap);
-static void  gdk_colormap_remove      (GdkColormap *cmap);
-static guint gdk_colormap_hash        (Colormap    *cmap);
-static gint  gdk_colormap_cmp         (Colormap    *a,
-                                      Colormap    *b);
-static void gdk_colormap_real_destroy (GdkColormap *colormap);
+static gint     gdk_colormap_match_color (GdkColormap *cmap,
+                                         GdkColor    *color,
+                                         const gchar *available);
+static void     gdk_colormap_add         (GdkColormap *cmap);
+static void     gdk_colormap_remove      (GdkColormap *cmap);
+static guint    gdk_colormap_hash        (Colormap    *cmap);
+static gboolean gdk_colormap_equal       (Colormap    *a,
+                                         Colormap    *b);
+static void     gdk_colormap_sync        (GdkColormap *colormap,
+                                          gboolean     force);
+
+static void gdk_colormap_init       (GdkColormap      *colormap);
+static void gdk_colormap_class_init (GdkColormapClass *klass);
+static void gdk_colormap_finalize   (GObject              *object);
+
+static gpointer parent_class = NULL;
 
 static GHashTable *colormap_hash = NULL;
 
+GType
+gdk_colormap_get_type (void)
+{
+  static GType object_type = 0;
+
+  if (!object_type)
+    {
+      static const GTypeInfo object_info =
+      {
+        sizeof (GdkColormapClass),
+        (GBaseInitFunc) NULL,
+        (GBaseFinalizeFunc) NULL,
+        (GClassInitFunc) gdk_colormap_class_init,
+        NULL,           /* class_finalize */
+        NULL,           /* class_data */
+        sizeof (GdkColormap),
+        0,              /* n_preallocs */
+        (GInstanceInitFunc) gdk_colormap_init,
+      };
+      
+      object_type = g_type_register_static (G_TYPE_OBJECT,
+                                            "GdkColormap",
+                                            &object_info, 0);
+    }
+  
+  return object_type;
+}
+
+static void
+gdk_colormap_init (GdkColormap *colormap)
+{
+  GdkColormapPrivateX11 *private;
+
+  private = g_new (GdkColormapPrivateX11, 1);
+
+  colormap->screen = NULL;
+  colormap->windowing_data = private;
+  
+  private->hash = NULL;
+  private->last_sync_time = 0;
+  private->info = NULL;
+
+  colormap->size = 0;
+  colormap->colors = NULL;
+}
+
+static void
+gdk_colormap_class_init (GdkColormapClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  parent_class = g_type_class_peek_parent (klass);
+
+  object_class->finalize = gdk_colormap_finalize;
+}
+
+static void
+gdk_colormap_finalize (GObject *object)
+{
+  GdkColormap *colormap = GDK_COLORMAP (object);
+  GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+  gdk_colormap_remove (colormap);
+
+  XFreeColormap (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap);
+
+  if (private->hash)
+    g_hash_table_destroy (private->hash);
+  
+  g_free (private->info);
+  g_free (colormap->colors);
+  
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
 
 GdkColormap*
 gdk_colormap_new (GdkVisual *visual,
-                 gint       private_cmap)
+                 gboolean   private_cmap)
 {
   GdkColormap *colormap;
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
   Visual *xvisual;
+  Display *xdisplay;
+  Window xrootwin;
   int size;
   int i;
 
+  /* FIXME when object properties settle down, there needs to be some
+   * kind of default construction (and construct-only arguments)
+   */
+  
   g_return_val_if_fail (visual != NULL, NULL);
 
-  private = g_new (GdkColormapPrivate, 1);
-  colormap = (GdkColormap*) private;
-
-  private->xdisplay = gdk_display;
-  private->visual = visual;
-  private->ref_count = 1;
+  colormap = g_object_new (gdk_colormap_get_type (), NULL);
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
-  private->hash = NULL;
-  private->last_sync_time = 0;
-  private->info = NULL;
+  colormap->visual = visual;
+  colormap->screen = visual->screen;
   
   xvisual = ((GdkVisualPrivate*) visual)->xvisual;
+  xdisplay = GDK_SCREEN_XDISPLAY (visual->screen);
+  xrootwin = GDK_SCREEN_XROOTWIN (visual->screen);
 
   colormap->size = visual->colormap_size;
-  colormap->colors = g_new (GdkColor, colormap->size);
 
   switch (visual->type)
     {
@@ -72,23 +179,23 @@ gdk_colormap_new (GdkVisual *visual,
       colormap->colors = g_new (GdkColor, colormap->size);
       
       private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
-                                       (GCompareFunc) gdk_color_equal);
+                                       (GEqualFunc) gdk_color_equal);
       
       private->private_val = private_cmap;
-      private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
+      private->xcolormap = XCreateColormap (xdisplay, xrootwin,
                                            xvisual, (private_cmap) ? (AllocAll) : (AllocNone));
 
       if (private_cmap)
        {
          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 (visual->screen)->xscreen),
                        default_colors, colormap->size);
 
          for (i = 0; i < colormap->size; i++)
@@ -107,8 +214,9 @@ 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);
 
       size = 1 << visual->red_prec;
       for (i = 0; i < size; i++)
@@ -127,9 +235,17 @@ gdk_colormap_new (GdkVisual *visual,
 
     case GDK_VISUAL_STATIC_GRAY:
     case GDK_VISUAL_STATIC_COLOR:
+      private->private_val = FALSE;
+      private->xcolormap = XCreateColormap (xdisplay, xrootwin,
+                                           xvisual, AllocNone);
+      
+      colormap->colors = g_new (GdkColor, colormap->size);
+      gdk_colormap_sync (colormap, TRUE);
+      break;
+      
     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;
     }
@@ -139,71 +255,19 @@ gdk_colormap_new (GdkVisual *visual,
   return colormap;
 }
 
-static void
-gdk_colormap_real_destroy (GdkColormap *colormap)
-{
-  GdkColormapPrivate *private = (GdkColormapPrivate*) colormap;
-
-  g_return_if_fail (colormap != NULL);
-  g_return_if_fail (private->ref_count > 0);
-
-  gdk_colormap_remove (colormap);
-  XFreeColormap (private->xdisplay, private->xcolormap);
-
-  if (private->hash)
-    g_hash_table_destroy (private->hash);
-  
-  g_free (private->info);
-  g_free (colormap->colors);
-  g_free (colormap);
-}
-
-GdkColormap*
-gdk_colormap_ref (GdkColormap *cmap)
-{
-  GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
-  g_return_val_if_fail (cmap != NULL, NULL);
-
-  private->ref_count += 1;
-  return cmap;
-}
-
-void
-gdk_colormap_unref (GdkColormap *cmap)
-{
-  GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
-  g_return_if_fail (cmap != NULL);
-
-  private->ref_count -= 1;
-  if (private->ref_count == 0)
-    gdk_colormap_real_destroy (cmap);
-}
-
-GdkVisual *
-gdk_colormap_get_visual (GdkColormap *colormap)
-{
-  GdkColormapPrivate *private;
-
-  g_return_val_if_fail (colormap != NULL, NULL);
-  
-  private = (GdkColormapPrivate *)colormap;
-
-  return private->visual;
-}
-     
 #define MIN_SYNC_TIME 2
 
-void
+static void
 gdk_colormap_sync (GdkColormap *colormap,
                   gboolean     force)
 {
   time_t current_time;
-  GdkColormapPrivate *private = (GdkColormapPrivate *)colormap;
+  GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
   XColor *xpalette;
   gint nlookup;
   gint i;
   
-  g_return_if_fail (colormap != NULL);
+  g_return_if_fail (GDK_IS_COLORMAP (colormap));
 
   current_time = time (NULL);
   if (!force && ((current_time - private->last_sync_time) < MIN_SYNC_TIME))
@@ -216,7 +280,7 @@ gdk_colormap_sync (GdkColormap *colormap,
   
   for (i = 0; i < colormap->size; i++)
     {
-      if (private->info[i].ref_count == 0)
+      if (!private->info || private->info[i].ref_count == 0)
        {
          xpalette[nlookup].pixel = i;
          xpalette[nlookup].red = 0;
@@ -226,7 +290,8 @@ gdk_colormap_sync (GdkColormap *colormap,
        }
     }
   
-  XQueryColors (gdk_display, private->xcolormap, xpalette, nlookup);
+  XQueryColors (GDK_SCREEN_XDISPLAY (colormap->screen),
+               private->xcolormap, xpalette, nlookup);
   
   for (i = 0; i < nlookup; i++)
     {
@@ -240,73 +305,108 @@ gdk_colormap_sync (GdkColormap *colormap,
   g_free (xpalette);
 }
                   
-
-GdkColormap*
-gdk_colormap_get_system (void)
+/**
+ * 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)
 {
-  static GdkColormap *colormap = NULL;
-  GdkColormapPrivate *private;
+  GdkColormap *colormap = NULL;
+  GdkColormapPrivateX11 *private;
+  GdkScreenX11 *screen_x11;
 
-  if (!colormap)
-    {
-      private = g_new (GdkColormapPrivate, 1);
-      colormap = (GdkColormap*) private;
+  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
+  screen_x11 = GDK_SCREEN_X11 (screen);
 
-      private->xdisplay = gdk_display;
-      private->xcolormap = DefaultColormap (gdk_display, gdk_screen);
-      private->visual = gdk_visual_get_system ();
-      private->private_val = FALSE;
-      private->ref_count = 1;
+  if (screen_x11->system_colormap)
+    return screen_x11->system_colormap;
 
-      private->hash = NULL;
-      private->last_sync_time = 0;
-      private->info = NULL;
+  colormap = g_object_new (gdk_colormap_get_type (), NULL);
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
-      colormap->colors = NULL;
-      colormap->size = private->visual->colormap_size;
+  colormap->screen = screen;
+  colormap->visual = gdk_screen_get_system_visual (screen);
+  
+  private->xcolormap = DefaultColormapOfScreen (screen_x11->xscreen);
+  private->private_val = FALSE;
 
-      if ((private->visual->type == GDK_VISUAL_GRAYSCALE) ||
-         (private->visual->type == GDK_VISUAL_PSEUDO_COLOR))
-       {
-         private->info = g_new0 (GdkColorInfo, colormap->size);
-         colormap->colors = g_new (GdkColor, colormap->size);
-         
-         private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
-                                           (GCompareFunc) gdk_color_equal);
+  private->hash = NULL;
+  private->last_sync_time = 0;
+  private->info = NULL;
 
-         gdk_colormap_sync (colormap, TRUE);
-       }
+  colormap->colors = NULL;
+  colormap->size = colormap->visual->colormap_size;
 
-      gdk_colormap_add (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;
     }
-
+  
+  gdk_colormap_add (colormap);
+  screen_x11->system_colormap = colormap;
+  
   return colormap;
 }
 
+/**
+ * gdk_colormap_get_system:
+ * 
+ * Gets the system's default colormap for the default screen. (See
+ * gdk_colormap_get_system_for_screen ())
+ * 
+ * Return value: the default colormap.
+ **/
+GdkColormap*
+gdk_colormap_get_system (void)
+{
+  return gdk_screen_get_system_colormap (gdk_get_default_screen ());
+}
+
 gint
 gdk_colormap_get_system_size (void)
 {
-  return DisplayCells (gdk_display, gdk_screen);
+  return DisplayCells (GDK_SCREEN_XDISPLAY (gdk_get_default_screen()),
+                      GDK_SCREEN_X11 (gdk_get_default_screen())->screen_num);
 }
 
 void
 gdk_colormap_change (GdkColormap *colormap,
                     gint         ncolors)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
   GdkVisual *visual;
   XColor *palette;
+  Display *xdisplay;
   gint shift;
   int max_colors;
   int size;
   int i;
 
-  g_return_if_fail (colormap != NULL);
+  g_return_if_fail (GDK_IS_COLORMAP (colormap));
 
+  xdisplay = GDK_SCREEN_XDISPLAY (colormap->screen);
   palette = g_new (XColor, ncolors);
 
-  private = (GdkColormapPrivate*) colormap;
-  switch (private->visual->type)
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+  switch (colormap->visual->type)
     {
     case GDK_VISUAL_GRAYSCALE:
     case GDK_VISUAL_PSEUDO_COLOR:
@@ -319,11 +419,11 @@ 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:
-      visual = private->visual;
+      visual = colormap->visual;
 
       shift = visual->red_shift;
       max_colors = 1 << visual->red_prec;
@@ -336,7 +436,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;
@@ -349,7 +449,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;
@@ -362,7 +462,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:
@@ -372,43 +472,25 @@ gdk_colormap_change (GdkColormap *colormap,
   g_free (palette);
 }
 
-void
-gdk_colors_store (GdkColormap   *colormap,
-                 GdkColor      *colors,
-                 gint           ncolors)
-{
-  gint i;
-
-  for (i = 0; i < ncolors; i++)
-    {
-      colormap->colors[i].pixel = colors[i].pixel;
-      colormap->colors[i].red = colors[i].red;
-      colormap->colors[i].green = colors[i].green;
-      colormap->colors[i].blue = colors[i].blue;
-    }
-
-  gdk_colormap_change (colormap, ncolors);
-}
-
 gboolean
 gdk_colors_alloc (GdkColormap   *colormap,
-                 gint           contiguous,
+                 gboolean       contiguous,
                  gulong        *planes,
                  gint           nplanes,
                  gulong        *pixels,
                  gint           npixels)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
   gint return_val;
   gint i;
 
-  g_return_val_if_fail (colormap != NULL, 0);
-
-  private = (GdkColormapPrivate*) colormap;
+  g_return_val_if_fail (GDK_IS_COLORMAP (colormap), 0);
 
-  return_val = XAllocColorCells (private->xdisplay, private->xcolormap,
-                                contiguous, planes, nplanes, pixels, npixels);
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
+  return_val = XAllocColorCells (GDK_SCREEN_XDISPLAY (colormap->screen),
+                                private->xcolormap,contiguous, planes,
+                                nplanes, pixels, npixels);
   if (return_val)
     {
       for (i=0; i<npixels; i++)
@@ -418,7 +500,7 @@ gdk_colors_alloc (GdkColormap   *colormap,
        }
     }
 
-  return return_val;
+  return return_val != 0;
 }
 
 /* This is almost identical to gdk_colormap_free_colors.
@@ -430,18 +512,18 @@ gdk_colors_free (GdkColormap *colormap,
                 gint         in_npixels,
                 gulong       planes)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
   gulong *pixels;
   gint npixels = 0;
   gint i;
 
-  g_return_if_fail (colormap != NULL);
+  g_return_if_fail (GDK_IS_COLORMAP (colormap));
   g_return_if_fail (in_pixels != NULL);
 
-  private = (GdkColormapPrivate*) colormap;
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
-  if ((private->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
-      (private->visual->type != GDK_VISUAL_GRAYSCALE))
+  if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
+      (colormap->visual->type != GDK_VISUAL_GRAYSCALE))
     return;
   
   pixels = g_new (gulong, in_npixels);
@@ -458,144 +540,65 @@ gdk_colors_free (GdkColormap *colormap,
            {
              pixels[npixels++] = pixel;
              if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
-               g_hash_table_remove (private->hash, &colormap->colors[in_pixels[i]]);
+               g_hash_table_remove (private->hash, &colormap->colors[pixel]);
              private->info[pixel].flags = 0;
            }
        }
     }
 
   if (npixels)
-    XFreeColors (private->xdisplay, private->xcolormap,
+    XFreeColors (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap,
                 pixels, npixels, planes);
   g_free (pixels);
 }
 
-/*
- *--------------------------------------------------------------
- * gdk_color_copy
- *
- *   Copy a color structure into new storage.
- *
- * Arguments:
- *   "color" is the color struct to copy.
- *
- * Results:
- *   A new color structure.  Free it with gdk_color_free.
- *
- *--------------------------------------------------------------
+/* This is almost identical to gdk_colors_free.
+ * Keep them in sync!
  */
-
-static GMemChunk *color_chunk;
-
-GdkColor*
-gdk_color_copy (GdkColor *color)
-{
-  GdkColor *new_color;
-  
-  g_return_val_if_fail (color != NULL, NULL);
-
-  if (color_chunk == NULL)
-    color_chunk = g_mem_chunk_new ("colors",
-                                  sizeof (GdkColor),
-                                  4096,
-                                  G_ALLOC_AND_FREE);
-
-  new_color = g_chunk_new (GdkColor, color_chunk);
-  *new_color = *color;
-  return new_color;
-}
-
-/*
- *--------------------------------------------------------------
- * gdk_color_free
- *
- *   Free a color structure obtained from gdk_color_copy.  Do not use
- *   with other color structures.
- *
- * Arguments:
- *   "color" is the color struct to free.
- *
- *-------------------------------------------------------------- */
-
 void
-gdk_color_free (GdkColor *color)
-{
-  g_assert (color_chunk != NULL);
-  g_return_if_fail (color != NULL);
-
-  g_mem_chunk_free (color_chunk, color);
-}
-
-gint
-gdk_color_white (GdkColormap *colormap,
-                GdkColor    *color)
+gdk_colormap_free_colors (GdkColormap *colormap,
+                         GdkColor    *colors,
+                         gint         ncolors)
 {
-  gint return_val;
-
-  g_return_val_if_fail (colormap != NULL, FALSE);
-
-  if (color)
-    {
-      color->pixel = WhitePixel (gdk_display, gdk_screen);
-      color->red = 65535;
-      color->green = 65535;
-      color->blue = 65535;
+  GdkColormapPrivateX11 *private;
+  gulong *pixels;
+  gint npixels = 0;
+  gint i;
 
-      return_val = gdk_color_alloc (colormap, color);
-    }
-  else
-    return_val = FALSE;
+  g_return_if_fail (GDK_IS_COLORMAP (colormap));
+  g_return_if_fail (colors != NULL);
 
-  return return_val;
-}
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
-gint
-gdk_color_black (GdkColormap *colormap,
-                GdkColor    *color)
-{
-  gint return_val;
+  if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
+      (colormap->visual->type != GDK_VISUAL_GRAYSCALE))
+    return;
 
-  g_return_val_if_fail (colormap != NULL, FALSE);
+  pixels = g_new (gulong, ncolors);
 
-  if (color)
+  for (i=0; i<ncolors; i++)
     {
-      color->pixel = BlackPixel (gdk_display, gdk_screen);
-      color->red = 0;
-      color->green = 0;
-      color->blue = 0;
+      gulong pixel = colors[i].pixel;
+      
+      if (private->info[pixel].ref_count)
+       {
+         private->info[pixel].ref_count--;
 
-      return_val = gdk_color_alloc (colormap, color);
+         if (private->info[pixel].ref_count == 0)
+           {
+             pixels[npixels++] = pixel;
+             if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
+               g_hash_table_remove (private->hash, &colormap->colors[pixel]);
+             private->info[pixel].flags = 0;
+           }
+       }
     }
-  else
-    return_val = FALSE;
-
-  return return_val;
-}
 
-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;
+  if (npixels)
+    XFreeColors (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap,
+                pixels, npixels, 0);
 
-  return return_val;
+  g_free (pixels);
 }
 
 /********************
@@ -610,10 +613,10 @@ gdk_colormap_alloc1 (GdkColormap *colormap,
                     GdkColor    *color,
                     GdkColor    *ret)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
   XColor xcolor;
 
-  private = (GdkColormapPrivate*) colormap;
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
   xcolor.red = color->red;
   xcolor.green = color->green;
@@ -621,7 +624,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 (colormap->screen), private->xcolormap, &xcolor))
     {
       ret->pixel = xcolor.pixel;
       ret->red = xcolor.red;
@@ -632,12 +635,13 @@ gdk_colormap_alloc1 (GdkColormap *colormap,
        {
          if (private->info[ret->pixel].ref_count) /* got a duplicate */
            {
-             XFreeColors (private->xdisplay, private->xcolormap,
-                          &ret->pixel, 1, 0);
+             XFreeColors (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap,
+                          &xcolor.pixel, 1, 0);
            }
          else
            {
              colormap->colors[ret->pixel] = *color;
+             colormap->colors[ret->pixel].pixel = ret->pixel;
              private->info[ret->pixel].ref_count = 1;
 
              g_hash_table_insert (private->hash,
@@ -661,12 +665,12 @@ gdk_colormap_alloc_colors_writeable (GdkColormap *colormap,
                                     gboolean     best_match,
                                     gboolean    *success)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
   gulong *pixels;
   Status status;
   gint i, index;
 
-  private = (GdkColormapPrivate*) colormap;
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
   if (private->private_val)
     {
@@ -693,7 +697,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 (colormap->screen), private->xcolormap,
                                  FALSE, NULL, 0, pixels, ncolors);
       if (status)
        {
@@ -719,13 +723,13 @@ gdk_colormap_alloc_colors_private (GdkColormap *colormap,
                                   gboolean     best_match,
                                   gboolean    *success)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
   gint i, index;
   XColor *store = g_new (XColor, ncolors);
   gint nstore = 0;
   gint nremaining = 0;
   
-  private = (GdkColormapPrivate*) colormap;
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
   index = -1;
 
   /* First, store the colors we have room for */
@@ -756,7 +760,8 @@ gdk_colormap_alloc_colors_private (GdkColormap *colormap,
        }
     }
   
-  XStoreColors (private->xdisplay, private->xcolormap, store, nstore);
+  XStoreColors (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap,
+               store, nstore);
   g_free (store);
 
   if (nremaining > 0 && best_match)
@@ -798,12 +803,12 @@ gdk_colormap_alloc_colors_shared (GdkColormap *colormap,
                                  gboolean     best_match,
                                  gboolean    *success)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
   gint i, index;
   gint nremaining = 0;
   gint nfailed = 0;
 
-  private = (GdkColormapPrivate*) colormap;
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
   index = -1;
 
   for (i=0; i<ncolors; i++)
@@ -823,7 +828,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)
@@ -890,12 +895,12 @@ gdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap,
                                       gboolean     best_match,
                                       gboolean    *success)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
   GdkColor *lookup_color;
   gint i;
   gint nremaining = 0;
 
-  private = (GdkColormapPrivate*) colormap;
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
   /* Check for an exact match among previously allocated colors */
 
@@ -937,23 +942,23 @@ gdk_colormap_alloc_colors (GdkColormap *colormap,
                           gboolean     best_match,
                           gboolean    *success)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
   GdkVisual *visual;
   gint i;
   gint nremaining = 0;
   XColor xcolor;
 
-  g_return_val_if_fail (colormap != NULL, FALSE);
+  g_return_val_if_fail (GDK_IS_COLORMAP (colormap), FALSE);
   g_return_val_if_fail (colors != NULL, FALSE);
 
-  private = (GdkColormapPrivate*) colormap;
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
   for (i=0; i<ncolors; i++)
     {
       success[i] = FALSE;
     }
 
-  switch (private->visual->type)
+  switch (colormap->visual->type)
     {
     case GDK_VISUAL_PSEUDO_COLOR:
     case GDK_VISUAL_GRAYSCALE:
@@ -967,7 +972,7 @@ gdk_colormap_alloc_colors (GdkColormap *colormap,
 
     case GDK_VISUAL_DIRECT_COLOR:
     case GDK_VISUAL_TRUE_COLOR:
-      visual = private->visual;
+      visual = colormap->visual;
 
       for (i=0; i<ncolors; i++)
        {
@@ -988,7 +993,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 (colormap->screen), private->xcolormap, &xcolor))
            {
              colors[i].pixel = xcolor.pixel;
              success[i] = TRUE;
@@ -1001,88 +1006,77 @@ gdk_colormap_alloc_colors (GdkColormap *colormap,
   return nremaining;
 }
 
-gboolean
-gdk_colormap_alloc_color (GdkColormap *colormap,
-                         GdkColor    *color,
-                         gboolean     writeable,
-                         gboolean     best_match)
-{
-  gboolean success;
-
-  gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
-                            &success);
-
-  return success;
-}
-
-/* This is almost identical to gdk_colors_free.
- * Keep them in sync!
- */
+/**
+ * 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_free_colors (GdkColormap *colormap,
-                         GdkColor    *colors,
-                         gint         ncolors)
-{
-  GdkColormapPrivate *private;
-  gulong *pixels;
-  gint npixels = 0;
-  gint i;
-
-  g_return_if_fail (colormap != NULL);
-  g_return_if_fail (colors != NULL);
-
-  private = (GdkColormapPrivate*) colormap;
-
-  if ((private->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
-      (private->visual->type != GDK_VISUAL_GRAYSCALE))
-    return;
-
-  pixels = g_new (gulong, ncolors);
-
-  for (i=0; i<ncolors; i++)
-    {
-      gulong pixel = colors[i].pixel;
-      
-      if (private->info[pixel].ref_count)
-       {
-         private->info[pixel].ref_count--;
-
-         if (private->info[pixel].ref_count == 0)
-           {
-             pixels[npixels++] = pixel;
-             if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
-               g_hash_table_remove (private->hash, &colors[i]);
-             private->info[pixel].flags = 0;
-           }
-       }
-    }
-
-  if (npixels)
-    XFreeColors (private->xdisplay, private->xcolormap,
-                pixels, npixels, 0);
-
-  g_free (pixels);
-}
-
-gboolean
-gdk_color_alloc (GdkColormap *colormap,
-                GdkColor    *color)
+gdk_colormap_query_color (GdkColormap *colormap,
+                         gulong       pixel,
+                         GdkColor    *result)
 {
-  gboolean success;
-
-  gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
-
-  return success;
+  XColor xcolor;
+  GdkVisual *visual;
+  GdkColormapPrivateX11 *private;
+  
+  g_return_if_fail (GDK_IS_COLORMAP (colormap));
+  
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+  visual = gdk_colormap_get_visual (colormap);
+
+  switch (visual->type) {
+  case GDK_VISUAL_DIRECT_COLOR:
+  case GDK_VISUAL_TRUE_COLOR:
+    result->red = 65535. * (double)((pixel & visual->red_mask) >> visual->red_shift) / ((1 << visual->red_prec) - 1);
+    result->green = 65535. * (double)((pixel & visual->green_mask) >> visual->green_shift) / ((1 << visual->green_prec) - 1);
+    result->blue = 65535. * (double)((pixel & visual->blue_mask) >> visual->blue_shift) / ((1 << visual->blue_prec) - 1);
+    break;
+  case GDK_VISUAL_STATIC_GRAY:
+  case GDK_VISUAL_GRAYSCALE:
+    result->red = result->green = result->blue = 65535. * (double)pixel/((1<<visual->depth) - 1);
+    break;
+  case GDK_VISUAL_STATIC_COLOR:
+    xcolor.pixel = pixel;
+    XQueryColor (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap, &xcolor);
+    result->red = xcolor.red;
+    result->green = xcolor.green;
+    result->blue =  xcolor.blue;
+    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;
+    break;
+  default:
+    g_assert_not_reached ();
+    break;
+  }
 }
 
 gboolean
 gdk_color_change (GdkColormap *colormap,
                  GdkColor    *color)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
   XColor xcolor;
 
-  g_return_val_if_fail (colormap != NULL, FALSE);
+  g_return_val_if_fail (GDK_IS_COLORMAP (colormap), FALSE);
   g_return_val_if_fail (color != NULL, FALSE);
 
   xcolor.pixel = color->pixel;
@@ -1091,34 +1085,12 @@ gdk_color_change (GdkColormap *colormap,
   xcolor.blue = color->blue;
   xcolor.flags = DoRed | DoGreen | DoBlue;
 
-  private = (GdkColormapPrivate*) colormap;
-  XStoreColor (private->xdisplay, private->xcolormap, &xcolor);
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+  XStoreColor (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap, &xcolor);
 
   return TRUE;
 }
 
-guint
-gdk_color_hash (const GdkColor *colora,
-               const GdkColor *colorb)
-{
-  return ((colora->red) +
-         (colora->green << 11) +
-         (colora->blue << 22) +
-         (colora->blue >> 6));
-}
-
-gint
-gdk_color_equal (const GdkColor *colora,
-                const GdkColor *colorb)
-{
-  g_return_val_if_fail (colora != NULL, FALSE);
-  g_return_val_if_fail (colorb != NULL, FALSE);
-
-  return ((colora->red == colorb->red) &&
-         (colora->green == colorb->green) &&
-         (colora->blue == colorb->blue));
-}
-
 /* XXX: Do not use this function until it is fixed. An X Colormap
  *      is useless unless we also have the visual.
  */
@@ -1126,22 +1098,24 @@ GdkColormap*
 gdkx_colormap_get (Colormap xcolormap)
 {
   GdkColormap *colormap;
-  GdkColormapPrivate *private;
+#if 0
+  GdkColormapPrivateX11 *private;
 
   colormap = gdk_colormap_lookup (xcolormap);
   if (colormap)
     return colormap;
 
-  if (xcolormap == DefaultColormap (gdk_display, gdk_screen))
+  if (xcolormap == DefaultColormap (gdk_display, _gdk_screen))
     return gdk_colormap_get_system ();
 
-  private = g_new (GdkColormapPrivate, 1);
-  colormap = (GdkColormap*) private;
+  colormap = g_object_new (gdk_colormap_get_type (), NULL);
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
   private->xdisplay = gdk_display;
   private->xcolormap = xcolormap;
-  private->visual = NULL;
+  colormap->visual = NULL;
   private->private_val = TRUE;
+#endif
 
   /* 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
@@ -1231,13 +1205,13 @@ gdk_colormap_lookup (Colormap xcolormap)
 static void
 gdk_colormap_add (GdkColormap *cmap)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
 
   if (!colormap_hash)
     colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
-                                     (GCompareFunc) gdk_colormap_cmp);
+                                     (GEqualFunc) gdk_colormap_equal);
 
-  private = (GdkColormapPrivate*) cmap;
+  private = GDK_COLORMAP_PRIVATE_DATA (cmap);
 
   g_hash_table_insert (colormap_hash, &private->xcolormap, cmap);
 }
@@ -1245,13 +1219,13 @@ gdk_colormap_add (GdkColormap *cmap)
 static void
 gdk_colormap_remove (GdkColormap *cmap)
 {
-  GdkColormapPrivate *private;
+  GdkColormapPrivateX11 *private;
 
   if (!colormap_hash)
     colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
-                                     (GCompareFunc) gdk_colormap_cmp);
+                                     (GEqualFunc) gdk_colormap_equal);
 
-  private = (GdkColormapPrivate*) cmap;
+  private = GDK_COLORMAP_PRIVATE_DATA (cmap);
 
   g_hash_table_remove (colormap_hash, &private->xcolormap);
 }
@@ -1262,9 +1236,33 @@ gdk_colormap_hash (Colormap *cmap)
   return *cmap;
 }
 
-static gint
-gdk_colormap_cmp (Colormap *a,
-                 Colormap *b)
+static gboolean
+gdk_colormap_equal (Colormap *a,
+                   Colormap *b)
 {
   return (*a == *b);
 }
+
+Display *
+gdk_x11_colormap_get_xdisplay (GdkColormap *colormap)
+{
+  GdkColormapPrivateX11 *private;
+
+  g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
+
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+  return private->xdisplay;
+}
+
+Colormap
+gdk_x11_colormap_get_xcolormap (GdkColormap *colormap)
+{
+  GdkColormapPrivateX11 *private;
+
+  g_return_val_if_fail (GDK_IS_COLORMAP (colormap), None);
+
+  private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+  return private->xcolormap;
+}