]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/win_gtk.c
Modified generic wxTreeCtrl to work under non-Unix platforms simultaneously
[wxWidgets.git] / src / gtk / win_gtk.c
1 /* ///////////////////////////////////////////////////////////////////////////
2 // Name: win_gtk.c
3 // Purpose: Native GTK+ widget for wxWindows, based on GtkLayout and
4 // GtkFixed. It makes use of the gravity window property and
5 // therefore does not work with GTK 1.0.
6 // Author: Robert Roebling
7 // Id: $Id$
8 // Copyright: (c) 1998 Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////// */
11
12 #ifdef __VMS
13 #define gtk_widget_get_child_requisition gtk_widget_get_child_requisitio
14 #define gtk_marshal_NONE__POINTER_POINTER gtk_marshal_NONE__POINTER_POINT
15 #endif
16
17 #include "wx/gtk/setup.h"
18 #include "wx/gtk/win_gtk.h"
19 #include "gtk/gtksignal.h"
20 #include "gtk/gtkprivate.h"
21 #include "gdk/gdkx.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif /* __cplusplus */
26
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
29 #include <X11/Xatom.h>
30
31 #define IS_ONSCREEN(x,y) ((x >= G_MINSHORT) && (x <= G_MAXSHORT) && \
32 (y >= G_MINSHORT) && (y <= G_MAXSHORT))
33
34 typedef struct _GtkPizzaAdjData GtkPizzaAdjData;
35
36 struct _GtkPizzaAdjData
37 {
38 gint dx;
39 gint dy;
40 };
41
42 static void gtk_pizza_class_init (GtkPizzaClass *klass);
43 static void gtk_pizza_init (GtkPizza *pizza);
44
45 static void gtk_pizza_realize (GtkWidget *widget);
46 static void gtk_pizza_unrealize (GtkWidget *widget);
47
48 static void gtk_pizza_map (GtkWidget *widget);
49
50 static void gtk_pizza_size_request (GtkWidget *widget,
51 GtkRequisition *requisition);
52 static void gtk_pizza_size_allocate (GtkWidget *widget,
53 GtkAllocation *allocation);
54 static void gtk_pizza_draw (GtkWidget *widget,
55 GdkRectangle *area);
56 static gint gtk_pizza_expose (GtkWidget *widget,
57 GdkEventExpose *event);
58 static void gtk_pizza_add (GtkContainer *container,
59 GtkWidget *widget);
60 static void gtk_pizza_remove (GtkContainer *container,
61 GtkWidget *widget);
62 static void gtk_pizza_forall (GtkContainer *container,
63 gboolean include_internals,
64 GtkCallback callback,
65 gpointer callback_data);
66
67 static void gtk_pizza_position_child (GtkPizza *pizza,
68 GtkPizzaChild *child);
69 static void gtk_pizza_allocate_child (GtkPizza *pizza,
70 GtkPizzaChild *child);
71 static void gtk_pizza_position_children (GtkPizza *pizza);
72
73 static void gtk_pizza_adjust_allocations_recurse (GtkWidget *widget,
74 gpointer cb_data);
75 static void gtk_pizza_adjust_allocations (GtkPizza *pizza,
76 gint dx,
77 gint dy);
78
79
80 static void gtk_pizza_expose_area (GtkPizza *pizza,
81 gint x,
82 gint y,
83 gint width,
84 gint height);
85 /* unused */
86 #if 0
87 static void gtk_pizza_adjustment_changed (GtkAdjustment *adjustment,
88 GtkPizza *pizza);
89 #endif
90
91 static GdkFilterReturn gtk_pizza_filter (GdkXEvent *gdk_xevent,
92 GdkEvent *event,
93 gpointer data);
94 static GdkFilterReturn gtk_pizza_main_filter (GdkXEvent *gdk_xevent,
95 GdkEvent *event,
96 gpointer data);
97
98
99 static GtkType gtk_pizza_child_type (GtkContainer *container);
100
101 static void gtk_pizza_scroll_set_adjustments (GtkPizza *pizza,
102 GtkAdjustment *hadj,
103 GtkAdjustment *vadj);
104
105
106 static GtkContainerClass *parent_class = NULL;
107 static gboolean gravity_works;
108
109 guint
110 gtk_pizza_get_type ()
111 {
112 static guint pizza_type = 0;
113
114 if (!pizza_type)
115 {
116 GtkTypeInfo pizza_info =
117 {
118 "GtkPizza",
119 sizeof (GtkPizza),
120 sizeof (GtkPizzaClass),
121 (GtkClassInitFunc) gtk_pizza_class_init,
122 (GtkObjectInitFunc) gtk_pizza_init,
123 /* reserved_1 */ NULL,
124 /* reserved_2 */ NULL,
125 (GtkClassInitFunc) NULL,
126 };
127 pizza_type = gtk_type_unique (gtk_container_get_type (), &pizza_info);
128 }
129
130 return pizza_type;
131 }
132
133 static void
134 gtk_pizza_class_init (GtkPizzaClass *klass)
135 {
136 GtkObjectClass *object_class;
137 GtkWidgetClass *widget_class;
138 GtkContainerClass *container_class;
139
140 object_class = (GtkObjectClass*) klass;
141 widget_class = (GtkWidgetClass*) klass;
142 container_class = (GtkContainerClass*) klass;
143 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
144
145 widget_class->map = gtk_pizza_map;
146 widget_class->realize = gtk_pizza_realize;
147 widget_class->unrealize = gtk_pizza_unrealize;
148 widget_class->size_request = gtk_pizza_size_request;
149 widget_class->size_allocate = gtk_pizza_size_allocate;
150 widget_class->draw = gtk_pizza_draw;
151 widget_class->expose_event = gtk_pizza_expose;
152
153 container_class->add = gtk_pizza_add;
154 container_class->remove = gtk_pizza_remove;
155 container_class->forall = gtk_pizza_forall;
156
157 container_class->child_type = gtk_pizza_child_type;
158
159 klass->set_scroll_adjustments = gtk_pizza_scroll_set_adjustments;
160
161 widget_class->set_scroll_adjustments_signal =
162 gtk_signal_new ("set_scroll_adjustments",
163 GTK_RUN_LAST,
164
165 #ifdef __WXGTK20__
166 GTK_CLASS_TYPE(object_class),
167 #else
168 object_class->type,
169 #endif
170 GTK_SIGNAL_OFFSET (GtkPizzaClass, set_scroll_adjustments),
171 gtk_marshal_NONE__POINTER_POINTER,
172 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
173 }
174
175 static GtkType
176 gtk_pizza_child_type (GtkContainer *container)
177 {
178 return GTK_TYPE_WIDGET;
179 }
180
181 static void
182 gtk_pizza_init (GtkPizza *pizza)
183 {
184 GTK_WIDGET_UNSET_FLAGS (pizza, GTK_NO_WINDOW);
185
186 pizza->shadow_type = GTK_MYSHADOW_NONE;
187
188 pizza->children = NULL;
189
190 pizza->width = 20;
191 pizza->height = 20;
192
193 pizza->bin_window = NULL;
194
195 pizza->xoffset = 0;
196 pizza->yoffset = 0;
197
198 pizza->configure_serial = 0;
199 pizza->scroll_x = 0;
200 pizza->scroll_y = 0;
201 pizza->visibility = GDK_VISIBILITY_PARTIAL;
202
203 pizza->clear_on_draw = TRUE;
204 pizza->use_filter = TRUE;
205 pizza->external_expose = FALSE;
206 }
207
208 GtkWidget*
209 gtk_pizza_new ()
210 {
211 GtkPizza *pizza;
212
213 pizza = gtk_type_new (gtk_pizza_get_type ());
214
215 return GTK_WIDGET (pizza);
216 }
217
218 static void
219 gtk_pizza_scroll_set_adjustments (GtkPizza *pizza,
220 GtkAdjustment *hadj,
221 GtkAdjustment *vadj)
222 {
223 /* We handle scrolling in the wxScrolledWindow, not here. */
224 }
225
226 void
227 gtk_pizza_set_shadow_type (GtkPizza *pizza,
228 GtkMyShadowType type)
229 {
230 g_return_if_fail (pizza != NULL);
231 g_return_if_fail (GTK_IS_PIZZA (pizza));
232
233 if ((GtkMyShadowType) pizza->shadow_type != type)
234 {
235 pizza->shadow_type = type;
236
237 if (GTK_WIDGET_VISIBLE (pizza))
238 {
239 gtk_widget_size_allocate (GTK_WIDGET (pizza), &(GTK_WIDGET (pizza)->allocation));
240 gtk_widget_queue_draw (GTK_WIDGET (pizza));
241 }
242 }
243 }
244
245 void
246 gtk_pizza_set_clear (GtkPizza *pizza,
247 gboolean clear)
248 {
249 g_return_if_fail (pizza != NULL);
250 g_return_if_fail (GTK_IS_PIZZA (pizza));
251
252 pizza->clear_on_draw = clear;
253 }
254
255 void
256 gtk_pizza_set_filter (GtkPizza *pizza,
257 gboolean use)
258 {
259 g_return_if_fail (pizza != NULL);
260 g_return_if_fail (GTK_IS_PIZZA (pizza));
261
262 pizza->use_filter = use;
263 }
264
265 void
266 gtk_pizza_set_external (GtkPizza *pizza,
267 gboolean expose)
268 {
269 g_return_if_fail (pizza != NULL);
270 g_return_if_fail (GTK_IS_PIZZA (pizza));
271
272 pizza->external_expose = expose;
273 }
274
275 void
276 gtk_pizza_put (GtkPizza *pizza,
277 GtkWidget *widget,
278 gint x,
279 gint y,
280 gint width,
281 gint height)
282 {
283 GtkPizzaChild *child_info;
284
285 g_return_if_fail (pizza != NULL);
286 g_return_if_fail (GTK_IS_PIZZA (pizza));
287 g_return_if_fail (widget != NULL);
288
289 child_info = g_new (GtkPizzaChild, 1);
290
291 child_info->widget = widget;
292 child_info->x = x;
293 child_info->y = y;
294 child_info->width = width;
295 child_info->height = height;
296
297 pizza->children = g_list_append (pizza->children, child_info);
298
299 gtk_widget_set_parent (widget, GTK_WIDGET (pizza));
300
301 if (GTK_WIDGET_REALIZED (pizza))
302 gtk_widget_set_parent_window (widget, pizza->bin_window);
303
304 #ifndef __WXGTK20__ /* FIXME? */
305 if (!IS_ONSCREEN (x, y))
306 GTK_PRIVATE_SET_FLAG (widget, GTK_IS_OFFSCREEN);
307 #endif
308
309 /*
310 if (GTK_WIDGET_REALIZED (pizza))
311 gtk_widget_realize (widget);
312 */
313
314 gtk_widget_set_usize (widget, width, height);
315
316 /*
317 if (GTK_WIDGET_VISIBLE (pizza) && GTK_WIDGET_VISIBLE (widget))
318 {
319 if (GTK_WIDGET_MAPPED (pizza))
320 gtk_widget_map (widget);
321
322 gtk_widget_queue_resize (widget);
323 }
324 */
325 }
326
327 void
328 gtk_pizza_move (GtkPizza *pizza,
329 GtkWidget *widget,
330 gint x,
331 gint y)
332 {
333 GtkPizzaChild *child;
334 GList *children;
335
336 g_return_if_fail (pizza != NULL);
337 g_return_if_fail (GTK_IS_PIZZA (pizza));
338 g_return_if_fail (widget != NULL);
339
340 children = pizza->children;
341 while (children)
342 {
343 child = children->data;
344 children = children->next;
345
346 if (child->widget == widget)
347 {
348 if ((child->x == x) && (child->y == y))
349 break;
350
351 child->x = x;
352 child->y = y;
353
354 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (pizza))
355 gtk_widget_queue_resize (widget);
356 break;
357 }
358 }
359 }
360
361 void
362 gtk_pizza_resize (GtkPizza *pizza,
363 GtkWidget *widget,
364 gint width,
365 gint height)
366 {
367 GtkPizzaChild *child;
368 GList *children;
369
370 g_return_if_fail (pizza != NULL);
371 g_return_if_fail (GTK_IS_PIZZA (pizza));
372 g_return_if_fail (widget != NULL);
373
374 children = pizza->children;
375 while (children)
376 {
377 child = children->data;
378 children = children->next;
379
380 if (child->widget == widget)
381 {
382 if ((child->width == width) && (child->height == height))
383 break;
384
385 child->width = width;
386 child->height = height;
387
388 gtk_widget_set_usize (widget, width, height);
389
390 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (pizza))
391 gtk_widget_queue_resize (widget);
392 break;
393 }
394 }
395 }
396
397 void
398 gtk_pizza_set_size (GtkPizza *pizza,
399 GtkWidget *widget,
400 gint x,
401 gint y,
402 gint width,
403 gint height)
404 {
405 GtkPizzaChild *child;
406 GList *children;
407
408 g_return_if_fail (pizza != NULL);
409 g_return_if_fail (GTK_IS_PIZZA (pizza));
410 g_return_if_fail (widget != NULL);
411
412 children = pizza->children;
413 while (children)
414 {
415 child = children->data;
416 children = children->next;
417
418 if (child->widget == widget)
419 {
420 if ((child->x == x) &&
421 (child->y == y) &&
422 (child->width == width) &&
423 (child->height == height)) return;
424
425 child->x = x;
426 child->y = y;
427 child->width = width;
428 child->height = height;
429
430 gtk_widget_set_usize (widget, width, height);
431
432 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (pizza))
433 gtk_widget_queue_resize (widget);
434
435 return;
436 }
437 }
438 }
439
440 gint
441 gtk_pizza_child_resized (GtkPizza *pizza,
442 GtkWidget *widget)
443 {
444 GtkPizzaChild *child;
445 GList *children;
446
447 g_return_val_if_fail (pizza != NULL, FALSE);
448 g_return_val_if_fail (GTK_IS_PIZZA (pizza), FALSE);
449 g_return_val_if_fail (widget != NULL, FALSE);
450
451 children = pizza->children;
452 while (children)
453 {
454 child = children->data;
455 children = children->next;
456
457 if (child->widget == widget)
458 {
459 return ((child->width == widget->allocation.width) &&
460 (child->height == widget->allocation.height));
461 }
462 }
463
464 return FALSE;
465 }
466
467 static void
468 gtk_pizza_map (GtkWidget *widget)
469 {
470 GtkPizza *pizza;
471 GtkPizzaChild *child;
472 GList *children;
473
474 g_return_if_fail (widget != NULL);
475 g_return_if_fail (GTK_IS_PIZZA (widget));
476
477 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
478 pizza = GTK_PIZZA (widget);
479
480 children = pizza->children;
481 while (children)
482 {
483 child = children->data;
484 children = children->next;
485
486 if ( GTK_WIDGET_VISIBLE (child->widget) &&
487 !GTK_WIDGET_MAPPED (child->widget) &&
488 #ifdef __WXGTK20__
489 TRUE)
490 #else
491 !GTK_WIDGET_IS_OFFSCREEN (child->widget))
492 #endif
493 {
494 gtk_widget_map (child->widget);
495 }
496 }
497
498 gdk_window_show (widget->window);
499 gdk_window_show (pizza->bin_window);
500 }
501
502 static void
503 gtk_pizza_realize (GtkWidget *widget)
504 {
505 GtkPizza *pizza;
506 GdkWindowAttr attributes;
507 gint attributes_mask;
508 GtkPizzaChild *child;
509 GList *children;
510
511 g_return_if_fail (widget != NULL);
512 g_return_if_fail (GTK_IS_PIZZA (widget));
513
514 pizza = GTK_PIZZA (widget);
515
516 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
517
518 attributes.window_type = GDK_WINDOW_CHILD;
519
520 attributes.x = widget->allocation.x;
521 attributes.y = widget->allocation.y;
522 attributes.width = widget->allocation.width;
523 attributes.height = widget->allocation.height;
524
525 if (pizza->shadow_type == GTK_MYSHADOW_NONE)
526 {
527 /* no border, no changes to sizes */
528 } else
529 if (pizza->shadow_type == GTK_MYSHADOW_THIN)
530 {
531 /* GTK_MYSHADOW_THIN == wxSIMPLE_BORDER */
532 attributes.x += 1;
533 attributes.y += 1;
534 attributes.width -= 2;
535 attributes.height -= 2;
536 } else
537 {
538 /* GTK_MYSHADOW_IN == wxSUNKEN_BORDER */
539 /* GTK_MYSHADOW_OUT == wxRAISED_BORDER */
540 attributes.x += 2;
541 attributes.y += 2;
542 attributes.width -= 4;
543 attributes.height -= 4;
544 }
545
546 /* minimal size */
547 if (attributes.width < 2) attributes.width = 2;
548 if (attributes.height < 2) attributes.height = 2;
549
550 attributes.wclass = GDK_INPUT_OUTPUT;
551 attributes.visual = gtk_widget_get_visual (widget);
552 attributes.colormap = gtk_widget_get_colormap (widget);
553 attributes.event_mask =
554 GDK_VISIBILITY_NOTIFY_MASK;
555 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
556
557 widget->window = gdk_window_new(gtk_widget_get_parent_window (widget),
558 &attributes, attributes_mask);
559 gdk_window_set_user_data (widget->window, widget);
560
561 attributes.x = 0;
562 attributes.y = 0;
563
564 attributes.event_mask = gtk_widget_get_events (widget);
565 attributes.event_mask |=
566 GDK_EXPOSURE_MASK |
567 GDK_POINTER_MOTION_MASK |
568 GDK_POINTER_MOTION_HINT_MASK |
569 GDK_BUTTON_MOTION_MASK |
570 GDK_BUTTON1_MOTION_MASK |
571 GDK_BUTTON2_MOTION_MASK |
572 GDK_BUTTON3_MOTION_MASK |
573 GDK_BUTTON_PRESS_MASK |
574 GDK_BUTTON_RELEASE_MASK |
575 GDK_KEY_PRESS_MASK |
576 GDK_KEY_RELEASE_MASK |
577 GDK_ENTER_NOTIFY_MASK |
578 GDK_LEAVE_NOTIFY_MASK |
579 GDK_FOCUS_CHANGE_MASK;
580
581 pizza->bin_window = gdk_window_new(widget->window,
582 &attributes, attributes_mask);
583 gdk_window_set_user_data (pizza->bin_window, widget);
584
585 widget->style = gtk_style_attach (widget->style, widget->window);
586 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
587 gtk_style_set_background (widget->style, pizza->bin_window, GTK_STATE_NORMAL );
588
589 /* add filters for intercepting visibility and expose events */
590 gdk_window_add_filter (widget->window, gtk_pizza_main_filter, pizza);
591 gdk_window_add_filter (pizza->bin_window, gtk_pizza_filter, pizza);
592
593 /* we NEED gravity or we'll give up */
594 gravity_works = gdk_window_set_static_gravities (pizza->bin_window, TRUE);
595
596 /* cannot be done before realisation */
597 children = pizza->children;
598 while (children)
599 {
600 child = children->data;
601 children = children->next;
602
603 gtk_widget_set_parent_window (child->widget, pizza->bin_window);
604 }
605 }
606
607 static void
608 gtk_pizza_unrealize (GtkWidget *widget)
609 {
610 GtkPizza *pizza;
611
612 g_return_if_fail (widget != NULL);
613 g_return_if_fail (GTK_IS_PIZZA (widget));
614
615 pizza = GTK_PIZZA (widget);
616
617 gdk_window_set_user_data (pizza->bin_window, NULL);
618 gdk_window_destroy (pizza->bin_window);
619 pizza->bin_window = NULL;
620
621 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
622 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
623 }
624
625 static void
626 gtk_pizza_size_request (GtkWidget *widget,
627 GtkRequisition *requisition)
628 {
629 GtkPizza *pizza;
630 GtkPizzaChild *child;
631 GList *children;
632 GtkRequisition child_requisition;
633
634 g_return_if_fail (widget != NULL);
635 g_return_if_fail (GTK_IS_PIZZA (widget));
636 g_return_if_fail (requisition != NULL);
637
638 pizza = GTK_PIZZA (widget);
639
640 children = pizza->children;
641 while (children)
642 {
643 child = children->data;
644 children = children->next;
645
646 if (GTK_WIDGET_VISIBLE (child->widget))
647 {
648 gtk_widget_size_request (child->widget, &child_requisition);
649 }
650 }
651
652 /* request very little, I'm not sure if requesting nothing
653 will always have positive effects on stability... */
654 requisition->width = 2;
655 requisition->height = 2;
656 }
657
658 static void
659 gtk_pizza_size_allocate (GtkWidget *widget,
660 GtkAllocation *allocation)
661 {
662 GtkPizza *pizza;
663 gint border;
664 gint x,y,w,h;
665 GtkPizzaChild *child;
666 GList *children;
667
668 g_return_if_fail (widget != NULL);
669 g_return_if_fail (GTK_IS_PIZZA(widget));
670 g_return_if_fail (allocation != NULL);
671
672 pizza = GTK_PIZZA (widget);
673
674 widget->allocation = *allocation;
675
676 if (pizza->shadow_type == GTK_MYSHADOW_NONE)
677 border = 0;
678 else
679 if (pizza->shadow_type == GTK_MYSHADOW_THIN)
680 border = 1;
681 else
682 border = 2;
683
684 x = allocation->x + border;
685 y = allocation->y + border;
686 w = allocation->width - border*2;
687 h = allocation->height - border*2;
688
689 if (GTK_WIDGET_REALIZED (widget))
690 {
691 gdk_window_move_resize( widget->window, x, y, w, h );
692 gdk_window_move_resize( pizza->bin_window, 0, 0, w, h );
693 }
694
695 children = pizza->children;
696 while (children)
697 {
698 child = children->data;
699 children = children->next;
700
701 gtk_pizza_position_child (pizza, child);
702 gtk_pizza_allocate_child (pizza, child);
703 }
704 }
705
706 static void
707 gtk_pizza_draw (GtkWidget *widget,
708 GdkRectangle *area)
709 {
710 GtkPizza *pizza;
711 GtkPizzaChild *child;
712 GdkRectangle child_area;
713 GList *children;
714
715 g_return_if_fail (widget != NULL);
716 g_return_if_fail (GTK_IS_PIZZA (widget));
717
718 pizza = GTK_PIZZA (widget);
719
720 /* Sometimes, We handle all expose events in window.cpp now. */
721 if (pizza->external_expose)
722 return;
723
724 children = pizza->children;
725 if ( !(GTK_WIDGET_APP_PAINTABLE (widget)) &&
726 (pizza->clear_on_draw))
727 {
728 gdk_window_clear_area( pizza->bin_window,
729 area->x, area->y, area->width, area->height);
730 }
731
732 while (children)
733 {
734 child = children->data;
735 children = children->next;
736
737 if (gtk_widget_intersect (child->widget, area, &child_area))
738 gtk_widget_draw (child->widget, &child_area);
739 }
740 }
741
742 static gint
743 gtk_pizza_expose (GtkWidget *widget,
744 GdkEventExpose *event)
745 {
746 GtkPizza *pizza;
747 GtkPizzaChild *child;
748 GdkEventExpose child_event;
749 GList *children;
750
751 g_return_val_if_fail (widget != NULL, FALSE);
752 g_return_val_if_fail (GTK_IS_PIZZA (widget), FALSE);
753 g_return_val_if_fail (event != NULL, FALSE);
754
755 pizza = GTK_PIZZA (widget);
756
757 /* Sometimes, We handle all expose events in window.cpp now. */
758 if (pizza->external_expose)
759 return FALSE;
760
761 if (event->window != pizza->bin_window)
762 return FALSE;
763
764 children = pizza->children;
765 while (children)
766 {
767 child = children->data;
768 children = children->next;
769
770 child_event = *event;
771
772 if (GTK_WIDGET_NO_WINDOW (child->widget) &&
773 GTK_WIDGET_DRAWABLE (child->widget) &&
774 gtk_widget_intersect (child->widget, &event->area, &child_event.area))
775 {
776 gtk_widget_event (child->widget, (GdkEvent*) &child_event);
777 }
778 }
779
780 return TRUE;
781 }
782
783 static void
784 gtk_pizza_add (GtkContainer *container,
785 GtkWidget *widget)
786 {
787 g_return_if_fail (container != NULL);
788 g_return_if_fail (GTK_IS_PIZZA (container));
789 g_return_if_fail (widget != NULL);
790
791 gtk_pizza_put (GTK_PIZZA (container), widget, 0, 0, 20, 20 );
792 }
793
794 static void
795 gtk_pizza_remove (GtkContainer *container,
796 GtkWidget *widget)
797 {
798 GtkPizza *pizza;
799 GtkPizzaChild *child;
800 GList *children;
801
802 g_return_if_fail (container != NULL);
803 g_return_if_fail (GTK_IS_PIZZA (container));
804 g_return_if_fail (widget != NULL);
805
806 pizza = GTK_PIZZA (container);
807
808 children = pizza->children;
809 while (children)
810 {
811 child = children->data;
812
813 if (child->widget == widget)
814 {
815 gtk_widget_unparent (widget);
816
817 /* security checks */
818 g_return_if_fail (GTK_IS_WIDGET (widget));
819
820 pizza->children = g_list_remove_link (pizza->children, children);
821 g_list_free (children);
822 g_free (child);
823
824 /* security checks */
825 g_return_if_fail (GTK_IS_WIDGET (widget));
826
827 #ifndef __WXGTK20__
828 GTK_PRIVATE_UNSET_FLAG (widget, GTK_IS_OFFSCREEN);
829 #endif
830
831 break;
832 }
833
834 children = children->next;
835 }
836 }
837
838 static void
839 gtk_pizza_forall (GtkContainer *container,
840 gboolean include_internals,
841 GtkCallback callback,
842 gpointer callback_data)
843 {
844 GtkPizza *pizza;
845 GtkPizzaChild *child;
846 GList *children;
847
848 g_return_if_fail (container != NULL);
849 g_return_if_fail (GTK_IS_PIZZA (container));
850 g_return_if_fail (callback != NULL);
851
852 pizza = GTK_PIZZA (container);
853
854 children = pizza->children;
855 while (children)
856 {
857 child = children->data;
858 children = children->next;
859
860 (* callback) (child->widget, callback_data);
861 }
862 }
863
864
865 /* Operations on children
866 */
867
868 static void
869 gtk_pizza_position_child (GtkPizza *pizza,
870 GtkPizzaChild *child)
871 {
872 gint x;
873 gint y;
874
875 x = child->x - pizza->xoffset;
876 y = child->y - pizza->yoffset;
877
878 if (IS_ONSCREEN (x,y))
879 {
880 if (GTK_WIDGET_MAPPED (pizza) &&
881 GTK_WIDGET_VISIBLE (child->widget))
882 {
883 if (!GTK_WIDGET_MAPPED (child->widget))
884 gtk_widget_map (child->widget);
885 }
886
887 #ifndef __WXGTK20__
888 if (GTK_WIDGET_IS_OFFSCREEN (child->widget))
889 GTK_PRIVATE_UNSET_FLAG (child->widget, GTK_IS_OFFSCREEN);
890 #endif
891 }
892 else
893 {
894 #ifndef __WXGTK20__
895 if (!GTK_WIDGET_IS_OFFSCREEN (child->widget))
896 GTK_PRIVATE_SET_FLAG (child->widget, GTK_IS_OFFSCREEN);
897 #endif
898
899 if (GTK_WIDGET_MAPPED (child->widget))
900 gtk_widget_unmap (child->widget);
901 }
902 }
903
904 static void
905 gtk_pizza_allocate_child (GtkPizza *pizza,
906 GtkPizzaChild *child)
907 {
908 GtkAllocation allocation;
909 GtkRequisition requisition;
910
911 allocation.x = child->x - pizza->xoffset;
912 allocation.y = child->y - pizza->yoffset;
913 gtk_widget_get_child_requisition (child->widget, &requisition);
914 allocation.width = requisition.width;
915 allocation.height = requisition.height;
916
917 gtk_widget_size_allocate (child->widget, &allocation);
918 }
919
920 static void
921 gtk_pizza_position_children (GtkPizza *pizza)
922 {
923 GList *tmp_list;
924
925 tmp_list = pizza->children;
926 while (tmp_list)
927 {
928 GtkPizzaChild *child = tmp_list->data;
929 tmp_list = tmp_list->next;
930
931 gtk_pizza_position_child (pizza, child);
932 }
933 }
934
935 static void
936 gtk_pizza_adjust_allocations_recurse (GtkWidget *widget,
937 gpointer cb_data)
938 {
939 GtkPizzaAdjData *data = cb_data;
940
941 widget->allocation.x += data->dx;
942 widget->allocation.y += data->dy;
943
944 if (GTK_WIDGET_NO_WINDOW (widget) && GTK_IS_CONTAINER (widget))
945 {
946 gtk_container_forall (GTK_CONTAINER (widget),
947 gtk_pizza_adjust_allocations_recurse,
948 cb_data);
949 }
950 }
951
952 static void
953 gtk_pizza_adjust_allocations (GtkPizza *pizza,
954 gint dx,
955 gint dy)
956 {
957 GList *tmp_list;
958 GtkPizzaAdjData data;
959
960 data.dx = dx;
961 data.dy = dy;
962
963 tmp_list = pizza->children;
964 while (tmp_list)
965 {
966 GtkPizzaChild *child = tmp_list->data;
967 tmp_list = tmp_list->next;
968
969 child->widget->allocation.x += dx;
970 child->widget->allocation.y += dy;
971
972 if (GTK_WIDGET_NO_WINDOW (child->widget) &&
973 GTK_IS_CONTAINER (child->widget))
974 {
975 gtk_container_forall (GTK_CONTAINER (child->widget),
976 gtk_pizza_adjust_allocations_recurse,
977 &data);
978 }
979 }
980 }
981
982 /* Callbacks */
983
984 /* Send a synthetic expose event to the widget
985 */
986 static void
987 gtk_pizza_expose_area (GtkPizza *pizza,
988 gint x, gint y, gint width, gint height)
989 {
990 if (pizza->visibility == GDK_VISIBILITY_UNOBSCURED)
991 {
992 GdkEventExpose event;
993
994 event.type = GDK_EXPOSE;
995 event.send_event = TRUE;
996 event.window = pizza->bin_window;
997 event.count = 0;
998
999 event.area.x = x;
1000 event.area.y = y;
1001 event.area.width = width;
1002 event.area.height = height;
1003
1004 gdk_window_ref (event.window);
1005 gtk_widget_event (GTK_WIDGET (pizza), (GdkEvent *)&event);
1006 gdk_window_unref (event.window);
1007 }
1008 }
1009
1010 /* This function is used to find events to process while scrolling
1011 */
1012
1013 static Bool
1014 gtk_pizza_expose_predicate (Display *display,
1015 XEvent *xevent,
1016 XPointer arg)
1017 {
1018 if ((xevent->type == Expose) ||
1019 ((xevent->xany.window == *(Window *)arg) &&
1020 (xevent->type == ConfigureNotify)))
1021 return True;
1022 else
1023 return False;
1024 }
1025
1026 /* This is the main routine to do the scrolling. Scrolling is
1027 * done by "Guffaw" scrolling, as in the Mozilla XFE, with
1028 * a few modifications.
1029 *
1030 * The main improvement is that we keep track of whether we
1031 * are obscured or not. If not, we ignore the generated expose
1032 * events and instead do the exposes ourself, without having
1033 * to wait for a roundtrip to the server. This also provides
1034 * a limited form of expose-event compression, since we do
1035 * the affected area as one big chunk.
1036 */
1037
1038 void
1039 gtk_pizza_scroll (GtkPizza *pizza, gint dx, gint dy)
1040 {
1041 GtkWidget *widget;
1042 XEvent xevent;
1043
1044 gint x,y,w,h,border;
1045
1046 widget = GTK_WIDGET (pizza);
1047
1048 pizza->xoffset += dx;
1049 pizza->yoffset += dy;
1050
1051 if (!GTK_WIDGET_MAPPED (pizza))
1052 {
1053 gtk_pizza_position_children (pizza);
1054 return;
1055 }
1056
1057 gtk_pizza_adjust_allocations (pizza, -dx, -dy);
1058
1059 if (pizza->shadow_type == GTK_MYSHADOW_NONE)
1060 border = 0;
1061 else
1062 if (pizza->shadow_type == GTK_MYSHADOW_THIN)
1063 border = 1;
1064 else
1065 border = 2;
1066
1067 x = 0;
1068 y = 0;
1069 w = widget->allocation.width - 2*border;
1070 h = widget->allocation.height - 2*border;
1071
1072 if (dx > 0)
1073 {
1074 if (gravity_works)
1075 {
1076 gdk_window_resize (pizza->bin_window,
1077 w + dx,
1078 h);
1079 gdk_window_move (pizza->bin_window, x-dx, y);
1080 gdk_window_move_resize (pizza->bin_window, x, y, w, h );
1081 }
1082 else
1083 {
1084 /* FIXME */
1085 }
1086 }
1087 else if (dx < 0)
1088 {
1089 if (gravity_works)
1090 {
1091 gdk_window_move_resize (pizza->bin_window,
1092 x + dx,
1093 y,
1094 w - dx,
1095 h);
1096 gdk_window_move (pizza->bin_window, x, y);
1097 gdk_window_resize (pizza->bin_window, w, h );
1098 }
1099 else
1100 {
1101 /* FIXME */
1102 }
1103 }
1104
1105 if (dy > 0)
1106 {
1107 if (gravity_works)
1108 {
1109 gdk_window_resize (pizza->bin_window, w, h + dy);
1110 gdk_window_move (pizza->bin_window, x, y-dy);
1111 gdk_window_move_resize (pizza->bin_window,
1112 x, y, w, h );
1113 }
1114 else
1115 {
1116 /* FIXME */
1117 }
1118 }
1119 else if (dy < 0)
1120 {
1121 if (gravity_works)
1122 {
1123 gdk_window_move_resize (pizza->bin_window,
1124 x, y+dy, w, h - dy );
1125 gdk_window_move (pizza->bin_window, x, y);
1126 gdk_window_resize (pizza->bin_window, w, h );
1127 }
1128 else
1129 {
1130 /* FIXME */
1131 }
1132 }
1133
1134 gtk_pizza_position_children (pizza);
1135
1136 gdk_flush();
1137 while (XCheckIfEvent(GDK_WINDOW_XDISPLAY (pizza->bin_window),
1138 &xevent,
1139 gtk_pizza_expose_predicate,
1140 (XPointer)&GDK_WINDOW_XWINDOW (pizza->bin_window)))
1141 {
1142 GdkEvent event;
1143 GtkWidget *event_widget;
1144
1145 if ((xevent.xany.window == GDK_WINDOW_XWINDOW (pizza->bin_window)) )
1146 gtk_pizza_filter (&xevent, &event, pizza);
1147
1148 if (xevent.type == Expose)
1149 {
1150 event.expose.window = gdk_window_lookup (xevent.xany.window);
1151 gdk_window_get_user_data (event.expose.window,
1152 (gpointer *)&event_widget);
1153
1154 if (event_widget)
1155 {
1156 event.expose.type = GDK_EXPOSE;
1157 event.expose.area.x = xevent.xexpose.x;
1158 event.expose.area.y = xevent.xexpose.y;
1159 event.expose.area.width = xevent.xexpose.width;
1160 event.expose.area.height = xevent.xexpose.height;
1161 event.expose.count = xevent.xexpose.count;
1162
1163 gdk_window_ref (event.expose.window);
1164 gtk_widget_event (event_widget, &event);
1165 gdk_window_unref (event.expose.window);
1166 }
1167 }
1168 }
1169 }
1170
1171 /* The main event filter. Actually, we probably don't really need
1172 * to install this as a filter at all, since we are calling it
1173 * directly above in the expose-handling hack. But in case scrollbars
1174 * are fixed up in some manner...
1175 *
1176 * This routine identifies expose events that are generated when
1177 * we've temporarily moved the bin_window_origin, and translates
1178 * them or discards them, depending on whether we are obscured
1179 * or not.
1180 */
1181 static GdkFilterReturn
1182 gtk_pizza_filter (GdkXEvent *gdk_xevent,
1183 GdkEvent *event,
1184 gpointer data)
1185 {
1186 XEvent *xevent;
1187 GtkPizza *pizza;
1188
1189 xevent = (XEvent *)gdk_xevent;
1190
1191 pizza = GTK_PIZZA (data);
1192
1193 if (!pizza->use_filter)
1194 return GDK_FILTER_CONTINUE;
1195
1196 switch (xevent->type)
1197 {
1198 case Expose:
1199 if (xevent->xexpose.serial == pizza->configure_serial)
1200 {
1201 xevent->xexpose.x += pizza->scroll_x;
1202 xevent->xexpose.y += pizza->scroll_y;
1203 }
1204 break;
1205
1206 case ConfigureNotify:
1207 {
1208 pizza->configure_serial = xevent->xconfigure.serial;
1209 pizza->scroll_x = xevent->xconfigure.x;
1210 pizza->scroll_y = xevent->xconfigure.y;
1211 }
1212 break;
1213 }
1214
1215 return GDK_FILTER_CONTINUE;
1216 }
1217
1218 /* Although GDK does have a GDK_VISIBILITY_NOTIFY event,
1219 * there is no corresponding event in GTK, so we have
1220 * to get the events from a filter
1221 */
1222 static GdkFilterReturn
1223 gtk_pizza_main_filter (GdkXEvent *gdk_xevent,
1224 GdkEvent *event,
1225 gpointer data)
1226 {
1227 XEvent *xevent;
1228 GtkPizza *pizza;
1229
1230 xevent = (XEvent *)gdk_xevent;
1231 pizza = GTK_PIZZA (data);
1232
1233 if (!pizza->use_filter)
1234 return GDK_FILTER_CONTINUE;
1235
1236 if (xevent->type == VisibilityNotify)
1237 {
1238 switch (xevent->xvisibility.state)
1239 {
1240 case VisibilityFullyObscured:
1241 pizza->visibility = GDK_VISIBILITY_FULLY_OBSCURED;
1242 break;
1243
1244 case VisibilityPartiallyObscured:
1245 pizza->visibility = GDK_VISIBILITY_PARTIAL;
1246 break;
1247
1248 case VisibilityUnobscured:
1249 pizza->visibility = GDK_VISIBILITY_UNOBSCURED;
1250 break;
1251 }
1252
1253 return GDK_FILTER_REMOVE;
1254 }
1255
1256 return GDK_FILTER_CONTINUE;
1257 }
1258
1259
1260 #ifdef __cplusplus
1261 }
1262 #endif /* __cplusplus */
1263