qulogic/libgnt

Make the combobox a tiny bit friendlier.
v2.7.3
2010-07-05, Sadrul Habib Chowdhury
ac8d8a62c098
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.
  • +2 -2
    configure.ac
  • +34 -2
    gntcombobox.c
  • +30 -0
    gnttree.c
  • +55 -0
    gnttree.h
  • --- a/configure.ac Mon Jul 05 15:00:36 2010 +0000
    +++ b/configure.ac Mon Jul 05 15:01:56 2010 +0000
    @@ -24,9 +24,9 @@
    # Make sure to update ../../configure.ac with libgnt version changes.
    #
    -m4_define([gnt_lt_current], [7])
    +m4_define([gnt_lt_current], [8])
    m4_define([gnt_major_version], [2])
    -m4_define([gnt_minor_version], [7])
    +m4_define([gnt_minor_version], [8])
    m4_define([gnt_micro_version], [0])
    m4_define([gnt_version_suffix], [devel])
    m4_define([gnt_version],
    --- a/gntcombobox.c Mon Jul 05 15:00:36 2010 +0000
    +++ b/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;
    }
    --- a/gnttree.c Mon Jul 05 15:00:36 2010 +0000
    +++ b/gnttree.c Mon Jul 05 15:01:56 2010 +0000
    @@ -1926,3 +1926,33 @@
    return (row && row->parent) ? row->parent->key : NULL;
    }
    +gpointer gnt_tree_row_get_key(GntTree *tree, GntTreeRow *row)
    +{
    + g_return_val_if_fail(row && row->tree == tree, NULL);
    + return row->key;
    +}
    +
    +GntTreeRow * gnt_tree_row_get_next(GntTree *tree, GntTreeRow *row)
    +{
    + g_return_val_if_fail(row && row->tree == tree, NULL);
    + return row->next;
    +}
    +
    +GntTreeRow * gnt_tree_row_get_prev(GntTree *tree, GntTreeRow *row)
    +{
    + g_return_val_if_fail(row && row->tree == tree, NULL);
    + return row->prev;
    +}
    +
    +GntTreeRow * gnt_tree_row_get_child(GntTree *tree, GntTreeRow *row)
    +{
    + g_return_val_if_fail(row && row->tree == tree, NULL);
    + return row->child;
    +}
    +
    +GntTreeRow * gnt_tree_row_get_parent(GntTree *tree, GntTreeRow *row)
    +{
    + g_return_val_if_fail(row && row->tree == tree, NULL);
    + return row->parent;
    +}
    +
    --- a/gnttree.h Mon Jul 05 15:00:36 2010 +0000
    +++ b/gnttree.h Mon Jul 05 15:01:56 2010 +0000
    @@ -222,6 +222,61 @@
    GList * gnt_tree_get_row_text_list(GntTree *tree, gpointer key);
    /**
    + * Get the key of a row.
    + *
    + * @param tree The tree
    + * @param row The GntTreeRow object
    + *
    + * @return The key of the row.
    + * @since 2.8.0 (gnt), 2.7.2 (pidgin)
    + */
    +gpointer gnt_tree_row_get_key(GntTree *tree, GntTreeRow *row);
    +
    +/**
    + * Get the next row.
    + *
    + * @param tree The tree
    + * @param row The GntTreeRow object
    + *
    + * @return The next row.
    + * @since 2.8.0 (gnt), 2.7.2 (pidgin)
    + */
    +GntTreeRow * gnt_tree_row_get_next(GntTree *tree, GntTreeRow *row);
    +
    +/**
    + * Get the previous row.
    + *
    + * @param tree The tree
    + * @param row The GntTreeRow object
    + *
    + * @return The previous row.
    + * @since 2.8.0 (gnt), 2.7.2 (pidgin)
    + */
    +GntTreeRow * gnt_tree_row_get_prev(GntTree *tree, GntTreeRow *row);
    +
    +/**
    + * Get the child row.
    + *
    + * @param tree The tree
    + * @param row The GntTreeRow object
    + *
    + * @return The child row.
    + * @since 2.8.0 (gnt), 2.7.2 (pidgin)
    + */
    +GntTreeRow * gnt_tree_row_get_child(GntTree *tree, GntTreeRow *row);
    +
    +/**
    + * Get the parent row.
    + *
    + * @param tree The tree
    + * @param row The GntTreeRow object
    + *
    + * @return The parent row.
    + * @since 2.8.0 (gnt), 2.7.2 (pidgin)
    + */
    +GntTreeRow * gnt_tree_row_get_parent(GntTree *tree, GntTreeRow *row);
    +
    +/**
    * Get a list of text of the current row.
    *
    * @param tree The tree