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
This commit is contained in:
Georges Basile Stavracas Neto 2016-07-26 17:12:31 -03:00
parent 7543cd8ce4
commit ddee89f4a3
2 changed files with 83 additions and 2 deletions

View File

@ -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]);
}
}

View File

@ -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__ */