]> Pileus Git - ~andy/gtk/blob - gtk/gtkpixmap.c
Merge from themes-2. See the ChangeLog for a somewhat detailed
[~andy/gtk] / gtk / gtkpixmap.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include "gtkcontainer.h"
20 #include "gtkpixmap.h"
21
22
23 static void gtk_pixmap_class_init (GtkPixmapClass  *klass);
24 static void gtk_pixmap_init       (GtkPixmap       *pixmap);
25 static gint gtk_pixmap_expose     (GtkWidget       *widget,
26                                    GdkEventExpose  *event);
27 static void gtk_pixmap_finalize   (GtkObject       *object);
28
29 static GtkWidgetClass *parent_class;
30
31 GtkType
32 gtk_pixmap_get_type (void)
33 {
34   static GtkType pixmap_type = 0;
35
36   if (!pixmap_type)
37     {
38       GtkTypeInfo pixmap_info =
39       {
40         "GtkPixmap",
41         sizeof (GtkPixmap),
42         sizeof (GtkPixmapClass),
43         (GtkClassInitFunc) gtk_pixmap_class_init,
44         (GtkObjectInitFunc) gtk_pixmap_init,
45         /* reserved_1 */ NULL,
46         /* reserved_2 */ NULL,
47         (GtkClassInitFunc) NULL,
48       };
49
50       pixmap_type = gtk_type_unique (GTK_TYPE_MISC, &pixmap_info);
51     }
52
53   return pixmap_type;
54 }
55
56 static void
57 gtk_pixmap_class_init (GtkPixmapClass *class)
58 {
59   GtkObjectClass *object_class;
60   GtkWidgetClass *widget_class;
61
62   object_class = (GtkObjectClass*) class;
63   widget_class = (GtkWidgetClass*) class;
64   parent_class = gtk_type_class (gtk_widget_get_type ());
65
66   object_class->finalize = gtk_pixmap_finalize;
67   widget_class->expose_event = gtk_pixmap_expose;
68 }
69
70 static void
71 gtk_pixmap_init (GtkPixmap *pixmap)
72 {
73   GTK_WIDGET_SET_FLAGS (pixmap, GTK_NO_WINDOW);
74
75   pixmap->pixmap = NULL;
76   pixmap->mask = NULL;
77 }
78
79 GtkWidget*
80 gtk_pixmap_new (GdkPixmap *val,
81                 GdkBitmap *mask)
82 {
83   GtkPixmap *pixmap;
84    
85   g_return_val_if_fail (val != NULL, NULL);
86   
87   pixmap = gtk_type_new (gtk_pixmap_get_type ());
88   
89   gtk_pixmap_set (pixmap, val, mask);
90   
91   return GTK_WIDGET (pixmap);
92 }
93
94 static void
95 gtk_pixmap_finalize (GtkObject *object)
96 {
97   gtk_pixmap_set (GTK_PIXMAP (object), NULL, NULL);
98   (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
99 }
100
101 void
102 gtk_pixmap_set (GtkPixmap *pixmap,
103                 GdkPixmap *val,
104                 GdkBitmap *mask)
105 {
106   gint width;
107   gint height;
108   gint oldwidth;
109   gint oldheight;
110
111   g_return_if_fail (pixmap != NULL);
112   g_return_if_fail (GTK_IS_PIXMAP (pixmap));
113
114   if (pixmap->pixmap != val)
115     {
116       oldwidth = GTK_WIDGET (pixmap)->requisition.width;
117       oldheight = GTK_WIDGET (pixmap)->requisition.height;
118       if (pixmap->pixmap)
119         gdk_pixmap_unref (pixmap->pixmap);
120       pixmap->pixmap = val;
121       if (pixmap->pixmap)
122         {
123           gdk_pixmap_ref (pixmap->pixmap);
124           gdk_window_get_size (pixmap->pixmap, &width, &height);
125           GTK_WIDGET (pixmap)->requisition.width =
126             width + GTK_MISC (pixmap)->xpad * 2;
127           GTK_WIDGET (pixmap)->requisition.height =
128             height + GTK_MISC (pixmap)->ypad * 2;
129         }
130       else
131         {
132           GTK_WIDGET (pixmap)->requisition.width = 0;
133           GTK_WIDGET (pixmap)->requisition.height = 0;
134         }
135       if (GTK_WIDGET_VISIBLE (pixmap))
136         {
137           if ((GTK_WIDGET (pixmap)->requisition.width != oldwidth) ||
138               (GTK_WIDGET (pixmap)->requisition.height != oldheight))
139             gtk_widget_queue_resize (GTK_WIDGET (pixmap));
140           else
141             gtk_widget_queue_clear (GTK_WIDGET (pixmap));
142         }
143     }
144
145   if (pixmap->mask != mask)
146     {
147       if (pixmap->mask)
148         gdk_bitmap_unref (pixmap->mask);
149       pixmap->mask = mask;
150       if (pixmap->mask)
151         gdk_bitmap_ref (pixmap->mask);
152     }
153 }
154
155 void
156 gtk_pixmap_get (GtkPixmap  *pixmap,
157                 GdkPixmap **val,
158                 GdkBitmap **mask)
159 {
160   g_return_if_fail (pixmap != NULL);
161   g_return_if_fail (GTK_IS_PIXMAP (pixmap));
162
163   if (val)
164     *val = pixmap->pixmap;
165   if (mask)
166     *mask = pixmap->mask;
167 }
168
169
170 static gint
171 gtk_pixmap_expose (GtkWidget      *widget,
172                    GdkEventExpose *event)
173 {
174   GtkPixmap *pixmap;
175   GtkMisc *misc;
176   gint x, y;
177
178   g_return_val_if_fail (widget != NULL, FALSE);
179   g_return_val_if_fail (GTK_IS_PIXMAP (widget), FALSE);
180   g_return_val_if_fail (event != NULL, FALSE);
181
182   if (GTK_WIDGET_DRAWABLE (widget))
183     {
184       pixmap = GTK_PIXMAP (widget);
185       misc = GTK_MISC (widget);
186
187       x = (widget->allocation.x * (1.0 - misc->xalign) +
188            (widget->allocation.x + widget->allocation.width
189             - (widget->requisition.width - misc->xpad * 2)) *
190            misc->xalign) + 0.5;
191       y = (widget->allocation.y * (1.0 - misc->yalign) +
192            (widget->allocation.y + widget->allocation.height
193             - (widget->requisition.height - misc->ypad * 2)) *
194            misc->yalign) + 0.5;
195
196       if (pixmap->mask)
197         {
198           gdk_gc_set_clip_mask (widget->style->black_gc, pixmap->mask);
199           gdk_gc_set_clip_origin (widget->style->black_gc, x, y);
200         }
201
202       gdk_draw_pixmap (widget->window,
203                        widget->style->black_gc,
204                        pixmap->pixmap,
205                        0, 0, x, y, -1, -1);
206
207       if (pixmap->mask)
208         {
209           gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
210           gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
211         }
212     }
213   return FALSE;
214 }