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 context = gtk_widget_get_style_context (widget);
349 gtk_style_context_set_background (context, window);
350 gtk_style_context_set_background (context, offscreen_box->offscreen_window1);
351 gtk_style_context_set_background (context, offscreen_box->offscreen_window2);
353 gdk_window_show (offscreen_box->offscreen_window1);
354 gdk_window_show (offscreen_box->offscreen_window2);
358 gtk_offscreen_box_unrealize (GtkWidget *widget)
360 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
362 gdk_window_set_user_data (offscreen_box->offscreen_window1, NULL);
363 gdk_window_destroy (offscreen_box->offscreen_window1);
364 offscreen_box->offscreen_window1 = NULL;
366 gdk_window_set_user_data (offscreen_box->offscreen_window2, NULL);
367 gdk_window_destroy (offscreen_box->offscreen_window2);
368 offscreen_box->offscreen_window2 = NULL;
370 GTK_WIDGET_CLASS (gtk_offscreen_box_parent_class)->unrealize (widget);
374 gtk_offscreen_box_child_type (GtkContainer *container)
376 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
378 if (offscreen_box->child1 && offscreen_box->child2)
381 return GTK_TYPE_WIDGET;
385 gtk_offscreen_box_add (GtkContainer *container,
388 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
390 if (!offscreen_box->child1)
391 gtk_offscreen_box_add1 (offscreen_box, widget);
392 else if (!offscreen_box->child2)
393 gtk_offscreen_box_add2 (offscreen_box, widget);
395 g_warning ("GtkOffscreenBox cannot have more than 2 children\n");
399 gtk_offscreen_box_add1 (GtkOffscreenBox *offscreen_box,
402 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
403 g_return_if_fail (GTK_IS_WIDGET (child));
405 if (offscreen_box->child1 == NULL)
407 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window1);
408 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
409 offscreen_box->child1 = child;
414 gtk_offscreen_box_add2 (GtkOffscreenBox *offscreen_box,
417 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
418 g_return_if_fail (GTK_IS_WIDGET (child));
420 if (offscreen_box->child2 == NULL)
422 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window2);
423 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
424 offscreen_box->child2 = child;
429 gtk_offscreen_box_remove (GtkContainer *container,
432 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
433 gboolean was_visible;
435 was_visible = gtk_widget_get_visible (widget);
437 if (offscreen_box->child1 == widget)
439 gtk_widget_unparent (widget);
441 offscreen_box->child1 = NULL;
443 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
444 gtk_widget_queue_resize (GTK_WIDGET (container));
446 else if (offscreen_box->child2 == widget)
448 gtk_widget_unparent (widget);
450 offscreen_box->child2 = NULL;
452 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
453 gtk_widget_queue_resize (GTK_WIDGET (container));
458 gtk_offscreen_box_forall (GtkContainer *container,
459 gboolean include_internals,
460 GtkCallback callback,
461 gpointer callback_data)
463 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
465 g_return_if_fail (callback != NULL);
467 if (offscreen_box->child1)
468 (*callback) (offscreen_box->child1, callback_data);
469 if (offscreen_box->child2)
470 (*callback) (offscreen_box->child2, callback_data);
474 gtk_offscreen_box_set_angle (GtkOffscreenBox *offscreen_box,
477 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
479 offscreen_box->angle = angle;
480 gtk_widget_queue_draw (GTK_WIDGET (offscreen_box));
482 /* TODO: Really needs to resent pointer events if over the rotated window */
487 gtk_offscreen_box_size_request (GtkWidget *widget,
488 GtkRequisition *requisition)
490 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
497 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
499 GtkRequisition child_requisition;
501 gtk_widget_get_preferred_size ( (offscreen_box->child1),
502 &child_requisition, NULL);
504 w = MAX (w, CHILD1_SIZE_SCALE * child_requisition.width);
505 h += CHILD1_SIZE_SCALE * child_requisition.height;
508 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
510 GtkRequisition child_requisition;
512 gtk_widget_get_preferred_size ( (offscreen_box->child2),
513 &child_requisition, NULL);
515 w = MAX (w, CHILD2_SIZE_SCALE * child_requisition.width);
516 h += CHILD2_SIZE_SCALE * child_requisition.height;
519 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
520 requisition->width = border_width * 2 + w;
521 requisition->height = border_width * 2 + h;
525 gtk_offscreen_box_get_preferred_width (GtkWidget *widget,
529 GtkRequisition requisition;
531 gtk_offscreen_box_size_request (widget, &requisition);
533 *minimum = *natural = requisition.width;
537 gtk_offscreen_box_get_preferred_height (GtkWidget *widget,
541 GtkRequisition requisition;
543 gtk_offscreen_box_size_request (widget, &requisition);
545 *minimum = *natural = requisition.height;
549 gtk_offscreen_box_size_allocate (GtkWidget *widget,
550 GtkAllocation *allocation)
552 GtkOffscreenBox *offscreen_box;
556 offscreen_box = GTK_OFFSCREEN_BOX (widget);
558 gtk_widget_set_allocation (widget, allocation);
560 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
562 if (gtk_widget_get_realized (widget))
563 gdk_window_move_resize (gtk_widget_get_window (widget),
564 allocation->x + border_width,
565 allocation->y + border_width,
566 allocation->width - border_width * 2,
567 allocation->height - border_width * 2);
571 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
573 GtkRequisition child_requisition;
574 GtkAllocation child_allocation;
576 gtk_widget_get_preferred_size (offscreen_box->child1,
577 &child_requisition, NULL);
578 child_allocation.x = child_requisition.width * (CHILD1_SIZE_SCALE - 1.0) / 2;
579 child_allocation.y = start_y + child_requisition.height * (CHILD1_SIZE_SCALE - 1.0) / 2;
580 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
581 child_allocation.height = child_requisition.height;
583 start_y += CHILD1_SIZE_SCALE * child_requisition.height;
585 if (gtk_widget_get_realized (widget))
586 gdk_window_move_resize (offscreen_box->offscreen_window1,
589 child_allocation.width,
590 child_allocation.height);
592 child_allocation.x = child_allocation.y = 0;
593 gtk_widget_size_allocate (offscreen_box->child1, &child_allocation);
596 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
598 GtkRequisition child_requisition;
599 GtkAllocation child_allocation;
601 gtk_widget_get_preferred_size (offscreen_box->child2,
602 &child_requisition, NULL);
603 child_allocation.x = child_requisition.width * (CHILD2_SIZE_SCALE - 1.0) / 2;
604 child_allocation.y = start_y + child_requisition.height * (CHILD2_SIZE_SCALE - 1.0) / 2;
605 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
606 child_allocation.height = child_requisition.height;
608 start_y += CHILD2_SIZE_SCALE * child_requisition.height;
610 if (gtk_widget_get_realized (widget))
611 gdk_window_move_resize (offscreen_box->offscreen_window2,
614 child_allocation.width,
615 child_allocation.height);
617 child_allocation.x = child_allocation.y = 0;
618 gtk_widget_size_allocate (offscreen_box->child2, &child_allocation);
623 gtk_offscreen_box_damage (GtkWidget *widget,
624 GdkEventExpose *event)
626 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
633 gtk_offscreen_box_draw (GtkWidget *widget,
636 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
639 window = gtk_widget_get_window (widget);
640 if (gtk_cairo_should_draw_window (cr, window))
642 cairo_surface_t *surface;
643 GtkAllocation child_area;
645 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
647 surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window1);
649 cairo_set_source_surface (cr, surface, 0, 0);
652 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
653 cairo_translate (cr, 0, child_area.height);
656 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
658 surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window2);
660 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
663 cairo_translate (cr, child_area.width / 2, child_area.height / 2);
664 cairo_rotate (cr, offscreen_box->angle);
665 cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
668 cairo_set_source_surface (cr, surface, 0, 0);
672 else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window1))
674 gtk_render_background (gtk_widget_get_style_context (widget), cr,
677 gdk_window_get_width (offscreen_box->offscreen_window1),
678 gdk_window_get_height (offscreen_box->offscreen_window1));
680 if (offscreen_box->child1)
681 gtk_container_propagate_draw (GTK_CONTAINER (widget),
682 offscreen_box->child1,
685 else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window2))
687 gtk_render_background (gtk_widget_get_style_context (widget), cr,
689 gdk_window_get_width (offscreen_box->offscreen_window2),
690 gdk_window_get_height (offscreen_box->offscreen_window2));
692 if (offscreen_box->child2)
693 gtk_container_propagate_draw (GTK_CONTAINER (widget),
694 offscreen_box->child2,