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