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