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