]> Pileus Git - ~andy/gtk/blob - gtk/gtkoffscreenwindow.c
tests: Add simple test for image clipboard
[~andy/gtk] / gtk / gtkoffscreenwindow.c
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the
14  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
15  * Boston, MA 02111-1307, USA.
16  *
17  * Authors: Cody Russell <crussell@canonical.com>
18  *          Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include "gtkoffscreenwindow.h"
24 #include "gtkwidgetprivate.h"
25 #include "gtkcontainerprivate.h"
26 #include "gtkprivate.h"
27
28 /**
29  * SECTION:gtkoffscreenwindow
30  * @short_description: A toplevel to manage offscreen rendering of child widgets
31  * @title: GtkOffscreenWindow
32  *
33  * GtkOffscreenWindow is strictly intended to be used for obtaining
34  * snapshots of widgets that are not part of a normal widget hierarchy.
35  * Since #GtkOffscreenWindow is a toplevel widget you cannot obtain
36  * snapshots of a full window with it since you cannot pack a toplevel
37  * widget in another toplevel.
38  *
39  * The idea is to take a widget and manually set the state of it,
40  * add it to a GtkOffscreenWindow and then retrieve the snapshot
41  * as a #cairo_surface_t or #GdkPixbuf.
42  *
43  * GtkOffscreenWindow derives from #GtkWindow only as an implementation
44  * detail.  Applications should not use any API specific to #GtkWindow
45  * to operate on this object.  It should be treated as a #GtkBin that
46  * has no parent widget.
47  *
48  * When contained offscreen widgets are redrawn, GtkOffscreenWindow
49  * will emit a #GtkWidget::damage-event signal.
50  */
51
52 G_DEFINE_TYPE (GtkOffscreenWindow, gtk_offscreen_window, GTK_TYPE_WINDOW);
53
54 static void
55 gtk_offscreen_window_get_preferred_width (GtkWidget *widget,
56                                           gint      *minimum,
57                                           gint      *natural)
58 {
59   GtkBin *bin = GTK_BIN (widget);
60   GtkWidget *child;
61   gint border_width;
62   gint default_width;
63
64   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
65
66   *minimum = border_width * 2;
67   *natural = border_width * 2;
68
69   child = gtk_bin_get_child (bin);
70
71   if (child != NULL && gtk_widget_get_visible (child))
72     {
73       gint child_min, child_nat;
74
75       gtk_widget_get_preferred_width (child, &child_min, &child_nat);
76
77       *minimum += child_min;
78       *natural += child_nat;
79     }
80
81   gtk_window_get_default_size (GTK_WINDOW (widget),
82                                &default_width, NULL);
83
84   *minimum = MAX (*minimum, default_width);
85   *natural = MAX (*natural, default_width);
86 }
87
88 static void
89 gtk_offscreen_window_get_preferred_height (GtkWidget *widget,
90                                            gint      *minimum,
91                                            gint      *natural)
92 {
93   GtkBin *bin = GTK_BIN (widget);
94   GtkWidget *child;
95   gint border_width;
96   gint default_height;
97
98   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
99
100   *minimum = border_width * 2;
101   *natural = border_width * 2;
102
103   child = gtk_bin_get_child (bin);
104
105   if (child != NULL && gtk_widget_get_visible (child))
106     {
107       gint child_min, child_nat;
108
109       gtk_widget_get_preferred_height (child, &child_min, &child_nat);
110
111       *minimum += child_min;
112       *natural += child_nat;
113     }
114
115   gtk_window_get_default_size (GTK_WINDOW (widget),
116                                NULL, &default_height);
117
118   *minimum = MAX (*minimum, default_height);
119   *natural = MAX (*natural, default_height);
120 }
121
122 static void
123 gtk_offscreen_window_size_allocate (GtkWidget *widget,
124                                     GtkAllocation *allocation)
125 {
126   GtkBin *bin = GTK_BIN (widget);
127   GtkWidget *child;
128   gint border_width;
129
130   gtk_widget_set_allocation (widget, allocation);
131
132   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
133
134   if (gtk_widget_get_realized (widget))
135     gdk_window_move_resize (gtk_widget_get_window (widget),
136                             allocation->x,
137                             allocation->y,
138                             allocation->width,
139                             allocation->height);
140
141   child = gtk_bin_get_child (bin);
142
143   if (child != NULL && gtk_widget_get_visible (child))
144     {
145       GtkAllocation  child_alloc;
146
147       child_alloc.x = border_width;
148       child_alloc.y = border_width;
149       child_alloc.width = allocation->width - 2 * border_width;
150       child_alloc.height = allocation->height - 2 * border_width;
151
152       gtk_widget_size_allocate (child, &child_alloc);
153     }
154
155   gtk_widget_queue_draw (widget);
156 }
157
158 static void
159 gtk_offscreen_window_realize (GtkWidget *widget)
160 {
161   GtkAllocation allocation;
162   GtkBin *bin;
163   GtkWidget *child;
164   GdkWindow *window;
165   GdkWindowAttr attributes;
166   gint attributes_mask;
167
168   bin = GTK_BIN (widget);
169
170   gtk_widget_set_realized (widget, TRUE);
171
172   gtk_widget_get_allocation (widget, &allocation);
173
174   attributes.x = allocation.x;
175   attributes.y = allocation.y;
176   attributes.width = allocation.width;
177   attributes.height = allocation.height;
178   attributes.window_type = GDK_WINDOW_OFFSCREEN;
179   attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
180   attributes.visual = gtk_widget_get_visual (widget);
181   attributes.wclass = GDK_INPUT_OUTPUT;
182
183   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
184
185   window = gdk_window_new (gtk_widget_get_parent_window (widget),
186                            &attributes, attributes_mask);
187   gtk_widget_set_window (widget, window);
188   gdk_window_set_user_data (window, widget);
189
190   child = gtk_bin_get_child (bin);
191   if (child)
192     gtk_widget_set_parent_window (child, window);
193
194   gtk_style_context_set_background (gtk_widget_get_style_context (widget),
195                                     window);
196 }
197
198 static void
199 gtk_offscreen_window_resize (GtkWidget *widget)
200 {
201   GtkAllocation allocation = { 0, 0 };
202   GtkRequisition requisition;
203
204   gtk_widget_get_preferred_size (widget, &requisition, NULL);
205
206   allocation.width  = requisition.width;
207   allocation.height = requisition.height;
208   gtk_widget_size_allocate (widget, &allocation);
209 }
210
211 static void
212 move_focus (GtkWidget       *widget,
213             GtkDirectionType dir)
214 {
215   gtk_widget_child_focus (widget, dir);
216
217   if (!gtk_container_get_focus_child (GTK_CONTAINER (widget)))
218     gtk_window_set_focus (GTK_WINDOW (widget), NULL);
219 }
220
221 static void
222 gtk_offscreen_window_show (GtkWidget *widget)
223 {
224   gboolean need_resize;
225   GtkContainer *container;
226
227   _gtk_widget_set_visible_flag (widget, TRUE);
228
229   container = GTK_CONTAINER (widget);
230   need_resize = _gtk_container_get_need_resize (container) || !gtk_widget_get_realized (widget);
231   _gtk_container_set_need_resize (container, FALSE);
232
233   if (need_resize)
234     gtk_offscreen_window_resize (widget);
235
236   gtk_widget_map (widget);
237
238   /* Try to make sure that we have some focused widget */
239   if (!gtk_window_get_focus (GTK_WINDOW (widget)))
240     move_focus (widget, GTK_DIR_TAB_FORWARD);
241 }
242
243 static void
244 gtk_offscreen_window_hide (GtkWidget *widget)
245 {
246   _gtk_widget_set_visible_flag (widget, FALSE);
247   gtk_widget_unmap (widget);
248 }
249
250 static void
251 gtk_offscreen_window_check_resize (GtkContainer *container)
252 {
253   GtkWidget *widget = GTK_WIDGET (container);
254
255   if (gtk_widget_get_visible (widget))
256     gtk_offscreen_window_resize (widget);
257 }
258
259 static void
260 gtk_offscreen_window_class_init (GtkOffscreenWindowClass *class)
261 {
262   GtkWidgetClass *widget_class;
263   GtkContainerClass *container_class;
264
265   widget_class = GTK_WIDGET_CLASS (class);
266   container_class = GTK_CONTAINER_CLASS (class);
267
268   widget_class->realize = gtk_offscreen_window_realize;
269   widget_class->show = gtk_offscreen_window_show;
270   widget_class->hide = gtk_offscreen_window_hide;
271   widget_class->get_preferred_width = gtk_offscreen_window_get_preferred_width;
272   widget_class->get_preferred_height = gtk_offscreen_window_get_preferred_height;
273   widget_class->size_allocate = gtk_offscreen_window_size_allocate;
274
275   container_class->check_resize = gtk_offscreen_window_check_resize;
276 }
277
278 static void
279 gtk_offscreen_window_init (GtkOffscreenWindow *window)
280 {
281 }
282
283 /* --- functions --- */
284 /**
285  * gtk_offscreen_window_new:
286  *
287  * Creates a toplevel container widget that is used to retrieve
288  * snapshots of widgets without showing them on the screen.
289  *
290  * Return value: A pointer to a #GtkWidget
291  *
292  * Since: 2.20
293  */
294 GtkWidget *
295 gtk_offscreen_window_new (void)
296 {
297   return g_object_new (gtk_offscreen_window_get_type (), NULL);
298 }
299
300 /**
301  * gtk_offscreen_window_get_surface:
302  * @offscreen: the #GtkOffscreenWindow contained widget.
303  *
304  * Retrieves a snapshot of the contained widget in the form of
305  * a #cairo_surface_t.  If you need to keep this around over window
306  * resizes then you should add a reference to it.
307  *
308  * Returns: (transfer none): A #cairo_surface_t pointer to the offscreen
309  *     surface, or %NULL.
310  *
311  * Since: 2.20
312  */
313 cairo_surface_t *
314 gtk_offscreen_window_get_surface (GtkOffscreenWindow *offscreen)
315 {
316   g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
317
318   return gdk_offscreen_window_get_surface (gtk_widget_get_window (GTK_WIDGET (offscreen)));
319 }
320
321 /**
322  * gtk_offscreen_window_get_pixbuf:
323  * @offscreen: the #GtkOffscreenWindow contained widget.
324  *
325  * Retrieves a snapshot of the contained widget in the form of
326  * a #GdkPixbuf.  This is a new pixbuf with a reference count of 1,
327  * and the application should unreference it once it is no longer
328  * needed.
329  *
330  * Returns: (transfer full): A #GdkPixbuf pointer, or %NULL.
331  *
332  * Since: 2.20
333  */
334 GdkPixbuf *
335 gtk_offscreen_window_get_pixbuf (GtkOffscreenWindow *offscreen)
336 {
337   cairo_surface_t *surface;
338   GdkPixbuf *pixbuf = NULL;
339   GdkWindow *window;
340
341   g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
342
343   window = gtk_widget_get_window (GTK_WIDGET (offscreen));
344   surface = gdk_offscreen_window_get_surface (window);
345
346   if (surface != NULL)
347     {
348       pixbuf = gdk_pixbuf_get_from_surface (surface,
349                                             0, 0,
350                                             gdk_window_get_width (window),
351                                             gdk_window_get_height (window));
352     }
353
354   return pixbuf;
355 }