]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/offscreen_window2.c
gtk-demo: Move resources into the tests they belong
[~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   context = gtk_widget_get_style_context (widget);
232   gtk_style_context_set_background (context, window);
233   gtk_style_context_set_background (context, bin->offscreen_window);
234   gdk_window_show (bin->offscreen_window);
235 }
236
237 static void
238 gtk_mirror_bin_unrealize (GtkWidget *widget)
239 {
240   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
241
242   gdk_window_set_user_data (bin->offscreen_window, NULL);
243   gdk_window_destroy (bin->offscreen_window);
244   bin->offscreen_window = NULL;
245
246   GTK_WIDGET_CLASS (gtk_mirror_bin_parent_class)->unrealize (widget);
247 }
248
249 static GType
250 gtk_mirror_bin_child_type (GtkContainer *container)
251 {
252   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
253
254   if (bin->child)
255     return G_TYPE_NONE;
256
257   return GTK_TYPE_WIDGET;
258 }
259
260 static void
261 gtk_mirror_bin_add (GtkContainer *container,
262                      GtkWidget    *widget)
263 {
264   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
265
266   if (!bin->child)
267     {
268       gtk_widget_set_parent_window (widget, bin->offscreen_window);
269       gtk_widget_set_parent (widget, GTK_WIDGET (bin));
270       bin->child = widget;
271     }
272   else
273     g_warning ("GtkMirrorBin cannot have more than one child\n");
274 }
275
276 static void
277 gtk_mirror_bin_remove (GtkContainer *container,
278                         GtkWidget    *widget)
279 {
280   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
281   gboolean was_visible;
282
283   was_visible = gtk_widget_get_visible (widget);
284
285   if (bin->child == widget)
286     {
287       gtk_widget_unparent (widget);
288
289       bin->child = NULL;
290
291       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
292         gtk_widget_queue_resize (GTK_WIDGET (container));
293     }
294 }
295
296 static void
297 gtk_mirror_bin_forall (GtkContainer *container,
298                         gboolean      include_internals,
299                         GtkCallback   callback,
300                         gpointer      callback_data)
301 {
302   GtkMirrorBin *bin = GTK_MIRROR_BIN (container);
303
304   g_return_if_fail (callback != NULL);
305
306   if (bin->child)
307     (*callback) (bin->child, callback_data);
308 }
309
310 static void
311 gtk_mirror_bin_size_request (GtkWidget      *widget,
312                               GtkRequisition *requisition)
313 {
314   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
315   GtkRequisition child_requisition;
316   guint border_width;
317
318   child_requisition.width = 0;
319   child_requisition.height = 0;
320
321   if (bin->child && gtk_widget_get_visible (bin->child))
322     gtk_widget_get_preferred_size ( (bin->child),
323                                &child_requisition, NULL);
324
325   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
326   requisition->width = border_width * 2 + child_requisition.width + 10;
327   requisition->height = border_width * 2 + child_requisition.height * 2 + 10;
328 }
329
330 static void
331 gtk_mirror_bin_get_preferred_width (GtkWidget *widget,
332                                     gint      *minimum,
333                                     gint      *natural)
334 {
335   GtkRequisition requisition;
336
337   gtk_mirror_bin_size_request (widget, &requisition);
338
339   *minimum = *natural = requisition.width;
340 }
341
342 static void
343 gtk_mirror_bin_get_preferred_height (GtkWidget *widget,
344                                      gint      *minimum,
345                                      gint      *natural)
346 {
347   GtkRequisition requisition;
348
349   gtk_mirror_bin_size_request (widget, &requisition);
350
351   *minimum = *natural = requisition.width;
352 }
353
354 static void
355 gtk_mirror_bin_size_allocate (GtkWidget     *widget,
356                                GtkAllocation *allocation)
357 {
358   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
359   gint w, h;
360   guint border_width;
361
362   gtk_widget_set_allocation (widget, allocation);
363
364   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
365
366   w = allocation->width - border_width * 2;
367   h = allocation->height - border_width * 2;
368
369   if (gtk_widget_get_realized (widget))
370     gdk_window_move_resize (gtk_widget_get_window (widget),
371                             allocation->x + border_width,
372                             allocation->y + border_width,
373                             w, h);
374
375   if (bin->child && gtk_widget_get_visible (bin->child))
376     {
377       GtkRequisition child_requisition;
378       GtkAllocation child_allocation;
379
380       gtk_widget_get_preferred_size (bin->child,
381                                      &child_requisition, NULL);
382       child_allocation.x = 0;
383       child_allocation.y = 0;
384       child_allocation.height = child_requisition.height;
385       child_allocation.width = child_requisition.width;
386
387       if (gtk_widget_get_realized (widget))
388         gdk_window_move_resize (bin->offscreen_window,
389                                 allocation->x + border_width,
390                                 allocation->y + border_width,
391                                 child_allocation.width, child_allocation.height);
392       gtk_widget_size_allocate (bin->child, &child_allocation);
393     }
394 }
395
396 static gboolean
397 gtk_mirror_bin_damage (GtkWidget      *widget,
398                         GdkEventExpose *event)
399 {
400   gdk_window_invalidate_rect (gtk_widget_get_window (widget),
401                               NULL, FALSE);
402
403   return TRUE;
404 }
405
406 static gboolean
407 gtk_mirror_bin_draw (GtkWidget *widget,
408                      cairo_t   *cr)
409 {
410   GtkMirrorBin *bin = GTK_MIRROR_BIN (widget);
411   GdkWindow *window;
412
413   window = gtk_widget_get_window (widget);
414   if (gtk_cairo_should_draw_window (cr, window))
415     {
416       cairo_surface_t *surface;
417       cairo_matrix_t matrix;
418       cairo_pattern_t *mask;
419       int height;
420
421       if (bin->child && gtk_widget_get_visible (bin->child))
422         {
423           surface = gdk_offscreen_window_get_surface (bin->offscreen_window);
424           height = gdk_window_get_height (bin->offscreen_window);
425
426           /* paint the offscreen child */
427           cairo_set_source_surface (cr, surface, 0, 0);
428           cairo_paint (cr);
429
430           cairo_matrix_init (&matrix, 1.0, 0.0, 0.3, 1.0, 0.0, 0.0);
431           cairo_matrix_scale (&matrix, 1.0, -1.0);
432           cairo_matrix_translate (&matrix, -10, - 3 * height - 10);
433           cairo_transform (cr, &matrix);
434
435           cairo_set_source_surface (cr, surface, 0, height);
436
437           /* create linear gradient as mask-pattern to fade out the source */
438           mask = cairo_pattern_create_linear (0.0, height, 0.0, 2*height);
439           cairo_pattern_add_color_stop_rgba (mask, 0.0,  0.0, 0.0, 0.0, 0.0);
440           cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.01);
441           cairo_pattern_add_color_stop_rgba (mask, 0.5,  0.0, 0.0, 0.0, 0.25);
442           cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.5);
443           cairo_pattern_add_color_stop_rgba (mask, 1.0,  0.0, 0.0, 0.0, 1.0);
444
445           /* paint the reflection */
446           cairo_mask (cr, mask);
447
448           cairo_pattern_destroy (mask);
449         }
450     }
451   else if (gtk_cairo_should_draw_window (cr, bin->offscreen_window))
452     {
453       gtk_render_background (gtk_widget_get_style_context (widget),
454                              cr,
455                              0, 0,
456                              gdk_window_get_width (bin->offscreen_window),
457                              gdk_window_get_height (bin->offscreen_window));
458
459       if (bin->child)
460         gtk_container_propagate_draw (GTK_CONTAINER (widget),
461                                       bin->child,
462                                       cr);
463     }
464
465   return FALSE;
466 }
467
468 /*** ***/
469
470 static GtkWidget *window = NULL;
471
472 GtkWidget *
473 do_offscreen_window2 (GtkWidget *do_widget)
474 {
475   if (!window)
476     {
477       GtkWidget *bin, *vbox;
478       GtkWidget *hbox, *entry, *applybutton, *backbutton;
479       GtkSizeGroup *group;
480
481       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
482       gtk_window_set_screen (GTK_WINDOW (window),
483                              gtk_widget_get_screen (do_widget));
484       gtk_window_set_title (GTK_WINDOW (window), "Effects");
485
486       g_signal_connect (window, "destroy",
487                         G_CALLBACK (gtk_widget_destroyed), &window);
488
489       gtk_container_set_border_width (GTK_CONTAINER (window), 10);
490
491       vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
492
493       bin = gtk_mirror_bin_new ();
494
495       group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
496
497       hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
498       backbutton = gtk_button_new ();
499       gtk_container_add (GTK_CONTAINER (backbutton),
500                          gtk_image_new_from_stock (GTK_STOCK_GO_BACK, 4));
501       gtk_size_group_add_widget (group, backbutton);
502       entry = gtk_entry_new ();
503       gtk_size_group_add_widget (group, entry);
504       applybutton = gtk_button_new ();
505       gtk_size_group_add_widget (group, applybutton);
506       gtk_container_add (GTK_CONTAINER (applybutton),
507                          gtk_image_new_from_stock (GTK_STOCK_APPLY, 4));
508
509       gtk_container_add (GTK_CONTAINER (window), vbox);
510       gtk_box_pack_start (GTK_BOX (vbox), bin, TRUE, TRUE, 0);
511       gtk_container_add (GTK_CONTAINER (bin), hbox);
512       gtk_box_pack_start (GTK_BOX (hbox), backbutton, FALSE, FALSE, 0);
513       gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
514       gtk_box_pack_start (GTK_BOX (hbox), applybutton, FALSE, FALSE, 0);
515     }
516
517   if (!gtk_widget_get_visible (window))
518     gtk_widget_show_all (window);
519   else
520     {
521       gtk_widget_destroy (window);
522       window = NULL;
523     }
524
525   return window;
526 }