]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkcolor-x11.c
7dcf27ecf99c8998abcc9879471ba817234a2f56
[~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 "gdkprivate-x11.h"
32
33 typedef struct _GdkColormapPrivateX11  GdkColormapPrivateX11;
34
35 struct _GdkColormapPrivateX11
36 {
37   Colormap xcolormap;
38   Display *xdisplay;
39   gint private_val;
40
41   GHashTable *hash;
42   GdkColorInfo *info;
43   time_t last_sync_time;
44 };
45
46 #define GDK_COLORMAP_PRIVATE_DATA(cmap) ((GdkColormapPrivateX11 *) GDK_COLORMAP (cmap)->windowing_data)
47
48 static gint     gdk_colormap_match_color (GdkColormap *cmap,
49                                           GdkColor    *color,
50                                           const gchar *available);
51 static void     gdk_colormap_add         (GdkColormap *cmap);
52 static void     gdk_colormap_remove      (GdkColormap *cmap);
53 static guint    gdk_colormap_hash        (Colormap    *cmap);
54 static gboolean gdk_colormap_equal       (Colormap    *a,
55                                           Colormap    *b);
56 static void     gdk_colormap_sync        (GdkColormap *colormap,
57                                           gboolean     force);
58
59 static void gdk_colormap_init       (GdkColormap      *colormap);
60 static void gdk_colormap_class_init (GdkColormapClass *klass);
61 static void gdk_colormap_finalize   (GObject              *object);
62
63 static gpointer parent_class = NULL;
64
65 static GHashTable *colormap_hash = NULL;
66
67 GType
68 gdk_colormap_get_type (void)
69 {
70   static GType object_type = 0;
71
72   if (!object_type)
73     {
74       static const GTypeInfo object_info =
75       {
76         sizeof (GdkColormapClass),
77         (GBaseInitFunc) NULL,
78         (GBaseFinalizeFunc) NULL,
79         (GClassInitFunc) gdk_colormap_class_init,
80         NULL,           /* class_finalize */
81         NULL,           /* class_data */
82         sizeof (GdkColormap),
83         0,              /* n_preallocs */
84         (GInstanceInitFunc) gdk_colormap_init,
85       };
86       
87       object_type = g_type_register_static (G_TYPE_OBJECT,
88                                             "GdkColormap",
89                                             &object_info, 0);
90     }
91   
92   return object_type;
93 }
94
95 static void
96 gdk_colormap_init (GdkColormap *colormap)
97 {
98   GdkColormapPrivateX11 *private;
99
100   private = g_new (GdkColormapPrivateX11, 1);
101
102   colormap->windowing_data = private;
103   
104   private->xdisplay = gdk_display;
105   private->hash = NULL;
106   private->last_sync_time = 0;
107   private->info = NULL;
108
109   colormap->size = 0;
110   colormap->colors = NULL;
111 }
112
113 static void
114 gdk_colormap_class_init (GdkColormapClass *klass)
115 {
116   GObjectClass *object_class = G_OBJECT_CLASS (klass);
117
118   parent_class = g_type_class_peek_parent (klass);
119
120   object_class->finalize = gdk_colormap_finalize;
121 }
122
123 static void
124 gdk_colormap_finalize (GObject *object)
125 {
126   GdkColormap *colormap = GDK_COLORMAP (object);
127   GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
128
129   gdk_colormap_remove (colormap);
130
131   XFreeColormap (private->xdisplay, private->xcolormap);
132
133   if (private->hash)
134     g_hash_table_destroy (private->hash);
135   
136   g_free (private->info);
137   g_free (colormap->colors);
138   
139   G_OBJECT_CLASS (parent_class)->finalize (object);
140 }
141
142 GdkColormap*
143 gdk_colormap_new (GdkVisual *visual,
144                   gboolean   private_cmap)
145 {
146   GdkColormap *colormap;
147   GdkColormapPrivateX11 *private;
148   Visual *xvisual;
149   int size;
150   int i;
151
152   /* FIXME when object properties settle down, there needs to be some
153    * kind of default construction (and construct-only arguments)
154    */
155   
156   g_return_val_if_fail (visual != NULL, NULL);
157
158   colormap = g_object_new (gdk_colormap_get_type (), NULL);
159   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
160
161   colormap->visual = visual;
162   
163   xvisual = ((GdkVisualPrivate*) visual)->xvisual;
164
165   colormap->size = visual->colormap_size;
166
167   switch (visual->type)
168     {
169     case GDK_VISUAL_GRAYSCALE:
170     case GDK_VISUAL_PSEUDO_COLOR:
171       private->info = g_new0 (GdkColorInfo, colormap->size);
172       colormap->colors = g_new (GdkColor, colormap->size);
173       
174       private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
175                                         (GEqualFunc) gdk_color_equal);
176       
177       private->private_val = private_cmap;
178       private->xcolormap = XCreateColormap (private->xdisplay, _gdk_root_window,
179                                             xvisual, (private_cmap) ? (AllocAll) : (AllocNone));
180
181       if (private_cmap)
182         {
183           XColor *default_colors;
184
185           default_colors = g_new (XColor, colormap->size);
186
187           for (i = 0; i < colormap->size; i++)
188             default_colors[i].pixel = i;
189
190           XQueryColors (private->xdisplay,
191                         DefaultColormap (private->xdisplay, _gdk_screen),
192                         default_colors, colormap->size);
193
194           for (i = 0; i < colormap->size; i++)
195             {
196               colormap->colors[i].pixel = default_colors[i].pixel;
197               colormap->colors[i].red = default_colors[i].red;
198               colormap->colors[i].green = default_colors[i].green;
199               colormap->colors[i].blue = default_colors[i].blue;
200             }
201
202           gdk_colormap_change (colormap, colormap->size);
203           
204           g_free (default_colors);
205         }
206       break;
207
208     case GDK_VISUAL_DIRECT_COLOR:
209       private->private_val = TRUE;
210       private->xcolormap = XCreateColormap (private->xdisplay, _gdk_root_window,
211                                             xvisual, AllocAll);
212       colormap->colors = g_new (GdkColor, colormap->size);
213
214       size = 1 << visual->red_prec;
215       for (i = 0; i < size; i++)
216         colormap->colors[i].red = i * 65535 / (size - 1);
217
218       size = 1 << visual->green_prec;
219       for (i = 0; i < size; i++)
220         colormap->colors[i].green = i * 65535 / (size - 1);
221
222       size = 1 << visual->blue_prec;
223       for (i = 0; i < size; i++)
224         colormap->colors[i].blue = i * 65535 / (size - 1);
225
226       gdk_colormap_change (colormap, colormap->size);
227       break;
228
229     case GDK_VISUAL_STATIC_GRAY:
230     case GDK_VISUAL_STATIC_COLOR:
231       private->private_val = FALSE;
232       private->xcolormap = XCreateColormap (private->xdisplay, _gdk_root_window,
233                                             xvisual, AllocNone);
234       
235       colormap->colors = g_new (GdkColor, colormap->size);
236       gdk_colormap_sync (colormap, TRUE);
237       break;
238       
239     case GDK_VISUAL_TRUE_COLOR:
240       private->private_val = FALSE;
241       private->xcolormap = XCreateColormap (private->xdisplay, _gdk_root_window,
242                                             xvisual, AllocNone);
243       break;
244     }
245
246   gdk_colormap_add (colormap);
247
248   return colormap;
249 }
250
251 #define MIN_SYNC_TIME 2
252
253 static void
254 gdk_colormap_sync (GdkColormap *colormap,
255                    gboolean     force)
256 {
257   time_t current_time;
258   GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
259   XColor *xpalette;
260   gint nlookup;
261   gint i;
262   
263   g_return_if_fail (GDK_IS_COLORMAP (colormap));
264
265   current_time = time (NULL);
266   if (!force && ((current_time - private->last_sync_time) < MIN_SYNC_TIME))
267     return;
268
269   private->last_sync_time = current_time;
270
271   nlookup = 0;
272   xpalette = g_new (XColor, colormap->size);
273   
274   for (i = 0; i < colormap->size; i++)
275     {
276       if (!private->info || private->info[i].ref_count == 0)
277         {
278           xpalette[nlookup].pixel = i;
279           xpalette[nlookup].red = 0;
280           xpalette[nlookup].green = 0;
281           xpalette[nlookup].blue = 0;
282           nlookup++;
283         }
284     }
285   
286   XQueryColors (gdk_display, private->xcolormap, xpalette, nlookup);
287   
288   for (i = 0; i < nlookup; i++)
289     {
290       gulong pixel = xpalette[i].pixel;
291       colormap->colors[pixel].pixel = pixel;
292       colormap->colors[pixel].red = xpalette[i].red;
293       colormap->colors[pixel].green = xpalette[i].green;
294       colormap->colors[pixel].blue = xpalette[i].blue;
295     }
296   
297   g_free (xpalette);
298 }
299                    
300
301 GdkColormap*
302 gdk_colormap_get_system (void)
303 {
304   static GdkColormap *colormap = NULL;
305   GdkColormapPrivateX11 *private;
306
307   if (!colormap)
308     {
309       colormap = g_object_new (gdk_colormap_get_type (), NULL);
310       private = GDK_COLORMAP_PRIVATE_DATA (colormap);
311
312       private->xdisplay = gdk_display;
313       private->xcolormap = DefaultColormap (gdk_display, _gdk_screen);
314       colormap->visual = gdk_visual_get_system ();
315       private->private_val = FALSE;
316
317       private->hash = NULL;
318       private->last_sync_time = 0;
319       private->info = NULL;
320
321       colormap->colors = NULL;
322       colormap->size = colormap->visual->colormap_size;
323
324       switch (colormap->visual->type)
325         {
326         case GDK_VISUAL_GRAYSCALE:
327         case GDK_VISUAL_PSEUDO_COLOR:
328           private->info = g_new0 (GdkColorInfo, colormap->size);
329           private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
330                                             (GEqualFunc) gdk_color_equal);
331           /* Fall through */
332         case GDK_VISUAL_STATIC_GRAY:
333         case GDK_VISUAL_STATIC_COLOR:
334           colormap->colors = g_new (GdkColor, colormap->size);
335           gdk_colormap_sync (colormap, TRUE);
336           
337         case GDK_VISUAL_DIRECT_COLOR:
338         case GDK_VISUAL_TRUE_COLOR:
339           break;
340         }
341
342       gdk_colormap_add (colormap);
343     }
344
345   return colormap;
346 }
347
348 gint
349 gdk_colormap_get_system_size (void)
350 {
351   return DisplayCells (gdk_display, _gdk_screen);
352 }
353
354 void
355 gdk_colormap_change (GdkColormap *colormap,
356                      gint         ncolors)
357 {
358   GdkColormapPrivateX11 *private;
359   GdkVisual *visual;
360   XColor *palette;
361   gint shift;
362   int max_colors;
363   int size;
364   int i;
365
366   g_return_if_fail (GDK_IS_COLORMAP (colormap));
367
368   palette = g_new (XColor, ncolors);
369
370   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
371   switch (colormap->visual->type)
372     {
373     case GDK_VISUAL_GRAYSCALE:
374     case GDK_VISUAL_PSEUDO_COLOR:
375       for (i = 0; i < ncolors; i++)
376         {
377           palette[i].pixel = colormap->colors[i].pixel;
378           palette[i].red = colormap->colors[i].red;
379           palette[i].green = colormap->colors[i].green;
380           palette[i].blue = colormap->colors[i].blue;
381           palette[i].flags = DoRed | DoGreen | DoBlue;
382         }
383
384       XStoreColors (private->xdisplay, private->xcolormap, palette, ncolors);
385       break;
386
387     case GDK_VISUAL_DIRECT_COLOR:
388       visual = colormap->visual;
389
390       shift = visual->red_shift;
391       max_colors = 1 << visual->red_prec;
392       size = (ncolors < max_colors) ? (ncolors) : (max_colors);
393
394       for (i = 0; i < size; i++)
395         {
396           palette[i].pixel = i << shift;
397           palette[i].red = colormap->colors[i].red;
398           palette[i].flags = DoRed;
399         }
400
401       XStoreColors (private->xdisplay, private->xcolormap, palette, size);
402
403       shift = visual->green_shift;
404       max_colors = 1 << visual->green_prec;
405       size = (ncolors < max_colors) ? (ncolors) : (max_colors);
406
407       for (i = 0; i < size; i++)
408         {
409           palette[i].pixel = i << shift;
410           palette[i].green = colormap->colors[i].green;
411           palette[i].flags = DoGreen;
412         }
413
414       XStoreColors (private->xdisplay, private->xcolormap, palette, size);
415
416       shift = visual->blue_shift;
417       max_colors = 1 << visual->blue_prec;
418       size = (ncolors < max_colors) ? (ncolors) : (max_colors);
419
420       for (i = 0; i < size; i++)
421         {
422           palette[i].pixel = i << shift;
423           palette[i].blue = colormap->colors[i].blue;
424           palette[i].flags = DoBlue;
425         }
426
427       XStoreColors (private->xdisplay, private->xcolormap, palette, size);
428       break;
429
430     default:
431       break;
432     }
433
434   g_free (palette);
435 }
436
437 gboolean
438 gdk_colors_alloc (GdkColormap   *colormap,
439                   gboolean       contiguous,
440                   gulong        *planes,
441                   gint           nplanes,
442                   gulong        *pixels,
443                   gint           npixels)
444 {
445   GdkColormapPrivateX11 *private;
446   gint return_val;
447   gint i;
448
449   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), 0);
450
451   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
452
453   return_val = XAllocColorCells (private->xdisplay, private->xcolormap,
454                                  contiguous, planes, nplanes, pixels, npixels);
455
456   if (return_val)
457     {
458       for (i=0; i<npixels; i++)
459         {
460           private->info[pixels[i]].ref_count++;
461           private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
462         }
463     }
464
465   return return_val != 0;
466 }
467
468 /* This is almost identical to gdk_colormap_free_colors.
469  * Keep them in sync!
470  */
471 void
472 gdk_colors_free (GdkColormap *colormap,
473                  gulong      *in_pixels,
474                  gint         in_npixels,
475                  gulong       planes)
476 {
477   GdkColormapPrivateX11 *private;
478   gulong *pixels;
479   gint npixels = 0;
480   gint i;
481
482   g_return_if_fail (GDK_IS_COLORMAP (colormap));
483   g_return_if_fail (in_pixels != NULL);
484
485   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
486
487   if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
488       (colormap->visual->type != GDK_VISUAL_GRAYSCALE))
489     return;
490   
491   pixels = g_new (gulong, in_npixels);
492
493   for (i=0; i<in_npixels; i++)
494     {
495       gulong pixel = in_pixels[i];
496       
497       if (private->info[pixel].ref_count)
498         {
499           private->info[pixel].ref_count--;
500
501           if (private->info[pixel].ref_count == 0)
502             {
503               pixels[npixels++] = pixel;
504               if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
505                 g_hash_table_remove (private->hash, &colormap->colors[pixel]);
506               private->info[pixel].flags = 0;
507             }
508         }
509     }
510
511   if (npixels)
512     XFreeColors (private->xdisplay, private->xcolormap,
513                  pixels, npixels, planes);
514   g_free (pixels);
515 }
516
517 /* This is almost identical to gdk_colors_free.
518  * Keep them in sync!
519  */
520 void
521 gdk_colormap_free_colors (GdkColormap *colormap,
522                           GdkColor    *colors,
523                           gint         ncolors)
524 {
525   GdkColormapPrivateX11 *private;
526   gulong *pixels;
527   gint npixels = 0;
528   gint i;
529
530   g_return_if_fail (GDK_IS_COLORMAP (colormap));
531   g_return_if_fail (colors != NULL);
532
533   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
534
535   if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
536       (colormap->visual->type != GDK_VISUAL_GRAYSCALE))
537     return;
538
539   pixels = g_new (gulong, ncolors);
540
541   for (i=0; i<ncolors; i++)
542     {
543       gulong pixel = colors[i].pixel;
544       
545       if (private->info[pixel].ref_count)
546         {
547           private->info[pixel].ref_count--;
548
549           if (private->info[pixel].ref_count == 0)
550             {
551               pixels[npixels++] = pixel;
552               if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
553                 g_hash_table_remove (private->hash, &colormap->colors[pixel]);
554               private->info[pixel].flags = 0;
555             }
556         }
557     }
558
559   if (npixels)
560     XFreeColors (private->xdisplay, private->xcolormap,
561                  pixels, npixels, 0);
562
563   g_free (pixels);
564 }
565
566 /********************
567  * Color allocation *
568  ********************/
569
570 /* Try to allocate a single color using XAllocColor. If it succeeds,
571  * cache the result in our colormap, and store in ret.
572  */
573 static gboolean 
574 gdk_colormap_alloc1 (GdkColormap *colormap,
575                      GdkColor    *color,
576                      GdkColor    *ret)
577 {
578   GdkColormapPrivateX11 *private;
579   XColor xcolor;
580
581   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
582
583   xcolor.red = color->red;
584   xcolor.green = color->green;
585   xcolor.blue = color->blue;
586   xcolor.pixel = color->pixel;
587   xcolor.flags = DoRed | DoGreen | DoBlue;
588
589   if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
590     {
591       ret->pixel = xcolor.pixel;
592       ret->red = xcolor.red;
593       ret->green = xcolor.green;
594       ret->blue = xcolor.blue;
595       
596       if (ret->pixel < colormap->size)
597         {
598           if (private->info[ret->pixel].ref_count) /* got a duplicate */
599             {
600               XFreeColors (private->xdisplay, private->xcolormap,
601                            &xcolor.pixel, 1, 0);
602             }
603           else
604             {
605               colormap->colors[ret->pixel] = *color;
606               colormap->colors[ret->pixel].pixel = ret->pixel;
607               private->info[ret->pixel].ref_count = 1;
608
609               g_hash_table_insert (private->hash,
610                                    &colormap->colors[ret->pixel],
611                                    &colormap->colors[ret->pixel]);
612             }
613         }
614       return TRUE;
615     }
616   else
617     {
618       return FALSE;
619     }
620 }
621
622 static gint
623 gdk_colormap_alloc_colors_writeable (GdkColormap *colormap,
624                                      GdkColor    *colors,
625                                      gint         ncolors,
626                                      gboolean     writeable,
627                                      gboolean     best_match,
628                                      gboolean    *success)
629 {
630   GdkColormapPrivateX11 *private;
631   gulong *pixels;
632   Status status;
633   gint i, index;
634
635   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
636
637   if (private->private_val)
638     {
639       index = 0;
640       for (i=0; i<ncolors; i++)
641         {
642           while ((index < colormap->size) && (private->info[index].ref_count != 0))
643             index++;
644           
645           if (index < colormap->size)
646             {
647               colors[i].pixel = index;
648               success[i] = TRUE;
649               private->info[index].ref_count++;
650               private->info[i].flags |= GDK_COLOR_WRITEABLE;
651             }
652           else
653             break;
654         }
655       return i;
656     }
657   else
658     {
659       pixels = g_new (gulong, ncolors);
660       /* Allocation of a writeable color cells */
661       
662       status =  XAllocColorCells (private->xdisplay, private->xcolormap,
663                                   FALSE, NULL, 0, pixels, ncolors);
664       if (status)
665         {
666           for (i=0; i<ncolors; i++)
667             {
668               colors[i].pixel = pixels[i];
669               private->info[pixels[i]].ref_count++;
670               private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
671             }
672         }
673       
674       g_free (pixels);
675
676       return status ? ncolors : 0; 
677     }
678 }
679
680 static gint
681 gdk_colormap_alloc_colors_private (GdkColormap *colormap,
682                                    GdkColor    *colors,
683                                    gint         ncolors,
684                                    gboolean     writeable,
685                                    gboolean     best_match,
686                                    gboolean    *success)
687 {
688   GdkColormapPrivateX11 *private;
689   gint i, index;
690   XColor *store = g_new (XColor, ncolors);
691   gint nstore = 0;
692   gint nremaining = 0;
693   
694   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
695   index = -1;
696
697   /* First, store the colors we have room for */
698
699   index = 0;
700   for (i=0; i<ncolors; i++)
701     {
702       if (!success[i])
703         {
704           while ((index < colormap->size) && (private->info[index].ref_count != 0))
705             index++;
706
707           if (index < colormap->size)
708             {
709               store[nstore].red = colors[i].red;
710               store[nstore].blue = colors[i].blue;
711               store[nstore].green = colors[i].green;
712               store[nstore].pixel = index;
713               nstore++;
714
715               success[i] = TRUE;
716
717               colors[i].pixel = index;
718               private->info[index].ref_count++;
719             }
720           else
721             nremaining++;
722         }
723     }
724   
725   XStoreColors (private->xdisplay, private->xcolormap, store, nstore);
726   g_free (store);
727
728   if (nremaining > 0 && best_match)
729     {
730       /* Get best matches for remaining colors */
731
732       gchar *available = g_new (gchar, colormap->size);
733       for (i = 0; i < colormap->size; i++)
734         available[i] = TRUE;
735
736       for (i=0; i<ncolors; i++)
737         {
738           if (!success[i])
739             {
740               index = gdk_colormap_match_color (colormap, 
741                                                 &colors[i], 
742                                                 available);
743               if (index != -1)
744                 {
745                   colors[i] = colormap->colors[index];
746                   private->info[index].ref_count++;
747
748                   success[i] = TRUE;
749                   nremaining--;
750                 }
751             }
752         }
753       g_free (available);
754     }
755
756   return (ncolors - nremaining);
757 }
758
759 static gint
760 gdk_colormap_alloc_colors_shared (GdkColormap *colormap,
761                                   GdkColor    *colors,
762                                   gint         ncolors,
763                                   gboolean     writeable,
764                                   gboolean     best_match,
765                                   gboolean    *success)
766 {
767   GdkColormapPrivateX11 *private;
768   gint i, index;
769   gint nremaining = 0;
770   gint nfailed = 0;
771
772   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
773   index = -1;
774
775   for (i=0; i<ncolors; i++)
776     {
777       if (!success[i])
778         {
779           if (gdk_colormap_alloc1 (colormap, &colors[i], &colors[i]))
780             success[i] = TRUE;
781           else
782             nremaining++;
783         }
784     }
785
786
787   if (nremaining > 0 && best_match)
788     {
789       gchar *available = g_new (gchar, colormap->size);
790       for (i = 0; i < colormap->size; i++)
791         available[i] = ((private->info[i].ref_count == 0) ||
792                         !(private->info[i].flags & GDK_COLOR_WRITEABLE));
793       gdk_colormap_sync (colormap, FALSE);
794       
795       while (nremaining > 0)
796         {
797           for (i=0; i<ncolors; i++)
798             {
799               if (!success[i])
800                 {
801                   index = gdk_colormap_match_color (colormap, &colors[i], available);
802                   if (index != -1)
803                     {
804                       if (private->info[index].ref_count)
805                         {
806                           private->info[index].ref_count++;
807                           colors[i] = colormap->colors[index];
808                           success[i] = TRUE;
809                           nremaining--;
810                         }
811                       else
812                         {
813                           if (gdk_colormap_alloc1 (colormap, 
814                                                    &colormap->colors[index],
815                                                    &colors[i]))
816                             {
817                               success[i] = TRUE;
818                               nremaining--;
819                               break;
820                             }
821                           else
822                             {
823                               available[index] = FALSE;
824                             }
825                         }
826                     }
827                   else
828                     {
829                       nfailed++;
830                       nremaining--;
831                       success[i] = 2; /* flag as permanent failure */
832                     }
833                 }
834             }
835         }
836       g_free (available);
837     }
838
839   /* Change back the values we flagged as permanent failures */
840   if (nfailed > 0)
841     {
842       for (i=0; i<ncolors; i++)
843         if (success[i] == 2)
844           success[i] = FALSE;
845       nremaining = nfailed;
846     }
847   
848   return (ncolors - nremaining);
849 }
850
851 static gint
852 gdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap,
853                                        GdkColor    *colors,
854                                        gint         ncolors,
855                                        gboolean     writeable,
856                                        gboolean     best_match,
857                                        gboolean    *success)
858 {
859   GdkColormapPrivateX11 *private;
860   GdkColor *lookup_color;
861   gint i;
862   gint nremaining = 0;
863
864   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
865
866   /* Check for an exact match among previously allocated colors */
867
868   for (i=0; i<ncolors; i++)
869     {
870       if (!success[i])
871         {
872           lookup_color = g_hash_table_lookup (private->hash, &colors[i]);
873           if (lookup_color)
874             {
875               private->info[lookup_color->pixel].ref_count++;
876               colors[i].pixel = lookup_color->pixel;
877               success[i] = TRUE;
878             }
879           else
880             nremaining++;
881         }
882     }
883
884   /* If that failed, we try to allocate a new color, or approxmiate
885    * with what we can get if best_match is TRUE.
886    */
887   if (nremaining > 0)
888     {
889       if (private->private_val)
890         return gdk_colormap_alloc_colors_private (colormap, colors, ncolors, writeable, best_match, success);
891       else
892         return gdk_colormap_alloc_colors_shared (colormap, colors, ncolors, writeable, best_match, success);
893     }
894   else
895     return 0;
896 }
897
898 gint
899 gdk_colormap_alloc_colors (GdkColormap *colormap,
900                            GdkColor    *colors,
901                            gint         ncolors,
902                            gboolean     writeable,
903                            gboolean     best_match,
904                            gboolean    *success)
905 {
906   GdkColormapPrivateX11 *private;
907   GdkVisual *visual;
908   gint i;
909   gint nremaining = 0;
910   XColor xcolor;
911
912   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), FALSE);
913   g_return_val_if_fail (colors != NULL, FALSE);
914
915   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
916
917   for (i=0; i<ncolors; i++)
918     {
919       success[i] = FALSE;
920     }
921
922   switch (colormap->visual->type)
923     {
924     case GDK_VISUAL_PSEUDO_COLOR:
925     case GDK_VISUAL_GRAYSCALE:
926       if (writeable)
927         return gdk_colormap_alloc_colors_writeable (colormap, colors, ncolors,
928                                                     writeable, best_match, success);
929       else
930         return gdk_colormap_alloc_colors_pseudocolor (colormap, colors, ncolors,
931                                                     writeable, best_match, success);
932       break;
933
934     case GDK_VISUAL_DIRECT_COLOR:
935     case GDK_VISUAL_TRUE_COLOR:
936       visual = colormap->visual;
937
938       for (i=0; i<ncolors; i++)
939         {
940           colors[i].pixel = (((colors[i].red >> (16 - visual->red_prec)) << visual->red_shift) +
941                              ((colors[i].green >> (16 - visual->green_prec)) << visual->green_shift) +
942                              ((colors[i].blue >> (16 - visual->blue_prec)) << visual->blue_shift));
943           success[i] = TRUE;
944         }
945       break;
946
947     case GDK_VISUAL_STATIC_GRAY:
948     case GDK_VISUAL_STATIC_COLOR:
949       for (i=0; i<ncolors; i++)
950         {
951           xcolor.red = colors[i].red;
952           xcolor.green = colors[i].green;
953           xcolor.blue = colors[i].blue;
954           xcolor.pixel = colors[i].pixel;
955           xcolor.flags = DoRed | DoGreen | DoBlue;
956
957           if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
958             {
959               colors[i].pixel = xcolor.pixel;
960               success[i] = TRUE;
961             }
962           else
963             nremaining++;
964         }
965       break;
966     }
967   return nremaining;
968 }
969
970 /**
971  * gdk_colormap_query_color:
972  * @colormap: a #GdkColormap
973  * @pixel: pixel value in hardware display format
974  * @result: #GdkColor with red, green, blue fields initialized
975  * 
976  * Locates the RGB color in @colormap corresponding to the given
977  * hardware pixel @pixel. @pixel must be a valid pixel in the
978  * colormap; it's a programmer error to call this function with a
979  * pixel which is not in the colormap. Hardware pixels are normally
980  * obtained from gdk_colormap_alloc_colors(), or from a #GdkImage. (A
981  * #GdkImage contains image data in hardware format, a #GdkPixbuf
982  * contains image data in a canonical 24-bit RGB format.)
983  *
984  * This function is rarely useful, it's used for example to
985  * implement the eyedropper feature in #GtkColorSelection.
986  * 
987  **/
988 void
989 gdk_colormap_query_color (GdkColormap *colormap,
990                           gulong       pixel,
991                           GdkColor    *result)
992 {
993   XColor xcolor;
994   GdkVisual *visual;
995   GdkColormapPrivateX11 *private;
996   
997   g_return_if_fail (GDK_IS_COLORMAP (colormap));
998   
999   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1000
1001   visual = gdk_colormap_get_visual (colormap);
1002
1003   switch (visual->type) {
1004   case GDK_VISUAL_DIRECT_COLOR:
1005   case GDK_VISUAL_TRUE_COLOR:
1006     result->red = 65535. * (double)((pixel & visual->red_mask) >> visual->red_shift) / ((1 << visual->red_prec) - 1);
1007     result->green = 65535. * (double)((pixel & visual->green_mask) >> visual->green_shift) / ((1 << visual->green_prec) - 1);
1008     result->blue = 65535. * (double)((pixel & visual->blue_mask) >> visual->blue_shift) / ((1 << visual->blue_prec) - 1);
1009     break;
1010   case GDK_VISUAL_STATIC_GRAY:
1011   case GDK_VISUAL_GRAYSCALE:
1012     result->red = result->green = result->blue = 65535. * (double)pixel/((1<<visual->depth) - 1);
1013     break;
1014   case GDK_VISUAL_STATIC_COLOR:
1015     xcolor.pixel = pixel;
1016     XQueryColor (private->xdisplay, private->xcolormap, &xcolor);
1017     result->red = xcolor.red;
1018     result->green = xcolor.green;
1019     result->blue =  xcolor.blue;
1020     break;
1021   case GDK_VISUAL_PSEUDO_COLOR:
1022     g_return_if_fail (pixel < colormap->size);
1023     result->red = colormap->colors[pixel].red;
1024     result->green = colormap->colors[pixel].green;
1025     result->blue = colormap->colors[pixel].blue;
1026     break;
1027   default:
1028     g_assert_not_reached ();
1029     break;
1030   }
1031 }
1032
1033 gboolean
1034 gdk_color_change (GdkColormap *colormap,
1035                   GdkColor    *color)
1036 {
1037   GdkColormapPrivateX11 *private;
1038   XColor xcolor;
1039
1040   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), FALSE);
1041   g_return_val_if_fail (color != NULL, FALSE);
1042
1043   xcolor.pixel = color->pixel;
1044   xcolor.red = color->red;
1045   xcolor.green = color->green;
1046   xcolor.blue = color->blue;
1047   xcolor.flags = DoRed | DoGreen | DoBlue;
1048
1049   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1050   XStoreColor (private->xdisplay, private->xcolormap, &xcolor);
1051
1052   return TRUE;
1053 }
1054
1055 /* XXX: Do not use this function until it is fixed. An X Colormap
1056  *      is useless unless we also have the visual.
1057  */
1058 GdkColormap*
1059 gdkx_colormap_get (Colormap xcolormap)
1060 {
1061   GdkColormap *colormap;
1062   GdkColormapPrivateX11 *private;
1063
1064   colormap = gdk_colormap_lookup (xcolormap);
1065   if (colormap)
1066     return colormap;
1067
1068   if (xcolormap == DefaultColormap (gdk_display, _gdk_screen))
1069     return gdk_colormap_get_system ();
1070
1071   colormap = g_object_new (gdk_colormap_get_type (), NULL);
1072   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1073
1074   private->xdisplay = gdk_display;
1075   private->xcolormap = xcolormap;
1076   colormap->visual = NULL;
1077   private->private_val = TRUE;
1078
1079   /* To do the following safely, we would have to have some way of finding
1080    * out what the size or visual of the given colormap is. It seems
1081    * X doesn't allow this
1082    */
1083
1084 #if 0
1085   for (i = 0; i < 256; i++)
1086     {
1087       xpalette[i].pixel = i;
1088       xpalette[i].red = 0;
1089       xpalette[i].green = 0;
1090       xpalette[i].blue = 0;
1091     }
1092
1093   XQueryColors (gdk_display, private->xcolormap, xpalette, 256);
1094
1095   for (i = 0; i < 256; i++)
1096     {
1097       colormap->colors[i].pixel = xpalette[i].pixel;
1098       colormap->colors[i].red = xpalette[i].red;
1099       colormap->colors[i].green = xpalette[i].green;
1100       colormap->colors[i].blue = xpalette[i].blue;
1101     }
1102 #endif
1103
1104   colormap->colors = NULL;
1105   colormap->size = 0;
1106
1107   gdk_colormap_add (colormap);
1108
1109   return colormap;
1110 }
1111
1112
1113 static gint
1114 gdk_colormap_match_color (GdkColormap *cmap,
1115                           GdkColor    *color,
1116                           const gchar *available)
1117 {
1118   GdkColor *colors;
1119   guint sum, max;
1120   gint rdiff, gdiff, bdiff;
1121   gint i, index;
1122
1123   g_return_val_if_fail (cmap != NULL, 0);
1124   g_return_val_if_fail (color != NULL, 0);
1125
1126   colors = cmap->colors;
1127   max = 3 * (65536);
1128   index = -1;
1129
1130   for (i = 0; i < cmap->size; i++)
1131     {
1132       if ((!available) || (available && available[i]))
1133         {
1134           rdiff = (color->red - colors[i].red);
1135           gdiff = (color->green - colors[i].green);
1136           bdiff = (color->blue - colors[i].blue);
1137
1138           sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1139
1140           if (sum < max)
1141             {
1142               index = i;
1143               max = sum;
1144             }
1145         }
1146     }
1147
1148   return index;
1149 }
1150
1151
1152 GdkColormap*
1153 gdk_colormap_lookup (Colormap xcolormap)
1154 {
1155   GdkColormap *cmap;
1156
1157   if (!colormap_hash)
1158     return NULL;
1159
1160   cmap = g_hash_table_lookup (colormap_hash, &xcolormap);
1161   return cmap;
1162 }
1163
1164 static void
1165 gdk_colormap_add (GdkColormap *cmap)
1166 {
1167   GdkColormapPrivateX11 *private;
1168
1169   if (!colormap_hash)
1170     colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
1171                                       (GEqualFunc) gdk_colormap_equal);
1172
1173   private = GDK_COLORMAP_PRIVATE_DATA (cmap);
1174
1175   g_hash_table_insert (colormap_hash, &private->xcolormap, cmap);
1176 }
1177
1178 static void
1179 gdk_colormap_remove (GdkColormap *cmap)
1180 {
1181   GdkColormapPrivateX11 *private;
1182
1183   if (!colormap_hash)
1184     colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
1185                                       (GEqualFunc) gdk_colormap_equal);
1186
1187   private = GDK_COLORMAP_PRIVATE_DATA (cmap);
1188
1189   g_hash_table_remove (colormap_hash, &private->xcolormap);
1190 }
1191
1192 static guint
1193 gdk_colormap_hash (Colormap *cmap)
1194 {
1195   return *cmap;
1196 }
1197
1198 static gboolean
1199 gdk_colormap_equal (Colormap *a,
1200                     Colormap *b)
1201 {
1202   return (*a == *b);
1203 }
1204
1205 Display *
1206 gdk_x11_colormap_get_xdisplay (GdkColormap *colormap)
1207 {
1208   GdkColormapPrivateX11 *private;
1209
1210   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
1211
1212   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1213
1214   return private->xdisplay;
1215 }
1216
1217 Colormap
1218 gdk_x11_colormap_get_xcolormap (GdkColormap *colormap)
1219 {
1220   GdkColormapPrivateX11 *private;
1221
1222   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), None);
1223
1224   private = GDK_COLORMAP_PRIVATE_DATA (colormap);
1225
1226   return private->xcolormap;
1227 }