summaryrefslogtreecommitdiff
path: root/gtk/gtkshortcutlabel.c
diff options
context:
space:
mode:
authorGeorges Basile Stavracas Neto <georges.stavracas@gmail.com>2016-07-26 17:12:31 -0300
committerGeorges Basile Stavracas Neto <georges.stavracas@gmail.com>2016-07-27 16:30:43 -0300
commitddee89f4a3b17f99faef5fcea528dc1e5d963061 (patch)
treee5327b9d4195ae9935c08510d2ce95b112232f52 /gtk/gtkshortcutlabel.c
parent7543cd8ce419a4d660f0ff7614f0c6e1cac81804 (diff)
downloadgtk+-ddee89f4a3b17f99faef5fcea528dc1e5d963061.tar.gz
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
Diffstat (limited to 'gtk/gtkshortcutlabel.c')
-rw-r--r--gtk/gtkshortcutlabel.c78
1 files changed, 76 insertions, 2 deletions
diff --git a/gtk/gtkshortcutlabel.c b/gtk/gtkshortcutlabel.c
index 45cf3daca5..905a27ba7c 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]);
+ }
+}