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