]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/offscreen_window2.c
Merge branch 'master' into toolpalette
[~andy/gtk] / demos / gtk-demo / offscreen_window2.c
1 /* Offscreen windows/Effects
2  *
3  * Offscreen windows can be used to render elements multiple times to achieve
4  * various effects.
5  */
6 #include <gtk/gtk.h>
7
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))
14
15 typedef struct _GtkMirrorBin   GtkMirrorBin;
16 typedef struct _GtkMirrorBinClass  GtkMirrorBinClass;
17
18 struct _GtkMirrorBin
19 {
20   GtkContainer container;
21
22   GtkWidget *child;
23   GdkWindow *offscreen_window;
24 };
25
26 struct _GtkMirrorBinClass
27 {
28   GtkContainerClass parent_class;
29 };
30
31 GType      gtk_mirror_bin_get_type  (void) G_GNUC_CONST;
32 GtkWidget* gtk_mirror_bin_new       (void);
33
34 /*** implementation ***/
35
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);
46
47 static void     gtk_mirror_bin_add           (GtkContainer    *container,
48                                                GtkWidget       *child);
49 static void     gtk_mirror_bin_remove        (GtkContainer    *container,
50                                                GtkWidget       *widget);
51 static void     gtk_mirror_bin_forall        (GtkContainer    *container,
52                                                gboolean         include_internals,
53                                                GtkCallback      callback,
54                                                gpointer         callback_data);
55 static GType    gtk_mirror_bin_child_type    (GtkContainer    *container);
56
57 G_DEFINE_TYPE (GtkMirrorBin, gtk_mirror_bin, GTK_TYPE_CONTAINER);
58
59 static void
60 to_child (GtkMirrorBin *bin,
61           double         widget_x,
62           double         widget_y,
63           double        *x_out,
64           double        *y_out)
65 {
66   *x_out = widget_x;
67   *y_out = widget_y;
68 }
69
70 static void
71 to_parent (GtkMirrorBin *bin,
72            double         offscreen_x,
73            double         offscreen_y,
74            double        *x_out,
75            double        *y_out)
76 {
77   *x_out = offscreen_x;
78   *y_out = offscreen_y;
79 }
80
81 static void
82 gtk_mirror_bin_class_init (GtkMirrorBinClass *klass)
83 {
84   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
85   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
86
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;
92
93   g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
94                                    GTK_TYPE_MIRROR_BIN,
95                                    g_cclosure_new (G_CALLBACK (gtk_mirror_bin_damage),
96                                                    NULL, NULL));
97
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;
102 }
103
104 static void
105 gtk_mirror_bin_init (GtkMirrorBin *bin)
106 {
107   GTK_WIDGET_UNSET_FLAGS (bin, GTK_NO_WINDOW);
108 }
109
110 GtkWidget *
111 gtk_mirror_bin_new (void)
112 {
113   return g_object_new (GTK_TYPE_MIRROR_BIN, NULL);
114 }
115
116 static GdkWindow *
117 pick_offscreen_child (GdkWindow     *offscreen_window,
118                       double         widget_x,
119                       double         widget_y,
120                       GtkMirrorBin *bin)
121 {
122  GtkAllocation child_area;
123  double x, y;
124
125  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
126     {
127       to_child (bin, widget_x, widget_y, &x, &y);
128
129       child_area = bin->child->allocation;
130
131       if (x >= 0 && x < child_area.width &&
132           y >= 0 && y < child_area.height)
133         return bin->offscreen_window;
134     }
135
136   return NULL;
137 }
138
139 static void
140 offscreen_window_to_parent (GdkWindow     *offscreen_window,
141                             double         offscreen_x,
142                             double         offscreen_y,
143                             double        *parent_x,
144                             double        *parent_y,
145                             GtkMirrorBin *bin)
146 {
147   to_parent (bin, offscreen_x, offscreen_y, parent_x, parent_y);
148 }
149
150 static void
151 offscreen_window_from_parent (GdkWindow     *window,
152                               double         parent_x,
153                               double         parent_y,
154                               double        *offscreen_x,
155                               double        *offscreen_y,
156                               GtkMirrorBin *bin)
157 {
158   to_child (bin, parent_x, parent_y, offscreen_x, offscreen_y);
159 }
160
161 static void
162 gtk_mirror_bin_realize (GtkWidget *widget)
163 {
164   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
165   GdkWindowAttr attributes;
166   gint attributes_mask;
167   gint border_width;
168   GtkRequisition child_requisition;
169
170   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
171
172   border_width = GTK_CONTAINER (widget)->border_width;
173
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)
180                         | GDK_EXPOSURE_MASK
181                         | GDK_POINTER_MOTION_MASK
182                         | GDK_BUTTON_PRESS_MASK
183                         | GDK_BUTTON_RELEASE_MASK
184                         | GDK_SCROLL_MASK
185                         | GDK_ENTER_NOTIFY_MASK
186                         | GDK_LEAVE_NOTIFY_MASK;
187
188   attributes.visual = gtk_widget_get_visual (widget);
189   attributes.colormap = gtk_widget_get_colormap (widget);
190   attributes.wclass = GDK_INPUT_OUTPUT;
191
192   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
193
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);
199
200   attributes.window_type = GDK_WINDOW_OFFSCREEN;
201
202   child_requisition.width = child_requisition.height = 0;
203   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
204     {
205       attributes.width = bin->child->allocation.width;
206       attributes.height = bin->child->allocation.height;
207     }
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);
211   if (bin->child)
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);
218
219   widget->style = gtk_style_attach (widget->style, widget->window);
220
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);
224 }
225
226 static void
227 gtk_mirror_bin_unrealize (GtkWidget *widget)
228 {
229   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
230
231   gdk_window_set_user_data (bin->offscreen_window, NULL);
232   gdk_window_destroy (bin->offscreen_window);
233   bin->offscreen_window = NULL;
234
235   GTK_WIDGET_CLASS (gtk_mirror_bin_parent_class)->unrealize (widget);
236 }
237
238 static GType
239 gtk_mirror_bin_child_type (GtkContainer *container)
240 {
241   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
242
243   if (bin->child)
244     return G_TYPE_NONE;
245
246   return GTK_TYPE_WIDGET;
247 }
248
249 static void
250 gtk_mirror_bin_add (GtkContainer *container,
251                      GtkWidget    *widget)
252 {
253   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
254
255   if (!bin->child)
256     {
257       gtk_widget_set_parent_window (widget, bin->offscreen_window);
258       gtk_widget_set_parent (widget, GTK_WIDGET (bin));
259       bin->child = widget;
260     }
261   else
262     g_warning ("GtkMirrorBin cannot have more than one child\n");
263 }
264
265 static void
266 gtk_mirror_bin_remove (GtkContainer *container,
267                         GtkWidget    *widget)
268 {
269   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
270   gboolean was_visible;
271
272   was_visible = GTK_WIDGET_VISIBLE (widget);
273
274   if (bin->child == widget)
275     {
276       gtk_widget_unparent (widget);
277
278       bin->child = NULL;
279
280       if (was_visible && GTK_WIDGET_VISIBLE (container))
281         gtk_widget_queue_resize (GTK_WIDGET (container));
282     }
283 }
284
285 static void
286 gtk_mirror_bin_forall (GtkContainer *container,
287                         gboolean      include_internals,
288                         GtkCallback   callback,
289                         gpointer      callback_data)
290 {
291   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
292
293   g_return_if_fail (callback != NULL);
294
295   if (bin->child)
296     (*callback) (bin->child, callback_data);
297 }
298
299 static void
300 gtk_mirror_bin_size_request (GtkWidget      *widget,
301                               GtkRequisition *requisition)
302 {
303   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
304   GtkRequisition child_requisition;
305
306   child_requisition.width = 0;
307   child_requisition.height = 0;
308
309   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
310     gtk_widget_size_request (bin->child, &child_requisition);
311
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;
314 }
315
316 static void
317 gtk_mirror_bin_size_allocate (GtkWidget     *widget,
318                                GtkAllocation *allocation)
319 {
320   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
321   gint border_width;
322   gint w, h;
323   widget->allocation = *allocation;
324
325   border_width = GTK_CONTAINER (widget)->border_width;
326
327   w = allocation->width - border_width * 2;
328   h = allocation->height - border_width * 2;
329
330   if (GTK_WIDGET_REALIZED (widget))
331     gdk_window_move_resize (widget->window,
332                             allocation->x + border_width,
333                             allocation->y + border_width,
334                             w, h);
335
336   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
337     {
338       GtkRequisition child_requisition;
339       GtkAllocation child_allocation;
340
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;
346
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);
353     }
354 }
355
356 static gboolean
357 gtk_mirror_bin_damage (GtkWidget      *widget,
358                         GdkEventExpose *event)
359 {
360   gdk_window_invalidate_rect (widget->window, NULL, FALSE);
361
362   return TRUE;
363 }
364
365 static gboolean
366 gtk_mirror_bin_expose (GtkWidget      *widget,
367                         GdkEventExpose *event)
368 {
369   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
370   gint width, height;
371
372   if (GTK_WIDGET_DRAWABLE (widget))
373     {
374       if (event->window == widget->window)
375         {
376           GdkPixmap *pixmap;
377           cairo_t *cr;
378           cairo_matrix_t matrix;
379           cairo_pattern_t *mask;
380
381           if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
382             {
383               pixmap = gdk_offscreen_window_get_pixmap (bin->offscreen_window);
384               gdk_drawable_get_size (pixmap, &width, &height);
385
386               cr = gdk_cairo_create (widget->window);
387
388               cairo_save (cr);
389
390               cairo_rectangle (cr, 0, 0, width, height);
391               cairo_clip (cr);
392
393               /* paint the offscreen child */
394               gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
395               cairo_paint (cr);
396
397               cairo_restore (cr);
398
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);
403
404               cairo_rectangle (cr, 0, height, width, height);
405               cairo_clip (cr);
406
407               gdk_cairo_set_source_pixmap (cr, pixmap, 0, height);
408
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);
416
417               /* paint the reflection */
418               cairo_mask (cr, mask);
419
420               cairo_pattern_destroy (mask);
421               cairo_destroy (cr);
422             }
423         }
424       else if (event->window == bin->offscreen_window)
425         {
426           gtk_paint_flat_box (widget->style, event->window,
427                               GTK_STATE_NORMAL, GTK_SHADOW_NONE,
428                               &event->area, widget, "blah",
429                               0, 0, -1, -1);
430
431           if (bin->child)
432             gtk_container_propagate_expose (GTK_CONTAINER (widget),
433                                             bin->child,
434                                             event);
435         }
436     }
437
438   return FALSE;
439 }
440
441 /*** ***/
442
443 static GtkWidget *window = NULL;
444
445 GtkWidget *
446 do_offscreen_window2 (GtkWidget *do_widget)
447 {
448   if (!window)
449     {
450       GtkWidget *bin, *vbox;
451       GtkWidget *hbox, *entry, *applybutton, *backbutton;
452       GtkSizeGroup *group;
453
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");
458
459       g_signal_connect (window, "destroy",
460                         G_CALLBACK (gtk_widget_destroyed), &window);
461
462       gtk_container_set_border_width (GTK_CONTAINER (window), 10);
463
464       vbox = gtk_vbox_new (0, FALSE);
465
466       bin = gtk_mirror_bin_new ();
467
468       group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
469
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));
481
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);
488     }
489
490   if (!GTK_WIDGET_VISIBLE (window))
491     gtk_widget_show_all (window);
492   else
493     {
494       gtk_widget_destroy (window);
495       window = NULL;
496     }
497
498   return window;
499 }
500