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