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);
238 gtk_offscreen_box_realize (GtkWidget *widget)
240 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
241 GtkAllocation allocation, child_area;
244 GdkWindowAttr attributes;
245 gint attributes_mask;
247 GtkRequisition child_requisition;
250 gtk_widget_set_realized (widget, TRUE);
252 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
254 gtk_widget_get_allocation (widget, &allocation);
256 attributes.x = allocation.x + border_width;
257 attributes.y = allocation.y + border_width;
258 attributes.width = allocation.width - 2 * border_width;
259 attributes.height = allocation.height - 2 * border_width;
260 attributes.window_type = GDK_WINDOW_CHILD;
261 attributes.event_mask = gtk_widget_get_events (widget)
263 | GDK_POINTER_MOTION_MASK
264 | GDK_BUTTON_PRESS_MASK
265 | GDK_BUTTON_RELEASE_MASK
267 | GDK_ENTER_NOTIFY_MASK
268 | GDK_LEAVE_NOTIFY_MASK;
270 attributes.visual = gtk_widget_get_visual (widget);
271 attributes.wclass = GDK_INPUT_OUTPUT;
273 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
275 window = gdk_window_new (gtk_widget_get_parent_window (widget),
276 &attributes, attributes_mask);
277 gtk_widget_set_window (widget, window);
278 gdk_window_set_user_data (window, widget);
280 g_signal_connect (window, "pick-embedded-child",
281 G_CALLBACK (pick_offscreen_child), offscreen_box);
283 attributes.window_type = GDK_WINDOW_OFFSCREEN;
286 attributes.x = attributes.y = 0;
287 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
289 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
291 attributes.width = child_area.width;
292 attributes.height = child_area.height;
293 start_y += child_area.height;
295 offscreen_box->offscreen_window1 = gdk_window_new (gtk_widget_get_root_window (widget),
296 &attributes, attributes_mask);
297 gdk_window_set_user_data (offscreen_box->offscreen_window1, widget);
298 if (offscreen_box->child1)
299 gtk_widget_set_parent_window (offscreen_box->child1, offscreen_box->offscreen_window1);
301 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window1,
304 g_signal_connect (offscreen_box->offscreen_window1, "to-embedder",
305 G_CALLBACK (offscreen_window_to_parent1), offscreen_box);
306 g_signal_connect (offscreen_box->offscreen_window1, "from-embedder",
307 G_CALLBACK (offscreen_window_from_parent1), offscreen_box);
310 attributes.y = start_y;
311 child_requisition.width = child_requisition.height = 0;
312 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
314 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
316 attributes.width = child_area.width;
317 attributes.height = child_area.height;
319 offscreen_box->offscreen_window2 = gdk_window_new (gtk_widget_get_root_window (widget),
320 &attributes, attributes_mask);
321 gdk_window_set_user_data (offscreen_box->offscreen_window2, widget);
322 if (offscreen_box->child2)
323 gtk_widget_set_parent_window (offscreen_box->child2, offscreen_box->offscreen_window2);
324 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window2,
326 g_signal_connect (offscreen_box->offscreen_window2, "to-embedder",
327 G_CALLBACK (offscreen_window_to_parent2), offscreen_box);
328 g_signal_connect (offscreen_box->offscreen_window2, "from-embedder",
329 G_CALLBACK (offscreen_window_from_parent2), offscreen_box);
331 gtk_widget_style_attach (widget);
332 style = gtk_widget_get_style (widget);
333 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
334 gtk_style_set_background (style, offscreen_box->offscreen_window1, GTK_STATE_NORMAL);
335 gtk_style_set_background (style, offscreen_box->offscreen_window2, GTK_STATE_NORMAL);
337 gdk_window_show (offscreen_box->offscreen_window1);
338 gdk_window_show (offscreen_box->offscreen_window2);
342 gtk_offscreen_box_unrealize (GtkWidget *widget)
344 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
346 gdk_window_set_user_data (offscreen_box->offscreen_window1, NULL);
347 gdk_window_destroy (offscreen_box->offscreen_window1);
348 offscreen_box->offscreen_window1 = NULL;
350 gdk_window_set_user_data (offscreen_box->offscreen_window2, NULL);
351 gdk_window_destroy (offscreen_box->offscreen_window2);
352 offscreen_box->offscreen_window2 = NULL;
354 GTK_WIDGET_CLASS (gtk_offscreen_box_parent_class)->unrealize (widget);
358 gtk_offscreen_box_child_type (GtkContainer *container)
360 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
362 if (offscreen_box->child1 && offscreen_box->child2)
365 return GTK_TYPE_WIDGET;
369 gtk_offscreen_box_add (GtkContainer *container,
372 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
374 if (!offscreen_box->child1)
375 gtk_offscreen_box_add1 (offscreen_box, widget);
376 else if (!offscreen_box->child2)
377 gtk_offscreen_box_add2 (offscreen_box, widget);
379 g_warning ("GtkOffscreenBox cannot have more than 2 children\n");
383 gtk_offscreen_box_add1 (GtkOffscreenBox *offscreen_box,
386 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
387 g_return_if_fail (GTK_IS_WIDGET (child));
389 if (offscreen_box->child1 == NULL)
391 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window1);
392 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
393 offscreen_box->child1 = child;
398 gtk_offscreen_box_add2 (GtkOffscreenBox *offscreen_box,
401 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
402 g_return_if_fail (GTK_IS_WIDGET (child));
404 if (offscreen_box->child2 == NULL)
406 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window2);
407 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
408 offscreen_box->child2 = child;
413 gtk_offscreen_box_remove (GtkContainer *container,
416 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
417 gboolean was_visible;
419 was_visible = gtk_widget_get_visible (widget);
421 if (offscreen_box->child1 == widget)
423 gtk_widget_unparent (widget);
425 offscreen_box->child1 = NULL;
427 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
428 gtk_widget_queue_resize (GTK_WIDGET (container));
430 else if (offscreen_box->child2 == widget)
432 gtk_widget_unparent (widget);
434 offscreen_box->child2 = NULL;
436 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
437 gtk_widget_queue_resize (GTK_WIDGET (container));
442 gtk_offscreen_box_forall (GtkContainer *container,
443 gboolean include_internals,
444 GtkCallback callback,
445 gpointer callback_data)
447 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
449 g_return_if_fail (callback != NULL);
451 if (offscreen_box->child1)
452 (*callback) (offscreen_box->child1, callback_data);
453 if (offscreen_box->child2)
454 (*callback) (offscreen_box->child2, callback_data);
458 gtk_offscreen_box_set_angle (GtkOffscreenBox *offscreen_box,
461 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
463 offscreen_box->angle = angle;
464 gtk_widget_queue_draw (GTK_WIDGET (offscreen_box));
466 /* TODO: Really needs to resent pointer events if over the rotated window */
471 gtk_offscreen_box_size_request (GtkWidget *widget,
472 GtkRequisition *requisition)
474 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
481 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
483 GtkRequisition child_requisition;
485 gtk_widget_get_preferred_size ( (offscreen_box->child1),
486 &child_requisition, NULL);
488 w = MAX (w, CHILD1_SIZE_SCALE * child_requisition.width);
489 h += CHILD1_SIZE_SCALE * child_requisition.height;
492 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
494 GtkRequisition child_requisition;
496 gtk_widget_get_preferred_size ( (offscreen_box->child2),
497 &child_requisition, NULL);
499 w = MAX (w, CHILD2_SIZE_SCALE * child_requisition.width);
500 h += CHILD2_SIZE_SCALE * child_requisition.height;
503 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
504 requisition->width = border_width * 2 + w;
505 requisition->height = border_width * 2 + h;
509 gtk_offscreen_box_size_allocate (GtkWidget *widget,
510 GtkAllocation *allocation)
512 GtkOffscreenBox *offscreen_box;
516 offscreen_box = GTK_OFFSCREEN_BOX (widget);
518 gtk_widget_set_allocation (widget, allocation);
520 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
522 if (gtk_widget_get_realized (widget))
523 gdk_window_move_resize (gtk_widget_get_window (widget),
524 allocation->x + border_width,
525 allocation->y + border_width,
526 allocation->width - border_width * 2,
527 allocation->height - border_width * 2);
531 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
533 GtkRequisition child_requisition;
534 GtkAllocation child_allocation;
536 gtk_widget_get_preferred_size (offscreen_box->child1,
537 &child_requisition, NULL);
538 child_allocation.x = child_requisition.width * (CHILD1_SIZE_SCALE - 1.0) / 2;
539 child_allocation.y = start_y + child_requisition.height * (CHILD1_SIZE_SCALE - 1.0) / 2;
540 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
541 child_allocation.height = child_requisition.height;
543 start_y += CHILD1_SIZE_SCALE * child_requisition.height;
545 if (gtk_widget_get_realized (widget))
546 gdk_window_move_resize (offscreen_box->offscreen_window1,
549 child_allocation.width,
550 child_allocation.height);
552 child_allocation.x = child_allocation.y = 0;
553 gtk_widget_size_allocate (offscreen_box->child1, &child_allocation);
556 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
558 GtkRequisition child_requisition;
559 GtkAllocation child_allocation;
561 gtk_widget_get_preferred_size (offscreen_box->child2,
562 &child_requisition, NULL);
563 child_allocation.x = child_requisition.width * (CHILD2_SIZE_SCALE - 1.0) / 2;
564 child_allocation.y = start_y + child_requisition.height * (CHILD2_SIZE_SCALE - 1.0) / 2;
565 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
566 child_allocation.height = child_requisition.height;
568 start_y += CHILD2_SIZE_SCALE * child_requisition.height;
570 if (gtk_widget_get_realized (widget))
571 gdk_window_move_resize (offscreen_box->offscreen_window2,
574 child_allocation.width,
575 child_allocation.height);
577 child_allocation.x = child_allocation.y = 0;
578 gtk_widget_size_allocate (offscreen_box->child2, &child_allocation);
583 gtk_offscreen_box_damage (GtkWidget *widget,
584 GdkEventExpose *event)
586 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
593 gtk_offscreen_box_draw (GtkWidget *widget,
596 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
599 window = gtk_widget_get_window (widget);
600 if (gtk_cairo_should_draw_window (cr, window))
602 cairo_surface_t *surface;
603 GtkAllocation child_area;
605 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
607 surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window1);
609 cairo_set_source_surface (cr, surface, 0, 0);
612 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
613 cairo_translate (cr, 0, child_area.height);
616 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
618 surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window2);
620 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
623 cairo_translate (cr, child_area.width / 2, child_area.height / 2);
624 cairo_rotate (cr, offscreen_box->angle);
625 cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
628 cairo_set_source_surface (cr, surface, 0, 0);
632 else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window1))
634 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
635 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
638 gdk_window_get_width (offscreen_box->offscreen_window1),
639 gdk_window_get_height (offscreen_box->offscreen_window1));
641 if (offscreen_box->child1)
642 gtk_container_propagate_draw (GTK_CONTAINER (widget),
643 offscreen_box->child1,
646 else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window2))
648 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
649 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
652 gdk_window_get_width (offscreen_box->offscreen_window2),
653 gdk_window_get_height (offscreen_box->offscreen_window2));
655 if (offscreen_box->child2)
656 gtk_container_propagate_draw (GTK_CONTAINER (widget),
657 offscreen_box->child2,