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