]> Pileus Git - ~andy/gtk/blob - gtk/gtkpixmap.c
Include "config.h" instead of <config.h> Command used: find -name
[~andy/gtk] / gtk / gtkpixmap.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * Insensitive pixmap building code by Eckehard Berns from GNOME Stock
5  * Copyright (C) 1997, 1998 Free Software Foundation
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /*
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/. 
28  */
29
30 #undef GDK_DISABLE_DEPRECATED
31 #undef GTK_DISABLE_DEPRECATED
32
33 #include "config.h"
34 #include <math.h>
35 #include "gtkcontainer.h"
36 #include "gtkpixmap.h"
37 #include "gtkintl.h"
38
39 #define GTK_DISABLE_DEPRECATED
40 #include "gtkalias.h"
41
42
43 static gint gtk_pixmap_expose     (GtkWidget       *widget,
44                                    GdkEventExpose  *event);
45 static void gtk_pixmap_finalize   (GObject         *object);
46 static void build_insensitive_pixmap (GtkPixmap *gtkpixmap);
47
48 G_DEFINE_TYPE (GtkPixmap, gtk_pixmap, GTK_TYPE_MISC)
49
50 static void
51 gtk_pixmap_class_init (GtkPixmapClass *class)
52 {
53   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
54   GtkWidgetClass *widget_class;
55
56   widget_class = (GtkWidgetClass*) class;
57
58   gobject_class->finalize = gtk_pixmap_finalize;
59
60   widget_class->expose_event = gtk_pixmap_expose;
61 }
62
63 static void
64 gtk_pixmap_init (GtkPixmap *pixmap)
65 {
66   GTK_WIDGET_SET_FLAGS (pixmap, GTK_NO_WINDOW);
67
68   pixmap->pixmap = NULL;
69   pixmap->mask = NULL;
70 }
71
72 GtkWidget*
73 gtk_pixmap_new (GdkPixmap *val,
74                 GdkBitmap *mask)
75 {
76   GtkPixmap *pixmap;
77    
78   g_return_val_if_fail (val != NULL, NULL);
79   
80   pixmap = gtk_type_new (gtk_pixmap_get_type ());
81   
82   pixmap->build_insensitive = TRUE;
83   gtk_pixmap_set (pixmap, val, mask);
84   
85   return GTK_WIDGET (pixmap);
86 }
87
88 static void
89 gtk_pixmap_finalize (GObject *object)
90 {
91   gtk_pixmap_set (GTK_PIXMAP (object), NULL, NULL);
92
93   G_OBJECT_CLASS (gtk_pixmap_parent_class)->finalize (object);
94 }
95
96 void
97 gtk_pixmap_set (GtkPixmap *pixmap,
98                 GdkPixmap *val,
99                 GdkBitmap *mask)
100 {
101   gint width;
102   gint height;
103   gint oldwidth;
104   gint oldheight;
105
106   g_return_if_fail (GTK_IS_PIXMAP (pixmap));
107   if(GDK_IS_DRAWABLE(val))
108     g_return_if_fail (gdk_colormap_get_visual (gtk_widget_get_colormap (GTK_WIDGET (pixmap)))->depth == gdk_drawable_get_depth (GDK_DRAWABLE (val)));
109
110   if (pixmap->pixmap != val)
111     {
112       oldwidth = GTK_WIDGET (pixmap)->requisition.width;
113       oldheight = GTK_WIDGET (pixmap)->requisition.height;
114       if (pixmap->pixmap)
115         gdk_pixmap_unref (pixmap->pixmap);
116       if (pixmap->pixmap_insensitive)
117         gdk_pixmap_unref (pixmap->pixmap_insensitive);
118       pixmap->pixmap = val;
119       pixmap->pixmap_insensitive = NULL;
120       if (pixmap->pixmap)
121         {
122           gdk_pixmap_ref (pixmap->pixmap);
123           gdk_window_get_size (pixmap->pixmap, &width, &height);
124           GTK_WIDGET (pixmap)->requisition.width =
125             width + GTK_MISC (pixmap)->xpad * 2;
126           GTK_WIDGET (pixmap)->requisition.height =
127             height + GTK_MISC (pixmap)->ypad * 2;
128         }
129       else
130         {
131           GTK_WIDGET (pixmap)->requisition.width = 0;
132           GTK_WIDGET (pixmap)->requisition.height = 0;
133         }
134       if (GTK_WIDGET_VISIBLE (pixmap))
135         {
136           if ((GTK_WIDGET (pixmap)->requisition.width != oldwidth) ||
137               (GTK_WIDGET (pixmap)->requisition.height != oldheight))
138             gtk_widget_queue_resize (GTK_WIDGET (pixmap));
139           else
140             gtk_widget_queue_clear (GTK_WIDGET (pixmap));
141         }
142     }
143
144   if (pixmap->mask != mask)
145     {
146       if (pixmap->mask)
147         gdk_bitmap_unref (pixmap->mask);
148       pixmap->mask = mask;
149       if (pixmap->mask)
150         gdk_bitmap_ref (pixmap->mask);
151     }
152 }
153
154 void
155 gtk_pixmap_get (GtkPixmap  *pixmap,
156                 GdkPixmap **val,
157                 GdkBitmap **mask)
158 {
159   g_return_if_fail (GTK_IS_PIXMAP (pixmap));
160
161   if (val)
162     *val = pixmap->pixmap;
163   if (mask)
164     *mask = pixmap->mask;
165 }
166
167 static gint
168 gtk_pixmap_expose (GtkWidget      *widget,
169                    GdkEventExpose *event)
170 {
171   GtkPixmap *pixmap;
172   GtkMisc *misc;
173   gint x, y;
174   gfloat xalign;
175
176   g_return_val_if_fail (GTK_IS_PIXMAP (widget), FALSE);
177   g_return_val_if_fail (event != NULL, FALSE);
178
179   if (GTK_WIDGET_DRAWABLE (widget))
180     {
181       pixmap = GTK_PIXMAP (widget);
182       misc = GTK_MISC (widget);
183
184       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
185         xalign = misc->xalign;
186       else
187         xalign = 1.0 - misc->xalign;
188   
189       x = floor (widget->allocation.x + misc->xpad
190                  + ((widget->allocation.width - widget->requisition.width) * xalign));
191       y = floor (widget->allocation.y + misc->ypad 
192                  + ((widget->allocation.height - widget->requisition.height) * misc->yalign));
193       
194       if (pixmap->mask)
195         {
196           gdk_gc_set_clip_mask (widget->style->black_gc, pixmap->mask);
197           gdk_gc_set_clip_origin (widget->style->black_gc, x, y);
198         }
199
200       if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE
201           && pixmap->build_insensitive)
202         {
203           if (!pixmap->pixmap_insensitive)
204             build_insensitive_pixmap (pixmap);
205           gdk_draw_pixmap (widget->window,
206                            widget->style->black_gc,
207                            pixmap->pixmap_insensitive,
208                            0, 0, x, y, -1, -1);
209         }
210       else
211         {
212           gdk_draw_pixmap (widget->window,
213                            widget->style->black_gc,
214                            pixmap->pixmap,
215                            0, 0, x, y, -1, -1);
216         }
217
218       if (pixmap->mask)
219         {
220           gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
221           gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
222         }
223     }
224   return FALSE;
225 }
226
227 void
228 gtk_pixmap_set_build_insensitive (GtkPixmap *pixmap, gboolean build)
229 {
230   g_return_if_fail (GTK_IS_PIXMAP (pixmap));
231
232   pixmap->build_insensitive = build;
233
234   if (GTK_WIDGET_VISIBLE (pixmap))
235     {
236       gtk_widget_queue_clear (GTK_WIDGET (pixmap));
237     }
238 }
239
240 static void
241 build_insensitive_pixmap (GtkPixmap *gtkpixmap)
242 {
243   GdkPixmap *pixmap = gtkpixmap->pixmap;
244   GdkPixmap *insensitive;
245   gint w, h;
246   GdkPixbuf *pixbuf;
247   GdkPixbuf *stated;
248   
249   gdk_window_get_size (pixmap, &w, &h);
250
251   pixbuf = gdk_pixbuf_get_from_drawable (NULL,
252                                          pixmap,
253                                          gtk_widget_get_colormap (GTK_WIDGET (gtkpixmap)),
254                                          0, 0,
255                                          0, 0,
256                                          w, h);
257   
258   stated = gdk_pixbuf_copy (pixbuf);
259   
260   gdk_pixbuf_saturate_and_pixelate (pixbuf, stated,
261                                     0.8, TRUE);
262
263   g_object_unref (G_OBJECT (pixbuf));
264   pixbuf = NULL;
265   
266   insensitive = gdk_pixmap_new (GTK_WIDGET (gtkpixmap)->window, w, h, -1);
267
268   gdk_draw_pixbuf (insensitive,
269                    GTK_WIDGET (gtkpixmap)->style->white_gc,
270                    stated,
271                    0, 0,
272                    0, 0,
273                    w, h,
274                    GDK_RGB_DITHER_NORMAL,
275                    0, 0);
276
277   gtkpixmap->pixmap_insensitive = insensitive;
278
279   g_object_unref (G_OBJECT (stated));
280 }
281
282 #define __GTK_PIXMAP_C__
283 #include "gtkaliasdef.c"