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