]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/win_gtk.c
Added chapter on collection and container classes to contents
[wxWidgets.git] / src / gtk / 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 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
24extern "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
34typedef struct _GtkPizzaAdjData GtkPizzaAdjData;
35
36struct _GtkPizzaAdjData
37{
38 gint dx;
39 gint dy;
40};
41
42static void gtk_pizza_class_init (GtkPizzaClass *klass);
43static void gtk_pizza_init (GtkPizza *pizza);
44
45static void gtk_pizza_realize (GtkWidget *widget);
46static void gtk_pizza_unrealize (GtkWidget *widget);
47
48static void gtk_pizza_map (GtkWidget *widget);
49
50static void gtk_pizza_size_request (GtkWidget *widget,
51 GtkRequisition *requisition);
52static void gtk_pizza_size_allocate (GtkWidget *widget,
53 GtkAllocation *allocation);
54static void gtk_pizza_draw (GtkWidget *widget,
55 GdkRectangle *area);
56static gint gtk_pizza_expose (GtkWidget *widget,
57 GdkEventExpose *event);
58static void gtk_pizza_add (GtkContainer *container,
59 GtkWidget *widget);
60static void gtk_pizza_remove (GtkContainer *container,
61 GtkWidget *widget);
62static void gtk_pizza_forall (GtkContainer *container,
63 gboolean include_internals,
64 GtkCallback callback,
65 gpointer callback_data);
66
67static void gtk_pizza_position_child (GtkPizza *pizza,
68 GtkPizzaChild *child);
69static void gtk_pizza_allocate_child (GtkPizza *pizza,
70 GtkPizzaChild *child);
71static void gtk_pizza_position_children (GtkPizza *pizza);
72
73static void gtk_pizza_adjust_allocations_recurse (GtkWidget *widget,
74 gpointer cb_data);
75static void gtk_pizza_adjust_allocations (GtkPizza *pizza,
76 gint dx,
77 gint dy);
78
79
80/* unused */
81#if 0
82static void gtk_pizza_expose_area (GtkPizza *pizza,
83 gint x,
84 gint y,
85 gint width,
86 gint height);
87static void gtk_pizza_adjustment_changed (GtkAdjustment *adjustment,
88 GtkPizza *pizza);
89#endif
90
91static GdkFilterReturn gtk_pizza_filter (GdkXEvent *gdk_xevent,
92 GdkEvent *event,
93 gpointer data);
94static GdkFilterReturn gtk_pizza_main_filter (GdkXEvent *gdk_xevent,
95 GdkEvent *event,
96 gpointer data);
97
98
99static GtkType gtk_pizza_child_type (GtkContainer *container);
100
101static void gtk_pizza_scroll_set_adjustments (GtkPizza *pizza,
102 GtkAdjustment *hadj,
103 GtkAdjustment *vadj);
104
105
106static GtkContainerClass *parent_class = NULL;
107static gboolean gravity_works;
108
109guint
110gtk_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
133static void
134gtk_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
175static GtkType
176gtk_pizza_child_type (GtkContainer *container)
177{
178 return GTK_TYPE_WIDGET;
179}
180
181static void
182gtk_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
208GtkWidget*
209gtk_pizza_new ()
210{
211 GtkPizza *pizza;
212
213 pizza = gtk_type_new (gtk_pizza_get_type ());
214
215 return GTK_WIDGET (pizza);
216}
217
218static void
219gtk_pizza_scroll_set_adjustments (GtkPizza *pizza,
220 GtkAdjustment *hadj,
221 GtkAdjustment *vadj)
222{
223 /* We handle scrolling in the wxScrolledWindow, not here. */
224}
225
226void
227gtk_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
245void
246gtk_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
255void
256gtk_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
265void
266gtk_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
275void
276gtk_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
327void
328gtk_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
361void
362gtk_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
397void
398gtk_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
440gint
441gtk_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
467static void
468gtk_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
502static void
503gtk_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
607static void
608gtk_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
625static void
626gtk_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
658static void
659gtk_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
706static void
707gtk_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
742static gint
743gtk_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
783static void
784gtk_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
794static void
795gtk_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
838static void
839gtk_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
868static void
869gtk_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
904static void
905gtk_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
920static void
921gtk_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
935static void
936gtk_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
952static void
953gtk_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/* unused */
985#if 0
986/* Send a synthetic expose event to the widget
987 */
988static void
989gtk_pizza_expose_area (GtkPizza *pizza,
990 gint x, gint y, gint width, gint height)
991{
992 if (pizza->visibility == GDK_VISIBILITY_UNOBSCURED)
993 {
994 GdkEventExpose event;
995
996 event.type = GDK_EXPOSE;
997 event.send_event = TRUE;
998 event.window = pizza->bin_window;
999 event.count = 0;
1000
1001 event.area.x = x;
1002 event.area.y = y;
1003 event.area.width = width;
1004 event.area.height = height;
1005
1006 gdk_window_ref (event.window);
1007 gtk_widget_event (GTK_WIDGET (pizza), (GdkEvent *)&event);
1008 gdk_window_unref (event.window);
1009 }
1010}
1011#endif /* unused */
1012
1013/* This function is used to find events to process while scrolling
1014 */
1015
1016static Bool
1017gtk_pizza_expose_predicate (Display *display,
1018 XEvent *xevent,
1019 XPointer arg)
1020{
1021 if ((xevent->type == Expose) ||
1022 ((xevent->xany.window == *(Window *)arg) &&
1023 (xevent->type == ConfigureNotify)))
1024 return True;
1025 else
1026 return False;
1027}
1028
1029/* This is the main routine to do the scrolling. Scrolling is
1030 * done by "Guffaw" scrolling, as in the Mozilla XFE, with
1031 * a few modifications.
1032 *
1033 * The main improvement is that we keep track of whether we
1034 * are obscured or not. If not, we ignore the generated expose
1035 * events and instead do the exposes ourself, without having
1036 * to wait for a roundtrip to the server. This also provides
1037 * a limited form of expose-event compression, since we do
1038 * the affected area as one big chunk.
1039 */
1040
1041void
1042gtk_pizza_scroll (GtkPizza *pizza, gint dx, gint dy)
1043{
1044 GtkWidget *widget;
1045 XEvent xevent;
1046
1047 gint x,y,w,h,border;
1048
1049 widget = GTK_WIDGET (pizza);
1050
1051 pizza->xoffset += dx;
1052 pizza->yoffset += dy;
1053
1054 if (!GTK_WIDGET_MAPPED (pizza))
1055 {
1056 gtk_pizza_position_children (pizza);
1057 return;
1058 }
1059
1060 gtk_pizza_adjust_allocations (pizza, -dx, -dy);
1061
1062 if (pizza->shadow_type == GTK_MYSHADOW_NONE)
1063 border = 0;
1064 else
1065 if (pizza->shadow_type == GTK_MYSHADOW_THIN)
1066 border = 1;
1067 else
1068 border = 2;
1069
1070 x = 0;
1071 y = 0;
1072 w = widget->allocation.width - 2*border;
1073 h = widget->allocation.height - 2*border;
1074
1075 if (dx > 0)
1076 {
1077 if (gravity_works)
1078 {
1079 gdk_window_resize (pizza->bin_window,
1080 w + dx,
1081 h);
1082 gdk_window_move (pizza->bin_window, x-dx, y);
1083 gdk_window_move_resize (pizza->bin_window, x, y, w, h );
1084 }
1085 else
1086 {
1087 /* FIXME */
1088 }
1089 }
1090 else if (dx < 0)
1091 {
1092 if (gravity_works)
1093 {
1094 gdk_window_move_resize (pizza->bin_window,
1095 x + dx,
1096 y,
1097 w - dx,
1098 h);
1099 gdk_window_move (pizza->bin_window, x, y);
1100 gdk_window_resize (pizza->bin_window, w, h );
1101 }
1102 else
1103 {
1104 /* FIXME */
1105 }
1106 }
1107
1108 if (dy > 0)
1109 {
1110 if (gravity_works)
1111 {
1112 gdk_window_resize (pizza->bin_window, w, h + dy);
1113 gdk_window_move (pizza->bin_window, x, y-dy);
1114 gdk_window_move_resize (pizza->bin_window,
1115 x, y, w, h );
1116 }
1117 else
1118 {
1119 /* FIXME */
1120 }
1121 }
1122 else if (dy < 0)
1123 {
1124 if (gravity_works)
1125 {
1126 gdk_window_move_resize (pizza->bin_window,
1127 x, y+dy, w, h - dy );
1128 gdk_window_move (pizza->bin_window, x, y);
1129 gdk_window_resize (pizza->bin_window, w, h );
1130 }
1131 else
1132 {
1133 /* FIXME */
1134 }
1135 }
1136
1137 gtk_pizza_position_children (pizza);
1138
1139 gdk_flush();
1140 while (XCheckIfEvent(GDK_WINDOW_XDISPLAY (pizza->bin_window),
1141 &xevent,
1142 gtk_pizza_expose_predicate,
1143 (XPointer)&GDK_WINDOW_XWINDOW (pizza->bin_window)))
1144 {
1145 GdkEvent event;
1146 GtkWidget *event_widget;
1147
1148 if ((xevent.xany.window == GDK_WINDOW_XWINDOW (pizza->bin_window)) )
1149 gtk_pizza_filter (&xevent, &event, pizza);
1150
1151 if (xevent.type == Expose)
1152 {
1153 event.expose.window = gdk_window_lookup (xevent.xany.window);
1154 gdk_window_get_user_data (event.expose.window,
1155 (gpointer *)&event_widget);
1156
1157 if (event_widget)
1158 {
1159 event.expose.type = GDK_EXPOSE;
1160 event.expose.area.x = xevent.xexpose.x;
1161 event.expose.area.y = xevent.xexpose.y;
1162 event.expose.area.width = xevent.xexpose.width;
1163 event.expose.area.height = xevent.xexpose.height;
1164 event.expose.count = xevent.xexpose.count;
1165
1166 gdk_window_ref (event.expose.window);
1167 gtk_widget_event (event_widget, &event);
1168 gdk_window_unref (event.expose.window);
1169 }
1170 }
1171 }
1172}
1173
1174/* The main event filter. Actually, we probably don't really need
1175 * to install this as a filter at all, since we are calling it
1176 * directly above in the expose-handling hack. But in case scrollbars
1177 * are fixed up in some manner...
1178 *
1179 * This routine identifies expose events that are generated when
1180 * we've temporarily moved the bin_window_origin, and translates
1181 * them or discards them, depending on whether we are obscured
1182 * or not.
1183 */
1184static GdkFilterReturn
1185gtk_pizza_filter (GdkXEvent *gdk_xevent,
1186 GdkEvent *event,
1187 gpointer data)
1188{
1189 XEvent *xevent;
1190 GtkPizza *pizza;
1191
1192 xevent = (XEvent *)gdk_xevent;
1193
1194 pizza = GTK_PIZZA (data);
1195
1196 if (!pizza->use_filter)
1197 return GDK_FILTER_CONTINUE;
1198
1199 switch (xevent->type)
1200 {
1201 case Expose:
1202 if (xevent->xexpose.serial == pizza->configure_serial)
1203 {
1204 xevent->xexpose.x += pizza->scroll_x;
1205 xevent->xexpose.y += pizza->scroll_y;
1206 }
1207 break;
1208
1209 case ConfigureNotify:
1210 {
1211 pizza->configure_serial = xevent->xconfigure.serial;
1212 pizza->scroll_x = xevent->xconfigure.x;
1213 pizza->scroll_y = xevent->xconfigure.y;
1214 }
1215 break;
1216 }
1217
1218 return GDK_FILTER_CONTINUE;
1219}
1220
1221/* Although GDK does have a GDK_VISIBILITY_NOTIFY event,
1222 * there is no corresponding event in GTK, so we have
1223 * to get the events from a filter
1224 */
1225static GdkFilterReturn
1226gtk_pizza_main_filter (GdkXEvent *gdk_xevent,
1227 GdkEvent *event,
1228 gpointer data)
1229{
1230 XEvent *xevent;
1231 GtkPizza *pizza;
1232
1233 xevent = (XEvent *)gdk_xevent;
1234 pizza = GTK_PIZZA (data);
1235
1236 if (!pizza->use_filter)
1237 return GDK_FILTER_CONTINUE;
1238
1239 if (xevent->type == VisibilityNotify)
1240 {
1241 switch (xevent->xvisibility.state)
1242 {
1243 case VisibilityFullyObscured:
1244 pizza->visibility = GDK_VISIBILITY_FULLY_OBSCURED;
1245 break;
1246
1247 case VisibilityPartiallyObscured:
1248 pizza->visibility = GDK_VISIBILITY_PARTIAL;
1249 break;
1250
1251 case VisibilityUnobscured:
1252 pizza->visibility = GDK_VISIBILITY_UNOBSCURED;
1253 break;
1254 }
1255
1256 return GDK_FILTER_REMOVE;
1257 }
1258
1259 return GDK_FILTER_CONTINUE;
1260}
1261
1262
1263#ifdef __cplusplus
1264}
1265#endif /* __cplusplus */
1266