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