diff options
Diffstat (limited to 'libdbusmenu-gtk/parser.c')
-rw-r--r-- | libdbusmenu-gtk/parser.c | 64 |
1 files changed, 60 insertions, 4 deletions
diff --git a/libdbusmenu-gtk/parser.c b/libdbusmenu-gtk/parser.c index a0bb5c9..9d93a1e 100644 --- a/libdbusmenu-gtk/parser.c +++ b/libdbusmenu-gtk/parser.c @@ -122,6 +122,27 @@ dbusmenu_gtk_parse_menu_structure (GtkWidget * widget) return returnval; } +/** + * 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) +{ + if (!GTK_IS_MENU_ITEM(widget)) { + return NULL; + } + return DBUSMENU_MENUITEM(g_object_get_data(G_OBJECT(widget), CACHED_MENUITEM)); +} + static void parse_data_free (gpointer data) { @@ -370,14 +391,46 @@ sanitize_label_text (const gchar * label) which we don't. */ gchar * sanitized = NULL; GError * error = NULL; + + if (label == NULL) { + return NULL; + } + if (pango_parse_markup (label, -1, 0, NULL, &sanitized, NULL, &error)) { return sanitized; } - else { + + if (error != NULL) { g_warning ("Could not parse '%s': %s", label, error->message); g_error_free (error); - return g_strdup (label); } + 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 @@ -463,7 +516,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); @@ -602,6 +655,9 @@ update_icon (DbusmenuMenuitem *menuitem, GtkImage *image) if (image != NULL && should_show_image (image)) { switch (gtk_image_get_storage_type (image)) { + case GTK_IMAGE_EMPTY: + break; + case GTK_IMAGE_PIXBUF: pixbuf = g_object_ref (gtk_image_get_pixbuf (image)); break; @@ -712,7 +768,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); |