]> Pileus Git - ~andy/gtk/blob - gtk/gtklayout.c
Add hidden aliases for exported symbols which are used internally in order
[~andy/gtk] / gtk / gtklayout.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * GtkLayout: Widget for scrolling of arbitrary-sized areas.
20  *
21  * Copyright Owen Taylor, 1998
22  */
23
24 /*
25  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
26  * file for a list of people on the GTK+ Team.  See the ChangeLog
27  * files for a list of changes.  These files are distributed with
28  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
29  */
30
31 #include <config.h>
32 #include "gdkconfig.h"
33
34 #include "gtkalias.h"
35 #include "gtklayout.h"
36 #include "gtkprivate.h"
37 #include "gtkintl.h"
38 #include "gtkmarshalers.h"
39
40 typedef struct _GtkLayoutChild   GtkLayoutChild;
41
42 struct _GtkLayoutChild {
43   GtkWidget *widget;
44   gint x;
45   gint y;
46 };
47
48 enum {
49    PROP_0,
50    PROP_HADJUSTMENT,
51    PROP_VADJUSTMENT,
52    PROP_WIDTH,
53    PROP_HEIGHT
54 };
55
56 enum {
57   CHILD_PROP_0,
58   CHILD_PROP_X,
59   CHILD_PROP_Y
60 };
61
62 static void gtk_layout_class_init         (GtkLayoutClass *class);
63 static void gtk_layout_get_property       (GObject        *object,
64                                            guint           prop_id,
65                                            GValue         *value,
66                                            GParamSpec     *pspec);
67 static void gtk_layout_set_property       (GObject        *object,
68                                            guint           prop_id,
69                                            const GValue   *value,
70                                            GParamSpec     *pspec);
71 static GObject *gtk_layout_constructor    (GType                  type,
72                                            guint                  n_properties,
73                                            GObjectConstructParam *properties);
74 static void gtk_layout_init               (GtkLayout      *layout);
75 static void gtk_layout_finalize           (GObject        *object);
76 static void gtk_layout_realize            (GtkWidget      *widget);
77 static void gtk_layout_unrealize          (GtkWidget      *widget);
78 static void gtk_layout_map                (GtkWidget      *widget);
79 static void gtk_layout_size_request       (GtkWidget      *widget,
80                                            GtkRequisition *requisition);
81 static void gtk_layout_size_allocate      (GtkWidget      *widget,
82                                            GtkAllocation  *allocation);
83 static gint gtk_layout_expose             (GtkWidget      *widget,
84                                            GdkEventExpose *event);
85 static void gtk_layout_add                (GtkContainer   *container,
86                                            GtkWidget      *widget);
87 static void gtk_layout_remove             (GtkContainer   *container,
88                                            GtkWidget      *widget);
89 static void gtk_layout_forall             (GtkContainer   *container,
90                                            gboolean        include_internals,
91                                            GtkCallback     callback,
92                                            gpointer        callback_data);
93 static void gtk_layout_set_adjustments    (GtkLayout      *layout,
94                                            GtkAdjustment  *hadj,
95                                            GtkAdjustment  *vadj);
96 static void gtk_layout_set_child_property (GtkContainer   *container,
97                                            GtkWidget      *child,
98                                            guint           property_id,
99                                            const GValue   *value,
100                                            GParamSpec     *pspec);
101 static void gtk_layout_get_child_property (GtkContainer   *container,
102                                            GtkWidget      *child,
103                                            guint           property_id,
104                                            GValue         *value,
105                                            GParamSpec     *pspec);
106 static void gtk_layout_allocate_child     (GtkLayout      *layout,
107                                            GtkLayoutChild *child);
108 static void gtk_layout_adjustment_changed (GtkAdjustment  *adjustment,
109                                            GtkLayout      *layout);
110 static void gtk_layout_style_set          (GtkWidget      *widget,
111                                            GtkStyle       *old_style);
112
113 static void gtk_layout_set_adjustment_upper (GtkAdjustment *adj,
114                                              gdouble        upper,
115                                              gboolean       always_emit_changed);
116
117 static GtkWidgetClass *parent_class = NULL;
118
119 /* Public interface
120  */
121 /**
122  * gtk_layout_new:
123  * @hadjustment: horizontal scroll adjustment, or %NULL
124  * @vadjustment: vertical scroll adjustment, or %NULL
125  * 
126  * Creates a new #GtkLayout. Unless you have a specific adjustment
127  * you'd like the layout to use for scrolling, pass %NULL for
128  * @hadjustment and @vadjustment.
129  * 
130  * Return value: a new #GtkLayout
131  **/
132   
133 GtkWidget*    
134 gtk_layout_new (GtkAdjustment *hadjustment,
135                 GtkAdjustment *vadjustment)
136 {
137   GtkLayout *layout;
138
139   layout = g_object_new (GTK_TYPE_LAYOUT,
140                          "hadjustment", hadjustment,
141                          "vadjustment", vadjustment,
142                          NULL);
143
144   return GTK_WIDGET (layout);
145 }
146
147 /**
148  * gtk_layout_get_hadjustment:
149  * @layout: a #GtkLayout
150  * 
151  * This function should only be called after the layout has been
152  * placed in a #GtkScrolledWindow or otherwise configured for
153  * scrolling. It returns the #GtkAdjustment used for communication
154  * between the horizontal scrollbar and @layout.
155  *
156  * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
157  * 
158  * Return value: horizontal scroll adjustment
159  **/
160 GtkAdjustment* 
161 gtk_layout_get_hadjustment (GtkLayout     *layout)
162 {
163   g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);
164
165   return layout->hadjustment;
166 }
167 /**
168  * gtk_layout_get_vadjustment:
169  * @layout: a #GtkLayout
170  * 
171  * This function should only be called after the layout has been
172  * placed in a #GtkScrolledWindow or otherwise configured for
173  * scrolling. It returns the #GtkAdjustment used for communication
174  * between the vertical scrollbar and @layout.
175  *
176  * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
177  * 
178  * Return value: vertical scroll adjustment
179  **/
180 GtkAdjustment* 
181 gtk_layout_get_vadjustment (GtkLayout     *layout)
182 {
183   g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);
184
185   return layout->vadjustment;
186 }
187
188 static GtkAdjustment *
189 new_default_adjustment (void)
190 {
191   return GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
192 }
193
194 static void           
195 gtk_layout_set_adjustments (GtkLayout     *layout,
196                             GtkAdjustment *hadj,
197                             GtkAdjustment *vadj)
198 {
199   gboolean need_adjust = FALSE;
200
201   g_return_if_fail (GTK_IS_LAYOUT (layout));
202
203   if (hadj)
204     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
205   else if (layout->hadjustment)
206     hadj = new_default_adjustment ();
207   if (vadj)
208     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
209   else if (layout->vadjustment)
210     vadj = new_default_adjustment ();
211   
212   if (layout->hadjustment && (layout->hadjustment != hadj))
213     {
214       g_signal_handlers_disconnect_by_func (layout->hadjustment,
215                                             gtk_layout_adjustment_changed,
216                                             layout);
217       g_object_unref (layout->hadjustment);
218     }
219   
220   if (layout->vadjustment && (layout->vadjustment != vadj))
221     {
222       g_signal_handlers_disconnect_by_func (layout->vadjustment,
223                                             gtk_layout_adjustment_changed,
224                                             layout);
225       g_object_unref (layout->vadjustment);
226     }
227   
228   if (layout->hadjustment != hadj)
229     {
230       layout->hadjustment = hadj;
231       g_object_ref (layout->hadjustment);
232       gtk_object_sink (GTK_OBJECT (layout->hadjustment));
233       gtk_layout_set_adjustment_upper (layout->hadjustment, layout->width, FALSE);
234       
235       g_signal_connect (layout->hadjustment, "value_changed",
236                         G_CALLBACK (gtk_layout_adjustment_changed),
237                         layout);
238       need_adjust = TRUE;
239     }
240   
241   if (layout->vadjustment != vadj)
242     {
243       layout->vadjustment = vadj;
244       g_object_ref (layout->vadjustment);
245       gtk_object_sink (GTK_OBJECT (layout->vadjustment));
246       gtk_layout_set_adjustment_upper (layout->vadjustment, layout->height, FALSE);
247       
248       g_signal_connect (layout->vadjustment, "value_changed",
249                         G_CALLBACK (gtk_layout_adjustment_changed),
250                         layout);
251       need_adjust = TRUE;
252     }
253
254   /* vadj or hadj can be NULL while constructing; don't emit a signal
255      then */
256   if (need_adjust && vadj && hadj)
257     gtk_layout_adjustment_changed (NULL, layout);
258 }
259
260 static void
261 gtk_layout_finalize (GObject *object)
262 {
263   GtkLayout *layout = GTK_LAYOUT (object);
264
265   g_object_unref (layout->hadjustment);
266   g_object_unref (layout->vadjustment);
267
268   G_OBJECT_CLASS (parent_class)->finalize (object);
269 }
270
271 /**
272  * gtk_layout_set_hadjustment:
273  * @layout: a #GtkLayout
274  * @adjustment: new scroll adjustment
275  *
276  * Sets the horizontal scroll adjustment for the layout.
277  *
278  * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
279  * 
280  **/
281 void           
282 gtk_layout_set_hadjustment (GtkLayout     *layout,
283                             GtkAdjustment *adjustment)
284 {
285   g_return_if_fail (GTK_IS_LAYOUT (layout));
286
287   gtk_layout_set_adjustments (layout, adjustment, layout->vadjustment);
288   g_object_notify (G_OBJECT (layout), "hadjustment");
289 }
290  
291 /**
292  * gtk_layout_set_vadjustment:
293  * @layout: a #GtkLayout
294  * @adjustment: new scroll adjustment
295  *
296  * Sets the vertical scroll adjustment for the layout.
297  *
298  * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
299  * 
300  **/
301 void           
302 gtk_layout_set_vadjustment (GtkLayout     *layout,
303                             GtkAdjustment *adjustment)
304 {
305   g_return_if_fail (GTK_IS_LAYOUT (layout));
306   
307   gtk_layout_set_adjustments (layout, layout->hadjustment, adjustment);
308   g_object_notify (G_OBJECT (layout), "vadjustment");
309 }
310
311 static GtkLayoutChild*
312 get_child (GtkLayout  *layout,
313            GtkWidget  *widget)
314 {
315   GList *children;
316   
317   children = layout->children;
318   while (children)
319     {
320       GtkLayoutChild *child;
321       
322       child = children->data;
323       children = children->next;
324
325       if (child->widget == widget)
326         return child;
327     }
328
329   return NULL;
330 }
331
332 /**
333  * gtk_layout_put:
334  * @layout: a #GtkLayout
335  * @child_widget: child widget
336  * @x: X position of child widget
337  * @y: Y position of child widget
338  *
339  * Adds @child_widget to @layout, at position (@x,@y).
340  * @layout becomes the new parent container of @child_widget.
341  * 
342  **/
343 void           
344 gtk_layout_put (GtkLayout     *layout, 
345                 GtkWidget     *child_widget, 
346                 gint           x, 
347                 gint           y)
348 {
349   GtkLayoutChild *child;
350
351   g_return_if_fail (GTK_IS_LAYOUT (layout));
352   g_return_if_fail (GTK_IS_WIDGET (child_widget));
353   
354   child = g_new (GtkLayoutChild, 1);
355
356   child->widget = child_widget;
357   child->x = x;
358   child->y = y;
359
360   layout->children = g_list_append (layout->children, child);
361   
362   if (GTK_WIDGET_REALIZED (layout))
363     gtk_widget_set_parent_window (child->widget, layout->bin_window);
364
365   gtk_widget_set_parent (child_widget, GTK_WIDGET (layout));
366 }
367
368 static void
369 gtk_layout_move_internal (GtkLayout       *layout,
370                           GtkWidget       *widget,
371                           gboolean         change_x,
372                           gint             x,
373                           gboolean         change_y,
374                           gint             y)
375 {
376   GtkLayoutChild *child;
377   
378   g_return_if_fail (GTK_IS_LAYOUT (layout));
379   g_return_if_fail (GTK_IS_WIDGET (widget));
380   g_return_if_fail (widget->parent == GTK_WIDGET (layout));  
381   
382   child = get_child (layout, widget);
383
384   g_assert (child);
385
386   gtk_widget_freeze_child_notify (widget);
387   
388   if (change_x)
389     {
390       child->x = x;
391       gtk_widget_child_notify (widget, "x");
392     }
393
394   if (change_y)
395     {
396       child->y = y;
397       gtk_widget_child_notify (widget, "y");
398     }
399
400   gtk_widget_thaw_child_notify (widget);
401   
402   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_VISIBLE (layout))
403     gtk_widget_queue_resize (GTK_WIDGET (widget));
404 }
405
406 /**
407  * gtk_layout_move:
408  * @layout: a #GtkLayout
409  * @child_widget: a current child of @layout
410  * @x: X position to move to
411  * @y: Y position to move to
412  *
413  * Moves a current child of @layout to a new position.
414  * 
415  **/
416 void           
417 gtk_layout_move (GtkLayout     *layout, 
418                  GtkWidget     *child_widget, 
419                  gint           x, 
420                  gint           y)
421 {
422   g_return_if_fail (GTK_IS_LAYOUT (layout));
423   g_return_if_fail (GTK_IS_WIDGET (child_widget));
424   g_return_if_fail (child_widget->parent == GTK_WIDGET (layout));  
425
426   gtk_layout_move_internal (layout, child_widget, TRUE, x, TRUE, y);
427 }
428
429 static void
430 gtk_layout_set_adjustment_upper (GtkAdjustment *adj,
431                                  gdouble        upper,
432                                  gboolean       always_emit_changed)
433 {
434   gboolean changed = FALSE;
435   gboolean value_changed = FALSE;
436   
437   gdouble min = MAX (0., upper - adj->page_size);
438
439   if (upper != adj->upper)
440     {
441       adj->upper = upper;
442       changed = TRUE;
443     }
444       
445   if (adj->value > min)
446     {
447       adj->value = min;
448       value_changed = TRUE;
449     }
450   
451   if (changed || always_emit_changed)
452     gtk_adjustment_changed (adj);
453   if (value_changed)
454     gtk_adjustment_value_changed (adj);
455 }
456
457 /**
458  * gtk_layout_set_size:
459  * @layout: a #GtkLayout
460  * @width: width of entire scrollable area
461  * @height: height of entire scrollable area
462  *
463  * Sets the size of the scrollable area of the layout.
464  * 
465  **/
466 void
467 gtk_layout_set_size (GtkLayout     *layout, 
468                      guint          width,
469                      guint          height)
470 {
471   GtkWidget *widget;
472   
473   g_return_if_fail (GTK_IS_LAYOUT (layout));
474   
475   widget = GTK_WIDGET (layout);
476   
477   g_object_freeze_notify (G_OBJECT (layout));
478   if (width != layout->width)
479      {
480         layout->width = width;
481         g_object_notify (G_OBJECT (layout), "width");
482      }
483   if (height != layout->height)
484      {
485         layout->height = height;
486         g_object_notify (G_OBJECT (layout), "height");
487      }
488   g_object_thaw_notify (G_OBJECT (layout));
489
490   if (layout->hadjustment)
491     gtk_layout_set_adjustment_upper (layout->hadjustment, layout->width, FALSE);
492   if (layout->vadjustment)
493     gtk_layout_set_adjustment_upper (layout->vadjustment, layout->height, FALSE);
494
495   if (GTK_WIDGET_REALIZED (layout))
496     {
497       width = MAX (width, widget->allocation.width);
498       height = MAX (height, widget->allocation.height);
499       gdk_window_resize (layout->bin_window, width, height);
500     }
501 }
502
503 /**
504  * gtk_layout_get_size:
505  * @layout: a #GtkLayout
506  * @width: location to store the width set on @layout, or %NULL
507  * @height: location to store the height set on @layout, or %NULL
508  *
509  * Gets the size that has been set on the layout, and that determines
510  * the total extents of the layout's scrollbar area. See
511  * gtk_layout_set_size ().
512  **/
513 void
514 gtk_layout_get_size (GtkLayout *layout,
515                      guint     *width,
516                      guint     *height)
517 {
518   g_return_if_fail (GTK_IS_LAYOUT (layout));
519
520   if (width)
521     *width = layout->width;
522   if (height)
523     *height = layout->height;
524 }
525
526 /**
527  * gtk_layout_freeze:
528  * @layout: a #GtkLayout
529  * 
530  * This is a deprecated function, it doesn't do anything useful.
531  **/
532 void
533 gtk_layout_freeze (GtkLayout *layout)
534 {
535   g_return_if_fail (GTK_IS_LAYOUT (layout));
536
537   layout->freeze_count++;
538 }
539
540 /**
541  * gtk_layout_thaw:
542  * @layout: a #GtkLayout
543  * 
544  * This is a deprecated function, it doesn't do anything useful.
545  **/
546 void
547 gtk_layout_thaw (GtkLayout *layout)
548 {
549   g_return_if_fail (GTK_IS_LAYOUT (layout));
550
551   if (layout->freeze_count)
552     {
553       if (!(--layout->freeze_count))
554         {
555           gtk_widget_queue_draw (GTK_WIDGET (layout));
556           gdk_window_process_updates (GTK_WIDGET (layout)->window, TRUE);
557         }
558     }
559
560 }
561
562 /* Basic Object handling procedures
563  */
564 GType
565 gtk_layout_get_type (void)
566 {
567   static GType layout_type = 0;
568
569   if (!layout_type)
570     {
571       static const GTypeInfo layout_info =
572       {
573         sizeof (GtkLayoutClass),
574         NULL,           /* base_init */
575         NULL,           /* base_finalize */
576         (GClassInitFunc) gtk_layout_class_init,
577         NULL,           /* class_finalize */
578         NULL,           /* class_data */
579         sizeof (GtkLayout),
580         0,              /* n_preallocs */
581         (GInstanceInitFunc) gtk_layout_init,
582       };
583
584       layout_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkLayout",
585                                             &layout_info, 0);
586     }
587
588   return layout_type;
589 }
590
591 static void
592 gtk_layout_class_init (GtkLayoutClass *class)
593 {
594   GObjectClass *gobject_class;
595   GtkWidgetClass *widget_class;
596   GtkContainerClass *container_class;
597
598   gobject_class = (GObjectClass*) class;
599   widget_class = (GtkWidgetClass*) class;
600   container_class = (GtkContainerClass*) class;
601
602   parent_class = g_type_class_peek_parent (class);
603
604   gobject_class->set_property = gtk_layout_set_property;
605   gobject_class->get_property = gtk_layout_get_property;
606   gobject_class->finalize = gtk_layout_finalize;
607   gobject_class->constructor = gtk_layout_constructor;
608
609   container_class->set_child_property = gtk_layout_set_child_property;
610   container_class->get_child_property = gtk_layout_get_child_property;
611
612   gtk_container_class_install_child_property (container_class,
613                                               CHILD_PROP_X,
614                                               g_param_spec_int ("x",
615                                                                 P_("X position"),
616                                                                 P_("X position of child widget"),
617                                                                 G_MININT,
618                                                                 G_MAXINT,
619                                                                 0,
620                                                                 G_PARAM_READWRITE));
621
622   gtk_container_class_install_child_property (container_class,
623                                               CHILD_PROP_Y,
624                                               g_param_spec_int ("y",
625                                                                 P_("Y position"),
626                                                                 P_("Y position of child widget"),
627                                                                 G_MININT,
628                                                                 G_MAXINT,
629                                                                 0,
630                                                                 G_PARAM_READWRITE));
631   
632   g_object_class_install_property (gobject_class,
633                                    PROP_HADJUSTMENT,
634                                    g_param_spec_object ("hadjustment",
635                                                         P_("Horizontal adjustment"),
636                                                         P_("The GtkAdjustment for the horizontal position"),
637                                                         GTK_TYPE_ADJUSTMENT,
638                                                         G_PARAM_READWRITE));
639   
640   g_object_class_install_property (gobject_class,
641                                    PROP_VADJUSTMENT,
642                                    g_param_spec_object ("vadjustment",
643                                                         P_("Vertical adjustment"),
644                                                         P_("The GtkAdjustment for the vertical position"),
645                                                         GTK_TYPE_ADJUSTMENT,
646                                                         G_PARAM_READWRITE));
647
648   g_object_class_install_property (gobject_class,
649                                    PROP_WIDTH,
650                                    g_param_spec_uint ("width",
651                                                      P_("Width"),
652                                                      P_("The width of the layout"),
653                                                      0,
654                                                      G_MAXINT,
655                                                      100,
656                                                      G_PARAM_READWRITE));
657   g_object_class_install_property (gobject_class,
658                                    PROP_HEIGHT,
659                                    g_param_spec_uint ("height",
660                                                      P_("Height"),
661                                                      P_("The height of the layout"),
662                                                      0,
663                                                      G_MAXINT,
664                                                      100,
665                                                      G_PARAM_READWRITE));
666   widget_class->realize = gtk_layout_realize;
667   widget_class->unrealize = gtk_layout_unrealize;
668   widget_class->map = gtk_layout_map;
669   widget_class->size_request = gtk_layout_size_request;
670   widget_class->size_allocate = gtk_layout_size_allocate;
671   widget_class->expose_event = gtk_layout_expose;
672   widget_class->style_set = gtk_layout_style_set;
673
674   container_class->add = gtk_layout_add;
675   container_class->remove = gtk_layout_remove;
676   container_class->forall = gtk_layout_forall;
677
678   class->set_scroll_adjustments = gtk_layout_set_adjustments;
679
680   widget_class->set_scroll_adjustments_signal =
681     g_signal_new ("set_scroll_adjustments",
682                   G_OBJECT_CLASS_TYPE (gobject_class),
683                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
684                   G_STRUCT_OFFSET (GtkLayoutClass, set_scroll_adjustments),
685                   NULL, NULL,
686                   _gtk_marshal_VOID__OBJECT_OBJECT,
687                   G_TYPE_NONE, 2,
688                   GTK_TYPE_ADJUSTMENT,
689                   GTK_TYPE_ADJUSTMENT);
690 }
691
692 static void
693 gtk_layout_get_property (GObject     *object,
694                          guint        prop_id,
695                          GValue      *value,
696                          GParamSpec  *pspec)
697 {
698   GtkLayout *layout = GTK_LAYOUT (object);
699   
700   switch (prop_id)
701     {
702     case PROP_HADJUSTMENT:
703       g_value_set_object (value, layout->hadjustment);
704       break;
705     case PROP_VADJUSTMENT:
706       g_value_set_object (value, layout->vadjustment);
707       break;
708     case PROP_WIDTH:
709       g_value_set_uint (value, layout->width);
710       break;
711     case PROP_HEIGHT:
712       g_value_set_uint (value, layout->height);
713       break;
714     default:
715       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
716       break;
717     }
718 }
719
720 static void
721 gtk_layout_set_property (GObject      *object,
722                          guint         prop_id,
723                          const GValue *value,
724                          GParamSpec   *pspec)
725 {
726   GtkLayout *layout = GTK_LAYOUT (object);
727   
728   switch (prop_id)
729     {
730     case PROP_HADJUSTMENT:
731       gtk_layout_set_hadjustment (layout, 
732                                   (GtkAdjustment*) g_value_get_object (value));
733       break;
734     case PROP_VADJUSTMENT:
735       gtk_layout_set_vadjustment (layout, 
736                                   (GtkAdjustment*) g_value_get_object (value));
737       break;
738     case PROP_WIDTH:
739       gtk_layout_set_size (layout, g_value_get_uint (value),
740                            layout->height);
741       break;
742     case PROP_HEIGHT:
743       gtk_layout_set_size (layout, layout->width,
744                            g_value_get_uint (value));
745       break;
746     default:
747       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
748       break;
749     }
750 }
751
752 static void
753 gtk_layout_set_child_property (GtkContainer    *container,
754                                GtkWidget       *child,
755                                guint            property_id,
756                                const GValue    *value,
757                                GParamSpec      *pspec)
758 {
759   switch (property_id)
760     {
761     case CHILD_PROP_X:
762       gtk_layout_move_internal (GTK_LAYOUT (container),
763                                 child,
764                                 TRUE, g_value_get_int (value),
765                                 FALSE, 0);
766       break;
767     case CHILD_PROP_Y:
768       gtk_layout_move_internal (GTK_LAYOUT (container),
769                                 child,
770                                 FALSE, 0,
771                                 TRUE, g_value_get_int (value));
772       break;
773     default:
774       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
775       break;
776     }
777 }
778
779 static void
780 gtk_layout_get_child_property (GtkContainer *container,
781                                GtkWidget    *child,
782                                guint         property_id,
783                                GValue       *value,
784                                GParamSpec   *pspec)
785 {
786   GtkLayoutChild *layout_child;
787
788   layout_child = get_child (GTK_LAYOUT (container), child);
789   
790   switch (property_id)
791     {
792     case CHILD_PROP_X:
793       g_value_set_int (value, layout_child->x);
794       break;
795     case CHILD_PROP_Y:
796       g_value_set_int (value, layout_child->y);
797       break;
798     default:
799       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
800       break;
801     }
802 }
803
804 static void
805 gtk_layout_init (GtkLayout *layout)
806 {
807   layout->children = NULL;
808
809   layout->width = 100;
810   layout->height = 100;
811
812   layout->hadjustment = NULL;
813   layout->vadjustment = NULL;
814
815   layout->bin_window = NULL;
816
817   layout->scroll_x = 0;
818   layout->scroll_y = 0;
819   layout->visibility = GDK_VISIBILITY_PARTIAL;
820
821   layout->freeze_count = 0;
822 }
823
824 static GObject *
825 gtk_layout_constructor (GType                  type,
826                         guint                  n_properties,
827                         GObjectConstructParam *properties)
828 {
829   GtkLayout *layout;
830   GObject *object;
831   GtkAdjustment *hadj, *vadj;
832   
833   object = G_OBJECT_CLASS (parent_class)->constructor (type,
834                                                        n_properties,
835                                                        properties);
836
837   layout = GTK_LAYOUT (object);
838
839   hadj = layout->hadjustment ? layout->hadjustment : new_default_adjustment ();
840   vadj = layout->vadjustment ? layout->vadjustment : new_default_adjustment ();
841
842   if (!layout->hadjustment || !layout->vadjustment)
843     gtk_layout_set_adjustments (layout, hadj, vadj);
844
845   return object;
846 }
847
848 /* Widget methods
849  */
850
851 static void 
852 gtk_layout_realize (GtkWidget *widget)
853 {
854   GList *tmp_list;
855   GtkLayout *layout;
856   GdkWindowAttr attributes;
857   gint attributes_mask;
858
859   g_return_if_fail (GTK_IS_LAYOUT (widget));
860
861   layout = GTK_LAYOUT (widget);
862   GTK_WIDGET_SET_FLAGS (layout, GTK_REALIZED);
863
864   attributes.window_type = GDK_WINDOW_CHILD;
865   attributes.x = widget->allocation.x;
866   attributes.y = widget->allocation.y;
867   attributes.width = widget->allocation.width;
868   attributes.height = widget->allocation.height;
869   attributes.wclass = GDK_INPUT_OUTPUT;
870   attributes.visual = gtk_widget_get_visual (widget);
871   attributes.colormap = gtk_widget_get_colormap (widget);
872   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
873
874   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
875
876   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
877                                    &attributes, attributes_mask);
878   gdk_window_set_user_data (widget->window, widget);
879
880   attributes.x = - layout->hadjustment->value,
881   attributes.y = - layout->vadjustment->value;
882   attributes.width = MAX (layout->width, widget->allocation.width);
883   attributes.height = MAX (layout->height, widget->allocation.height);
884   attributes.event_mask = GDK_EXPOSURE_MASK | GDK_SCROLL_MASK | 
885                           gtk_widget_get_events (widget);
886
887   layout->bin_window = gdk_window_new (widget->window,
888                                         &attributes, attributes_mask);
889   gdk_window_set_user_data (layout->bin_window, widget);
890
891   widget->style = gtk_style_attach (widget->style, widget->window);
892   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
893   gtk_style_set_background (widget->style, layout->bin_window, GTK_STATE_NORMAL);
894
895   tmp_list = layout->children;
896   while (tmp_list)
897     {
898       GtkLayoutChild *child = tmp_list->data;
899       tmp_list = tmp_list->next;
900
901       gtk_widget_set_parent_window (child->widget, layout->bin_window);
902     }
903 }
904
905 static void
906 gtk_layout_style_set (GtkWidget *widget, GtkStyle *old_style)
907 {
908   if (GTK_WIDGET_CLASS (parent_class)->style_set)
909     (* GTK_WIDGET_CLASS (parent_class)->style_set) (widget, old_style);
910
911   if (GTK_WIDGET_REALIZED (widget))
912     {
913       gtk_style_set_background (widget->style, GTK_LAYOUT (widget)->bin_window, GTK_STATE_NORMAL);
914     }
915 }
916
917 static void 
918 gtk_layout_map (GtkWidget *widget)
919 {
920   GList *tmp_list;
921   GtkLayout *layout;
922
923   g_return_if_fail (GTK_IS_LAYOUT (widget));
924
925   layout = GTK_LAYOUT (widget);
926
927   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
928
929   tmp_list = layout->children;
930   while (tmp_list)
931     {
932       GtkLayoutChild *child = tmp_list->data;
933       tmp_list = tmp_list->next;
934
935       if (GTK_WIDGET_VISIBLE (child->widget))
936         {
937           if (!GTK_WIDGET_MAPPED (child->widget))
938             gtk_widget_map (child->widget);
939         }
940     }
941
942   gdk_window_show (layout->bin_window);
943   gdk_window_show (widget->window);
944 }
945
946 static void 
947 gtk_layout_unrealize (GtkWidget *widget)
948 {
949   GtkLayout *layout;
950
951   g_return_if_fail (GTK_IS_LAYOUT (widget));
952
953   layout = GTK_LAYOUT (widget);
954
955   gdk_window_set_user_data (layout->bin_window, NULL);
956   gdk_window_destroy (layout->bin_window);
957   layout->bin_window = NULL;
958
959   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
960     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
961 }
962
963 static void     
964 gtk_layout_size_request (GtkWidget     *widget,
965                          GtkRequisition *requisition)
966 {
967   GList *tmp_list;
968   GtkLayout *layout;
969
970   g_return_if_fail (GTK_IS_LAYOUT (widget));
971
972   layout = GTK_LAYOUT (widget);
973
974   requisition->width = 0;
975   requisition->height = 0;
976
977   tmp_list = layout->children;
978
979   while (tmp_list)
980     {
981       GtkLayoutChild *child = tmp_list->data;
982       GtkRequisition child_requisition;
983       
984       tmp_list = tmp_list->next;
985
986       gtk_widget_size_request (child->widget, &child_requisition);
987     }
988 }
989
990 static void     
991 gtk_layout_size_allocate (GtkWidget     *widget,
992                           GtkAllocation *allocation)
993 {
994   GList *tmp_list;
995   GtkLayout *layout;
996
997   g_return_if_fail (GTK_IS_LAYOUT (widget));
998
999   widget->allocation = *allocation;
1000   
1001   layout = GTK_LAYOUT (widget);
1002
1003   tmp_list = layout->children;
1004
1005   while (tmp_list)
1006     {
1007       GtkLayoutChild *child = tmp_list->data;
1008       tmp_list = tmp_list->next;
1009
1010       gtk_layout_allocate_child (layout, child);
1011     }
1012
1013   if (GTK_WIDGET_REALIZED (widget))
1014     {
1015       gdk_window_move_resize (widget->window,
1016                               allocation->x, allocation->y,
1017                               allocation->width, allocation->height);
1018
1019       gdk_window_resize (layout->bin_window,
1020                          MAX (layout->width, allocation->width),
1021                          MAX (layout->height, allocation->height));
1022     }
1023
1024   layout->hadjustment->page_size = allocation->width;
1025   layout->hadjustment->page_increment = allocation->width * 0.9;
1026   layout->hadjustment->lower = 0;
1027   /* set_adjustment_upper() emits ::changed */
1028   gtk_layout_set_adjustment_upper (layout->hadjustment, MAX (allocation->width, layout->width), TRUE);
1029
1030   layout->vadjustment->page_size = allocation->height;
1031   layout->vadjustment->page_increment = allocation->height * 0.9;
1032   layout->vadjustment->lower = 0;
1033   layout->vadjustment->upper = MAX (allocation->height, layout->height);
1034   gtk_layout_set_adjustment_upper (layout->vadjustment, MAX (allocation->height, layout->height), TRUE);
1035 }
1036
1037 static gint 
1038 gtk_layout_expose (GtkWidget *widget, GdkEventExpose *event)
1039 {
1040   GtkLayout *layout;
1041
1042   g_return_val_if_fail (GTK_IS_LAYOUT (widget), FALSE);
1043
1044   layout = GTK_LAYOUT (widget);
1045
1046   if (event->window != layout->bin_window)
1047     return FALSE;
1048   
1049   (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1050
1051   return FALSE;
1052 }
1053
1054 /* Container methods
1055  */
1056 static void
1057 gtk_layout_add (GtkContainer *container,
1058                 GtkWidget    *widget)
1059 {
1060   gtk_layout_put (GTK_LAYOUT (container), widget, 0, 0);
1061 }
1062
1063 static void
1064 gtk_layout_remove (GtkContainer *container, 
1065                    GtkWidget    *widget)
1066 {
1067   GList *tmp_list;
1068   GtkLayout *layout;
1069   GtkLayoutChild *child = NULL;
1070   
1071   g_return_if_fail (GTK_IS_LAYOUT (container));
1072   
1073   layout = GTK_LAYOUT (container);
1074
1075   tmp_list = layout->children;
1076   while (tmp_list)
1077     {
1078       child = tmp_list->data;
1079       if (child->widget == widget)
1080         break;
1081       tmp_list = tmp_list->next;
1082     }
1083
1084   if (tmp_list)
1085     {
1086       gtk_widget_unparent (widget);
1087
1088       layout->children = g_list_remove_link (layout->children, tmp_list);
1089       g_list_free_1 (tmp_list);
1090       g_free (child);
1091     }
1092 }
1093
1094 static void
1095 gtk_layout_forall (GtkContainer *container,
1096                    gboolean      include_internals,
1097                    GtkCallback   callback,
1098                    gpointer      callback_data)
1099 {
1100   GtkLayout *layout;
1101   GtkLayoutChild *child;
1102   GList *tmp_list;
1103
1104   g_return_if_fail (GTK_IS_LAYOUT (container));
1105   g_return_if_fail (callback != NULL);
1106
1107   layout = GTK_LAYOUT (container);
1108
1109   tmp_list = layout->children;
1110   while (tmp_list)
1111     {
1112       child = tmp_list->data;
1113       tmp_list = tmp_list->next;
1114
1115       (* callback) (child->widget, callback_data);
1116     }
1117 }
1118
1119 /* Operations on children
1120  */
1121
1122 static void
1123 gtk_layout_allocate_child (GtkLayout      *layout,
1124                            GtkLayoutChild *child)
1125 {
1126   GtkAllocation allocation;
1127   GtkRequisition requisition;
1128
1129   allocation.x = child->x;
1130   allocation.y = child->y;
1131   gtk_widget_get_child_requisition (child->widget, &requisition);
1132   allocation.width = requisition.width;
1133   allocation.height = requisition.height;
1134   
1135   gtk_widget_size_allocate (child->widget, &allocation);
1136 }
1137
1138 /* Callbacks */
1139
1140 static void
1141 gtk_layout_adjustment_changed (GtkAdjustment *adjustment,
1142                                GtkLayout     *layout)
1143 {
1144   GtkWidget *widget;
1145
1146   widget = GTK_WIDGET (layout);
1147
1148   if (layout->freeze_count)
1149     return;
1150
1151   if (GTK_WIDGET_REALIZED (layout))
1152     {
1153       gdk_window_move (layout->bin_window,
1154                        - layout->hadjustment->value,
1155                        - layout->vadjustment->value);
1156       
1157       gdk_window_process_updates (layout->bin_window, TRUE);
1158     }
1159 }