colormap finished and working nicely

This commit is contained in:
Havoc Pennington
2000-05-18 18:43:27 +00:00
parent 789b7e9023
commit 930ff89929
8 changed files with 184 additions and 125 deletions

View File

@@ -32,37 +32,23 @@
GdkColormap*
gdk_colormap_ref (GdkColormap *cmap)
{
GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
g_object_ref (G_OBJECT (cmap));
g_return_val_if_fail (cmap != NULL, NULL);
private->ref_count += 1;
return cmap;
}
void
gdk_colormap_unref (GdkColormap *cmap)
{
GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
g_return_if_fail (cmap != NULL);
g_return_if_fail (private->ref_count > 0);
private->ref_count -= 1;
if (private->ref_count == 0)
_gdk_colormap_real_destroy (cmap);
g_object_unref (G_OBJECT (cmap));
}
GdkVisual *
gdk_colormap_get_visual (GdkColormap *colormap)
{
GdkColormapPrivate *private;
g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
g_return_val_if_fail (colormap != NULL, NULL);
private = (GdkColormapPrivate *)colormap;
return private->visual;
return colormap->visual;
}
void

View File

@@ -24,12 +24,38 @@ struct _GdkColor
/* The colormap type.
*/
typedef struct _GdkColormapClass GdkColormapClass;
#define GDK_TYPE_COLORMAP (gdk_colormap_get_type ())
#define GDK_COLORMAP(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_COLORMAP, GdkColormap))
#define GDK_COLORMAP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_COLORMAP, GdkColormapClass))
#define GDK_IS_COLORMAP(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_COLORMAP))
#define GDK_IS_COLORMAP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_COLORMAP))
#define GDK_COLORMAP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_COLORMAP, GdkColormapClass))
struct _GdkColormap
{
GObject parent_instance;
/*< public >*/
gint size;
GdkColor *colors;
/*< private >*/
GdkVisual *visual;
gpointer windowing_data;
};
struct _GdkColormapClass
{
GObjectClass parent_class;
};
GType gdk_colormap_get_type (void);
GdkColormap* gdk_colormap_new (GdkVisual *visual,
gboolean allocate);

View File

@@ -54,7 +54,6 @@ extern "C" {
typedef struct _GdkImageClass GdkImageClass;
typedef struct _GdkImagePrivate GdkImagePrivate;
typedef struct _GdkGCPrivate GdkGCPrivate;
typedef struct _GdkColormapPrivate GdkColormapPrivate;
typedef struct _GdkColorInfo GdkColorInfo;
typedef struct _GdkFontPrivate GdkFontPrivate;
typedef struct _GdkEventFilter GdkEventFilter;
@@ -110,14 +109,6 @@ struct _GdkColorInfo
guint ref_count;
};
struct _GdkColormapPrivate
{
GdkColormap colormap;
GdkVisual *visual;
guint ref_count;
};
struct _GdkEventFilter {
GdkFilterFunc function;
gpointer data;

View File

@@ -38,36 +38,113 @@ static guint gdk_colormap_hash (Colormap *cmap);
static gint gdk_colormap_cmp (Colormap *a,
Colormap *b);
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);
}
return object_type;
}
static void
gdk_colormap_init (GdkColormap *colormap)
{
GdkColormapPrivateData *private;
private = g_new (GdkColormapPrivateData, 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 (G_TYPE_OBJECT);
object_class->finalize = gdk_colormap_finalize;
}
static void
gdk_colormap_finalize (GObject *object)
{
GdkColormap *colormap = GDK_COLORMAP (object);
GdkColormapPrivateData *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;
GdkColormapPrivateData *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;
colormap = GDK_COLORMAP (g_type_create_instance (gdk_colormap_get_type ()));
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
private->xdisplay = gdk_display;
private->base.visual = visual;
private->base.ref_count = 1;
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)
{
@@ -145,22 +222,6 @@ gdk_colormap_new (GdkVisual *visual,
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
@@ -168,12 +229,12 @@ gdk_colormap_sync (GdkColormap *colormap,
gboolean force)
{
time_t current_time;
GdkColormapPrivateX *private = (GdkColormapPrivateX *)colormap;
GdkColormapPrivateData *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))
@@ -215,28 +276,27 @@ GdkColormap*
gdk_colormap_get_system (void)
{
static GdkColormap *colormap = NULL;
GdkColormapPrivateX *private;
GdkColormapPrivateData *private;
if (!colormap)
{
private = g_new (GdkColormapPrivateX, 1);
colormap = (GdkColormap*) private;
colormap = GDK_COLORMAP (g_type_create_instance (gdk_colormap_get_type ()));
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))
if ((colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
(colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
{
private->info = g_new0 (GdkColorInfo, colormap->size);
colormap->colors = g_new (GdkColor, colormap->size);
@@ -263,7 +323,7 @@ void
gdk_colormap_change (GdkColormap *colormap,
gint ncolors)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *private;
GdkVisual *visual;
XColor *palette;
gint shift;
@@ -271,12 +331,12 @@ gdk_colormap_change (GdkColormap *colormap,
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:
@@ -293,7 +353,7 @@ gdk_colormap_change (GdkColormap *colormap,
break;
case GDK_VISUAL_DIRECT_COLOR:
visual = private->base.visual;
visual = colormap->visual;
shift = visual->red_shift;
max_colors = 1 << visual->red_prec;
@@ -350,13 +410,13 @@ gdk_colors_alloc (GdkColormap *colormap,
gulong *pixels,
gint npixels)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *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);
@@ -408,18 +468,18 @@ gdk_colors_free (GdkColormap *colormap,
gint in_npixels,
gulong planes)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *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);
@@ -456,18 +516,18 @@ gdk_colormap_free_colors (GdkColormap *colormap,
GdkColor *colors,
gint ncolors)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *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);
@@ -509,10 +569,10 @@ gdk_colormap_alloc1 (GdkColormap *colormap,
GdkColor *color,
GdkColor *ret)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *private;
XColor xcolor;
private = (GdkColormapPrivateX*) colormap;
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
xcolor.red = color->red;
xcolor.green = color->green;
@@ -561,12 +621,12 @@ gdk_colormap_alloc_colors_writeable (GdkColormap *colormap,
gboolean best_match,
gboolean *success)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *private;
gulong *pixels;
Status status;
gint i, index;
private = (GdkColormapPrivateX*) colormap;
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
if (private->private_val)
{
@@ -619,13 +679,13 @@ gdk_colormap_alloc_colors_private (GdkColormap *colormap,
gboolean best_match,
gboolean *success)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *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 */
@@ -698,12 +758,12 @@ gdk_colormap_alloc_colors_shared (GdkColormap *colormap,
gboolean best_match,
gboolean *success)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *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++)
@@ -790,12 +850,12 @@ gdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap,
gboolean best_match,
gboolean *success)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *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 */
@@ -837,23 +897,23 @@ gdk_colormap_alloc_colors (GdkColormap *colormap,
gboolean best_match,
gboolean *success)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *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:
@@ -867,7 +927,7 @@ gdk_colormap_alloc_colors (GdkColormap *colormap,
case GDK_VISUAL_DIRECT_COLOR:
case GDK_VISUAL_TRUE_COLOR:
visual = private->base.visual;
visual = colormap->visual;
for (i=0; i<ncolors; i++)
{
@@ -905,10 +965,10 @@ gboolean
gdk_color_change (GdkColormap *colormap,
GdkColor *color)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *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;
@@ -917,7 +977,7 @@ gdk_color_change (GdkColormap *colormap,
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;
@@ -930,7 +990,7 @@ GdkColormap*
gdkx_colormap_get (Colormap xcolormap)
{
GdkColormap *colormap;
GdkColormapPrivateX *private;
GdkColormapPrivateData *private;
colormap = gdk_colormap_lookup (xcolormap);
if (colormap)
@@ -939,12 +999,12 @@ gdkx_colormap_get (Colormap xcolormap)
if (xcolormap == DefaultColormap (gdk_display, gdk_screen))
return gdk_colormap_get_system ();
private = g_new (GdkColormapPrivateX, 1);
colormap = (GdkColormap*) private;
colormap = GDK_COLORMAP (g_type_create_instance (gdk_colormap_get_type ()));
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
@@ -1035,13 +1095,13 @@ gdk_colormap_lookup (Colormap xcolormap)
static void
gdk_colormap_add (GdkColormap *cmap)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *private;
if (!colormap_hash)
colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
(GCompareFunc) gdk_colormap_cmp);
private = (GdkColormapPrivateX*)cmap;
private = GDK_COLORMAP_PRIVATE_DATA (cmap);
g_hash_table_insert (colormap_hash, &private->xcolormap, cmap);
}
@@ -1049,13 +1109,13 @@ gdk_colormap_add (GdkColormap *cmap)
static void
gdk_colormap_remove (GdkColormap *cmap)
{
GdkColormapPrivateX *private;
GdkColormapPrivateData *private;
if (!colormap_hash)
colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
(GCompareFunc) gdk_colormap_cmp);
private = (GdkColormapPrivateX *)cmap;
private = GDK_COLORMAP_PRIVATE_DATA (cmap);
g_hash_table_remove (colormap_hash, &private->xcolormap);
}

View File

@@ -537,7 +537,7 @@ _gdk_pixmap_create_from_xpm (GdkWindow *window,
visual = gdk_drawable_get_visual (window);
}
else
visual = ((GdkColormapPrivate *)colormap)->visual;
visual = colormap->visual;
buffer = (*get_buf) (op_header, handle);
if (buffer == NULL)

View File

@@ -34,6 +34,8 @@
#include <gdk/gdkprivate.h>
#include "gdkx.h"
#define GDK_COLORMAP_PRIVATE_DATA(cmap) ((GdkColormapPrivateData *) GDK_COLORMAP (cmap)->windowing_data)
void gdk_xid_table_insert (XID *xid,
gpointer data);
void gdk_xid_table_remove (XID xid);

View File

@@ -36,12 +36,12 @@
#include <gdk/x11/gdkwindow-x11.h>
#include <gdk/x11/gdkpixmap-x11.h>
typedef struct _GdkGCXData GdkGCXData;
typedef struct _GdkColormapPrivateX GdkColormapPrivateX;
typedef struct _GdkCursorPrivate GdkCursorPrivate;
typedef struct _GdkFontPrivateX GdkFontPrivateX;
typedef struct _GdkImagePrivateX GdkImagePrivateX;
typedef struct _GdkVisualPrivate GdkVisualPrivate;
typedef struct _GdkGCXData GdkGCXData;
typedef struct _GdkColormapPrivateData GdkColormapPrivateData;
typedef struct _GdkCursorPrivate GdkCursorPrivate;
typedef struct _GdkFontPrivateX GdkFontPrivateX;
typedef struct _GdkImagePrivateX GdkImagePrivateX;
typedef struct _GdkVisualPrivate GdkVisualPrivate;
#ifdef USE_XIM
typedef struct _GdkICPrivate GdkICPrivate;
@@ -82,10 +82,8 @@ struct _GdkVisualPrivate
Visual *xvisual;
};
struct _GdkColormapPrivateX
struct _GdkColormapPrivateData
{
GdkColormapPrivate base;
Colormap xcolormap;
Display *xdisplay;
gint private_val;
@@ -128,8 +126,8 @@ struct _GdkICPrivate
#define GDK_IMAGE_XDISPLAY(image) (((GdkImagePrivate*) image)->xdisplay)
#define GDK_IMAGE_XIMAGE(image) (((GdkImagePrivate*) image)->ximage)
#define GDK_GC_XDISPLAY(gc) (GDK_GC_XDATA(gc)->xdisplay)
#define GDK_COLORMAP_XDISPLAY(cmap) (((GdkColormapPrivateX *)cmap)->xdisplay)
#define GDK_COLORMAP_XCOLORMAP(cmap) (((GdkColormapPrivateX *)cmap)->xcolormap)
#define GDK_COLORMAP_XDISPLAY(cmap) (((GdkColormapPrivateData *)GDK_COLORMAP (cmap)->windowing_data)->xdisplay)
#define GDK_COLORMAP_XCOLORMAP(cmap) (((GdkColormapPrivateData *)GDK_COLORMAP (cmap)->windowing_data)->xcolormap)
#define GDK_VISUAL_XVISUAL(vis) (((GdkVisualPrivate*) vis)->xvisual)
#define GDK_FONT_XDISPLAY(font) (((GdkFontPrivate*) font)->xdisplay)
#define GDK_FONT_XFONT(font) (((GdkFontPrivateX *)font)->xfont)

View File

@@ -29,10 +29,6 @@
; gdk_point_copy
; gdk_point_destroy)
(define-boxed GdkColormap
gdk_colormap_ref
gdk_colormap_unref)
(define-boxed GdkVisual
gdk_visual_ref
gdk_visual_unref)