]> Pileus Git - ~andy/gtk/blob - gtk/gtklayout.c
0f9cfed09bd4982f03978baf3165fd17d5712196
[~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
33 #include "gtklayout.h"
34
35 #include "gdk/gdk.h"
36
37 #include "gtkprivate.h"
38 #include "gtkintl.h"
39 #include "gtkmarshalers.h"
40 #include "gtkscrollable.h"
41
42
43 /**
44  * SECTION:gtklayout
45  * @Short_description: Infinite scrollable area containing child widgets
46  *   and/or custom drawing
47  * @Title: GtkLayout
48  * @See_also: #GtkDrawingArea, #GtkScrolledWindow
49  *
50  * #GtkLayout is similar to #GtkDrawingArea in that it's a "blank slate"
51  * and doesn't do anything but paint a blank background by default. It's
52  * different in that it supports scrolling natively (you can add it to a
53  * #GtkScrolledWindow), and it can contain child widgets, since it's a
54  * #GtkContainer. However if you're just going to draw, a #GtkDrawingArea
55  * is a better choice since it has lower overhead.
56  *
57  * When handling expose events on a #GtkLayout, you must draw to
58  * GTK_LAYOUT (layout)->bin_window, rather than to
59  * GTK_WIDGET (layout)->window, as you would for a drawing
60  * area.
61  */
62
63
64 typedef struct _GtkLayoutChild   GtkLayoutChild;
65
66 struct _GtkLayoutPrivate
67 {
68   /* Properties */
69   guint width;
70   guint height;
71
72   GtkAdjustment *hadjustment;
73   GtkAdjustment *vadjustment;
74
75   /* GtkScrollablePolicy needs to be checked when
76    * driving the scrollable adjustment values */
77   guint hscroll_policy : 1;
78   guint vscroll_policy : 1;
79
80   /* Properties */
81
82   GdkVisibilityState visibility;
83   GdkWindow *bin_window;
84
85   GList *children;
86
87   gint scroll_x;
88   gint scroll_y;
89
90   guint freeze_count;
91 };
92
93 struct _GtkLayoutChild {
94   GtkWidget *widget;
95   gint x;
96   gint y;
97 };
98
99 enum {
100    PROP_0,
101    PROP_HADJUSTMENT,
102    PROP_VADJUSTMENT,
103    PROP_HSCROLL_POLICY,
104    PROP_VSCROLL_POLICY,
105    PROP_WIDTH,
106    PROP_HEIGHT
107 };
108
109 enum {
110   CHILD_PROP_0,
111   CHILD_PROP_X,
112   CHILD_PROP_Y
113 };
114
115 static void gtk_layout_get_property       (GObject        *object,
116                                            guint           prop_id,
117                                            GValue         *value,
118                                            GParamSpec     *pspec);
119 static void gtk_layout_set_property       (GObject        *object,
120                                            guint           prop_id,
121                                            const GValue   *value,
122                                            GParamSpec     *pspec);
123 static void gtk_layout_finalize           (GObject        *object);
124 static void gtk_layout_realize            (GtkWidget      *widget);
125 static void gtk_layout_unrealize          (GtkWidget      *widget);
126 static void gtk_layout_map                (GtkWidget      *widget);
127 static void gtk_layout_get_preferred_width  (GtkWidget     *widget,
128                                              gint          *minimum,
129                                              gint          *natural);
130 static void gtk_layout_get_preferred_height (GtkWidget     *widget,
131                                              gint          *minimum,
132                                              gint          *natural);
133 static void gtk_layout_size_allocate      (GtkWidget      *widget,
134                                            GtkAllocation  *allocation);
135 static gint gtk_layout_draw               (GtkWidget      *widget,
136                                            cairo_t        *cr);
137 static void gtk_layout_add                (GtkContainer   *container,
138                                            GtkWidget      *widget);
139 static void gtk_layout_remove             (GtkContainer   *container,
140                                            GtkWidget      *widget);
141 static void gtk_layout_forall             (GtkContainer   *container,
142                                            gboolean        include_internals,
143                                            GtkCallback     callback,
144                                            gpointer        callback_data);
145 static void gtk_layout_set_child_property (GtkContainer   *container,
146                                            GtkWidget      *child,
147                                            guint           property_id,
148                                            const GValue   *value,
149                                            GParamSpec     *pspec);
150 static void gtk_layout_get_child_property (GtkContainer   *container,
151                                            GtkWidget      *child,
152                                            guint           property_id,
153                                            GValue         *value,
154                                            GParamSpec     *pspec);
155 static void gtk_layout_allocate_child     (GtkLayout      *layout,
156                                            GtkLayoutChild *child);
157 static void gtk_layout_adjustment_changed (GtkAdjustment  *adjustment,
158                                            GtkLayout      *layout);
159 static void gtk_layout_style_updated      (GtkWidget      *widget);
160
161 static void gtk_layout_set_hadjustment_values (GtkLayout      *layout);
162 static void gtk_layout_set_vadjustment_values (GtkLayout      *layout);
163
164 G_DEFINE_TYPE_WITH_CODE (GtkLayout, gtk_layout, GTK_TYPE_CONTAINER,
165                          G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
166
167 /* Public interface
168  */
169 /**
170  * gtk_layout_new:
171  * @hadjustment: (allow-none): horizontal scroll adjustment, or %NULL
172  * @vadjustment: (allow-none): vertical scroll adjustment, or %NULL
173  * 
174  * Creates a new #GtkLayout. Unless you have a specific adjustment
175  * you'd like the layout to use for scrolling, pass %NULL for
176  * @hadjustment and @vadjustment.
177  * 
178  * Return value: a new #GtkLayout
179  **/
180   
181 GtkWidget*    
182 gtk_layout_new (GtkAdjustment *hadjustment,
183                 GtkAdjustment *vadjustment)
184 {
185   GtkLayout *layout;
186
187   layout = g_object_new (GTK_TYPE_LAYOUT,
188                          "hadjustment", hadjustment,
189                          "vadjustment", vadjustment,
190                          NULL);
191
192   return GTK_WIDGET (layout);
193 }
194
195 /**
196  * gtk_layout_get_bin_window:
197  * @layout: a #GtkLayout
198  *
199  * Retrieve the bin window of the layout used for drawing operations.
200  *
201  * Return value: (transfer none): a #GdkWindow
202  *
203  * Since: 2.14
204  **/
205 GdkWindow*
206 gtk_layout_get_bin_window (GtkLayout *layout)
207 {
208   g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);
209
210   return layout->priv->bin_window;
211 }
212
213 /**
214  * gtk_layout_get_hadjustment:
215  * @layout: a #GtkLayout
216  *
217  * This function should only be called after the layout has been
218  * placed in a #GtkScrolledWindow or otherwise configured for
219  * scrolling. It returns the #GtkAdjustment used for communication
220  * between the horizontal scrollbar and @layout.
221  *
222  * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
223  *
224  * Return value: (transfer none): horizontal scroll adjustment
225  *
226  * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
227  **/
228 GtkAdjustment*
229 gtk_layout_get_hadjustment (GtkLayout *layout)
230 {
231   g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);
232
233   return layout->priv->hadjustment;
234 }
235 /**
236  * gtk_layout_get_vadjustment:
237  * @layout: a #GtkLayout
238  *
239  * This function should only be called after the layout has been
240  * placed in a #GtkScrolledWindow or otherwise configured for
241  * scrolling. It returns the #GtkAdjustment used for communication
242  * between the vertical scrollbar and @layout.
243  *
244  * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
245  *
246  * Return value: (transfer none): vertical scroll adjustment
247  *
248  * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
249  **/
250 GtkAdjustment*
251 gtk_layout_get_vadjustment (GtkLayout *layout)
252 {
253   g_return_val_if_fail (GTK_IS_LAYOUT (layout), NULL);
254
255   return layout->priv->vadjustment;
256 }
257
258 static void
259 gtk_layout_set_hadjustment_values (GtkLayout *layout)
260 {
261   GtkLayoutPrivate *priv = layout->priv;
262   GtkAllocation  allocation;
263   GtkAdjustment *adj = priv->hadjustment;
264   gdouble old_value;
265   gdouble new_value;
266   gdouble new_upper;
267
268   gtk_widget_get_allocation (GTK_WIDGET (layout), &allocation);
269
270   old_value = gtk_adjustment_get_value (adj);
271   new_upper = MAX (allocation.width, priv->width);
272
273   g_object_set (adj,
274                 "lower", 0.0,
275                 "upper", new_upper,
276                 "page-size", (gdouble)allocation.width,
277                 "step-increment", allocation.width * 0.1,
278                 "page-increment", allocation.width * 0.9,
279                 NULL);
280
281   new_value = CLAMP (old_value, 0, new_upper - allocation.width);
282   if (new_value != old_value)
283     gtk_adjustment_set_value (adj, new_value);
284 }
285
286 static void
287 gtk_layout_set_vadjustment_values (GtkLayout *layout)
288 {
289   GtkAllocation  allocation;
290   GtkAdjustment *adj = layout->priv->vadjustment;
291   gdouble old_value;
292   gdouble new_value;
293   gdouble new_upper;
294
295   gtk_widget_get_allocation (GTK_WIDGET (layout), &allocation);
296
297   old_value = gtk_adjustment_get_value (adj);
298   new_upper = MAX (allocation.height, layout->priv->height);
299
300   g_object_set (adj,
301                 "lower", 0.0,
302                 "upper", new_upper,
303                 "page-size", (gdouble)allocation.height,
304                 "step-increment", allocation.height * 0.1,
305                 "page-increment", allocation.height * 0.9,
306                 NULL);
307
308   new_value = CLAMP (old_value, 0, new_upper - allocation.height);
309   if (new_value != old_value)
310     gtk_adjustment_set_value (adj, new_value);
311 }
312
313 static void
314 gtk_layout_finalize (GObject *object)
315 {
316   GtkLayout *layout = GTK_LAYOUT (object);
317   GtkLayoutPrivate *priv = layout->priv;
318
319   g_object_unref (priv->hadjustment);
320   g_object_unref (priv->vadjustment);
321
322   G_OBJECT_CLASS (gtk_layout_parent_class)->finalize (object);
323 }
324
325 static void
326 gtk_layout_do_set_hadjustment (GtkLayout     *layout,
327                                GtkAdjustment *adjustment)
328 {
329   GtkLayoutPrivate *priv;
330
331   priv = layout->priv;
332
333   if (adjustment && priv->hadjustment == adjustment)
334         return;
335
336   if (priv->hadjustment != NULL)
337     {
338       g_signal_handlers_disconnect_by_func (priv->hadjustment,
339                                             gtk_layout_adjustment_changed,
340                                             layout);
341       g_object_unref (priv->hadjustment);
342     }
343
344   if (adjustment == NULL)
345     adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
346
347   g_signal_connect (adjustment, "value-changed",
348                     G_CALLBACK (gtk_layout_adjustment_changed), layout);
349   priv->hadjustment = g_object_ref_sink (adjustment);
350   gtk_layout_set_hadjustment_values (layout);
351
352   g_object_notify (G_OBJECT (layout), "hadjustment");
353 }
354
355 /**
356  * gtk_layout_set_hadjustment:
357  * @layout: a #GtkLayout
358  * @adjustment: (allow-none): new scroll adjustment
359  *
360  * Sets the horizontal scroll adjustment for the layout.
361  *
362  * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
363  *
364  * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
365  **/
366 void
367 gtk_layout_set_hadjustment (GtkLayout     *layout,
368                             GtkAdjustment *adjustment)
369 {
370   g_return_if_fail (GTK_IS_LAYOUT (layout));
371   g_return_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment));
372
373   gtk_layout_do_set_hadjustment (layout, adjustment);
374 }
375
376 static void
377 gtk_layout_do_set_vadjustment (GtkLayout     *layout,
378                                GtkAdjustment *adjustment)
379 {
380   GtkLayoutPrivate *priv;
381
382   priv = layout->priv;
383
384   if (adjustment && priv->vadjustment == adjustment)
385         return;
386
387   if (priv->vadjustment != NULL)
388     {
389       g_signal_handlers_disconnect_by_func (priv->vadjustment,
390                                             gtk_layout_adjustment_changed,
391                                             layout);
392       g_object_unref (priv->vadjustment);
393     }
394
395   if (adjustment == NULL)
396     adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
397
398   g_signal_connect (adjustment, "value-changed",
399                     G_CALLBACK (gtk_layout_adjustment_changed), layout);
400   priv->vadjustment = g_object_ref_sink (adjustment);
401   gtk_layout_set_vadjustment_values (layout);
402
403   g_object_notify (G_OBJECT (layout), "vadjustment");
404 }
405
406 /**
407  * gtk_layout_set_vadjustment:
408  * @layout: a #GtkLayout
409  * @adjustment: (allow-none): new scroll adjustment
410  *
411  * Sets the vertical scroll adjustment for the layout.
412  *
413  * See #GtkScrolledWindow, #GtkScrollbar, #GtkAdjustment for details.
414  *
415  * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
416  **/
417 void
418 gtk_layout_set_vadjustment (GtkLayout     *layout,
419                             GtkAdjustment *adjustment)
420 {
421   g_return_if_fail (GTK_IS_LAYOUT (layout));
422   g_return_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment));
423
424   gtk_layout_do_set_vadjustment (layout, adjustment);
425 }
426
427 static GtkLayoutChild*
428 get_child (GtkLayout  *layout,
429            GtkWidget  *widget)
430 {
431   GtkLayoutPrivate *priv = layout->priv;
432   GList *children;
433
434   children = priv->children;
435   while (children)
436     {
437       GtkLayoutChild *child;
438       
439       child = children->data;
440       children = children->next;
441
442       if (child->widget == widget)
443         return child;
444     }
445
446   return NULL;
447 }
448
449 /**
450  * gtk_layout_put:
451  * @layout: a #GtkLayout
452  * @child_widget: child widget
453  * @x: X position of child widget
454  * @y: Y position of child widget
455  *
456  * Adds @child_widget to @layout, at position (@x,@y).
457  * @layout becomes the new parent container of @child_widget.
458  * 
459  **/
460 void           
461 gtk_layout_put (GtkLayout     *layout, 
462                 GtkWidget     *child_widget, 
463                 gint           x, 
464                 gint           y)
465 {
466   GtkLayoutPrivate *priv;
467   GtkLayoutChild *child;
468
469   g_return_if_fail (GTK_IS_LAYOUT (layout));
470   g_return_if_fail (GTK_IS_WIDGET (child_widget));
471
472   priv = layout->priv;
473
474   child = g_new (GtkLayoutChild, 1);
475
476   child->widget = child_widget;
477   child->x = x;
478   child->y = y;
479
480   priv->children = g_list_append (priv->children, child);
481
482   if (gtk_widget_get_realized (GTK_WIDGET (layout)))
483     gtk_widget_set_parent_window (child->widget, priv->bin_window);
484
485   gtk_widget_set_parent (child_widget, GTK_WIDGET (layout));
486 }
487
488 static void
489 gtk_layout_move_internal (GtkLayout       *layout,
490                           GtkWidget       *widget,
491                           gboolean         change_x,
492                           gint             x,
493                           gboolean         change_y,
494                           gint             y)
495 {
496   GtkLayoutChild *child;
497
498   child = get_child (layout, widget);
499
500   g_assert (child);
501
502   gtk_widget_freeze_child_notify (widget);
503   
504   if (change_x)
505     {
506       child->x = x;
507       gtk_widget_child_notify (widget, "x");
508     }
509
510   if (change_y)
511     {
512       child->y = y;
513       gtk_widget_child_notify (widget, "y");
514     }
515
516   gtk_widget_thaw_child_notify (widget);
517   
518   if (gtk_widget_get_visible (widget) &&
519       gtk_widget_get_visible (GTK_WIDGET (layout)))
520     gtk_widget_queue_resize (widget);
521 }
522
523 /**
524  * gtk_layout_move:
525  * @layout: a #GtkLayout
526  * @child_widget: a current child of @layout
527  * @x: X position to move to
528  * @y: Y position to move to
529  *
530  * Moves a current child of @layout to a new position.
531  * 
532  **/
533 void           
534 gtk_layout_move (GtkLayout     *layout, 
535                  GtkWidget     *child_widget, 
536                  gint           x, 
537                  gint           y)
538 {
539   g_return_if_fail (GTK_IS_LAYOUT (layout));
540   g_return_if_fail (GTK_IS_WIDGET (child_widget));
541   g_return_if_fail (gtk_widget_get_parent (child_widget) == GTK_WIDGET (layout));
542
543   gtk_layout_move_internal (layout, child_widget, TRUE, x, TRUE, y);
544 }
545
546 /**
547  * gtk_layout_set_size:
548  * @layout: a #GtkLayout
549  * @width: width of entire scrollable area
550  * @height: height of entire scrollable area
551  *
552  * Sets the size of the scrollable area of the layout.
553  * 
554  **/
555 void
556 gtk_layout_set_size (GtkLayout     *layout, 
557                      guint          width,
558                      guint          height)
559 {
560   GtkLayoutPrivate *priv;
561   GtkWidget *widget;
562
563   g_return_if_fail (GTK_IS_LAYOUT (layout));
564
565   priv = layout->priv;
566   widget = GTK_WIDGET (layout);
567
568   g_object_freeze_notify (G_OBJECT (layout));
569   if (width != priv->width)
570      {
571         priv->width = width;
572         g_object_notify (G_OBJECT (layout), "width");
573      }
574   if (height != priv->height)
575      {
576         priv->height = height;
577         g_object_notify (G_OBJECT (layout), "height");
578      }
579   g_object_thaw_notify (G_OBJECT (layout));
580
581   if (gtk_widget_get_realized (widget))
582     {
583       GtkAllocation allocation;
584
585       gtk_widget_get_allocation (widget, &allocation);
586       width = MAX (width, allocation.width);
587       height = MAX (height, allocation.height);
588       gdk_window_resize (priv->bin_window, width, height);
589     }
590
591   gtk_layout_set_hadjustment_values (layout);
592   gtk_layout_set_vadjustment_values (layout);
593 }
594
595 /**
596  * gtk_layout_get_size:
597  * @layout: a #GtkLayout
598  * @width: (out) (allow-none): location to store the width set on
599  *     @layout, or %NULL
600  * @height: (out) (allow-none): location to store the height set on
601  *     @layout, or %NULL
602  *
603  * Gets the size that has been set on the layout, and that determines
604  * the total extents of the layout's scrollbar area. See
605  * gtk_layout_set_size ().
606  **/
607 void
608 gtk_layout_get_size (GtkLayout *layout,
609                      guint     *width,
610                      guint     *height)
611 {
612   GtkLayoutPrivate *priv;
613
614   g_return_if_fail (GTK_IS_LAYOUT (layout));
615
616   priv = layout->priv;
617
618   if (width)
619     *width = priv->width;
620   if (height)
621     *height = priv->height;
622 }
623
624 /* Basic Object handling procedures
625  */
626 static void
627 gtk_layout_class_init (GtkLayoutClass *class)
628 {
629   GObjectClass *gobject_class;
630   GtkWidgetClass *widget_class;
631   GtkContainerClass *container_class;
632
633   gobject_class = (GObjectClass*) class;
634   widget_class = (GtkWidgetClass*) class;
635   container_class = (GtkContainerClass*) class;
636
637   gobject_class->set_property = gtk_layout_set_property;
638   gobject_class->get_property = gtk_layout_get_property;
639   gobject_class->finalize = gtk_layout_finalize;
640
641   container_class->set_child_property = gtk_layout_set_child_property;
642   container_class->get_child_property = gtk_layout_get_child_property;
643
644   gtk_container_class_install_child_property (container_class,
645                                               CHILD_PROP_X,
646                                               g_param_spec_int ("x",
647                                                                 P_("X position"),
648                                                                 P_("X position of child widget"),
649                                                                 G_MININT,
650                                                                 G_MAXINT,
651                                                                 0,
652                                                                 GTK_PARAM_READWRITE));
653
654   gtk_container_class_install_child_property (container_class,
655                                               CHILD_PROP_Y,
656                                               g_param_spec_int ("y",
657                                                                 P_("Y position"),
658                                                                 P_("Y position of child widget"),
659                                                                 G_MININT,
660                                                                 G_MAXINT,
661                                                                 0,
662                                                                 GTK_PARAM_READWRITE));
663   
664   /* Scrollable interface */
665   g_object_class_override_property (gobject_class, PROP_HADJUSTMENT,    "hadjustment");
666   g_object_class_override_property (gobject_class, PROP_VADJUSTMENT,    "vadjustment");
667   g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
668   g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
669
670   g_object_class_install_property (gobject_class,
671                                    PROP_WIDTH,
672                                    g_param_spec_uint ("width",
673                                                      P_("Width"),
674                                                      P_("The width of the layout"),
675                                                      0,
676                                                      G_MAXINT,
677                                                      100,
678                                                      GTK_PARAM_READWRITE));
679   g_object_class_install_property (gobject_class,
680                                    PROP_HEIGHT,
681                                    g_param_spec_uint ("height",
682                                                      P_("Height"),
683                                                      P_("The height of the layout"),
684                                                      0,
685                                                      G_MAXINT,
686                                                      100,
687                                                      GTK_PARAM_READWRITE));
688   widget_class->realize = gtk_layout_realize;
689   widget_class->unrealize = gtk_layout_unrealize;
690   widget_class->map = gtk_layout_map;
691   widget_class->get_preferred_width = gtk_layout_get_preferred_width;
692   widget_class->get_preferred_height = gtk_layout_get_preferred_height;
693   widget_class->size_allocate = gtk_layout_size_allocate;
694   widget_class->draw = gtk_layout_draw;
695   widget_class->style_updated = gtk_layout_style_updated;
696
697   container_class->add = gtk_layout_add;
698   container_class->remove = gtk_layout_remove;
699   container_class->forall = gtk_layout_forall;
700
701   g_type_class_add_private (class, sizeof (GtkLayoutPrivate));
702 }
703
704 static void
705 gtk_layout_get_property (GObject     *object,
706                          guint        prop_id,
707                          GValue      *value,
708                          GParamSpec  *pspec)
709 {
710   GtkLayout *layout = GTK_LAYOUT (object);
711   GtkLayoutPrivate *priv = layout->priv;
712
713   switch (prop_id)
714     {
715     case PROP_HADJUSTMENT:
716       g_value_set_object (value, priv->hadjustment);
717       break;
718     case PROP_VADJUSTMENT:
719       g_value_set_object (value, priv->vadjustment);
720       break;
721     case PROP_HSCROLL_POLICY:
722       g_value_set_enum (value, priv->hscroll_policy);
723       break;
724     case PROP_VSCROLL_POLICY:
725       g_value_set_enum (value, priv->vscroll_policy);
726       break;
727     case PROP_WIDTH:
728       g_value_set_uint (value, priv->width);
729       break;
730     case PROP_HEIGHT:
731       g_value_set_uint (value, priv->height);
732       break;
733     default:
734       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
735       break;
736     }
737 }
738
739 static void
740 gtk_layout_set_property (GObject      *object,
741                          guint         prop_id,
742                          const GValue *value,
743                          GParamSpec   *pspec)
744 {
745   GtkLayout *layout = GTK_LAYOUT (object);
746   GtkLayoutPrivate *priv = layout->priv;
747
748   switch (prop_id)
749     {
750     case PROP_HADJUSTMENT:
751       gtk_layout_do_set_hadjustment (layout, g_value_get_object (value));
752       break;
753     case PROP_VADJUSTMENT:
754       gtk_layout_do_set_vadjustment (layout, g_value_get_object (value));
755       break;
756     case PROP_HSCROLL_POLICY:
757       priv->hscroll_policy = g_value_get_enum (value);
758       gtk_widget_queue_resize (GTK_WIDGET (layout));
759       break;
760     case PROP_VSCROLL_POLICY:
761       priv->vscroll_policy = g_value_get_enum (value);
762       gtk_widget_queue_resize (GTK_WIDGET (layout));
763       break;
764     case PROP_WIDTH:
765       gtk_layout_set_size (layout, g_value_get_uint (value),
766                            priv->height);
767       break;
768     case PROP_HEIGHT:
769       gtk_layout_set_size (layout, priv->width,
770                            g_value_get_uint (value));
771       break;
772     default:
773       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
774       break;
775     }
776 }
777
778 static void
779 gtk_layout_set_child_property (GtkContainer    *container,
780                                GtkWidget       *child,
781                                guint            property_id,
782                                const GValue    *value,
783                                GParamSpec      *pspec)
784 {
785   switch (property_id)
786     {
787     case CHILD_PROP_X:
788       gtk_layout_move_internal (GTK_LAYOUT (container),
789                                 child,
790                                 TRUE, g_value_get_int (value),
791                                 FALSE, 0);
792       break;
793     case CHILD_PROP_Y:
794       gtk_layout_move_internal (GTK_LAYOUT (container),
795                                 child,
796                                 FALSE, 0,
797                                 TRUE, g_value_get_int (value));
798       break;
799     default:
800       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
801       break;
802     }
803 }
804
805 static void
806 gtk_layout_get_child_property (GtkContainer *container,
807                                GtkWidget    *child,
808                                guint         property_id,
809                                GValue       *value,
810                                GParamSpec   *pspec)
811 {
812   GtkLayoutChild *layout_child;
813
814   layout_child = get_child (GTK_LAYOUT (container), child);
815   
816   switch (property_id)
817     {
818     case CHILD_PROP_X:
819       g_value_set_int (value, layout_child->x);
820       break;
821     case CHILD_PROP_Y:
822       g_value_set_int (value, layout_child->y);
823       break;
824     default:
825       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
826       break;
827     }
828 }
829
830 static void
831 gtk_layout_init (GtkLayout *layout)
832 {
833   GtkLayoutPrivate *priv;
834
835   layout->priv = G_TYPE_INSTANCE_GET_PRIVATE (layout,
836                                               GTK_TYPE_LAYOUT,
837                                               GtkLayoutPrivate);
838   priv = layout->priv;
839
840   priv->children = NULL;
841
842   priv->width = 100;
843   priv->height = 100;
844
845   priv->hadjustment = NULL;
846   priv->vadjustment = NULL;
847
848   priv->bin_window = NULL;
849
850   priv->scroll_x = 0;
851   priv->scroll_y = 0;
852   priv->visibility = GDK_VISIBILITY_PARTIAL;
853
854   priv->freeze_count = 0;
855 }
856
857 /* Widget methods
858  */
859
860 static void 
861 gtk_layout_realize (GtkWidget *widget)
862 {
863   GtkLayout *layout = GTK_LAYOUT (widget);
864   GtkLayoutPrivate *priv = layout->priv;
865   GtkAllocation allocation;
866   GdkWindow *window;
867   GdkWindowAttr attributes;
868   GList *tmp_list;
869   gint attributes_mask;
870
871   gtk_widget_set_realized (widget, TRUE);
872
873   gtk_widget_get_allocation (widget, &allocation);
874
875   attributes.window_type = GDK_WINDOW_CHILD;
876   attributes.x = allocation.x;
877   attributes.y = allocation.y;
878   attributes.width = allocation.width;
879   attributes.height = allocation.height;
880   attributes.wclass = GDK_INPUT_OUTPUT;
881   attributes.visual = gtk_widget_get_visual (widget);
882   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
883
884   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
885
886   window = gdk_window_new (gtk_widget_get_parent_window (widget),
887                            &attributes, attributes_mask);
888   gtk_widget_set_window (widget, window);
889   gdk_window_set_user_data (window, widget);
890
891   gtk_widget_get_allocation (widget, &allocation);
892
893   attributes.x = - gtk_adjustment_get_value (priv->hadjustment),
894   attributes.y = - gtk_adjustment_get_value (priv->vadjustment);
895   attributes.width = MAX (priv->width, allocation.width);
896   attributes.height = MAX (priv->height, allocation.height);
897   attributes.event_mask = GDK_EXPOSURE_MASK | GDK_SCROLL_MASK | 
898                           gtk_widget_get_events (widget);
899
900   priv->bin_window = gdk_window_new (window,
901                                      &attributes, attributes_mask);
902   gdk_window_set_user_data (priv->bin_window, widget);
903   gtk_style_context_set_background (gtk_widget_get_style_context (widget), priv->bin_window);
904
905   tmp_list = priv->children;
906   while (tmp_list)
907     {
908       GtkLayoutChild *child = tmp_list->data;
909       tmp_list = tmp_list->next;
910
911       gtk_widget_set_parent_window (child->widget, priv->bin_window);
912     }
913 }
914
915 static void
916 gtk_layout_style_updated (GtkWidget *widget)
917 {
918   GtkLayoutPrivate *priv;
919
920   GTK_WIDGET_CLASS (gtk_layout_parent_class)->style_updated (widget);
921
922   if (gtk_widget_get_realized (widget))
923     {
924       priv = GTK_LAYOUT (widget)->priv;
925       gtk_style_context_set_background (gtk_widget_get_style_context (widget), priv->bin_window);
926     }
927 }
928
929 static void
930 gtk_layout_map (GtkWidget *widget)
931 {
932   GtkLayout *layout = GTK_LAYOUT (widget);
933   GtkLayoutPrivate *priv = layout->priv;
934   GList *tmp_list;
935
936   gtk_widget_set_mapped (widget, TRUE);
937
938   tmp_list = priv->children;
939   while (tmp_list)
940     {
941       GtkLayoutChild *child = tmp_list->data;
942       tmp_list = tmp_list->next;
943
944       if (gtk_widget_get_visible (child->widget))
945         {
946           if (!gtk_widget_get_mapped (child->widget))
947             gtk_widget_map (child->widget);
948         }
949     }
950
951   gdk_window_show (priv->bin_window);
952   gdk_window_show (gtk_widget_get_window (widget));
953 }
954
955 static void 
956 gtk_layout_unrealize (GtkWidget *widget)
957 {
958   GtkLayout *layout = GTK_LAYOUT (widget);
959   GtkLayoutPrivate *priv = layout->priv;
960
961   gdk_window_set_user_data (priv->bin_window, NULL);
962   gdk_window_destroy (priv->bin_window);
963   priv->bin_window = NULL;
964
965   GTK_WIDGET_CLASS (gtk_layout_parent_class)->unrealize (widget);
966 }
967
968 static void
969 gtk_layout_get_preferred_width (GtkWidget *widget,
970                                 gint      *minimum,
971                                 gint      *natural)
972 {
973   *minimum = *natural = 0;
974 }
975
976 static void
977 gtk_layout_get_preferred_height (GtkWidget *widget,
978                                  gint      *minimum,
979                                  gint      *natural)
980 {
981   *minimum = *natural = 0;
982 }
983
984 static void
985 gtk_layout_size_allocate (GtkWidget     *widget,
986                           GtkAllocation *allocation)
987 {
988   GtkLayout *layout = GTK_LAYOUT (widget);
989   GtkLayoutPrivate *priv = layout->priv;
990   GList *tmp_list;
991
992   gtk_widget_set_allocation (widget, allocation);
993
994   tmp_list = priv->children;
995
996   while (tmp_list)
997     {
998       GtkLayoutChild *child = tmp_list->data;
999       tmp_list = tmp_list->next;
1000
1001       gtk_layout_allocate_child (layout, child);
1002     }
1003
1004   if (gtk_widget_get_realized (widget))
1005     {
1006       gdk_window_move_resize (gtk_widget_get_window (widget),
1007                               allocation->x, allocation->y,
1008                               allocation->width, allocation->height);
1009
1010       gdk_window_resize (priv->bin_window,
1011                          MAX (priv->width, allocation->width),
1012                          MAX (priv->height, allocation->height));
1013     }
1014
1015   gtk_layout_set_hadjustment_values (layout);
1016   gtk_layout_set_vadjustment_values (layout);
1017 }
1018
1019 static gboolean
1020 gtk_layout_draw (GtkWidget *widget,
1021                  cairo_t   *cr)
1022 {
1023   GtkLayout *layout = GTK_LAYOUT (widget);
1024   GtkLayoutPrivate *priv = layout->priv;
1025
1026   if (gtk_cairo_should_draw_window (cr, priv->bin_window))
1027     GTK_WIDGET_CLASS (gtk_layout_parent_class)->draw (widget, cr);
1028
1029   return FALSE;
1030 }
1031
1032 /* Container methods
1033  */
1034 static void
1035 gtk_layout_add (GtkContainer *container,
1036                 GtkWidget    *widget)
1037 {
1038   gtk_layout_put (GTK_LAYOUT (container), widget, 0, 0);
1039 }
1040
1041 static void
1042 gtk_layout_remove (GtkContainer *container, 
1043                    GtkWidget    *widget)
1044 {
1045   GtkLayout *layout = GTK_LAYOUT (container);
1046   GtkLayoutPrivate *priv = layout->priv;
1047   GList *tmp_list;
1048   GtkLayoutChild *child = NULL;
1049
1050   tmp_list = priv->children;
1051   while (tmp_list)
1052     {
1053       child = tmp_list->data;
1054       if (child->widget == widget)
1055         break;
1056       tmp_list = tmp_list->next;
1057     }
1058
1059   if (tmp_list)
1060     {
1061       gtk_widget_unparent (widget);
1062
1063       priv->children = g_list_remove_link (priv->children, tmp_list);
1064       g_list_free_1 (tmp_list);
1065       g_free (child);
1066     }
1067 }
1068
1069 static void
1070 gtk_layout_forall (GtkContainer *container,
1071                    gboolean      include_internals,
1072                    GtkCallback   callback,
1073                    gpointer      callback_data)
1074 {
1075   GtkLayout *layout = GTK_LAYOUT (container);
1076   GtkLayoutPrivate *priv = layout->priv;
1077   GtkLayoutChild *child;
1078   GList *tmp_list;
1079
1080   tmp_list = priv->children;
1081   while (tmp_list)
1082     {
1083       child = tmp_list->data;
1084       tmp_list = tmp_list->next;
1085
1086       (* callback) (child->widget, callback_data);
1087     }
1088 }
1089
1090 /* Operations on children
1091  */
1092
1093 static void
1094 gtk_layout_allocate_child (GtkLayout      *layout,
1095                            GtkLayoutChild *child)
1096 {
1097   GtkAllocation allocation;
1098   GtkRequisition requisition;
1099
1100   allocation.x = child->x;
1101   allocation.y = child->y;
1102
1103   gtk_widget_get_preferred_size (child->widget, &requisition, NULL);
1104   allocation.width = requisition.width;
1105   allocation.height = requisition.height;
1106   
1107   gtk_widget_size_allocate (child->widget, &allocation);
1108 }
1109
1110 /* Callbacks */
1111
1112 static void
1113 gtk_layout_adjustment_changed (GtkAdjustment *adjustment,
1114                                GtkLayout     *layout)
1115 {
1116   GtkLayoutPrivate *priv = layout->priv;
1117
1118   if (priv->freeze_count)
1119     return;
1120
1121   if (gtk_widget_get_realized (GTK_WIDGET (layout)))
1122     {
1123       gdk_window_move (priv->bin_window,
1124                        - gtk_adjustment_get_value (priv->hadjustment),
1125                        - gtk_adjustment_get_value (priv->vadjustment));
1126
1127       gdk_window_process_updates (priv->bin_window, TRUE);
1128     }
1129 }