* 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
+ * 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.
*/
/*
- * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS
+ * 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 "gdkx.h"
+#include "gdkprivate-x11.h"
-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);
+#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 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->windowing_data = private;
+
+ private->xdisplay = gdk_display;
+ 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 (private->xdisplay, 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,
gboolean private_cmap)
{
GdkColormap *colormap;
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
Visual *xvisual;
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 (GdkColormapPrivateX, 1);
- colormap = (GdkColormap*) private;
-
- private->xdisplay = gdk_display;
- private->base.visual = visual;
- private->base.ref_count = 1;
+ colormap = g_object_new (gdk_colormap_get_type (), NULL);
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
- private->hash = NULL;
- private->last_sync_time = 0;
- private->info = NULL;
+ colormap->visual = visual;
xvisual = ((GdkVisualPrivate*) visual)->xvisual;
colormap->size = visual->colormap_size;
- colormap->colors = NULL;
switch (visual->type)
{
colormap->colors = g_new (GdkColor, colormap->size);
private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
- (GCompareFunc) gdk_color_equal);
+ (GEqualFunc) gdk_color_equal);
private->private_val = private_cmap;
private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
case GDK_VISUAL_STATIC_GRAY:
case GDK_VISUAL_STATIC_COLOR:
+ private->private_val = FALSE;
+ private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
+ 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,
return colormap;
}
-void
-_gdk_colormap_real_destroy (GdkColormap *colormap)
-{
- GdkColormapPrivateX *private = (GdkColormapPrivateX*) colormap;
-
- gdk_colormap_remove (colormap);
- XFreeColormap (private->xdisplay, private->xcolormap);
-
- if (private->hash)
- g_hash_table_destroy (private->hash);
-
- g_free (private->info);
- g_free (colormap->colors);
- g_free (colormap);
-}
-
#define MIN_SYNC_TIME 2
-void
+static void
gdk_colormap_sync (GdkColormap *colormap,
gboolean force)
{
time_t current_time;
- GdkColormapPrivateX *private = (GdkColormapPrivateX *)colormap;
+ GdkColormapPrivateX11 *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
XColor *xpalette;
gint nlookup;
gint i;
- g_return_if_fail (colormap != NULL);
+ g_return_if_fail (GDK_IS_COLORMAP (colormap));
current_time = time (NULL);
if (!force && ((current_time - private->last_sync_time) < MIN_SYNC_TIME))
for (i = 0; i < colormap->size; i++)
{
- if (private->info[i].ref_count == 0)
+ if (!private->info || private->info[i].ref_count == 0)
{
xpalette[nlookup].pixel = i;
xpalette[nlookup].red = 0;
gdk_colormap_get_system (void)
{
static GdkColormap *colormap = NULL;
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
if (!colormap)
{
- private = g_new (GdkColormapPrivateX, 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->base.visual = gdk_visual_get_system ();
+ colormap->visual = gdk_visual_get_system ();
private->private_val = FALSE;
- private->base.ref_count = 1;
private->hash = NULL;
private->last_sync_time = 0;
private->info = NULL;
colormap->colors = NULL;
- colormap->size = private->base.visual->colormap_size;
+ colormap->size = colormap->visual->colormap_size;
- if ((private->base.visual->type == GDK_VISUAL_GRAYSCALE) ||
- (private->base.visual->type == GDK_VISUAL_PSEUDO_COLOR))
+ switch (colormap->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,
- (GCompareFunc) gdk_color_equal);
-
+ (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);
gdk_colormap_change (GdkColormap *colormap,
gint ncolors)
{
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
GdkVisual *visual;
XColor *palette;
gint shift;
int size;
int i;
- g_return_if_fail (colormap != NULL);
+ g_return_if_fail (GDK_IS_COLORMAP (colormap));
palette = g_new (XColor, ncolors);
- private = (GdkColormapPrivateX*) colormap;
- switch (private->base.visual->type)
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
+ switch (colormap->visual->type)
{
case GDK_VISUAL_GRAYSCALE:
case GDK_VISUAL_PSEUDO_COLOR:
break;
case GDK_VISUAL_DIRECT_COLOR:
- visual = private->base.visual;
+ visual = colormap->visual;
shift = visual->red_shift;
max_colors = 1 << visual->red_prec;
gulong *pixels,
gint npixels)
{
- GdkColormapPrivateX *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 = (GdkColormapPrivateX*) colormap;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
return_val = XAllocColorCells (private->xdisplay, private->xcolormap,
contiguous, planes, nplanes, pixels, npixels);
gint in_npixels,
gulong planes)
{
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
gulong *pixels;
gint npixels = 0;
gint i;
- g_return_if_fail (colormap != NULL);
+ g_return_if_fail (GDK_IS_COLORMAP (colormap));
g_return_if_fail (in_pixels != NULL);
- private = (GdkColormapPrivateX*) colormap;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
- if ((private->base.visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
- (private->base.visual->type != GDK_VISUAL_GRAYSCALE))
+ if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
+ (colormap->visual->type != GDK_VISUAL_GRAYSCALE))
return;
pixels = g_new (gulong, in_npixels);
GdkColor *colors,
gint ncolors)
{
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
gulong *pixels;
gint npixels = 0;
gint i;
- g_return_if_fail (colormap != NULL);
+ g_return_if_fail (GDK_IS_COLORMAP (colormap));
g_return_if_fail (colors != NULL);
- private = (GdkColormapPrivateX*) colormap;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
- if ((private->base.visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
- (private->base.visual->type != GDK_VISUAL_GRAYSCALE))
+ if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
+ (colormap->visual->type != GDK_VISUAL_GRAYSCALE))
return;
pixels = g_new (gulong, ncolors);
GdkColor *color,
GdkColor *ret)
{
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
XColor xcolor;
- private = (GdkColormapPrivateX*) colormap;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
xcolor.red = color->red;
xcolor.green = color->green;
gboolean best_match,
gboolean *success)
{
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
gulong *pixels;
Status status;
gint i, index;
- private = (GdkColormapPrivateX*) colormap;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
if (private->private_val)
{
gboolean best_match,
gboolean *success)
{
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
gint i, index;
XColor *store = g_new (XColor, ncolors);
gint nstore = 0;
gint nremaining = 0;
- private = (GdkColormapPrivateX*) colormap;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
index = -1;
/* First, store the colors we have room for */
gboolean best_match,
gboolean *success)
{
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
gint i, index;
gint nremaining = 0;
gint nfailed = 0;
- private = (GdkColormapPrivateX*) colormap;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
index = -1;
for (i=0; i<ncolors; i++)
gboolean best_match,
gboolean *success)
{
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
GdkColor *lookup_color;
gint i;
gint nremaining = 0;
- private = (GdkColormapPrivateX*) colormap;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
/* Check for an exact match among previously allocated colors */
gboolean best_match,
gboolean *success)
{
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
GdkVisual *visual;
gint i;
gint nremaining = 0;
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 (colors != NULL, FALSE);
- private = (GdkColormapPrivateX*) colormap;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
for (i=0; i<ncolors; i++)
{
success[i] = FALSE;
}
- switch (private->base.visual->type)
+ switch (colormap->visual->type)
{
case GDK_VISUAL_PSEUDO_COLOR:
case GDK_VISUAL_GRAYSCALE:
case GDK_VISUAL_DIRECT_COLOR:
case GDK_VISUAL_TRUE_COLOR:
- visual = private->base.visual;
+ visual = colormap->visual;
for (i=0; i<ncolors; i++)
{
return nremaining;
}
+void
+gdk_colormap_query_color (GdkColormap *colormap,
+ gulong pixel,
+ GdkColor *result)
+{
+ XColor xcolor;
+ GdkVisual *visual;
+
+ g_return_if_fail (GDK_IS_COLORMAP (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_DISPLAY (), GDK_COLORMAP_XCOLORMAP (colormap), &xcolor);
+ result->red = xcolor.red;
+ result->green = xcolor.green;
+ result->blue = xcolor.blue;
+ break;
+ case GDK_VISUAL_PSEUDO_COLOR:
+ 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;
+ }
+}
+
gboolean
gdk_color_change (GdkColormap *colormap,
GdkColor *color)
{
- GdkColormapPrivateX *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 = (GdkColormapPrivateX*) colormap;
+ private = GDK_COLORMAP_PRIVATE_DATA (colormap);
XStoreColor (private->xdisplay, private->xcolormap, &xcolor);
return TRUE;
gdkx_colormap_get (Colormap xcolormap)
{
GdkColormap *colormap;
- GdkColormapPrivateX *private;
+ GdkColormapPrivateX11 *private;
colormap = gdk_colormap_lookup (xcolormap);
if (colormap)
if (xcolormap == DefaultColormap (gdk_display, gdk_screen))
return gdk_colormap_get_system ();
- private = g_new (GdkColormapPrivateX, 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->base.visual = NULL;
+ colormap->visual = NULL;
private->private_val = TRUE;
/* To do the following safely, we would have to have some way of finding
static void
gdk_colormap_add (GdkColormap *cmap)
{
- GdkColormapPrivateX *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 = (GdkColormapPrivateX*)cmap;
+ private = GDK_COLORMAP_PRIVATE_DATA (cmap);
g_hash_table_insert (colormap_hash, &private->xcolormap, cmap);
}
static void
gdk_colormap_remove (GdkColormap *cmap)
{
- GdkColormapPrivateX *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 = (GdkColormapPrivateX *)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);
}