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_expose (GtkWidget *widget,
45 GdkEventExpose *offscreen);
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->expose_event = gtk_mirror_bin_expose;
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_UNSET_FLAGS (bin, GTK_NO_WINDOW);
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_VISIBLE (bin->child))
127 to_child (bin, widget_x, widget_y, &x, &y);
129 child_area = bin->child->allocation;
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 GdkWindowAttr attributes;
166 gint attributes_mask;
168 GtkRequisition child_requisition;
170 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
172 border_width = GTK_CONTAINER (widget)->border_width;
174 attributes.x = widget->allocation.x + border_width;
175 attributes.y = widget->allocation.y + border_width;
176 attributes.width = widget->allocation.width - 2 * border_width;
177 attributes.height = widget->allocation.height - 2 * border_width;
178 attributes.window_type = GDK_WINDOW_CHILD;
179 attributes.event_mask = gtk_widget_get_events (widget)
181 | GDK_POINTER_MOTION_MASK
182 | GDK_BUTTON_PRESS_MASK
183 | GDK_BUTTON_RELEASE_MASK
185 | GDK_ENTER_NOTIFY_MASK
186 | GDK_LEAVE_NOTIFY_MASK;
188 attributes.visual = gtk_widget_get_visual (widget);
189 attributes.colormap = gtk_widget_get_colormap (widget);
190 attributes.wclass = GDK_INPUT_OUTPUT;
192 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
194 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
195 &attributes, attributes_mask);
196 gdk_window_set_user_data (widget->window, widget);
197 g_signal_connect (widget->window, "pick-embedded-child",
198 G_CALLBACK (pick_offscreen_child), bin);
200 attributes.window_type = GDK_WINDOW_OFFSCREEN;
202 child_requisition.width = child_requisition.height = 0;
203 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
205 attributes.width = bin->child->allocation.width;
206 attributes.height = bin->child->allocation.height;
208 bin->offscreen_window = gdk_window_new (gtk_widget_get_root_window (widget),
209 &attributes, attributes_mask);
210 gdk_window_set_user_data (bin->offscreen_window, widget);
212 gtk_widget_set_parent_window (bin->child, bin->offscreen_window);
213 gdk_offscreen_window_set_embedder (bin->offscreen_window, widget->window);
214 g_signal_connect (bin->offscreen_window, "to-embedder",
215 G_CALLBACK (offscreen_window_to_parent), bin);
216 g_signal_connect (bin->offscreen_window, "from-embedder",
217 G_CALLBACK (offscreen_window_from_parent), bin);
219 widget->style = gtk_style_attach (widget->style, widget->window);
221 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
222 gtk_style_set_background (widget->style, bin->offscreen_window, GTK_STATE_NORMAL);
223 gdk_window_show (bin->offscreen_window);
227 gtk_mirror_bin_unrealize (GtkWidget *widget)
229 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
231 gdk_window_set_user_data (bin->offscreen_window, NULL);
232 gdk_window_destroy (bin->offscreen_window);
233 bin->offscreen_window = NULL;
235 GTK_WIDGET_CLASS (gtk_mirror_bin_parent_class)->unrealize (widget);
239 gtk_mirror_bin_child_type (GtkContainer *container)
241 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
246 return GTK_TYPE_WIDGET;
250 gtk_mirror_bin_add (GtkContainer *container,
253 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
257 gtk_widget_set_parent_window (widget, bin->offscreen_window);
258 gtk_widget_set_parent (widget, GTK_WIDGET (bin));
262 g_warning ("GtkMirrorBin cannot have more than one child\n");
266 gtk_mirror_bin_remove (GtkContainer *container,
269 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
270 gboolean was_visible;
272 was_visible = GTK_WIDGET_VISIBLE (widget);
274 if (bin->child == widget)
276 gtk_widget_unparent (widget);
280 if (was_visible && GTK_WIDGET_VISIBLE (container))
281 gtk_widget_queue_resize (GTK_WIDGET (container));
286 gtk_mirror_bin_forall (GtkContainer *container,
287 gboolean include_internals,
288 GtkCallback callback,
289 gpointer callback_data)
291 GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
293 g_return_if_fail (callback != NULL);
296 (*callback) (bin->child, callback_data);
300 gtk_mirror_bin_size_request (GtkWidget *widget,
301 GtkRequisition *requisition)
303 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
304 GtkRequisition child_requisition;
306 child_requisition.width = 0;
307 child_requisition.height = 0;
309 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
310 gtk_widget_size_request (bin->child, &child_requisition);
312 requisition->width = GTK_CONTAINER (widget)->border_width * 2 + child_requisition.width + 10;
313 requisition->height = GTK_CONTAINER (widget)->border_width * 2 + child_requisition.height * 2 + 10;
317 gtk_mirror_bin_size_allocate (GtkWidget *widget,
318 GtkAllocation *allocation)
320 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
323 widget->allocation = *allocation;
325 border_width = GTK_CONTAINER (widget)->border_width;
327 w = allocation->width - border_width * 2;
328 h = allocation->height - border_width * 2;
330 if (GTK_WIDGET_REALIZED (widget))
331 gdk_window_move_resize (widget->window,
332 allocation->x + border_width,
333 allocation->y + border_width,
336 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
338 GtkRequisition child_requisition;
339 GtkAllocation child_allocation;
341 gtk_widget_get_child_requisition (bin->child, &child_requisition);
342 child_allocation.x = 0;
343 child_allocation.y = 0;
344 child_allocation.height = child_requisition.height;
345 child_allocation.width = child_requisition.width;
347 if (GTK_WIDGET_REALIZED (widget))
348 gdk_window_move_resize (bin->offscreen_window,
349 allocation->x + border_width,
350 allocation->y + border_width,
351 child_allocation.width, child_allocation.height);
352 gtk_widget_size_allocate (bin->child, &child_allocation);
357 gtk_mirror_bin_damage (GtkWidget *widget,
358 GdkEventExpose *event)
360 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
366 gtk_mirror_bin_expose (GtkWidget *widget,
367 GdkEventExpose *event)
369 GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
372 if (GTK_WIDGET_DRAWABLE (widget))
374 if (event->window == widget->window)
378 cairo_matrix_t matrix;
379 cairo_pattern_t *mask;
381 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
383 pixmap = gdk_offscreen_window_get_pixmap (bin->offscreen_window);
384 gdk_drawable_get_size (pixmap, &width, &height);
386 cr = gdk_cairo_create (widget->window);
390 cairo_rectangle (cr, 0, 0, width, height);
393 /* paint the offscreen child */
394 gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
399 cairo_matrix_init (&matrix, 1.0, 0.0, 0.3, 1.0, 0.0, 0.0);
400 cairo_matrix_scale (&matrix, 1.0, -1.0);
401 cairo_matrix_translate (&matrix, -10, - 3 * height - 10);
402 cairo_transform (cr, &matrix);
404 cairo_rectangle (cr, 0, height, width, height);
407 gdk_cairo_set_source_pixmap (cr, pixmap, 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);
424 else if (event->window == bin->offscreen_window)
426 gtk_paint_flat_box (widget->style, event->window,
427 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
428 &event->area, widget, "blah",
432 gtk_container_propagate_expose (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_vbox_new (0, FALSE);
466 bin = gtk_mirror_bin_new ();
468 group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
470 hbox = gtk_hbox_new (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_VISIBLE (window))
491 gtk_widget_show_all (window);
494 gtk_widget_destroy (window);