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