1 /* Offscreen windows/Effects
3 * Offscreen windows can be used to render elements multiple times to achieve
8 #define GTK_TYPE_MIRROR_BIN (gtk_mirror_bin_get_type ())
9 #define GTK_MIRROR_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_MIRROR_BIN, GtkMirrorBin))
10 #define GTK_MIRROR_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_MIRROR_BIN, GtkMirrorBinClass))
11 #define GTK_IS_MIRROR_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_MIRROR_BIN))
12 #define GTK_IS_MIRROR_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_MIRROR_BIN))
13 #define GTK_MIRROR_BIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_MIRROR_BIN, GtkMirrorBinClass))
15 typedef struct _GtkMirrorBin GtkMirrorBin;
16 typedef struct _GtkMirrorBinClass GtkMirrorBinClass;
20 GtkContainer container;
23 GdkWindow *offscreen_window;
26 struct _GtkMirrorBinClass
28 GtkContainerClass parent_class;
31 GType gtk_mirror_bin_get_type (void) G_GNUC_CONST;
32 GtkWidget* gtk_mirror_bin_new (void);
34 /*** implementation ***/
36 static void gtk_mirror_bin_realize (GtkWidget *widget);
37 static void gtk_mirror_bin_unrealize (GtkWidget *widget);
38 static void gtk_mirror_bin_get_preferred_width (GtkWidget *widget,
41 static void gtk_mirror_bin_get_preferred_height (GtkWidget *widget,
44 static void gtk_mirror_bin_size_allocate (GtkWidget *widget,
45 GtkAllocation *allocation);
46 static gboolean gtk_mirror_bin_damage (GtkWidget *widget,
47 GdkEventExpose *event);
48 static gboolean gtk_mirror_bin_draw (GtkWidget *widget,
51 static void gtk_mirror_bin_add (GtkContainer *container,
53 static void gtk_mirror_bin_remove (GtkContainer *container,
55 static void gtk_mirror_bin_forall (GtkContainer *container,
56 gboolean include_internals,
58 gpointer callback_data);
59 static GType gtk_mirror_bin_child_type (GtkContainer *container);
61 G_DEFINE_TYPE (GtkMirrorBin, gtk_mirror_bin, GTK_TYPE_CONTAINER);
64 to_child (GtkMirrorBin *bin,
75 to_parent (GtkMirrorBin *bin,
86 gtk_mirror_bin_class_init (GtkMirrorBinClass *klass)
88 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
89 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
91 widget_class->realize = gtk_mirror_bin_realize;
92 widget_class->unrealize = gtk_mirror_bin_unrealize;
93 widget_class->get_preferred_width = gtk_mirror_bin_get_preferred_width;
94 widget_class->get_preferred_height = gtk_mirror_bin_get_preferred_height;
95 widget_class->size_allocate = gtk_mirror_bin_size_allocate;
96 widget_class->draw = gtk_mirror_bin_draw;
98 g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
100 g_cclosure_new (G_CALLBACK (gtk_mirror_bin_damage),
103 container_class->add = gtk_mirror_bin_add;
104 container_class->remove = gtk_mirror_bin_remove;
105 container_class->forall = gtk_mirror_bin_forall;
106 container_class->child_type = gtk_mirror_bin_child_type;
110 gtk_mirror_bin_init (GtkMirrorBin *bin)
112 gtk_widget_set_has_window (GTK_WIDGET (bin), TRUE);
116 gtk_mirror_bin_new (void)
118 return g_object_new (GTK_TYPE_MIRROR_BIN, NULL);
122 pick_offscreen_child (GdkWindow *offscreen_window,
127 GtkAllocation child_area;
130 if (bin->child && gtk_widget_get_visible (bin->child))
132 to_child (bin, widget_x, widget_y, &x, &y);
134 gtk_widget_get_allocation (bin->child, &child_area);
136 if (x >= 0 && x < child_area.width &&
137 y >= 0 && y < child_area.height)
138 return bin->offscreen_window;
145 offscreen_window_to_parent (GdkWindow *offscreen_window,
152 to_parent (bin, offscreen_x, offscreen_y, parent_x, parent_y);
156 offscreen_window_from_parent (GdkWindow *window,
163 to_child (bin, parent_x, parent_y, offscreen_x, offscreen_y);
167 gtk_mirror_bin_realize (GtkWidget *widget)
169 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
170 GtkAllocation allocation;
171 GtkStyleContext *context;
173 GdkWindowAttr attributes;
174 gint attributes_mask;
176 GtkRequisition child_requisition;
178 gtk_widget_set_realized (widget, TRUE);
180 gtk_widget_get_allocation (widget, &allocation);
181 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
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)
190 | GDK_POINTER_MOTION_MASK
191 | GDK_BUTTON_PRESS_MASK
192 | GDK_BUTTON_RELEASE_MASK
194 | GDK_ENTER_NOTIFY_MASK
195 | GDK_LEAVE_NOTIFY_MASK;
197 attributes.visual = gtk_widget_get_visual (widget);
198 attributes.wclass = GDK_INPUT_OUTPUT;
200 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
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);
209 attributes.window_type = GDK_WINDOW_OFFSCREEN;
211 child_requisition.width = child_requisition.height = 0;
212 if (bin->child && gtk_widget_get_visible (bin->child))
214 GtkAllocation child_allocation;
216 gtk_widget_get_allocation (bin->child, &child_allocation);
217 attributes.width = child_allocation.width;
218 attributes.height = child_allocation.height;
220 bin->offscreen_window = gdk_window_new (gtk_widget_get_root_window (widget),
221 &attributes, attributes_mask);
222 gdk_window_set_user_data (bin->offscreen_window, widget);
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);
231 gtk_widget_style_attach (widget);
232 context = gtk_widget_get_style_context (widget);
233 gtk_style_context_set_background (context, window);
234 gtk_style_context_set_background (context, bin->offscreen_window);
235 gdk_window_show (bin->offscreen_window);
239 gtk_mirror_bin_unrealize (GtkWidget *widget)
241 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
243 gdk_window_set_user_data (bin->offscreen_window, NULL);
244 gdk_window_destroy (bin->offscreen_window);
245 bin->offscreen_window = NULL;
247 GTK_WIDGET_CLASS (gtk_mirror_bin_parent_class)->unrealize (widget);
251 gtk_mirror_bin_child_type (GtkContainer *container)
253 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
258 return GTK_TYPE_WIDGET;
262 gtk_mirror_bin_add (GtkContainer *container,
265 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
269 gtk_widget_set_parent_window (widget, bin->offscreen_window);
270 gtk_widget_set_parent (widget, GTK_WIDGET (bin));
274 g_warning ("GtkMirrorBin cannot have more than one child\n");
278 gtk_mirror_bin_remove (GtkContainer *container,
281 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
282 gboolean was_visible;
284 was_visible = gtk_widget_get_visible (widget);
286 if (bin->child == widget)
288 gtk_widget_unparent (widget);
292 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
293 gtk_widget_queue_resize (GTK_WIDGET (container));
298 gtk_mirror_bin_forall (GtkContainer *container,
299 gboolean include_internals,
300 GtkCallback callback,
301 gpointer callback_data)
303 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
305 g_return_if_fail (callback != NULL);
308 (*callback) (bin->child, callback_data);
312 gtk_mirror_bin_size_request (GtkWidget *widget,
313 GtkRequisition *requisition)
315 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
316 GtkRequisition child_requisition;
319 child_requisition.width = 0;
320 child_requisition.height = 0;
322 if (bin->child && gtk_widget_get_visible (bin->child))
323 gtk_widget_get_preferred_size ( (bin->child),
324 &child_requisition, NULL);
326 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
327 requisition->width = border_width * 2 + child_requisition.width + 10;
328 requisition->height = border_width * 2 + child_requisition.height * 2 + 10;
332 gtk_mirror_bin_get_preferred_width (GtkWidget *widget,
336 GtkRequisition requisition;
338 gtk_mirror_bin_size_request (widget, &requisition);
340 *minimum = *natural = requisition.width;
344 gtk_mirror_bin_get_preferred_height (GtkWidget *widget,
348 GtkRequisition requisition;
350 gtk_mirror_bin_size_request (widget, &requisition);
352 *minimum = *natural = requisition.width;
356 gtk_mirror_bin_size_allocate (GtkWidget *widget,
357 GtkAllocation *allocation)
359 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
363 gtk_widget_set_allocation (widget, allocation);
365 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
367 w = allocation->width - border_width * 2;
368 h = allocation->height - border_width * 2;
370 if (gtk_widget_get_realized (widget))
371 gdk_window_move_resize (gtk_widget_get_window (widget),
372 allocation->x + border_width,
373 allocation->y + border_width,
376 if (bin->child && gtk_widget_get_visible (bin->child))
378 GtkRequisition child_requisition;
379 GtkAllocation child_allocation;
381 gtk_widget_get_preferred_size (bin->child,
382 &child_requisition, NULL);
383 child_allocation.x = 0;
384 child_allocation.y = 0;
385 child_allocation.height = child_requisition.height;
386 child_allocation.width = child_requisition.width;
388 if (gtk_widget_get_realized (widget))
389 gdk_window_move_resize (bin->offscreen_window,
390 allocation->x + border_width,
391 allocation->y + border_width,
392 child_allocation.width, child_allocation.height);
393 gtk_widget_size_allocate (bin->child, &child_allocation);
398 gtk_mirror_bin_damage (GtkWidget *widget,
399 GdkEventExpose *event)
401 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
408 gtk_mirror_bin_draw (GtkWidget *widget,
411 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
414 window = gtk_widget_get_window (widget);
415 if (gtk_cairo_should_draw_window (cr, window))
417 cairo_surface_t *surface;
418 cairo_matrix_t matrix;
419 cairo_pattern_t *mask;
422 if (bin->child && gtk_widget_get_visible (bin->child))
424 surface = gdk_offscreen_window_get_surface (bin->offscreen_window);
425 height = gdk_window_get_height (bin->offscreen_window);
427 /* paint the offscreen child */
428 cairo_set_source_surface (cr, surface, 0, 0);
431 cairo_matrix_init (&matrix, 1.0, 0.0, 0.3, 1.0, 0.0, 0.0);
432 cairo_matrix_scale (&matrix, 1.0, -1.0);
433 cairo_matrix_translate (&matrix, -10, - 3 * height - 10);
434 cairo_transform (cr, &matrix);
436 cairo_set_source_surface (cr, surface, 0, height);
438 /* create linear gradient as mask-pattern to fade out the source */
439 mask = cairo_pattern_create_linear (0.0, height, 0.0, 2*height);
440 cairo_pattern_add_color_stop_rgba (mask, 0.0, 0.0, 0.0, 0.0, 0.0);
441 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.01);
442 cairo_pattern_add_color_stop_rgba (mask, 0.5, 0.0, 0.0, 0.0, 0.25);
443 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.5);
444 cairo_pattern_add_color_stop_rgba (mask, 1.0, 0.0, 0.0, 0.0, 1.0);
446 /* paint the reflection */
447 cairo_mask (cr, mask);
449 cairo_pattern_destroy (mask);
452 else if (gtk_cairo_should_draw_window (cr, bin->offscreen_window))
454 gtk_render_background (gtk_widget_get_style_context (widget),
457 gdk_window_get_width (bin->offscreen_window),
458 gdk_window_get_height (bin->offscreen_window));
461 gtk_container_propagate_draw (GTK_CONTAINER (widget),
471 static GtkWidget *window = NULL;
474 do_offscreen_window2 (GtkWidget *do_widget)
478 GtkWidget *bin, *vbox;
479 GtkWidget *hbox, *entry, *applybutton, *backbutton;
482 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
483 gtk_window_set_screen (GTK_WINDOW (window),
484 gtk_widget_get_screen (do_widget));
485 gtk_window_set_title (GTK_WINDOW (window), "Effects");
487 g_signal_connect (window, "destroy",
488 G_CALLBACK (gtk_widget_destroyed), &window);
490 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
492 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
494 bin = gtk_mirror_bin_new ();
496 group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
498 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
499 backbutton = gtk_button_new ();
500 gtk_container_add (GTK_CONTAINER (backbutton),
501 gtk_image_new_from_stock (GTK_STOCK_GO_BACK, 4));
502 gtk_size_group_add_widget (group, backbutton);
503 entry = gtk_entry_new ();
504 gtk_size_group_add_widget (group, entry);
505 applybutton = gtk_button_new ();
506 gtk_size_group_add_widget (group, applybutton);
507 gtk_container_add (GTK_CONTAINER (applybutton),
508 gtk_image_new_from_stock (GTK_STOCK_APPLY, 4));
510 gtk_container_add (GTK_CONTAINER (window), vbox);
511 gtk_box_pack_start (GTK_BOX (vbox), bin, TRUE, TRUE, 0);
512 gtk_container_add (GTK_CONTAINER (bin), hbox);
513 gtk_box_pack_start (GTK_BOX (hbox), backbutton, FALSE, FALSE, 0);
514 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
515 gtk_box_pack_start (GTK_BOX (hbox), applybutton, FALSE, FALSE, 0);
518 if (!gtk_widget_get_visible (window))
519 gtk_widget_show_all (window);
522 gtk_widget_destroy (window);