From 5471365b07f24e9deb1021c030674b0953a0c830 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 8 Feb 2015 21:03:34 +0100 Subject: option list: Underline matches When using search in an option list, show matches in the list. This commit is just a proof-of-concept, it needs more work to be utf8-safe. --- gtk/gtkoptionlist.c | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/gtk/gtkoptionlist.c b/gtk/gtkoptionlist.c index 759440d0cc..bee1d3c717 100644 --- a/gtk/gtkoptionlist.c +++ b/gtk/gtkoptionlist.c @@ -382,21 +382,45 @@ static gboolean gtk_option_list_row_matches (GtkOptionListRow *row, const gchar *match) { - gchar *row_text; - gchar *search_text; - gchar *hit; + gchar *hit = NULL; + PangoAttrList *attrs = NULL; + gboolean ret = TRUE; - if (match[0] == '\0') - return TRUE; + if (match[0] != '\0') + { + gchar *row_text; + gchar *search_text; + + row_text = g_utf8_strdown (row->text, -1); + search_text = g_utf8_strdown (match, -1); + + hit = strstr (row_text, search_text); + ret = hit != NULL; - row_text = g_utf8_strdown (row->text, -1); - search_text = g_utf8_strdown (match, -1); - hit = strstr (row_text, search_text); + if (hit != NULL) + { + gint offset; + gint length; + PangoAttribute *attr; + + /* FIXME: this is not utf8 safe */ + offset = hit - row_text; + length = strlen (match); + + attrs = pango_attr_list_new (); + attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE); + attr->start_index = offset; + attr->end_index = offset + length; + pango_attr_list_insert (attrs, attr); + } + + g_free (row_text); + g_free (search_text); + } - g_free (row_text); - g_free (search_text); + gtk_label_set_attributes (GTK_LABEL (row->label), attrs); - return hit != NULL; + return ret; } static const gchar * -- cgit v1.2.1