[gtk+] shortcut-label: add 'disabled-text' property



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

    shortcut-label: add 'disabled-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 |   78 ++++++++++++++++++++++++++++++++++++++++++++++-
 gtk/gtkshortcutlabel.h |    7 ++++
 2 files changed, 83 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkshortcutlabel.c b/gtk/gtkshortcutlabel.c
index 45cf3da..905a27b 100644
--- a/gtk/gtkshortcutlabel.c
+++ b/gtk/gtkshortcutlabel.c
@@ -39,6 +39,7 @@ struct _GtkShortcutLabel
 {
   GtkBox  parent_instance;
   gchar  *accelerator;
+  gchar  *disabled_text;
 };
 
 struct _GtkShortcutLabelClass
@@ -51,6 +52,7 @@ G_DEFINE_TYPE (GtkShortcutLabel, gtk_shortcut_label, GTK_TYPE_BOX)
 enum {
   PROP_0,
   PROP_ACCELERATOR,
+  PROP_DISABLED_TEXT,
   LAST_PROP
 };
 
@@ -373,8 +375,16 @@ 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')
+    {
+      GtkWidget *label;
+
+      label = dim_label (self->disabled_text);
+      gtk_widget_show (label);
+
+      gtk_container_add (GTK_CONTAINER (self), label);
+      return;
+    }
 
   accels = g_strsplit (self->accelerator, " ", 0);
   for (k = 0; accels[k]; k++)
@@ -397,6 +407,7 @@ gtk_shortcut_label_finalize (GObject *object)
   GtkShortcutLabel *self = (GtkShortcutLabel *)object;
 
   g_free (self->accelerator);
+  g_free (self->disabled_text);
 
   G_OBJECT_CLASS (gtk_shortcut_label_parent_class)->finalize (object);
 }
@@ -415,6 +426,10 @@ gtk_shortcut_label_get_property (GObject    *object,
       g_value_set_string (value, gtk_shortcut_label_get_accelerator (self));
       break;
 
+    case PROP_DISABLED_TEXT:
+      g_value_set_string (value, gtk_shortcut_label_get_disabled_text (self));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -434,6 +449,10 @@ gtk_shortcut_label_set_property (GObject      *object,
       gtk_shortcut_label_set_accelerator (self, g_value_get_string (value));
       break;
 
+    case PROP_DISABLED_TEXT:
+      gtk_shortcut_label_set_disabled_text (self, g_value_get_string (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -461,6 +480,18 @@ gtk_shortcut_label_class_init (GtkShortcutLabelClass *klass)
                          NULL,
                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  /**
+   * GtkShortcutLabel:disabled-text:
+   *
+   * The text that is displayed when no accelerator is set.
+   *
+   * Since: 3.22
+   */
+  properties[PROP_DISABLED_TEXT] =
+    g_param_spec_string ("disabled-text", P_("Disabled text"), P_("Disabled text"),
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_properties (object_class, LAST_PROP, properties);
 }
 
@@ -529,3 +560,46 @@ gtk_shortcut_label_set_accelerator (GtkShortcutLabel *self,
       g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACCELERATOR]);
     }
 }
+
+/**
+ * gtk_shortcut_label_get_disabled_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_disabled_text (GtkShortcutLabel *self)
+{
+  g_return_val_if_fail (GTK_IS_SHORTCUT_LABEL (self), NULL);
+
+  return self->disabled_text;
+}
+
+/**
+ * gtk_shortcut_label_set_disabled_text:
+ * @self: a #GtkShortcutLabel
+ * @disabled_text: the text to be displayed when no accelerator is set
+ *
+ * Sets the text to be displayed by @self when no accelerator is set.
+ *
+ * Since: 3.22
+ */
+void
+gtk_shortcut_label_set_disabled_text (GtkShortcutLabel *self,
+                                      const gchar      *disabled_text)
+{
+  g_return_if_fail (GTK_IS_SHORTCUT_LABEL (self));
+
+  if (g_strcmp0 (disabled_text, self->disabled_text) != 0)
+    {
+      g_free (self->disabled_text);
+      self->disabled_text = g_strdup (disabled_text);
+      gtk_shortcut_label_rebuild (self);
+      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DISABLED_TEXT]);
+    }
+}
diff --git a/gtk/gtkshortcutlabel.h b/gtk/gtkshortcutlabel.h
index aecaa78..e45669e 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_disabled_text (GtkShortcutLabel *self);
+
+GDK_AVAILABLE_IN_3_22
+void         gtk_shortcut_label_set_disabled_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]