]> Pileus Git - ~andy/gtk/blob - gtk/gtkpixmap.c
Initial revision
[~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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #include "gtkcontainer.h"
19 #include "gtkpixmap.h"
20
21
22 static void gtk_pixmap_class_init (GtkPixmapClass  *klass);
23 static void gtk_pixmap_init       (GtkPixmap       *pixmap);
24 static gint gtk_pixmap_expose     (GtkWidget       *widget,
25                                    GdkEventExpose  *event);
26
27
28 guint
29 gtk_pixmap_get_type ()
30 {
31   static guint pixmap_type = 0;
32
33   if (!pixmap_type)
34     {
35       GtkTypeInfo pixmap_info =
36       {
37         "GtkPixmap",
38         sizeof (GtkPixmap),
39         sizeof (GtkPixmapClass),
40         (GtkClassInitFunc) gtk_pixmap_class_init,
41         (GtkObjectInitFunc) gtk_pixmap_init,
42         (GtkArgFunc) NULL,
43       };
44
45       pixmap_type = gtk_type_unique (gtk_misc_get_type (), &pixmap_info);
46     }
47
48   return pixmap_type;
49 }
50
51 static void
52 gtk_pixmap_class_init (GtkPixmapClass *class)
53 {
54   GtkWidgetClass *widget_class;
55
56   widget_class = (GtkWidgetClass*) class;
57
58   widget_class->expose_event = gtk_pixmap_expose;
59 }
60
61 static void
62 gtk_pixmap_init (GtkPixmap *pixmap)
63 {
64   GTK_WIDGET_SET_FLAGS (pixmap, GTK_NO_WINDOW);
65
66   pixmap->pixmap = NULL;
67   pixmap->mask = NULL;
68 }
69
70 GtkWidget*
71 gtk_pixmap_new (GdkPixmap *val,
72                 GdkBitmap *mask)
73 {
74   GtkPixmap *pixmap;
75
76   g_return_val_if_fail (val != NULL, NULL);
77
78   pixmap = gtk_type_new (gtk_pixmap_get_type ());
79
80   gtk_pixmap_set (pixmap, val, mask);
81
82   return GTK_WIDGET (pixmap);
83 }
84
85 void
86 gtk_pixmap_set (GtkPixmap *pixmap,
87                 GdkPixmap *val,
88                 GdkBitmap *mask)
89 {
90   gint width;
91   gint height;
92
93   g_return_if_fail (pixmap != NULL);
94   g_return_if_fail (GTK_IS_PIXMAP (pixmap));
95   g_return_if_fail (val != NULL);
96
97   pixmap->pixmap = val;
98   pixmap->mask = mask;
99
100   if (pixmap->pixmap)
101     {
102       gdk_window_get_size (pixmap->pixmap, &width, &height);
103       GTK_WIDGET (pixmap)->requisition.width = width + GTK_MISC (pixmap)->xpad * 2;
104       GTK_WIDGET (pixmap)->requisition.height = height + GTK_MISC (pixmap)->ypad * 2;
105     }
106   else
107     {
108       GTK_WIDGET (pixmap)->requisition.width = 0;
109       GTK_WIDGET (pixmap)->requisition.height = 0;
110     }
111
112   if (GTK_WIDGET_VISIBLE (pixmap))
113     gtk_widget_queue_resize (GTK_WIDGET (pixmap));
114 }
115
116 void
117 gtk_pixmap_get (GtkPixmap  *pixmap,
118                 GdkPixmap **val,
119                 GdkBitmap **mask)
120 {
121   g_return_if_fail (pixmap != NULL);
122   g_return_if_fail (GTK_IS_PIXMAP (pixmap));
123
124   if (val)
125     *val = pixmap->pixmap;
126   if (mask)
127     *mask = pixmap->mask;
128 }
129
130
131 static gint
132 gtk_pixmap_expose (GtkWidget      *widget,
133                    GdkEventExpose *event)
134 {
135   GtkPixmap *pixmap;
136   GtkMisc *misc;
137   gint x, y;
138
139   g_return_val_if_fail (widget != NULL, FALSE);
140   g_return_val_if_fail (GTK_IS_PIXMAP (widget), FALSE);
141   g_return_val_if_fail (event != NULL, FALSE);
142
143   if (GTK_WIDGET_DRAWABLE (widget))
144     {
145       pixmap = GTK_PIXMAP (widget);
146       misc = GTK_MISC (widget);
147
148       x = (widget->allocation.x * (1.0 - misc->xalign) +
149            (widget->allocation.x + widget->allocation.width
150             - (widget->requisition.width - misc->xpad * 2)) *
151            misc->xalign) + 0.5;
152       y = (widget->allocation.y * (1.0 - misc->yalign) +
153            (widget->allocation.y + widget->allocation.height
154             - (widget->requisition.height - misc->ypad * 2)) *
155            misc->yalign) + 0.5;
156
157       if (pixmap->mask)
158         {
159           gdk_gc_set_clip_mask (widget->style->black_gc, pixmap->mask);
160           gdk_gc_set_clip_origin (widget->style->black_gc, x, y);
161         }
162
163       gdk_draw_pixmap (widget->window,
164                        widget->style->black_gc,
165                        pixmap->pixmap,
166                        0, 0, x, y, -1, -1);
167
168       if (pixmap->mask)
169         {
170           gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
171           gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
172         }
173     }
174
175   return FALSE;
176 }