[gtk/bin-removal: 16/50] button: Add a child property



commit 65b3a5bf89aa73d8bf9eb0fcbe203ba1bf76d6d9
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat May 2 02:45:09 2020 -0400

    button: Add a child property

 docs/reference/gtk/gtk4-sections.txt |  2 ++
 gtk/gtkbutton.c                      | 60 ++++++++++++++++++++++++++++++++----
 gtk/gtkbutton.h                      |  5 +++
 3 files changed, 61 insertions(+), 6 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index f72dc55e04..ec513092d7 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -539,6 +539,8 @@ gtk_button_get_use_underline
 gtk_button_set_use_underline
 gtk_button_set_icon_name
 gtk_button_get_icon_name
+gtk_button_set_child
+gtk_button_get_child
 
 <SUBSECTION Standard>
 GTK_BUTTON
diff --git a/gtk/gtkbutton.c b/gtk/gtkbutton.c
index 947e440a63..b161c18059 100644
--- a/gtk/gtkbutton.c
+++ b/gtk/gtkbutton.c
@@ -104,6 +104,7 @@ enum {
   PROP_HAS_FRAME,
   PROP_USE_UNDERLINE,
   PROP_ICON_NAME,
+  PROP_CHILD,
 
   /* actionable properties */
   PROP_ACTION_NAME,
@@ -241,6 +242,12 @@ gtk_button_class_init (GtkButtonClass *klass)
                          NULL,
                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
 
+  props[PROP_CHILD] =
+    g_param_spec_object ("child",
+                         P_("Child"),
+                         P_("The child widget"),
+                         GTK_TYPE_WIDGET,
+                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
 
   g_object_class_install_properties (gobject_class, LAST_PROP, props);
 
@@ -491,6 +498,9 @@ gtk_button_set_property (GObject         *object,
     case PROP_ICON_NAME:
       gtk_button_set_icon_name (button, g_value_get_string (value));
       break;
+    case PROP_CHILD:
+      gtk_button_set_child (button, g_value_get_object (value));
+      break;
     case PROP_ACTION_NAME:
       gtk_button_set_action_name (GTK_ACTIONABLE (button), g_value_get_string (value));
       break;
@@ -526,6 +536,9 @@ gtk_button_get_property (GObject         *object,
     case PROP_ICON_NAME:
       g_value_set_string (value, gtk_button_get_icon_name (button));
       break;
+    case PROP_CHILD:
+      g_value_set_object (value, gtk_button_get_child (button));
+      break;
     case PROP_ACTION_NAME:
       g_value_set_string (value, gtk_action_helper_get_action_name (priv->action_helper));
       break;
@@ -779,7 +792,7 @@ gtk_button_set_label (GtkButton   *button,
   if (priv->child_type != LABEL_CHILD || child == NULL)
     {
       if (child != NULL)
-        gtk_container_remove (GTK_CONTAINER (button), child);
+        gtk_button_remove (GTK_CONTAINER (button), child);
 
       child = gtk_label_new (NULL);
       if (priv->use_underline)
@@ -791,7 +804,7 @@ gtk_button_set_label (GtkButton   *button,
         {
           gtk_label_set_xalign (GTK_LABEL (child), 0.0);
         }
-      gtk_container_add (GTK_CONTAINER (button), child);
+      gtk_button_add (GTK_CONTAINER (button), child);
       gtk_widget_remove_css_class (GTK_WIDGET (button), "image-button");
       gtk_widget_add_css_class (GTK_WIDGET (button), "text-button");
     }
@@ -933,11 +946,8 @@ gtk_button_set_icon_name (GtkButton  *button,
 
   if (priv->child_type != ICON_CHILD || child == NULL)
     {
-      if (child != NULL)
-        gtk_container_remove (GTK_CONTAINER (button), child);
-
       child = gtk_image_new_from_icon_name (icon_name);
-      gtk_container_add (GTK_CONTAINER (button), child);
+      gtk_button_set_child (GTK_BUTTON (button), child);
       gtk_widget_remove_css_class (GTK_WIDGET (button), "text-button");
       gtk_widget_add_css_class (GTK_WIDGET (button), "image-button");
     }
@@ -982,3 +992,41 @@ gtk_button_get_gesture (GtkButton *button)
 
   return priv->gesture;
 }
+
+/**
+ * gtk_button_set_child:
+ * @button: a #GtkButton
+ * @child: (allow-none): the child widget
+ *
+ * Sets the child widget of @button.
+ */
+void
+gtk_button_set_child (GtkButton *button,
+                      GtkWidget *child)
+{
+  g_return_if_fail (GTK_IS_BUTTON (button));
+  g_return_if_fail (child == NULL || GTK_IS_WIDGET (child));
+
+  if (gtk_bin_get_child (GTK_BIN (button)))
+    gtk_button_remove (GTK_CONTAINER (button),
+                       gtk_bin_get_child (GTK_BIN (button)));
+  gtk_button_add (GTK_CONTAINER (button), child);
+  g_object_notify (G_OBJECT (button), "child");
+}
+
+/**
+ * gtk_button_get_child:
+ * @button: a #GtkButton
+ *
+ * Gets the child widget of @button.
+ *
+ * Returns: (nullable) (transfer none): the child widget of @button
+ */
+GtkWidget *
+gtk_button_get_child (GtkButton *button)
+{
+  g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
+
+  return gtk_bin_get_child (GTK_BIN (button));
+}
+
diff --git a/gtk/gtkbutton.h b/gtk/gtkbutton.h
index 9a4b15c992..64440efb5f 100644
--- a/gtk/gtkbutton.h
+++ b/gtk/gtkbutton.h
@@ -107,6 +107,11 @@ void                  gtk_button_set_icon_name      (GtkButton      *button,
 GDK_AVAILABLE_IN_ALL
 const char *          gtk_button_get_icon_name      (GtkButton      *button);
 
+GDK_AVAILABLE_IN_ALL
+void                  gtk_button_set_child          (GtkButton      *button,
+                                                     GtkWidget      *child);
+GDK_AVAILABLE_IN_ALL
+GtkWidget *           gtk_button_get_child          (GtkButton      *button);
 
 G_END_DECLS
 


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