]> 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 d18e8073c92bf4768683756ac7dc12a1779aecc1..3e2d202fe4c63a5dc3018e150d0bfaf2aef63860 100644 (file)
 #include <time.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)
 
@@ -39,6 +55,8 @@ 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);
@@ -83,9 +101,9 @@ gdk_colormap_init (GdkColormap *colormap)
 
   private = g_new (GdkColormapPrivateX11, 1);
 
+  colormap->screen = NULL;
   colormap->windowing_data = private;
   
-  private->xdisplay = gdk_display;
   private->hash = NULL;
   private->last_sync_time = 0;
   private->info = NULL;
@@ -112,7 +130,7 @@ gdk_colormap_finalize (GObject *object)
 
   gdk_colormap_remove (colormap);
 
-  XFreeColormap (private->xdisplay, private->xcolormap);
+  XFreeColormap (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap);
 
   if (private->hash)
     g_hash_table_destroy (private->hash);
@@ -130,6 +148,8 @@ gdk_colormap_new (GdkVisual *visual,
   GdkColormap *colormap;
   GdkColormapPrivateX11 *private;
   Visual *xvisual;
+  Display *xdisplay;
+  Window xrootwin;
   int size;
   int i;
 
@@ -143,8 +163,11 @@ gdk_colormap_new (GdkVisual *visual,
   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
   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;
 
@@ -159,20 +182,20 @@ gdk_colormap_new (GdkVisual *visual,
                                        (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++)
@@ -191,7 +214,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);
 
@@ -213,16 +236,16 @@ 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);
-      gdk_colormap_sync (colormap);
+      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;
     }
@@ -267,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++)
     {
@@ -281,58 +305,86 @@ 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;
+  GdkColormap *colormap = NULL;
   GdkColormapPrivateX11 *private;
+  GdkScreenX11 *screen_x11;
 
-  if (!colormap)
-    {
-      colormap = g_object_new (gdk_colormap_get_type (), NULL);
-      private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+  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);
-      colormap->visual = gdk_visual_get_system ();
-      private->private_val = FALSE;
+  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 = colormap->visual->colormap_size;
+  colormap->screen = screen;
+  colormap->visual = gdk_screen_get_system_visual (screen);
+  
+  private->xcolormap = DefaultColormapOfScreen (screen_x11->xscreen);
+  private->private_val = FALSE;
 
-      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;
-       }
+  private->hash = NULL;
+  private->last_sync_time = 0;
+  private->info = NULL;
 
-      gdk_colormap_add (colormap);
-    }
+  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:
+      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
@@ -342,6 +394,7 @@ gdk_colormap_change (GdkColormap *colormap,
   GdkColormapPrivateX11 *private;
   GdkVisual *visual;
   XColor *palette;
+  Display *xdisplay;
   gint shift;
   int max_colors;
   int size;
@@ -349,6 +402,7 @@ gdk_colormap_change (GdkColormap *colormap,
 
   g_return_if_fail (GDK_IS_COLORMAP (colormap));
 
+  xdisplay = GDK_SCREEN_XDISPLAY (colormap->screen);
   palette = g_new (XColor, ncolors);
 
   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
@@ -365,7 +419,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:
@@ -382,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;
@@ -395,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;
@@ -408,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:
@@ -434,9 +488,9 @@ gdk_colors_alloc (GdkColormap   *colormap,
 
   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
 
-  return_val = XAllocColorCells (private->xdisplay, private->xcolormap,
-                                contiguous, planes, nplanes, pixels, npixels);
-
+  return_val = XAllocColorCells (GDK_SCREEN_XDISPLAY (colormap->screen),
+                                private->xcolormap,contiguous, planes,
+                                nplanes, pixels, npixels);
   if (return_val)
     {
       for (i=0; i<npixels; i++)
@@ -449,32 +503,6 @@ 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!
  */
@@ -519,7 +547,7 @@ gdk_colors_free (GdkColormap *colormap,
     }
 
   if (npixels)
-    XFreeColors (private->xdisplay, private->xcolormap,
+    XFreeColors (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap,
                 pixels, npixels, planes);
   g_free (pixels);
 }
@@ -567,7 +595,7 @@ gdk_colormap_free_colors (GdkColormap *colormap,
     }
 
   if (npixels)
-    XFreeColors (private->xdisplay, private->xcolormap,
+    XFreeColors (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap,
                 pixels, npixels, 0);
 
   g_free (pixels);
@@ -596,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;
@@ -607,8 +635,8 @@ 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
            {
@@ -669,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)
        {
@@ -732,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)
@@ -799,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)
@@ -964,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;
@@ -977,6 +1006,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,
@@ -984,9 +1031,12 @@ gdk_colormap_query_color (GdkColormap *colormap,
 {
   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) {
@@ -1002,12 +1052,13 @@ gdk_colormap_query_color (GdkColormap *colormap,
     break;
   case GDK_VISUAL_STATIC_COLOR:
     xcolor.pixel = pixel;
-    XQueryColor (GDK_DISPLAY (), GDK_COLORMAP_XCOLORMAP (colormap), &xcolor);
+    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;
@@ -1035,7 +1086,7 @@ gdk_color_change (GdkColormap *colormap,
   xcolor.flags = DoRed | DoGreen | DoBlue;
 
   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
-  XStoreColor (private->xdisplay, private->xcolormap, &xcolor);
+  XStoreColor (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap, &xcolor);
 
   return TRUE;
 }
@@ -1047,13 +1098,14 @@ GdkColormap*
 gdkx_colormap_get (Colormap xcolormap)
 {
   GdkColormap *colormap;
+#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 ();
 
   colormap = g_object_new (gdk_colormap_get_type (), NULL);
@@ -1063,6 +1115,7 @@ gdkx_colormap_get (Colormap xcolormap)
   private->xcolormap = xcolormap;
   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
@@ -1189,3 +1242,27 @@ gdk_colormap_equal (Colormap *a,
 {
   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;
+}