]> Pileus Git - ~andy/gtk/blob - gtk/gtklayout.c
b5f74522f60b3efc0f34e5db21b8321e4616bdc0
[~andy/gtk] / gtk / gtklayout.c
1 /* GTK - The GTK+ 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   widget_class->set_scroll_adjustments_signal =
662     g_signal_new (I_("set_scroll_adjustments"),
663                   G_OBJECT_CLASS_TYPE (gobject_class),
664                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
665                   G_STRUCT_OFFSET (GtkLayoutClass, set_scroll_adjustments),
666                   NULL, NULL,
667                   _gtk_marshal_VOID__OBJECT_OBJECT,
668                   G_TYPE_NONE, 2,
669                   GTK_TYPE_ADJUSTMENT,
670                   GTK_TYPE_ADJUSTMENT);
671 }
672
673 static void
674 gtk_layout_get_property (GObject     *object,
675                          guint        prop_id,
676                          GValue      *value,
677                          GParamSpec  *pspec)
678 {
679   GtkLayout *layout = GTK_LAYOUT (object);
680   
681   switch (prop_id)
682     {
683     case PROP_HADJUSTMENT:
684       g_value_set_object (value, layout->hadjustment);
685       break;
686     case PROP_VADJUSTMENT:
687       g_value_set_object (value, layout->vadjustment);
688       break;
689     case PROP_WIDTH:
690       g_value_set_uint (value, layout->width);
691       break;
692     case PROP_HEIGHT:
693       g_value_set_uint (value, layout->height);
694       break;
695     default:
696       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
697       break;
698     }
699 }
700
701 static void
702 gtk_layout_set_property (GObject      *object,
703                          guint         prop_id,
704                          const GValue *value,
705                          GParamSpec   *pspec)
706 {
707   GtkLayout *layout = GTK_LAYOUT (object);
708   
709   switch (prop_id)
710     {
711     case PROP_HADJUSTMENT:
712       gtk_layout_set_hadjustment (layout, 
713                                   (GtkAdjustment*) g_value_get_object (value));
714       break;
715     case PROP_VADJUSTMENT:
716       gtk_layout_set_vadjustment (layout, 
717                                   (GtkAdjustment*) g_value_get_object (value));
718       break;
719     case PROP_WIDTH:
720       gtk_layout_set_size (layout, g_value_get_uint (value),
721                            layout->height);
722       break;
723     case PROP_HEIGHT:
724       gtk_layout_set_size (layout, layout->width,
725                            g_value_get_uint (value));
726       break;
727     default:
728       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
729       break;
730     }
731 }
732
733 static void
734 gtk_layout_set_child_property (GtkContainer    *container,
735                                GtkWidget       *child,
736                                guint            property_id,
737                                const GValue    *value,
738                                GParamSpec      *pspec)
739 {
740   switch (property_id)
741     {
742     case CHILD_PROP_X:
743       gtk_layout_move_internal (GTK_LAYOUT (container),
744                                 child,
745                                 TRUE, g_value_get_int (value),
746                                 FALSE, 0);
747       break;
748     case CHILD_PROP_Y:
749       gtk_layout_move_internal (GTK_LAYOUT (container),
750                                 child,
751                                 FALSE, 0,
752                                 TRUE, g_value_get_int (value));
753       break;
754     default:
755       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
756       break;
757     }
758 }
759
760 static void
761 gtk_layout_get_child_property (GtkContainer *container,
762                                GtkWidget    *child,
763                                guint         property_id,
764                                GValue       *value,
765                                GParamSpec   *pspec)
766 {
767   GtkLayoutChild *layout_child;
768
769   layout_child = get_child (GTK_LAYOUT (container), child);
770   
771   switch (property_id)
772     {
773     case CHILD_PROP_X:
774       g_value_set_int (value, layout_child->x);
775       break;
776     case CHILD_PROP_Y:
777       g_value_set_int (value, layout_child->y);
778       break;
779     default:
780       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
781       break;
782     }
783 }
784
785 static void
786 gtk_layout_init (GtkLayout *layout)
787 {
788   layout->children = NULL;
789
790   layout->width = 100;
791   layout->height = 100;
792
793   layout->hadjustment = NULL;
794   layout->vadjustment = NULL;
795
796   layout->bin_window = NULL;
797
798   layout->scroll_x = 0;
799   layout->scroll_y = 0;
800   layout->visibility = GDK_VISIBILITY_PARTIAL;
801
802   layout->freeze_count = 0;
803 }
804
805 static GObject *
806 gtk_layout_constructor (GType                  type,
807                         guint                  n_properties,
808                         GObjectConstructParam *properties)
809 {
810   GtkLayout *layout;
811   GObject *object;
812   GtkAdjustment *hadj, *vadj;
813   
814   object = G_OBJECT_CLASS (gtk_layout_parent_class)->constructor (type,
815                                                                   n_properties,
816                                                                   properties);
817
818   layout = GTK_LAYOUT (object);
819
820   hadj = layout->hadjustment ? layout->hadjustment : new_default_adjustment ();
821   vadj = layout->vadjustment ? layout->vadjustment : new_default_adjustment ();
822
823   if (!layout->hadjustment || !layout->vadjustment)
824     gtk_layout_set_adjustments (layout, hadj, vadj);
825
826   return object;
827 }
828
829 /* Widget methods
830  */
831
832 static void 
833 gtk_layout_realize (GtkWidget *widget)
834 {
835   GtkLayout *layout = GTK_LAYOUT (widget);
836   GList *tmp_list;
837   GdkWindowAttr attributes;
838   gint attributes_mask;
839
840   GTK_WIDGET_SET_FLAGS (layout, GTK_REALIZED);
841
842   attributes.window_type = GDK_WINDOW_CHILD;
843   attributes.x = widget->allocation.x;
844   attributes.y = widget->allocation.y;
845   attributes.width = widget->allocation.width;
846   attributes.height = widget->allocation.height;
847   attributes.wclass = GDK_INPUT_OUTPUT;
848   attributes.visual = gtk_widget_get_visual (widget);
849   attributes.colormap = gtk_widget_get_colormap (widget);
850   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
851
852   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
853
854   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
855                                    &attributes, attributes_mask);
856   gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
857   gdk_window_set_user_data (widget->window, widget);
858
859   attributes.x = - layout->hadjustment->value,
860   attributes.y = - layout->vadjustment->value;
861   attributes.width = MAX (layout->width, widget->allocation.width);
862   attributes.height = MAX (layout->height, widget->allocation.height);
863   attributes.event_mask = GDK_EXPOSURE_MASK | GDK_SCROLL_MASK | 
864                           gtk_widget_get_events (widget);
865
866   layout->bin_window = gdk_window_new (widget->window,
867                                         &attributes, attributes_mask);
868   gdk_window_set_user_data (layout->bin_window, widget);
869
870   widget->style = gtk_style_attach (widget->style, widget->window);
871   gtk_style_set_background (widget->style, layout->bin_window, GTK_STATE_NORMAL);
872
873   tmp_list = layout->children;
874   while (tmp_list)
875     {
876       GtkLayoutChild *child = tmp_list->data;
877       tmp_list = tmp_list->next;
878
879       gtk_widget_set_parent_window (child->widget, layout->bin_window);
880     }
881 }
882
883 static void
884 gtk_layout_style_set (GtkWidget *widget, GtkStyle *old_style)
885 {
886   if (GTK_WIDGET_CLASS (gtk_layout_parent_class)->style_set)
887     (* GTK_WIDGET_CLASS (gtk_layout_parent_class)->style_set) (widget, old_style);
888
889   if (GTK_WIDGET_REALIZED (widget))
890     {
891       gtk_style_set_background (widget->style, GTK_LAYOUT (widget)->bin_window, GTK_STATE_NORMAL);
892     }
893 }
894
895 static void
896 gtk_layout_map (GtkWidget *widget)
897 {
898   GtkLayout *layout = GTK_LAYOUT (widget);
899   GList *tmp_list;
900
901   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
902
903   tmp_list = layout->children;
904   while (tmp_list)
905     {
906       GtkLayoutChild *child = tmp_list->data;
907       tmp_list = tmp_list->next;
908
909       if (GTK_WIDGET_VISIBLE (child->widget))
910         {
911           if (!GTK_WIDGET_MAPPED (child->widget))
912             gtk_widget_map (child->widget);
913         }
914     }
915
916   gdk_window_show (layout->bin_window);
917   gdk_window_show (widget->window);
918 }
919
920 static void 
921 gtk_layout_unrealize (GtkWidget *widget)
922 {
923   GtkLayout *layout = GTK_LAYOUT (widget);
924
925   gdk_window_set_user_data (layout->bin_window, NULL);
926   gdk_window_destroy (layout->bin_window);
927   layout->bin_window = NULL;
928
929   if (GTK_WIDGET_CLASS (gtk_layout_parent_class)->unrealize)
930     (* GTK_WIDGET_CLASS (gtk_layout_parent_class)->unrealize) (widget);
931 }
932
933 static void     
934 gtk_layout_size_request (GtkWidget     *widget,
935                          GtkRequisition *requisition)
936 {
937   GtkLayout *layout = GTK_LAYOUT (widget);
938   GList *tmp_list;
939
940   requisition->width = 0;
941   requisition->height = 0;
942
943   tmp_list = layout->children;
944
945   while (tmp_list)
946     {
947       GtkLayoutChild *child = tmp_list->data;
948       GtkRequisition child_requisition;
949       
950       tmp_list = tmp_list->next;
951
952       gtk_widget_size_request (child->widget, &child_requisition);
953     }
954 }
955
956 static void     
957 gtk_layout_size_allocate (GtkWidget     *widget,
958                           GtkAllocation *allocation)
959 {
960   GtkLayout *layout = GTK_LAYOUT (widget);
961   GList *tmp_list;
962
963   widget->allocation = *allocation;
964
965   tmp_list = layout->children;
966
967   while (tmp_list)
968     {
969       GtkLayoutChild *child = tmp_list->data;
970       tmp_list = tmp_list->next;
971
972       gtk_layout_allocate_child (layout, child);
973     }
974
975   if (GTK_WIDGET_REALIZED (widget))
976     {
977       gdk_window_move_resize (widget->window,
978                               allocation->x, allocation->y,
979                               allocation->width, allocation->height);
980
981       gdk_window_resize (layout->bin_window,
982                          MAX (layout->width, allocation->width),
983                          MAX (layout->height, allocation->height));
984     }
985
986   layout->hadjustment->page_size = allocation->width;
987   layout->hadjustment->page_increment = allocation->width * 0.9;
988   layout->hadjustment->lower = 0;
989   /* set_adjustment_upper() emits ::changed */
990   gtk_layout_set_adjustment_upper (layout->hadjustment, MAX (allocation->width, layout->width), TRUE);
991
992   layout->vadjustment->page_size = allocation->height;
993   layout->vadjustment->page_increment = allocation->height * 0.9;
994   layout->vadjustment->lower = 0;
995   layout->vadjustment->upper = MAX (allocation->height, layout->height);
996   gtk_layout_set_adjustment_upper (layout->vadjustment, MAX (allocation->height, layout->height), TRUE);
997 }
998
999 static gint 
1000 gtk_layout_expose (GtkWidget      *widget,
1001                    GdkEventExpose *event)
1002 {
1003   GtkLayout *layout = GTK_LAYOUT (widget);
1004
1005   if (event->window != layout->bin_window)
1006     return FALSE;
1007   
1008   (* GTK_WIDGET_CLASS (gtk_layout_parent_class)->expose_event) (widget, event);
1009
1010   return FALSE;
1011 }
1012
1013 /* Container methods
1014  */
1015 static void
1016 gtk_layout_add (GtkContainer *container,
1017                 GtkWidget    *widget)
1018 {
1019   gtk_layout_put (GTK_LAYOUT (container), widget, 0, 0);
1020 }
1021
1022 static void
1023 gtk_layout_remove (GtkContainer *container, 
1024                    GtkWidget    *widget)
1025 {
1026   GtkLayout *layout = GTK_LAYOUT (container);
1027   GList *tmp_list;
1028   GtkLayoutChild *child = NULL;
1029
1030   tmp_list = layout->children;
1031   while (tmp_list)
1032     {
1033       child = tmp_list->data;
1034       if (child->widget == widget)
1035         break;
1036       tmp_list = tmp_list->next;
1037     }
1038
1039   if (tmp_list)
1040     {
1041       gtk_widget_unparent (widget);
1042
1043       layout->children = g_list_remove_link (layout->children, tmp_list);
1044       g_list_free_1 (tmp_list);
1045       g_free (child);
1046     }
1047 }
1048
1049 static void
1050 gtk_layout_forall (GtkContainer *container,
1051                    gboolean      include_internals,
1052                    GtkCallback   callback,
1053                    gpointer      callback_data)
1054 {
1055   GtkLayout *layout = GTK_LAYOUT (container);
1056   GtkLayoutChild *child;
1057   GList *tmp_list;
1058
1059   tmp_list = layout->children;
1060   while (tmp_list)
1061     {
1062       child = tmp_list->data;
1063       tmp_list = tmp_list->next;
1064
1065       (* callback) (child->widget, callback_data);
1066     }
1067 }
1068
1069 /* Operations on children
1070  */
1071
1072 static void
1073 gtk_layout_allocate_child (GtkLayout      *layout,
1074                            GtkLayoutChild *child)
1075 {
1076   GtkAllocation allocation;
1077   GtkRequisition requisition;
1078
1079   allocation.x = child->x;
1080   allocation.y = child->y;
1081   gtk_widget_get_child_requisition (child->widget, &requisition);
1082   allocation.width = requisition.width;
1083   allocation.height = requisition.height;
1084   
1085   gtk_widget_size_allocate (child->widget, &allocation);
1086 }
1087
1088 /* Callbacks */
1089
1090 static void
1091 gtk_layout_adjustment_changed (GtkAdjustment *adjustment,
1092                                GtkLayout     *layout)
1093 {
1094   if (layout->freeze_count)
1095     return;
1096
1097   if (GTK_WIDGET_REALIZED (layout))
1098     {
1099       gdk_window_move (layout->bin_window,
1100                        - layout->hadjustment->value,
1101                        - layout->vadjustment->value);
1102       
1103       gdk_window_process_updates (layout->bin_window, TRUE);
1104     }
1105 }
1106
1107 #define __GTK_LAYOUT_C__
1108 #include "gtkaliasdef.c"