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, write to the
14 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
15 * Boston, MA 02111-1307, USA.
17 * Authors: Cody Russell <crussell@canonical.com>
18 * Alexander Larsson <alexl@redhat.com>
23 #include "gtkoffscreenwindow.h"
24 #include "gtkwidgetprivate.h"
25 #include "gtkcontainerprivate.h"
26 #include "gtkprivate.h"
29 * SECTION:gtkoffscreenwindow
30 * @short_description: A toplevel to manage offscreen rendering of child widgets
31 * @title: GtkOffscreenWindow
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.
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.
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.
48 * When contained offscreen widgets are redrawn, GtkOffscreenWindow
49 * will emit a #GtkWidget::damage-event signal.
52 G_DEFINE_TYPE (GtkOffscreenWindow, gtk_offscreen_window, GTK_TYPE_WINDOW);
55 gtk_offscreen_window_get_preferred_width (GtkWidget *widget,
59 GtkBin *bin = GTK_BIN (widget);
64 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
66 *minimum = border_width * 2;
67 *natural = border_width * 2;
69 child = gtk_bin_get_child (bin);
71 if (child != NULL && gtk_widget_get_visible (child))
73 gint child_min, child_nat;
75 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
77 *minimum += child_min;
78 *natural += child_nat;
81 gtk_window_get_default_size (GTK_WINDOW (widget),
82 &default_width, NULL);
84 *minimum = MAX (*minimum, default_width);
85 *natural = MAX (*natural, default_width);
89 gtk_offscreen_window_get_preferred_height (GtkWidget *widget,
93 GtkBin *bin = GTK_BIN (widget);
98 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
100 *minimum = border_width * 2;
101 *natural = border_width * 2;
103 child = gtk_bin_get_child (bin);
105 if (child != NULL && gtk_widget_get_visible (child))
107 gint child_min, child_nat;
109 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
111 *minimum += child_min;
112 *natural += child_nat;
115 gtk_window_get_default_size (GTK_WINDOW (widget),
116 NULL, &default_height);
118 *minimum = MAX (*minimum, default_height);
119 *natural = MAX (*natural, default_height);
123 gtk_offscreen_window_size_allocate (GtkWidget *widget,
124 GtkAllocation *allocation)
126 GtkBin *bin = GTK_BIN (widget);
130 gtk_widget_set_allocation (widget, allocation);
132 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
134 if (gtk_widget_get_realized (widget))
135 gdk_window_move_resize (gtk_widget_get_window (widget),
141 child = gtk_bin_get_child (bin);
143 if (child != NULL && gtk_widget_get_visible (child))
145 GtkAllocation child_alloc;
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;
152 gtk_widget_size_allocate (child, &child_alloc);
155 gtk_widget_queue_draw (widget);
159 gtk_offscreen_window_realize (GtkWidget *widget)
161 GtkAllocation allocation;
165 GdkWindowAttr attributes;
166 gint attributes_mask;
168 bin = GTK_BIN (widget);
170 gtk_widget_set_realized (widget, TRUE);
172 gtk_widget_get_allocation (widget, &allocation);
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;
183 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
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);
190 child = gtk_bin_get_child (bin);
192 gtk_widget_set_parent_window (child, window);
194 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
199 gtk_offscreen_window_resize (GtkWidget *widget)
201 GtkAllocation allocation = { 0, 0 };
202 GtkRequisition requisition;
204 gtk_widget_get_preferred_size (widget, &requisition, NULL);
206 allocation.width = requisition.width;
207 allocation.height = requisition.height;
208 gtk_widget_size_allocate (widget, &allocation);
212 move_focus (GtkWidget *widget,
213 GtkDirectionType dir)
215 gtk_widget_child_focus (widget, dir);
217 if (!gtk_container_get_focus_child (GTK_CONTAINER (widget)))
218 gtk_window_set_focus (GTK_WINDOW (widget), NULL);
222 gtk_offscreen_window_show (GtkWidget *widget)
224 gboolean need_resize;
225 GtkContainer *container;
227 _gtk_widget_set_visible_flag (widget, TRUE);
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);
234 gtk_offscreen_window_resize (widget);
236 gtk_widget_map (widget);
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);
244 gtk_offscreen_window_hide (GtkWidget *widget)
246 _gtk_widget_set_visible_flag (widget, FALSE);
247 gtk_widget_unmap (widget);
251 gtk_offscreen_window_check_resize (GtkContainer *container)
253 GtkWidget *widget = GTK_WIDGET (container);
255 if (gtk_widget_get_visible (widget))
256 gtk_offscreen_window_resize (widget);
260 gtk_offscreen_window_class_init (GtkOffscreenWindowClass *class)
262 GtkWidgetClass *widget_class;
263 GtkContainerClass *container_class;
265 widget_class = GTK_WIDGET_CLASS (class);
266 container_class = GTK_CONTAINER_CLASS (class);
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;
275 container_class->check_resize = gtk_offscreen_window_check_resize;
279 gtk_offscreen_window_init (GtkOffscreenWindow *window)
283 /* --- functions --- */
285 * gtk_offscreen_window_new:
287 * Creates a toplevel container widget that is used to retrieve
288 * snapshots of widgets without showing them on the screen.
290 * Return value: A pointer to a #GtkWidget
295 gtk_offscreen_window_new (void)
297 return g_object_new (gtk_offscreen_window_get_type (), NULL);
301 * gtk_offscreen_window_get_surface:
302 * @offscreen: the #GtkOffscreenWindow contained widget.
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.
308 * Returns: (transfer none): A #cairo_surface_t pointer to the offscreen
314 gtk_offscreen_window_get_surface (GtkOffscreenWindow *offscreen)
316 g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
318 return gdk_offscreen_window_get_surface (gtk_widget_get_window (GTK_WIDGET (offscreen)));
322 * gtk_offscreen_window_get_pixbuf:
323 * @offscreen: the #GtkOffscreenWindow contained widget.
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
330 * Returns: (transfer full): A #GdkPixbuf pointer, or %NULL.
335 gtk_offscreen_window_get_pixbuf (GtkOffscreenWindow *offscreen)
337 cairo_surface_t *surface;
338 GdkPixbuf *pixbuf = NULL;
341 g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
343 window = gtk_widget_get_window (GTK_WIDGET (offscreen));
344 surface = gdk_offscreen_window_get_surface (window);
348 pixbuf = gdk_pixbuf_get_from_surface (surface,
350 gdk_window_get_width (window),
351 gdk_window_get_height (window));