10 #include "gtkoffscreenbox.h"
12 static void gtk_offscreen_box_realize (GtkWidget *widget);
13 static void gtk_offscreen_box_unrealize (GtkWidget *widget);
14 static void gtk_offscreen_box_size_request (GtkWidget *widget,
15 GtkRequisition *requisition);
16 static void gtk_offscreen_box_size_allocate (GtkWidget *widget,
17 GtkAllocation *allocation);
18 static gboolean gtk_offscreen_box_damage (GtkWidget *widget,
19 GdkEventExpose *event);
20 static gboolean gtk_offscreen_box_draw (GtkWidget *widget,
23 static void gtk_offscreen_box_add (GtkContainer *container,
25 static void gtk_offscreen_box_remove (GtkContainer *container,
27 static void gtk_offscreen_box_forall (GtkContainer *container,
28 gboolean include_internals,
30 gpointer callback_data);
31 static GType gtk_offscreen_box_child_type (GtkContainer *container);
33 #define CHILD1_SIZE_SCALE 1.0
34 #define CHILD2_SIZE_SCALE 1.0
36 G_DEFINE_TYPE (GtkOffscreenBox, gtk_offscreen_box, GTK_TYPE_CONTAINER);
39 to_child_2 (GtkOffscreenBox *offscreen_box,
40 double widget_x, double widget_y,
41 double *x_out, double *y_out)
43 GtkAllocation child_area;
45 double cos_angle, sin_angle;
50 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
52 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
53 y -= child_area.height;
56 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
58 x -= child_area.width / 2;
59 y -= child_area.height / 2;
61 cos_angle = cos (-offscreen_box->angle);
62 sin_angle = sin (-offscreen_box->angle);
64 xr = x * cos_angle - y * sin_angle;
65 yr = x * sin_angle + y * cos_angle;
69 x += child_area.width / 2;
70 y += child_area.height / 2;
77 to_parent_2 (GtkOffscreenBox *offscreen_box,
78 double offscreen_x, double offscreen_y,
79 double *x_out, double *y_out)
81 GtkAllocation child_area;
83 double cos_angle, sin_angle;
85 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
90 x -= child_area.width / 2;
91 y -= child_area.height / 2;
93 cos_angle = cos (offscreen_box->angle);
94 sin_angle = sin (offscreen_box->angle);
96 xr = x * cos_angle - y * sin_angle;
97 yr = x * sin_angle + y * cos_angle;
101 x += child_area.width / 2;
102 y += child_area.height / 2;
104 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
106 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
107 y += child_area.height;
115 gtk_offscreen_box_class_init (GtkOffscreenBoxClass *klass)
117 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
118 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
120 widget_class->realize = gtk_offscreen_box_realize;
121 widget_class->unrealize = gtk_offscreen_box_unrealize;
122 widget_class->size_request = gtk_offscreen_box_size_request;
123 widget_class->size_allocate = gtk_offscreen_box_size_allocate;
124 widget_class->draw = gtk_offscreen_box_draw;
126 g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
127 GTK_TYPE_OFFSCREEN_BOX,
128 g_cclosure_new (G_CALLBACK (gtk_offscreen_box_damage),
131 container_class->add = gtk_offscreen_box_add;
132 container_class->remove = gtk_offscreen_box_remove;
133 container_class->forall = gtk_offscreen_box_forall;
134 container_class->child_type = gtk_offscreen_box_child_type;
138 gtk_offscreen_box_init (GtkOffscreenBox *offscreen_box)
140 gtk_widget_set_has_window (GTK_WIDGET (offscreen_box), TRUE);
144 gtk_offscreen_box_new (void)
146 return g_object_new (GTK_TYPE_OFFSCREEN_BOX, NULL);
150 pick_offscreen_child (GdkWindow *offscreen_window,
151 double widget_x, double widget_y,
152 GtkOffscreenBox *offscreen_box)
154 GtkAllocation child_area;
157 /* First check for child 2 */
158 if (offscreen_box->child2 &&
159 gtk_widget_get_visible (offscreen_box->child2))
161 to_child_2 (offscreen_box,
165 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
167 if (x >= 0 && x < child_area.width &&
168 y >= 0 && y < child_area.height)
169 return offscreen_box->offscreen_window2;
172 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
177 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
179 if (x >= 0 && x < child_area.width &&
180 y >= 0 && y < child_area.height)
181 return offscreen_box->offscreen_window1;
188 offscreen_window_to_parent1 (GdkWindow *offscreen_window,
193 GtkOffscreenBox *offscreen_box)
195 *parent_x = offscreen_x;
196 *parent_y = offscreen_y;
200 offscreen_window_from_parent1 (GdkWindow *window,
205 GtkOffscreenBox *offscreen_box)
207 *offscreen_x = parent_x;
208 *offscreen_y = parent_y;
212 offscreen_window_to_parent2 (GdkWindow *offscreen_window,
217 GtkOffscreenBox *offscreen_box)
219 to_parent_2 (offscreen_box,
220 offscreen_x, offscreen_y,
225 offscreen_window_from_parent2 (GdkWindow *window,
230 GtkOffscreenBox *offscreen_box)
232 to_child_2 (offscreen_box,
234 offscreen_x, offscreen_y);
237 static cairo_surface_t *
238 gdk_offscreen_box_create_alpha_image_surface (GdkWindow *offscreen,
242 return cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
246 gtk_offscreen_box_realize (GtkWidget *widget)
248 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
249 GtkAllocation allocation, child_area;
252 GdkWindowAttr attributes;
253 gint attributes_mask;
255 GtkRequisition child_requisition;
258 gtk_widget_set_realized (widget, TRUE);
260 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
262 gtk_widget_get_allocation (widget, &allocation);
264 attributes.x = allocation.x + border_width;
265 attributes.y = allocation.y + border_width;
266 attributes.width = allocation.width - 2 * border_width;
267 attributes.height = allocation.height - 2 * border_width;
268 attributes.window_type = GDK_WINDOW_CHILD;
269 attributes.event_mask = gtk_widget_get_events (widget)
271 | GDK_POINTER_MOTION_MASK
272 | GDK_BUTTON_PRESS_MASK
273 | GDK_BUTTON_RELEASE_MASK
275 | GDK_ENTER_NOTIFY_MASK
276 | GDK_LEAVE_NOTIFY_MASK;
278 attributes.visual = gtk_widget_get_visual (widget);
279 attributes.wclass = GDK_INPUT_OUTPUT;
281 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
283 window = gdk_window_new (gtk_widget_get_parent_window (widget),
284 &attributes, attributes_mask);
285 gtk_widget_set_window (widget, window);
286 gdk_window_set_user_data (window, widget);
288 g_signal_connect (window, "pick-embedded-child",
289 G_CALLBACK (pick_offscreen_child), offscreen_box);
291 attributes.window_type = GDK_WINDOW_OFFSCREEN;
294 attributes.x = attributes.y = 0;
295 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
297 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
299 attributes.width = child_area.width;
300 attributes.height = child_area.height;
301 start_y += child_area.height;
303 offscreen_box->offscreen_window1 = gdk_window_new (gtk_widget_get_root_window (widget),
304 &attributes, attributes_mask);
305 gdk_window_set_user_data (offscreen_box->offscreen_window1, widget);
306 if (offscreen_box->child1)
307 gtk_widget_set_parent_window (offscreen_box->child1, offscreen_box->offscreen_window1);
309 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window1,
312 g_signal_connect (offscreen_box->offscreen_window1, "to-embedder",
313 G_CALLBACK (offscreen_window_to_parent1), offscreen_box);
314 g_signal_connect (offscreen_box->offscreen_window1, "from-embedder",
315 G_CALLBACK (offscreen_window_from_parent1), offscreen_box);
318 attributes.y = start_y;
319 child_requisition.width = child_requisition.height = 0;
320 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
322 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
324 attributes.width = child_area.width;
325 attributes.height = child_area.height;
327 offscreen_box->offscreen_window2 = gdk_window_new (gtk_widget_get_root_window (widget),
328 &attributes, attributes_mask);
329 gdk_window_set_user_data (offscreen_box->offscreen_window2, widget);
330 if (offscreen_box->child2)
331 gtk_widget_set_parent_window (offscreen_box->child2, offscreen_box->offscreen_window2);
332 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window2,
335 g_signal_connect (offscreen_box->offscreen_window2, "create-surface",
336 G_CALLBACK (gdk_offscreen_box_create_alpha_image_surface),
338 g_signal_connect (offscreen_box->offscreen_window2, "to-embedder",
339 G_CALLBACK (offscreen_window_to_parent2), offscreen_box);
340 g_signal_connect (offscreen_box->offscreen_window2, "from-embedder",
341 G_CALLBACK (offscreen_window_from_parent2), offscreen_box);
343 gtk_widget_style_attach (widget);
344 style = gtk_widget_get_style (widget);
345 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
346 gtk_style_set_background (style, offscreen_box->offscreen_window1, GTK_STATE_NORMAL);
347 gtk_style_set_background (style, offscreen_box->offscreen_window2, GTK_STATE_NORMAL);
349 gdk_window_show (offscreen_box->offscreen_window1);
350 gdk_window_show (offscreen_box->offscreen_window2);
354 gtk_offscreen_box_unrealize (GtkWidget *widget)
356 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
358 gdk_window_set_user_data (offscreen_box->offscreen_window1, NULL);
359 gdk_window_destroy (offscreen_box->offscreen_window1);
360 offscreen_box->offscreen_window1 = NULL;
362 gdk_window_set_user_data (offscreen_box->offscreen_window2, NULL);
363 gdk_window_destroy (offscreen_box->offscreen_window2);
364 offscreen_box->offscreen_window2 = NULL;
366 GTK_WIDGET_CLASS (gtk_offscreen_box_parent_class)->unrealize (widget);
370 gtk_offscreen_box_child_type (GtkContainer *container)
372 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
374 if (offscreen_box->child1 && offscreen_box->child2)
377 return GTK_TYPE_WIDGET;
381 gtk_offscreen_box_add (GtkContainer *container,
384 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
386 if (!offscreen_box->child1)
387 gtk_offscreen_box_add1 (offscreen_box, widget);
388 else if (!offscreen_box->child2)
389 gtk_offscreen_box_add2 (offscreen_box, widget);
391 g_warning ("GtkOffscreenBox cannot have more than 2 children\n");
395 gtk_offscreen_box_add1 (GtkOffscreenBox *offscreen_box,
398 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
399 g_return_if_fail (GTK_IS_WIDGET (child));
401 if (offscreen_box->child1 == NULL)
403 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window1);
404 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
405 offscreen_box->child1 = child;
410 gtk_offscreen_box_add2 (GtkOffscreenBox *offscreen_box,
413 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
414 g_return_if_fail (GTK_IS_WIDGET (child));
416 if (offscreen_box->child2 == NULL)
418 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window2);
419 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
420 offscreen_box->child2 = child;
425 gtk_offscreen_box_remove (GtkContainer *container,
428 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
429 gboolean was_visible;
431 was_visible = gtk_widget_get_visible (widget);
433 if (offscreen_box->child1 == widget)
435 gtk_widget_unparent (widget);
437 offscreen_box->child1 = NULL;
439 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
440 gtk_widget_queue_resize (GTK_WIDGET (container));
442 else if (offscreen_box->child2 == widget)
444 gtk_widget_unparent (widget);
446 offscreen_box->child2 = NULL;
448 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
449 gtk_widget_queue_resize (GTK_WIDGET (container));
454 gtk_offscreen_box_forall (GtkContainer *container,
455 gboolean include_internals,
456 GtkCallback callback,
457 gpointer callback_data)
459 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
461 g_return_if_fail (callback != NULL);
463 if (offscreen_box->child1)
464 (*callback) (offscreen_box->child1, callback_data);
465 if (offscreen_box->child2)
466 (*callback) (offscreen_box->child2, callback_data);
470 gtk_offscreen_box_set_angle (GtkOffscreenBox *offscreen_box,
473 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
475 offscreen_box->angle = angle;
476 gtk_widget_queue_draw (GTK_WIDGET (offscreen_box));
478 /* TODO: Really needs to resent pointer events if over the rotated window */
483 gtk_offscreen_box_size_request (GtkWidget *widget,
484 GtkRequisition *requisition)
486 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
493 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
495 GtkRequisition child_requisition;
497 gtk_widget_get_preferred_size ( (offscreen_box->child1),
498 &child_requisition, NULL);
500 w = MAX (w, CHILD1_SIZE_SCALE * child_requisition.width);
501 h += CHILD1_SIZE_SCALE * child_requisition.height;
504 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
506 GtkRequisition child_requisition;
508 gtk_widget_get_preferred_size ( (offscreen_box->child2),
509 &child_requisition, NULL);
511 w = MAX (w, CHILD2_SIZE_SCALE * child_requisition.width);
512 h += CHILD2_SIZE_SCALE * child_requisition.height;
515 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
516 requisition->width = border_width * 2 + w;
517 requisition->height = border_width * 2 + h;
521 gtk_offscreen_box_size_allocate (GtkWidget *widget,
522 GtkAllocation *allocation)
524 GtkOffscreenBox *offscreen_box;
528 offscreen_box = GTK_OFFSCREEN_BOX (widget);
530 gtk_widget_set_allocation (widget, allocation);
532 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
534 if (gtk_widget_get_realized (widget))
535 gdk_window_move_resize (gtk_widget_get_window (widget),
536 allocation->x + border_width,
537 allocation->y + border_width,
538 allocation->width - border_width * 2,
539 allocation->height - border_width * 2);
543 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
545 GtkRequisition child_requisition;
546 GtkAllocation child_allocation;
548 gtk_widget_get_preferred_size (offscreen_box->child1,
549 &child_requisition, NULL);
550 child_allocation.x = child_requisition.width * (CHILD1_SIZE_SCALE - 1.0) / 2;
551 child_allocation.y = start_y + child_requisition.height * (CHILD1_SIZE_SCALE - 1.0) / 2;
552 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
553 child_allocation.height = child_requisition.height;
555 start_y += CHILD1_SIZE_SCALE * child_requisition.height;
557 if (gtk_widget_get_realized (widget))
558 gdk_window_move_resize (offscreen_box->offscreen_window1,
561 child_allocation.width,
562 child_allocation.height);
564 child_allocation.x = child_allocation.y = 0;
565 gtk_widget_size_allocate (offscreen_box->child1, &child_allocation);
568 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
570 GtkRequisition child_requisition;
571 GtkAllocation child_allocation;
573 gtk_widget_get_preferred_size (offscreen_box->child2,
574 &child_requisition, NULL);
575 child_allocation.x = child_requisition.width * (CHILD2_SIZE_SCALE - 1.0) / 2;
576 child_allocation.y = start_y + child_requisition.height * (CHILD2_SIZE_SCALE - 1.0) / 2;
577 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
578 child_allocation.height = child_requisition.height;
580 start_y += CHILD2_SIZE_SCALE * child_requisition.height;
582 if (gtk_widget_get_realized (widget))
583 gdk_window_move_resize (offscreen_box->offscreen_window2,
586 child_allocation.width,
587 child_allocation.height);
589 child_allocation.x = child_allocation.y = 0;
590 gtk_widget_size_allocate (offscreen_box->child2, &child_allocation);
595 gtk_offscreen_box_damage (GtkWidget *widget,
596 GdkEventExpose *event)
598 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
605 gtk_offscreen_box_draw (GtkWidget *widget,
608 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
611 window = gtk_widget_get_window (widget);
612 if (gtk_cairo_should_draw_window (cr, window))
614 cairo_surface_t *surface;
615 GtkAllocation child_area;
617 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
619 surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window1);
621 cairo_set_source_surface (cr, surface, 0, 0);
624 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
625 cairo_translate (cr, 0, child_area.height);
628 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
630 surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window2);
632 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
635 cairo_translate (cr, child_area.width / 2, child_area.height / 2);
636 cairo_rotate (cr, offscreen_box->angle);
637 cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
640 cairo_set_source_surface (cr, surface, 0, 0);
644 else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window1))
646 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
647 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
650 gdk_window_get_width (offscreen_box->offscreen_window1),
651 gdk_window_get_height (offscreen_box->offscreen_window1));
653 if (offscreen_box->child1)
654 gtk_container_propagate_draw (GTK_CONTAINER (widget),
655 offscreen_box->child1,
658 else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window2))
660 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
661 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
664 gdk_window_get_width (offscreen_box->offscreen_window2),
665 gdk_window_get_height (offscreen_box->offscreen_window2));
667 if (offscreen_box->child2)
668 gtk_container_propagate_draw (GTK_CONTAINER (widget),
669 offscreen_box->child2,