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