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_get_preferred_width (GtkWidget *widget,
17 static void gtk_offscreen_box_get_preferred_height (GtkWidget *widget,
20 static void gtk_offscreen_box_size_allocate (GtkWidget *widget,
21 GtkAllocation *allocation);
22 static gboolean gtk_offscreen_box_damage (GtkWidget *widget,
23 GdkEventExpose *event);
24 static gboolean gtk_offscreen_box_draw (GtkWidget *widget,
27 static void gtk_offscreen_box_add (GtkContainer *container,
29 static void gtk_offscreen_box_remove (GtkContainer *container,
31 static void gtk_offscreen_box_forall (GtkContainer *container,
32 gboolean include_internals,
34 gpointer callback_data);
35 static GType gtk_offscreen_box_child_type (GtkContainer *container);
37 #define CHILD1_SIZE_SCALE 1.0
38 #define CHILD2_SIZE_SCALE 1.0
40 G_DEFINE_TYPE (GtkOffscreenBox, gtk_offscreen_box, GTK_TYPE_CONTAINER);
43 to_child_2 (GtkOffscreenBox *offscreen_box,
44 double widget_x, double widget_y,
45 double *x_out, double *y_out)
47 GtkAllocation child_area;
49 double cos_angle, sin_angle;
54 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
56 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
57 y -= child_area.height;
60 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
62 x -= child_area.width / 2;
63 y -= child_area.height / 2;
65 cos_angle = cos (-offscreen_box->angle);
66 sin_angle = sin (-offscreen_box->angle);
68 xr = x * cos_angle - y * sin_angle;
69 yr = x * sin_angle + y * cos_angle;
73 x += child_area.width / 2;
74 y += child_area.height / 2;
81 to_parent_2 (GtkOffscreenBox *offscreen_box,
82 double offscreen_x, double offscreen_y,
83 double *x_out, double *y_out)
85 GtkAllocation child_area;
87 double cos_angle, sin_angle;
89 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
94 x -= child_area.width / 2;
95 y -= child_area.height / 2;
97 cos_angle = cos (offscreen_box->angle);
98 sin_angle = sin (offscreen_box->angle);
100 xr = x * cos_angle - y * sin_angle;
101 yr = x * sin_angle + y * cos_angle;
105 x += child_area.width / 2;
106 y += child_area.height / 2;
108 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
110 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
111 y += child_area.height;
119 gtk_offscreen_box_class_init (GtkOffscreenBoxClass *klass)
121 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
122 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
124 widget_class->realize = gtk_offscreen_box_realize;
125 widget_class->unrealize = gtk_offscreen_box_unrealize;
126 widget_class->get_preferred_width = gtk_offscreen_box_get_preferred_width;
127 widget_class->get_preferred_height = gtk_offscreen_box_get_preferred_height;
128 widget_class->size_allocate = gtk_offscreen_box_size_allocate;
129 widget_class->draw = gtk_offscreen_box_draw;
131 g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
132 GTK_TYPE_OFFSCREEN_BOX,
133 g_cclosure_new (G_CALLBACK (gtk_offscreen_box_damage),
136 container_class->add = gtk_offscreen_box_add;
137 container_class->remove = gtk_offscreen_box_remove;
138 container_class->forall = gtk_offscreen_box_forall;
139 container_class->child_type = gtk_offscreen_box_child_type;
143 gtk_offscreen_box_init (GtkOffscreenBox *offscreen_box)
145 gtk_widget_set_has_window (GTK_WIDGET (offscreen_box), TRUE);
149 gtk_offscreen_box_new (void)
151 return g_object_new (GTK_TYPE_OFFSCREEN_BOX, NULL);
155 pick_offscreen_child (GdkWindow *offscreen_window,
156 double widget_x, double widget_y,
157 GtkOffscreenBox *offscreen_box)
159 GtkAllocation child_area;
162 /* First check for child 2 */
163 if (offscreen_box->child2 &&
164 gtk_widget_get_visible (offscreen_box->child2))
166 to_child_2 (offscreen_box,
170 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
172 if (x >= 0 && x < child_area.width &&
173 y >= 0 && y < child_area.height)
174 return offscreen_box->offscreen_window2;
177 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
182 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
184 if (x >= 0 && x < child_area.width &&
185 y >= 0 && y < child_area.height)
186 return offscreen_box->offscreen_window1;
193 offscreen_window_to_parent1 (GdkWindow *offscreen_window,
198 GtkOffscreenBox *offscreen_box)
200 *parent_x = offscreen_x;
201 *parent_y = offscreen_y;
205 offscreen_window_from_parent1 (GdkWindow *window,
210 GtkOffscreenBox *offscreen_box)
212 *offscreen_x = parent_x;
213 *offscreen_y = parent_y;
217 offscreen_window_to_parent2 (GdkWindow *offscreen_window,
222 GtkOffscreenBox *offscreen_box)
224 to_parent_2 (offscreen_box,
225 offscreen_x, offscreen_y,
230 offscreen_window_from_parent2 (GdkWindow *window,
235 GtkOffscreenBox *offscreen_box)
237 to_child_2 (offscreen_box,
239 offscreen_x, offscreen_y);
242 static cairo_surface_t *
243 gdk_offscreen_box_create_alpha_image_surface (GdkWindow *offscreen,
247 return cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
251 gtk_offscreen_box_realize (GtkWidget *widget)
253 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
254 GtkAllocation allocation, child_area;
255 GtkStyleContext *context;
257 GdkWindowAttr attributes;
258 gint attributes_mask;
260 GtkRequisition child_requisition;
263 gtk_widget_set_realized (widget, TRUE);
265 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
267 gtk_widget_get_allocation (widget, &allocation);
269 attributes.x = allocation.x + border_width;
270 attributes.y = allocation.y + border_width;
271 attributes.width = allocation.width - 2 * border_width;
272 attributes.height = allocation.height - 2 * border_width;
273 attributes.window_type = GDK_WINDOW_CHILD;
274 attributes.event_mask = gtk_widget_get_events (widget)
276 | GDK_POINTER_MOTION_MASK
277 | GDK_BUTTON_PRESS_MASK
278 | GDK_BUTTON_RELEASE_MASK
280 | GDK_ENTER_NOTIFY_MASK
281 | GDK_LEAVE_NOTIFY_MASK;
283 attributes.visual = gtk_widget_get_visual (widget);
284 attributes.wclass = GDK_INPUT_OUTPUT;
286 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
288 window = gdk_window_new (gtk_widget_get_parent_window (widget),
289 &attributes, attributes_mask);
290 gtk_widget_set_window (widget, window);
291 gdk_window_set_user_data (window, widget);
293 g_signal_connect (window, "pick-embedded-child",
294 G_CALLBACK (pick_offscreen_child), offscreen_box);
296 attributes.window_type = GDK_WINDOW_OFFSCREEN;
299 attributes.x = attributes.y = 0;
300 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
302 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
304 attributes.width = child_area.width;
305 attributes.height = child_area.height;
306 start_y += child_area.height;
308 offscreen_box->offscreen_window1 = gdk_window_new (gtk_widget_get_root_window (widget),
309 &attributes, attributes_mask);
310 gdk_window_set_user_data (offscreen_box->offscreen_window1, widget);
311 if (offscreen_box->child1)
312 gtk_widget_set_parent_window (offscreen_box->child1, offscreen_box->offscreen_window1);
314 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window1,
317 g_signal_connect (offscreen_box->offscreen_window1, "to-embedder",
318 G_CALLBACK (offscreen_window_to_parent1), offscreen_box);
319 g_signal_connect (offscreen_box->offscreen_window1, "from-embedder",
320 G_CALLBACK (offscreen_window_from_parent1), offscreen_box);
323 attributes.y = start_y;
324 child_requisition.width = child_requisition.height = 0;
325 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
327 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
329 attributes.width = child_area.width;
330 attributes.height = child_area.height;
332 offscreen_box->offscreen_window2 = gdk_window_new (gtk_widget_get_root_window (widget),
333 &attributes, attributes_mask);
334 gdk_window_set_user_data (offscreen_box->offscreen_window2, widget);
335 if (offscreen_box->child2)
336 gtk_widget_set_parent_window (offscreen_box->child2, offscreen_box->offscreen_window2);
337 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window2,
340 g_signal_connect (offscreen_box->offscreen_window2, "create-surface",
341 G_CALLBACK (gdk_offscreen_box_create_alpha_image_surface),
343 g_signal_connect (offscreen_box->offscreen_window2, "to-embedder",
344 G_CALLBACK (offscreen_window_to_parent2), offscreen_box);
345 g_signal_connect (offscreen_box->offscreen_window2, "from-embedder",
346 G_CALLBACK (offscreen_window_from_parent2), offscreen_box);
348 gtk_widget_style_attach (widget);
349 context = gtk_widget_get_style_context (widget);
350 gtk_style_context_set_background (context, window);
351 gtk_style_context_set_background (context, offscreen_box->offscreen_window1);
352 gtk_style_context_set_background (context, offscreen_box->offscreen_window2);
354 gdk_window_show (offscreen_box->offscreen_window1);
355 gdk_window_show (offscreen_box->offscreen_window2);
359 gtk_offscreen_box_unrealize (GtkWidget *widget)
361 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
363 gdk_window_set_user_data (offscreen_box->offscreen_window1, NULL);
364 gdk_window_destroy (offscreen_box->offscreen_window1);
365 offscreen_box->offscreen_window1 = NULL;
367 gdk_window_set_user_data (offscreen_box->offscreen_window2, NULL);
368 gdk_window_destroy (offscreen_box->offscreen_window2);
369 offscreen_box->offscreen_window2 = NULL;
371 GTK_WIDGET_CLASS (gtk_offscreen_box_parent_class)->unrealize (widget);
375 gtk_offscreen_box_child_type (GtkContainer *container)
377 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
379 if (offscreen_box->child1 && offscreen_box->child2)
382 return GTK_TYPE_WIDGET;
386 gtk_offscreen_box_add (GtkContainer *container,
389 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
391 if (!offscreen_box->child1)
392 gtk_offscreen_box_add1 (offscreen_box, widget);
393 else if (!offscreen_box->child2)
394 gtk_offscreen_box_add2 (offscreen_box, widget);
396 g_warning ("GtkOffscreenBox cannot have more than 2 children\n");
400 gtk_offscreen_box_add1 (GtkOffscreenBox *offscreen_box,
403 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
404 g_return_if_fail (GTK_IS_WIDGET (child));
406 if (offscreen_box->child1 == NULL)
408 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window1);
409 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
410 offscreen_box->child1 = child;
415 gtk_offscreen_box_add2 (GtkOffscreenBox *offscreen_box,
418 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
419 g_return_if_fail (GTK_IS_WIDGET (child));
421 if (offscreen_box->child2 == NULL)
423 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window2);
424 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
425 offscreen_box->child2 = child;
430 gtk_offscreen_box_remove (GtkContainer *container,
433 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
434 gboolean was_visible;
436 was_visible = gtk_widget_get_visible (widget);
438 if (offscreen_box->child1 == widget)
440 gtk_widget_unparent (widget);
442 offscreen_box->child1 = NULL;
444 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
445 gtk_widget_queue_resize (GTK_WIDGET (container));
447 else if (offscreen_box->child2 == widget)
449 gtk_widget_unparent (widget);
451 offscreen_box->child2 = NULL;
453 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
454 gtk_widget_queue_resize (GTK_WIDGET (container));
459 gtk_offscreen_box_forall (GtkContainer *container,
460 gboolean include_internals,
461 GtkCallback callback,
462 gpointer callback_data)
464 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
466 g_return_if_fail (callback != NULL);
468 if (offscreen_box->child1)
469 (*callback) (offscreen_box->child1, callback_data);
470 if (offscreen_box->child2)
471 (*callback) (offscreen_box->child2, callback_data);
475 gtk_offscreen_box_set_angle (GtkOffscreenBox *offscreen_box,
478 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
480 offscreen_box->angle = angle;
481 gtk_widget_queue_draw (GTK_WIDGET (offscreen_box));
483 /* TODO: Really needs to resent pointer events if over the rotated window */
488 gtk_offscreen_box_size_request (GtkWidget *widget,
489 GtkRequisition *requisition)
491 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
498 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
500 GtkRequisition child_requisition;
502 gtk_widget_get_preferred_size ( (offscreen_box->child1),
503 &child_requisition, NULL);
505 w = MAX (w, CHILD1_SIZE_SCALE * child_requisition.width);
506 h += CHILD1_SIZE_SCALE * child_requisition.height;
509 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
511 GtkRequisition child_requisition;
513 gtk_widget_get_preferred_size ( (offscreen_box->child2),
514 &child_requisition, NULL);
516 w = MAX (w, CHILD2_SIZE_SCALE * child_requisition.width);
517 h += CHILD2_SIZE_SCALE * child_requisition.height;
520 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
521 requisition->width = border_width * 2 + w;
522 requisition->height = border_width * 2 + h;
526 gtk_offscreen_box_get_preferred_width (GtkWidget *widget,
530 GtkRequisition requisition;
532 gtk_offscreen_box_size_request (widget, &requisition);
534 *minimum = *natural = requisition.width;
538 gtk_offscreen_box_get_preferred_height (GtkWidget *widget,
542 GtkRequisition requisition;
544 gtk_offscreen_box_size_request (widget, &requisition);
546 *minimum = *natural = requisition.height;
550 gtk_offscreen_box_size_allocate (GtkWidget *widget,
551 GtkAllocation *allocation)
553 GtkOffscreenBox *offscreen_box;
557 offscreen_box = GTK_OFFSCREEN_BOX (widget);
559 gtk_widget_set_allocation (widget, allocation);
561 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
563 if (gtk_widget_get_realized (widget))
564 gdk_window_move_resize (gtk_widget_get_window (widget),
565 allocation->x + border_width,
566 allocation->y + border_width,
567 allocation->width - border_width * 2,
568 allocation->height - border_width * 2);
572 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
574 GtkRequisition child_requisition;
575 GtkAllocation child_allocation;
577 gtk_widget_get_preferred_size (offscreen_box->child1,
578 &child_requisition, NULL);
579 child_allocation.x = child_requisition.width * (CHILD1_SIZE_SCALE - 1.0) / 2;
580 child_allocation.y = start_y + child_requisition.height * (CHILD1_SIZE_SCALE - 1.0) / 2;
581 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
582 child_allocation.height = child_requisition.height;
584 start_y += CHILD1_SIZE_SCALE * child_requisition.height;
586 if (gtk_widget_get_realized (widget))
587 gdk_window_move_resize (offscreen_box->offscreen_window1,
590 child_allocation.width,
591 child_allocation.height);
593 child_allocation.x = child_allocation.y = 0;
594 gtk_widget_size_allocate (offscreen_box->child1, &child_allocation);
597 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
599 GtkRequisition child_requisition;
600 GtkAllocation child_allocation;
602 gtk_widget_get_preferred_size (offscreen_box->child2,
603 &child_requisition, NULL);
604 child_allocation.x = child_requisition.width * (CHILD2_SIZE_SCALE - 1.0) / 2;
605 child_allocation.y = start_y + child_requisition.height * (CHILD2_SIZE_SCALE - 1.0) / 2;
606 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
607 child_allocation.height = child_requisition.height;
609 start_y += CHILD2_SIZE_SCALE * child_requisition.height;
611 if (gtk_widget_get_realized (widget))
612 gdk_window_move_resize (offscreen_box->offscreen_window2,
615 child_allocation.width,
616 child_allocation.height);
618 child_allocation.x = child_allocation.y = 0;
619 gtk_widget_size_allocate (offscreen_box->child2, &child_allocation);
624 gtk_offscreen_box_damage (GtkWidget *widget,
625 GdkEventExpose *event)
627 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
634 gtk_offscreen_box_draw (GtkWidget *widget,
637 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
640 window = gtk_widget_get_window (widget);
641 if (gtk_cairo_should_draw_window (cr, window))
643 cairo_surface_t *surface;
644 GtkAllocation child_area;
646 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
648 surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window1);
650 cairo_set_source_surface (cr, surface, 0, 0);
653 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
654 cairo_translate (cr, 0, child_area.height);
657 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
659 surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window2);
661 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
664 cairo_translate (cr, child_area.width / 2, child_area.height / 2);
665 cairo_rotate (cr, offscreen_box->angle);
666 cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
669 cairo_set_source_surface (cr, surface, 0, 0);
673 else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window1))
675 gtk_render_background (gtk_widget_get_style_context (widget), cr,
678 gdk_window_get_width (offscreen_box->offscreen_window1),
679 gdk_window_get_height (offscreen_box->offscreen_window1));
681 if (offscreen_box->child1)
682 gtk_container_propagate_draw (GTK_CONTAINER (widget),
683 offscreen_box->child1,
686 else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window2))
688 gtk_render_background (gtk_widget_get_style_context (widget), cr,
690 gdk_window_get_width (offscreen_box->offscreen_window2),
691 gdk_window_get_height (offscreen_box->offscreen_window2));
693 if (offscreen_box->child2)
694 gtk_container_propagate_draw (GTK_CONTAINER (widget),
695 offscreen_box->child2,