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