]> Pileus Git - ~andy/gtk/blob - gtk/gtkoffscreenwindow.c
offscreenwindow: Fix includes
[~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
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_size_request (GtkWidget *widget,
55                                    GtkRequisition *requisition)
56 {
57   GtkBin *bin = GTK_BIN (widget);
58   GtkWidget *child;
59   gint border_width;
60   gint default_width, default_height;
61
62   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
63
64   requisition->width = border_width * 2;
65   requisition->height = border_width * 2;
66
67   child = gtk_bin_get_child (bin);
68
69   if (child != NULL && gtk_widget_get_visible (child))
70     {
71       GtkRequisition child_req;
72
73       gtk_widget_get_preferred_size (child, &child_req, NULL);
74
75       requisition->width += child_req.width;
76       requisition->height += child_req.height;
77     }
78
79   gtk_window_get_default_size (GTK_WINDOW (widget),
80                                &default_width, &default_height);
81   if (default_width > 0)
82     requisition->width = default_width;
83
84   if (default_height > 0)
85     requisition->height = default_height;
86 }
87
88 static void
89 gtk_offscreen_window_size_allocate (GtkWidget *widget,
90                                     GtkAllocation *allocation)
91 {
92   GtkBin *bin = GTK_BIN (widget);
93   GtkWidget *child;
94   gint border_width;
95
96   gtk_widget_set_allocation (widget, allocation);
97
98   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
99
100   if (gtk_widget_get_realized (widget))
101     gdk_window_move_resize (gtk_widget_get_window (widget),
102                             allocation->x,
103                             allocation->y,
104                             allocation->width,
105                             allocation->height);
106
107   child = gtk_bin_get_child (bin);
108
109   if (child != NULL && gtk_widget_get_visible (child))
110     {
111       GtkAllocation  child_alloc;
112
113       child_alloc.x = border_width;
114       child_alloc.y = border_width;
115       child_alloc.width = allocation->width - 2 * border_width;
116       child_alloc.height = allocation->height - 2 * border_width;
117
118       gtk_widget_size_allocate (child, &child_alloc);
119     }
120
121   gtk_widget_queue_draw (widget);
122 }
123
124 static void
125 gtk_offscreen_window_realize (GtkWidget *widget)
126 {
127   GtkAllocation allocation;
128   GtkBin *bin;
129   GtkWidget *child;
130   GdkWindow *window;
131   GdkWindowAttr attributes;
132   gint attributes_mask;
133   gint border_width;
134
135   bin = GTK_BIN (widget);
136
137   gtk_widget_set_realized (widget, TRUE);
138
139   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
140
141   gtk_widget_get_allocation (widget, &allocation);
142
143   attributes.x = allocation.x;
144   attributes.y = allocation.y;
145   attributes.width = allocation.width;
146   attributes.height = allocation.height;
147   attributes.window_type = GDK_WINDOW_OFFSCREEN;
148   attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
149   attributes.visual = gtk_widget_get_visual (widget);
150   attributes.wclass = GDK_INPUT_OUTPUT;
151
152   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
153
154   window = gdk_window_new (gtk_widget_get_parent_window (widget),
155                            &attributes, attributes_mask);
156   gtk_widget_set_window (widget, window);
157   gdk_window_set_user_data (window, widget);
158
159   child = gtk_bin_get_child (bin);
160   if (child)
161     gtk_widget_set_parent_window (child, window);
162
163   gtk_widget_style_attach (widget);
164   gtk_style_set_background (gtk_widget_get_style (widget),
165                             window, GTK_STATE_NORMAL);
166 }
167
168 static void
169 gtk_offscreen_window_resize (GtkWidget *widget)
170 {
171   GtkAllocation allocation = { 0, 0 };
172   GtkRequisition requisition;
173
174   gtk_widget_get_preferred_size (widget, &requisition, NULL);
175
176   allocation.width  = requisition.width;
177   allocation.height = requisition.height;
178   gtk_widget_size_allocate (widget, &allocation);
179 }
180
181 static void
182 move_focus (GtkWidget       *widget,
183             GtkDirectionType dir)
184 {
185   gtk_widget_child_focus (widget, dir);
186
187   if (!gtk_container_get_focus_child (GTK_CONTAINER (widget)))
188     gtk_window_set_focus (GTK_WINDOW (widget), NULL);
189 }
190
191 static void
192 gtk_offscreen_window_show (GtkWidget *widget)
193 {
194   gboolean need_resize;
195   GtkContainer *container;
196
197   _gtk_widget_set_visible_flag (widget, TRUE);
198
199   container = GTK_CONTAINER (widget);
200   need_resize = _gtk_container_get_need_resize (container) || !gtk_widget_get_realized (widget);
201   _gtk_container_set_need_resize (container, FALSE);
202
203   if (need_resize)
204     gtk_offscreen_window_resize (widget);
205
206   gtk_widget_map (widget);
207
208   /* Try to make sure that we have some focused widget */
209   if (!gtk_window_get_focus (GTK_WINDOW (widget)))
210     move_focus (widget, GTK_DIR_TAB_FORWARD);
211 }
212
213 static void
214 gtk_offscreen_window_hide (GtkWidget *widget)
215 {
216   _gtk_widget_set_visible_flag (widget, FALSE);
217   gtk_widget_unmap (widget);
218 }
219
220 static void
221 gtk_offscreen_window_check_resize (GtkContainer *container)
222 {
223   GtkWidget *widget = GTK_WIDGET (container);
224
225   if (gtk_widget_get_visible (widget))
226     gtk_offscreen_window_resize (widget);
227 }
228
229 static void
230 gtk_offscreen_window_class_init (GtkOffscreenWindowClass *class)
231 {
232   GtkWidgetClass *widget_class;
233   GtkContainerClass *container_class;
234
235   widget_class = GTK_WIDGET_CLASS (class);
236   container_class = GTK_CONTAINER_CLASS (class);
237
238   widget_class->realize = gtk_offscreen_window_realize;
239   widget_class->show = gtk_offscreen_window_show;
240   widget_class->hide = gtk_offscreen_window_hide;
241   widget_class->size_request = gtk_offscreen_window_size_request;
242   widget_class->size_allocate = gtk_offscreen_window_size_allocate;
243
244   container_class->check_resize = gtk_offscreen_window_check_resize;
245 }
246
247 static void
248 gtk_offscreen_window_init (GtkOffscreenWindow *window)
249 {
250 }
251
252 /* --- functions --- */
253 /**
254  * gtk_offscreen_window_new:
255  *
256  * Creates a toplevel container widget that is used to retrieve
257  * snapshots of widgets without showing them on the screen.
258  *
259  * Return value: A pointer to a #GtkWidget
260  *
261  * Since: 2.20
262  */
263 GtkWidget *
264 gtk_offscreen_window_new (void)
265 {
266   return g_object_new (gtk_offscreen_window_get_type (), NULL);
267 }
268
269 /**
270  * gtk_offscreen_window_get_surface:
271  * @offscreen: the #GtkOffscreenWindow contained widget.
272  *
273  * Retrieves a snapshot of the contained widget in the form of
274  * a #cairo_surface_t.  If you need to keep this around over window
275  * resizes then you should add a reference to it.
276  *
277  * Returns: (transfer none): A #cairo_surface_t pointer to the offscreen
278  *     surface, or %NULL.
279  *
280  * Since: 2.20
281  */
282 cairo_surface_t *
283 gtk_offscreen_window_get_surface (GtkOffscreenWindow *offscreen)
284 {
285   g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
286
287   return gdk_offscreen_window_get_surface (gtk_widget_get_window (GTK_WIDGET (offscreen)));
288 }
289
290 /**
291  * gtk_offscreen_window_get_pixbuf:
292  * @offscreen: the #GtkOffscreenWindow contained widget.
293  *
294  * Retrieves a snapshot of the contained widget in the form of
295  * a #GdkPixbuf.  This is a new pixbuf with a reference count of 1,
296  * and the application should unreference it once it is no longer
297  * needed.
298  *
299  * Returns: (transfer full): A #GdkPixbuf pointer, or %NULL.
300  *
301  * Since: 2.20
302  */
303 GdkPixbuf *
304 gtk_offscreen_window_get_pixbuf (GtkOffscreenWindow *offscreen)
305 {
306   cairo_surface_t *surface;
307   GdkPixbuf *pixbuf = NULL;
308   GdkWindow *window;
309
310   g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
311
312   window = gtk_widget_get_window (GTK_WIDGET (offscreen));
313   surface = gdk_offscreen_window_get_surface (window);
314
315   if (surface != NULL)
316     {
317       pixbuf = gdk_pixbuf_get_from_surface (NULL, surface,
318                                             0, 0, 0, 0,
319                                             gdk_window_get_width (window),
320                                             gdk_window_get_height (window));
321     }
322
323   return pixbuf;
324 }