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 child_area = bin->child->allocation;
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 child_area = bin->child->allocation;
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_UNSET_FLAGS (bin, GTK_NO_WINDOW);
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 child_area = bin->child->allocation;
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 GdkWindowAttr attributes;
228 gint attributes_mask;
230 GtkRequisition child_requisition;
232 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
234 border_width = GTK_CONTAINER (widget)->border_width;
236 attributes.x = widget->allocation.x + border_width;
237 attributes.y = widget->allocation.y + border_width;
238 attributes.width = widget->allocation.width - 2 * border_width;
239 attributes.height = widget->allocation.height - 2 * border_width;
240 attributes.window_type = GDK_WINDOW_CHILD;
241 attributes.event_mask = gtk_widget_get_events (widget)
243 | GDK_POINTER_MOTION_MASK
244 | GDK_BUTTON_PRESS_MASK
245 | GDK_BUTTON_RELEASE_MASK
247 | GDK_ENTER_NOTIFY_MASK
248 | GDK_LEAVE_NOTIFY_MASK;
250 attributes.visual = gtk_widget_get_visual (widget);
251 attributes.colormap = gtk_widget_get_colormap (widget);
252 attributes.wclass = GDK_INPUT_OUTPUT;
254 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
256 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
257 &attributes, attributes_mask);
258 gdk_window_set_user_data (widget->window, widget);
259 g_signal_connect (widget->window, "pick-embedded-child",
260 G_CALLBACK (pick_offscreen_child), bin);
262 attributes.window_type = GDK_WINDOW_OFFSCREEN;
264 child_requisition.width = child_requisition.height = 0;
265 if (bin->child && gtk_widget_get_visible (bin->child))
267 attributes.width = bin->child->allocation.width;
268 attributes.height = bin->child->allocation.height;
270 bin->offscreen_window = gdk_window_new (gtk_widget_get_root_window (widget),
271 &attributes, attributes_mask);
272 gdk_window_set_user_data (bin->offscreen_window, widget);
274 gtk_widget_set_parent_window (bin->child, bin->offscreen_window);
275 gdk_offscreen_window_set_embedder (bin->offscreen_window, widget->window);
276 g_signal_connect (bin->offscreen_window, "to-embedder",
277 G_CALLBACK (offscreen_window_to_parent), bin);
278 g_signal_connect (bin->offscreen_window, "from-embedder",
279 G_CALLBACK (offscreen_window_from_parent), bin);
281 widget->style = gtk_style_attach (widget->style, widget->window);
283 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
284 gtk_style_set_background (widget->style, bin->offscreen_window, GTK_STATE_NORMAL);
285 gdk_window_show (bin->offscreen_window);
289 gtk_rotated_bin_unrealize (GtkWidget *widget)
291 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
293 gdk_window_set_user_data (bin->offscreen_window, NULL);
294 gdk_window_destroy (bin->offscreen_window);
295 bin->offscreen_window = NULL;
297 GTK_WIDGET_CLASS (gtk_rotated_bin_parent_class)->unrealize (widget);
301 gtk_rotated_bin_child_type (GtkContainer *container)
303 GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
308 return GTK_TYPE_WIDGET;
312 gtk_rotated_bin_add (GtkContainer *container,
315 GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
319 gtk_widget_set_parent_window (widget, bin->offscreen_window);
320 gtk_widget_set_parent (widget, GTK_WIDGET (bin));
324 g_warning ("GtkRotatedBin cannot have more than one child\n");
328 gtk_rotated_bin_remove (GtkContainer *container,
331 GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
332 gboolean was_visible;
334 was_visible = gtk_widget_get_visible (widget);
336 if (bin->child == widget)
338 gtk_widget_unparent (widget);
342 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
343 gtk_widget_queue_resize (GTK_WIDGET (container));
348 gtk_rotated_bin_forall (GtkContainer *container,
349 gboolean include_internals,
350 GtkCallback callback,
351 gpointer callback_data)
353 GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
355 g_return_if_fail (callback != NULL);
358 (*callback) (bin->child, callback_data);
362 gtk_rotated_bin_set_angle (GtkRotatedBin *bin,
365 g_return_if_fail (GTK_IS_ROTATED_BIN (bin));
368 gtk_widget_queue_resize (GTK_WIDGET (bin));
370 gdk_window_geometry_changed (bin->offscreen_window);
374 gtk_rotated_bin_size_request (GtkWidget *widget,
375 GtkRequisition *requisition)
377 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
378 GtkRequisition child_requisition;
382 child_requisition.width = 0;
383 child_requisition.height = 0;
385 if (bin->child && gtk_widget_get_visible (bin->child))
386 gtk_widget_size_request (bin->child, &child_requisition);
388 s = sin (bin->angle);
389 c = cos (bin->angle);
390 w = c * child_requisition.width + s * child_requisition.height;
391 h = s * child_requisition.width + c * child_requisition.height;
393 requisition->width = GTK_CONTAINER (widget)->border_width * 2 + w;
394 requisition->height = GTK_CONTAINER (widget)->border_width * 2 + h;
398 gtk_rotated_bin_size_allocate (GtkWidget *widget,
399 GtkAllocation *allocation)
401 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
406 widget->allocation = *allocation;
408 border_width = GTK_CONTAINER (widget)->border_width;
410 w = allocation->width - border_width * 2;
411 h = allocation->height - border_width * 2;
413 if (GTK_WIDGET_REALIZED (widget))
414 gdk_window_move_resize (widget->window,
415 allocation->x + border_width,
416 allocation->y + border_width,
419 if (bin->child && gtk_widget_get_visible (bin->child))
421 GtkRequisition child_requisition;
422 GtkAllocation child_allocation;
424 s = sin (bin->angle);
425 c = cos (bin->angle);
427 gtk_widget_get_child_requisition (bin->child, &child_requisition);
428 child_allocation.x = 0;
429 child_allocation.y = 0;
430 child_allocation.height = child_requisition.height;
432 child_allocation.width = h / s;
434 child_allocation.width = w / c;
436 child_allocation.width = MIN ((w - s * child_allocation.height) / c,
437 (h - c * child_allocation.height) / s);
439 if (GTK_WIDGET_REALIZED (widget))
440 gdk_window_move_resize (bin->offscreen_window,
443 child_allocation.width,
444 child_allocation.height);
446 child_allocation.x = child_allocation.y = 0;
447 gtk_widget_size_allocate (bin->child, &child_allocation);
452 gtk_rotated_bin_damage (GtkWidget *widget,
453 GdkEventExpose *event)
455 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
461 gtk_rotated_bin_expose (GtkWidget *widget,
462 GdkEventExpose *event)
464 GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
469 if (gtk_widget_is_drawable (widget))
471 if (event->window == widget->window)
474 GtkAllocation child_area;
477 if (bin->child && gtk_widget_get_visible (bin->child))
479 pixmap = gdk_offscreen_window_get_pixmap (bin->offscreen_window);
480 child_area = bin->child->allocation;
482 cr = gdk_cairo_create (widget->window);
485 s = sin (bin->angle);
486 c = cos (bin->angle);
487 w = c * child_area.width + s * child_area.height;
488 h = s * child_area.width + c * child_area.height;
490 cairo_translate (cr, (w - child_area.width) / 2, (h - child_area.height) / 2);
491 cairo_translate (cr, child_area.width / 2, child_area.height / 2);
492 cairo_rotate (cr, bin->angle);
493 cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
496 gdk_drawable_get_size (pixmap, &width, &height);
497 cairo_rectangle (cr, 0, 0, width, height);
500 gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
506 else if (event->window == bin->offscreen_window)
508 gtk_paint_flat_box (widget->style, event->window,
509 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
510 &event->area, widget, "blah",
514 gtk_container_propagate_expose (GTK_CONTAINER (widget),
526 scale_changed (GtkRange *range,
529 gtk_rotated_bin_set_angle (bin, gtk_range_get_value (range));
532 static GtkWidget *window = NULL;
535 do_offscreen_window (GtkWidget *do_widget)
539 GtkWidget *bin, *vbox, *scale, *button;
542 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
543 gtk_window_set_screen (GTK_WINDOW (window),
544 gtk_widget_get_screen (do_widget));
545 gtk_window_set_title (GTK_WINDOW (window), "Rotated widget");
547 g_signal_connect (window, "destroy",
548 G_CALLBACK (gtk_widget_destroyed), &window);
550 gdk_color_parse ("black", &black);
551 gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &black);
552 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
554 vbox = gtk_vbox_new (0, FALSE);
555 scale = gtk_hscale_new_with_range (0, G_PI/2, 0.01);
556 gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
558 button = gtk_button_new_with_label ("A Button");
559 bin = gtk_rotated_bin_new ();
561 g_signal_connect (scale, "value-changed", G_CALLBACK (scale_changed), bin);
563 gtk_container_add (GTK_CONTAINER (window), vbox);
564 gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 0);
565 gtk_box_pack_start (GTK_BOX (vbox), bin, TRUE, TRUE, 0);
566 gtk_container_add (GTK_CONTAINER (bin), button);
569 if (!gtk_widget_get_visible (window))
570 gtk_widget_show_all (window);
573 gtk_widget_destroy (window);