From 255c93f5c070077cc54aed3f7360cb165fc97000 Mon Sep 17 00:00:00 2001 From: Robert Tari Date: Tue, 9 Jul 2024 16:07:52 +0200 Subject: src/idoswitchmenuitem.c: Allow the switch to display an accelerator. This enables the IDO switch menu item to process GMenuItem's standard 'accel' property and display it in a user readable format. --- src/idoswitchmenuitem.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/idoswitchmenuitem.c b/src/idoswitchmenuitem.c index 9bc9ef9..af90d7b 100644 --- a/src/idoswitchmenuitem.c +++ b/src/idoswitchmenuitem.c @@ -33,6 +33,7 @@ typedef struct GtkWidget * label; GtkWidget * image; GtkWidget * switch_w; + GtkWidget * accelerator; } IdoSwitchMenuItemPrivate; /*** @@ -168,12 +169,46 @@ ido_switch_menu_item_set_label (IdoSwitchMenuItem *item, priv->label = gtk_label_new (NULL); gtk_widget_set_halign (priv->label, GTK_ALIGN_START); gtk_widget_show (priv->label); - gtk_box_pack_end (GTK_BOX (priv->content_area), priv->label, TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (priv->content_area), priv->label, TRUE, TRUE, 0); } gtk_label_set_text (GTK_LABEL (priv->label), label); } +/** + * ido_switch_menu_item_set_accelerator: + * @item: a #IdoSwitchMenuItem. + * @accelerator: a string to set as the accelerator of @item + * + * Set the accelerator of @item to @accelerator. + **/ + +void ido_switch_menu_item_set_accelerator (IdoSwitchMenuItem *item, const gchar *accelerator) +{ + IdoSwitchMenuItemPrivate *priv; + + g_return_if_fail (IDO_IS_SWITCH_MENU_ITEM (item)); + g_return_if_fail (accelerator != NULL); + + priv = ido_switch_menu_item_get_instance_private (item); + + if (priv->accelerator == NULL) + { + priv->accelerator = gtk_label_new (NULL); + gtk_widget_set_halign (priv->accelerator, GTK_ALIGN_END); + GtkStyleContext *pContext = gtk_widget_get_style_context (priv->accelerator); + gtk_style_context_add_class (pContext, "accelerator"); + gtk_widget_show (priv->accelerator); + gtk_box_pack_end (GTK_BOX (priv->content_area), priv->accelerator, FALSE, FALSE, 6); + } + + guint nKey; + GdkModifierType cType; + gtk_accelerator_parse (accelerator, &nKey, &cType); + gchar *sText = gtk_accelerator_get_label (nKey, cType); + gtk_label_set_text (GTK_LABEL (priv->accelerator), sText); +} + /** * ido_switch_menu_item_set_icon: * @item: a #IdoSwitchMenuItem. @@ -248,6 +283,15 @@ ido_switch_menu_item_new_from_menu_model (GMenuItem *menuitem, g_free (label); } + gchar *sAccelerator; + gboolean bAccelerator = g_menu_item_get_attribute (menuitem, "accel", "s", &sAccelerator); + + if (bAccelerator) + { + ido_switch_menu_item_set_accelerator (IDO_SWITCH_MENU_ITEM (item), sAccelerator); + g_free (sAccelerator); + } + serialized_icon = g_menu_item_get_attribute_value (menuitem, "icon", NULL); if (serialized_icon) { -- cgit v1.2.3