This one should be a complete autocomplete implementation,
including history callback. I would like some feedback
before commiting anything.
diff --git a/src/hid/common/actions.c b/src/hid/common/actions.c
index 43c50f8..f511cc4 100644
--- a/src/hid/common/actions.c
+++ b/src/hid/common/actions.c
@@ -37,6 +37,19 @@ check_action_name (const char *s)
return NULL;
}
+/* Accessors needed by autocomplete list */
+int
+get_n_actions (void)
+{
+ return n_actions;
+}
+
+HID_Action **
+get_all_actions (void)
+{
+ return all_actions;
+}
+
void
hid_register_actions (HID_Action * a, int n)
{
diff --git a/src/hid/common/actions.h b/src/hid/common/actions.h
index 1cc6f24..a90d923 100644
--- a/src/hid/common/actions.h
+++ b/src/hid/common/actions.h
@@ -3,5 +3,7 @@
#define __HID_ACTIONS_INCLUDED__
void print_actions (void);
+HID_Action **get_all_actions (void);
+int get_n_actions (void);
#endif
diff --git a/src/hid/gtk/gui-command-window.c b/src/hid/gtk/gui-command-window.c
index b08836e..f95febf 100644
--- a/src/hid/gtk/gui-command-window.c
+++ b/src/hid/gtk/gui-command-window.c
@@ -35,6 +35,7 @@
#include "gui.h"
#include
+#include "hid/common/actions.h"
#include "command.h"
#include "crosshair.h"
@@ -46,10 +47,20 @@ RCSID ("$Id$");
static GtkWidget *command_window;
static GtkWidget *combo_vbox;
-static GList *history_list;
static gchar *command_entered;
static GMainLoop *loop;
+#define AUTO_MAX_HEIGHT250
+static GtkListStore *auto_complete_list;
+static GtkTreeModel *auto_complete;
+static gint auto_row_height;
+static bool is_auto_complete_change;
+static bool is_history_change;
+static const char *auto_complete_prefix;
+
+static GList *history;
+static GList *history_cursor;
+
/* gui-command-window.c provides two interfaces for getting user input
| for executing a command.
@@ -135,60 +146,65 @@ static gchar *command_ref_text[] = {
N_("\tLoad a vendor file. If 'filename' omitted, pop up file select
dialog.\n"),
};
-
- /* Put an allocated string on the history list and combo text list
- | if it is not a duplicate. The history_list is just a shadow of the
- | combo list, but I think is needed because I don't see an api for
reading
- | the combo strings. The combo box strings take "const gchar *", so the
- | same allocated string can go in both the history list and the combo
list.
- | If removed from both lists, a string can be freed.
- */
-static void
-command_history_add (gchar * cmd)
+static gboolean
+autocomplete_visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer
data)
{
- GList *list;
- gchar *s;
- gint i;
-
- if (!cmd || !*cmd)
-return;
+ /* Visible if row is non-empty and first column is "HI" */
+ gchar *str;
+ gboolean visible = FALSE;
- /* Check for a duplicate command. If found, move it to the
- | top of the list and similarly modify the combo box strings.
- */
- for (i = 0, list = history_list; list; list = list->next, ++i)
-{
- s = (gchar *) list->data;
- if (!strcmp (cmd, s))
- {
- history_list = g_list_remove (history_list, s);
- history_list = g_list_prepend (history_list, s);
- gtk_combo_box_remove_text (GTK_COMBO_BOX
-(ghidgui->command_combo_box), i);
- gtk_combo_box_prepend_text (GTK_COMBO_BOX
- (ghidgui->command_combo_box), s);
- return;
- }
-}
+ gtk_tree_model_get (model, iter, 0, &str, -1);
+ if (str && auto_complete_prefix &&
+ strncasecmp (str, auto_complete_prefix, strlen (auto_complete_prefix))
== 0)
+visible = TRUE;
+ g_free (str);
- /* Not a duplicate, so put first in history list and combo box text list.
- */
- s = g_strdup (cmd);
- history_list = g_list_prepend (history_list, s);
- gtk_combo_box_prepend_text (GTK_COMBO_BOX (ghidgui->command_combo_box), s);
+ return visible;
+}
- /* And keep the lists trimmed!
- */
- if (g_list_length (history_list) > ghidgui->history_size)
+static GtkTreeModel *
+create_autocomplete_model (void)
+{
+ int i;
+ int n = get_n_actions ();
+ HID_Action **actions = get_all_actions ();
+ GtkTreeIter iter;
+
+ auto_complete_prefix = NULL;
+ auto_complete_list = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
+ auto_complete = gtk_tree_model_filter_new (GTK_TREE_MODEL
(auto_complete_list), NULL);
+ gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
(auto_complete),
+ autocomplete_visible_func, NULL,
NULL);
+ /* First entry will be filled with textentry contents */
+ gtk_list_store_append (auto_complete_list, &iter);
+ gtk_list_store_set (auto_complete_list, &iter, 0, "", 1, "", -1);
+ for (i = 0; i < n; ++i)
{
- s = (gchar *) g_list_nth_data (history_list, ghidgui->history_size);
- history_list = g_list_remove (history_list, s);
- gtk_combo_box_remove_text (GTK_COMBO_BOX (ghidgui->comm