[gtk+/wip/gbsneto/export-shortcuts-label: 2/2] shortcut-label: add 'unset-text' property



commit d95487825a04fff6a4dd5dcc22656ae62689f5a1
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Jul 26 17:12:31 2016 -0300

    shortcut-label: add 'unset-text' property
    
    When there's no useful shortcut accelerator set,
    GtkShortcutLabel doesn't show any useful information.
    
    To work around that, add a new property to set the
    text to be displayed when there's no accelerator
    available.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=769205

 gtk/gtkshortcutlabel.c |   73 ++++++++++++++++++++++++++++++++++++++++++++++-
 gtk/gtkshortcutlabel.h |    7 ++++
 2 files changed, 78 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkshortcutlabel.c b/gtk/gtkshortcutlabel.c
index 98355ee..be20b03 100644
--- a/gtk/gtkshortcutlabel.c
+++ b/gtk/gtkshortcutlabel.c
@@ -39,6 +39,7 @@ struct _GtkShortcutLabel
 {
   GtkBox  parent_instance;
   gchar  *accelerator;
+  gchar  *unset_text;
 };
 
 struct _GtkShortcutLabelClass
@@ -51,6 +52,7 @@ G_DEFINE_TYPE (GtkShortcutLabel, gtk_shortcut_label, GTK_TYPE_BOX)
 enum {
   PROP_0,
   PROP_ACCELERATOR,
+  PROP_UNSET_TEXT,
   LAST_PROP
 };
 
@@ -373,8 +375,12 @@ gtk_shortcut_label_rebuild (GtkShortcutLabel *self)
 
   gtk_container_foreach (GTK_CONTAINER (self), (GtkCallback)gtk_widget_destroy, NULL);
 
-  if (self->accelerator == NULL)
-    return;
+  if (self->accelerator == NULL || self->accelerator[0] == '\0')
+    {
+      gtk_container_add (GTK_CONTAINER (self), dim_label (self->unset_text));
+      gtk_widget_show_all (GTK_WIDGET (self));
+      return;
+    }
 
   accels = g_strsplit (self->accelerator, " ", 0);
   for (k = 0; accels[k]; k++)
@@ -415,6 +421,10 @@ gtk_shortcut_label_get_property (GObject    *object,
       g_value_set_string (value, gtk_shortcut_label_get_accelerator (self));
       break;
 
+    case PROP_UNSET_TEXT:
+      g_value_set_string (value, gtk_shortcut_label_get_unset_text (self));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -434,6 +444,10 @@ gtk_shortcut_label_set_property (GObject      *object,
       gtk_shortcut_label_set_accelerator (self, g_value_get_string (value));
       break;
 
+    case PROP_UNSET_TEXT:
+      gtk_shortcut_label_set_unset_text (self, g_value_get_string (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -460,6 +474,18 @@ gtk_shortcut_label_class_init (GtkShortcutLabelClass *klass)
                          NULL,
                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  /**
+   * Gtkshortcutlabel:unset-text:
+   *
+   * The text that is displayed when no accelerator is set.
+   *
+   * Since: 3.22
+   */
+  properties[PROP_ACCELERATOR] =
+    g_param_spec_string ("accelerator", P_("Accelerator"), P_("Accelerator"),
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_properties (object_class, LAST_PROP, properties);
 }
 
@@ -528,3 +554,46 @@ gtk_shortcut_label_set_accelerator (GtkShortcutLabel *self,
       g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACCELERATOR]);
     }
 }
+
+/**
+ * gtk_shortcut_label_get_unset_text:
+ * @self: a #Gtkshortcutlabel
+ *
+ * Retrieves the text that is displayed when no accelerator is set.
+ *
+ * Returns: (transfer none)(nullable): the current text displayed when no
+ * accelerator is set.
+ *
+ * Since: 3.22
+ */
+const gchar *
+gtk_shortcut_label_get_unset_text (GtkShortcutLabel *self)
+{
+  g_return_val_if_fail (GTK_IS_SHORTCUT_LABEL (self), NULL);
+
+  return self->unset_text;
+}
+
+/**
+ * gtk_shortcut_label_set_unset_text:
+ * @self: a #GtkShortcutLabel
+ * @unset_text: the new accelerator
+ *
+ * Sets the text to be displayed by @self when no accelerator is set.
+ *
+ * Since: 3.22
+ */
+void
+gtk_shortcut_label_set_unset_text (GtkShortcutLabel *self,
+                                   const gchar      *unset_text)
+{
+  g_return_if_fail (GTK_IS_SHORTCUT_LABEL (self));
+
+  if (g_strcmp0 (unset_text, self->unset_text) != 0)
+    {
+      g_free (self->unset_text);
+      self->unset_text = g_strdup (unset_text);
+      gtk_shortcut_label_rebuild (self);
+      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_UNSET_TEXT]);
+    }
+}
diff --git a/gtk/gtkshortcutlabel.h b/gtk/gtkshortcutlabel.h
index aecaa78..8db0a74 100644
--- a/gtk/gtkshortcutlabel.h
+++ b/gtk/gtkshortcutlabel.h
@@ -47,6 +47,13 @@ GDK_AVAILABLE_IN_3_22
 void         gtk_shortcut_label_set_accelerator (GtkShortcutLabel *self,
                                                  const gchar      *accelerator);
 
+GDK_AVAILABLE_IN_3_22
+const gchar *gtk_shortcut_label_get_unset_text  (GtkShortcutLabel *self);
+
+GDK_AVAILABLE_IN_3_22
+void         gtk_shortcut_label_set_unset_text  (GtkShortcutLabel *self,
+                                                 const gchar      *unset_text);
+
 G_END_DECLS
 
 #endif /* __GTK_SHORTCUT_LABEL_H__ */


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