Debian Codegraph @

demos/gtk-demo/offscreen_window.c

raw
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_get_preferred_width (GtkWidget *widget,
43 gint *minimum,
44 gint *natural);
45 static void gtk_rotated_bin_get_preferred_height (GtkWidget *widget,
46 gint *minimum,
47 gint *natural);
48 static void gtk_rotated_bin_size_allocate (GtkWidget *widget,
49 GtkAllocation *allocation);
50 static gboolean gtk_rotated_bin_damage (GtkWidget *widget,
51 GdkEventExpose *event);
52 static gboolean gtk_rotated_bin_draw (GtkWidget *widget,
53 cairo_t *cr);
54
55 static void gtk_rotated_bin_add (GtkContainer *container,
56 GtkWidget *child);
57 static void gtk_rotated_bin_remove (GtkContainer *container,
58 GtkWidget *widget);
59 static void gtk_rotated_bin_forall (GtkContainer *container,
60 gboolean include_internals,
61 GtkCallback callback,
62 gpointer callback_data);
63 static GType gtk_rotated_bin_child_type (GtkContainer *container);
64
65 G_DEFINE_TYPE (GtkRotatedBin, gtk_rotated_bin, GTK_TYPE_CONTAINER);
66
67 static void
68 to_child (GtkRotatedBin *bin,
69 double widget_x,
70 double widget_y,
71 double *x_out,
72 double *y_out)
73 {
74 GtkAllocation child_area;
75 double x, y, xr, yr;
76 double c, s;
77 double w, h;
78
79 s = sin (bin->angle);
80 c = cos (bin->angle);
81 gtk_widget_get_allocation (bin->child, &child_area);
82
83 w = c * child_area.width + s * child_area.height;
84 h = s * child_area.width + c * child_area.height;
85
86 x = widget_x;
87 y = widget_y;
88
89 x -= (w - child_area.width) / 2;
90 y -= (h - child_area.height) / 2;
91
92 x -= child_area.width / 2;
93 y -= child_area.height / 2;
94
95 xr = x * c + y * s;
96 yr = y * c - x * s;
97 x = xr;
98 y = yr;
99
100 x += child_area.width / 2;
101 y += child_area.height / 2;
102
103 *x_out = x;
104 *y_out = y;
105 }
106
107 static void
108 to_parent (GtkRotatedBin *bin,
109 double offscreen_x,
110 double offscreen_y,
111 double *x_out,
112 double *y_out)
113 {
114 GtkAllocation child_area;
115 double x, y, xr, yr;
116 double c, s;
117 double w, h;
118
119 s = sin (bin->angle);
120 c = cos (bin->angle);
121 gtk_widget_get_allocation (bin->child, &child_area);
122
123 w = c * child_area.width + s * child_area.height;
124 h = s * child_area.width + c * child_area.height;
125
126 x = offscreen_x;
127 y = offscreen_y;
128
129 x -= child_area.width / 2;
130 y -= child_area.height / 2;
131
132 xr = x * c - y * s;
133 yr = x * s + y * c;
134 x = xr;
135 y = yr;
136
137 x += child_area.width / 2;
138 y += child_area.height / 2;
139
140 x -= (w - child_area.width) / 2;
141 y -= (h - child_area.height) / 2;
142
143 *x_out = x;
144 *y_out = y;
145 }
146
147 static void
148 gtk_rotated_bin_class_init (GtkRotatedBinClass *klass)
149 {
150 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
151 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
152
153 widget_class->realize = gtk_rotated_bin_realize;
154 widget_class->unrealize = gtk_rotated_bin_unrealize;
155 widget_class->get_preferred_width = gtk_rotated_bin_get_preferred_width;
156 widget_class->get_preferred_height = gtk_rotated_bin_get_preferred_height;
157 widget_class->size_allocate = gtk_rotated_bin_size_allocate;
158 widget_class->draw = gtk_rotated_bin_draw;
159
160 g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
161 GTK_TYPE_ROTATED_BIN,
162 g_cclosure_new (G_CALLBACK (gtk_rotated_bin_damage),
163 NULL, NULL));
164
165 container_class->add = gtk_rotated_bin_add;
166 container_class->remove = gtk_rotated_bin_remove;
167 container_class->forall = gtk_rotated_bin_forall;
168 container_class->child_type = gtk_rotated_bin_child_type;
169 }
170
171 static void
172 gtk_rotated_bin_init (GtkRotatedBin *bin)
173 {
174 gtk_widget_set_has_window (GTK_WIDGET (bin), TRUE);
175 }
176
177 GtkWidget *
178 gtk_rotated_bin_new (void)
179 {
180 return g_object_new (GTK_TYPE_ROTATED_BIN, NULL);
181 }
182
183 static GdkWindow *
184 pick_offscreen_child (GdkWindow *offscreen_window,
185 double widget_x,
186 double widget_y,
187 GtkRotatedBin *bin)
188 {
189 GtkAllocation child_area;
190 double x, y;
191
192 if (bin->child && gtk_widget_get_visible (bin->child))
193 {
194 to_child (bin, widget_x, widget_y, &x, &y);
195
196 gtk_widget_get_allocation (bin->child, &child_area);
197
198 if (x >= 0 && x < child_area.width &&
199 y >= 0 && y < child_area.height)
200 return bin->offscreen_window;
201 }
202
203 return NULL;
204 }
205
206 static void
207 offscreen_window_to_parent (GdkWindow *offscreen_window,
208 double offscreen_x,
209 double offscreen_y,
210 double *parent_x,
211 double *parent_y,
212 GtkRotatedBin *bin)
213 {
214 to_parent (bin, offscreen_x, offscreen_y, parent_x, parent_y);
215 }
216
217 static void
218 offscreen_window_from_parent (GdkWindow *window,
219 double parent_x,
220 double parent_y,
221 double *offscreen_x,
222 double *offscreen_y,
223 GtkRotatedBin *bin)
224 {
225 to_child (bin, parent_x, parent_y, offscreen_x, offscreen_y);
226 }
227
228 static void
229 gtk_rotated_bin_realize (GtkWidget *widget)
230 {
231 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
232 GtkAllocation allocation;
233 GdkWindow *window;
234 GdkWindowAttr attributes;
235 gint attributes_mask;
236 guint border_width;
237 GtkRequisition child_requisition;
238
239 gtk_widget_set_realized (widget, TRUE);
240
241 gtk_widget_get_allocation (widget, &allocation);
242 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
243
244 attributes.x = allocation.x + border_width;
245 attributes.y = allocation.y + border_width;
246 attributes.width = allocation.width - 2 * border_width;
247 attributes.height = allocation.height - 2 * border_width;
248 attributes.window_type = GDK_WINDOW_CHILD;
249 attributes.event_mask = gtk_widget_get_events (widget)
250 | GDK_EXPOSURE_MASK
251 | GDK_POINTER_MOTION_MASK
252 | GDK_BUTTON_PRESS_MASK
253 | GDK_BUTTON_RELEASE_MASK
254 | GDK_SCROLL_MASK
255 | GDK_ENTER_NOTIFY_MASK
256 | GDK_LEAVE_NOTIFY_MASK;
257
258 attributes.visual = gtk_widget_get_visual (widget);
259 attributes.wclass = GDK_INPUT_OUTPUT;
260
261 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
262
263 window = gdk_window_new (gtk_widget_get_parent_window (widget),
264 &attributes, attributes_mask);
265 gtk_widget_set_window (widget, window);
266 gdk_window_set_user_data (window, widget);
267 g_signal_connect (window, "pick-embedded-child",
268 G_CALLBACK (pick_offscreen_child), bin);
269
270 attributes.window_type = GDK_WINDOW_OFFSCREEN;
271
272 child_requisition.width = child_requisition.height = 0;
273 if (bin->child && gtk_widget_get_visible (bin->child))
274 {
275 GtkAllocation child_allocation;
276
277 gtk_widget_get_allocation (bin->child, &child_allocation);
278 attributes.width = child_allocation.width;
279 attributes.height = child_allocation.height;
280 }
281 bin->offscreen_window = gdk_window_new (gdk_screen_get_root_window (gtk_widget_get_screen (widget)),
282 &attributes, attributes_mask);
283 gdk_window_set_user_data (bin->offscreen_window, widget);
284 if (bin->child)
285 gtk_widget_set_parent_window (bin->child, bin->offscreen_window);
286 gdk_offscreen_window_set_embedder (bin->offscreen_window, window);
287 g_signal_connect (bin->offscreen_window, "to-embedder",
288 G_CALLBACK (offscreen_window_to_parent), bin);
289 g_signal_connect (bin->offscreen_window, "from-embedder",
290 G_CALLBACK (offscreen_window_from_parent), bin);
291
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");
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_get_preferred_width (GtkWidget *widget,
409 gint *minimum,
410 gint *natural)
411 {
412 GtkRequisition requisition;
413
414 gtk_rotated_bin_size_request (widget, &requisition);
415
416 *minimum = *natural = requisition.width;
417 }
418
419 static void
420 gtk_rotated_bin_get_preferred_height (GtkWidget *widget,
421 gint *minimum,
422 gint *natural)
423 {
424 GtkRequisition requisition;
425
426 gtk_rotated_bin_size_request (widget, &requisition);
427
428 *minimum = *natural = requisition.height;
429 }
430
431 static void
432 gtk_rotated_bin_size_allocate (GtkWidget *widget,
433 GtkAllocation *allocation)
434 {
435 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
436 guint border_width;
437 gint w, h;
438 gdouble s, c;
439
440 gtk_widget_set_allocation (widget, allocation);
441
442 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
443
444 w = allocation->width - border_width * 2;
445 h = allocation->height - border_width * 2;
446
447 if (gtk_widget_get_realized (widget))
448 gdk_window_move_resize (gtk_widget_get_window (widget),
449 allocation->x + border_width,
450 allocation->y + border_width,
451 w, h);
452
453 if (bin->child && gtk_widget_get_visible (bin->child))
454 {
455 GtkRequisition child_requisition;
456 GtkAllocation child_allocation;
457
458 s = sin (bin->angle);
459 c = cos (bin->angle);
460
461 gtk_widget_get_preferred_size (bin->child,
462 &child_requisition, NULL);
463 child_allocation.x = 0;
464 child_allocation.y = 0;
465 child_allocation.height = child_requisition.height;
466 if (c == 0.0)
467 child_allocation.width = h / s;
468 else if (s == 0.0)
469 child_allocation.width = w / c;
470 else
471 child_allocation.width = MIN ((w - s * child_allocation.height) / c,
472 (h - c * child_allocation.height) / s);
473
474 if (gtk_widget_get_realized (widget))
475 gdk_window_move_resize (bin->offscreen_window,
476 child_allocation.x,
477 child_allocation.y,
478 child_allocation.width,
479 child_allocation.height);
480
481 child_allocation.x = child_allocation.y = 0;
482 gtk_widget_size_allocate (bin->child, &child_allocation);
483 }
484 }
485
486 static gboolean
487 gtk_rotated_bin_damage (GtkWidget *widget,
488 GdkEventExpose *event)
489 {
490 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
491 NULL, FALSE);
492
493 return TRUE;
494 }
495
496 static gboolean
497 gtk_rotated_bin_draw (GtkWidget *widget,
498 cairo_t *cr)
499 {
500 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
501 GdkWindow *window;
502 gdouble s, c;
503 gdouble w, h;
504
505 window = gtk_widget_get_window (widget);
506 if (gtk_cairo_should_draw_window (cr, window))
507 {
508 cairo_surface_t *surface;
509 GtkAllocation child_area;
510
511 if (bin->child && gtk_widget_get_visible (bin->child))
512 {
513 surface = gdk_offscreen_window_get_surface (bin->offscreen_window);
514 gtk_widget_get_allocation (bin->child, &child_area);
515
516 /* transform */
517 s = sin (bin->angle);
518 c = cos (bin->angle);
519 w = c * child_area.width + s * child_area.height;
520 h = s * child_area.width + c * child_area.height;
521
522 cairo_translate (cr, (w - child_area.width) / 2, (h - child_area.height) / 2);
523 cairo_translate (cr, child_area.width / 2, child_area.height / 2);
524 cairo_rotate (cr, bin->angle);
525 cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
526
527 /* clip */
528 cairo_rectangle (cr,
529 0, 0,
530 gdk_window_get_width (bin->offscreen_window),
531 gdk_window_get_height (bin->offscreen_window));
532 cairo_clip (cr);
533 /* paint */
534 cairo_set_source_surface (cr, surface, 0, 0);
535 cairo_paint (cr);
536 }
537 }
538 if (gtk_cairo_should_draw_window (cr, bin->offscreen_window))
539 {
540 gtk_render_background (gtk_widget_get_style_context (widget),
541 cr,
542 0, 0,
543 gdk_window_get_width (bin->offscreen_window),
544 gdk_window_get_height (bin->offscreen_window));
545
546 if (bin->child)
547 gtk_container_propagate_draw (GTK_CONTAINER (widget),
548 bin->child,
549 cr);
550 }
551
552 return FALSE;
553 }
554
555 /*** ***/
556
557 static void
558 scale_changed (GtkRange *range,
559 GtkRotatedBin *bin)
560 {
561 gtk_rotated_bin_set_angle (bin, gtk_range_get_value (range));
562 }
563
564 GtkWidget *
565 do_offscreen_window (GtkWidget *do_widget)
566 {
567 static GtkWidget *window = NULL;
568
569 if (!window)
570 {
571 GtkWidget *bin, *vbox, *scale, *button;
572
573 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
574 gtk_window_set_screen (GTK_WINDOW (window),
575 gtk_widget_get_screen (do_widget));
576 gtk_window_set_title (GTK_WINDOW (window), "Rotated Button");
577
578 g_signal_connect (window, "destroy",
579 G_CALLBACK (gtk_widget_destroyed), &window);
580
581 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
582
583 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
584 scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
585 0, G_PI/2, 0.01);
586 gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
587
588 button = gtk_button_new_with_label ("A Button");
589 bin = gtk_rotated_bin_new ();
590
591 g_signal_connect (scale, "value-changed", G_CALLBACK (scale_changed), bin);
592
593 gtk_container_add (GTK_CONTAINER (window), vbox);
594 gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 0);
595 gtk_box_pack_start (GTK_BOX (vbox), bin, TRUE, TRUE, 0);
596 gtk_container_add (GTK_CONTAINER (bin), button);
597 }
598
599 if (!gtk_widget_get_visible (window))
600 gtk_widget_show_all (window);
601 else
602 gtk_widget_destroy (window);
603
604 return window;
605 }