Debian Codegraph @

gdk/x11/gdkcolor-x11.c

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