]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/offscreen_window.c
gtk: remove "gboolean homogeneous" from gtk_box_new()
[~andy/gtk] / demos / gtk-demo / offscreen_window.c
1 /* Offscreen windows/Rotated button
2  *
3  * Offscreen windows can be used to transform parts of a widget
4  * hierarchy. Note that the rotated button is fully functional.
5  */
6 #include <math.h>
7 #include <gtk/gtk.h>
8
9 #define GTK_TYPE_ROTATED_BIN              (gtk_rotated_bin_get_type ())
10 #define GTK_ROTATED_BIN(obj)              (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ROTATED_BIN, GtkRotatedBin))
11 #define GTK_ROTATED_BIN_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_ROTATED_BIN, GtkRotatedBinClass))
12 #define GTK_IS_ROTATED_BIN(obj)           (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ROTATED_BIN))
13 #define GTK_IS_ROTATED_BIN_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_ROTATED_BIN))
14 #define GTK_ROTATED_BIN_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_ROTATED_BIN, GtkRotatedBinClass))
15
16 typedef struct _GtkRotatedBin   GtkRotatedBin;
17 typedef struct _GtkRotatedBinClass  GtkRotatedBinClass;
18
19 struct _GtkRotatedBin
20 {
21   GtkContainer container;
22
23   GtkWidget *child;
24   GdkWindow *offscreen_window;
25   gdouble angle;
26 };
27
28 struct _GtkRotatedBinClass
29 {
30   GtkContainerClass parent_class;
31 };
32
33 GType      gtk_rotated_bin_get_type  (void) G_GNUC_CONST;
34 GtkWidget* gtk_rotated_bin_new       (void);
35 void       gtk_rotated_bin_set_angle (GtkRotatedBin *bin,
36                                       gdouble        angle);
37
38 /*** implementation ***/
39
40 static void     gtk_rotated_bin_realize       (GtkWidget       *widget);
41 static void     gtk_rotated_bin_unrealize     (GtkWidget       *widget);
42 static void     gtk_rotated_bin_size_request  (GtkWidget       *widget,
43                                                GtkRequisition  *requisition);
44 static void     gtk_rotated_bin_size_allocate (GtkWidget       *widget,
45                                                GtkAllocation   *allocation);
46 static gboolean gtk_rotated_bin_damage        (GtkWidget       *widget,
47                                                GdkEventExpose  *event);
48 static gboolean gtk_rotated_bin_draw          (GtkWidget       *widget,
49                                                cairo_t         *cr);
50
51 static void     gtk_rotated_bin_add           (GtkContainer    *container,
52                                                GtkWidget       *child);
53 static void     gtk_rotated_bin_remove        (GtkContainer    *container,
54                                                GtkWidget       *widget);
55 static void     gtk_rotated_bin_forall        (GtkContainer    *container,
56                                                gboolean         include_internals,
57                                                GtkCallback      callback,
58                                                gpointer         callback_data);
59 static GType    gtk_rotated_bin_child_type    (GtkContainer    *container);
60
61 G_DEFINE_TYPE (GtkRotatedBin, gtk_rotated_bin, GTK_TYPE_CONTAINER);
62
63 static void
64 to_child (GtkRotatedBin *bin,
65           double         widget_x,
66           double         widget_y,
67           double        *x_out,
68           double        *y_out)
69 {
70   GtkAllocation child_area;
71   double x, y, xr, yr;
72   double c, s;
73   double w, h;
74
75   s = sin (bin->angle);
76   c = cos (bin->angle);
77   gtk_widget_get_allocation (bin->child, &child_area);
78
79   w = c * child_area.width + s * child_area.height;
80   h = s * child_area.width + c * child_area.height;
81
82   x = widget_x;
83   y = widget_y;
84
85   x -= (w - child_area.width) / 2;
86   y -= (h - child_area.height) / 2;
87
88   x -= child_area.width / 2;
89   y -= child_area.height / 2;
90
91   xr = x * c + y * s;
92   yr = y * c - x * s;
93   x = xr;
94   y = yr;
95
96   x += child_area.width / 2;
97   y += child_area.height / 2;
98
99   *x_out = x;
100   *y_out = y;
101 }
102
103 static void
104 to_parent (GtkRotatedBin *bin,
105            double         offscreen_x,
106            double         offscreen_y,
107            double        *x_out,
108            double        *y_out)
109 {
110   GtkAllocation child_area;
111   double x, y, xr, yr;
112   double c, s;
113   double w, h;
114
115   s = sin (bin->angle);
116   c = cos (bin->angle);
117   gtk_widget_get_allocation (bin->child, &child_area);
118
119   w = c * child_area.width + s * child_area.height;
120   h = s * child_area.width + c * child_area.height;
121
122   x = offscreen_x;
123   y = offscreen_y;
124
125   x -= child_area.width / 2;
126   y -= child_area.height / 2;
127
128   xr = x * c - y * s;
129   yr = x * s + y * c;
130   x = xr;
131   y = yr;
132
133   x += child_area.width / 2;
134   y += child_area.height / 2;
135
136   x -= (w - child_area.width) / 2;
137   y -= (h - child_area.height) / 2;
138
139   *x_out = x;
140   *y_out = y;
141 }
142
143 static void
144 gtk_rotated_bin_class_init (GtkRotatedBinClass *klass)
145 {
146   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
147   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
148
149   widget_class->realize = gtk_rotated_bin_realize;
150   widget_class->unrealize = gtk_rotated_bin_unrealize;
151   widget_class->size_request = gtk_rotated_bin_size_request;
152   widget_class->size_allocate = gtk_rotated_bin_size_allocate;
153   widget_class->draw = gtk_rotated_bin_draw;
154
155   g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
156                                    GTK_TYPE_ROTATED_BIN,
157                                    g_cclosure_new (G_CALLBACK (gtk_rotated_bin_damage),
158                                                    NULL, NULL));
159
160   container_class->add = gtk_rotated_bin_add;
161   container_class->remove = gtk_rotated_bin_remove;
162   container_class->forall = gtk_rotated_bin_forall;
163   container_class->child_type = gtk_rotated_bin_child_type;
164 }
165
166 static void
167 gtk_rotated_bin_init (GtkRotatedBin *bin)
168 {
169   gtk_widget_set_has_window (GTK_WIDGET (bin), TRUE);
170 }
171
172 GtkWidget *
173 gtk_rotated_bin_new (void)
174 {
175   return g_object_new (GTK_TYPE_ROTATED_BIN, NULL);
176 }
177
178 static GdkWindow *
179 pick_offscreen_child (GdkWindow     *offscreen_window,
180                       double         widget_x,
181                       double         widget_y,
182                       GtkRotatedBin *bin)
183 {
184  GtkAllocation child_area;
185  double x, y;
186
187  if (bin->child && gtk_widget_get_visible (bin->child))
188     {
189       to_child (bin, widget_x, widget_y, &x, &y);
190
191       gtk_widget_get_allocation (bin->child, &child_area);
192
193       if (x >= 0 && x < child_area.width &&
194           y >= 0 && y < child_area.height)
195         return bin->offscreen_window;
196     }
197
198   return NULL;
199 }
200
201 static void
202 offscreen_window_to_parent (GdkWindow     *offscreen_window,
203                             double         offscreen_x,
204                             double         offscreen_y,
205                             double        *parent_x,
206                             double        *parent_y,
207                             GtkRotatedBin *bin)
208 {
209   to_parent (bin, offscreen_x, offscreen_y, parent_x, parent_y);
210 }
211
212 static void
213 offscreen_window_from_parent (GdkWindow     *window,
214                               double         parent_x,
215                               double         parent_y,
216                               double        *offscreen_x,
217                               double        *offscreen_y,
218                               GtkRotatedBin *bin)
219 {
220   to_child (bin, parent_x, parent_y, offscreen_x, offscreen_y);
221 }
222
223 static void
224 gtk_rotated_bin_realize (GtkWidget *widget)
225 {
226   GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
227   GtkAllocation allocation;
228   GtkStyle *style;
229   GdkWindow *window;
230   GdkWindowAttr attributes;
231   gint attributes_mask;
232   guint border_width;
233   GtkRequisition child_requisition;
234
235   gtk_widget_set_realized (widget, TRUE);
236
237   gtk_widget_get_allocation (widget, &allocation);
238   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
239
240   attributes.x = allocation.x + border_width;
241   attributes.y = allocation.y + border_width;
242   attributes.width = allocation.width - 2 * border_width;
243   attributes.height = allocation.height - 2 * border_width;
244   attributes.window_type = GDK_WINDOW_CHILD;
245   attributes.event_mask = gtk_widget_get_events (widget)
246                         | GDK_EXPOSURE_MASK
247                         | GDK_POINTER_MOTION_MASK
248                         | GDK_BUTTON_PRESS_MASK
249                         | GDK_BUTTON_RELEASE_MASK
250                         | GDK_SCROLL_MASK
251                         | GDK_ENTER_NOTIFY_MASK
252                         | GDK_LEAVE_NOTIFY_MASK;
253
254   attributes.visual = gtk_widget_get_visual (widget);
255   attributes.wclass = GDK_INPUT_OUTPUT;
256
257   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
258
259   window = gdk_window_new (gtk_widget_get_parent_window (widget),
260                            &attributes, attributes_mask);
261   gtk_widget_set_window (widget, window);
262   gdk_window_set_user_data (window, widget);
263   g_signal_connect (window, "pick-embedded-child",
264                     G_CALLBACK (pick_offscreen_child), bin);
265
266   attributes.window_type = GDK_WINDOW_OFFSCREEN;
267
268   child_requisition.width = child_requisition.height = 0;
269   if (bin->child && gtk_widget_get_visible (bin->child))
270     {
271       GtkAllocation child_allocation;
272
273       gtk_widget_get_allocation (bin->child, &child_allocation);
274       attributes.width = child_allocation.width;
275       attributes.height = child_allocation.height;
276     }
277   bin->offscreen_window = gdk_window_new (gtk_widget_get_root_window (widget),
278                                           &attributes, attributes_mask);
279   gdk_window_set_user_data (bin->offscreen_window, widget);
280   if (bin->child)
281     gtk_widget_set_parent_window (bin->child, bin->offscreen_window);
282   gdk_offscreen_window_set_embedder (bin->offscreen_window, window);
283   g_signal_connect (bin->offscreen_window, "to-embedder",
284                     G_CALLBACK (offscreen_window_to_parent), bin);
285   g_signal_connect (bin->offscreen_window, "from-embedder",
286                     G_CALLBACK (offscreen_window_from_parent), bin);
287
288   gtk_widget_style_attach (widget);
289   style = gtk_widget_get_style (widget);
290   gtk_style_set_background (style, window, GTK_STATE_NORMAL);
291   gtk_style_set_background (style, bin->offscreen_window, GTK_STATE_NORMAL);
292   gdk_window_show (bin->offscreen_window);
293 }
294
295 static void
296 gtk_rotated_bin_unrealize (GtkWidget *widget)
297 {
298   GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
299
300   gdk_window_set_user_data (bin->offscreen_window, NULL);
301   gdk_window_destroy (bin->offscreen_window);
302   bin->offscreen_window = NULL;
303
304   GTK_WIDGET_CLASS (gtk_rotated_bin_parent_class)->unrealize (widget);
305 }
306
307 static GType
308 gtk_rotated_bin_child_type (GtkContainer *container)
309 {
310   GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
311
312   if (bin->child)
313     return G_TYPE_NONE;
314
315   return GTK_TYPE_WIDGET;
316 }
317
318 static void
319 gtk_rotated_bin_add (GtkContainer *container,
320                      GtkWidget    *widget)
321 {
322   GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
323
324   if (!bin->child)
325     {
326       gtk_widget_set_parent_window (widget, bin->offscreen_window);
327       gtk_widget_set_parent (widget, GTK_WIDGET (bin));
328       bin->child = widget;
329     }
330   else
331     g_warning ("GtkRotatedBin cannot have more than one child\n");
332 }
333
334 static void
335 gtk_rotated_bin_remove (GtkContainer *container,
336                         GtkWidget    *widget)
337 {
338   GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
339   gboolean was_visible;
340
341   was_visible = gtk_widget_get_visible (widget);
342
343   if (bin->child == widget)
344     {
345       gtk_widget_unparent (widget);
346
347       bin->child = NULL;
348
349       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
350         gtk_widget_queue_resize (GTK_WIDGET (container));
351     }
352 }
353
354 static void
355 gtk_rotated_bin_forall (GtkContainer *container,
356                         gboolean      include_internals,
357                         GtkCallback   callback,
358                         gpointer      callback_data)
359 {
360   GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
361
362   g_return_if_fail (callback != NULL);
363
364   if (bin->child)
365     (*callback) (bin->child, callback_data);
366 }
367
368 void
369 gtk_rotated_bin_set_angle (GtkRotatedBin *bin,
370                            gdouble        angle)
371 {
372   g_return_if_fail (GTK_IS_ROTATED_BIN (bin));
373
374   bin->angle = angle;
375   gtk_widget_queue_resize (GTK_WIDGET (bin));
376
377   gdk_window_geometry_changed (bin->offscreen_window);
378 }
379
380 static void
381 gtk_rotated_bin_size_request (GtkWidget      *widget,
382                               GtkRequisition *requisition)
383 {
384   GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
385   GtkRequisition child_requisition;
386   double s, c;
387   double w, h;
388   guint border_width;
389
390   child_requisition.width = 0;
391   child_requisition.height = 0;
392
393   if (bin->child && gtk_widget_get_visible (bin->child))
394     gtk_widget_get_preferred_size ( (bin->child),
395                                &child_requisition, NULL);
396
397   s = sin (bin->angle);
398   c = cos (bin->angle);
399   w = c * child_requisition.width + s * child_requisition.height;
400   h = s * child_requisition.width + c * child_requisition.height;
401
402   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
403   requisition->width = border_width * 2 + w;
404   requisition->height = border_width * 2 + h;
405 }
406
407 static void
408 gtk_rotated_bin_size_allocate (GtkWidget     *widget,
409                                GtkAllocation *allocation)
410 {
411   GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
412   guint border_width;
413   gint w, h;
414   gdouble s, c;
415
416   gtk_widget_set_allocation (widget, allocation);
417
418   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
419
420   w = allocation->width - border_width * 2;
421   h = allocation->height - border_width * 2;
422
423   if (gtk_widget_get_realized (widget))
424     gdk_window_move_resize (gtk_widget_get_window (widget),
425                             allocation->x + border_width,
426                             allocation->y + border_width,
427                             w, h);
428
429   if (bin->child && gtk_widget_get_visible (bin->child))
430     {
431       GtkRequisition child_requisition;
432       GtkAllocation child_allocation;
433
434       s = sin (bin->angle);
435       c = cos (bin->angle);
436
437       gtk_widget_get_preferred_size (bin->child,
438                                      &child_requisition, NULL);
439       child_allocation.x = 0;
440       child_allocation.y = 0;
441       child_allocation.height = child_requisition.height;
442       if (c == 0.0)
443         child_allocation.width = h / s;
444       else if (s == 0.0)
445         child_allocation.width = w / c;
446       else
447         child_allocation.width = MIN ((w - s * child_allocation.height) / c,
448                                       (h - c * child_allocation.height) / s);
449
450       if (gtk_widget_get_realized (widget))
451         gdk_window_move_resize (bin->offscreen_window,
452                                 child_allocation.x,
453                                 child_allocation.y,
454                                 child_allocation.width,
455                                 child_allocation.height);
456
457       child_allocation.x = child_allocation.y = 0;
458       gtk_widget_size_allocate (bin->child, &child_allocation);
459     }
460 }
461
462 static gboolean
463 gtk_rotated_bin_damage (GtkWidget      *widget,
464                         GdkEventExpose *event)
465 {
466   gdk_window_invalidate_rect (gtk_widget_get_window (widget),
467                               NULL, FALSE);
468
469   return TRUE;
470 }
471
472 static gboolean
473 gtk_rotated_bin_draw (GtkWidget *widget,
474                       cairo_t   *cr)
475 {
476   GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
477   GdkWindow *window;
478   gdouble s, c;
479   gdouble w, h;
480
481   window = gtk_widget_get_window (widget);
482   if (gtk_cairo_should_draw_window (cr, window))
483     {
484       cairo_surface_t *surface;
485       GtkAllocation child_area;
486
487       if (bin->child && gtk_widget_get_visible (bin->child))
488         {
489           surface = gdk_offscreen_window_get_surface (bin->offscreen_window);
490           gtk_widget_get_allocation (bin->child, &child_area);
491
492           /* transform */
493           s = sin (bin->angle);
494           c = cos (bin->angle);
495           w = c * child_area.width + s * child_area.height;
496           h = s * child_area.width + c * child_area.height;
497
498           cairo_translate (cr, (w - child_area.width) / 2, (h - child_area.height) / 2);
499           cairo_translate (cr, child_area.width / 2, child_area.height / 2);
500           cairo_rotate (cr, bin->angle);
501           cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
502
503           /* clip */
504           cairo_rectangle (cr,
505                            0, 0,
506                            gdk_window_get_width (bin->offscreen_window),
507                            gdk_window_get_height (bin->offscreen_window));
508           cairo_clip (cr);
509           /* paint */
510           cairo_set_source_surface (cr, surface, 0, 0);
511           cairo_paint (cr);
512         }
513     }
514   if (gtk_cairo_should_draw_window (cr, bin->offscreen_window))
515     {
516       gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
517                           GTK_STATE_NORMAL, GTK_SHADOW_NONE,
518                           widget, "blah",
519                           0, 0,
520                           gdk_window_get_width (bin->offscreen_window),
521                           gdk_window_get_height (bin->offscreen_window));
522
523       if (bin->child)
524         gtk_container_propagate_draw (GTK_CONTAINER (widget),
525                                       bin->child,
526                                       cr);
527     }
528
529   return FALSE;
530 }
531
532 /*** ***/
533
534 static void
535 scale_changed (GtkRange      *range,
536                GtkRotatedBin *bin)
537 {
538   gtk_rotated_bin_set_angle (bin, gtk_range_get_value (range));
539 }
540
541 static GtkWidget *window = NULL;
542
543 GtkWidget *
544 do_offscreen_window (GtkWidget *do_widget)
545 {
546   if (!window)
547     {
548       GtkWidget *bin, *vbox, *scale, *button;
549       GdkColor black;
550
551       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
552       gtk_window_set_screen (GTK_WINDOW (window),
553                              gtk_widget_get_screen (do_widget));
554       gtk_window_set_title (GTK_WINDOW (window), "Rotated widget");
555
556       g_signal_connect (window, "destroy",
557                         G_CALLBACK (gtk_widget_destroyed), &window);
558
559       gdk_color_parse ("black", &black);
560       gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &black);
561       gtk_container_set_border_width (GTK_CONTAINER (window), 10);
562
563       vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
564       scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
565                                         0, G_PI/2, 0.01);
566       gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
567
568       button = gtk_button_new_with_label ("A Button");
569       bin = gtk_rotated_bin_new ();
570
571       g_signal_connect (scale, "value-changed", G_CALLBACK (scale_changed), bin);
572
573       gtk_container_add (GTK_CONTAINER (window), vbox);
574       gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 0);
575       gtk_box_pack_start (GTK_BOX (vbox), bin, TRUE, TRUE, 0);
576       gtk_container_add (GTK_CONTAINER (bin), button);
577     }
578
579   if (!gtk_widget_get_visible (window))
580     gtk_widget_show_all (window);
581   else
582     {
583       gtk_widget_destroy (window);
584       window = NULL;
585     }
586
587   return window;
588 }
589