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