[gtk+] scrolledwindow: make gtk_scrolled_window_add() smart



commit 9efa28591cc49c33e8fb6a66a5e888c57b9b3b3f
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Fri Feb 1 17:03:44 2013 +0100

    scrolledwindow: make gtk_scrolled_window_add() smart
    
    There's really no reason why we shouldn't automatically create a
    GtkViewport when the widget added to GtkScrolledWindow is not a
    GtkScrollable, instead of just printing a g_warning.
    Copy the viewport special case into the scrolled window implementation
    of gtk_container_add().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=693015

 gtk/gtkscrolledwindow.c |   29 ++++++++++++++++++-----------
 gtk/gtkviewport.c       |    5 +++--
 2 files changed, 21 insertions(+), 13 deletions(-)
---
diff --git a/gtk/gtkscrolledwindow.c b/gtk/gtkscrolledwindow.c
index 230fab6..665c570 100644
--- a/gtk/gtkscrolledwindow.c
+++ b/gtk/gtkscrolledwindow.c
@@ -67,8 +67,8 @@
  * If a widget has native scrolling abilities, it can be added to the
  * #GtkScrolledWindow with gtk_container_add(). If a widget does not, you
  * must first add the widget to a #GtkViewport, then add the #GtkViewport
- * to the scrolled window. The convenience function
- * gtk_scrolled_window_add_with_viewport() does exactly this, so you can
+ * to the scrolled window. gtk_container_add() will do this for you for
+ * widgets that don't implement #GtkScrollable natively, so you can
  * ignore the presence of the viewport.
  *
  * The position of the scrollbars is controlled by the scroll
@@ -3032,7 +3032,7 @@ gtk_scrolled_window_add (GtkContainer *container,
   GtkScrolledWindowPrivate *priv;
   GtkScrolledWindow *scrolled_window;
   GtkBin *bin;
-  GtkWidget *child_widget;
+  GtkWidget *child_widget, *scrollable_child;
   GtkAdjustment *hadj, *vadj;
 
   bin = GTK_BIN (container);
@@ -3042,20 +3042,27 @@ gtk_scrolled_window_add (GtkContainer *container,
   scrolled_window = GTK_SCROLLED_WINDOW (container);
   priv = scrolled_window->priv;
 
+  if (GTK_IS_SCROLLABLE (child))
+    {
+      scrollable_child = child;
+    }
+  else
+    {
+      scrollable_child = gtk_viewport_new (NULL, NULL);
+      gtk_widget_show (scrollable_child);
+      gtk_container_add (GTK_CONTAINER (scrollable_child), child);
+    }
+
   if (gtk_widget_get_realized (GTK_WIDGET (bin)))
-    gtk_widget_set_parent_window (child, priv->overshoot_window);
+    gtk_widget_set_parent_window (scrollable_child, priv->overshoot_window);
 
-  _gtk_bin_set_child (bin, child);
-  gtk_widget_set_parent (child, GTK_WIDGET (bin));
+  _gtk_bin_set_child (bin, scrollable_child);
+  gtk_widget_set_parent (scrollable_child, GTK_WIDGET (bin));
 
   hadj = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
   vadj = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
 
-  if (GTK_IS_SCROLLABLE (child))
-    g_object_set (child, "hadjustment", hadj, "vadjustment", vadj, NULL);
-  else
-    g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
-               "use gtk_scrolled_window_add_with_viewport() instead");
+  g_object_set (scrollable_child, "hadjustment", hadj, "vadjustment", vadj, NULL);
 }
 
 static void
diff --git a/gtk/gtkviewport.c b/gtk/gtkviewport.c
index a80e811..e82d70b 100644
--- a/gtk/gtkviewport.c
+++ b/gtk/gtkviewport.c
@@ -49,8 +49,9 @@
  * #GtkTreeView or #GtkIconView, it can be added to a #GtkScrolledWindow
  * with gtk_container_add(). If a widget does not, you must first add the
  * widget to a #GtkViewport, then add the viewport to the scrolled window.
- * The convenience function gtk_scrolled_window_add_with_viewport() does
- * exactly this, so you can ignore the presence of the viewport.
+ * gtk_container_add() does this automatically if a child that does not
+ * implement #GtkScrollable is added to a #GtkScrolledWindow, so you can
+ * ignore the presence of the viewport.
  *
  * The #GtkViewport will start scrolling content only if allocated less
  * than the child widget's minimum size in a given orientation.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]