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 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
51 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
52 y -= child_area.height;
54 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
56 x -= child_area.width / 2;
57 y -= child_area.height / 2;
59 cos_angle = cos (-offscreen_box->angle);
60 sin_angle = sin (-offscreen_box->angle);
62 xr = x * cos_angle - y * sin_angle;
63 yr = x * sin_angle + y * cos_angle;
67 x += child_area.width / 2;
68 y += child_area.height / 2;
75 to_parent_2 (GtkOffscreenBox *offscreen_box,
76 double offscreen_x, double offscreen_y,
77 double *x_out, double *y_out)
79 GtkAllocation child_area;
81 double cos_angle, sin_angle;
83 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
88 x -= child_area.width / 2;
89 y -= child_area.height / 2;
91 cos_angle = cos (offscreen_box->angle);
92 sin_angle = sin (offscreen_box->angle);
94 xr = x * cos_angle - y * sin_angle;
95 yr = x * sin_angle + y * cos_angle;
99 x += child_area.width / 2;
100 y += child_area.height / 2;
102 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
103 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
104 y += child_area.height;
111 gtk_offscreen_box_class_init (GtkOffscreenBoxClass *klass)
113 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
114 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
116 widget_class->realize = gtk_offscreen_box_realize;
117 widget_class->unrealize = gtk_offscreen_box_unrealize;
118 widget_class->size_request = gtk_offscreen_box_size_request;
119 widget_class->size_allocate = gtk_offscreen_box_size_allocate;
120 widget_class->draw = gtk_offscreen_box_draw;
122 g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
123 GTK_TYPE_OFFSCREEN_BOX,
124 g_cclosure_new (G_CALLBACK (gtk_offscreen_box_damage),
127 container_class->add = gtk_offscreen_box_add;
128 container_class->remove = gtk_offscreen_box_remove;
129 container_class->forall = gtk_offscreen_box_forall;
130 container_class->child_type = gtk_offscreen_box_child_type;
134 gtk_offscreen_box_init (GtkOffscreenBox *offscreen_box)
136 gtk_widget_set_has_window (GTK_WIDGET (offscreen_box), TRUE);
140 gtk_offscreen_box_new (void)
142 return g_object_new (GTK_TYPE_OFFSCREEN_BOX, NULL);
146 pick_offscreen_child (GdkWindow *offscreen_window,
147 double widget_x, double widget_y,
148 GtkOffscreenBox *offscreen_box)
150 GtkAllocation child_area;
153 /* First check for child 2 */
154 if (offscreen_box->child2 &&
155 gtk_widget_get_visible (offscreen_box->child2))
157 to_child_2 (offscreen_box,
161 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
163 if (x >= 0 && x < child_area.width &&
164 y >= 0 && y < child_area.height)
165 return offscreen_box->offscreen_window2;
168 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
173 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
175 if (x >= 0 && x < child_area.width &&
176 y >= 0 && y < child_area.height)
177 return offscreen_box->offscreen_window1;
184 offscreen_window_to_parent1 (GdkWindow *offscreen_window,
189 GtkOffscreenBox *offscreen_box)
191 *parent_x = offscreen_x;
192 *parent_y = offscreen_y;
196 offscreen_window_from_parent1 (GdkWindow *window,
201 GtkOffscreenBox *offscreen_box)
203 *offscreen_x = parent_x;
204 *offscreen_y = parent_y;
208 offscreen_window_to_parent2 (GdkWindow *offscreen_window,
213 GtkOffscreenBox *offscreen_box)
215 to_parent_2 (offscreen_box,
216 offscreen_x, offscreen_y,
221 offscreen_window_from_parent2 (GdkWindow *window,
226 GtkOffscreenBox *offscreen_box)
228 to_child_2 (offscreen_box,
230 offscreen_x, offscreen_y);
234 gtk_offscreen_box_realize (GtkWidget *widget)
236 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
237 GtkAllocation allocation, child_area;
240 GdkWindowAttr attributes;
241 gint attributes_mask;
243 GtkRequisition child_requisition;
246 gtk_widget_set_realized (widget, TRUE);
248 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
250 gtk_widget_get_allocation (widget, &allocation);
252 attributes.x = allocation.x + border_width;
253 attributes.y = allocation.y + border_width;
254 attributes.width = allocation.width - 2 * border_width;
255 attributes.height = allocation.height - 2 * border_width;
256 attributes.window_type = GDK_WINDOW_CHILD;
257 attributes.event_mask = gtk_widget_get_events (widget)
259 | GDK_POINTER_MOTION_MASK
260 | GDK_BUTTON_PRESS_MASK
261 | GDK_BUTTON_RELEASE_MASK
263 | GDK_ENTER_NOTIFY_MASK
264 | GDK_LEAVE_NOTIFY_MASK;
266 attributes.visual = gtk_widget_get_visual (widget);
267 attributes.wclass = GDK_INPUT_OUTPUT;
269 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
271 window = gdk_window_new (gtk_widget_get_parent_window (widget),
272 &attributes, attributes_mask);
273 gtk_widget_set_window (widget, window);
274 gdk_window_set_user_data (window, widget);
276 g_signal_connect (window, "pick-embedded-child",
277 G_CALLBACK (pick_offscreen_child), offscreen_box);
279 attributes.window_type = GDK_WINDOW_OFFSCREEN;
282 attributes.x = attributes.y = 0;
283 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
285 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
287 attributes.width = child_area.width;
288 attributes.height = child_area.height;
289 start_y += child_area.height;
291 offscreen_box->offscreen_window1 = gdk_window_new (gtk_widget_get_root_window (widget),
292 &attributes, attributes_mask);
293 gdk_window_set_user_data (offscreen_box->offscreen_window1, widget);
294 if (offscreen_box->child1)
295 gtk_widget_set_parent_window (offscreen_box->child1, offscreen_box->offscreen_window1);
297 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window1,
300 g_signal_connect (offscreen_box->offscreen_window1, "to-embedder",
301 G_CALLBACK (offscreen_window_to_parent1), offscreen_box);
302 g_signal_connect (offscreen_box->offscreen_window1, "from-embedder",
303 G_CALLBACK (offscreen_window_from_parent1), offscreen_box);
306 attributes.y = start_y;
307 child_requisition.width = child_requisition.height = 0;
308 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
310 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
312 attributes.width = child_area.width;
313 attributes.height = child_area.height;
315 offscreen_box->offscreen_window2 = gdk_window_new (gtk_widget_get_root_window (widget),
316 &attributes, attributes_mask);
317 gdk_window_set_user_data (offscreen_box->offscreen_window2, widget);
318 if (offscreen_box->child2)
319 gtk_widget_set_parent_window (offscreen_box->child2, offscreen_box->offscreen_window2);
320 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window2,
322 g_signal_connect (offscreen_box->offscreen_window2, "to-embedder",
323 G_CALLBACK (offscreen_window_to_parent2), offscreen_box);
324 g_signal_connect (offscreen_box->offscreen_window2, "from-embedder",
325 G_CALLBACK (offscreen_window_from_parent2), offscreen_box);
327 gtk_widget_style_attach (widget);
328 style = gtk_widget_get_style (widget);
329 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
330 gtk_style_set_background (style, offscreen_box->offscreen_window1, GTK_STATE_NORMAL);
331 gtk_style_set_background (style, offscreen_box->offscreen_window2, GTK_STATE_NORMAL);
333 gdk_window_show (offscreen_box->offscreen_window1);
334 gdk_window_show (offscreen_box->offscreen_window2);
338 gtk_offscreen_box_unrealize (GtkWidget *widget)
340 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
342 gdk_window_set_user_data (offscreen_box->offscreen_window1, NULL);
343 gdk_window_destroy (offscreen_box->offscreen_window1);
344 offscreen_box->offscreen_window1 = NULL;
346 gdk_window_set_user_data (offscreen_box->offscreen_window2, NULL);
347 gdk_window_destroy (offscreen_box->offscreen_window2);
348 offscreen_box->offscreen_window2 = NULL;
350 GTK_WIDGET_CLASS (gtk_offscreen_box_parent_class)->unrealize (widget);
354 gtk_offscreen_box_child_type (GtkContainer *container)
356 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
358 if (offscreen_box->child1 && offscreen_box->child2)
361 return GTK_TYPE_WIDGET;
365 gtk_offscreen_box_add (GtkContainer *container,
368 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
370 if (!offscreen_box->child1)
371 gtk_offscreen_box_add1 (offscreen_box, widget);
372 else if (!offscreen_box->child2)
373 gtk_offscreen_box_add2 (offscreen_box, widget);
375 g_warning ("GtkOffscreenBox cannot have more than 2 children\n");
379 gtk_offscreen_box_add1 (GtkOffscreenBox *offscreen_box,
382 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
383 g_return_if_fail (GTK_IS_WIDGET (child));
385 if (offscreen_box->child1 == NULL)
387 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window1);
388 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
389 offscreen_box->child1 = child;
394 gtk_offscreen_box_add2 (GtkOffscreenBox *offscreen_box,
397 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
398 g_return_if_fail (GTK_IS_WIDGET (child));
400 if (offscreen_box->child2 == NULL)
402 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window2);
403 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
404 offscreen_box->child2 = child;
409 gtk_offscreen_box_remove (GtkContainer *container,
412 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
413 gboolean was_visible;
415 was_visible = gtk_widget_get_visible (widget);
417 if (offscreen_box->child1 == widget)
419 gtk_widget_unparent (widget);
421 offscreen_box->child1 = NULL;
423 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
424 gtk_widget_queue_resize (GTK_WIDGET (container));
426 else if (offscreen_box->child2 == widget)
428 gtk_widget_unparent (widget);
430 offscreen_box->child2 = NULL;
432 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
433 gtk_widget_queue_resize (GTK_WIDGET (container));
438 gtk_offscreen_box_forall (GtkContainer *container,
439 gboolean include_internals,
440 GtkCallback callback,
441 gpointer callback_data)
443 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
445 g_return_if_fail (callback != NULL);
447 if (offscreen_box->child1)
448 (*callback) (offscreen_box->child1, callback_data);
449 if (offscreen_box->child2)
450 (*callback) (offscreen_box->child2, callback_data);
454 gtk_offscreen_box_set_angle (GtkOffscreenBox *offscreen_box,
457 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
459 offscreen_box->angle = angle;
460 gtk_widget_queue_draw (GTK_WIDGET (offscreen_box));
462 /* TODO: Really needs to resent pointer events if over the rotated window */
467 gtk_offscreen_box_size_request (GtkWidget *widget,
468 GtkRequisition *requisition)
470 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
477 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
479 GtkRequisition child_requisition;
481 gtk_size_request_get_size (GTK_SIZE_REQUEST (offscreen_box->child1),
482 &child_requisition, NULL);
484 w = MAX (w, CHILD1_SIZE_SCALE * child_requisition.width);
485 h += CHILD1_SIZE_SCALE * child_requisition.height;
488 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
490 GtkRequisition child_requisition;
492 gtk_size_request_get_size (GTK_SIZE_REQUEST (offscreen_box->child2),
493 &child_requisition, NULL);
495 w = MAX (w, CHILD2_SIZE_SCALE * child_requisition.width);
496 h += CHILD2_SIZE_SCALE * child_requisition.height;
499 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
500 requisition->width = border_width * 2 + w;
501 requisition->height = border_width * 2 + h;
505 gtk_offscreen_box_size_allocate (GtkWidget *widget,
506 GtkAllocation *allocation)
508 GtkOffscreenBox *offscreen_box;
512 offscreen_box = GTK_OFFSCREEN_BOX (widget);
514 gtk_widget_set_allocation (widget, allocation);
516 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
518 if (gtk_widget_get_realized (widget))
519 gdk_window_move_resize (gtk_widget_get_window (widget),
520 allocation->x + border_width,
521 allocation->y + border_width,
522 allocation->width - border_width * 2,
523 allocation->height - border_width * 2);
527 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
529 GtkRequisition child_requisition;
530 GtkAllocation child_allocation;
532 gtk_size_request_get_size (GTK_SIZE_REQUEST (offscreen_box->child1),
533 &child_requisition, NULL);
534 child_allocation.x = child_requisition.width * (CHILD1_SIZE_SCALE - 1.0) / 2;
535 child_allocation.y = start_y + child_requisition.height * (CHILD1_SIZE_SCALE - 1.0) / 2;
536 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
537 child_allocation.height = child_requisition.height;
539 start_y += CHILD1_SIZE_SCALE * child_requisition.height;
541 if (gtk_widget_get_realized (widget))
542 gdk_window_move_resize (offscreen_box->offscreen_window1,
545 child_allocation.width,
546 child_allocation.height);
548 child_allocation.x = child_allocation.y = 0;
549 gtk_widget_size_allocate (offscreen_box->child1, &child_allocation);
552 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
554 GtkRequisition child_requisition;
555 GtkAllocation child_allocation;
557 gtk_size_request_get_size (GTK_SIZE_REQUEST (offscreen_box->child2),
558 &child_requisition, NULL);
559 child_allocation.x = child_requisition.width * (CHILD2_SIZE_SCALE - 1.0) / 2;
560 child_allocation.y = start_y + child_requisition.height * (CHILD2_SIZE_SCALE - 1.0) / 2;
561 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
562 child_allocation.height = child_requisition.height;
564 start_y += CHILD2_SIZE_SCALE * child_requisition.height;
566 if (gtk_widget_get_realized (widget))
567 gdk_window_move_resize (offscreen_box->offscreen_window2,
570 child_allocation.width,
571 child_allocation.height);
573 child_allocation.x = child_allocation.y = 0;
574 gtk_widget_size_allocate (offscreen_box->child2, &child_allocation);
579 gtk_offscreen_box_damage (GtkWidget *widget,
580 GdkEventExpose *event)
582 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
589 gtk_offscreen_box_draw (GtkWidget *widget,
592 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
595 window = gtk_widget_get_window (widget);
596 if (gtk_cairo_should_draw_window (cr, window))
598 cairo_surface_t *surface;
599 GtkAllocation child_area;
601 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
603 surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window1);
605 cairo_set_source_surface (cr, surface, 0, 0);
608 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
609 cairo_translate (cr, 0, child_area.height);
612 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
614 surface = gdk_offscreen_window_get_surface (offscreen_box->offscreen_window2);
615 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
618 cairo_translate (cr, child_area.width / 2, child_area.height / 2);
619 cairo_rotate (cr, offscreen_box->angle);
620 cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
623 cairo_set_source_surface (cr, surface, 0, 0);
627 else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window1))
629 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
630 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
633 gdk_window_get_width (offscreen_box->offscreen_window1),
634 gdk_window_get_height (offscreen_box->offscreen_window1));
636 if (offscreen_box->child1)
637 gtk_container_propagate_draw (GTK_CONTAINER (widget),
638 offscreen_box->child1,
641 else if (gtk_cairo_should_draw_window (cr, offscreen_box->offscreen_window2))
643 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
644 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
647 gdk_window_get_width (offscreen_box->offscreen_window2),
648 gdk_window_get_height (offscreen_box->offscreen_window2));
650 if (offscreen_box->child2)
651 gtk_container_propagate_draw (GTK_CONTAINER (widget),
652 offscreen_box->child2,