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