]> Pileus Git - ~andy/gtk/blob - gtk/gtkimage.c
Changed LGPL address for FSF in all .h and .c files
[~andy/gtk] / gtk / gtkimage.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 "gtkimage.h"
21
22
23 static void gtk_image_class_init (GtkImageClass  *klass);
24 static void gtk_image_init       (GtkImage       *image);
25 static gint gtk_image_expose     (GtkWidget      *widget,
26                                   GdkEventExpose *event);
27
28
29 guint
30 gtk_image_get_type ()
31 {
32   static guint image_type = 0;
33
34   if (!image_type)
35     {
36       GtkTypeInfo image_info =
37       {
38         "GtkImage",
39         sizeof (GtkImage),
40         sizeof (GtkImageClass),
41         (GtkClassInitFunc) gtk_image_class_init,
42         (GtkObjectInitFunc) gtk_image_init,
43         (GtkArgSetFunc) NULL,
44         (GtkArgGetFunc) NULL,
45       };
46
47       image_type = gtk_type_unique (gtk_misc_get_type (), &image_info);
48     }
49
50   return image_type;
51 }
52
53 static void
54 gtk_image_class_init (GtkImageClass *class)
55 {
56   GtkWidgetClass *widget_class;
57
58   widget_class = (GtkWidgetClass*) class;
59
60   widget_class->expose_event = gtk_image_expose;
61 }
62
63 static void
64 gtk_image_init (GtkImage *image)
65 {
66   GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);
67
68   image->image = NULL;
69   image->mask = NULL;
70 }
71
72 GtkWidget*
73 gtk_image_new (GdkImage  *val,
74                GdkBitmap *mask)
75 {
76   GtkImage *image;
77
78   g_return_val_if_fail (val != NULL, NULL);
79
80   image = gtk_type_new (gtk_image_get_type ());
81
82   gtk_image_set (image, val, mask);
83
84   return GTK_WIDGET (image);
85 }
86
87 void
88 gtk_image_set (GtkImage  *image,
89                GdkImage  *val,
90                GdkBitmap *mask)
91 {
92   g_return_if_fail (image != NULL);
93   g_return_if_fail (GTK_IS_IMAGE (image));
94
95   image->image = val;
96   image->mask = mask;
97
98   if (image->image)
99     {
100       GTK_WIDGET (image)->requisition.width = image->image->width + GTK_MISC (image)->xpad * 2;
101       GTK_WIDGET (image)->requisition.height = image->image->height + GTK_MISC (image)->ypad * 2;
102     }
103   else
104     {
105       GTK_WIDGET (image)->requisition.width = 0;
106       GTK_WIDGET (image)->requisition.height = 0;
107     }
108
109   if (GTK_WIDGET_VISIBLE (image))
110     gtk_widget_queue_resize (GTK_WIDGET (image));
111 }
112
113 void
114 gtk_image_get (GtkImage   *image,
115                GdkImage  **val,
116                GdkBitmap **mask)
117 {
118   g_return_if_fail (image != NULL);
119   g_return_if_fail (GTK_IS_IMAGE (image));
120
121   if (val)
122     *val = image->image;
123   if (mask)
124     *mask = image->mask;
125 }
126
127
128 static gint
129 gtk_image_expose (GtkWidget      *widget,
130                   GdkEventExpose *event)
131 {
132   g_return_val_if_fail (widget != NULL, FALSE);
133   g_return_val_if_fail (GTK_IS_IMAGE (widget), FALSE);
134   g_return_val_if_fail (event != NULL, FALSE);
135
136   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
137     {
138       GtkImage *image;
139       GtkMisc *misc;
140       GdkRectangle area, image_bound, intersection;
141       gint x, y;
142       
143       image = GTK_IMAGE (widget);
144       misc = GTK_MISC (widget);
145
146       x = (widget->allocation.x * (1.0 - misc->xalign) +
147            (widget->allocation.x + widget->allocation.width
148             - (widget->requisition.width - misc->xpad * 2)) *
149            misc->xalign) + 0.5;
150       y = (widget->allocation.y * (1.0 - misc->yalign) +
151            (widget->allocation.y + widget->allocation.height
152             - (widget->requisition.height - misc->ypad * 2)) *
153            misc->yalign) + 0.5;
154
155       if (image->mask)
156         {
157           gdk_gc_set_clip_mask (widget->style->black_gc, image->mask);
158           gdk_gc_set_clip_origin (widget->style->black_gc, x, y);
159         }
160
161       image_bound.x = x;
162       image_bound.y = y;      
163       image_bound.width = image->image->width;
164       image_bound.height = image->image->height;      
165
166       area = event->area;
167       
168       if(gdk_rectangle_intersect(&image_bound, &area, &intersection))
169         {
170           gdk_draw_image (widget->window,
171                           widget->style->black_gc,
172                           image->image,
173                           image_bound.x - x, image_bound.y - y,
174                           image_bound.x, image_bound.y,
175                           image_bound.width, image_bound.height);
176         }
177       
178       if (image->mask)
179         {
180           gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
181           gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
182         }
183     }
184
185   return FALSE;
186 }