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, see <http://www.gnu.org/licenses/>.
14 *
15 * Authors: Cody Russell <crussell@canonical.com>
16 * Alexander Larsson <alexl@redhat.com>
17 */
18
19 #include "config.h"
20
21 #include "gtkoffscreenwindow.h"
22 #include "gtkwidgetprivate.h"
23 #include "gtkcontainerprivate.h"
24 #include "gtkprivate.h"
25
26 /**
27 * SECTION:gtkoffscreenwindow
28 * @short_description: A toplevel to manage offscreen rendering of child widgets
29 * @title: GtkOffscreenWindow
30 *
31 * GtkOffscreenWindow is strictly intended to be used for obtaining
32 * snapshots of widgets that are not part of a normal widget hierarchy.
33 * Since #GtkOffscreenWindow is a toplevel widget you cannot obtain
34 * snapshots of a full window with it since you cannot pack a toplevel
35 * 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 #cairo_surface_t 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
gtk_offscreen_window_get_preferred_width(GtkWidget * widget,gint * minimum,gint * natural)53 gtk_offscreen_window_get_preferred_width (GtkWidget *widget,
54 gint *minimum,
55 gint *natural)
56 {
57 GtkBin *bin = GTK_BIN (widget);
58 GtkWidget *child;
59 gint border_width;
60 gint default_width;
61
62 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
63
64 *minimum = border_width * 2;
65 *natural = border_width * 2;
66
67 child = gtk_bin_get_child (bin);
68
69 if (child != NULL && gtk_widget_get_visible (child))
70 {
71 gint child_min, child_nat;
72
73 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
74
75 *minimum += child_min;
76 *natural += child_nat;
77 }
78
79 gtk_window_get_default_size (GTK_WINDOW (widget),
80 &default_width, NULL);
81
82 *minimum = MAX (*minimum, default_width);
83 *natural = MAX (*natural, default_width);
84 }
85
86 static void
gtk_offscreen_window_get_preferred_height(GtkWidget * widget,gint * minimum,gint * natural)87 gtk_offscreen_window_get_preferred_height (GtkWidget *widget,
88 gint *minimum,
89 gint *natural)
90 {
91 GtkBin *bin = GTK_BIN (widget);
92 GtkWidget *child;
93 gint border_width;
94 gint default_height;
95
96 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
97
98 *minimum = border_width * 2;
99 *natural = border_width * 2;
100
101 child = gtk_bin_get_child (bin);
102
103 if (child != NULL && gtk_widget_get_visible (child))
104 {
105 gint child_min, child_nat;
106
107 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
108
109 *minimum += child_min;
110 *natural += child_nat;
111 }
112
113 gtk_window_get_default_size (GTK_WINDOW (widget),
114 NULL, &default_height);
115
116 *minimum = MAX (*minimum, default_height);
117 *natural = MAX (*natural, default_height);
118 }
119
120 static void
gtk_offscreen_window_size_allocate(GtkWidget * widget,GtkAllocation * allocation)121 gtk_offscreen_window_size_allocate (GtkWidget *widget,
122 GtkAllocation *allocation)
123 {
124 GtkBin *bin = GTK_BIN (widget);
125 GtkWidget *child;
126 gint border_width;
127
128 gtk_widget_set_allocation (widget, allocation);
129
130 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
131
132 if (gtk_widget_get_realized (widget))
133 gdk_window_move_resize (gtk_widget_get_window (widget),
134 allocation->x,
135 allocation->y,
136 allocation->width,
137 allocation->height);
138
139 child = gtk_bin_get_child (bin);
140
141 if (child != NULL && gtk_widget_get_visible (child))
142 {
143 GtkAllocation child_alloc;
144
145 child_alloc.x = border_width;
146 child_alloc.y = border_width;
147 child_alloc.width = allocation->width - 2 * border_width;
148 child_alloc.height = allocation->height - 2 * border_width;
149
150 gtk_widget_size_allocate (child, &child_alloc);
151 }
152
153 gtk_widget_queue_draw (widget);
154 }
155
156 static void
gtk_offscreen_window_realize(GtkWidget * widget)157 gtk_offscreen_window_realize (GtkWidget *widget)
158 {
159 GtkAllocation allocation;
160 GtkBin *bin;
161 GtkWidget *child;
162 GdkWindow *window;
163 GdkWindowAttr attributes;
164 gint attributes_mask;
165
166 bin = GTK_BIN (widget);
167
168 gtk_widget_set_realized (widget, TRUE);
169
170 gtk_widget_get_allocation (widget, &allocation);
171
172 attributes.x = allocation.x;
173 attributes.y = allocation.y;
174 attributes.width = allocation.width;
175 attributes.height = allocation.height;
176 attributes.window_type = GDK_WINDOW_OFFSCREEN;
177 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
178 attributes.visual = gtk_widget_get_visual (widget);
179 attributes.wclass = GDK_INPUT_OUTPUT;
180
181 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
182
183 window = gdk_window_new (gtk_widget_get_parent_window (widget),
184 &attributes, attributes_mask);
185 gtk_widget_set_window (widget, window);
186 gtk_widget_register_window (widget, window);
187
188 child = gtk_bin_get_child (bin);
189 if (child)
190 gtk_widget_set_parent_window (child, window);
191 }
192
193 static void
gtk_offscreen_window_resize(GtkWidget * widget)194 gtk_offscreen_window_resize (GtkWidget *widget)
195 {
196 GtkAllocation allocation = { 0, 0 };
197 GtkRequisition requisition;
198
199 gtk_widget_get_preferred_size (widget, &requisition, NULL);
200
201 allocation.width = requisition.width;
202 allocation.height = requisition.height;
203 gtk_widget_size_allocate (widget, &allocation);
204 }
205
206 static void
move_focus(GtkWidget * widget,GtkDirectionType dir)207 move_focus (GtkWidget *widget,
208 GtkDirectionType dir)
209 {
210 gtk_widget_child_focus (widget, dir);
211
212 if (!gtk_container_get_focus_child (GTK_CONTAINER (widget)))
213 gtk_window_set_focus (GTK_WINDOW (widget), NULL);
214 }
215
216 static void
gtk_offscreen_window_show(GtkWidget * widget)217 gtk_offscreen_window_show (GtkWidget *widget)
218 {
219 gboolean need_resize;
220
221 _gtk_widget_set_visible_flag (widget, TRUE);
222
223 need_resize = _gtk_widget_get_alloc_needed (widget) || !gtk_widget_get_realized (widget);
224
225 if (need_resize)
226 gtk_offscreen_window_resize (widget);
227
228 gtk_widget_map (widget);
229
230 /* Try to make sure that we have some focused widget */
231 if (!gtk_window_get_focus (GTK_WINDOW (widget)))
232 move_focus (widget, GTK_DIR_TAB_FORWARD);
233 }
234
235 static void
gtk_offscreen_window_hide(GtkWidget * widget)236 gtk_offscreen_window_hide (GtkWidget *widget)
237 {
238 _gtk_widget_set_visible_flag (widget, FALSE);
239 gtk_widget_unmap (widget);
240 }
241
242 static void
gtk_offscreen_window_check_resize(GtkContainer * container)243 gtk_offscreen_window_check_resize (GtkContainer *container)
244 {
245 GtkWidget *widget = GTK_WIDGET (container);
246
247 if (gtk_widget_get_visible (widget))
248 gtk_offscreen_window_resize (widget);
249 }
250
251 static void
gtk_offscreen_window_class_init(GtkOffscreenWindowClass * class)252 gtk_offscreen_window_class_init (GtkOffscreenWindowClass *class)
253 {
254 GtkWidgetClass *widget_class;
255 GtkContainerClass *container_class;
256
257 widget_class = GTK_WIDGET_CLASS (class);
258 container_class = GTK_CONTAINER_CLASS (class);
259
260 widget_class->realize = gtk_offscreen_window_realize;
261 widget_class->show = gtk_offscreen_window_show;
262 widget_class->hide = gtk_offscreen_window_hide;
263 widget_class->get_preferred_width = gtk_offscreen_window_get_preferred_width;
264 widget_class->get_preferred_height = gtk_offscreen_window_get_preferred_height;
265 widget_class->size_allocate = gtk_offscreen_window_size_allocate;
266
267 container_class->check_resize = gtk_offscreen_window_check_resize;
268 }
269
270 static void
gtk_offscreen_window_init(GtkOffscreenWindow * window)271 gtk_offscreen_window_init (GtkOffscreenWindow *window)
272 {
273 }
274
275 /* --- functions --- */
276 /**
277 * gtk_offscreen_window_new:
278 *
279 * Creates a toplevel container widget that is used to retrieve
280 * snapshots of widgets without showing them on the screen.
281 *
282 * Returns: A pointer to a #GtkWidget
283 *
284 * Since: 2.20
285 */
286 GtkWidget *
gtk_offscreen_window_new(void)287 gtk_offscreen_window_new (void)
288 {
289 return g_object_new (gtk_offscreen_window_get_type (), NULL);
290 }
291
292 /**
293 * gtk_offscreen_window_get_surface:
294 * @offscreen: the #GtkOffscreenWindow contained widget.
295 *
296 * Retrieves a snapshot of the contained widget in the form of
297 * a #cairo_surface_t. If you need to keep this around over window
298 * resizes then you should add a reference to it.
299 *
300 * Returns: (nullable) (transfer none): A #cairo_surface_t pointer to the offscreen
301 * surface, or %NULL.
302 *
303 * Since: 2.20
304 */
305 cairo_surface_t *
gtk_offscreen_window_get_surface(GtkOffscreenWindow * offscreen)306 gtk_offscreen_window_get_surface (GtkOffscreenWindow *offscreen)
307 {
308 g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
309
310 return gdk_offscreen_window_get_surface (gtk_widget_get_window (GTK_WIDGET (offscreen)));
311 }
312
313 /**
314 * gtk_offscreen_window_get_pixbuf:
315 * @offscreen: the #GtkOffscreenWindow contained widget.
316 *
317 * Retrieves a snapshot of the contained widget in the form of
318 * a #GdkPixbuf. This is a new pixbuf with a reference count of 1,
319 * and the application should unreference it once it is no longer
320 * needed.
321 *
322 * Returns: (nullable) (transfer full): A #GdkPixbuf pointer, or %NULL.
323 *
324 * Since: 2.20
325 */
326 GdkPixbuf *
gtk_offscreen_window_get_pixbuf(GtkOffscreenWindow * offscreen)327 gtk_offscreen_window_get_pixbuf (GtkOffscreenWindow *offscreen)
328 {
329 cairo_surface_t *surface;
330 GdkPixbuf *pixbuf = NULL;
331 GdkWindow *window;
332
333 g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);
334
335 window = gtk_widget_get_window (GTK_WIDGET (offscreen));
336 surface = gdk_offscreen_window_get_surface (window);
337
338 if (surface != NULL)
339 {
340 pixbuf = gdk_pixbuf_get_from_surface (surface,
341 0, 0,
342 gdk_window_get_width (window),
343 gdk_window_get_height (window));
344 }
345
346 return pixbuf;
347 }
348