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_size_request (GtkWidget *widget,
39 GtkRequisition *requisition);
40 static void gtk_mirror_bin_size_allocate (GtkWidget *widget,
41 GtkAllocation *allocation);
42 static gboolean gtk_mirror_bin_damage (GtkWidget *widget,
43 GdkEventExpose *event);
44 static gboolean gtk_mirror_bin_draw (GtkWidget *widget,
47 static void gtk_mirror_bin_add (GtkContainer *container,
49 static void gtk_mirror_bin_remove (GtkContainer *container,
51 static void gtk_mirror_bin_forall (GtkContainer *container,
52 gboolean include_internals,
54 gpointer callback_data);
55 static GType gtk_mirror_bin_child_type (GtkContainer *container);
57 G_DEFINE_TYPE (GtkMirrorBin, gtk_mirror_bin, GTK_TYPE_CONTAINER);
60 to_child (GtkMirrorBin *bin,
71 to_parent (GtkMirrorBin *bin,
82 gtk_mirror_bin_class_init (GtkMirrorBinClass *klass)
84 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
85 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
87 widget_class->realize = gtk_mirror_bin_realize;
88 widget_class->unrealize = gtk_mirror_bin_unrealize;
89 widget_class->size_request = gtk_mirror_bin_size_request;
90 widget_class->size_allocate = gtk_mirror_bin_size_allocate;
91 widget_class->draw = gtk_mirror_bin_draw;
93 g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
95 g_cclosure_new (G_CALLBACK (gtk_mirror_bin_damage),
98 container_class->add = gtk_mirror_bin_add;
99 container_class->remove = gtk_mirror_bin_remove;
100 container_class->forall = gtk_mirror_bin_forall;
101 container_class->child_type = gtk_mirror_bin_child_type;
105 gtk_mirror_bin_init (GtkMirrorBin *bin)
107 gtk_widget_set_has_window (GTK_WIDGET (bin), TRUE);
111 gtk_mirror_bin_new (void)
113 return g_object_new (GTK_TYPE_MIRROR_BIN, NULL);
117 pick_offscreen_child (GdkWindow *offscreen_window,
122 GtkAllocation child_area;
125 if (bin->child && gtk_widget_get_visible (bin->child))
127 to_child (bin, widget_x, widget_y, &x, &y);
129 gtk_widget_get_allocation (bin->child, &child_area);
131 if (x >= 0 && x < child_area.width &&
132 y >= 0 && y < child_area.height)
133 return bin->offscreen_window;
140 offscreen_window_to_parent (GdkWindow *offscreen_window,
147 to_parent (bin, offscreen_x, offscreen_y, parent_x, parent_y);
151 offscreen_window_from_parent (GdkWindow *window,
158 to_child (bin, parent_x, parent_y, offscreen_x, offscreen_y);
162 gtk_mirror_bin_realize (GtkWidget *widget)
164 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
165 GtkAllocation allocation;
168 GdkWindowAttr attributes;
169 gint attributes_mask;
171 GtkRequisition child_requisition;
173 gtk_widget_set_realized (widget, TRUE);
175 gtk_widget_get_allocation (widget, &allocation);
176 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
178 attributes.x = allocation.x + border_width;
179 attributes.y = allocation.y + border_width;
180 attributes.width = allocation.width - 2 * border_width;
181 attributes.height = allocation.height - 2 * border_width;
182 attributes.window_type = GDK_WINDOW_CHILD;
183 attributes.event_mask = gtk_widget_get_events (widget)
185 | GDK_POINTER_MOTION_MASK
186 | GDK_BUTTON_PRESS_MASK
187 | GDK_BUTTON_RELEASE_MASK
189 | GDK_ENTER_NOTIFY_MASK
190 | GDK_LEAVE_NOTIFY_MASK;
192 attributes.visual = gtk_widget_get_visual (widget);
193 attributes.wclass = GDK_INPUT_OUTPUT;
195 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
197 window = gdk_window_new (gtk_widget_get_parent_window (widget),
198 &attributes, attributes_mask);
199 gtk_widget_set_window (widget, window);
200 gdk_window_set_user_data (window, widget);
201 g_signal_connect (window, "pick-embedded-child",
202 G_CALLBACK (pick_offscreen_child), bin);
204 attributes.window_type = GDK_WINDOW_OFFSCREEN;
206 child_requisition.width = child_requisition.height = 0;
207 if (bin->child && gtk_widget_get_visible (bin->child))
209 GtkAllocation child_allocation;
211 gtk_widget_get_allocation (bin->child, &child_allocation);
212 attributes.width = child_allocation.width;
213 attributes.height = child_allocation.height;
215 bin->offscreen_window = gdk_window_new (gtk_widget_get_root_window (widget),
216 &attributes, attributes_mask);
217 gdk_window_set_user_data (bin->offscreen_window, widget);
219 gtk_widget_set_parent_window (bin->child, bin->offscreen_window);
220 gdk_offscreen_window_set_embedder (bin->offscreen_window, window);
221 g_signal_connect (bin->offscreen_window, "to-embedder",
222 G_CALLBACK (offscreen_window_to_parent), bin);
223 g_signal_connect (bin->offscreen_window, "from-embedder",
224 G_CALLBACK (offscreen_window_from_parent), bin);
226 gtk_widget_style_attach (widget);
227 style = gtk_widget_get_style (widget);
228 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
229 gtk_style_set_background (style, bin->offscreen_window, GTK_STATE_NORMAL);
230 gdk_window_show (bin->offscreen_window);
234 gtk_mirror_bin_unrealize (GtkWidget *widget)
236 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
238 gdk_window_set_user_data (bin->offscreen_window, NULL);
239 gdk_window_destroy (bin->offscreen_window);
240 bin->offscreen_window = NULL;
242 GTK_WIDGET_CLASS (gtk_mirror_bin_parent_class)->unrealize (widget);
246 gtk_mirror_bin_child_type (GtkContainer *container)
248 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
253 return GTK_TYPE_WIDGET;
257 gtk_mirror_bin_add (GtkContainer *container,
260 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
264 gtk_widget_set_parent_window (widget, bin->offscreen_window);
265 gtk_widget_set_parent (widget, GTK_WIDGET (bin));
269 g_warning ("GtkMirrorBin cannot have more than one child\n");
273 gtk_mirror_bin_remove (GtkContainer *container,
276 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
277 gboolean was_visible;
279 was_visible = gtk_widget_get_visible (widget);
281 if (bin->child == widget)
283 gtk_widget_unparent (widget);
287 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
288 gtk_widget_queue_resize (GTK_WIDGET (container));
293 gtk_mirror_bin_forall (GtkContainer *container,
294 gboolean include_internals,
295 GtkCallback callback,
296 gpointer callback_data)
298 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
300 g_return_if_fail (callback != NULL);
303 (*callback) (bin->child, callback_data);
307 gtk_mirror_bin_size_request (GtkWidget *widget,
308 GtkRequisition *requisition)
310 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
311 GtkRequisition child_requisition;
314 child_requisition.width = 0;
315 child_requisition.height = 0;
317 if (bin->child && gtk_widget_get_visible (bin->child))
318 gtk_widget_get_preferred_size ( (bin->child),
319 &child_requisition, NULL);
321 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
322 requisition->width = border_width * 2 + child_requisition.width + 10;
323 requisition->height = border_width * 2 + child_requisition.height * 2 + 10;
327 gtk_mirror_bin_size_allocate (GtkWidget *widget,
328 GtkAllocation *allocation)
330 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
334 gtk_widget_set_allocation (widget, allocation);
336 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
338 w = allocation->width - border_width * 2;
339 h = allocation->height - border_width * 2;
341 if (gtk_widget_get_realized (widget))
342 gdk_window_move_resize (gtk_widget_get_window (widget),
343 allocation->x + border_width,
344 allocation->y + border_width,
347 if (bin->child && gtk_widget_get_visible (bin->child))
349 GtkRequisition child_requisition;
350 GtkAllocation child_allocation;
352 gtk_widget_get_preferred_size (bin->child,
353 &child_requisition, NULL);
354 child_allocation.x = 0;
355 child_allocation.y = 0;
356 child_allocation.height = child_requisition.height;
357 child_allocation.width = child_requisition.width;
359 if (gtk_widget_get_realized (widget))
360 gdk_window_move_resize (bin->offscreen_window,
361 allocation->x + border_width,
362 allocation->y + border_width,
363 child_allocation.width, child_allocation.height);
364 gtk_widget_size_allocate (bin->child, &child_allocation);
369 gtk_mirror_bin_damage (GtkWidget *widget,
370 GdkEventExpose *event)
372 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
379 gtk_mirror_bin_draw (GtkWidget *widget,
382 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
385 window = gtk_widget_get_window (widget);
386 if (gtk_cairo_should_draw_window (cr, window))
388 cairo_surface_t *surface;
389 cairo_matrix_t matrix;
390 cairo_pattern_t *mask;
393 if (bin->child && gtk_widget_get_visible (bin->child))
395 surface = gdk_offscreen_window_get_surface (bin->offscreen_window);
396 height = gdk_window_get_height (bin->offscreen_window);
398 /* paint the offscreen child */
399 cairo_set_source_surface (cr, surface, 0, 0);
402 cairo_matrix_init (&matrix, 1.0, 0.0, 0.3, 1.0, 0.0, 0.0);
403 cairo_matrix_scale (&matrix, 1.0, -1.0);
404 cairo_matrix_translate (&matrix, -10, - 3 * height - 10);
405 cairo_transform (cr, &matrix);
407 cairo_set_source_surface (cr, surface, 0, height);
409 /* create linear gradient as mask-pattern to fade out the source */
410 mask = cairo_pattern_create_linear (0.0, height, 0.0, 2*height);
411 cairo_pattern_add_color_stop_rgba (mask, 0.0, 0.0, 0.0, 0.0, 0.0);
412 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.01);
413 cairo_pattern_add_color_stop_rgba (mask, 0.5, 0.0, 0.0, 0.0, 0.25);
414 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.5);
415 cairo_pattern_add_color_stop_rgba (mask, 1.0, 0.0, 0.0, 0.0, 1.0);
417 /* paint the reflection */
418 cairo_mask (cr, mask);
420 cairo_pattern_destroy (mask);
423 else if (gtk_cairo_should_draw_window (cr, bin->offscreen_window))
425 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
426 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
429 gdk_window_get_width (bin->offscreen_window),
430 gdk_window_get_height (bin->offscreen_window));
433 gtk_container_propagate_draw (GTK_CONTAINER (widget),
443 static GtkWidget *window = NULL;
446 do_offscreen_window2 (GtkWidget *do_widget)
450 GtkWidget *bin, *vbox;
451 GtkWidget *hbox, *entry, *applybutton, *backbutton;
454 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
455 gtk_window_set_screen (GTK_WINDOW (window),
456 gtk_widget_get_screen (do_widget));
457 gtk_window_set_title (GTK_WINDOW (window), "Effects");
459 g_signal_connect (window, "destroy",
460 G_CALLBACK (gtk_widget_destroyed), &window);
462 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
464 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0, FALSE);
466 bin = gtk_mirror_bin_new ();
468 group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
470 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 6);
471 backbutton = gtk_button_new ();
472 gtk_container_add (GTK_CONTAINER (backbutton),
473 gtk_image_new_from_stock (GTK_STOCK_GO_BACK, 4));
474 gtk_size_group_add_widget (group, backbutton);
475 entry = gtk_entry_new ();
476 gtk_size_group_add_widget (group, entry);
477 applybutton = gtk_button_new ();
478 gtk_size_group_add_widget (group, applybutton);
479 gtk_container_add (GTK_CONTAINER (applybutton),
480 gtk_image_new_from_stock (GTK_STOCK_APPLY, 4));
482 gtk_container_add (GTK_CONTAINER (window), vbox);
483 gtk_box_pack_start (GTK_BOX (vbox), bin, TRUE, TRUE, 0);
484 gtk_container_add (GTK_CONTAINER (bin), hbox);
485 gtk_box_pack_start (GTK_BOX (hbox), backbutton, FALSE, FALSE, 0);
486 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
487 gtk_box_pack_start (GTK_BOX (hbox), applybutton, FALSE, FALSE, 0);
490 if (!gtk_widget_get_visible (window))
491 gtk_widget_show_all (window);
494 gtk_widget_destroy (window);