diff finch/libgnt/gntcombobox.c @ 30245:e1b511df0d3e

Make the combobox a tiny bit friendlier. Pressing the first letter of an item will now jump to that item (and popup the dropdown first if necessary). Add some API in libgnt in the process.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Mon, 05 Jul 2010 15:01:56 +0000
parents 658e8b9522bc
children
line wrap: on
line diff
--- a/finch/libgnt/gntcombobox.c	Mon Jul 05 15:00:36 2010 +0000
+++ b/finch/libgnt/gntcombobox.c	Mon Jul 05 15:01:56 2010 +0000
@@ -150,7 +150,9 @@
 gnt_combo_box_key_pressed(GntWidget *widget, const char *text)
 {
 	GntComboBox *box = GNT_COMBO_BOX(widget);
-	if (GNT_WIDGET_IS_FLAG_SET(box->dropdown->parent, GNT_WIDGET_MAPPED)) {
+	gboolean showing = !!GNT_WIDGET_IS_FLAG_SET(box->dropdown->parent, GNT_WIDGET_MAPPED);
+
+	if (showing) {
 		if (text[1] == 0) {
 			switch (text[0]) {
 				case '\r':
@@ -166,11 +168,41 @@
 	}
 
 	if (gnt_widget_key_pressed(box->dropdown, text)) {
-		if (!GNT_WIDGET_IS_FLAG_SET(box->dropdown->parent, GNT_WIDGET_MAPPED))
+		if (!showing)
 			popup_dropdown(box);
 		return TRUE;
 	}
 
+	{
+#define SEARCH_IN_RANGE(start, end) do { \
+		GntTreeRow *row; \
+		for (row = start; row != end; \
+				row = gnt_tree_row_get_next(tree, row)) { \
+			gpointer key = gnt_tree_row_get_key(tree, row); \
+			GList *list = gnt_tree_get_row_text_list(tree, key); \
+			gboolean found = FALSE; \
+			found = (list->data && g_ascii_strncasecmp(text, list->data, len) == 0); \
+			g_list_foreach(list, (GFunc)g_free, NULL); \
+			g_list_free(list); \
+			if (found) { \
+				if (!showing) \
+					popup_dropdown(box); \
+				gnt_tree_set_selected(tree, key); \
+				return TRUE; \
+			} \
+		} \
+} while (0)
+
+		int len = strlen(text);
+		GntTree *tree = GNT_TREE(box->dropdown);
+		GntTreeRow *current = tree->current;
+
+		SEARCH_IN_RANGE(gnt_tree_row_get_next(tree, current), NULL);
+		SEARCH_IN_RANGE(tree->top, current);
+
+#undef SEARCH_IN_RANGE
+	}
+
 	return FALSE;
 }