]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkcolor-x11.c
57f78b44c0d84d3d55baf5de59cf314896ad0dd1
[~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  * @pixels: the pixel values of the colors to free.
608  * @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      *pixels,
617                  gint         npixels,
618                  gulong       planes)
619 {
620   GdkColormapPrivateX11 *private;
621   gulong *pixels_to_free;
622   gint npixels_to_free = 0;
623   gint i;
624
625   g_return_if_fail (GDK_IS_COLORMAP (colormap));
626   g_return_if_fail (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_to_free = g_new (gulong, npixels);
635
636   for (i=0; i<npixels; i++)
637     {
638       gulong pixel = 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_to_free[npixels_to_free++] = 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_to_free && !private->screen->closed)
655     XFreeColors (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap,
656                  pixels_to_free, npixels_to_free, planes);
657   g_free (pixels_to_free);
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                            gboolean     writeable,
1076                            gboolean     best_match,
1077                            gboolean    *success)
1078 {
1079   GdkColormapPrivateX11 *private;
1080   GdkVisual *visual;
1081   gint i;
1082   gint nremaining = 0;
1083   XColor xcolor;
1084
1085   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), ncolors);
1086   g_return_val_if_fail (colors != NULL, ncolors);
1087
1088   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1089
1090   if (private->screen->closed)
1091     return ncolors;
1092
1093   for (i=0; i<ncolors; i++)
1094     {
1095       success[i] = FALSE;
1096     }
1097
1098   switch (colormap->visual->type)
1099     {
1100     case GDK_VISUAL_PSEUDO_COLOR:
1101     case GDK_VISUAL_GRAYSCALE:
1102       if (writeable)
1103         return gdk_colormap_alloc_colors_writeable (colormap, colors, ncolors,
1104                                                     writeable, best_match, success);
1105       else
1106         return gdk_colormap_alloc_colors_pseudocolor (colormap, colors, ncolors,
1107                                                     writeable, best_match, success);
1108       break;
1109
1110     case GDK_VISUAL_DIRECT_COLOR:
1111     case GDK_VISUAL_TRUE_COLOR:
1112       visual = colormap->visual;
1113
1114       for (i=0; i<ncolors; i++)
1115         {
1116           colors[i].pixel = (((colors[i].red >> (16 - visual->red_prec)) << visual->red_shift) +
1117                              ((colors[i].green >> (16 - visual->green_prec)) << visual->green_shift) +
1118                              ((colors[i].blue >> (16 - visual->blue_prec)) << visual->blue_shift));
1119           success[i] = TRUE;
1120         }
1121       break;
1122     case GDK_VISUAL_STATIC_GRAY:
1123     case GDK_VISUAL_STATIC_COLOR:
1124       for (i=0; i<ncolors; i++)
1125         {
1126           xcolor.red = colors[i].red;
1127           xcolor.green = colors[i].green;
1128           xcolor.blue = colors[i].blue;
1129           xcolor.pixel = colors[i].pixel;
1130           xcolor.flags = DoRed | DoGreen | DoBlue;
1131
1132           if (XAllocColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor))
1133             {
1134               colors[i].pixel = xcolor.pixel;
1135               success[i] = TRUE;
1136             }
1137           else
1138             nremaining++;
1139         }
1140       break;
1141     }
1142   return nremaining;
1143 }
1144
1145 /**
1146  * gdk_colormap_query_color:
1147  * @colormap: a #GdkColormap
1148  * @pixel: pixel value in hardware display format
1149  * @result: #GdkColor with red, green, blue fields initialized
1150  * 
1151  * Locates the RGB color in @colormap corresponding to the given
1152  * hardware pixel @pixel. @pixel must be a valid pixel in the
1153  * colormap; it's a programmer error to call this function with a
1154  * pixel which is not in the colormap. Hardware pixels are normally
1155  * obtained from gdk_colormap_alloc_colors(), or from a #GdkImage. (A
1156  * #GdkImage contains image data in hardware format, a #GdkPixbuf
1157  * contains image data in a canonical 24-bit RGB format.)
1158  *
1159  * This function is rarely useful; it's used for example to
1160  * implement the eyedropper feature in #GtkColorSelection.
1161  * 
1162  **/
1163 void
1164 gdk_colormap_query_color (GdkColormap *colormap,
1165                           gulong       pixel,
1166                           GdkColor    *result)
1167 {
1168   XColor xcolor;
1169   GdkVisual *visual;
1170   GdkColormapPrivateX11 *private;
1171   
1172   g_return_if_fail (GDK_IS_COLORMAP (colormap));
1173   
1174   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1175
1176   visual = gdk_colormap_get_visual (colormap);
1177
1178   switch (visual->type) {
1179   case GDK_VISUAL_DIRECT_COLOR:
1180   case GDK_VISUAL_TRUE_COLOR:
1181     result->red = 65535. * (double)((pixel & visual->red_mask) >> visual->red_shift) / ((1 << visual->red_prec) - 1);
1182     result->green = 65535. * (double)((pixel & visual->green_mask) >> visual->green_shift) / ((1 << visual->green_prec) - 1);
1183     result->blue = 65535. * (double)((pixel & visual->blue_mask) >> visual->blue_shift) / ((1 << visual->blue_prec) - 1);
1184     break;
1185   case GDK_VISUAL_STATIC_GRAY:
1186   case GDK_VISUAL_GRAYSCALE:
1187     result->red = result->green = result->blue = 65535. * (double)pixel/((1<<visual->depth) - 1);
1188     break;
1189   case GDK_VISUAL_STATIC_COLOR:
1190     xcolor.pixel = pixel;
1191     if (!private->screen->closed)
1192       {
1193         XQueryColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor);
1194         result->red = xcolor.red;
1195         result->green = xcolor.green;
1196         result->blue =  xcolor.blue;
1197       }
1198     else
1199       result->red = result->green = result->blue = 0;
1200     break;
1201   case GDK_VISUAL_PSEUDO_COLOR:
1202     g_return_if_fail (pixel < colormap->size);
1203     result->red = colormap->colors[pixel].red;
1204     result->green = colormap->colors[pixel].green;
1205     result->blue = colormap->colors[pixel].blue;
1206     break;
1207   default:
1208     g_assert_not_reached ();
1209     break;
1210   }
1211 }
1212
1213 /**
1214  * gdk_color_change:
1215  * @colormap: a #GdkColormap.
1216  * @color: a #GdkColor, with the color to change
1217  * in the <structfield>pixel</structfield> field,
1218  * and the new value in the remaining fields.
1219  * 
1220  * Changes the value of a color that has already
1221  * been allocated. If @colormap is not a private
1222  * colormap, then the color must have been allocated
1223  * using gdk_colormap_alloc_colors() with the 
1224  * @writeable set to %TRUE.
1225  * 
1226  * Return value: %TRUE if the color was successfully changed.
1227  **/
1228 gboolean
1229 gdk_color_change (GdkColormap *colormap,
1230                   GdkColor    *color)
1231 {
1232   GdkColormapPrivateX11 *private;
1233   XColor xcolor;
1234
1235   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), FALSE);
1236   g_return_val_if_fail (color != NULL, FALSE);
1237
1238   xcolor.pixel = color->pixel;
1239   xcolor.red = color->red;
1240   xcolor.green = color->green;
1241   xcolor.blue = color->blue;
1242   xcolor.flags = DoRed | DoGreen | DoBlue;
1243
1244   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1245   if (!private->screen->closed)
1246     XStoreColor (GDK_SCREEN_XDISPLAY (private->screen), private->xcolormap, &xcolor);
1247
1248   return TRUE;
1249 }
1250
1251 /**
1252  * gdk_x11_colormap_foreign_new:
1253  * @visual: a #GdkVisual
1254  * @xcolormap: The XID of a colormap with visual @visual
1255  * 
1256  * If xcolormap refers to a colormap previously known to GTK+,
1257  * returns a new reference to the existing #GdkColormap object,
1258  * otherwise creates a new GdkColormap object and returns that
1259  *
1260  * Return value: the #GdkColormap object for @xcolormap.
1261  *   Free with g_object_unref(). Note that for colormap created
1262  *   with gdk_x11_colormap_foreign_new(), unref'ing the last
1263  *   reference to the object will only free the #GdkColoramp
1264  *   object and not call XFreeColormap()
1265  **/
1266 GdkColormap *
1267 gdk_x11_colormap_foreign_new (GdkVisual *visual,
1268                               Colormap   xcolormap)
1269 {
1270   GdkColormap *colormap;
1271   GdkScreen *screen;
1272   GdkColormapPrivateX11 *private;
1273   
1274   g_return_val_if_fail (GDK_IS_VISUAL (visual), NULL);
1275   g_return_val_if_fail (xcolormap != None, NULL);
1276
1277   screen = gdk_visual_get_screen (visual);
1278   
1279   if (xcolormap == DefaultColormap (GDK_SCREEN_XDISPLAY (screen),
1280                                     GDK_SCREEN_XNUMBER (screen)));
1281     return g_object_ref (gdk_screen_get_system_colormap (screen));
1282
1283   colormap = gdk_colormap_lookup (screen, xcolormap);
1284   if (colormap)
1285     return g_object_ref (colormap);
1286
1287   colormap = g_object_new (GDK_TYPE_COLORMAP, NULL);
1288   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1289
1290   colormap->visual = visual;
1291
1292   private->screen = screen;
1293   private->xcolormap = xcolormap;
1294   private->xdisplay = GDK_SCREEN_XDISPLAY (screen);
1295   private->private_val = FALSE;
1296
1297   colormap->size = visual->colormap_size;
1298
1299   switch (colormap->visual->type)
1300     {
1301     case GDK_VISUAL_GRAYSCALE:
1302     case GDK_VISUAL_PSEUDO_COLOR:
1303       private->info = g_new0 (GdkColorInfo, colormap->size);
1304       private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
1305                                         (GEqualFunc) gdk_color_equal);
1306       /* Fall through */
1307     case GDK_VISUAL_STATIC_GRAY:
1308     case GDK_VISUAL_STATIC_COLOR:
1309     case GDK_VISUAL_DIRECT_COLOR:
1310       colormap->colors = g_new (GdkColor, colormap->size);
1311       gdk_colormap_sync (colormap, TRUE);
1312       
1313     case GDK_VISUAL_TRUE_COLOR:
1314       break;
1315     }
1316
1317   gdk_colormap_add (colormap);
1318
1319   return colormap;
1320   
1321 }
1322
1323 /**
1324  * gdkx_colormap_get:
1325  * @xcolormap: the XID of a colormap for the default screen.
1326  * 
1327  * Returns a #GdkColormap corresponding to a X colormap;
1328  * this function only works if the colormap is already
1329  * known to GTK+ (a colormap created by GTK+ or the default
1330  * colormap for the screen), since GTK+ 
1331  *
1332  * Always use gdk_x11_colormap_foreign_new() instead.
1333  *
1334  * Return value: the existing #GdkColormap object if it was
1335  *  already known to GTK+, otherwise warns and return
1336  *  %NULL.
1337  **/
1338 GdkColormap*
1339 gdkx_colormap_get (Colormap xcolormap)
1340 {
1341   GdkScreen *screen = gdk_screen_get_default ();
1342   GdkColormap *colormap;
1343
1344   if (xcolormap == DefaultColormap (GDK_SCREEN_XDISPLAY (screen),
1345                                     GDK_SCREEN_XNUMBER (screen)));
1346     return g_object_ref (gdk_screen_get_system_colormap (screen));
1347
1348   colormap = gdk_colormap_lookup (screen, xcolormap);
1349   if (colormap)
1350     return g_object_ref (colormap);
1351
1352   g_warning ("Colormap passed to gdkx_colormap_get\n"
1353              "does not previously exist");
1354
1355   return NULL;
1356 }
1357
1358 static gint
1359 gdk_colormap_match_color (GdkColormap *cmap,
1360                           GdkColor    *color,
1361                           const gchar *available)
1362 {
1363   GdkColor *colors;
1364   guint sum, max;
1365   gint rdiff, gdiff, bdiff;
1366   gint i, index;
1367
1368   g_return_val_if_fail (cmap != NULL, 0);
1369   g_return_val_if_fail (color != NULL, 0);
1370
1371   colors = cmap->colors;
1372   max = 3 * (65536);
1373   index = -1;
1374
1375   for (i = 0; i < cmap->size; i++)
1376     {
1377       if ((!available) || (available && available[i]))
1378         {
1379           rdiff = (color->red - colors[i].red);
1380           gdiff = (color->green - colors[i].green);
1381           bdiff = (color->blue - colors[i].blue);
1382
1383           sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1384
1385           if (sum < max)
1386             {
1387               index = i;
1388               max = sum;
1389             }
1390         }
1391     }
1392
1393   return index;
1394 }
1395
1396
1397 static GdkColormap*
1398 gdk_colormap_lookup (GdkScreen *screen,
1399                      Colormap   xcolormap)
1400 {
1401   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
1402
1403   if (screen_x11->colormap_hash)
1404     return g_hash_table_lookup (screen_x11->colormap_hash, &xcolormap);
1405   else
1406     return NULL;
1407 }
1408
1409 static void
1410 gdk_colormap_add (GdkColormap *cmap)
1411 {
1412   GdkScreenX11 *screen_x11;
1413   GdkColormapPrivateX11 *private;
1414
1415   private = GDK_COLORMAP_PRIVATE_DATA (cmap);
1416   screen_x11 = GDK_SCREEN_X11 (private->screen);
1417
1418   if (!screen_x11->colormap_hash)
1419     screen_x11->colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
1420                                                   (GEqualFunc) gdk_colormap_equal);
1421
1422   g_hash_table_insert (screen_x11->colormap_hash, &private->xcolormap, cmap);
1423 }
1424
1425 static void
1426 gdk_colormap_remove (GdkColormap *cmap)
1427 {
1428   GdkScreenX11 *screen_x11;
1429   GdkColormapPrivateX11 *private;
1430
1431   private = GDK_COLORMAP_PRIVATE_DATA (cmap);
1432   screen_x11 = GDK_SCREEN_X11 (private->screen);
1433
1434   if (screen_x11->colormap_hash)
1435     g_hash_table_remove (screen_x11->colormap_hash, &private->xcolormap);
1436 }
1437
1438 static guint
1439 gdk_colormap_hash (Colormap *colormap)
1440 {
1441   return *colormap;
1442 }
1443
1444 static gboolean
1445 gdk_colormap_equal (Colormap *a,
1446                     Colormap *b)
1447 {
1448   return (*a == *b);
1449 }
1450
1451 /**
1452  * gdk_x11_colormap_get_xdisplay:
1453  * @colormap: a #GdkColormap.
1454  * 
1455  * Returns the display of a #GdkColormap.
1456  * 
1457  * Return value: an Xlib <type>Display*</type>.
1458  **/
1459 Display *
1460 gdk_x11_colormap_get_xdisplay (GdkColormap *colormap)
1461 {
1462   GdkColormapPrivateX11 *private;
1463
1464   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
1465
1466   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1467
1468   return private->xdisplay;
1469 }
1470
1471 /**
1472  * gdk_x11_colormap_get_xcolormap:
1473  * @colormap:  a #GdkColormap.
1474  * 
1475  * Returns the X colormap belonging to a #GdkColormap.
1476  * 
1477  * Return value: an Xlib <type>Colormap</type>.
1478  **/
1479 Colormap
1480 gdk_x11_colormap_get_xcolormap (GdkColormap *colormap)
1481 {
1482   GdkColormapPrivateX11 *private;
1483
1484   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), None);
1485
1486   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1487
1488   if (private->screen->closed)
1489     return None;
1490   else
1491     return private->xcolormap;
1492 }
1493
1494 /**
1495  * gdk_colormap_get_screen:
1496  * @cmap: a #GdkColormap
1497  * 
1498  * Gets the screen for which this colormap was created.
1499  * 
1500  * Return value: the screen for which this colormap was created.
1501  **/
1502 GdkScreen *
1503 gdk_colormap_get_screen (GdkColormap *cmap)
1504 {
1505   g_return_val_if_fail (GDK_IS_COLORMAP (cmap), NULL);
1506
1507   return  GDK_COLORMAP_PRIVATE_DATA (cmap)->screen;
1508 }