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>
21 #include "gtkoffscreenwindow.h"
25 * SECTION:offscreen_windows
26 * @short description: A toplevel container widget used to manage offscreen
27 * rendering of child widgets.
28 * @title: Offscreen windows
30 * #GtkOffscreenWindow is strictly intended to be used for obtaining
31 * snapshots of widgets that are not part of a normal widget hierarchy.
32 * It differs from gtk_widget_get_snapshot() in that the widget you
33 * want to get a snapshot of need not be displayed on the user's screen
34 * as a part of a widget hierarchy. However, since #GtkOffscreenWindow
35 * is a toplevel widget you cannot obtain snapshots of a full window
36 * with it since you cannot pack a toplevel widget in another toplevel.
38 * The idea is to take a widget and manually set the size and state of
39 * it, add it to a #GtkOffscreenWindow and then retrieve the snapshot
40 * as a #GdkPixmap or #GdkPixbuf.
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.
48 G_DEFINE_TYPE (GtkOffscreenWindow, gtk_offscreen_window, GTK_TYPE_WINDOW);
51 gtk_offscreen_window_size_request (GtkWidget *widget,
52 GtkRequisition *requisition)
54 GtkBin *bin = GTK_BIN (widget);
56 gint default_width, default_height;
58 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
60 requisition->width = border_width * 2;
61 requisition->height = border_width * 2;
63 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
65 GtkRequisition child_req;
67 gtk_widget_size_request (bin->child, &child_req);
69 requisition->width += child_req.width;
70 requisition->height += child_req.height;
73 gtk_window_get_default_size (GTK_WINDOW (widget),
74 &default_width, &default_height);
75 if (default_width > 0)
76 requisition->width = default_width;
78 if (default_height > 0)
79 requisition->height = default_height;
83 gtk_offscreen_window_size_allocate (GtkWidget *widget,
84 GtkAllocation *allocation)
86 GtkBin *bin = GTK_BIN (widget);
89 widget->allocation = *allocation;
91 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
93 if (GTK_WIDGET_REALIZED (widget))
94 gdk_window_move_resize (widget->window,
100 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
102 GtkAllocation child_alloc;
104 child_alloc.x = border_width;
105 child_alloc.y = border_width;
106 child_alloc.width = allocation->width - 2 * border_width;
107 child_alloc.height = allocation->height - 2 * border_width;
109 gtk_widget_size_allocate (bin->child, &child_alloc);
112 gtk_widget_queue_draw (widget);
116 gtk_offscreen_window_realize (GtkWidget *widget)
119 GdkWindowAttr attributes;
120 gint attributes_mask;
123 bin = GTK_BIN (widget);
125 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
127 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
129 attributes.x = widget->allocation.x;
130 attributes.y = widget->allocation.y;
131 attributes.width = widget->allocation.width;
132 attributes.height = widget->allocation.height;
133 attributes.window_type = GDK_WINDOW_OFFSCREEN;
134 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
135 attributes.visual = gtk_widget_get_visual (widget);
136 attributes.colormap = gtk_widget_get_colormap (widget);
137 attributes.wclass = GDK_INPUT_OUTPUT;
139 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
141 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
142 &attributes, attributes_mask);
143 gdk_window_set_user_data (widget->window, widget);
146 gtk_widget_set_parent_window (bin->child, widget->window);
148 widget->style = gtk_style_attach (widget->style, widget->window);
150 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
154 gtk_offscreen_window_resize (GtkWidget *widget)
156 GtkAllocation allocation = { 0, 0 };
157 GtkRequisition requisition;
159 gtk_widget_size_request (widget, &requisition);
161 allocation.width = requisition.width;
162 allocation.height = requisition.height;
163 gtk_widget_size_allocate (widget, &allocation);
167 move_focus (GtkWidget *widget,
168 GtkDirectionType dir)
170 gtk_widget_child_focus (widget, dir);
172 if (!GTK_CONTAINER (widget)->focus_child)
173 gtk_window_set_focus (GTK_WINDOW (widget), NULL);
177 gtk_offscreen_window_show (GtkWidget *widget)
179 gboolean need_resize;
180 GtkContainer *container;
182 GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
184 container = GTK_CONTAINER (widget);
185 need_resize = container->need_resize || !GTK_WIDGET_REALIZED (widget);
186 container->need_resize = FALSE;
189 gtk_offscreen_window_resize (widget);
191 gtk_widget_map (widget);
193 /* Try to make sure that we have some focused widget */
194 if (!gtk_window_get_focus (GTK_WINDOW (widget)))
195 move_focus (widget, GTK_DIR_TAB_FORWARD);
199 gtk_offscreen_window_hide (GtkWidget *widget)
201 GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
202 gtk_widget_unmap (widget);
206 gtk_offscreen_window_check_resize (GtkContainer *container)
208 GtkWidget *widget = GTK_WIDGET (container);
210 if (GTK_WIDGET_VISIBLE (widget))
211 gtk_offscreen_window_resize (widget);
215 gtk_offscreen_window_class_init (GtkOffscreenWindowClass *class)
217 GtkWidgetClass *widget_class;
218 GtkContainerClass *container_class;
220 widget_class = GTK_WIDGET_CLASS (class);
221 container_class = GTK_CONTAINER_CLASS (class);
223 widget_class->realize = gtk_offscreen_window_realize;
224 widget_class->show = gtk_offscreen_window_show;
225 widget_class->hide = gtk_offscreen_window_hide;
226 widget_class->size_request = gtk_offscreen_window_size_request;
227 widget_class->size_allocate = gtk_offscreen_window_size_allocate;
229 container_class->check_resize = gtk_offscreen_window_check_resize;
233 gtk_offscreen_window_init (GtkOffscreenWindow *window)
237 /* --- functions --- */
239 * gtk_offscreen_window_new:
241 * Creates a toplevel container widget that is used to retrieve
242 * snapshots of widgets without showing them on the screen. For
243 * widgets that are on the screen and part of a normal widget
244 * hierarchy, gtk_widget_get_snapshot() can be used instead.
246 * Return value: A pointer to a #GtkWidget
249 gtk_offscreen_window_new (void)
251 return g_object_new (gtk_offscreen_window_get_type (), NULL);
255 * gtk_offscreen_window_get_pixmap:
257 * Retrieves a snapshot of the contained widget in the form of
258 * a #GdkPixmap. If you need to keep this around over window
259 * resizes then you should add a reference to it.
261 * Returns: A #GdkPixmap pointer to the offscreen pixmap, or %NULL.
264 gtk_offscreen_window_get_pixmap (GtkOffscreenWindow *offscreen)
266 g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
268 return gdk_offscreen_window_get_pixmap (GTK_WIDGET (offscreen)->window);
272 * gtk_offscreen_window_get_pixbuf:
274 * Retrieves a snapshot of the contained widget in the form of
275 * a #GdkPixbuf. This is a new pixbuf with a reference count of 1,
276 * and the application should unreference it once it is no longer
279 * Returns: A #GdkPixbuf pointer, or %NULL.
282 gtk_offscreen_window_get_pixbuf (GtkOffscreenWindow *offscreen)
284 GdkPixmap *pixmap = NULL;
285 GdkPixbuf *pixbuf = NULL;
287 g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
289 pixmap = gdk_offscreen_window_get_pixmap (GTK_WIDGET (offscreen)->window);
295 gdk_drawable_get_size (pixmap, &width, &height);
297 pixbuf = gdk_pixbuf_get_from_drawable (NULL, pixmap, NULL,
305 #define __GTK_OFFSCREEN_WINDOW_C__
306 #include "gtkaliasdef.c"