1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * Insensitive pixmap building code by Eckehard Berns from GNOME Stock
5 * Copyright (C) 1997, 1998 Free Software Foundation
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkcontainer.h"
32 #include "gtkpixmap.h"
35 static void gtk_pixmap_class_init (GtkPixmapClass *klass);
36 static void gtk_pixmap_init (GtkPixmap *pixmap);
37 static gint gtk_pixmap_expose (GtkWidget *widget,
38 GdkEventExpose *event);
39 static void gtk_pixmap_finalize (GObject *object);
40 static void build_insensitive_pixmap (GtkPixmap *gtkpixmap);
42 static GtkWidgetClass *parent_class;
45 gtk_pixmap_get_type (void)
47 static GtkType pixmap_type = 0;
51 static const GtkTypeInfo pixmap_info =
55 sizeof (GtkPixmapClass),
56 (GtkClassInitFunc) gtk_pixmap_class_init,
57 (GtkObjectInitFunc) gtk_pixmap_init,
58 /* reserved_1 */ NULL,
59 /* reserved_2 */ NULL,
60 (GtkClassInitFunc) NULL,
63 pixmap_type = gtk_type_unique (GTK_TYPE_MISC, &pixmap_info);
70 gtk_pixmap_class_init (GtkPixmapClass *class)
72 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
73 GtkObjectClass *object_class;
74 GtkWidgetClass *widget_class;
76 object_class = (GtkObjectClass*) class;
77 widget_class = (GtkWidgetClass*) class;
78 parent_class = gtk_type_class (gtk_widget_get_type ());
80 gobject_class->finalize = gtk_pixmap_finalize;
82 widget_class->expose_event = gtk_pixmap_expose;
86 gtk_pixmap_init (GtkPixmap *pixmap)
88 GTK_WIDGET_SET_FLAGS (pixmap, GTK_NO_WINDOW);
90 pixmap->pixmap = NULL;
95 gtk_pixmap_new (GdkPixmap *val,
100 g_return_val_if_fail (val != NULL, NULL);
102 pixmap = gtk_type_new (gtk_pixmap_get_type ());
104 pixmap->build_insensitive = TRUE;
105 gtk_pixmap_set (pixmap, val, mask);
107 return GTK_WIDGET (pixmap);
111 gtk_pixmap_finalize (GObject *object)
113 gtk_pixmap_set (GTK_PIXMAP (object), NULL, NULL);
115 G_OBJECT_CLASS (parent_class)->finalize (object);
119 gtk_pixmap_set (GtkPixmap *pixmap,
128 g_return_if_fail (GTK_IS_PIXMAP (pixmap));
130 if (pixmap->pixmap != val)
132 oldwidth = GTK_WIDGET (pixmap)->requisition.width;
133 oldheight = GTK_WIDGET (pixmap)->requisition.height;
135 gdk_pixmap_unref (pixmap->pixmap);
136 if (pixmap->pixmap_insensitive)
137 gdk_pixmap_unref (pixmap->pixmap_insensitive);
138 pixmap->pixmap = val;
139 pixmap->pixmap_insensitive = NULL;
142 gdk_pixmap_ref (pixmap->pixmap);
143 gdk_window_get_size (pixmap->pixmap, &width, &height);
144 GTK_WIDGET (pixmap)->requisition.width =
145 width + GTK_MISC (pixmap)->xpad * 2;
146 GTK_WIDGET (pixmap)->requisition.height =
147 height + GTK_MISC (pixmap)->ypad * 2;
151 GTK_WIDGET (pixmap)->requisition.width = 0;
152 GTK_WIDGET (pixmap)->requisition.height = 0;
154 if (GTK_WIDGET_VISIBLE (pixmap))
156 if ((GTK_WIDGET (pixmap)->requisition.width != oldwidth) ||
157 (GTK_WIDGET (pixmap)->requisition.height != oldheight))
158 gtk_widget_queue_resize (GTK_WIDGET (pixmap));
160 gtk_widget_queue_clear (GTK_WIDGET (pixmap));
164 if (pixmap->mask != mask)
167 gdk_bitmap_unref (pixmap->mask);
170 gdk_bitmap_ref (pixmap->mask);
175 gtk_pixmap_get (GtkPixmap *pixmap,
179 g_return_if_fail (GTK_IS_PIXMAP (pixmap));
182 *val = pixmap->pixmap;
184 *mask = pixmap->mask;
188 gtk_pixmap_expose (GtkWidget *widget,
189 GdkEventExpose *event)
196 g_return_val_if_fail (GTK_IS_PIXMAP (widget), FALSE);
197 g_return_val_if_fail (event != NULL, FALSE);
199 if (GTK_WIDGET_DRAWABLE (widget))
201 pixmap = GTK_PIXMAP (widget);
202 misc = GTK_MISC (widget);
204 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
205 xalign = misc->xalign;
207 xalign = 1.0 - misc->xalign;
209 x = floor (widget->allocation.x + misc->xpad
210 + ((widget->allocation.width - widget->requisition.width) * xalign)
212 y = floor (widget->allocation.y + misc->ypad
213 + ((widget->allocation.height - widget->requisition.height) * misc->yalign)
218 gdk_gc_set_clip_mask (widget->style->black_gc, pixmap->mask);
219 gdk_gc_set_clip_origin (widget->style->black_gc, x, y);
222 if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE
223 && pixmap->build_insensitive)
225 if (!pixmap->pixmap_insensitive)
226 build_insensitive_pixmap (pixmap);
227 gdk_draw_pixmap (widget->window,
228 widget->style->black_gc,
229 pixmap->pixmap_insensitive,
234 gdk_draw_pixmap (widget->window,
235 widget->style->black_gc,
242 gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
243 gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
250 gtk_pixmap_set_build_insensitive (GtkPixmap *pixmap, gboolean build)
252 g_return_if_fail (GTK_IS_PIXMAP (pixmap));
254 pixmap->build_insensitive = build;
256 if (GTK_WIDGET_VISIBLE (pixmap))
258 gtk_widget_queue_clear (GTK_WIDGET (pixmap));
263 build_insensitive_pixmap (GtkPixmap *gtkpixmap)
265 GdkPixmap *pixmap = gtkpixmap->pixmap;
266 GdkPixmap *insensitive;
271 gdk_window_get_size (pixmap, &w, &h);
273 pixbuf = gdk_pixbuf_get_from_drawable (NULL,
275 gtk_widget_get_colormap (GTK_WIDGET(gtkpixmap)),
280 stated = gdk_pixbuf_copy (pixbuf);
282 gdk_pixbuf_saturate_and_pixelate (pixbuf, stated,
285 g_object_unref (G_OBJECT (pixbuf));
288 insensitive = gdk_pixmap_new (GTK_WIDGET (gtkpixmap)->window, w, h, -1);
290 gdk_pixbuf_render_to_drawable (stated,
292 GTK_WIDGET (gtkpixmap)->style->white_gc,
296 GDK_RGB_DITHER_NORMAL,
299 gtkpixmap->pixmap_insensitive = insensitive;
301 g_object_unref (G_OBJECT (stated));