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 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->expose_event = gtk_offscreen_box_expose;
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.colormap = gtk_widget_get_colormap (widget);
268 attributes.wclass = GDK_INPUT_OUTPUT;
270 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
272 window = gdk_window_new (gtk_widget_get_parent_window (widget),
273 &attributes, attributes_mask);
274 gtk_widget_set_window (widget, window);
275 gdk_window_set_user_data (window, widget);
277 g_signal_connect (window, "pick-embedded-child",
278 G_CALLBACK (pick_offscreen_child), offscreen_box);
280 attributes.window_type = GDK_WINDOW_OFFSCREEN;
283 attributes.x = attributes.y = 0;
284 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
286 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
288 attributes.width = child_area.width;
289 attributes.height = child_area.height;
290 start_y += child_area.height;
292 offscreen_box->offscreen_window1 = gdk_window_new (gtk_widget_get_root_window (widget),
293 &attributes, attributes_mask);
294 gdk_window_set_user_data (offscreen_box->offscreen_window1, widget);
295 if (offscreen_box->child1)
296 gtk_widget_set_parent_window (offscreen_box->child1, offscreen_box->offscreen_window1);
298 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window1,
301 g_signal_connect (offscreen_box->offscreen_window1, "to-embedder",
302 G_CALLBACK (offscreen_window_to_parent1), offscreen_box);
303 g_signal_connect (offscreen_box->offscreen_window1, "from-embedder",
304 G_CALLBACK (offscreen_window_from_parent1), offscreen_box);
307 attributes.y = start_y;
308 child_requisition.width = child_requisition.height = 0;
309 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
311 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
313 attributes.width = child_area.width;
314 attributes.height = child_area.height;
316 offscreen_box->offscreen_window2 = gdk_window_new (gtk_widget_get_root_window (widget),
317 &attributes, attributes_mask);
318 gdk_window_set_user_data (offscreen_box->offscreen_window2, widget);
319 if (offscreen_box->child2)
320 gtk_widget_set_parent_window (offscreen_box->child2, offscreen_box->offscreen_window2);
321 gdk_offscreen_window_set_embedder (offscreen_box->offscreen_window2,
323 g_signal_connect (offscreen_box->offscreen_window2, "to-embedder",
324 G_CALLBACK (offscreen_window_to_parent2), offscreen_box);
325 g_signal_connect (offscreen_box->offscreen_window2, "from-embedder",
326 G_CALLBACK (offscreen_window_from_parent2), offscreen_box);
328 gtk_widget_style_attach (widget);
329 style = gtk_widget_get_style (widget);
330 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
331 gtk_style_set_background (style, offscreen_box->offscreen_window1, GTK_STATE_NORMAL);
332 gtk_style_set_background (style, offscreen_box->offscreen_window2, GTK_STATE_NORMAL);
334 gdk_window_show (offscreen_box->offscreen_window1);
335 gdk_window_show (offscreen_box->offscreen_window2);
339 gtk_offscreen_box_unrealize (GtkWidget *widget)
341 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
343 gdk_window_set_user_data (offscreen_box->offscreen_window1, NULL);
344 gdk_window_destroy (offscreen_box->offscreen_window1);
345 offscreen_box->offscreen_window1 = NULL;
347 gdk_window_set_user_data (offscreen_box->offscreen_window2, NULL);
348 gdk_window_destroy (offscreen_box->offscreen_window2);
349 offscreen_box->offscreen_window2 = NULL;
351 GTK_WIDGET_CLASS (gtk_offscreen_box_parent_class)->unrealize (widget);
355 gtk_offscreen_box_child_type (GtkContainer *container)
357 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
359 if (offscreen_box->child1 && offscreen_box->child2)
362 return GTK_TYPE_WIDGET;
366 gtk_offscreen_box_add (GtkContainer *container,
369 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
371 if (!offscreen_box->child1)
372 gtk_offscreen_box_add1 (offscreen_box, widget);
373 else if (!offscreen_box->child2)
374 gtk_offscreen_box_add2 (offscreen_box, widget);
376 g_warning ("GtkOffscreenBox cannot have more than 2 children\n");
380 gtk_offscreen_box_add1 (GtkOffscreenBox *offscreen_box,
383 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
384 g_return_if_fail (GTK_IS_WIDGET (child));
386 if (offscreen_box->child1 == NULL)
388 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window1);
389 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
390 offscreen_box->child1 = child;
395 gtk_offscreen_box_add2 (GtkOffscreenBox *offscreen_box,
398 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
399 g_return_if_fail (GTK_IS_WIDGET (child));
401 if (offscreen_box->child2 == NULL)
403 gtk_widget_set_parent_window (child, offscreen_box->offscreen_window2);
404 gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box));
405 offscreen_box->child2 = child;
410 gtk_offscreen_box_remove (GtkContainer *container,
413 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
414 gboolean was_visible;
416 was_visible = gtk_widget_get_visible (widget);
418 if (offscreen_box->child1 == widget)
420 gtk_widget_unparent (widget);
422 offscreen_box->child1 = NULL;
424 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
425 gtk_widget_queue_resize (GTK_WIDGET (container));
427 else if (offscreen_box->child2 == widget)
429 gtk_widget_unparent (widget);
431 offscreen_box->child2 = NULL;
433 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
434 gtk_widget_queue_resize (GTK_WIDGET (container));
439 gtk_offscreen_box_forall (GtkContainer *container,
440 gboolean include_internals,
441 GtkCallback callback,
442 gpointer callback_data)
444 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container);
446 g_return_if_fail (callback != NULL);
448 if (offscreen_box->child1)
449 (*callback) (offscreen_box->child1, callback_data);
450 if (offscreen_box->child2)
451 (*callback) (offscreen_box->child2, callback_data);
455 gtk_offscreen_box_set_angle (GtkOffscreenBox *offscreen_box,
458 g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box));
460 offscreen_box->angle = angle;
461 gtk_widget_queue_draw (GTK_WIDGET (offscreen_box));
463 /* TODO: Really needs to resent pointer events if over the rotated window */
468 gtk_offscreen_box_size_request (GtkWidget *widget,
469 GtkRequisition *requisition)
471 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
478 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
480 GtkRequisition child_requisition;
482 gtk_widget_size_request (offscreen_box->child1, &child_requisition);
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_widget_size_request (offscreen_box->child2, &child_requisition);
494 w = MAX (w, CHILD2_SIZE_SCALE * child_requisition.width);
495 h += CHILD2_SIZE_SCALE * child_requisition.height;
498 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
499 requisition->width = border_width * 2 + w;
500 requisition->height = border_width * 2 + h;
504 gtk_offscreen_box_size_allocate (GtkWidget *widget,
505 GtkAllocation *allocation)
507 GtkOffscreenBox *offscreen_box;
511 offscreen_box = GTK_OFFSCREEN_BOX (widget);
513 gtk_widget_set_allocation (widget, allocation);
515 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
517 if (gtk_widget_get_realized (widget))
518 gdk_window_move_resize (gtk_widget_get_window (widget),
519 allocation->x + border_width,
520 allocation->y + border_width,
521 allocation->width - border_width * 2,
522 allocation->height - border_width * 2);
526 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
528 GtkRequisition child_requisition;
529 GtkAllocation child_allocation;
531 gtk_size_request_get_size (GTK_SIZE_REQUEST (offscreen_box->child1),
532 &child_requisition, NULL);
533 child_allocation.x = child_requisition.width * (CHILD1_SIZE_SCALE - 1.0) / 2;
534 child_allocation.y = start_y + child_requisition.height * (CHILD1_SIZE_SCALE - 1.0) / 2;
535 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
536 child_allocation.height = child_requisition.height;
538 start_y += CHILD1_SIZE_SCALE * child_requisition.height;
540 if (gtk_widget_get_realized (widget))
541 gdk_window_move_resize (offscreen_box->offscreen_window1,
544 child_allocation.width,
545 child_allocation.height);
547 child_allocation.x = child_allocation.y = 0;
548 gtk_widget_size_allocate (offscreen_box->child1, &child_allocation);
551 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
553 GtkRequisition child_requisition;
554 GtkAllocation child_allocation;
556 gtk_size_request_get_size (GTK_SIZE_REQUEST (offscreen_box->child2),
557 &child_requisition, NULL);
558 child_allocation.x = child_requisition.width * (CHILD2_SIZE_SCALE - 1.0) / 2;
559 child_allocation.y = start_y + child_requisition.height * (CHILD2_SIZE_SCALE - 1.0) / 2;
560 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
561 child_allocation.height = child_requisition.height;
563 start_y += CHILD2_SIZE_SCALE * child_requisition.height;
565 if (gtk_widget_get_realized (widget))
566 gdk_window_move_resize (offscreen_box->offscreen_window2,
569 child_allocation.width,
570 child_allocation.height);
572 child_allocation.x = child_allocation.y = 0;
573 gtk_widget_size_allocate (offscreen_box->child2, &child_allocation);
578 gtk_offscreen_box_damage (GtkWidget *widget,
579 GdkEventExpose *event)
581 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
588 gtk_offscreen_box_expose (GtkWidget *widget,
589 GdkEventExpose *event)
591 GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget);
594 if (gtk_widget_is_drawable (widget))
596 window = gtk_widget_get_window (widget);
597 if (event->window == window)
600 GtkAllocation child_area;
604 if (offscreen_box->child1 && gtk_widget_get_visible (offscreen_box->child1))
606 pixmap = gdk_offscreen_window_get_pixmap (offscreen_box->offscreen_window1);
607 gtk_widget_get_allocation (offscreen_box->child1, &child_area);
609 cr = gdk_cairo_create (window);
611 gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
616 start_y += child_area.height;
619 if (offscreen_box->child2 && gtk_widget_get_visible (offscreen_box->child2))
623 pixmap = gdk_offscreen_window_get_pixmap (offscreen_box->offscreen_window2);
624 gtk_widget_get_allocation (offscreen_box->child2, &child_area);
626 cr = gdk_cairo_create (window);
629 cairo_translate (cr, 0, start_y);
630 cairo_translate (cr, child_area.width / 2, child_area.height / 2);
631 cairo_rotate (cr, offscreen_box->angle);
632 cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
635 gdk_drawable_get_size (pixmap, &w, &h);
636 cairo_rectangle (cr, 0, 0, w, h);
640 gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
646 else if (event->window == offscreen_box->offscreen_window1)
648 gtk_paint_flat_box (gtk_widget_get_style (widget), event->window,
649 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
650 &event->area, widget, "blah",
653 if (offscreen_box->child1)
654 gtk_container_propagate_expose (GTK_CONTAINER (widget),
655 offscreen_box->child1,
658 else if (event->window == offscreen_box->offscreen_window2)
660 gtk_paint_flat_box (gtk_widget_get_style (widget), event->window,
661 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
662 &event->area, widget, "blah",
665 if (offscreen_box->child2)
666 gtk_container_propagate_expose (GTK_CONTAINER (widget),
667 offscreen_box->child2,