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_expose (GtkWidget *widget,
21 GdkEventExpose *offscreen);
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))
51 y -= offscreen_box->child1->allocation.height;
53 child_area = offscreen_box->child2->allocation;
55 x -= child_area.width / 2;
56 y -= child_area.height / 2;
58 cos_angle = cos (-offscreen_box->angle);
59 sin_angle = sin (-offscreen_box->angle);
61 xr = x * cos_angle - y * sin_angle;
62 yr = x * sin_angle + y * cos_angle;
66 x += child_area.width / 2;
67 y += child_area.height / 2;
74 to_parent_2 (GtkOffscreenBox *offscreen_box,
75 double offscreen_x, double offscreen_y,
76 double *x_out, double *y_out)
78 GtkAllocation child_area;
80 double cos_angle, sin_angle;
82 child_area = offscreen_box->child2->allocation;
87 x -= child_area.width / 2;
88 y -= child_area.height / 2;
90 cos_angle = cos (offscreen_box->angle);
91 sin_angle = sin (offscreen_box->angle);
93 xr = x * cos_angle - y * sin_angle;
94 yr = x * sin_angle + y * cos_angle;
98 x += child_area.width / 2;
99 y += child_area.height / 2;
101 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
102 y += offscreen_box->child1->allocation.height;
109 gtk_offscreen_box_class_init (GtkOffscreenBoxClass *klass)
111 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
112 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
114 widget_class->realize = gtk_offscreen_box_realize;
115 widget_class->unrealize = gtk_offscreen_box_unrealize;
116 widget_class->size_request = gtk_offscreen_box_size_request;
117 widget_class->size_allocate = gtk_offscreen_box_size_allocate;
118 widget_class->expose_event = gtk_offscreen_box_expose;
120 g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
121 GTK_TYPE_OFFSCREEN_BOX,
122 g_cclosure_new (G_CALLBACK (gtk_offscreen_box_damage),
125 container_class->add = gtk_offscreen_box_add;
126 container_class->remove = gtk_offscreen_box_remove;
127 container_class->forall = gtk_offscreen_box_forall;
128 container_class->child_type = gtk_offscreen_box_child_type;
132 gtk_offscreen_box_init (GtkOffscreenBox *offscreen_box)
134 GTK_WIDGET_UNSET_FLAGS (offscreen_box, GTK_NO_WINDOW);
138 gtk_offscreen_box_new (void)
140 return g_object_new (GTK_TYPE_OFFSCREEN_BOX, NULL);
144 pick_offscreen_child (GdkWindow *offscreen_window,
145 double widget_x, double widget_y,
146 GtkOffscreenBox *offscreen_box)
148 GtkAllocation child_area;
151 /* First check for child 2 */
152 if (offscreen_box->child2 &&
153 gtk_widget_get_visible (offscreen_box->child2))
155 to_child_2 (offscreen_box,
159 child_area = offscreen_box->child2->allocation;
161 if (x >= 0 && x < child_area.width &&
162 y >= 0 && y < child_area.height)
163 return offscreen_box->offscreen_window2;
166 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
171 child_area = offscreen_box->child1->allocation;
173 if (x >= 0 && x < child_area.width &&
174 y >= 0 && y < child_area.height)
175 return offscreen_box->offscreen_window1;
182 offscreen_window_to_parent1 (GdkWindow *offscreen_window,
187 GtkOffscreenBox *offscreen_box)
189 *parent_x = offscreen_x;
190 *parent_y = offscreen_y;
194 offscreen_window_from_parent1 (GdkWindow *window,
199 GtkOffscreenBox *offscreen_box)
201 *offscreen_x = parent_x;
202 *offscreen_y = parent_y;
206 offscreen_window_to_parent2 (GdkWindow *offscreen_window,
211 GtkOffscreenBox *offscreen_box)
213 to_parent_2 (offscreen_box,
214 offscreen_x, offscreen_y,
219 offscreen_window_from_parent2 (GdkWindow *window,
224 GtkOffscreenBox *offscreen_box)
226 to_child_2 (offscreen_box,
228 offscreen_x, offscreen_y);
232 gtk_offscreen_box_realize (GtkWidget *widget)
234 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
235 GdkWindowAttr attributes;
236 gint attributes_mask;
238 GtkRequisition child_requisition;
241 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
243 border_width = GTK_CONTAINER (widget)->border_width;
245 attributes.x = widget->allocation.x + border_width;
246 attributes.y = widget->allocation.y + border_width;
247 attributes.width = widget->allocation.width - 2 * border_width;
248 attributes.height = widget->allocation.height - 2 * border_width;
249 attributes.window_type = GDK_WINDOW_CHILD;
250 attributes.event_mask = gtk_widget_get_events (widget)
252 | GDK_POINTER_MOTION_MASK
253 | GDK_BUTTON_PRESS_MASK
254 | GDK_BUTTON_RELEASE_MASK
256 | GDK_ENTER_NOTIFY_MASK
257 | GDK_LEAVE_NOTIFY_MASK;
259 attributes.visual = gtk_widget_get_visual (widget);
260 attributes.colormap = gtk_widget_get_colormap (widget);
261 attributes.wclass = GDK_INPUT_OUTPUT;
263 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
265 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
266 &attributes, attributes_mask);
267 gdk_window_set_user_data (widget->window, widget);
269 g_signal_connect (widget->window, "pick-embedded-child",
270 G_CALLBACK (pick_offscreen_child), offscreen_box);
272 attributes.window_type = GDK_WINDOW_OFFSCREEN;
275 attributes.x = attributes.y = 0;
276 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
278 attributes.width = offscreen_box->child1->allocation.width;
279 attributes.height = offscreen_box->child1->allocation.height;
280 start_y += offscreen_box->child1->allocation.height;
282 offscreen_box->offscreen_window1 = gdk_window_new (gtk_widget_get_root_window (widget),
283 &attributes, attributes_mask);
284 gdk_window_set_user_data (offscreen_box->offscreen_window1, widget);
285 if (offscreen_box->child1)
286 gtk_widget_set_parent_window (offscreen_box->child1, offscreen_box->offscreen_window1);
288 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window1,
291 g_signal_connect (offscreen_box->offscreen_window1, "to-embedder",
292 G_CALLBACK (offscreen_window_to_parent1), offscreen_box);
293 g_signal_connect (offscreen_box->offscreen_window1, "from-embedder",
294 G_CALLBACK (offscreen_window_from_parent1), offscreen_box);
297 attributes.y = start_y;
298 child_requisition.width = child_requisition.height = 0;
299 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
301 attributes.width = offscreen_box->child2->allocation.width;
302 attributes.height = offscreen_box->child2->allocation.height;
304 offscreen_box->offscreen_window2 = gdk_window_new (gtk_widget_get_root_window (widget),
305 &attributes, attributes_mask);
306 gdk_window_set_user_data (offscreen_box->offscreen_window2, widget);
307 if (offscreen_box->child2)
308 gtk_widget_set_parent_window (offscreen_box->child2, offscreen_box->offscreen_window2);
309 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window2,
311 g_signal_connect (offscreen_box->offscreen_window2, "to-embedder",
312 G_CALLBACK (offscreen_window_to_parent2), offscreen_box);
313 g_signal_connect (offscreen_box->offscreen_window2, "from-embedder",
314 G_CALLBACK (offscreen_window_from_parent2), offscreen_box);
316 widget->style = gtk_style_attach (widget->style, widget->window);
318 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
319 gtk_style_set_background (widget->style, offscreen_box->offscreen_window1, GTK_STATE_NORMAL);
320 gtk_style_set_background (widget->style, offscreen_box->offscreen_window2, GTK_STATE_NORMAL);
322 gdk_window_show (offscreen_box->offscreen_window1);
323 gdk_window_show (offscreen_box->offscreen_window2);
327 gtk_offscreen_box_unrealize (GtkWidget *widget)
329 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
331 gdk_window_set_user_data (offscreen_box->offscreen_window1, NULL);
332 gdk_window_destroy (offscreen_box->offscreen_window1);
333 offscreen_box->offscreen_window1 = NULL;
335 gdk_window_set_user_data (offscreen_box->offscreen_window2, NULL);
336 gdk_window_destroy (offscreen_box->offscreen_window2);
337 offscreen_box->offscreen_window2 = NULL;
339 GTK_WIDGET_CLASS (gtk_offscreen_box_parent_class)->unrealize (widget);
343 gtk_offscreen_box_child_type (GtkContainer *container)
345 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
347 if (offscreen_box->child1 && offscreen_box->child2)
350 return GTK_TYPE_WIDGET;
354 gtk_offscreen_box_add (GtkContainer *container,
357 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
359 if (!offscreen_box->child1)
360 gtk_offscreen_box_add1 (offscreen_box, widget);
361 else if (!offscreen_box->child2)
362 gtk_offscreen_box_add2 (offscreen_box, widget);
364 g_warning ("GtkOffscreenBox cannot have more than 2 children\n");
368 gtk_offscreen_box_add1 (GtkOffscreenBox *offscreen_box,
371 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
372 g_return_if_fail (GTK_IS_WIDGET (child));
374 if (offscreen_box->child1 == NULL)
376 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window1);
377 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
378 offscreen_box->child1 = child;
383 gtk_offscreen_box_add2 (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->child2 == NULL)
391 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window2);
392 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
393 offscreen_box->child2 = child;
398 gtk_offscreen_box_remove (GtkContainer *container,
401 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
402 gboolean was_visible;
404 was_visible = gtk_widget_get_visible (widget);
406 if (offscreen_box->child1 == widget)
408 gtk_widget_unparent (widget);
410 offscreen_box->child1 = NULL;
412 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
413 gtk_widget_queue_resize (GTK_WIDGET (container));
415 else if (offscreen_box->child2 == widget)
417 gtk_widget_unparent (widget);
419 offscreen_box->child2 = NULL;
421 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
422 gtk_widget_queue_resize (GTK_WIDGET (container));
427 gtk_offscreen_box_forall (GtkContainer *container,
428 gboolean include_internals,
429 GtkCallback callback,
430 gpointer callback_data)
432 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
434 g_return_if_fail (callback != NULL);
436 if (offscreen_box->child1)
437 (*callback) (offscreen_box->child1, callback_data);
438 if (offscreen_box->child2)
439 (*callback) (offscreen_box->child2, callback_data);
443 gtk_offscreen_box_set_angle (GtkOffscreenBox *offscreen_box,
446 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
448 offscreen_box->angle = angle;
449 gtk_widget_queue_draw (GTK_WIDGET (offscreen_box));
451 /* TODO: Really needs to resent pointer events if over the rotated window */
456 gtk_offscreen_box_size_request (GtkWidget *widget,
457 GtkRequisition *requisition)
459 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
465 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
467 GtkRequisition child_requisition;
469 gtk_widget_size_request (offscreen_box->child1, &child_requisition);
471 w = MAX (w, CHILD1_SIZE_SCALE * child_requisition.width);
472 h += CHILD1_SIZE_SCALE * child_requisition.height;
475 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
477 GtkRequisition child_requisition;
479 gtk_widget_size_request (offscreen_box->child2, &child_requisition);
481 w = MAX (w, CHILD2_SIZE_SCALE * child_requisition.width);
482 h += CHILD2_SIZE_SCALE * child_requisition.height;
485 requisition->width = GTK_CONTAINER (widget)->border_width * 2 + w;
486 requisition->height = GTK_CONTAINER (widget)->border_width * 2 + h;
490 gtk_offscreen_box_size_allocate (GtkWidget *widget,
491 GtkAllocation *allocation)
493 GtkOffscreenBox *offscreen_box;
497 widget->allocation = *allocation;
498 offscreen_box = GTK_OFFSCREEN_BOX (widget);
500 border_width = GTK_CONTAINER (widget)->border_width;
502 if (GTK_WIDGET_REALIZED (widget))
503 gdk_window_move_resize (widget->window,
504 allocation->x + border_width,
505 allocation->y + border_width,
506 allocation->width - border_width * 2,
507 allocation->height - border_width * 2);
511 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
513 GtkRequisition child_requisition;
514 GtkAllocation child_allocation;
516 gtk_widget_get_child_requisition (offscreen_box->child1, &child_requisition);
517 child_allocation.x = child_requisition.width * (CHILD1_SIZE_SCALE - 1.0) / 2;
518 child_allocation.y = start_y + child_requisition.height * (CHILD1_SIZE_SCALE - 1.0) / 2;
519 child_allocation.width = MAX (1, (gint) widget->allocation.width - 2 * border_width);
520 child_allocation.height = child_requisition.height;
522 start_y += CHILD1_SIZE_SCALE * child_requisition.height;
524 if (GTK_WIDGET_REALIZED (widget))
525 gdk_window_move_resize (offscreen_box->offscreen_window1,
528 child_allocation.width,
529 child_allocation.height);
531 child_allocation.x = child_allocation.y = 0;
532 gtk_widget_size_allocate (offscreen_box->child1, &child_allocation);
535 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
537 GtkRequisition child_requisition;
538 GtkAllocation child_allocation;
540 gtk_widget_get_child_requisition (offscreen_box->child2, &child_requisition);
541 child_allocation.x = child_requisition.width * (CHILD2_SIZE_SCALE - 1.0) / 2;
542 child_allocation.y = start_y + child_requisition.height * (CHILD2_SIZE_SCALE - 1.0) / 2;
543 child_allocation.width = MAX (1, (gint) widget->allocation.width - 2 * border_width);
544 child_allocation.height = child_requisition.height;
546 start_y += CHILD2_SIZE_SCALE * child_requisition.height;
548 if (GTK_WIDGET_REALIZED (widget))
549 gdk_window_move_resize (offscreen_box->offscreen_window2,
552 child_allocation.width,
553 child_allocation.height);
555 child_allocation.x = child_allocation.y = 0;
556 gtk_widget_size_allocate (offscreen_box->child2, &child_allocation);
561 gtk_offscreen_box_damage (GtkWidget *widget,
562 GdkEventExpose *event)
564 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
570 gtk_offscreen_box_expose (GtkWidget *widget,
571 GdkEventExpose *event)
573 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
575 if (gtk_widget_is_drawable (widget))
577 if (event->window == widget->window)
580 GtkAllocation child_area;
584 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
586 pixmap = gdk_offscreen_window_get_pixmap (offscreen_box->offscreen_window1);
587 child_area = offscreen_box->child1->allocation;
589 cr = gdk_cairo_create (widget->window);
591 gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
596 start_y += child_area.height;
599 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
603 pixmap = gdk_offscreen_window_get_pixmap (offscreen_box->offscreen_window2);
604 child_area = offscreen_box->child2->allocation;
606 cr = gdk_cairo_create (widget->window);
609 cairo_translate (cr, 0, start_y);
610 cairo_translate (cr, child_area.width / 2, child_area.height / 2);
611 cairo_rotate (cr, offscreen_box->angle);
612 cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
615 gdk_drawable_get_size (pixmap, &w, &h);
616 cairo_rectangle (cr, 0, 0, w, h);
620 gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
626 else if (event->window == offscreen_box->offscreen_window1)
628 gtk_paint_flat_box (widget->style, event->window,
629 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
630 &event->area, widget, "blah",
633 if (offscreen_box->child1)
634 gtk_container_propagate_expose (GTK_CONTAINER (widget),
635 offscreen_box->child1,
638 else if (event->window == offscreen_box->offscreen_window2)
640 gtk_paint_flat_box (widget->style, event->window,
641 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
642 &event->area, widget, "blah",
645 if (offscreen_box->child2)
646 gtk_container_propagate_expose (GTK_CONTAINER (widget),
647 offscreen_box->child2,