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