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