From 3ff07dc2e4582f3f6ef7fcbb57122a8a0a22caa6 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Tue, 8 Mar 2011 11:16:35 -0500 Subject: pass on mnemonics and markup from label --- libdbusmenu-gtk/parser.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'libdbusmenu-gtk/parser.c') diff --git a/libdbusmenu-gtk/parser.c b/libdbusmenu-gtk/parser.c index cf2003f..71df266 100644 --- a/libdbusmenu-gtk/parser.c +++ b/libdbusmenu-gtk/parser.c @@ -425,7 +425,7 @@ construct_dbusmenu_for_widget (GtkWidget * widget) // (like empathy), so watch the label especially for that. dbusmenu_menuitem_property_set (mi, "label", - gtk_label_get_text (GTK_LABEL (label))); + gtk_label_get_label (GTK_LABEL (label))); pdata->label = label; g_signal_connect (G_OBJECT (label), @@ -670,7 +670,7 @@ label_notify_cb (GtkWidget *widget, { dbusmenu_menuitem_property_set (child, DBUSMENU_MENUITEM_PROP_LABEL, - gtk_label_get_text (GTK_LABEL (widget))); + gtk_label_get_label (GTK_LABEL (widget))); } } -- cgit v1.2.3 From 415011b666e92ec899b2c53c80708fdfc1d90322 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Wed, 9 Mar 2011 08:26:36 -0500 Subject: sanitize label text to strip pango markup --- libdbusmenu-gtk/parser.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'libdbusmenu-gtk/parser.c') diff --git a/libdbusmenu-gtk/parser.c b/libdbusmenu-gtk/parser.c index 71df266..2d42de5 100644 --- a/libdbusmenu-gtk/parser.c +++ b/libdbusmenu-gtk/parser.c @@ -340,6 +340,23 @@ parse_menu_structure_helper (GtkWidget * widget, RecurseContext * recurse) return; } +static gchar * +sanitize_label_text (const gchar * label) +{ + /* Label contains underscores, which we like, and pango markup, + which we don't. */ + gchar * sanitized = NULL; + GError * error = NULL; + if (pango_parse_markup (label, -1, 0, NULL, &sanitized, NULL, &error)) { + return sanitized; + } + else { + g_warning ("Could not parse '%s': %s", label, error->message); + g_error_free (error); + return g_strdup (label); + } +} + /* Turn a widget into a dbusmenu item depending on the type of GTK object that it is. */ static DbusmenuMenuitem * @@ -423,9 +440,9 @@ construct_dbusmenu_for_widget (GtkWidget * widget) { // Sometimes, an app will directly find and modify the label // (like empathy), so watch the label especially for that. - dbusmenu_menuitem_property_set (mi, - "label", - gtk_label_get_label (GTK_LABEL (label))); + gchar * text = sanitize_label_text (gtk_label_get_label (GTK_LABEL (label))); + dbusmenu_menuitem_property_set (mi, "label", text); + g_free (text); pdata->label = label; g_signal_connect (G_OBJECT (label), @@ -668,9 +685,11 @@ label_notify_cb (GtkWidget *widget, if (pspec->name == g_intern_static_string ("label")) { + gchar * text = sanitize_label_text (gtk_label_get_label (GTK_LABEL (widget))); dbusmenu_menuitem_property_set (child, DBUSMENU_MENUITEM_PROP_LABEL, - gtk_label_get_label (GTK_LABEL (widget))); + text); + g_free (text); } } @@ -724,9 +743,11 @@ action_notify_cb (GtkAction *action, } else if (pspec->name == g_intern_static_string ("label")) { + gchar * text = sanitize_label_text (gtk_action_get_label (action)); dbusmenu_menuitem_property_set (mi, DBUSMENU_MENUITEM_PROP_LABEL, - gtk_action_get_label (action)); + text); + g_free (text); } } -- cgit v1.2.3 From 49ce3e92dd5e30a514813a9e9e8e38087a5e73bd Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Wed, 9 Mar 2011 15:02:07 -0500 Subject: respect use-underline and use-markup of labels in menu items --- libdbusmenu-gtk/parser.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'libdbusmenu-gtk/parser.c') diff --git a/libdbusmenu-gtk/parser.c b/libdbusmenu-gtk/parser.c index 8954d64..a890543 100644 --- a/libdbusmenu-gtk/parser.c +++ b/libdbusmenu-gtk/parser.c @@ -362,6 +362,32 @@ sanitize_label_text (const gchar * label) } } +static gchar * +sanitize_label (GtkLabel * label) +{ + gchar * text; + + if (gtk_label_get_use_markup (label)) { + text = sanitize_label_text (gtk_label_get_label (label)); + } + else { + text = g_strdup (gtk_label_get_label (label)); + } + + if (!gtk_label_get_use_underline (label)) { + /* Insert extra underscores */ + GRegex * regex = g_regex_new ("_", 0, 0, NULL); + gchar * escaped = g_regex_replace_literal (regex, text, -1, 0, "__", 0, NULL); + + g_regex_unref (regex); + g_free (text); + + text = escaped; + } + + return text; +} + /* Turn a widget into a dbusmenu item depending on the type of GTK object that it is. */ static DbusmenuMenuitem * @@ -445,7 +471,7 @@ construct_dbusmenu_for_widget (GtkWidget * widget) { // Sometimes, an app will directly find and modify the label // (like empathy), so watch the label especially for that. - gchar * text = sanitize_label_text (gtk_label_get_label (GTK_LABEL (label))); + gchar * text = sanitize_label (GTK_LABEL (label)); dbusmenu_menuitem_property_set (mi, "label", text); g_free (text); @@ -690,7 +716,7 @@ label_notify_cb (GtkWidget *widget, if (pspec->name == g_intern_static_string ("label")) { - gchar * text = sanitize_label_text (gtk_label_get_label (GTK_LABEL (widget))); + gchar * text = sanitize_label (GTK_LABEL (widget)); dbusmenu_menuitem_property_set (child, DBUSMENU_MENUITEM_PROP_LABEL, text); -- cgit v1.2.3 From 02b4e66962229f0d2ed48732466c6258b3b9a20a Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Wed, 9 Mar 2011 16:52:46 -0600 Subject: Add a function to get the cached menu item --- libdbusmenu-gtk/parser.c | 19 +++++++++++++++++++ libdbusmenu-gtk/parser.h | 1 + 2 files changed, 20 insertions(+) (limited to 'libdbusmenu-gtk/parser.c') diff --git a/libdbusmenu-gtk/parser.c b/libdbusmenu-gtk/parser.c index a890543..1b032bb 100644 --- a/libdbusmenu-gtk/parser.c +++ b/libdbusmenu-gtk/parser.c @@ -109,6 +109,25 @@ dbusmenu_gtk_parse_menu_structure (GtkWidget * widget) return recurse.parent; } +/** + * dbusmenu_gtk_parse_get_cached_item: + * @widget: A #GtkMenuItem that may have a cached #DbusmenuMenuitem from the parser + * + * The Dbusmenu GTK parser adds cached items on the various + * menu items throughout the tree. Sometimes it can be useful + * to get that cached item to use directly. This function + * will retrieve it for you. + * + * Return value: (transfer none): A pointer to the cached item + * or NULL if it isn't there. + */ +DbusmenuMenuitem * +dbusmenu_gtk_parse_get_cached_item (GtkWidget * widget) +{ + g_return_val_if_fail(GTK_IS_MENU_ITEM(widget), NULL); + return DBUSMENU_MENUITEM(g_object_get_data(G_OBJECT(widget), CACHED_MENUITEM)); +} + static void parse_data_free (gpointer data) { diff --git a/libdbusmenu-gtk/parser.h b/libdbusmenu-gtk/parser.h index 8187a8e..97fa9c6 100644 --- a/libdbusmenu-gtk/parser.h +++ b/libdbusmenu-gtk/parser.h @@ -35,6 +35,7 @@ License version 3 and version 2.1 along with this program. If not, see G_BEGIN_DECLS DbusmenuMenuitem * dbusmenu_gtk_parse_menu_structure (GtkWidget * widget); +DbusmenuMenuitem * dbusmenu_gtk_parse_get_cached_item (GtkWidget * widget); /** SECTION:parser -- cgit v1.2.3