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