]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/offscreen_window2.c
Merge branch 'master' into broadway
[~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_get_preferred_width  (GtkWidget *widget,
39                                                      gint      *minimum,
40                                                      gint      *natural);
41 static void     gtk_mirror_bin_get_preferred_height (GtkWidget *widget,
42                                                      gint      *minimum,
43                                                      gint      *natural);
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,
49                                               cairo_t         *cr);
50
51 static void     gtk_mirror_bin_add           (GtkContainer    *container,
52                                                GtkWidget       *child);
53 static void     gtk_mirror_bin_remove        (GtkContainer    *container,
54                                                GtkWidget       *widget);
55 static void     gtk_mirror_bin_forall        (GtkContainer    *container,
56                                                gboolean         include_internals,
57                                                GtkCallback      callback,
58                                                gpointer         callback_data);
59 static GType    gtk_mirror_bin_child_type    (GtkContainer    *container);
60
61 G_DEFINE_TYPE (GtkMirrorBin, gtk_mirror_bin, GTK_TYPE_CONTAINER);
62
63 static void
64 to_child (GtkMirrorBin *bin,
65           double         widget_x,
66           double         widget_y,
67           double        *x_out,
68           double        *y_out)
69 {
70   *x_out = widget_x;
71   *y_out = widget_y;
72 }
73
74 static void
75 to_parent (GtkMirrorBin *bin,
76            double         offscreen_x,
77            double         offscreen_y,
78            double        *x_out,
79            double        *y_out)
80 {
81   *x_out = offscreen_x;
82   *y_out = offscreen_y;
83 }
84
85 static void
86 gtk_mirror_bin_class_init (GtkMirrorBinClass *klass)
87 {
88   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
89   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
90
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;
97
98   g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
99                                    GTK_TYPE_MIRROR_BIN,
100                                    g_cclosure_new (G_CALLBACK (gtk_mirror_bin_damage),
101                                                    NULL, NULL));
102
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;
107 }
108
109 static void
110 gtk_mirror_bin_init (GtkMirrorBin *bin)
111 {
112   gtk_widget_set_has_window (GTK_WIDGET (bin), TRUE);
113 }
114
115 GtkWidget *
116 gtk_mirror_bin_new (void)
117 {
118   return g_object_new (GTK_TYPE_MIRROR_BIN, NULL);
119 }
120
121 static GdkWindow *
122 pick_offscreen_child (GdkWindow     *offscreen_window,
123                       double         widget_x,
124                       double         widget_y,
125                       GtkMirrorBin *bin)
126 {
127  GtkAllocation child_area;
128  double x, y;
129
130  if (bin->child && gtk_widget_get_visible (bin->child))
131     {
132       to_child (bin, widget_x, widget_y, &x, &y);
133
134       gtk_widget_get_allocation (bin->child, &child_area);
135
136       if (x >= 0 && x < child_area.width &&
137           y >= 0 && y < child_area.height)
138         return bin->offscreen_window;
139     }
140
141   return NULL;
142 }
143
144 static void
145 offscreen_window_to_parent (GdkWindow     *offscreen_window,
146                             double         offscreen_x,
147                             double         offscreen_y,
148                             double        *parent_x,
149                             double        *parent_y,
150                             GtkMirrorBin *bin)
151 {
152   to_parent (bin, offscreen_x, offscreen_y, parent_x, parent_y);
153 }
154
155 static void
156 offscreen_window_from_parent (GdkWindow     *window,
157                               double         parent_x,
158                               double         parent_y,
159                               double        *offscreen_x,
160                               double        *offscreen_y,
161                               GtkMirrorBin *bin)
162 {
163   to_child (bin, parent_x, parent_y, offscreen_x, offscreen_y);
164 }
165
166 static void
167 gtk_mirror_bin_realize (GtkWidget *widget)
168 {
169   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
170   GtkAllocation allocation;
171   GtkStyleContext *context;
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 (gtk_widget_get_root_window (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   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);
236 }
237
238 static void
239 gtk_mirror_bin_unrealize (GtkWidget *widget)
240 {
241   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
242
243   gdk_window_set_user_data (bin->offscreen_window, NULL);
244   gdk_window_destroy (bin->offscreen_window);
245   bin->offscreen_window = NULL;
246
247   GTK_WIDGET_CLASS (gtk_mirror_bin_parent_class)->unrealize (widget);
248 }
249
250 static GType
251 gtk_mirror_bin_child_type (GtkContainer *container)
252 {
253   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
254
255   if (bin->child)
256     return G_TYPE_NONE;
257
258   return GTK_TYPE_WIDGET;
259 }
260
261 static void
262 gtk_mirror_bin_add (GtkContainer *container,
263                      GtkWidget    *widget)
264 {
265   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
266
267   if (!bin->child)
268     {
269       gtk_widget_set_parent_window (widget, bin->offscreen_window);
270       gtk_widget_set_parent (widget, GTK_WIDGET (bin));
271       bin->child = widget;
272     }
273   else
274     g_warning ("GtkMirrorBin cannot have more than one child\n");
275 }
276
277 static void
278 gtk_mirror_bin_remove (GtkContainer *container,
279                         GtkWidget    *widget)
280 {
281   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
282   gboolean was_visible;
283
284   was_visible = gtk_widget_get_visible (widget);
285
286   if (bin->child == widget)
287     {
288       gtk_widget_unparent (widget);
289
290       bin->child = NULL;
291
292       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
293         gtk_widget_queue_resize (GTK_WIDGET (container));
294     }
295 }
296
297 static void
298 gtk_mirror_bin_forall (GtkContainer *container,
299                         gboolean      include_internals,
300                         GtkCallback   callback,
301                         gpointer      callback_data)
302 {
303   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
304
305   g_return_if_fail (callback != NULL);
306
307   if (bin->child)
308     (*callback) (bin->child, callback_data);
309 }
310
311 static void
312 gtk_mirror_bin_size_request (GtkWidget      *widget,
313                               GtkRequisition *requisition)
314 {
315   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
316   GtkRequisition child_requisition;
317   guint border_width;
318
319   child_requisition.width = 0;
320   child_requisition.height = 0;
321
322   if (bin->child && gtk_widget_get_visible (bin->child))
323     gtk_widget_get_preferred_size ( (bin->child),
324                                &child_requisition, NULL);
325
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;
329 }
330
331 static void
332 gtk_mirror_bin_get_preferred_width (GtkWidget *widget,
333                                     gint      *minimum,
334                                     gint      *natural)
335 {
336   GtkRequisition requisition;
337
338   gtk_mirror_bin_size_request (widget, &requisition);
339
340   *minimum = *natural = requisition.width;
341 }
342
343 static void
344 gtk_mirror_bin_get_preferred_height (GtkWidget *widget,
345                                      gint      *minimum,
346                                      gint      *natural)
347 {
348   GtkRequisition requisition;
349
350   gtk_mirror_bin_size_request (widget, &requisition);
351
352   *minimum = *natural = requisition.width;
353 }
354
355 static void
356 gtk_mirror_bin_size_allocate (GtkWidget     *widget,
357                                GtkAllocation *allocation)
358 {
359   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
360   gint w, h;
361   guint border_width;
362
363   gtk_widget_set_allocation (widget, allocation);
364
365   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
366
367   w = allocation->width - border_width * 2;
368   h = allocation->height - border_width * 2;
369
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,
374                             w, h);
375
376   if (bin->child && gtk_widget_get_visible (bin->child))
377     {
378       GtkRequisition child_requisition;
379       GtkAllocation child_allocation;
380
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;
387
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);
394     }
395 }
396
397 static gboolean
398 gtk_mirror_bin_damage (GtkWidget      *widget,
399                         GdkEventExpose *event)
400 {
401   gdk_window_invalidate_rect (gtk_widget_get_window (widget),
402                               NULL, FALSE);
403
404   return TRUE;
405 }
406
407 static gboolean
408 gtk_mirror_bin_draw (GtkWidget *widget,
409                      cairo_t   *cr)
410 {
411   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
412   GdkWindow *window;
413
414   window = gtk_widget_get_window (widget);
415   if (gtk_cairo_should_draw_window (cr, window))
416     {
417       cairo_surface_t *surface;
418       cairo_matrix_t matrix;
419       cairo_pattern_t *mask;
420       int height;
421
422       if (bin->child && gtk_widget_get_visible (bin->child))
423         {
424           surface = gdk_offscreen_window_get_surface (bin->offscreen_window);
425           height = gdk_window_get_height (bin->offscreen_window);
426
427           /* paint the offscreen child */
428           cairo_set_source_surface (cr, surface, 0, 0);
429           cairo_paint (cr);
430
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);
435
436           cairo_set_source_surface (cr, surface, 0, height);
437
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);
445
446           /* paint the reflection */
447           cairo_mask (cr, mask);
448
449           cairo_pattern_destroy (mask);
450         }
451     }
452   else if (gtk_cairo_should_draw_window (cr, bin->offscreen_window))
453     {
454       gtk_render_background (gtk_widget_get_style_context (widget),
455                              cr,
456                              0, 0,
457                              gdk_window_get_width (bin->offscreen_window),
458                              gdk_window_get_height (bin->offscreen_window));
459
460       if (bin->child)
461         gtk_container_propagate_draw (GTK_CONTAINER (widget),
462                                       bin->child,
463                                       cr);
464     }
465
466   return FALSE;
467 }
468
469 /*** ***/
470
471 static GtkWidget *window = NULL;
472
473 GtkWidget *
474 do_offscreen_window2 (GtkWidget *do_widget)
475 {
476   if (!window)
477     {
478       GtkWidget *bin, *vbox;
479       GtkWidget *hbox, *entry, *applybutton, *backbutton;
480       GtkSizeGroup *group;
481
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");
486
487       g_signal_connect (window, "destroy",
488                         G_CALLBACK (gtk_widget_destroyed), &window);
489
490       gtk_container_set_border_width (GTK_CONTAINER (window), 10);
491
492       vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
493
494       bin = gtk_mirror_bin_new ();
495
496       group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
497
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));
509
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);
516     }
517
518   if (!gtk_widget_get_visible (window))
519     gtk_widget_show_all (window);
520   else
521     {
522       gtk_widget_destroy (window);
523       window = NULL;
524     }
525
526   return window;
527 }
528