]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkcolor-x11.c
Documentation updates.
[~andy/gtk] / gdk / x11 / gdkcolor-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <time.h>
28
29 #include "gdkcolor.h"
30 #include "gdkinternals.h"
31 #include "gdkx.h"
32 #include "gdkprivate-x11.h"
33 #include "gdkscreen-x11.h"
34
35 typedef struct _GdkColormapPrivateX11  GdkColormapPrivateX11;
36
37 struct _GdkColormapPrivateX11
38 {
39   GdkScreen *screen;
40   Colormap xcolormap;
41   Display *xdisplay;
42   gint private_val;
43
44   GHashTable *hash;
45   GdkColorInfo *info;
46   time_t last_sync_time;
47
48   guint foreign : 1;
49 };
50
51 #define GDK_COLORMAP_PRIVATE_DATA(cmap) ((GdkColormapPrivateX11 *) GDK_COLORMAP (cmap)->windowing_data)
52
53 static gint     gdk_colormap_match_color (GdkColormap *cmap,
54                                           GdkColor    *color,
55                                           const gchar *available);
56 static void     gdk_colormap_add         (GdkColormap *cmap);
57 static void     gdk_colormap_remove      (GdkColormap *cmap);
58
59 static GdkColormap *gdk_colormap_lookup   (GdkScreen   *screen,
60                                            Colormap     xcolormap);
61
62 static guint    gdk_colormap_hash        (Colormap    *cmap);
63 static gboolean gdk_colormap_equal       (Colormap    *a,
64                                           Colormap    *b);
65 static void     gdk_colormap_sync        (GdkColormap *colormap,
66                                           gboolean     force);
67
68 static void gdk_colormap_init       (GdkColormap      *colormap);
69 static void gdk_colormap_class_init (GdkColormapClass *klass);
70 static void gdk_colormap_finalize   (GObject              *object);
71
72 static gpointer parent_class = NULL;
73
74 GType
75 gdk_colormap_get_type (void)
76 {
77   static GType object_type = 0;
78
79   if (!object_type)
80     {
81       static const GTypeInfo object_info =
82       {
83         sizeof (GdkColormapClass),
84         (GBaseInitFunc) NULL,
85         (GBaseFinalizeFunc) NULL,
86         (GClassInitFunc) gdk_colormap_class_init,
87         NULL,           /* class_finalize */
88         NULL,           /* class_data */
89         sizeof (GdkColormap),
90         0,              /* n_preallocs */
91         (GInstanceInitFunc) gdk_colormap_init,
92       };
93       
94       object_type = g_type_register_static (G_TYPE_OBJECT,
95                                             "GdkColormap",
96                                             &object_info, 0);
97     }
98   
99   return object_type;
100 }
101
102 static void
103 gdk_colormap_init (GdkColormap *colormap)
104 {
105   GdkColormapPrivateX11 *private;
106
107   private = g_new (GdkColormapPrivateX11, 1);
108
109   colormap->windowing_data = private;
110   
111   private->screen = NULL;
112   private->hash = NULL;
113   private->last_sync_time = 0;
114   private->info = NULL;
115
116   colormap->size = 0;
117   colormap->colors = NULL;
118 }
119
120 static void
121 gdk_colormap_class_init (GdkColormapClass *klass)
122 {
123   GObjectClass *object_class = G_OBJECT_CLASS (klass);
124
125   parent_class = g_type_class_peek_parent (klass);
126
127   object_class->finalize = gdk_colormap_finalize;
128 }
129
130 static void
131 gdk_colormap_finalize (GObject *object)
132 {
133   GdkColormap *colormap = GDK_COLORMAP (object);
134   GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
135
136   gdk_colormap_remove (colormap);
137
138   if (!private->screen->closed)
139     XFreeColormap (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap);
140
141   if (private->hash)
142     g_hash_table_destroy (private->hash);
143   
144   g_free (private->info);
145   g_free (colormap->colors);
146   
147   G_OBJECT_CLASS (parent_class)->finalize (object);
148 }
149
150 /**
151  * gdk_colormap_new:
152  * @visual: a #GdkVisual.
153  * @allocate: if %TRUE, the newly created colormap will be
154  * a private colormap, and all colors in it will be
155  * allocated for the applications use.
156  * 
157  * Creates a new colormap for the given visual.
158  * 
159  * Return value: the new #GdkColormap.
160  **/
161 GdkColormap*
162 gdk_colormap_new (GdkVisual *visual,
163                   gboolean   allocate)
164 {
165   GdkColormap *colormap;
166   GdkColormapPrivateX11 *private;
167   Visual *xvisual;
168   Display *xdisplay;
169   Window xrootwin;
170   int size;
171   int i;
172
173   /* FIXME when object properties settle down, there needs to be some
174    * kind of default construction (and construct-only arguments)
175    */
176   
177   g_return_val_if_fail (visual != NULL, NULL);
178
179   colormap = g_object_new (GDK_TYPE_COLORMAP, NULL);
180   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
181
182   colormap->visual = visual;
183   private->screen = gdk_visual_get_screen (visual);
184   
185   xvisual = ((GdkVisualPrivate*) visual)->xvisual;
186   xdisplay = GDK_SCREEN_XDISPLAY (private->screen);
187   xrootwin = GDK_SCREEN_XROOTWIN (private->screen);
188
189   colormap->size = visual->colormap_size;
190
191   switch (visual->type)
192     {
193     case GDK_VISUAL_GRAYSCALE:
194     case GDK_VISUAL_PSEUDO_COLOR:
195       private->info = g_new0 (GdkColorInfo, colormap->size);
196       colormap->colors = g_new (GdkColor, colormap->size);
197       
198       private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
199                                         (GEqualFunc) gdk_color_equal);
200       
201       private->private_val = allocate;
202       private->xcolormap = XCreateColormap (xdisplay, xrootwin,
203                                             xvisual, (allocate) ? (AllocAll) : (AllocNone));
204
205       if (allocate)
206         {
207           XColor *default_colors;
208
209           default_colors = g_new (XColor, colormap->size);
210
211           for (i = 0; i < colormap->size; i++)
212             default_colors[i].pixel = i;
213           
214           XQueryColors (xdisplay,
215                         DefaultColormapOfScreen (GDK_SCREEN_X11 (private->screen)->xscreen),
216                         default_colors, colormap->size);
217
218           for (i = 0; i < colormap->size; i++)
219             {
220               colormap->colors[i].pixel = default_colors[i].pixel;
221               colormap->colors[i].red = default_colors[i].red;
222               colormap->colors[i].green = default_colors[i].green;
223               colormap->colors[i].blue = default_colors[i].blue;
224             }
225
226           gdk_colormap_change (colormap, colormap->size);
227           
228           g_free (default_colors);
229         }
230       break;
231
232     case GDK_VISUAL_DIRECT_COLOR:
233       private->private_val = TRUE;
234       private->xcolormap = XCreateColormap (xdisplay, xrootwin,
235                                             xvisual, AllocAll);
236       colormap->colors = g_new (GdkColor, colormap->size);
237
238       size = 1 << visual->red_prec;
239       for (i = 0; i < size; i++)
240         colormap->colors[i].red = i * 65535 / (size - 1);
241
242       size = 1 << visual->green_prec;
243       for (i = 0; i < size; i++)
244         colormap->colors[i].green = i * 65535 / (size - 1);
245
246       size = 1 << visual->blue_prec;
247       for (i = 0; i < size; i++)
248         colormap->colors[i].blue = i * 65535 / (size - 1);
249
250       gdk_colormap_change (colormap, colormap->size);
251       break;
252
253     case GDK_VISUAL_STATIC_GRAY:
254     case GDK_VISUAL_STATIC_COLOR:
255       private->private_val = FALSE;
256       private->xcolormap = XCreateColormap (xdisplay, xrootwin,
257                                             xvisual, AllocNone);
258       
259       colormap->colors = g_new (GdkColor, colormap->size);
260       gdk_colormap_sync (colormap, TRUE);
261       break;
262       
263     case GDK_VISUAL_TRUE_COLOR:
264       private->private_val = FALSE;
265       private->xcolormap = XCreateColormap (xdisplay, xrootwin,
266                                             xvisual, AllocNone);
267       break;
268     }
269
270   gdk_colormap_add (colormap);
271
272   return colormap;
273 }
274
275 static void
276 gdk_colormap_sync_palette (GdkColormap *colormap)
277 {
278   GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
279   XColor *xpalette;
280   gint nlookup;
281   gint i;
282
283   nlookup = 0;
284   xpalette = g_new (XColor, colormap->size);
285   
286   for (i = 0; i < colormap->size; i++)
287     {
288       if (!private->info || private->info[i].ref_count == 0)
289         {
290           xpalette[nlookup].pixel = i;
291           xpalette[nlookup].red = 0;
292           xpalette[nlookup].green = 0;
293           xpalette[nlookup].blue = 0;
294           nlookup++;
295         }
296     }
297
298   XQueryColors (GDK_SCREEN_XDISPLAY (private->screen),
299                 private->xcolormap, xpalette, nlookup);
300   
301   for (i = 0; i < nlookup; i++)
302     {
303       gulong pixel = xpalette[i].pixel;
304       colormap->colors[pixel].pixel = pixel;
305       colormap->colors[pixel].red = xpalette[i].red;
306       colormap->colors[pixel].green = xpalette[i].green;
307       colormap->colors[pixel].blue = xpalette[i].blue;
308     }
309   
310   g_free (xpalette);
311 }
312
313 static void
314 gdk_colormap_sync_direct_color (GdkColormap *colormap)
315 {
316   GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
317   GdkVisual *visual = colormap->visual;
318   XColor *xpalette;
319   gint i;
320
321   xpalette = g_new (XColor, colormap->size);
322   
323   for (i = 0; i < colormap->size; i++)
324     {
325       xpalette[i].pixel =
326         (((i << visual->red_shift)   & visual->red_mask)   |
327          ((i << visual->green_shift) & visual->green_mask) |
328          ((i << visual->blue_shift)  & visual->blue_mask));
329     }
330
331   XQueryColors (GDK_SCREEN_XDISPLAY (private->screen),
332                 private->xcolormap, xpalette, colormap->size);
333   
334   for (i = 0; i < colormap->size; i++)
335     {
336       colormap->colors[i].pixel = xpalette[i].pixel;
337       colormap->colors[i].red = xpalette[i].red;
338       colormap->colors[i].green = xpalette[i].green;
339       colormap->colors[i].blue = xpalette[i].blue;
340     }
341   
342   g_free (xpalette);
343 }
344
345 #define MIN_SYNC_TIME 2
346
347 static void
348 gdk_colormap_sync (GdkColormap *colormap,
349                    gboolean     force)
350 {
351   time_t current_time;
352   GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
353
354   g_return_if_fail (GDK_IS_COLORMAP (colormap));
355
356   if (private->screen->closed)
357     return;
358
359   current_time = time (NULL);
360   if (!force && ((current_time - private->last_sync_time) < MIN_SYNC_TIME))
361     return;
362
363   private->last_sync_time = current_time;
364
365   if (colormap->visual->type == GDK_VISUAL_DIRECT_COLOR)
366     gdk_colormap_sync_direct_color (colormap);
367   else
368     gdk_colormap_sync_palette (colormap);
369 }
370                    
371 /**
372  * gdk_screen_get_system_colormap:
373  * @screen: a #GdkScreen
374  *
375  * Gets the system's default colormap for @screen
376  *
377  * Returns: the default colormap for @screen.
378  */
379 GdkColormap *
380 gdk_screen_get_system_colormap (GdkScreen *screen)
381 {
382   GdkColormap *colormap = NULL;
383   GdkColormapPrivateX11 *private;
384   GdkScreenX11 *screen_x11;
385
386   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
387   screen_x11 = GDK_SCREEN_X11 (screen);
388
389   if (screen_x11->system_colormap)
390     return screen_x11->system_colormap;
391
392   colormap = g_object_new (GDK_TYPE_COLORMAP, NULL);
393   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
394
395   private->screen = screen;
396   colormap->visual = gdk_screen_get_system_visual (screen);
397   
398   private->xcolormap = DefaultColormapOfScreen (screen_x11->xscreen);
399   private->private_val = FALSE;
400
401   private->hash = NULL;
402   private->last_sync_time = 0;
403   private->info = NULL;
404
405   colormap->colors = NULL;
406   colormap->size = colormap->visual->colormap_size;
407
408   switch (colormap->visual->type)
409     {
410     case GDK_VISUAL_GRAYSCALE:
411     case GDK_VISUAL_PSEUDO_COLOR:
412       private->info = g_new0 (GdkColorInfo, colormap->size);
413       private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
414                                         (GEqualFunc) gdk_color_equal);
415       /* Fall through */
416     case GDK_VISUAL_STATIC_GRAY:
417     case GDK_VISUAL_STATIC_COLOR:
418     case GDK_VISUAL_DIRECT_COLOR:
419       colormap->colors = g_new (GdkColor, colormap->size);
420       gdk_colormap_sync (colormap, TRUE);
421       
422     case GDK_VISUAL_TRUE_COLOR:
423       break;
424     }
425   
426   gdk_colormap_add (colormap);
427   screen_x11->system_colormap = colormap;
428   
429   return colormap;
430 }
431
432 /**
433  * gdk_colormap_get_system_size:
434  * 
435  * Returns the size of the system's default colormap.
436  * (See the description of struct #GdkColormap for an
437  * explanation of the size of a colormap.)
438  * 
439  * Return value: the size of the system's default colormap.
440  **/
441 gint
442 gdk_colormap_get_system_size (void)
443 {
444   return DisplayCells (GDK_SCREEN_XDISPLAY (gdk_screen_get_default()),
445                        GDK_SCREEN_X11 (gdk_screen_get_default())->screen_num);
446 }
447
448 /**
449  * gdk_colormap_change:
450  * @colormap: a #GdkColormap.
451  * @ncolors: the number of colors to change.
452  * 
453  * Changes the value of the first @ncolors in a private colormap
454  * to match the values in the <structfield>colors</structfield>
455  * array in the colormap. This function is obsolete and
456  * should not be used. See gdk_color_change().
457  **/
458 void
459 gdk_colormap_change (GdkColormap *colormap,
460                      gint         ncolors)
461 {
462   GdkColormapPrivateX11 *private;
463   GdkVisual *visual;
464   XColor *palette;
465   Display *xdisplay;
466   gint shift;
467   int max_colors;
468   int size;
469   int i;
470
471   g_return_if_fail (GDK_IS_COLORMAP (colormap));
472
473   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
474
475   if (private->screen->closed)
476     return;
477
478   xdisplay = GDK_SCREEN_XDISPLAY (private->screen);
479   palette = g_new (XColor, ncolors);
480
481   switch (colormap->visual->type)
482     {
483     case GDK_VISUAL_GRAYSCALE:
484     case GDK_VISUAL_PSEUDO_COLOR:
485       for (i = 0; i < ncolors; i++)
486         {
487           palette[i].pixel = colormap->colors[i].pixel;
488           palette[i].red = colormap->colors[i].red;
489           palette[i].green = colormap->colors[i].green;
490           palette[i].blue = colormap->colors[i].blue;
491           palette[i].flags = DoRed | DoGreen | DoBlue;
492         }
493
494       XStoreColors (xdisplay, private->xcolormap, palette, ncolors);
495       break;
496
497     case GDK_VISUAL_DIRECT_COLOR:
498       visual = colormap->visual;
499
500       shift = visual->red_shift;
501       max_colors = 1 << visual->red_prec;
502       size = (ncolors < max_colors) ? (ncolors) : (max_colors);
503
504       for (i = 0; i < size; i++)
505         {
506           palette[i].pixel = i << shift;
507           palette[i].red = colormap->colors[i].red;
508           palette[i].flags = DoRed;
509         }
510
511       XStoreColors (xdisplay, private->xcolormap, palette, size);
512
513       shift = visual->green_shift;
514       max_colors = 1 << visual->green_prec;
515       size = (ncolors < max_colors) ? (ncolors) : (max_colors);
516
517       for (i = 0; i < size; i++)
518         {
519           palette[i].pixel = i << shift;
520           palette[i].green = colormap->colors[i].green;
521           palette[i].flags = DoGreen;
522         }
523
524       XStoreColors (xdisplay, private->xcolormap, palette, size);
525
526       shift = visual->blue_shift;
527       max_colors = 1 << visual->blue_prec;
528       size = (ncolors < max_colors) ? (ncolors) : (max_colors);
529
530       for (i = 0; i < size; i++)
531         {
532           palette[i].pixel = i << shift;
533           palette[i].blue = colormap->colors[i].blue;
534           palette[i].flags = DoBlue;
535         }
536
537       XStoreColors (xdisplay, private->xcolormap, palette, size);
538       break;
539
540     default:
541       break;
542     }
543
544   g_free (palette);
545 }
546
547 /**
548  * gdk_colors_alloc:
549  * @colormap: a #GdkColormap.
550  * @contiguous: if %TRUE, the colors should be allocated
551  *    in contiguous color cells.
552  * @planes: an array in which to store the plane masks.
553  * @nplanes: the number of planes to allocate. (Or zero,
554  *    to indicate that the color allocation should not be planar.)
555  * @pixels: an array into which to store allocated pixel values.
556  * @npixels: the number of pixels in each plane to allocate.
557  * 
558  * Allocates colors from a colormap. This function
559  * is obsolete. See gdk_colormap_alloc_colors().
560  * For full documentation of the fields, see 
561  * the Xlib documentation for <function>XAllocColorCells()</function>.
562  * 
563  * Return value: 
564  **/
565 gboolean
566 gdk_colors_alloc (GdkColormap   *colormap,
567                   gboolean       contiguous,
568                   gulong        *planes,
569                   gint           nplanes,
570                   gulong        *pixels,
571                   gint           npixels)
572 {
573   GdkColormapPrivateX11 *private;
574   gint return_val;
575   gint i;
576
577   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), 0);
578
579   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
580
581   if (private->screen->closed)
582     return FALSE;
583
584   return_val = XAllocColorCells (GDK_SCREEN_XDISPLAY (private->screen),
585                                  private->xcolormap,contiguous, planes,
586                                  nplanes, pixels, npixels);
587   if (return_val)
588     {
589       for (i=0; i<npixels; i++)
590         {
591           private->info[pixels[i]].ref_count++;
592           private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
593         }
594     }
595
596   return return_val != 0;
597 }
598
599 /* This is almost identical to gdk_colormap_free_colors.
600  * Keep them in sync!
601  */
602
603
604 /**
605  * gdk_colors_free:
606  * @colormap: a #GdkColormap.
607  * @in_pixels: the pixel values of the colors to free.
608  * @in_npixels: the number of values in @pixels.
609  * @planes: the plane masks for all planes to free, OR'd together.
610  * 
611  * Frees colors allocated with gdk_colors_alloc(). This
612  * function is obsolete. See gdk_colormap_free_colors().
613  **/
614 void
615 gdk_colors_free (GdkColormap *colormap,
616                  gulong      *in_pixels,
617                  gint         in_npixels,
618                  gulong       planes)
619 {
620   GdkColormapPrivateX11 *private;
621   gulong *pixels;
622   gint npixels = 0;
623   gint i;
624
625   g_return_if_fail (GDK_IS_COLORMAP (colormap));
626   g_return_if_fail (in_pixels != NULL);
627
628   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
629
630   if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
631       (colormap->visual->type != GDK_VISUAL_GRAYSCALE))
632     return;
633   
634   pixels = g_new (gulong, in_npixels);
635
636   for (i=0; i<in_npixels; i++)
637     {
638       gulong pixel = in_pixels[i];
639       
640       if (private->info[pixel].ref_count)
641         {
642           private->info[pixel].ref_count--;
643
644           if (private->info[pixel].ref_count == 0)
645             {
646               pixels[npixels++] = pixel;
647               if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
648                 g_hash_table_remove (private->hash, &colormap->colors[pixel]);
649               private->info[pixel].flags = 0;
650             }
651         }
652     }
653
654   if (npixels && !private->screen->closed)
655     XFreeColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,
656                  pixels, npixels, planes);
657   g_free (pixels);
658 }
659
660 /* This is almost identical to gdk_colors_free.
661  * Keep them in sync!
662  */
663
664 /**
665  * gdk_colormap_free_colors:
666  * @colormap: a #GdkColormap.
667  * @colors: the colors to free.
668  * @ncolors: the number of colors in @colors.
669  * 
670  * Frees previously allocated colors.
671  **/
672 void
673 gdk_colormap_free_colors (GdkColormap *colormap,
674                           GdkColor    *colors,
675                           gint         ncolors)
676 {
677   GdkColormapPrivateX11 *private;
678   gulong *pixels;
679   gint npixels = 0;
680   gint i;
681
682   g_return_if_fail (GDK_IS_COLORMAP (colormap));
683   g_return_if_fail (colors != NULL);
684
685   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
686
687   if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
688       (colormap->visual->type != GDK_VISUAL_GRAYSCALE))
689     return;
690
691   pixels = g_new (gulong, ncolors);
692
693   for (i=0; i<ncolors; i++)
694     {
695       gulong pixel = colors[i].pixel;
696       
697       if (private->info[pixel].ref_count)
698         {
699           private->info[pixel].ref_count--;
700
701           if (private->info[pixel].ref_count == 0)
702             {
703               pixels[npixels++] = pixel;
704               if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
705                 g_hash_table_remove (private->hash, &colormap->colors[pixel]);
706               private->info[pixel].flags = 0;
707             }
708         }
709     }
710
711   if (npixels && !private->screen->closed)
712     XFreeColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,
713                  pixels, npixels, 0);
714
715   g_free (pixels);
716 }
717
718 /********************
719  * Color allocation *
720  ********************/
721
722 /* Try to allocate a single color using XAllocColor. If it succeeds,
723  * cache the result in our colormap, and store in ret.
724  */
725 static gboolean 
726 gdk_colormap_alloc1 (GdkColormap *colormap,
727                      GdkColor    *color,
728                      GdkColor    *ret)
729 {
730   GdkColormapPrivateX11 *private;
731   XColor xcolor;
732
733   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
734
735   xcolor.red = color->red;
736   xcolor.green = color->green;
737   xcolor.blue = color->blue;
738   xcolor.pixel = color->pixel;
739   xcolor.flags = DoRed | DoGreen | DoBlue;
740
741   if (XAllocColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor))
742     {
743       ret->pixel = xcolor.pixel;
744       ret->red = xcolor.red;
745       ret->green = xcolor.green;
746       ret->blue = xcolor.blue;
747       
748       if (ret->pixel < colormap->size)
749         {
750           if (private->info[ret->pixel].ref_count) /* got a duplicate */
751             {
752               XFreeColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,
753                            &xcolor.pixel, 1, 0);
754             }
755           else
756             {
757               colormap->colors[ret->pixel] = *color;
758               colormap->colors[ret->pixel].pixel = ret->pixel;
759               private->info[ret->pixel].ref_count = 1;
760
761               g_hash_table_insert (private->hash,
762                                    &colormap->colors[ret->pixel],
763                                    &colormap->colors[ret->pixel]);
764             }
765         }
766       return TRUE;
767     }
768   else
769     {
770       return FALSE;
771     }
772 }
773
774 static gint
775 gdk_colormap_alloc_colors_writeable (GdkColormap *colormap,
776                                      GdkColor    *colors,
777                                      gint         ncolors,
778                                      gboolean     writeable,
779                                      gboolean     best_match,
780                                      gboolean    *success)
781 {
782   GdkColormapPrivateX11 *private;
783   gulong *pixels;
784   Status status;
785   gint i, index;
786
787   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
788
789   if (private->private_val)
790     {
791       index = 0;
792       for (i=0; i<ncolors; i++)
793         {
794           while ((index < colormap->size) && (private->info[index].ref_count != 0))
795             index++;
796           
797           if (index < colormap->size)
798             {
799               colors[i].pixel = index;
800               success[i] = TRUE;
801               private->info[index].ref_count++;
802               private->info[i].flags |= GDK_COLOR_WRITEABLE;
803             }
804           else
805             break;
806         }
807       return i;
808     }
809   else
810     {
811       pixels = g_new (gulong, ncolors);
812       /* Allocation of a writeable color cells */
813       
814       status =  XAllocColorCells (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,
815                                   FALSE, NULL, 0, pixels, ncolors);
816       if (status)
817         {
818           for (i=0; i<ncolors; i++)
819             {
820               colors[i].pixel = pixels[i];
821               private->info[pixels[i]].ref_count++;
822               private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
823             }
824         }
825       
826       g_free (pixels);
827
828       return status ? ncolors : 0; 
829     }
830 }
831
832 static gint
833 gdk_colormap_alloc_colors_private (GdkColormap *colormap,
834                                    GdkColor    *colors,
835                                    gint         ncolors,
836                                    gboolean     writeable,
837                                    gboolean     best_match,
838                                    gboolean    *success)
839 {
840   GdkColormapPrivateX11 *private;
841   gint i, index;
842   XColor *store = g_new (XColor, ncolors);
843   gint nstore = 0;
844   gint nremaining = 0;
845   
846   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
847   index = -1;
848
849   /* First, store the colors we have room for */
850
851   index = 0;
852   for (i=0; i<ncolors; i++)
853     {
854       if (!success[i])
855         {
856           while ((index < colormap->size) && (private->info[index].ref_count != 0))
857             index++;
858
859           if (index < colormap->size)
860             {
861               store[nstore].red = colors[i].red;
862               store[nstore].blue = colors[i].blue;
863               store[nstore].green = colors[i].green;
864               store[nstore].pixel = index;
865               nstore++;
866
867               success[i] = TRUE;
868
869               colors[i].pixel = index;
870               private->info[index].ref_count++;
871             }
872           else
873             nremaining++;
874         }
875     }
876   
877   XStoreColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,
878                 store, nstore);
879   g_free (store);
880
881   if (nremaining > 0 && best_match)
882     {
883       /* Get best matches for remaining colors */
884
885       gchar *available = g_new (gchar, colormap->size);
886       for (i = 0; i < colormap->size; i++)
887         available[i] = TRUE;
888
889       for (i=0; i<ncolors; i++)
890         {
891           if (!success[i])
892             {
893               index = gdk_colormap_match_color (colormap, 
894                                                 &colors[i], 
895                                                 available);
896               if (index != -1)
897                 {
898                   colors[i] = colormap->colors[index];
899                   private->info[index].ref_count++;
900
901                   success[i] = TRUE;
902                   nremaining--;
903                 }
904             }
905         }
906       g_free (available);
907     }
908
909   return (ncolors - nremaining);
910 }
911
912 static gint
913 gdk_colormap_alloc_colors_shared (GdkColormap *colormap,
914                                   GdkColor    *colors,
915                                   gint         ncolors,
916                                   gboolean     writeable,
917                                   gboolean     best_match,
918                                   gboolean    *success)
919 {
920   GdkColormapPrivateX11 *private;
921   gint i, index;
922   gint nremaining = 0;
923   gint nfailed = 0;
924
925   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
926   index = -1;
927
928   for (i=0; i<ncolors; i++)
929     {
930       if (!success[i])
931         {
932           if (gdk_colormap_alloc1 (colormap, &colors[i], &colors[i]))
933             success[i] = TRUE;
934           else
935             nremaining++;
936         }
937     }
938
939
940   if (nremaining > 0 && best_match)
941     {
942       gchar *available = g_new (gchar, colormap->size);
943       for (i = 0; i < colormap->size; i++)
944         available[i] = ((private->info[i].ref_count == 0) ||
945                         !(private->info[i].flags & GDK_COLOR_WRITEABLE));
946       gdk_colormap_sync (colormap, FALSE);
947       
948       while (nremaining > 0)
949         {
950           for (i=0; i<ncolors; i++)
951             {
952               if (!success[i])
953                 {
954                   index = gdk_colormap_match_color (colormap, &colors[i], available);
955                   if (index != -1)
956                     {
957                       if (private->info[index].ref_count)
958                         {
959                           private->info[index].ref_count++;
960                           colors[i] = colormap->colors[index];
961                           success[i] = TRUE;
962                           nremaining--;
963                         }
964                       else
965                         {
966                           if (gdk_colormap_alloc1 (colormap, 
967                                                    &colormap->colors[index],
968                                                    &colors[i]))
969                             {
970                               success[i] = TRUE;
971                               nremaining--;
972                               break;
973                             }
974                           else
975                             {
976                               available[index] = FALSE;
977                             }
978                         }
979                     }
980                   else
981                     {
982                       nfailed++;
983                       nremaining--;
984                       success[i] = 2; /* flag as permanent failure */
985                     }
986                 }
987             }
988         }
989       g_free (available);
990     }
991
992   /* Change back the values we flagged as permanent failures */
993   if (nfailed > 0)
994     {
995       for (i=0; i<ncolors; i++)
996         if (success[i] == 2)
997           success[i] = FALSE;
998       nremaining = nfailed;
999     }
1000   
1001   return (ncolors - nremaining);
1002 }
1003
1004 static gint
1005 gdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap,
1006                                        GdkColor    *colors,
1007                                        gint         ncolors,
1008                                        gboolean     writeable,
1009                                        gboolean     best_match,
1010                                        gboolean    *success)
1011 {
1012   GdkColormapPrivateX11 *private;
1013   GdkColor *lookup_color;
1014   gint i;
1015   gint nremaining = 0;
1016
1017   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1018
1019   /* Check for an exact match among previously allocated colors */
1020
1021   for (i=0; i<ncolors; i++)
1022     {
1023       if (!success[i])
1024         {
1025           lookup_color = g_hash_table_lookup (private->hash, &colors[i]);
1026           if (lookup_color)
1027             {
1028               private->info[lookup_color->pixel].ref_count++;
1029               colors[i].pixel = lookup_color->pixel;
1030               success[i] = TRUE;
1031             }
1032           else
1033             nremaining++;
1034         }
1035     }
1036
1037   /* If that failed, we try to allocate a new color, or approxmiate
1038    * with what we can get if best_match is TRUE.
1039    */
1040   if (nremaining > 0)
1041     {
1042       if (private->private_val)
1043         return gdk_colormap_alloc_colors_private (colormap, colors, ncolors, writeable, best_match, success);
1044       else
1045         return gdk_colormap_alloc_colors_shared (colormap, colors, ncolors, writeable, best_match, success);
1046     }
1047   else
1048     return 0;
1049 }
1050
1051 /**
1052  * gdk_colormap_alloc_colors:
1053  * @colormap: a #GdkColormap.
1054  * @colors: The color values to allocate. On return, the pixel
1055  *    values for allocated colors will be filled in.
1056  * @ncolors: The number of colors in @colors.
1057  * @writeable: If %TRUE, the colors are allocated writeable
1058  *    (their values can later be changed using gdk_color_change()).
1059  *    Writeable colors cannot be shared between applications.
1060  * @best_match: If %TRUE, GDK will attempt to do matching against
1061  *    existing colors if the colors cannot be allocated as requested.
1062  * @success: An array of length @ncolors. On return, this
1063  *   indicates whether the corresponding color in @colors was
1064  *   sucessfully allocated or not.
1065  * 
1066  * Allocates colors from a colormap.
1067  * 
1068  * Return value: The number of colors that were not sucessfully 
1069  * allocated.
1070  **/
1071 gint
1072 gdk_colormap_alloc_colors (GdkColormap *colormap,
1073                            GdkColor    *colors,
1074                            gint         ncolors,
1075 /**
1076  * gdk_colormap_alloc_colors:
1077  * @colormap: a #GdkColormap.
1078  * @colors: The color values to allocate. On return, the pixel
1079  *    values for allocated colors will be filled in.
1080  * @ncolors: The number of colors in @colors.
1081  * @writeable: If %TRUE, the colors are allocated writeable
1082  *    (their values can later be changed using gdk_color_change()).
1083  *    Writeable colors cannot be shared between applications.
1084  * @best_match: If %TRUE, GDK will attempt to do matching against
1085  *    existing colors if the colors cannot be allocated as requested.
1086  * @success: An array of length @ncolors. On return, this
1087  *   indicates whether the corresponding color in @colors was
1088  *   sucessfully allocated or not.
1089  * 
1090  * Allocates colors from a colormap.
1091  * 
1092  * Return value: The number of colors that were not sucessfully 
1093  * allocated.
1094  **/
1095                            gboolean     writeable,
1096                            gboolean     best_match,
1097                            gboolean    *success)
1098 {
1099   GdkColormapPrivateX11 *private;
1100   GdkVisual *visual;
1101   gint i;
1102   gint nremaining = 0;
1103   XColor xcolor;
1104
1105   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), ncolors);
1106   g_return_val_if_fail (colors != NULL, ncolors);
1107
1108   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1109
1110   if (private->screen->closed)
1111     return ncolors;
1112
1113   for (i=0; i<ncolors; i++)
1114     {
1115       success[i] = FALSE;
1116     }
1117
1118   switch (colormap->visual->type)
1119     {
1120     case GDK_VISUAL_PSEUDO_COLOR:
1121     case GDK_VISUAL_GRAYSCALE:
1122       if (writeable)
1123         return gdk_colormap_alloc_colors_writeable (colormap, colors, ncolors,
1124                                                     writeable, best_match, success);
1125       else
1126         return gdk_colormap_alloc_colors_pseudocolor (colormap, colors, ncolors,
1127                                                     writeable, best_match, success);
1128       break;
1129
1130     case GDK_VISUAL_DIRECT_COLOR:
1131     case GDK_VISUAL_TRUE_COLOR:
1132       visual = colormap->visual;
1133
1134       for (i=0; i<ncolors; i++)
1135         {
1136           colors[i].pixel = (((colors[i].red >> (16 - visual->red_prec)) << visual->red_shift) +
1137                              ((colors[i].green >> (16 - visual->green_prec)) << visual->green_shift) +
1138                              ((colors[i].blue >> (16 - visual->blue_prec)) << visual->blue_shift));
1139           success[i] = TRUE;
1140         }
1141       break;
1142     case GDK_VISUAL_STATIC_GRAY:
1143     case GDK_VISUAL_STATIC_COLOR:
1144       for (i=0; i<ncolors; i++)
1145         {
1146           xcolor.red = colors[i].red;
1147           xcolor.green = colors[i].green;
1148           xcolor.blue = colors[i].blue;
1149           xcolor.pixel = colors[i].pixel;
1150           xcolor.flags = DoRed | DoGreen | DoBlue;
1151
1152           if (XAllocColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor))
1153             {
1154               colors[i].pixel = xcolor.pixel;
1155               success[i] = TRUE;
1156             }
1157           else
1158             nremaining++;
1159         }
1160       break;
1161     }
1162   return nremaining;
1163 }
1164
1165 /**
1166  * gdk_colormap_query_color:
1167  * @colormap: a #GdkColormap
1168  * @pixel: pixel value in hardware display format
1169  * @result: #GdkColor with red, green, blue fields initialized
1170  * 
1171  * Locates the RGB color in @colormap corresponding to the given
1172  * hardware pixel @pixel. @pixel must be a valid pixel in the
1173  * colormap; it's a programmer error to call this function with a
1174  * pixel which is not in the colormap. Hardware pixels are normally
1175  * obtained from gdk_colormap_alloc_colors(), or from a #GdkImage. (A
1176  * #GdkImage contains image data in hardware format, a #GdkPixbuf
1177  * contains image data in a canonical 24-bit RGB format.)
1178  *
1179  * This function is rarely useful; it's used for example to
1180  * implement the eyedropper feature in #GtkColorSelection.
1181  * 
1182  **/
1183 void
1184 gdk_colormap_query_color (GdkColormap *colormap,
1185                           gulong       pixel,
1186                           GdkColor    *result)
1187 {
1188   XColor xcolor;
1189   GdkVisual *visual;
1190   GdkColormapPrivateX11 *private;
1191   
1192   g_return_if_fail (GDK_IS_COLORMAP (colormap));
1193   
1194   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1195
1196   visual = gdk_colormap_get_visual (colormap);
1197
1198   switch (visual->type) {
1199   case GDK_VISUAL_DIRECT_COLOR:
1200   case GDK_VISUAL_TRUE_COLOR:
1201     result->red = 65535. * (double)((pixel & visual->red_mask) >> visual->red_shift) / ((1 << visual->red_prec) - 1);
1202     result->green = 65535. * (double)((pixel & visual->green_mask) >> visual->green_shift) / ((1 << visual->green_prec) - 1);
1203     result->blue = 65535. * (double)((pixel & visual->blue_mask) >> visual->blue_shift) / ((1 << visual->blue_prec) - 1);
1204     break;
1205   case GDK_VISUAL_STATIC_GRAY:
1206   case GDK_VISUAL_GRAYSCALE:
1207     result->red = result->green = result->blue = 65535. * (double)pixel/((1<<visual->depth) - 1);
1208     break;
1209   case GDK_VISUAL_STATIC_COLOR:
1210     xcolor.pixel = pixel;
1211     if (!private->screen->closed)
1212       {
1213         XQueryColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor);
1214         result->red = xcolor.red;
1215         result->green = xcolor.green;
1216         result->blue =  xcolor.blue;
1217       }
1218     else
1219       result->red = result->green = result->blue = 0;
1220     break;
1221   case GDK_VISUAL_PSEUDO_COLOR:
1222     g_return_if_fail (pixel < colormap->size);
1223     result->red = colormap->colors[pixel].red;
1224     result->green = colormap->colors[pixel].green;
1225     result->blue = colormap->colors[pixel].blue;
1226     break;
1227   default:
1228     g_assert_not_reached ();
1229     break;
1230   }
1231 }
1232
1233 /**
1234  * gdk_color_change:
1235  * @colormap: a #GdkColormap.
1236  * @color: a #GdkColor, with the color to change
1237  * in the <structfield>pixel</structfield> field,
1238  * and the new value in the remaining fields.
1239  * 
1240  * Changes the value of a color that has already
1241  * been allocated. If @colormap is not a private
1242  * colormap, then the color must have been allocated
1243  * using gdk_colormap_alloc_colors() with the 
1244  * @writeable set to %TRUE.
1245  * 
1246  * Return value: %TRUE if the color was successfully changed.
1247  **/
1248 gboolean
1249 gdk_color_change (GdkColormap *colormap,
1250                   GdkColor    *color)
1251 {
1252   GdkColormapPrivateX11 *private;
1253   XColor xcolor;
1254
1255   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), FALSE);
1256   g_return_val_if_fail (color != NULL, FALSE);
1257
1258   xcolor.pixel = color->pixel;
1259   xcolor.red = color->red;
1260   xcolor.green = color->green;
1261   xcolor.blue = color->blue;
1262   xcolor.flags = DoRed | DoGreen | DoBlue;
1263
1264   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1265   if (!private->screen->closed)
1266     XStoreColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor);
1267
1268   return TRUE;
1269 }
1270
1271 /**
1272  * gdk_x11_colormap_foreign_new:
1273  * @visual: a #GdkVisual
1274  * @xcolormap: The XID of a colormap with visual @visual
1275  * 
1276  * If xcolormap refers to a colormap previously known to GTK+,
1277  * returns a new reference to the existing #GdkColormap object,
1278  * otherwise creates a new GdkColormap object and returns that
1279  *
1280  * Return value: the #GdkColormap object for @xcolormap.
1281  *   Free with g_object_unref(). Note that for colormap created
1282  *   with gdk_x11_colormap_foreign_new(), unref'ing the last
1283  *   reference to the object will only free the #GdkColoramp
1284  *   object and not call XFreeColormap()
1285  **/
1286 GdkColormap *
1287 gdk_x11_colormap_foreign_new (GdkVisual *visual,
1288                               Colormap   xcolormap)
1289 {
1290   GdkColormap *colormap;
1291   GdkScreen *screen;
1292   GdkColormapPrivateX11 *private;
1293   
1294   g_return_val_if_fail (GDK_IS_VISUAL (visual), NULL);
1295   g_return_val_if_fail (xcolormap != None, NULL);
1296
1297   screen = gdk_visual_get_screen (visual);
1298   
1299   if (xcolormap == DefaultColormap (GDK_SCREEN_XDISPLAY (screen),
1300                                     GDK_SCREEN_XNUMBER (screen)));
1301     return g_object_ref (gdk_screen_get_system_colormap (screen));
1302
1303   colormap = gdk_colormap_lookup (screen, xcolormap);
1304   if (colormap)
1305     return g_object_ref (colormap);
1306
1307   colormap = g_object_new (GDK_TYPE_COLORMAP, NULL);
1308   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1309
1310   colormap->visual = visual;
1311
1312   private->screen = screen;
1313   private->xcolormap = xcolormap;
1314   private->xdisplay = GDK_SCREEN_XDISPLAY (screen);
1315   private->private_val = FALSE;
1316
1317   colormap->size = visual->colormap_size;
1318
1319   switch (colormap->visual->type)
1320     {
1321     case GDK_VISUAL_GRAYSCALE:
1322     case GDK_VISUAL_PSEUDO_COLOR:
1323       private->info = g_new0 (GdkColorInfo, colormap->size);
1324       private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
1325                                         (GEqualFunc) gdk_color_equal);
1326       /* Fall through */
1327     case GDK_VISUAL_STATIC_GRAY:
1328     case GDK_VISUAL_STATIC_COLOR:
1329     case GDK_VISUAL_DIRECT_COLOR:
1330       colormap->colors = g_new (GdkColor, colormap->size);
1331       gdk_colormap_sync (colormap, TRUE);
1332       
1333     case GDK_VISUAL_TRUE_COLOR:
1334       break;
1335     }
1336
1337   gdk_colormap_add (colormap);
1338
1339   return colormap;
1340   
1341 }
1342
1343 /**
1344  * gdkx_colormap_get:
1345  * @xcolormap: the XID of a colormap for the default screen.
1346  * 
1347  * Returns a #GdkColormap corresponding to a X colormap;
1348  * this function only works if the colormap is already
1349  * known to GTK+ (a colormap created by GTK+ or the default
1350  * colormap for the screen), since GTK+ 
1351  *
1352  * Always use gdk_x11_colormap_foreign_new() instead.
1353  *
1354  * Return value: the existing #GdkColormap object if it was
1355  *  already known to GTK+, otherwise warns and return
1356  *  %NULL.
1357  **/
1358 GdkColormap*
1359 gdkx_colormap_get (Colormap xcolormap)
1360 {
1361   GdkScreen *screen = gdk_screen_get_default ();
1362   GdkColormap *colormap;
1363
1364   if (xcolormap == DefaultColormap (GDK_SCREEN_XDISPLAY (screen),
1365                                     GDK_SCREEN_XNUMBER (screen)));
1366     return g_object_ref (gdk_screen_get_system_colormap (screen));
1367
1368   colormap = gdk_colormap_lookup (screen, xcolormap);
1369   if (colormap)
1370     return g_object_ref (colormap);
1371
1372   g_warning ("Colormap passed to gdkx_colormap_get\n"
1373              "does not previously exist");
1374
1375   return NULL;
1376 }
1377
1378 static gint
1379 gdk_colormap_match_color (GdkColormap *cmap,
1380                           GdkColor    *color,
1381                           const gchar *available)
1382 {
1383   GdkColor *colors;
1384   guint sum, max;
1385   gint rdiff, gdiff, bdiff;
1386   gint i, index;
1387
1388   g_return_val_if_fail (cmap != NULL, 0);
1389   g_return_val_if_fail (color != NULL, 0);
1390
1391   colors = cmap->colors;
1392   max = 3 * (65536);
1393   index = -1;
1394
1395   for (i = 0; i < cmap->size; i++)
1396     {
1397       if ((!available) || (available && available[i]))
1398         {
1399           rdiff = (color->red - colors[i].red);
1400           gdiff = (color->green - colors[i].green);
1401           bdiff = (color->blue - colors[i].blue);
1402
1403           sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1404
1405           if (sum < max)
1406             {
1407               index = i;
1408               max = sum;
1409             }
1410         }
1411     }
1412
1413   return index;
1414 }
1415
1416
1417 static GdkColormap*
1418 gdk_colormap_lookup (GdkScreen *screen,
1419                      Colormap   xcolormap)
1420 {
1421   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
1422
1423   if (screen_x11->colormap_hash)
1424     return g_hash_table_lookup (screen_x11->colormap_hash, &xcolormap);
1425   else
1426     return NULL;
1427 }
1428
1429 static void
1430 gdk_colormap_add (GdkColormap *cmap)
1431 {
1432   GdkScreenX11 *screen_x11;
1433   GdkColormapPrivateX11 *private;
1434
1435   private = GDK_COLORMAP_PRIVATE_DATA (cmap);
1436   screen_x11 = GDK_SCREEN_X11 (private->screen);
1437
1438   if (!screen_x11->colormap_hash)
1439     screen_x11->colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
1440                                                   (GEqualFunc) gdk_colormap_equal);
1441
1442   g_hash_table_insert (screen_x11->colormap_hash, &private->xcolormap, cmap);
1443 }
1444
1445 static void
1446 gdk_colormap_remove (GdkColormap *cmap)
1447 {
1448   GdkScreenX11 *screen_x11;
1449   GdkColormapPrivateX11 *private;
1450
1451   private = GDK_COLORMAP_PRIVATE_DATA (cmap);
1452   screen_x11 = GDK_SCREEN_X11 (private->screen);
1453
1454   if (screen_x11->colormap_hash)
1455     g_hash_table_remove (screen_x11->colormap_hash, &private->xcolormap);
1456 }
1457
1458 static guint
1459 gdk_colormap_hash (Colormap *colormap)
1460 {
1461   return *colormap;
1462 }
1463
1464 static gboolean
1465 gdk_colormap_equal (Colormap *a,
1466                     Colormap *b)
1467 {
1468   return (*a == *b);
1469 }
1470
1471 /**
1472  * gdk_x11_colormap_get_xdisplay:
1473  * @colormap: a #GdkColormap.
1474  * 
1475  * Returns the display of a #GdkColormap.
1476  * 
1477  * Return value: an Xlib <type>Display*</type>.
1478  **/
1479 Display *
1480 gdk_x11_colormap_get_xdisplay (GdkColormap *colormap)
1481 {
1482   GdkColormapPrivateX11 *private;
1483
1484   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
1485
1486   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1487
1488   return private->xdisplay;
1489 }
1490
1491 /**
1492  * gdk_x11_colormap_get_xcolormap:
1493  * @colormap:  a #GdkColormap.
1494  * 
1495  * Returns the X colormap belonging to a #GdkColormap.
1496  * 
1497  * Return value: an Xlib <type>Colormap</type>.
1498  **/
1499 Colormap
1500 gdk_x11_colormap_get_xcolormap (GdkColormap *colormap)
1501 {
1502   GdkColormapPrivateX11 *private;
1503
1504   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), None);
1505
1506   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1507
1508   if (private->screen->closed)
1509     return None;
1510   else
1511     return private->xcolormap;
1512 }
1513
1514 /**
1515  * gdk_colormap_get_screen:
1516  * @cmap: a #GdkColormap
1517  * 
1518  * Gets the screen for which this colormap was created.
1519  * 
1520  * Return value: the screen for which this colormap was created.
1521  **/
1522 GdkScreen *
1523 gdk_colormap_get_screen (GdkColormap *cmap)
1524 {
1525   g_return_val_if_fail (GDK_IS_COLORMAP (cmap), NULL);
1526
1527   return  GDK_COLORMAP_PRIVATE_DATA (cmap)->screen;
1528 }