widget: Allow adding event controllers in ui files
authorBenjamin Otte <otte@redhat.com>
Thu, 8 Mar 2018 02:13:57 +0000 (03:13 +0100)
committerCarlos Garnacho <carlosg@gnome.org>
Thu, 26 Apr 2018 15:59:41 +0000 (17:59 +0200)
16 files changed:
gtk/gtkactionbar.c
gtk/gtkcellview.c
gtk/gtkcenterbox.c
gtk/gtkcombobox.c
gtk/gtkcontainer.c
gtk/gtkdialog.c
gtk/gtkexpander.c
gtk/gtkframe.c
gtk/gtkheaderbar.c
gtk/gtkiconview.c
gtk/gtklistbox.c
gtk/gtknotebook.c
gtk/gtkoverlay.c
gtk/gtktreeview.c
gtk/gtkwidget.c
gtk/gtkwindow.c

index d0f64669a68ae4261c97540c7ceb2aad3bd63ddf..e5e5ba79201310392f5f5d89bc4a8b58b4026084 100644 (file)
@@ -428,6 +428,8 @@ gtk_action_bar_init (GtkActionBar *action_bar)
   gtk_container_add (GTK_CONTAINER (priv->revealer), priv->center_box);
 }
 
+static GtkBuildableIface *parent_buildable_iface;
+
 static void
 gtk_action_bar_buildable_add_child (GtkBuildable *buildable,
                                     GtkBuilder   *builder,
@@ -438,14 +440,10 @@ gtk_action_bar_buildable_add_child (GtkBuildable *buildable,
 
   if (type && strcmp (type, "center") == 0)
     gtk_action_bar_set_center_widget (action_bar, GTK_WIDGET (child));
-  else if (!type)
-    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
   else
-    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (action_bar, type);
+    parent_buildable_iface->add_child (buildable, builder, child, type);
 }
 
-static GtkBuildableIface *parent_buildable_iface;
-
 static void
 gtk_action_bar_buildable_interface_init (GtkBuildableIface *iface)
 {
index 0a0a4485a49dbc58a9b56eb4d818205014b13e6d..ae52674ac321bc5fc92e2e19dc0a94a6cdabfc99 100644 (file)
@@ -268,11 +268,23 @@ gtk_cell_view_class_init (GtkCellViewClass *klass)
   gtk_widget_class_set_css_name (widget_class, I_("cellview"));
 }
 
+static void
+gtk_cell_view_buildable_add_child (GtkBuildable *buildable,
+                                   GtkBuilder   *builder,
+                                   GObject      *child,
+                                   const gchar  *type)
+{
+  if (GTK_IS_CELL_RENDERER (child))
+    _gtk_cell_layout_buildable_add_child (buildable, builder, child, type);
+  else
+    parent_buildable_iface->add_child (buildable, builder, child, type);
+}
+
 static void
 gtk_cell_view_buildable_init (GtkBuildableIface *iface)
 {
   parent_buildable_iface = g_type_interface_peek_parent (iface);
-  iface->add_child = _gtk_cell_layout_buildable_add_child;
+  iface->add_child = gtk_cell_view_buildable_add_child;
   iface->custom_tag_start = gtk_cell_view_buildable_custom_tag_start;
   iface->custom_tag_end = gtk_cell_view_buildable_custom_tag_end;
 }
index 83d378f9a0b9ef4053b666226e4a4f9a6614f5d1..2d3354c0d7e515d9572a88c14d700f8ce1c6e643 100644 (file)
@@ -88,6 +88,8 @@ enum {
   PROP_ORIENTATION
 };
 
+static GtkBuildableIface *parent_buildable_iface;
+
 static void gtk_center_box_buildable_init (GtkBuildableIface *iface);
 
 G_DEFINE_TYPE_WITH_CODE (GtkCenterBox, gtk_center_box, GTK_TYPE_WIDGET,
@@ -107,12 +109,14 @@ gtk_center_box_buildable_add_child (GtkBuildable  *buildable,
   else if (g_strcmp0 (type, "end") == 0)
     gtk_center_box_set_end_widget (GTK_CENTER_BOX (buildable), GTK_WIDGET (child));
   else
-    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_CENTER_BOX (buildable), type);
+    parent_buildable_iface->add_child (buildable, builder, child, type);
 }
 
 static void
 gtk_center_box_buildable_init (GtkBuildableIface *iface)
 {
+  parent_buildable_iface = g_type_interface_peek_parent (iface);
+
   iface->add_child = gtk_center_box_buildable_add_child;
 }
 
index 25e325dc368d08fb9edf33e3531882da12a99a41..60190425ed42a6b70e6250df452ddae80694eb45 100644 (file)
@@ -3053,13 +3053,10 @@ gtk_combo_box_buildable_add_child (GtkBuildable *buildable,
                                    GObject      *child,
                                    const gchar  *type)
 {
-  if (GTK_IS_WIDGET (child))
-    {
-      parent_buildable_iface->add_child (buildable, builder, child, type);
-      return;
-    }
-
-  _gtk_cell_layout_buildable_add_child (buildable, builder, child, type);
+  if (GTK_IS_CELL_RENDERER (child))
+    _gtk_cell_layout_buildable_add_child (buildable, builder, child, type);
+  else
+    parent_buildable_iface->add_child (buildable, builder, child, type);
 }
 
 static gboolean
index 888d9d59d27c0b7bbde0ac01c808bbffb06fcd51..731bdb1da031a64dd4c83ac9d1b1612efcfdfc44 100644 (file)
@@ -392,18 +392,22 @@ gtk_container_buildable_add_child (GtkBuildable  *buildable,
                                    GObject       *child,
                                    const gchar   *type)
 {
-  if (type)
+  if (GTK_IS_WIDGET (child) &&
+      _gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
     {
-      GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
+      if (type)
+        {
+          GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
+        }
+      else
+        {
+          gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
+        }
     }
-  else if (GTK_IS_WIDGET (child) &&
-           _gtk_widget_get_parent (GTK_WIDGET (child)) == NULL)
+  else
     {
-      gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
+      parent_buildable_iface->add_child (buildable, builder, child, type);
     }
-  else
-    g_warning ("Cannot add an object of type %s to a container of type %s",
-               g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
 }
 
 static inline void
index 103ab7cf9eb732ee20a5f11a1360057309689cc1..3c7acf5895b81d6cd9da9827aaaa681312b134ca 100644 (file)
@@ -1560,7 +1560,7 @@ gtk_dialog_buildable_add_child (GtkBuildable  *buildable,
   GtkDialogPrivate *priv = dialog->priv;
 
   if (type == NULL)
-    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
+    parent_buildable_iface->add_child (buildable, builder, child, type);
   else if (g_str_equal (type, "titlebar"))
     {
       priv->headerbar = GTK_WIDGET (child);
index db794ee3e62ac091fdc75e614b035bc8359f8c31..0777fc39a62fe042135daa20d816283cbb36013d 100644 (file)
@@ -378,23 +378,25 @@ gtk_expander_init (GtkExpander *expander)
                                               GTK_PHASE_BUBBLE);
 }
 
+static GtkBuildableIface *parent_buildable_iface;
+
 static void
 gtk_expander_buildable_add_child (GtkBuildable  *buildable,
                                   GtkBuilder    *builder,
                                   GObject       *child,
                                   const gchar   *type)
 {
-  if (!type)
-    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
-  else if (strcmp (type, "label") == 0)
+  if (g_strcmp0 (type, "label") == 0)
     gtk_expander_set_label_widget (GTK_EXPANDER (buildable), GTK_WIDGET (child));
   else
-    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_EXPANDER (buildable), type);
+    parent_buildable_iface->add_child (buildable, builder, child, type);
 }
 
 static void
 gtk_expander_buildable_init (GtkBuildableIface *iface)
 {
+  parent_buildable_iface = g_type_interface_peek_parent (iface);
+
   iface->add_child = gtk_expander_buildable_add_child;
 }
 
index 38dea2211dfe308f79a889bf8486e507ab72472f..62a8428dc65dbcb9e2dfde7992cb0047bcfa354e 100644 (file)
@@ -206,9 +206,13 @@ gtk_frame_class_init (GtkFrameClass *class)
   gtk_widget_class_set_css_name (widget_class, I_("frame"));
 }
 
+static GtkBuildableIface *parent_buildable_iface;
+
 static void
 gtk_frame_buildable_init (GtkBuildableIface *iface)
 {
+  parent_buildable_iface = g_type_interface_peek_parent (iface);
+
   iface->add_child = gtk_frame_buildable_add_child;
 }
 
@@ -220,10 +224,8 @@ gtk_frame_buildable_add_child (GtkBuildable *buildable,
 {
   if (type && strcmp (type, "label") == 0)
     gtk_frame_set_label_widget (GTK_FRAME (buildable), GTK_WIDGET (child));
-  else if (!type)
-    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
   else
-    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_FRAME (buildable), type);
+    parent_buildable_iface->add_child (buildable, builder, child, type);
 }
 
 static void
index c5e3d5c8d84f2fd90cd9d2efe5c144fb6d04d093..6c42ebb845d95f171751fa726068756a32ec0fb8 100644 (file)
@@ -1988,6 +1988,8 @@ gtk_header_bar_init (GtkHeaderBar *bar)
   construct_label_box (bar);
 }
 
+static GtkBuildableIface *parent_buildable_iface;
+
 static void
 gtk_header_bar_buildable_add_child (GtkBuildable *buildable,
                                     GtkBuilder   *builder,
@@ -1996,15 +1998,15 @@ gtk_header_bar_buildable_add_child (GtkBuildable *buildable,
 {
   if (type && strcmp (type, "title") == 0)
     gtk_header_bar_set_custom_title (GTK_HEADER_BAR (buildable), GTK_WIDGET (child));
-  else if (!type)
-    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
   else
-    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_HEADER_BAR (buildable), type);
+    parent_buildable_iface->add_child (buildable, builder, child, type);
 }
 
 static void
 gtk_header_bar_buildable_init (GtkBuildableIface *iface)
 {
+  parent_buildable_iface = g_type_interface_peek_parent (iface);
+
   iface->add_child = gtk_header_bar_buildable_add_child;
 }
 
index ff7ca21cd6193d1c80d84d33d3880b08ebeff167..a1718c17906b32a2d9b1dc22c07bfb4636b9a5b3 100644 (file)
@@ -888,11 +888,23 @@ gtk_icon_view_class_init (GtkIconViewClass *klass)
   gtk_widget_class_set_css_name (widget_class, I_("iconview"));
 }
 
+static void
+gtk_icon_view_buildable_add_child (GtkBuildable *buildable,
+                                   GtkBuilder   *builder,
+                                   GObject      *child,
+                                   const gchar  *type)
+{
+  if (GTK_IS_CELL_RENDERER (child))
+    _gtk_cell_layout_buildable_add_child (buildable, builder, child, type);
+  else
+    parent_buildable_iface->add_child (buildable, builder, child, type);
+}
+
 static void
 gtk_icon_view_buildable_init (GtkBuildableIface *iface)
 {
   parent_buildable_iface = g_type_interface_peek_parent (iface);
-  iface->add_child = _gtk_cell_layout_buildable_add_child;
+  iface->add_child = gtk_icon_view_buildable_add_child;
   iface->custom_tag_start = gtk_icon_view_buildable_custom_tag_start;
   iface->custom_tag_end = gtk_icon_view_buildable_custom_tag_end;
 }
index 311661b8e2d3d1eb1ac8e7b97585e231077edcd5..c258836185b73aee51f4c569d3aab53e6bd83c67 100644 (file)
@@ -180,6 +180,8 @@ enum {
 #define BOX_PRIV(box) ((GtkListBoxPrivate*)gtk_list_box_get_instance_private ((GtkListBox*)(box)))
 #define ROW_PRIV(row) ((GtkListBoxRowPrivate*)gtk_list_box_row_get_instance_private ((GtkListBoxRow*)(row)))
 
+static GtkBuildableIface *parent_buildable_iface;
+
 static void     gtk_list_box_buildable_interface_init   (GtkBuildableIface *iface);
 
 static void     gtk_list_box_row_actionable_iface_init  (GtkActionableInterface *iface);
@@ -3474,15 +3476,15 @@ gtk_list_box_buildable_add_child (GtkBuildable *buildable,
 {
   if (type && strcmp (type, "placeholder") == 0)
     gtk_list_box_set_placeholder (GTK_LIST_BOX (buildable), GTK_WIDGET (child));
-  else if (!type)
-    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
   else
-    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
+    parent_buildable_iface->add_child (buildable, builder, child, type);
 }
 
 static void
 gtk_list_box_buildable_interface_init (GtkBuildableIface *iface)
 {
+  parent_buildable_iface = g_type_interface_peek_parent (iface);
+
   iface->add_child = gtk_list_box_buildable_add_child;
 }
 
index 57b554adb356756d7280b39480735c36d0d6abe9..80abd55d56862555bd3930e5e451e4f856c3b4f3 100644 (file)
@@ -1124,9 +1124,13 @@ gtk_notebook_init (GtkNotebook *notebook)
                                GTK_STYLE_CLASS_FRAME);
 }
 
+static GtkBuildableIface *parent_buildable_iface;
+
 static void
 gtk_notebook_buildable_init (GtkBuildableIface *iface)
 {
+  parent_buildable_iface = g_type_interface_peek_parent (iface);
+
   iface->add_child = gtk_notebook_buildable_add_child;
 }
 
@@ -1138,31 +1142,38 @@ gtk_notebook_buildable_add_child (GtkBuildable  *buildable,
 {
   GtkNotebook *notebook = GTK_NOTEBOOK (buildable);
 
-  if (type && strcmp (type, "tab") == 0)
+  if (GTK_IS_WIDGET (child))
     {
-      GtkWidget * page;
-
-      page = gtk_notebook_get_nth_page (notebook, -1);
-      /* To set the tab label widget, we must have already a child
-       * inside the tab container. */
-      g_assert (page != NULL);
-      /* warn when Glade tries to overwrite label */
-      if (gtk_notebook_get_tab_label (notebook, page))
-        g_warning ("Overriding tab label for notebook");
-      gtk_notebook_set_tab_label (notebook, page, GTK_WIDGET (child));
-    }
-  else if (type && strcmp (type, "action-start") == 0)
-    {
-      gtk_notebook_set_action_widget (notebook, GTK_WIDGET (child), GTK_PACK_START);
+      if (type && strcmp (type, "tab") == 0)
+        {
+          GtkWidget * page;
+
+          page = gtk_notebook_get_nth_page (notebook, -1);
+          /* To set the tab label widget, we must have already a child
+           * inside the tab container. */
+          g_assert (page != NULL);
+          /* warn when Glade tries to overwrite label */
+          if (gtk_notebook_get_tab_label (notebook, page))
+            g_warning ("Overriding tab label for notebook");
+          gtk_notebook_set_tab_label (notebook, page, GTK_WIDGET (child));
+        }
+      else if (type && strcmp (type, "action-start") == 0)
+        {
+          gtk_notebook_set_action_widget (notebook, GTK_WIDGET (child), GTK_PACK_START);
+        }
+      else if (type && strcmp (type, "action-end") == 0)
+        {
+          gtk_notebook_set_action_widget (notebook, GTK_WIDGET (child), GTK_PACK_END);
+        }
+      else if (!type)
+        gtk_notebook_append_page (notebook, GTK_WIDGET (child), NULL);
+      else
+        GTK_BUILDER_WARN_INVALID_CHILD_TYPE (notebook, type);
     }
-  else if (type && strcmp (type, "action-end") == 0)
+  else
     {
-      gtk_notebook_set_action_widget (notebook, GTK_WIDGET (child), GTK_PACK_END);
+      parent_buildable_iface->add_child (buildable, builder, child, type);
     }
-  else if (!type)
-    gtk_notebook_append_page (notebook, GTK_WIDGET (child), NULL);
-  else
-    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (notebook, type);
 }
 
 static gboolean
index 785865730d0cc9bd18a2df8d1fb867b96a48ab40..0b2b44e3f08a5fd04bd8c01a69fcc5ee8b1bb7c0 100644 (file)
@@ -838,27 +838,38 @@ gtk_overlay_init (GtkOverlay *overlay)
   gtk_widget_set_has_surface (GTK_WIDGET (overlay), FALSE);
 }
 
+static GtkBuildableIface *parent_buildable_iface;
+
 static void
 gtk_overlay_buildable_add_child (GtkBuildable *buildable,
                                  GtkBuilder   *builder,
                                  GObject      *child,
                                  const gchar  *type)
 {
-  if (type && strcmp (type, "overlay") == 0)
-    gtk_overlay_add_overlay (GTK_OVERLAY (buildable), GTK_WIDGET (child));
-  else if (!type)
+  if (GTK_IS_WIDGET (child))
     {
-      /* Make sure the main-child node is the first one */
-      gtk_widget_insert_after (GTK_WIDGET (child), GTK_WIDGET (buildable), NULL);
-      _gtk_bin_set_child (GTK_BIN (buildable), GTK_WIDGET (child));
+      if (type && strcmp (type, "overlay") == 0)
+        gtk_overlay_add_overlay (GTK_OVERLAY (buildable), GTK_WIDGET (child));
+      else if (!type)
+        {
+          /* Make sure the main-child node is the first one */
+          gtk_widget_insert_after (GTK_WIDGET (child), GTK_WIDGET (buildable), NULL);
+          _gtk_bin_set_child (GTK_BIN (buildable), GTK_WIDGET (child));
+        }
+      else
+        GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
     }
   else
-    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
+    {
+      parent_buildable_iface->add_child (buildable, builder, child, type);
+    }
 }
 
 static void
 gtk_overlay_buildable_init (GtkBuildableIface *iface)
 {
+  parent_buildable_iface = g_type_interface_peek_parent (iface);
+
   iface->add_child = gtk_overlay_buildable_add_child;
 }
 
index a00c0bd4dd821382a3fe19431111323dd78a282c..3b0795b1cd4a2ccd1b8804e99e747c113794b519 100644 (file)
@@ -1971,7 +1971,10 @@ gtk_tree_view_buildable_add_child (GtkBuildable *tree_view,
                                   GObject     *child,
                                   const gchar *type)
 {
-  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), GTK_TREE_VIEW_COLUMN (child));
+  if (GTK_IS_TREE_VIEW_COLUMN (child))
+    gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), GTK_TREE_VIEW_COLUMN (child));
+  else
+    parent_buildable_iface->add_child (tree_view, builder, child, type);
 }
 
 static GObject *
index b4361c65f7ba300adf899c45cb06bc510a1fbbb9..50286e39a2de75436c81efe44f5bcac6b16c993f 100644 (file)
@@ -9947,7 +9947,23 @@ gtk_widget_buildable_add_child (GtkBuildable  *buildable,
                                 GObject       *child,
                                 const gchar   *type)
 {
-  gtk_widget_set_parent (GTK_WIDGET (child), GTK_WIDGET (buildable));
+  if (type != NULL)
+    {
+      GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
+    }
+  if (GTK_IS_WIDGET (child))
+    {
+      gtk_widget_set_parent (GTK_WIDGET (child), GTK_WIDGET (buildable));
+    }
+  else if (GTK_IS_EVENT_CONTROLLER (child))
+    {
+      gtk_widget_add_controller (GTK_WIDGET (buildable), g_object_ref (GTK_EVENT_CONTROLLER (child)));
+    }
+  else
+    {
+      g_warning ("Cannot add an object of type %s to a widget of type %s",
+                 g_type_name (G_OBJECT_TYPE (child)), g_type_name (G_OBJECT_TYPE (buildable)));
+    }
 }
 
 static void
index 72ec97cac6aa612c46c8fabbbcb6fafa32d8f96d..0b090e323c2d16ca1b62f18c86d33286268e6e2f 100644 (file)
@@ -2229,10 +2229,8 @@ gtk_window_buildable_add_child (GtkBuildable *buildable,
 {
   if (type && strcmp (type, "titlebar") == 0)
     gtk_window_set_titlebar (GTK_WINDOW (buildable), GTK_WIDGET (child));
-  else if (!type)
-    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
   else
-    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);
+    parent_buildable_iface->add_child (buildable, builder, child, type);
 }
 
 static void