* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
*
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
*/
-#include <X11/Xlib.h>
-#include "gdk.h"
-#include "gdkprivate.h"
+/*
+ * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
+ * file for a list of people on the GTK+ Team. See the ChangeLog
+ * files for a list of changes. These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
+ */
+
+#include <time.h>
+
+#include "gdkcolor.h"
+#include "gdkinternals.h"
+#include "gdkx.h"
+#include "gdkprivate-x11.h"
+#include "gdkscreen-x11.h"
+
+typedef struct _GdkColormapPrivateX11 GdkColormapPrivateX11;
+
+struct _GdkColormapPrivateX11
+{
+ Colormap xcolormap;
+ Display *xdisplay;
+ gint private_val;
+
+ GHashTable *hash;
+ GdkColorInfo *info;
+ time_t last_sync_time;
+};
+
+#define GDK_COLORMAP_PRIVATE_DATA(cmap) ((GdkColormapPrivateX11 *) GDK_COLORMAP (cmap)->windowing_data)
+
+static gint gdk_colormap_match_color (GdkColormap *cmap,
+ GdkColor *color,
+ const gchar *available);
+static void gdk_colormap_add (GdkColormap *cmap);
+static void gdk_colormap_remove (GdkColormap *cmap);
+static guint gdk_colormap_hash (Colormap *cmap);
+static gboolean gdk_colormap_equal (Colormap *a,
+ Colormap *b);
+static void gdk_colormap_sync (GdkColormap *colormap,
+ gboolean force);
+
+static void gdk_colormap_init (GdkColormap *colormap);
+static void gdk_colormap_class_init (GdkColormapClass *klass);
+static void gdk_colormap_finalize (GObject *object);
-static gint gdk_colormap_match_color (GdkColormap *cmap,
- GdkColor *color,
- const gchar *available);
-static void gdk_colormap_add (GdkColormap *cmap);
-static void gdk_colormap_remove (GdkColormap *cmap);
-static guint gdk_colormap_hash (Colormap *cmap);
-static gint gdk_colormap_cmp (Colormap *a,
- Colormap *b);
-static void gdk_colormap_real_destroy (GdkColormap *colormap);
+static gpointer parent_class = NULL;
static GHashTable *colormap_hash = NULL;
+GType
+gdk_colormap_get_type (void)
+{
+ static GType object_type = 0;
+
+ if (!object_type)
+ {
+ static const GTypeInfo object_info =
+ {
+ sizeof (GdkColormapClass),
+ (GBaseInitFunc) NULL,
+ (GBaseFinalizeFunc) NULL,
+ (GClassInitFunc) gdk_colormap_class_init,
+ NULL, /* class_finalize */
+ NULL, /* class_data */
+ sizeof (GdkColormap),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) gdk_colormap_init,
+ };
+
+ object_type = g_type_register_static (G_TYPE_OBJECT,
+ "GdkColormap",
+ &object_info, 0);
+ }
+
+ return object_type;
+}
+
+static void
+gdk_colormap_init (GdkColormap *colormap)
+{
+ GdkColormapPrivateX11 *private;
+
+ private = g_new (GdkColormapPrivateX11, 1);
+
+ colormap->screen = NULL;
+ colormap->windowing_data = private;
+
+ private->hash = NULL;
+ private->last_sync_time = 0;
+ private->info = NULL;
+
+ colormap->size = 0;
+ colormap->colors = NULL;
+}
+
+static void
+gdk_colormap_class_init (GdkColormapClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ parent_class = g_type_class_peek_parent (klass);
+
+ object_class->finalize = gdk_colormap_finalize;
+}
+
+static void
+gdk_colormap_finalize (GObject *object)
+{
+ GdkColormap *colormap = GDK_COLORMAP (object);
+ GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+ gdk_colormap_remove (colormap);
+
+ XFreeColormap (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap);
+
+ if (private->hash)
+ g_hash_table_destroy (private->hash);
+
+ g_free (private->info);
+ g_free (colormap->colors);
+
+ G_OBJECT_CLASS (parent_class)->finalize (object);
+}
GdkColormap*
gdk_colormap_new (GdkVisual *visual,
- gint private_cmap)
+ gboolean private_cmap)
{
GdkColormap *colormap;
- GdkColormapPrivate *private;
+ GdkColormapPrivateX11 *private;
Visual *xvisual;
- XColor default_colors[256];
+ Display *xdisplay;
+ Window xrootwin;
int size;
int i;
+ /* FIXME when object properties settle down, there needs to be some
+ * kind of default construction (and construct-only arguments)
+ */
+
g_return_val_if_fail (visual != NULL, NULL);
- private = g_new (GdkColormapPrivate, 1);
- colormap = (GdkColormap*) private;
+ colormap = g_object_new (gdk_colormap_get_type (), NULL);
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
- private->xdisplay = gdk_display;
- private->visual = visual;
- private->next_color = 0;
- private->ref_count = 1;
+ colormap->visual = visual;
+ colormap->screen = visual->screen;
+
xvisual = ((GdkVisualPrivate*) visual)->xvisual;
+ xdisplay = GDK_SCREEN_XDISPLAY (visual->screen);
+ xrootwin = GDK_SCREEN_XROOTWIN (visual->screen);
+
+ colormap->size = visual->colormap_size;
switch (visual->type)
{
case GDK_VISUAL_GRAYSCALE:
case GDK_VISUAL_PSEUDO_COLOR:
+ private->info = g_new0 (GdkColorInfo, colormap->size);
+ colormap->colors = g_new (GdkColor, colormap->size);
+
+ private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
+ (GEqualFunc) gdk_color_equal);
+
private->private_val = private_cmap;
- private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
+ private->xcolormap = XCreateColormap (xdisplay, xrootwin,
xvisual, (private_cmap) ? (AllocAll) : (AllocNone));
if (private_cmap)
{
- for (i = 0; i < 256; i++)
- default_colors[i].pixel = i;
+ XColor *default_colors;
- XQueryColors (private->xdisplay,
- DefaultColormap (private->xdisplay, gdk_screen),
- default_colors, visual->colormap_size);
+ default_colors = g_new (XColor, colormap->size);
- for (i = 0; i < visual->colormap_size; i++)
+ for (i = 0; i < colormap->size; i++)
+ default_colors[i].pixel = i;
+
+ XQueryColors (xdisplay,
+ DefaultColormapOfScreen (GDK_SCREEN_X11 (visual->screen)->xscreen),
+ default_colors, colormap->size);
+
+ for (i = 0; i < colormap->size; i++)
{
colormap->colors[i].pixel = default_colors[i].pixel;
colormap->colors[i].red = default_colors[i].red;
colormap->colors[i].blue = default_colors[i].blue;
}
- gdk_colormap_change (colormap, visual->colormap_size);
+ gdk_colormap_change (colormap, colormap->size);
+
+ g_free (default_colors);
}
break;
case GDK_VISUAL_DIRECT_COLOR:
private->private_val = TRUE;
- private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
+ private->xcolormap = XCreateColormap (xdisplay, xrootwin,
xvisual, AllocAll);
+ colormap->colors = g_new (GdkColor, colormap->size);
size = 1 << visual->red_prec;
for (i = 0; i < size; i++)
for (i = 0; i < size; i++)
colormap->colors[i].blue = i * 65535 / (size - 1);
- gdk_colormap_change (colormap, visual->colormap_size);
+ gdk_colormap_change (colormap, colormap->size);
break;
case GDK_VISUAL_STATIC_GRAY:
case GDK_VISUAL_STATIC_COLOR:
+ private->private_val = FALSE;
+ private->xcolormap = XCreateColormap (xdisplay, xrootwin,
+ xvisual, AllocNone);
+
+ colormap->colors = g_new (GdkColor, colormap->size);
+ gdk_colormap_sync (colormap, TRUE);
+ break;
+
case GDK_VISUAL_TRUE_COLOR:
private->private_val = FALSE;
- private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
+ private->xcolormap = XCreateColormap (xdisplay, xrootwin,
xvisual, AllocNone);
break;
}
return colormap;
}
+#define MIN_SYNC_TIME 2
+
static void
-gdk_colormap_real_destroy (GdkColormap *colormap)
+gdk_colormap_sync (GdkColormap *colormap,
+ gboolean force)
{
- GdkColormapPrivate *private = (GdkColormapPrivate*) colormap;
-
- g_return_if_fail (colormap != NULL);
+ time_t current_time;
+ GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+ XColor *xpalette;
+ gint nlookup;
+ gint i;
+
+ g_return_if_fail (GDK_IS_COLORMAP (colormap));
- if (private->ref_count > 0)
+ current_time = time (NULL);
+ if (!force && ((current_time - private->last_sync_time) < MIN_SYNC_TIME))
return;
- gdk_colormap_remove (colormap);
- XFreeColormap (private->xdisplay, private->xcolormap);
- g_free (colormap);
-}
-
-GdkColormap*
-gdk_colormap_ref (GdkColormap *cmap)
-{
- GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
- g_return_val_if_fail (cmap != NULL, NULL);
+ private->last_sync_time = current_time;
- private->ref_count += 1;
- return cmap;
+ nlookup = 0;
+ xpalette = g_new (XColor, colormap->size);
+
+ for (i = 0; i < colormap->size; i++)
+ {
+ if (!private->info || private->info[i].ref_count == 0)
+ {
+ xpalette[nlookup].pixel = i;
+ xpalette[nlookup].red = 0;
+ xpalette[nlookup].green = 0;
+ xpalette[nlookup].blue = 0;
+ nlookup++;
+ }
+ }
+
+ XQueryColors (GDK_SCREEN_XDISPLAY (colormap->screen),
+ private->xcolormap, xpalette, nlookup);
+
+ for (i = 0; i < nlookup; i++)
+ {
+ gulong pixel = xpalette[i].pixel;
+ colormap->colors[pixel].pixel = pixel;
+ colormap->colors[pixel].red = xpalette[i].red;
+ colormap->colors[pixel].green = xpalette[i].green;
+ colormap->colors[pixel].blue = xpalette[i].blue;
+ }
+
+ g_free (xpalette);
}
-
-void
-gdk_colormap_unref (GdkColormap *cmap)
+
+/**
+ * gdk_screen_get_system_colormap:
+ * @screen: a #GdkScreen
+ *
+ * Gets the system's default colormap for @screen
+ *
+ * Returns: the default colormap for @screen.
+ */
+GdkColormap *
+gdk_screen_get_system_colormap (GdkScreen *screen)
{
- GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
- g_return_if_fail (cmap != NULL);
+ GdkColormap *colormap = NULL;
+ GdkColormapPrivateX11 *private;
+ GdkScreenX11 *screen_x11;
- private->ref_count -= 1;
- if (private->ref_count == 0)
- gdk_colormap_real_destroy (cmap);
-}
+ g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
+ screen_x11 = GDK_SCREEN_X11 (screen);
-GdkColormap*
-gdk_colormap_get_system (void)
-{
- static GdkColormap *colormap = NULL;
- GdkColormapPrivate *private;
- XColor xpalette[256];
- gint i;
+ if (screen_x11->system_colormap)
+ return screen_x11->system_colormap;
- if (!colormap)
- {
- private = g_new (GdkColormapPrivate, 1);
- colormap = (GdkColormap*) private;
+ colormap = g_object_new (gdk_colormap_get_type (), NULL);
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
- private->xdisplay = gdk_display;
- private->xcolormap = DefaultColormap (gdk_display, gdk_screen);
- private->visual = gdk_visual_get_system ();
- private->private_val = FALSE;
- private->next_color = 0;
- private->ref_count = 1;
+ colormap->screen = screen;
+ colormap->visual = gdk_screen_get_system_visual (screen);
+
+ private->xcolormap = DefaultColormapOfScreen (screen_x11->xscreen);
+ private->private_val = FALSE;
- if ((private->visual->type == GDK_VISUAL_GRAYSCALE) ||
- (private->visual->type == GDK_VISUAL_PSEUDO_COLOR))
- {
- for (i = 0; i < 256; i++)
- {
- xpalette[i].pixel = i;
- xpalette[i].red = 0;
- xpalette[i].green = 0;
- xpalette[i].blue = 0;
- }
-
- XQueryColors (gdk_display, private->xcolormap, xpalette,
- MIN (private->visual->colormap_size, 256));
-
- for (i = 0; i < 256; i++)
- {
- colormap->colors[i].pixel = xpalette[i].pixel;
- colormap->colors[i].red = xpalette[i].red;
- colormap->colors[i].green = xpalette[i].green;
- colormap->colors[i].blue = xpalette[i].blue;
- }
- }
+ private->hash = NULL;
+ private->last_sync_time = 0;
+ private->info = NULL;
- gdk_colormap_add (colormap);
- }
+ colormap->colors = NULL;
+ colormap->size = colormap->visual->colormap_size;
+ switch (colormap->visual->type)
+ {
+ case GDK_VISUAL_GRAYSCALE:
+ case GDK_VISUAL_PSEUDO_COLOR:
+ private->info = g_new0 (GdkColorInfo, colormap->size);
+ private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
+ (GEqualFunc) gdk_color_equal);
+ /* Fall through */
+ case GDK_VISUAL_STATIC_GRAY:
+ case GDK_VISUAL_STATIC_COLOR:
+ colormap->colors = g_new (GdkColor, colormap->size);
+ gdk_colormap_sync (colormap, TRUE);
+
+ case GDK_VISUAL_DIRECT_COLOR:
+ case GDK_VISUAL_TRUE_COLOR:
+ break;
+ }
+
+ gdk_colormap_add (colormap);
+ screen_x11->system_colormap = colormap;
+
return colormap;
}
+/**
+ * gdk_colormap_get_system:
+ *
+ * Gets the system's default colormap for the default screen. (See
+ * gdk_colormap_get_system_for_screen ())
+ *
+ * Return value: the default colormap.
+ **/
+GdkColormap*
+gdk_colormap_get_system (void)
+{
+ return gdk_screen_get_system_colormap (gdk_get_default_screen ());
+}
+
gint
gdk_colormap_get_system_size (void)
{
- return DisplayCells (gdk_display, gdk_screen);
+ return DisplayCells (GDK_SCREEN_XDISPLAY (gdk_get_default_screen()),
+ GDK_SCREEN_X11 (gdk_get_default_screen())->screen_num);
}
void
gdk_colormap_change (GdkColormap *colormap,
gint ncolors)
{
- GdkColormapPrivate *private;
+ GdkColormapPrivateX11 *private;
GdkVisual *visual;
- XColor palette[256];
+ XColor *palette;
+ Display *xdisplay;
gint shift;
int max_colors;
int size;
int i;
- g_return_if_fail (colormap != NULL);
+ g_return_if_fail (GDK_IS_COLORMAP (colormap));
+
+ xdisplay = GDK_SCREEN_XDISPLAY (colormap->screen);
+ palette = g_new (XColor, ncolors);
- private = (GdkColormapPrivate*) colormap;
- switch (private->visual->type)
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+ switch (colormap->visual->type)
{
case GDK_VISUAL_GRAYSCALE:
case GDK_VISUAL_PSEUDO_COLOR:
palette[i].flags = DoRed | DoGreen | DoBlue;
}
- XStoreColors (private->xdisplay, private->xcolormap, palette, ncolors);
- private->next_color = MAX (private->next_color, ncolors);
+ XStoreColors (xdisplay, private->xcolormap, palette, ncolors);
break;
case GDK_VISUAL_DIRECT_COLOR:
- visual = private->visual;
+ visual = colormap->visual;
shift = visual->red_shift;
max_colors = 1 << visual->red_prec;
palette[i].flags = DoRed;
}
- XStoreColors (private->xdisplay, private->xcolormap, palette, size);
+ XStoreColors (xdisplay, private->xcolormap, palette, size);
shift = visual->green_shift;
max_colors = 1 << visual->green_prec;
palette[i].flags = DoGreen;
}
- XStoreColors (private->xdisplay, private->xcolormap, palette, size);
+ XStoreColors (xdisplay, private->xcolormap, palette, size);
shift = visual->blue_shift;
max_colors = 1 << visual->blue_prec;
palette[i].flags = DoBlue;
}
- XStoreColors (private->xdisplay, private->xcolormap, palette, size);
+ XStoreColors (xdisplay, private->xcolormap, palette, size);
break;
default:
break;
}
-}
-
-void
-gdk_colors_store (GdkColormap *colormap,
- GdkColor *colors,
- gint ncolors)
-{
- gint i;
-
- for (i = 0; i < ncolors; i++)
- {
- colormap->colors[i].pixel = colors[i].pixel;
- colormap->colors[i].red = colors[i].red;
- colormap->colors[i].green = colors[i].green;
- colormap->colors[i].blue = colors[i].blue;
- }
- gdk_colormap_change (colormap, ncolors);
+ g_free (palette);
}
-gint
+gboolean
gdk_colors_alloc (GdkColormap *colormap,
- gint contiguous,
+ gboolean contiguous,
gulong *planes,
gint nplanes,
gulong *pixels,
gint npixels)
{
- GdkColormapPrivate *private;
+ GdkColormapPrivateX11 *private;
gint return_val;
+ gint i;
- g_return_val_if_fail (colormap != NULL, 0);
+ g_return_val_if_fail (GDK_IS_COLORMAP (colormap), 0);
- private = (GdkColormapPrivate*) colormap;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
- return_val = XAllocColorCells (private->xdisplay, private->xcolormap,
- contiguous, planes, nplanes, pixels, npixels);
+ return_val = XAllocColorCells (GDK_SCREEN_XDISPLAY (colormap->screen),
+ private->xcolormap,contiguous, planes,
+ nplanes, pixels, npixels);
+ if (return_val)
+ {
+ for (i=0; i<npixels; i++)
+ {
+ private->info[pixels[i]].ref_count++;
+ private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
+ }
+ }
- return return_val;
+ return return_val != 0;
}
+/* This is almost identical to gdk_colormap_free_colors.
+ * Keep them in sync!
+ */
void
gdk_colors_free (GdkColormap *colormap,
- gulong *pixels,
- gint npixels,
+ gulong *in_pixels,
+ gint in_npixels,
gulong planes)
{
- GdkColormapPrivate *private;
+ GdkColormapPrivateX11 *private;
+ gulong *pixels;
+ gint npixels = 0;
+ gint i;
+
+ g_return_if_fail (GDK_IS_COLORMAP (colormap));
+ g_return_if_fail (in_pixels != NULL);
- g_return_if_fail (colormap != NULL);
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+ if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
+ (colormap->visual->type != GDK_VISUAL_GRAYSCALE))
+ return;
+
+ pixels = g_new (gulong, in_npixels);
+
+ for (i=0; i<in_npixels; i++)
+ {
+ gulong pixel = in_pixels[i];
+
+ if (private->info[pixel].ref_count)
+ {
+ private->info[pixel].ref_count--;
- private = (GdkColormapPrivate*) colormap;
+ if (private->info[pixel].ref_count == 0)
+ {
+ pixels[npixels++] = pixel;
+ if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
+ g_hash_table_remove (private->hash, &colormap->colors[pixel]);
+ private->info[pixel].flags = 0;
+ }
+ }
+ }
- XFreeColors (private->xdisplay, private->xcolormap,
- pixels, npixels, planes);
+ if (npixels)
+ XFreeColors (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap,
+ pixels, npixels, planes);
+ g_free (pixels);
}
-gint
-gdk_color_white (GdkColormap *colormap,
- GdkColor *color)
+/* This is almost identical to gdk_colors_free.
+ * Keep them in sync!
+ */
+void
+gdk_colormap_free_colors (GdkColormap *colormap,
+ GdkColor *colors,
+ gint ncolors)
{
- gint return_val;
+ GdkColormapPrivateX11 *private;
+ gulong *pixels;
+ gint npixels = 0;
+ gint i;
+
+ g_return_if_fail (GDK_IS_COLORMAP (colormap));
+ g_return_if_fail (colors != NULL);
- g_return_val_if_fail (colormap != NULL, FALSE);
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
- if (color)
+ if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
+ (colormap->visual->type != GDK_VISUAL_GRAYSCALE))
+ return;
+
+ pixels = g_new (gulong, ncolors);
+
+ for (i=0; i<ncolors; i++)
{
- color->pixel = WhitePixel (gdk_display, gdk_screen);
- color->red = 65535;
- color->green = 65535;
- color->blue = 65535;
+ gulong pixel = colors[i].pixel;
+
+ if (private->info[pixel].ref_count)
+ {
+ private->info[pixel].ref_count--;
- return_val = gdk_color_alloc (colormap, color);
+ if (private->info[pixel].ref_count == 0)
+ {
+ pixels[npixels++] = pixel;
+ if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
+ g_hash_table_remove (private->hash, &colormap->colors[pixel]);
+ private->info[pixel].flags = 0;
+ }
+ }
}
- else
- return_val = FALSE;
- return return_val;
+ if (npixels)
+ XFreeColors (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap,
+ pixels, npixels, 0);
+
+ g_free (pixels);
}
-gint
-gdk_color_black (GdkColormap *colormap,
- GdkColor *color)
+/********************
+ * Color allocation *
+ ********************/
+
+/* Try to allocate a single color using XAllocColor. If it succeeds,
+ * cache the result in our colormap, and store in ret.
+ */
+static gboolean
+gdk_colormap_alloc1 (GdkColormap *colormap,
+ GdkColor *color,
+ GdkColor *ret)
{
- gint return_val;
+ GdkColormapPrivateX11 *private;
+ XColor xcolor;
+
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
- g_return_val_if_fail (colormap != NULL, FALSE);
+ xcolor.red = color->red;
+ xcolor.green = color->green;
+ xcolor.blue = color->blue;
+ xcolor.pixel = color->pixel;
+ xcolor.flags = DoRed | DoGreen | DoBlue;
- if (color)
+ if (XAllocColor (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap, &xcolor))
{
- color->pixel = BlackPixel (gdk_display, gdk_screen);
- color->red = 0;
- color->green = 0;
- color->blue = 0;
+ ret->pixel = xcolor.pixel;
+ ret->red = xcolor.red;
+ ret->green = xcolor.green;
+ ret->blue = xcolor.blue;
+
+ if (ret->pixel < colormap->size)
+ {
+ if (private->info[ret->pixel].ref_count) /* got a duplicate */
+ {
+ XFreeColors (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap,
+ &xcolor.pixel, 1, 0);
+ }
+ else
+ {
+ colormap->colors[ret->pixel] = *color;
+ colormap->colors[ret->pixel].pixel = ret->pixel;
+ private->info[ret->pixel].ref_count = 1;
- return_val = gdk_color_alloc (colormap, color);
+ g_hash_table_insert (private->hash,
+ &colormap->colors[ret->pixel],
+ &colormap->colors[ret->pixel]);
+ }
+ }
+ return TRUE;
}
else
- return_val = FALSE;
-
- return return_val;
+ {
+ return FALSE;
+ }
}
-gint
-gdk_color_parse (const gchar *spec,
- GdkColor *color)
+static gint
+gdk_colormap_alloc_colors_writeable (GdkColormap *colormap,
+ GdkColor *colors,
+ gint ncolors,
+ gboolean writeable,
+ gboolean best_match,
+ gboolean *success)
{
- Colormap xcolormap;
- XColor xcolor;
- gint return_val;
-
- g_return_val_if_fail (spec != NULL, FALSE);
- g_return_val_if_fail (color != NULL, FALSE);
+ GdkColormapPrivateX11 *private;
+ gulong *pixels;
+ Status status;
+ gint i, index;
- xcolormap = DefaultColormap (gdk_display, gdk_screen);
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
- if (XParseColor (gdk_display, xcolormap, spec, &xcolor))
+ if (private->private_val)
{
- return_val = TRUE;
- color->red = xcolor.red;
- color->green = xcolor.green;
- color->blue = xcolor.blue;
+ index = 0;
+ for (i=0; i<ncolors; i++)
+ {
+ while ((index < colormap->size) && (private->info[index].ref_count != 0))
+ index++;
+
+ if (index < colormap->size)
+ {
+ colors[i].pixel = index;
+ success[i] = TRUE;
+ private->info[index].ref_count++;
+ private->info[i].flags |= GDK_COLOR_WRITEABLE;
+ }
+ else
+ break;
+ }
+ return i;
}
else
- return_val = FALSE;
+ {
+ pixels = g_new (gulong, ncolors);
+ /* Allocation of a writeable color cells */
+
+ status = XAllocColorCells (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap,
+ FALSE, NULL, 0, pixels, ncolors);
+ if (status)
+ {
+ for (i=0; i<ncolors; i++)
+ {
+ colors[i].pixel = pixels[i];
+ private->info[pixels[i]].ref_count++;
+ private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
+ }
+ }
+
+ g_free (pixels);
- return return_val;
+ return status ? ncolors : 0;
+ }
}
-gint
-gdk_color_alloc (GdkColormap *colormap,
- GdkColor *color)
+static gint
+gdk_colormap_alloc_colors_private (GdkColormap *colormap,
+ GdkColor *colors,
+ gint ncolors,
+ gboolean writeable,
+ gboolean best_match,
+ gboolean *success)
{
- GdkColormapPrivate *private;
- GdkVisual *visual;
- XColor xcolor;
- gchar available[256];
- gint available_init;
- gint return_val;
+ GdkColormapPrivateX11 *private;
gint i, index;
+ XColor *store = g_new (XColor, ncolors);
+ gint nstore = 0;
+ gint nremaining = 0;
+
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+ index = -1;
- g_return_val_if_fail (colormap != NULL, FALSE);
- g_return_val_if_fail (color != NULL, FALSE);
-
- xcolor.red = color->red;
- xcolor.green = color->green;
- xcolor.blue = color->blue;
- xcolor.pixel = color->pixel;
- xcolor.flags = DoRed | DoGreen | DoBlue;
-
- return_val = FALSE;
- private = (GdkColormapPrivate*) colormap;
+ /* First, store the colors we have room for */
- switch (private->visual->type)
+ index = 0;
+ for (i=0; i<ncolors; i++)
{
- case GDK_VISUAL_GRAYSCALE:
- case GDK_VISUAL_PSEUDO_COLOR:
- if (private->private_val)
+ if (!success[i])
{
- if (private->next_color > 255)
+ while ((index < colormap->size) && (private->info[index].ref_count != 0))
+ index++;
+
+ if (index < colormap->size)
{
- for (i = 0; i < 256; i++)
- available[i] = TRUE;
+ store[nstore].red = colors[i].red;
+ store[nstore].blue = colors[i].blue;
+ store[nstore].green = colors[i].green;
+ store[nstore].pixel = index;
+ nstore++;
- index = gdk_colormap_match_color (colormap, color, available);
- if (index != -1)
- {
- available[index] = FALSE;
- *color = colormap->colors[index];
- return_val = TRUE;
- }
- else
- {
- return_val = FALSE;
- }
+ success[i] = TRUE;
+
+ colors[i].pixel = index;
+ private->info[index].ref_count++;
}
else
+ nremaining++;
+ }
+ }
+
+ XStoreColors (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap,
+ store, nstore);
+ g_free (store);
+
+ if (nremaining > 0 && best_match)
+ {
+ /* Get best matches for remaining colors */
+
+ gchar *available = g_new (gchar, colormap->size);
+ for (i = 0; i < colormap->size; i++)
+ available[i] = TRUE;
+
+ for (i=0; i<ncolors; i++)
+ {
+ if (!success[i])
{
- xcolor.pixel = 255 - private->next_color;
- color->pixel = xcolor.pixel;
- private->next_color += 1;
+ index = gdk_colormap_match_color (colormap,
+ &colors[i],
+ available);
+ if (index != -1)
+ {
+ colors[i] = colormap->colors[index];
+ private->info[index].ref_count++;
- XStoreColor (private->xdisplay, private->xcolormap, &xcolor);
- return_val = TRUE;
+ success[i] = TRUE;
+ nremaining--;
+ }
}
}
- else
- {
- available_init = 1;
+ g_free (available);
+ }
- while (1)
- {
- if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
- {
- color->pixel = xcolor.pixel;
- color->red = xcolor.red;
- color->green = xcolor.green;
- color->blue = xcolor.blue;
+ return (ncolors - nremaining);
+}
+
+static gint
+gdk_colormap_alloc_colors_shared (GdkColormap *colormap,
+ GdkColor *colors,
+ gint ncolors,
+ gboolean writeable,
+ gboolean best_match,
+ gboolean *success)
+{
+ GdkColormapPrivateX11 *private;
+ gint i, index;
+ gint nremaining = 0;
+ gint nfailed = 0;
- colormap->colors[color->pixel] = *color;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+ index = -1;
- return_val = TRUE;
- break;
- }
- else
- {
- if (available_init)
- {
- available_init = 0;
- for (i = 0; i < 256; i++)
- available[i] = TRUE;
- }
+ for (i=0; i<ncolors; i++)
+ {
+ if (!success[i])
+ {
+ if (gdk_colormap_alloc1 (colormap, &colors[i], &colors[i]))
+ success[i] = TRUE;
+ else
+ nremaining++;
+ }
+ }
- index = gdk_colormap_match_color (colormap, color, available);
+
+ if (nremaining > 0 && best_match)
+ {
+ gchar *available = g_new (gchar, colormap->size);
+ for (i = 0; i < colormap->size; i++)
+ available[i] = ((private->info[i].ref_count == 0) ||
+ !(private->info[i].flags & GDK_COLOR_WRITEABLE));
+ gdk_colormap_sync (colormap, FALSE);
+
+ while (nremaining > 0)
+ {
+ for (i=0; i<ncolors; i++)
+ {
+ if (!success[i])
+ {
+ index = gdk_colormap_match_color (colormap, &colors[i], available);
if (index != -1)
{
- available[index] = FALSE;
- xcolor.red = colormap->colors[index].red;
- xcolor.green = colormap->colors[index].green;
- xcolor.blue = colormap->colors[index].blue;
+ if (private->info[index].ref_count)
+ {
+ private->info[index].ref_count++;
+ colors[i] = colormap->colors[index];
+ success[i] = TRUE;
+ nremaining--;
+ }
+ else
+ {
+ if (gdk_colormap_alloc1 (colormap,
+ &colormap->colors[index],
+ &colors[i]))
+ {
+ success[i] = TRUE;
+ nremaining--;
+ break;
+ }
+ else
+ {
+ available[index] = FALSE;
+ }
+ }
}
else
{
- return_val = FALSE;
- break;
+ nfailed++;
+ nremaining--;
+ success[i] = 2; /* flag as permanent failure */
}
}
}
}
+ g_free (available);
+ }
+
+ /* Change back the values we flagged as permanent failures */
+ if (nfailed > 0)
+ {
+ for (i=0; i<ncolors; i++)
+ if (success[i] == 2)
+ success[i] = FALSE;
+ nremaining = nfailed;
+ }
+
+ return (ncolors - nremaining);
+}
+
+static gint
+gdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap,
+ GdkColor *colors,
+ gint ncolors,
+ gboolean writeable,
+ gboolean best_match,
+ gboolean *success)
+{
+ GdkColormapPrivateX11 *private;
+ GdkColor *lookup_color;
+ gint i;
+ gint nremaining = 0;
+
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+ /* Check for an exact match among previously allocated colors */
+
+ for (i=0; i<ncolors; i++)
+ {
+ if (!success[i])
+ {
+ lookup_color = g_hash_table_lookup (private->hash, &colors[i]);
+ if (lookup_color)
+ {
+ private->info[lookup_color->pixel].ref_count++;
+ colors[i].pixel = lookup_color->pixel;
+ success[i] = TRUE;
+ }
+ else
+ nremaining++;
+ }
+ }
+
+ /* If that failed, we try to allocate a new color, or approxmiate
+ * with what we can get if best_match is TRUE.
+ */
+ if (nremaining > 0)
+ {
+ if (private->private_val)
+ return gdk_colormap_alloc_colors_private (colormap, colors, ncolors, writeable, best_match, success);
+ else
+ return gdk_colormap_alloc_colors_shared (colormap, colors, ncolors, writeable, best_match, success);
+ }
+ else
+ return 0;
+}
+
+gint
+gdk_colormap_alloc_colors (GdkColormap *colormap,
+ GdkColor *colors,
+ gint ncolors,
+ gboolean writeable,
+ gboolean best_match,
+ gboolean *success)
+{
+ GdkColormapPrivateX11 *private;
+ GdkVisual *visual;
+ gint i;
+ gint nremaining = 0;
+ XColor xcolor;
+
+ g_return_val_if_fail (GDK_IS_COLORMAP (colormap), FALSE);
+ g_return_val_if_fail (colors != NULL, FALSE);
+
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+ for (i=0; i<ncolors; i++)
+ {
+ success[i] = FALSE;
+ }
+
+ switch (colormap->visual->type)
+ {
+ case GDK_VISUAL_PSEUDO_COLOR:
+ case GDK_VISUAL_GRAYSCALE:
+ if (writeable)
+ return gdk_colormap_alloc_colors_writeable (colormap, colors, ncolors,
+ writeable, best_match, success);
+ else
+ return gdk_colormap_alloc_colors_pseudocolor (colormap, colors, ncolors,
+ writeable, best_match, success);
break;
case GDK_VISUAL_DIRECT_COLOR:
- visual = private->visual;
- xcolor.pixel = (((xcolor.red >> (16 - visual->red_prec)) << visual->red_shift) +
- ((xcolor.green >> (16 - visual->green_prec)) << visual->green_shift) +
- ((xcolor.blue >> (16 - visual->blue_prec)) << visual->blue_shift));
- color->pixel = xcolor.pixel;
- return_val = TRUE;
+ case GDK_VISUAL_TRUE_COLOR:
+ visual = colormap->visual;
+
+ for (i=0; i<ncolors; i++)
+ {
+ colors[i].pixel = (((colors[i].red >> (16 - visual->red_prec)) << visual->red_shift) +
+ ((colors[i].green >> (16 - visual->green_prec)) << visual->green_shift) +
+ ((colors[i].blue >> (16 - visual->blue_prec)) << visual->blue_shift));
+ success[i] = TRUE;
+ }
break;
case GDK_VISUAL_STATIC_GRAY:
case GDK_VISUAL_STATIC_COLOR:
- case GDK_VISUAL_TRUE_COLOR:
- if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
+ for (i=0; i<ncolors; i++)
{
- color->pixel = xcolor.pixel;
- return_val = TRUE;
+ xcolor.red = colors[i].red;
+ xcolor.green = colors[i].green;
+ xcolor.blue = colors[i].blue;
+ xcolor.pixel = colors[i].pixel;
+ xcolor.flags = DoRed | DoGreen | DoBlue;
+
+ if (XAllocColor (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap, &xcolor))
+ {
+ colors[i].pixel = xcolor.pixel;
+ success[i] = TRUE;
+ }
+ else
+ nremaining++;
}
- else
- return_val = FALSE;
break;
}
+ return nremaining;
+}
- return return_val;
+/**
+ * gdk_colormap_query_color:
+ * @colormap: a #GdkColormap
+ * @pixel: pixel value in hardware display format
+ * @result: #GdkColor with red, green, blue fields initialized
+ *
+ * Locates the RGB color in @colormap corresponding to the given
+ * hardware pixel @pixel. @pixel must be a valid pixel in the
+ * colormap; it's a programmer error to call this function with a
+ * pixel which is not in the colormap. Hardware pixels are normally
+ * obtained from gdk_colormap_alloc_colors(), or from a #GdkImage. (A
+ * #GdkImage contains image data in hardware format, a #GdkPixbuf
+ * contains image data in a canonical 24-bit RGB format.)
+ *
+ * This function is rarely useful, it's used for example to
+ * implement the eyedropper feature in #GtkColorSelection.
+ *
+ **/
+void
+gdk_colormap_query_color (GdkColormap *colormap,
+ gulong pixel,
+ GdkColor *result)
+{
+ XColor xcolor;
+ GdkVisual *visual;
+ GdkColormapPrivateX11 *private;
+
+ g_return_if_fail (GDK_IS_COLORMAP (colormap));
+
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+ visual = gdk_colormap_get_visual (colormap);
+
+ switch (visual->type) {
+ case GDK_VISUAL_DIRECT_COLOR:
+ case GDK_VISUAL_TRUE_COLOR:
+ result->red = 65535. * (double)((pixel & visual->red_mask) >> visual->red_shift) / ((1 << visual->red_prec) - 1);
+ result->green = 65535. * (double)((pixel & visual->green_mask) >> visual->green_shift) / ((1 << visual->green_prec) - 1);
+ result->blue = 65535. * (double)((pixel & visual->blue_mask) >> visual->blue_shift) / ((1 << visual->blue_prec) - 1);
+ break;
+ case GDK_VISUAL_STATIC_GRAY:
+ case GDK_VISUAL_GRAYSCALE:
+ result->red = result->green = result->blue = 65535. * (double)pixel/((1<<visual->depth) - 1);
+ break;
+ case GDK_VISUAL_STATIC_COLOR:
+ xcolor.pixel = pixel;
+ XQueryColor (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap, &xcolor);
+ result->red = xcolor.red;
+ result->green = xcolor.green;
+ result->blue = xcolor.blue;
+ break;
+ case GDK_VISUAL_PSEUDO_COLOR:
+ g_return_if_fail (pixel < colormap->size);
+ result->red = colormap->colors[pixel].red;
+ result->green = colormap->colors[pixel].green;
+ result->blue = colormap->colors[pixel].blue;
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
}
-gint
+gboolean
gdk_color_change (GdkColormap *colormap,
GdkColor *color)
{
- GdkColormapPrivate *private;
+ GdkColormapPrivateX11 *private;
XColor xcolor;
- g_return_val_if_fail (colormap != NULL, FALSE);
+ g_return_val_if_fail (GDK_IS_COLORMAP (colormap), FALSE);
g_return_val_if_fail (color != NULL, FALSE);
xcolor.pixel = color->pixel;
xcolor.blue = color->blue;
xcolor.flags = DoRed | DoGreen | DoBlue;
- private = (GdkColormapPrivate*) colormap;
- XStoreColor (private->xdisplay, private->xcolormap, &xcolor);
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+ XStoreColor (GDK_SCREEN_XDISPLAY (colormap->screen), private->xcolormap, &xcolor);
return TRUE;
}
-gint
-gdk_color_equal (GdkColor *colora,
- GdkColor *colorb)
-{
- g_return_val_if_fail (colora != NULL, FALSE);
- g_return_val_if_fail (colorb != NULL, FALSE);
-
- return ((colora->red == colorb->red) &&
- (colora->green == colorb->green) &&
- (colora->blue == colorb->blue));
-}
-
+/* XXX: Do not use this function until it is fixed. An X Colormap
+ * is useless unless we also have the visual.
+ */
GdkColormap*
gdkx_colormap_get (Colormap xcolormap)
{
GdkColormap *colormap;
- GdkColormapPrivate *private;
- XColor xpalette[256];
- gint i;
+#if 0
+ GdkColormapPrivateX11 *private;
colormap = gdk_colormap_lookup (xcolormap);
if (colormap)
return colormap;
- if (xcolormap == DefaultColormap (gdk_display, gdk_screen))
+ if (xcolormap == DefaultColormap (gdk_display, _gdk_screen))
return gdk_colormap_get_system ();
- private = g_new (GdkColormapPrivate, 1);
- colormap = (GdkColormap*) private;
+ colormap = g_object_new (gdk_colormap_get_type (), NULL);
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
private->xdisplay = gdk_display;
private->xcolormap = xcolormap;
- private->visual = NULL;
+ colormap->visual = NULL;
private->private_val = TRUE;
- private->next_color = 0;
+#endif
+
+ /* To do the following safely, we would have to have some way of finding
+ * out what the size or visual of the given colormap is. It seems
+ * X doesn't allow this
+ */
+#if 0
for (i = 0; i < 256; i++)
{
xpalette[i].pixel = i;
colormap->colors[i].green = xpalette[i].green;
colormap->colors[i].blue = xpalette[i].blue;
}
+#endif
+
+ colormap->colors = NULL;
+ colormap->size = 0;
gdk_colormap_add (colormap);
max = 3 * (65536);
index = -1;
- for (i = 0; i < 256; i++)
+ for (i = 0; i < cmap->size; i++)
{
if ((!available) || (available && available[i]))
{
static void
gdk_colormap_add (GdkColormap *cmap)
{
- GdkColormapPrivate *private;
+ GdkColormapPrivateX11 *private;
if (!colormap_hash)
colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
- (GCompareFunc) gdk_colormap_cmp);
+ (GEqualFunc) gdk_colormap_equal);
- private = (GdkColormapPrivate*) cmap;
+ private = GDK_COLORMAP_PRIVATE_DATA (cmap);
g_hash_table_insert (colormap_hash, &private->xcolormap, cmap);
}
static void
gdk_colormap_remove (GdkColormap *cmap)
{
- GdkColormapPrivate *private;
+ GdkColormapPrivateX11 *private;
if (!colormap_hash)
colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
- (GCompareFunc) gdk_colormap_cmp);
+ (GEqualFunc) gdk_colormap_equal);
- private = (GdkColormapPrivate*) cmap;
+ private = GDK_COLORMAP_PRIVATE_DATA (cmap);
g_hash_table_remove (colormap_hash, &private->xcolormap);
}
return *cmap;
}
-static gint
-gdk_colormap_cmp (Colormap *a,
- Colormap *b)
+static gboolean
+gdk_colormap_equal (Colormap *a,
+ Colormap *b)
{
return (*a == *b);
}
+
+Display *
+gdk_x11_colormap_get_xdisplay (GdkColormap *colormap)
+{
+ GdkColormapPrivateX11 *private;
+
+ g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
+
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+ return private->xdisplay;
+}
+
+Colormap
+gdk_x11_colormap_get_xcolormap (GdkColormap *colormap)
+{
+ GdkColormapPrivateX11 *private;
+
+ g_return_val_if_fail (GDK_IS_COLORMAP (colormap), None);
+
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+
+ return private->xcolormap;
+}