[gtk+/wip/combo] option list: Underline matches



commit 5471365b07f24e9deb1021c030674b0953a0c830
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Feb 8 21:03:34 2015 +0100

    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 files changed, 35 insertions(+), 11 deletions(-)
---
diff --git a/gtk/gtkoptionlist.c b/gtk/gtkoptionlist.c
index 759440d..bee1d3c 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 *


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]