1 /* Offscreen windows/Rotated button
3 * Offscreen windows can be used to transform parts of a widget
4 * hierarchy. Note that the rotated button is fully functional.
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))
16 typedef struct _GtkRotatedBin GtkRotatedBin;
17 typedef struct _GtkRotatedBinClass GtkRotatedBinClass;
21 GtkContainer container;
24 GdkWindow *offscreen_window;
28 struct _GtkRotatedBinClass
30 GtkContainerClass parent_class;
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,
38 /*** implementation ***/
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_expose (GtkWidget *widget,
49 GdkEventExpose *offscreen);
51 static void gtk_rotated_bin_add (GtkContainer *container,
53 static void gtk_rotated_bin_remove (GtkContainer *container,
55 static void gtk_rotated_bin_forall (GtkContainer *container,
56 gboolean include_internals,
58 gpointer callback_data);
59 static GType gtk_rotated_bin_child_type (GtkContainer *container);
61 G_DEFINE_TYPE (GtkRotatedBin, gtk_rotated_bin, GTK_TYPE_CONTAINER);
64 to_child (GtkRotatedBin *bin,
70 GtkAllocation child_area;
77 gtk_widget_get_allocation (bin->child, &child_area);
79 w = c * child_area.width + s * child_area.height;
80 h = s * child_area.width + c * child_area.height;
85 x -= (w - child_area.width) / 2;
86 y -= (h - child_area.height) / 2;
88 x -= child_area.width / 2;
89 y -= child_area.height / 2;
96 x += child_area.width / 2;
97 y += child_area.height / 2;
104 to_parent (GtkRotatedBin *bin,
110 GtkAllocation child_area;
115 s = sin (bin->angle);
116 c = cos (bin->angle);
117 gtk_widget_get_allocation (bin->child, &child_area);
119 w = c * child_area.width + s * child_area.height;
120 h = s * child_area.width + c * child_area.height;
125 x -= child_area.width / 2;
126 y -= child_area.height / 2;
133 x += child_area.width / 2;
134 y += child_area.height / 2;
136 x -= (w - child_area.width) / 2;
137 y -= (h - child_area.height) / 2;
144 gtk_rotated_bin_class_init (GtkRotatedBinClass *klass)
146 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
147 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
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->expose_event = gtk_rotated_bin_expose;
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),
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;
167 gtk_rotated_bin_init (GtkRotatedBin *bin)
169 gtk_widget_set_has_window (GTK_WIDGET (bin), TRUE);
173 gtk_rotated_bin_new (void)
175 return g_object_new (GTK_TYPE_ROTATED_BIN, NULL);
179 pick_offscreen_child (GdkWindow *offscreen_window,
184 GtkAllocation child_area;
187 if (bin->child && gtk_widget_get_visible (bin->child))
189 to_child (bin, widget_x, widget_y, &x, &y);
191 gtk_widget_get_allocation (bin->child, &child_area);
193 if (x >= 0 && x < child_area.width &&
194 y >= 0 && y < child_area.height)
195 return bin->offscreen_window;
202 offscreen_window_to_parent (GdkWindow *offscreen_window,
209 to_parent (bin, offscreen_x, offscreen_y, parent_x, parent_y);
213 offscreen_window_from_parent (GdkWindow *window,
220 to_child (bin, parent_x, parent_y, offscreen_x, offscreen_y);
224 gtk_rotated_bin_realize (GtkWidget *widget)
226 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
227 GtkAllocation allocation;
230 GdkWindowAttr attributes;
231 gint attributes_mask;
233 GtkRequisition child_requisition;
235 gtk_widget_set_realized (widget, TRUE);
237 gtk_widget_get_allocation (widget, &allocation);
238 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
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)
247 | GDK_POINTER_MOTION_MASK
248 | GDK_BUTTON_PRESS_MASK
249 | GDK_BUTTON_RELEASE_MASK
251 | GDK_ENTER_NOTIFY_MASK
252 | GDK_LEAVE_NOTIFY_MASK;
254 attributes.visual = gtk_widget_get_visual (widget);
255 attributes.colormap = gtk_widget_get_colormap (widget);
256 attributes.wclass = GDK_INPUT_OUTPUT;
258 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
260 window = gdk_window_new (gtk_widget_get_parent_window (widget),
261 &attributes, attributes_mask);
262 gtk_widget_set_window (widget, window);
263 gdk_window_set_user_data (window, widget);
264 g_signal_connect (window, "pick-embedded-child",
265 G_CALLBACK (pick_offscreen_child), bin);
267 attributes.window_type = GDK_WINDOW_OFFSCREEN;
269 child_requisition.width = child_requisition.height = 0;
270 if (bin->child && gtk_widget_get_visible (bin->child))
272 GtkAllocation child_allocation;
274 gtk_widget_get_allocation (bin->child, &child_allocation);
275 attributes.width = child_allocation.width;
276 attributes.height = child_allocation.height;
278 bin->offscreen_window = gdk_window_new (gtk_widget_get_root_window (widget),
279 &attributes, attributes_mask);
280 gdk_window_set_user_data (bin->offscreen_window, widget);
282 gtk_widget_set_parent_window (bin->child, bin->offscreen_window);
283 gdk_offscreen_window_set_embedder (bin->offscreen_window, window);
284 g_signal_connect (bin->offscreen_window, "to-embedder",
285 G_CALLBACK (offscreen_window_to_parent), bin);
286 g_signal_connect (bin->offscreen_window, "from-embedder",
287 G_CALLBACK (offscreen_window_from_parent), bin);
289 gtk_widget_style_attach (widget);
290 style = gtk_widget_get_style (widget);
291 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
292 gtk_style_set_background (style, bin->offscreen_window, GTK_STATE_NORMAL);
293 gdk_window_show (bin->offscreen_window);
297 gtk_rotated_bin_unrealize (GtkWidget *widget)
299 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
301 gdk_window_set_user_data (bin->offscreen_window, NULL);
302 gdk_window_destroy (bin->offscreen_window);
303 bin->offscreen_window = NULL;
305 GTK_WIDGET_CLASS (gtk_rotated_bin_parent_class)->unrealize (widget);
309 gtk_rotated_bin_child_type (GtkContainer *container)
311 GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
316 return GTK_TYPE_WIDGET;
320 gtk_rotated_bin_add (GtkContainer *container,
323 GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
327 gtk_widget_set_parent_window (widget, bin->offscreen_window);
328 gtk_widget_set_parent (widget, GTK_WIDGET (bin));
332 g_warning ("GtkRotatedBin cannot have more than one child\n");
336 gtk_rotated_bin_remove (GtkContainer *container,
339 GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
340 gboolean was_visible;
342 was_visible = gtk_widget_get_visible (widget);
344 if (bin->child == widget)
346 gtk_widget_unparent (widget);
350 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
351 gtk_widget_queue_resize (GTK_WIDGET (container));
356 gtk_rotated_bin_forall (GtkContainer *container,
357 gboolean include_internals,
358 GtkCallback callback,
359 gpointer callback_data)
361 GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
363 g_return_if_fail (callback != NULL);
366 (*callback) (bin->child, callback_data);
370 gtk_rotated_bin_set_angle (GtkRotatedBin *bin,
373 g_return_if_fail (GTK_IS_ROTATED_BIN (bin));
376 gtk_widget_queue_resize (GTK_WIDGET (bin));
378 gdk_window_geometry_changed (bin->offscreen_window);
382 gtk_rotated_bin_size_request (GtkWidget *widget,
383 GtkRequisition *requisition)
385 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
386 GtkRequisition child_requisition;
391 child_requisition.width = 0;
392 child_requisition.height = 0;
394 if (bin->child && gtk_widget_get_visible (bin->child))
395 gtk_size_request_get_size (GTK_SIZE_REQUEST (bin->child),
396 &child_requisition, NULL);
398 s = sin (bin->angle);
399 c = cos (bin->angle);
400 w = c * child_requisition.width + s * child_requisition.height;
401 h = s * child_requisition.width + c * child_requisition.height;
403 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
404 requisition->width = border_width * 2 + w;
405 requisition->height = border_width * 2 + h;
409 gtk_rotated_bin_size_allocate (GtkWidget *widget,
410 GtkAllocation *allocation)
412 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
417 gtk_widget_set_allocation (widget, allocation);
419 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
421 w = allocation->width - border_width * 2;
422 h = allocation->height - border_width * 2;
424 if (gtk_widget_get_realized (widget))
425 gdk_window_move_resize (gtk_widget_get_window (widget),
426 allocation->x + border_width,
427 allocation->y + border_width,
430 if (bin->child && gtk_widget_get_visible (bin->child))
432 GtkRequisition child_requisition;
433 GtkAllocation child_allocation;
435 s = sin (bin->angle);
436 c = cos (bin->angle);
438 gtk_size_request_get_size (GTK_SIZE_REQUEST (bin->child),
439 &child_requisition, NULL);
440 child_allocation.x = 0;
441 child_allocation.y = 0;
442 child_allocation.height = child_requisition.height;
444 child_allocation.width = h / s;
446 child_allocation.width = w / c;
448 child_allocation.width = MIN ((w - s * child_allocation.height) / c,
449 (h - c * child_allocation.height) / s);
451 if (gtk_widget_get_realized (widget))
452 gdk_window_move_resize (bin->offscreen_window,
455 child_allocation.width,
456 child_allocation.height);
458 child_allocation.x = child_allocation.y = 0;
459 gtk_widget_size_allocate (bin->child, &child_allocation);
464 gtk_rotated_bin_damage (GtkWidget *widget,
465 GdkEventExpose *event)
467 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
474 gtk_rotated_bin_expose (GtkWidget *widget,
475 GdkEventExpose *event)
477 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
483 if (gtk_widget_is_drawable (widget))
485 window = gtk_widget_get_window (widget);
486 if (event->window == window)
488 cairo_surface_t *surface;
489 GtkAllocation child_area;
492 if (bin->child && gtk_widget_get_visible (bin->child))
494 surface = gdk_offscreen_window_get_surface (bin->offscreen_window);
495 gtk_widget_get_allocation (bin->child, &child_area);
497 cr = gdk_cairo_create (window);
500 s = sin (bin->angle);
501 c = cos (bin->angle);
502 w = c * child_area.width + s * child_area.height;
503 h = s * child_area.width + c * child_area.height;
505 cairo_translate (cr, (w - child_area.width) / 2, (h - child_area.height) / 2);
506 cairo_translate (cr, child_area.width / 2, child_area.height / 2);
507 cairo_rotate (cr, bin->angle);
508 cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
511 gdk_drawable_get_size (bin->offscreen_window, &width, &height);
512 cairo_rectangle (cr, 0, 0, width, height);
515 cairo_set_source_surface (cr, surface, 0, 0);
521 else if (event->window == bin->offscreen_window)
523 gtk_paint_flat_box (gtk_widget_get_style (widget), event->window,
524 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
525 &event->area, widget, "blah",
529 gtk_container_propagate_expose (GTK_CONTAINER (widget),
541 scale_changed (GtkRange *range,
544 gtk_rotated_bin_set_angle (bin, gtk_range_get_value (range));
547 static GtkWidget *window = NULL;
550 do_offscreen_window (GtkWidget *do_widget)
554 GtkWidget *bin, *vbox, *scale, *button;
557 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
558 gtk_window_set_screen (GTK_WINDOW (window),
559 gtk_widget_get_screen (do_widget));
560 gtk_window_set_title (GTK_WINDOW (window), "Rotated widget");
562 g_signal_connect (window, "destroy",
563 G_CALLBACK (gtk_widget_destroyed), &window);
565 gdk_color_parse ("black", &black);
566 gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &black);
567 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
569 vbox = gtk_vbox_new (0, FALSE);
570 scale = gtk_hscale_new_with_range (0, G_PI/2, 0.01);
571 gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
573 button = gtk_button_new_with_label ("A Button");
574 bin = gtk_rotated_bin_new ();
576 g_signal_connect (scale, "value-changed", G_CALLBACK (scale_changed), bin);
578 gtk_container_add (GTK_CONTAINER (window), vbox);
579 gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 0);
580 gtk_box_pack_start (GTK_BOX (vbox), bin, TRUE, TRUE, 0);
581 gtk_container_add (GTK_CONTAINER (bin), button);
584 if (!gtk_widget_get_visible (window))
585 gtk_widget_show_all (window);
588 gtk_widget_destroy (window);