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"
24 * SECTION:gtkoffscreenwindow
25 * @short_description: A toplevel container widget used to manage offscreen
26 * rendering of child widgets.
27 * @title: GtkOffscreenWindow
29 * #GtkOffscreenWindow is strictly intended to be used for obtaining
30 * snapshots of widgets that are not part of a normal widget hierarchy.
31 * It differs from gtk_widget_get_snapshot() in that the widget you
32 * want to get a snapshot of need not be displayed on the user's screen
33 * as a part of a widget hierarchy. However, since #GtkOffscreenWindow
34 * is a toplevel widget you cannot obtain snapshots of a full window
35 * with it since you cannot pack a toplevel 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 #GdkPixmap 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_size_request (GtkWidget *widget,
54 GtkRequisition *requisition)
56 GtkBin *bin = GTK_BIN (widget);
58 gint default_width, default_height;
60 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
62 requisition->width = border_width * 2;
63 requisition->height = border_width * 2;
65 if (bin->child && gtk_widget_get_visible (bin->child))
67 GtkRequisition child_req;
69 gtk_widget_size_request (bin->child, &child_req);
71 requisition->width += child_req.width;
72 requisition->height += child_req.height;
75 gtk_window_get_default_size (GTK_WINDOW (widget),
76 &default_width, &default_height);
77 if (default_width > 0)
78 requisition->width = default_width;
80 if (default_height > 0)
81 requisition->height = default_height;
85 gtk_offscreen_window_size_allocate (GtkWidget *widget,
86 GtkAllocation *allocation)
88 GtkBin *bin = GTK_BIN (widget);
91 widget->allocation = *allocation;
93 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
95 if (gtk_widget_get_realized (widget))
96 gdk_window_move_resize (widget->window,
102 if (bin->child && gtk_widget_get_visible (bin->child))
104 GtkAllocation child_alloc;
106 child_alloc.x = border_width;
107 child_alloc.y = border_width;
108 child_alloc.width = allocation->width - 2 * border_width;
109 child_alloc.height = allocation->height - 2 * border_width;
111 gtk_widget_size_allocate (bin->child, &child_alloc);
114 gtk_widget_queue_draw (widget);
118 gtk_offscreen_window_realize (GtkWidget *widget)
121 GdkWindowAttr attributes;
122 gint attributes_mask;
125 bin = GTK_BIN (widget);
127 gtk_widget_set_realized (widget, TRUE);
129 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
131 attributes.x = widget->allocation.x;
132 attributes.y = widget->allocation.y;
133 attributes.width = widget->allocation.width;
134 attributes.height = widget->allocation.height;
135 attributes.window_type = GDK_WINDOW_OFFSCREEN;
136 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
137 attributes.visual = gtk_widget_get_visual (widget);
138 attributes.colormap = gtk_widget_get_colormap (widget);
139 attributes.wclass = GDK_INPUT_OUTPUT;
141 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
143 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
144 &attributes, attributes_mask);
145 gdk_window_set_user_data (widget->window, widget);
148 gtk_widget_set_parent_window (bin->child, widget->window);
150 widget->style = gtk_style_attach (widget->style, widget->window);
152 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
156 gtk_offscreen_window_resize (GtkWidget *widget)
158 GtkAllocation allocation = { 0, 0 };
159 GtkRequisition requisition;
161 gtk_widget_size_request (widget, &requisition);
163 allocation.width = requisition.width;
164 allocation.height = requisition.height;
165 gtk_widget_size_allocate (widget, &allocation);
169 move_focus (GtkWidget *widget,
170 GtkDirectionType dir)
172 gtk_widget_child_focus (widget, dir);
174 if (!gtk_container_get_focus_child (GTK_CONTAINER (widget)))
175 gtk_window_set_focus (GTK_WINDOW (widget), NULL);
179 gtk_offscreen_window_show (GtkWidget *widget)
181 gboolean need_resize;
182 GtkContainer *container;
184 GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
186 container = GTK_CONTAINER (widget);
187 need_resize = _gtk_container_get_need_resize (container) || !gtk_widget_get_realized (widget);
188 _gtk_container_set_need_resize (container, FALSE);
191 gtk_offscreen_window_resize (widget);
193 gtk_widget_map (widget);
195 /* Try to make sure that we have some focused widget */
196 if (!gtk_window_get_focus (GTK_WINDOW (widget)))
197 move_focus (widget, GTK_DIR_TAB_FORWARD);
201 gtk_offscreen_window_hide (GtkWidget *widget)
203 GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
204 gtk_widget_unmap (widget);
208 gtk_offscreen_window_check_resize (GtkContainer *container)
210 GtkWidget *widget = GTK_WIDGET (container);
212 if (gtk_widget_get_visible (widget))
213 gtk_offscreen_window_resize (widget);
217 gtk_offscreen_window_class_init (GtkOffscreenWindowClass *class)
219 GtkWidgetClass *widget_class;
220 GtkContainerClass *container_class;
222 widget_class = GTK_WIDGET_CLASS (class);
223 container_class = GTK_CONTAINER_CLASS (class);
225 widget_class->realize = gtk_offscreen_window_realize;
226 widget_class->show = gtk_offscreen_window_show;
227 widget_class->hide = gtk_offscreen_window_hide;
228 widget_class->size_request = gtk_offscreen_window_size_request;
229 widget_class->size_allocate = gtk_offscreen_window_size_allocate;
231 container_class->check_resize = gtk_offscreen_window_check_resize;
235 gtk_offscreen_window_init (GtkOffscreenWindow *window)
239 /* --- functions --- */
241 * gtk_offscreen_window_new:
243 * Creates a toplevel container widget that is used to retrieve
244 * snapshots of widgets without showing them on the screen. For
245 * widgets that are on the screen and part of a normal widget
246 * hierarchy, gtk_widget_get_snapshot() can be used instead.
248 * Return value: A pointer to a #GtkWidget
253 gtk_offscreen_window_new (void)
255 return g_object_new (gtk_offscreen_window_get_type (), NULL);
259 * gtk_offscreen_window_get_pixmap:
260 * @offscreen: the #GtkOffscreenWindow contained widget.
262 * Retrieves a snapshot of the contained widget in the form of
263 * a #GdkPixmap. If you need to keep this around over window
264 * resizes then you should add a reference to it.
266 * Returns: A #GdkPixmap pointer to the offscreen pixmap, or %NULL.
271 gtk_offscreen_window_get_pixmap (GtkOffscreenWindow *offscreen)
273 g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
275 return gdk_offscreen_window_get_pixmap (GTK_WIDGET (offscreen)->window);
279 * gtk_offscreen_window_get_pixbuf:
280 * @offscreen: the #GtkOffscreenWindow contained widget.
282 * Retrieves a snapshot of the contained widget in the form of
283 * a #GdkPixbuf. This is a new pixbuf with a reference count of 1,
284 * and the application should unreference it once it is no longer
287 * Returns: A #GdkPixbuf pointer, or %NULL.
292 gtk_offscreen_window_get_pixbuf (GtkOffscreenWindow *offscreen)
294 GdkPixmap *pixmap = NULL;
295 GdkPixbuf *pixbuf = NULL;
297 g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
299 pixmap = gdk_offscreen_window_get_pixmap (GTK_WIDGET (offscreen)->window);
305 gdk_drawable_get_size (pixmap, &width, &height);
307 pixbuf = gdk_pixbuf_get_from_drawable (NULL, pixmap, NULL,