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.
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.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
15 * Authors: Cody Russell <crussell@canonical.com>
16 * Alexander Larsson <alexl@redhat.com>
21 #include "gtkoffscreenwindow.h"
22 #include "gtkwidgetprivate.h"
23 #include "gtkcontainerprivate.h"
24 #include "gtkprivate.h"
27 * SECTION:gtkoffscreenwindow
28 * @short_description: A toplevel to manage offscreen rendering of child widgets
29 * @title: GtkOffscreenWindow
31 * GtkOffscreenWindow is strictly intended to be used for obtaining
32 * snapshots of widgets that are not part of a normal widget hierarchy.
33 * Since #GtkOffscreenWindow is a toplevel widget you cannot obtain
34 * snapshots of a full window with it since you cannot pack a toplevel
35 * widget in another toplevel.
37 * The idea is to take a widget and manually set the state of it,
38 * add it to a GtkOffscreenWindow and then retrieve the snapshot
39 * as a #cairo_surface_t or #GdkPixbuf.
41 * GtkOffscreenWindow derives from #GtkWindow only as an implementation
42 * detail. Applications should not use any API specific to #GtkWindow
43 * to operate on this object. It should be treated as a #GtkBin that
44 * has no parent widget.
46 * When contained offscreen widgets are redrawn, GtkOffscreenWindow
47 * will emit a #GtkWidget::damage-event signal.
50 G_DEFINE_TYPE (GtkOffscreenWindow, gtk_offscreen_window, GTK_TYPE_WINDOW);
53 gtk_offscreen_window_get_preferred_width (GtkWidget *widget,
57 GtkBin *bin = GTK_BIN (widget);
62 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
64 *minimum = border_width * 2;
65 *natural = border_width * 2;
67 child = gtk_bin_get_child (bin);
69 if (child != NULL && gtk_widget_get_visible (child))
71 gint child_min, child_nat;
73 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
75 *minimum += child_min;
76 *natural += child_nat;
79 gtk_window_get_default_size (GTK_WINDOW (widget),
80 &default_width, NULL);
82 *minimum = MAX (*minimum, default_width);
83 *natural = MAX (*natural, default_width);
87 gtk_offscreen_window_get_preferred_height (GtkWidget *widget,
91 GtkBin *bin = GTK_BIN (widget);
96 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
98 *minimum = border_width * 2;
99 *natural = border_width * 2;
101 child = gtk_bin_get_child (bin);
103 if (child != NULL && gtk_widget_get_visible (child))
105 gint child_min, child_nat;
107 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
109 *minimum += child_min;
110 *natural += child_nat;
113 gtk_window_get_default_size (GTK_WINDOW (widget),
114 NULL, &default_height);
116 *minimum = MAX (*minimum, default_height);
117 *natural = MAX (*natural, default_height);
121 gtk_offscreen_window_size_allocate (GtkWidget *widget,
122 GtkAllocation *allocation)
124 GtkBin *bin = GTK_BIN (widget);
128 gtk_widget_set_allocation (widget, allocation);
130 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
132 if (gtk_widget_get_realized (widget))
133 gdk_window_move_resize (gtk_widget_get_window (widget),
139 child = gtk_bin_get_child (bin);
141 if (child != NULL && gtk_widget_get_visible (child))
143 GtkAllocation child_alloc;
145 child_alloc.x = border_width;
146 child_alloc.y = border_width;
147 child_alloc.width = allocation->width - 2 * border_width;
148 child_alloc.height = allocation->height - 2 * border_width;
150 gtk_widget_size_allocate (child, &child_alloc);
153 gtk_widget_queue_draw (widget);
157 gtk_offscreen_window_realize (GtkWidget *widget)
159 GtkAllocation allocation;
163 GdkWindowAttr attributes;
164 gint attributes_mask;
166 bin = GTK_BIN (widget);
168 gtk_widget_set_realized (widget, TRUE);
170 gtk_widget_get_allocation (widget, &allocation);
172 attributes.x = allocation.x;
173 attributes.y = allocation.y;
174 attributes.width = allocation.width;
175 attributes.height = allocation.height;
176 attributes.window_type = GDK_WINDOW_OFFSCREEN;
177 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
178 attributes.visual = gtk_widget_get_visual (widget);
179 attributes.wclass = GDK_INPUT_OUTPUT;
181 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
183 window = gdk_window_new (gtk_widget_get_parent_window (widget),
184 &attributes, attributes_mask);
185 gtk_widget_set_window (widget, window);
186 gdk_window_set_user_data (window, widget);
188 child = gtk_bin_get_child (bin);
190 gtk_widget_set_parent_window (child, window);
192 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
197 gtk_offscreen_window_resize (GtkWidget *widget)
199 GtkAllocation allocation = { 0, 0 };
200 GtkRequisition requisition;
202 gtk_widget_get_preferred_size (widget, &requisition, NULL);
204 allocation.width = requisition.width;
205 allocation.height = requisition.height;
206 gtk_widget_size_allocate (widget, &allocation);
210 move_focus (GtkWidget *widget,
211 GtkDirectionType dir)
213 gtk_widget_child_focus (widget, dir);
215 if (!gtk_container_get_focus_child (GTK_CONTAINER (widget)))
216 gtk_window_set_focus (GTK_WINDOW (widget), NULL);
220 gtk_offscreen_window_show (GtkWidget *widget)
222 gboolean need_resize;
224 _gtk_widget_set_visible_flag (widget, TRUE);
226 need_resize = _gtk_widget_get_alloc_needed (widget) || !gtk_widget_get_realized (widget);
229 gtk_offscreen_window_resize (widget);
231 gtk_widget_map (widget);
233 /* Try to make sure that we have some focused widget */
234 if (!gtk_window_get_focus (GTK_WINDOW (widget)))
235 move_focus (widget, GTK_DIR_TAB_FORWARD);
239 gtk_offscreen_window_hide (GtkWidget *widget)
241 _gtk_widget_set_visible_flag (widget, FALSE);
242 gtk_widget_unmap (widget);
246 gtk_offscreen_window_check_resize (GtkContainer *container)
248 GtkWidget *widget = GTK_WIDGET (container);
250 if (gtk_widget_get_visible (widget))
251 gtk_offscreen_window_resize (widget);
255 gtk_offscreen_window_class_init (GtkOffscreenWindowClass *class)
257 GtkWidgetClass *widget_class;
258 GtkContainerClass *container_class;
260 widget_class = GTK_WIDGET_CLASS (class);
261 container_class = GTK_CONTAINER_CLASS (class);
263 widget_class->realize = gtk_offscreen_window_realize;
264 widget_class->show = gtk_offscreen_window_show;
265 widget_class->hide = gtk_offscreen_window_hide;
266 widget_class->get_preferred_width = gtk_offscreen_window_get_preferred_width;
267 widget_class->get_preferred_height = gtk_offscreen_window_get_preferred_height;
268 widget_class->size_allocate = gtk_offscreen_window_size_allocate;
270 container_class->check_resize = gtk_offscreen_window_check_resize;
274 gtk_offscreen_window_init (GtkOffscreenWindow *window)
278 /* --- functions --- */
280 * gtk_offscreen_window_new:
282 * Creates a toplevel container widget that is used to retrieve
283 * snapshots of widgets without showing them on the screen.
285 * Return value: A pointer to a #GtkWidget
290 gtk_offscreen_window_new (void)
292 return g_object_new (gtk_offscreen_window_get_type (), NULL);
296 * gtk_offscreen_window_get_surface:
297 * @offscreen: the #GtkOffscreenWindow contained widget.
299 * Retrieves a snapshot of the contained widget in the form of
300 * a #cairo_surface_t. If you need to keep this around over window
301 * resizes then you should add a reference to it.
303 * Returns: (transfer none): A #cairo_surface_t pointer to the offscreen
309 gtk_offscreen_window_get_surface (GtkOffscreenWindow *offscreen)
311 g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
313 return gdk_offscreen_window_get_surface (gtk_widget_get_window (GTK_WIDGET (offscreen)));
317 * gtk_offscreen_window_get_pixbuf:
318 * @offscreen: the #GtkOffscreenWindow contained widget.
320 * Retrieves a snapshot of the contained widget in the form of
321 * a #GdkPixbuf. This is a new pixbuf with a reference count of 1,
322 * and the application should unreference it once it is no longer
325 * Returns: (transfer full): A #GdkPixbuf pointer, or %NULL.
330 gtk_offscreen_window_get_pixbuf (GtkOffscreenWindow *offscreen)
332 cairo_surface_t *surface;
333 GdkPixbuf *pixbuf = NULL;
336 g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
338 window = gtk_widget_get_window (GTK_WIDGET (offscreen));
339 surface = gdk_offscreen_window_get_surface (window);
343 pixbuf = gdk_pixbuf_get_from_surface (surface,
345 gdk_window_get_width (window),
346 gdk_window_get_height (window));