Debian Codegraph @

demos/gtk-demo/offscreen_window2.c

raw
1 /* Offscreen Windows/Effects
2 *
3 * Offscreen windows can be used to render elements multiple times to achieve
4 * various effects.
5 */
6 #include <glib/gi18n.h>
7 #include <gtk/gtk.h>
8
9 #define GTK_TYPE_MIRROR_BIN (gtk_mirror_bin_get_type ())
10 #define GTK_MIRROR_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_MIRROR_BIN, GtkMirrorBin))
11 #define GTK_MIRROR_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_MIRROR_BIN, GtkMirrorBinClass))
12 #define GTK_IS_MIRROR_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_MIRROR_BIN))
13 #define GTK_IS_MIRROR_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_MIRROR_BIN))
14 #define GTK_MIRROR_BIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_MIRROR_BIN, GtkMirrorBinClass))
15
16 typedef struct _GtkMirrorBin GtkMirrorBin;
17 typedef struct _GtkMirrorBinClass GtkMirrorBinClass;
18
19 struct _GtkMirrorBin
20 {
21 GtkContainer container;
22
23 GtkWidget *child;
24 GdkWindow *offscreen_window;
25 };
26
27 struct _GtkMirrorBinClass
28 {
29 GtkContainerClass parent_class;
30 };
31
32 GType gtk_mirror_bin_get_type (void) G_GNUC_CONST;
33 GtkWidget* gtk_mirror_bin_new (void);
34
35 /*** implementation ***/
36
37 static void gtk_mirror_bin_realize (GtkWidget *widget);
38 static void gtk_mirror_bin_unrealize (GtkWidget *widget);
39 static void gtk_mirror_bin_get_preferred_width (GtkWidget *widget,
40 gint *minimum,
41 gint *natural);
42 static void gtk_mirror_bin_get_preferred_height (GtkWidget *widget,
43 gint *minimum,
44 gint *natural);
45 static void gtk_mirror_bin_size_allocate (GtkWidget *widget,
46 GtkAllocation *allocation);
47 static gboolean gtk_mirror_bin_damage (GtkWidget *widget,
48 GdkEventExpose *event);
49 static gboolean gtk_mirror_bin_draw (GtkWidget *widget,
50 cairo_t *cr);
51
52 static void gtk_mirror_bin_add (GtkContainer *container,
53 GtkWidget *child);
54 static void gtk_mirror_bin_remove (GtkContainer *container,
55 GtkWidget *widget);
56 static void gtk_mirror_bin_forall (GtkContainer *container,
57 gboolean include_internals,
58 GtkCallback callback,
59 gpointer callback_data);
60 static GType gtk_mirror_bin_child_type (GtkContainer *container);
61
62 G_DEFINE_TYPE (GtkMirrorBin, gtk_mirror_bin, GTK_TYPE_CONTAINER);
63
64 static void
65 to_child (GtkMirrorBin *bin,
66 double widget_x,
67 double widget_y,
68 double *x_out,
69 double *y_out)
70 {
71 *x_out = widget_x;
72 *y_out = widget_y;
73 }
74
75 static void
76 to_parent (GtkMirrorBin *bin,
77 double offscreen_x,
78 double offscreen_y,
79 double *x_out,
80 double *y_out)
81 {
82 *x_out = offscreen_x;
83 *y_out = offscreen_y;
84 }
85
86 static void
87 gtk_mirror_bin_class_init (GtkMirrorBinClass *klass)
88 {
89 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
90 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
91
92 widget_class->realize = gtk_mirror_bin_realize;
93 widget_class->unrealize = gtk_mirror_bin_unrealize;
94 widget_class->get_preferred_width = gtk_mirror_bin_get_preferred_width;
95 widget_class->get_preferred_height = gtk_mirror_bin_get_preferred_height;
96 widget_class->size_allocate = gtk_mirror_bin_size_allocate;
97 widget_class->draw = gtk_mirror_bin_draw;
98
99 g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
100 GTK_TYPE_MIRROR_BIN,
101 g_cclosure_new (G_CALLBACK (gtk_mirror_bin_damage),
102 NULL, NULL));
103
104 container_class->add = gtk_mirror_bin_add;
105 container_class->remove = gtk_mirror_bin_remove;
106 container_class->forall = gtk_mirror_bin_forall;
107 container_class->child_type = gtk_mirror_bin_child_type;
108 }
109
110 static void
111 gtk_mirror_bin_init (GtkMirrorBin *bin)
112 {
113 gtk_widget_set_has_window (GTK_WIDGET (bin), TRUE);
114 }
115
116 GtkWidget *
117 gtk_mirror_bin_new (void)
118 {
119 return g_object_new (GTK_TYPE_MIRROR_BIN, NULL);
120 }
121
122 static GdkWindow *
123 pick_offscreen_child (GdkWindow *offscreen_window,
124 double widget_x,
125 double widget_y,
126 GtkMirrorBin *bin)
127 {
128 GtkAllocation child_area;
129 double x, y;
130
131 if (bin->child && gtk_widget_get_visible (bin->child))
132 {
133 to_child (bin, widget_x, widget_y, &x, &y);
134
135 gtk_widget_get_allocation (bin->child, &child_area);
136
137 if (x >= 0 && x < child_area.width &&
138 y >= 0 && y < child_area.height)
139 return bin->offscreen_window;
140 }
141
142 return NULL;
143 }
144
145 static void
146 offscreen_window_to_parent (GdkWindow *offscreen_window,
147 double offscreen_x,
148 double offscreen_y,
149 double *parent_x,
150 double *parent_y,
151 GtkMirrorBin *bin)
152 {
153 to_parent (bin, offscreen_x, offscreen_y, parent_x, parent_y);
154 }
155
156 static void
157 offscreen_window_from_parent (GdkWindow *window,
158 double parent_x,
159 double parent_y,
160 double *offscreen_x,
161 double *offscreen_y,
162 GtkMirrorBin *bin)
163 {
164 to_child (bin, parent_x, parent_y, offscreen_x, offscreen_y);
165 }
166
167 static void
168 gtk_mirror_bin_realize (GtkWidget *widget)
169 {
170 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
171 GtkAllocation allocation;
172 GdkWindow *window;
173 GdkWindowAttr attributes;
174 gint attributes_mask;
175 guint border_width;
176 GtkRequisition child_requisition;
177
178 gtk_widget_set_realized (widget, TRUE);
179
180 gtk_widget_get_allocation (widget, &allocation);
181 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
182
183 attributes.x = allocation.x + border_width;
184 attributes.y = allocation.y + border_width;
185 attributes.width = allocation.width - 2 * border_width;
186 attributes.height = allocation.height - 2 * border_width;
187 attributes.window_type = GDK_WINDOW_CHILD;
188 attributes.event_mask = gtk_widget_get_events (widget)
189 | GDK_EXPOSURE_MASK
190 | GDK_POINTER_MOTION_MASK
191 | GDK_BUTTON_PRESS_MASK
192 | GDK_BUTTON_RELEASE_MASK
193 | GDK_SCROLL_MASK
194 | GDK_ENTER_NOTIFY_MASK
195 | GDK_LEAVE_NOTIFY_MASK;
196
197 attributes.visual = gtk_widget_get_visual (widget);
198 attributes.wclass = GDK_INPUT_OUTPUT;
199
200 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
201
202 window = gdk_window_new (gtk_widget_get_parent_window (widget),
203 &attributes, attributes_mask);
204 gtk_widget_set_window (widget, window);
205 gdk_window_set_user_data (window, widget);
206 g_signal_connect (window, "pick-embedded-child",
207 G_CALLBACK (pick_offscreen_child), bin);
208
209 attributes.window_type = GDK_WINDOW_OFFSCREEN;
210
211 child_requisition.width = child_requisition.height = 0;
212 if (bin->child && gtk_widget_get_visible (bin->child))
213 {
214 GtkAllocation child_allocation;
215
216 gtk_widget_get_allocation (bin->child, &child_allocation);
217 attributes.width = child_allocation.width;
218 attributes.height = child_allocation.height;
219 }
220 bin->offscreen_window = gdk_window_new (gdk_screen_get_root_window (gtk_widget_get_screen (widget)),
221 &attributes, attributes_mask);
222 gdk_window_set_user_data (bin->offscreen_window, widget);
223 if (bin->child)
224 gtk_widget_set_parent_window (bin->child, bin->offscreen_window);
225 gdk_offscreen_window_set_embedder (bin->offscreen_window, window);
226 g_signal_connect (bin->offscreen_window, "to-embedder",
227 G_CALLBACK (offscreen_window_to_parent), bin);
228 g_signal_connect (bin->offscreen_window, "from-embedder",
229 G_CALLBACK (offscreen_window_from_parent), bin);
230
231 gdk_window_show (bin->offscreen_window);
232 }
233
234 static void
235 gtk_mirror_bin_unrealize (GtkWidget *widget)
236 {
237 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
238
239 gdk_window_set_user_data (bin->offscreen_window, NULL);
240 gdk_window_destroy (bin->offscreen_window);
241 bin->offscreen_window = NULL;
242
243 GTK_WIDGET_CLASS (gtk_mirror_bin_parent_class)->unrealize (widget);
244 }
245
246 static GType
247 gtk_mirror_bin_child_type (GtkContainer *container)
248 {
249 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
250
251 if (bin->child)
252 return G_TYPE_NONE;
253
254 return GTK_TYPE_WIDGET;
255 }
256
257 static void
258 gtk_mirror_bin_add (GtkContainer *container,
259 GtkWidget *widget)
260 {
261 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
262
263 if (!bin->child)
264 {
265 gtk_widget_set_parent_window (widget, bin->offscreen_window);
266 gtk_widget_set_parent (widget, GTK_WIDGET (bin));
267 bin->child = widget;
268 }
269 else
270 g_warning ("GtkMirrorBin cannot have more than one child");
271 }
272
273 static void
274 gtk_mirror_bin_remove (GtkContainer *container,
275 GtkWidget *widget)
276 {
277 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
278 gboolean was_visible;
279
280 was_visible = gtk_widget_get_visible (widget);
281
282 if (bin->child == widget)
283 {
284 gtk_widget_unparent (widget);
285
286 bin->child = NULL;
287
288 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
289 gtk_widget_queue_resize (GTK_WIDGET (container));
290 }
291 }
292
293 static void
294 gtk_mirror_bin_forall (GtkContainer *container,
295 gboolean include_internals,
296 GtkCallback callback,
297 gpointer callback_data)
298 {
299 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
300
301 g_return_if_fail (callback != NULL);
302
303 if (bin->child)
304 (*callback) (bin->child, callback_data);
305 }
306
307 static void
308 gtk_mirror_bin_size_request (GtkWidget *widget,
309 GtkRequisition *requisition)
310 {
311 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
312 GtkRequisition child_requisition;
313 guint border_width;
314
315 child_requisition.width = 0;
316 child_requisition.height = 0;
317
318 if (bin->child && gtk_widget_get_visible (bin->child))
319 gtk_widget_get_preferred_size ( (bin->child),
320 &child_requisition, NULL);
321
322 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
323 requisition->width = border_width * 2 + child_requisition.width + 10;
324 requisition->height = border_width * 2 + child_requisition.height * 2 + 10;
325 }
326
327 static void
328 gtk_mirror_bin_get_preferred_width (GtkWidget *widget,
329 gint *minimum,
330 gint *natural)
331 {
332 GtkRequisition requisition;
333
334 gtk_mirror_bin_size_request (widget, &requisition);
335
336 *minimum = *natural = requisition.width;
337 }
338
339 static void
340 gtk_mirror_bin_get_preferred_height (GtkWidget *widget,
341 gint *minimum,
342 gint *natural)
343 {
344 GtkRequisition requisition;
345
346 gtk_mirror_bin_size_request (widget, &requisition);
347
348 *minimum = *natural = requisition.height;
349 }
350
351 static void
352 gtk_mirror_bin_size_allocate (GtkWidget *widget,
353 GtkAllocation *allocation)
354 {
355 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
356 gint w, h;
357 guint border_width;
358
359 gtk_widget_set_allocation (widget, allocation);
360
361 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
362
363 w = allocation->width - border_width * 2;
364 h = allocation->height - border_width * 2;
365
366 if (gtk_widget_get_realized (widget))
367 gdk_window_move_resize (gtk_widget_get_window (widget),
368 allocation->x + border_width,
369 allocation->y + border_width,
370 w, h);
371
372 if (bin->child && gtk_widget_get_visible (bin->child))
373 {
374 GtkRequisition child_requisition;
375 GtkAllocation child_allocation;
376
377 gtk_widget_get_preferred_size (bin->child,
378 &child_requisition, NULL);
379 child_allocation.x = 0;
380 child_allocation.y = 0;
381 child_allocation.height = child_requisition.height;
382 child_allocation.width = child_requisition.width;
383
384 if (gtk_widget_get_realized (widget))
385 gdk_window_move_resize (bin->offscreen_window,
386 allocation->x + border_width,
387 allocation->y + border_width,
388 child_allocation.width, child_allocation.height);
389 gtk_widget_size_allocate (bin->child, &child_allocation);
390 }
391 }
392
393 static gboolean
394 gtk_mirror_bin_damage (GtkWidget *widget,
395 GdkEventExpose *event)
396 {
397 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
398 NULL, FALSE);
399
400 return TRUE;
401 }
402
403 static gboolean
404 gtk_mirror_bin_draw (GtkWidget *widget,
405 cairo_t *cr)
406 {
407 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
408 GdkWindow *window;
409
410 window = gtk_widget_get_window (widget);
411 if (gtk_cairo_should_draw_window (cr, window))
412 {
413 cairo_surface_t *surface;
414 cairo_matrix_t matrix;
415 cairo_pattern_t *mask;
416 int height;
417
418 if (bin->child && gtk_widget_get_visible (bin->child))
419 {
420 surface = gdk_offscreen_window_get_surface (bin->offscreen_window);
421 height = gdk_window_get_height (bin->offscreen_window);
422
423 /* paint the offscreen child */
424 cairo_set_source_surface (cr, surface, 0, 0);
425 cairo_paint (cr);
426
427 cairo_matrix_init (&matrix, 1.0, 0.0, 0.3, 1.0, 0.0, 0.0);
428 cairo_matrix_scale (&matrix, 1.0, -1.0);
429 cairo_matrix_translate (&matrix, -10, - 3 * height - 10);
430 cairo_transform (cr, &matrix);
431
432 cairo_set_source_surface (cr, surface, 0, height);
433
434 /* create linear gradient as mask-pattern to fade out the source */
435 mask = cairo_pattern_create_linear (0.0, height, 0.0, 2*height);
436 cairo_pattern_add_color_stop_rgba (mask, 0.0, 0.0, 0.0, 0.0, 0.0);
437 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.01);
438 cairo_pattern_add_color_stop_rgba (mask, 0.5, 0.0, 0.0, 0.0, 0.25);
439 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.5);
440 cairo_pattern_add_color_stop_rgba (mask, 1.0, 0.0, 0.0, 0.0, 1.0);
441
442 /* paint the reflection */
443 cairo_mask (cr, mask);
444
445 cairo_pattern_destroy (mask);
446 }
447 }
448 else if (gtk_cairo_should_draw_window (cr, bin->offscreen_window))
449 {
450 gtk_render_background (gtk_widget_get_style_context (widget),
451 cr,
452 0, 0,
453 gdk_window_get_width (bin->offscreen_window),
454 gdk_window_get_height (bin->offscreen_window));
455
456 if (bin->child)
457 gtk_container_propagate_draw (GTK_CONTAINER (widget),
458 bin->child,
459 cr);
460 }
461
462 return FALSE;
463 }
464
465 /*** ***/
466
467 GtkWidget *
468 do_offscreen_window2 (GtkWidget *do_widget)
469 {
470 static GtkWidget *window = NULL;
471
472 if (!window)
473 {
474 GtkWidget *bin, *vbox;
475 GtkWidget *hbox, *entry, *applybutton, *backbutton;
476 GtkSizeGroup *group;
477
478 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
479 gtk_window_set_screen (GTK_WINDOW (window),
480 gtk_widget_get_screen (do_widget));
481 gtk_window_set_title (GTK_WINDOW (window), "Effects");
482
483 g_signal_connect (window, "destroy",
484 G_CALLBACK (gtk_widget_destroyed), &window);
485
486 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
487
488 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
489
490 bin = gtk_mirror_bin_new ();
491
492 group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
493
494 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
495 backbutton = gtk_button_new ();
496 gtk_container_add (GTK_CONTAINER (backbutton),
497 gtk_image_new_from_icon_name ("go-previous", 4));
498 gtk_size_group_add_widget (group, backbutton);
499 entry = gtk_entry_new ();
500 gtk_size_group_add_widget (group, entry);
501 applybutton = gtk_button_new_with_label (_("Apply"));
502 gtk_size_group_add_widget (group, applybutton);
503
504 gtk_container_add (GTK_CONTAINER (window), vbox);
505 gtk_box_pack_start (GTK_BOX (vbox), bin, TRUE, TRUE, 0);
506 gtk_container_add (GTK_CONTAINER (bin), hbox);
507 gtk_box_pack_start (GTK_BOX (hbox), backbutton, FALSE, FALSE, 0);
508 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
509 gtk_box_pack_start (GTK_BOX (hbox), applybutton, FALSE, FALSE, 0);
510 }
511
512 if (!gtk_widget_get_visible (window))
513 gtk_widget_show_all (window);
514 else
515 gtk_widget_destroy (window);
516
517 return window;
518 }