[gimp/wip/nielsdg/propwidgets-g-bind-property] propwidgets: Use g_bind_property()



commit f32ca3f87c286d9d73fa6258a03bbbdf4ed36280
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Fri May 15 16:43:35 2020 +0200

    propwidgets: Use g_bind_property()
    
    Make some property widgets implementations easier by just using
    `g_bind_property()`, which does all the property change handling for us.

 libgimpwidgets/gimppropwidgets.c | 193 ++++-----------------------------------
 1 file changed, 16 insertions(+), 177 deletions(-)
---
diff --git a/libgimpwidgets/gimppropwidgets.c b/libgimpwidgets/gimppropwidgets.c
index c5b38eb3d3..a7ca711cbd 100644
--- a/libgimpwidgets/gimppropwidgets.c
+++ b/libgimpwidgets/gimppropwidgets.c
@@ -79,12 +79,6 @@ static void         connect_notify     (GObject     *config,
 /*  check button  */
 /******************/
 
-static void   gimp_prop_check_button_callback (GtkWidget  *widget,
-                                               GObject    *config);
-static void   gimp_prop_check_button_notify   (GObject    *config,
-                                               GParamSpec *param_spec,
-                                               GtkWidget  *button);
-
 /**
  * gimp_prop_check_button_new:
  * @config:        Object to which property is attached.
@@ -107,7 +101,6 @@ gimp_prop_check_button_new (GObject     *config,
 {
   GParamSpec  *param_spec;
   GtkWidget   *button;
-  gboolean     value;
 
   g_return_val_if_fail (G_IS_OBJECT (config), NULL);
   g_return_val_if_fail (property_name != NULL, NULL);
@@ -120,71 +113,14 @@ gimp_prop_check_button_new (GObject     *config,
   if (! label)
     label = g_param_spec_get_nick (param_spec);
 
-  g_object_get (config,
-                property_name, &value,
-                NULL);
-
   button = gtk_check_button_new_with_mnemonic (label);
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), value);
-
-  set_param_spec (G_OBJECT (button), button, param_spec);
-
-  g_signal_connect (button, "toggled",
-                    G_CALLBACK (gimp_prop_check_button_callback),
-                    config);
-
-  connect_notify (config, property_name,
-                  G_CALLBACK (gimp_prop_check_button_notify),
-                  button);
-
   gtk_widget_show (button);
 
-  return button;
-}
-
-static void
-gimp_prop_check_button_callback (GtkWidget *widget,
-                                 GObject   *config)
-{
-  GParamSpec *param_spec;
-  gboolean    value;
-  gboolean    v;
-
-  param_spec = get_param_spec (G_OBJECT (widget));
-  if (! param_spec)
-    return;
-
-  value = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
-
-  g_object_get (config, param_spec->name, &v, NULL);
-
-  if (v != value)
-    g_object_set (config, param_spec->name, value, NULL);
-}
-
-static void
-gimp_prop_check_button_notify (GObject    *config,
-                               GParamSpec *param_spec,
-                               GtkWidget  *button)
-{
-  gboolean value;
-
-  g_object_get (config,
-                param_spec->name, &value,
-                NULL);
-
-  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)) != value)
-    {
-      g_signal_handlers_block_by_func (button,
-                                       gimp_prop_check_button_callback,
-                                       config);
+  g_object_bind_property (config, property_name,
+                          button, "active",
+                          G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
 
-      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), value);
-
-      g_signal_handlers_unblock_by_func (button,
-                                         gimp_prop_check_button_callback,
-                                         config);
-    }
+  return button;
 }
 
 
@@ -378,7 +314,6 @@ gimp_prop_int_combo_box_new (GObject      *config,
 {
   GParamSpec *param_spec;
   GtkWidget  *combo_box;
-  gint        value;
 
   g_return_val_if_fail (G_IS_OBJECT (config), NULL);
   g_return_val_if_fail (property_name != NULL, NULL);
@@ -388,27 +323,14 @@ gimp_prop_int_combo_box_new (GObject      *config,
   if (! param_spec)
     return NULL;
 
-  g_object_get (config,
-                property_name, &value,
-                NULL);
-
   combo_box = g_object_new (GIMP_TYPE_INT_COMBO_BOX,
                             "model", store,
+                            "visible", TRUE,
                             NULL);
 
-  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (combo_box), value);
-
-  g_signal_connect (combo_box, "changed",
-                    G_CALLBACK (gimp_prop_int_combo_box_callback),
-                    config);
-
-  set_param_spec (G_OBJECT (combo_box), combo_box, param_spec);
-
-  connect_notify (config, property_name,
-                  G_CALLBACK (gimp_prop_int_combo_box_notify),
-                  combo_box);
-
-  gtk_widget_show (combo_box);
+  g_object_bind_property (config, property_name,
+                          combo_box, "active",
+                          G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
 
   return combo_box;
 }
@@ -1960,10 +1882,6 @@ gimp_prop_memsize_notify (GObject          *config,
 /*  label  */
 /***********/
 
-static void   gimp_prop_label_notify (GObject    *config,
-                                      GParamSpec *param_spec,
-                                      GtkWidget  *label);
-
 /**
  * gimp_prop_label_new:
  * @config:        Object to which property is attached.
@@ -1989,68 +1907,18 @@ gimp_prop_label_new (GObject     *config,
   g_return_val_if_fail (property_name != NULL, NULL);
 
   param_spec = find_param_spec (config, property_name, G_STRFUNC);
-
   if (! param_spec)
     return NULL;
 
-  if (! g_value_type_transformable (param_spec->value_type, G_TYPE_STRING))
-    {
-      g_warning ("%s: property '%s' of %s is not transformable to string",
-                 G_STRLOC,
-                 param_spec->name,
-                 g_type_name (param_spec->owner_type));
-      return NULL;
-    }
-
   label = gtk_label_new (NULL);
   gtk_widget_set_halign (label, GTK_ALIGN_START);
-
-  set_param_spec (G_OBJECT (label), label, param_spec);
-
-  connect_notify (config, property_name,
-                  G_CALLBACK (gimp_prop_label_notify),
-                  label);
-
-  gimp_prop_label_notify (config, param_spec, label);
-
   gtk_widget_show (label);
 
-  return label;
-}
-
-static void
-gimp_prop_label_notify (GObject    *config,
-                        GParamSpec *param_spec,
-                        GtkWidget  *label)
-{
-  GValue  value = G_VALUE_INIT;
-
-  g_value_init (&value, param_spec->value_type);
-
-  g_object_get_property (config, param_spec->name, &value);
-
-  if (G_VALUE_HOLDS_STRING (&value))
-    {
-      const gchar *str = g_value_get_string (&value);
-
-      gtk_label_set_text (GTK_LABEL (label), str ? str : "");
-    }
-  else
-    {
-      GValue       str_value = G_VALUE_INIT;
-      const gchar *str;
-
-      g_value_init (&str_value, G_TYPE_STRING);
-      g_value_transform (&value, &str_value);
+  g_object_bind_property (config, property_name,
+                          label, "label",
+                          G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
 
-      str = g_value_get_string (&str_value);
-
-      gtk_label_set_text (GTK_LABEL (label), str ? str : "");
-
-      g_value_unset (&str_value);
-    }
-
-  g_value_unset (&value);
+  return label;
 }
 
 
@@ -3967,10 +3835,6 @@ gimp_prop_unit_combo_box_notify (GObject    *config,
 /*  icon name  */
 /***************/
 
-static void   gimp_prop_icon_image_notify (GObject    *config,
-                                           GParamSpec *param_spec,
-                                           GtkWidget  *image);
-
 /**
  * gimp_prop_icon_image_new:
  * @config:        Object to which property is attached.
@@ -4004,38 +3868,13 @@ gimp_prop_icon_image_new (GObject     *config,
                 NULL);
 
   image = gtk_image_new_from_icon_name (icon_name, icon_size);
-
-  if (icon_name)
-    g_free (icon_name);
-
-  set_param_spec (G_OBJECT (image), image, param_spec);
-
-  connect_notify (config, property_name,
-                  G_CALLBACK (gimp_prop_icon_image_notify),
-                  image);
-
   gtk_widget_show (image);
 
-  return image;
-}
+  g_object_bind_property (config, property_name,
+                          image, "icon-name",
+                          G_BINDING_BIDIRECTIONAL);
 
-static void
-gimp_prop_icon_image_notify (GObject    *config,
-                             GParamSpec *param_spec,
-                             GtkWidget  *image)
-{
-  gchar       *icon_name;
-  GtkIconSize  icon_size;
-
-  g_object_get (config,
-                param_spec->name, &icon_name,
-                NULL);
-
-  gtk_image_get_icon_name (GTK_IMAGE (image), NULL, &icon_size);
-  gtk_image_set_from_icon_name (GTK_IMAGE (image), icon_name, icon_size);
-
-  if (icon_name)
-    g_free (icon_name);
+  return image;
 }
 
 


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