diff --git a/gdk/gdkcolor.c b/gdk/gdkcolor.c index e06bc400a8..2398e7a6a3 100644 --- a/gdk/gdkcolor.c +++ b/gdk/gdkcolor.c @@ -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 diff --git a/gdk/gdkcolor.h b/gdk/gdkcolor.h index d2af35266a..f7ab3ff5da 100644 --- a/gdk/gdkcolor.h +++ b/gdk/gdkcolor.h @@ -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); diff --git a/gdk/gdkprivate.h b/gdk/gdkprivate.h index 24fa6891fb..e5b67ac64c 100644 --- a/gdk/gdkprivate.h +++ b/gdk/gdkprivate.h @@ -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; diff --git a/gdk/x11/gdkcolor-x11.c b/gdk/x11/gdkcolor-x11.c index 156933cec4..b22cf4bd51 100644 --- a/gdk/x11/gdkcolor-x11.c +++ b/gdk/x11/gdkcolor-x11.c @@ -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,35 +276,34 @@ 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); private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash, (GCompareFunc) gdk_color_equal); - + gdk_colormap_sync (colormap, TRUE); } @@ -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; ibase.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; ipixel; @@ -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); } diff --git a/gdk/x11/gdkpixmap-x11.c b/gdk/x11/gdkpixmap-x11.c index 8920003f8e..8511b1245e 100644 --- a/gdk/x11/gdkpixmap-x11.c +++ b/gdk/x11/gdkpixmap-x11.c @@ -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) diff --git a/gdk/x11/gdkprivate-x11.h b/gdk/x11/gdkprivate-x11.h index 5c84355fd7..990d1f4fc7 100644 --- a/gdk/x11/gdkprivate-x11.h +++ b/gdk/x11/gdkprivate-x11.h @@ -34,6 +34,8 @@ #include #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); diff --git a/gdk/x11/gdkx.h b/gdk/x11/gdkx.h index e00d2f0335..744cae3012 100644 --- a/gdk/x11/gdkx.h +++ b/gdk/x11/gdkx.h @@ -36,12 +36,12 @@ #include #include -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) diff --git a/gtk/gtk-boxed.defs b/gtk/gtk-boxed.defs index 4bfa401436..37a685d009 100644 --- a/gtk/gtk-boxed.defs +++ b/gtk/gtk-boxed.defs @@ -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)