aboutsummaryrefslogtreecommitdiff
path: root/libdbusmenu-gtk/parser.c
diff options
context:
space:
mode:
authorTed Gould <ted@gould.cx>2011-03-10 11:05:16 -0600
committerTed Gould <ted@gould.cx>2011-03-10 11:05:16 -0600
commit9aa5445985ebf244681ccfb3bb6f264eb4282bca (patch)
treeb8a4f31686c6339498e34785252ff57016a5f518 /libdbusmenu-gtk/parser.c
parent05d9b0b58fa3c9247860f911bb3477cd52665671 (diff)
parentcec350b1d24eeeb308fd02846bf637d4ec92fbb5 (diff)
downloadlibdbusmenu-9aa5445985ebf244681ccfb3bb6f264eb4282bca.tar.gz
libdbusmenu-9aa5445985ebf244681ccfb3bb6f264eb4282bca.tar.bz2
libdbusmenu-9aa5445985ebf244681ccfb3bb6f264eb4282bca.zip
New upstream release.
∘ Add helper to get cached menuitem from widget ∘ Have the GTK Dbusmenu Client handle theme directories (LP: #727325) ∘ Fix a bunch of memory leaks (LP: #722972) ∘ Handle _ in menuitems more correctly when parsing (LP: #621301) ∘ Protect from NULL layout_props (LP: #729722) ∘ Remove the property from the hashtable before signaling that it's been changed (LP: #725603) ∘ Wrap emitted properties in a variant ∘ Fix enum templates so they can be included by other programs without error ∘ Setting the cached menu item (LP: #723463) (LP: #729128) (LP: #729194)
Diffstat (limited to 'libdbusmenu-gtk/parser.c')
-rw-r--r--libdbusmenu-gtk/parser.c76
1 files changed, 71 insertions, 5 deletions
diff --git a/libdbusmenu-gtk/parser.c b/libdbusmenu-gtk/parser.c
index b0a5cfd..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)
{
@@ -345,6 +364,49 @@ 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);
+ }
+}
+
+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 *
@@ -428,9 +490,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_text (GTK_LABEL (label)));
+ gchar * text = sanitize_label (GTK_LABEL (label));
+ dbusmenu_menuitem_property_set (mi, "label", text);
+ g_free (text);
pdata->label = label;
g_signal_connect (G_OBJECT (label),
@@ -673,9 +735,11 @@ label_notify_cb (GtkWidget *widget,
if (pspec->name == g_intern_static_string ("label"))
{
+ gchar * text = sanitize_label (GTK_LABEL (widget));
dbusmenu_menuitem_property_set (child,
DBUSMENU_MENUITEM_PROP_LABEL,
- gtk_label_get_text (GTK_LABEL (widget)));
+ text);
+ g_free (text);
}
}
@@ -729,9 +793,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);
}
}