From: Michael Tremer Date: Wed, 6 Oct 2010 10:20:43 +0000 (+0200) Subject: Add search to connection list box. X-Git-Url: http://git.ipfire.org/?p=people%2Fms%2Fputty.git;a=commitdiff_plain Add search to connection list box. --- diff --git a/config.c b/config.c index a1454b4..8085ebe 100644 --- a/config.c +++ b/config.c @@ -4,6 +4,7 @@ */ #include +#include #include #include "putty.h" @@ -398,6 +399,8 @@ struct sessionsaver_data { union control *okbutton, *cancelbutton; struct sesslist sesslist; int midsession; + char filterstr[SAVEDSESSION_LEN]; + int filteritems[2048]; }; /* @@ -410,6 +413,12 @@ static int load_selected_session(struct sessionsaver_data *ssd, void *dlg, Config *cfg, int *maybe_launch) { int i = dlg_listbox_index(ssd->listbox, dlg); + /* If filterstr is set, we need to pick the i item out + * of filteritems. + */ + if (strlen(ssd->filterstr)) { + i = ssd->filteritems[i]; + } int isdef; if (i < 0) { dlg_beep(dlg); @@ -463,33 +472,40 @@ static void sessionsaver_handler(union control *ctrl, void *dlg, if (ctrl == ssd->editbox) { dlg_editbox_set(ctrl, dlg, savedsession); } else if (ctrl == ssd->listbox) { - int i; + int i, j = 0, k; + char checkstr[SAVEDSESSION_LEN]; dlg_update_start(ctrl, dlg); dlg_listbox_clear(ctrl, dlg); - for (i = 0; i < ssd->sesslist.nsessions; i++) + for (i = 0; i < ssd->sesslist.nsessions; i++) { + /* Loop to build listbox content: + * We only put the item into it when it matches + * the given string in ssd->filterstr. + */ + for (k = 0; k < SAVEDSESSION_LEN; k++) { + checkstr[k] = tolower(ssd->sesslist.sessions[i][k]); + } + if (!strstr(checkstr, ssd->filterstr)) + continue; + ssd->filteritems[j++] = i; dlg_listbox_add(ctrl, dlg, ssd->sesslist.sessions[i]); + } dlg_update_done(ctrl, dlg); } } else if (event == EVENT_VALCHANGE) { - int top, bottom, halfway, i; + int i; if (ctrl == ssd->editbox) { dlg_editbox_get(ctrl, dlg, savedsession, SAVEDSESSION_LEN); - top = ssd->sesslist.nsessions; - bottom = -1; - while (top-bottom > 1) { - halfway = (top+bottom)/2; - i = strcmp(savedsession, ssd->sesslist.sessions[halfway]); - if (i <= 0 ) { - top = halfway; - } else { - bottom = halfway; - } - } - if (top == ssd->sesslist.nsessions) { - top -= 1; - } - dlg_listbox_select(ssd->listbox, dlg, top); + /* + * Save string that was typed into the textbox and + * save it into ssd->filterstr. We convert the string + * to lowercase for a case insensitive matching. + */ + for (i = 0; i < SAVEDSESSION_LEN; i++) { + ssd->filterstr[i] = tolower(savedsession[i]); + } + // Refreshing listbox. + dlg_refresh(ssd->listbox, dlg); } } else if (event == EVENT_ACTION) { int mbl = FALSE; @@ -1232,7 +1248,7 @@ void setup_config_box(struct controlbox *b, int midsession, "Load, save or delete a stored session"); ctrl_columns(s, 2, 75, 25); get_sesslist(&ssd->sesslist, TRUE); - ssd->editbox = ctrl_editbox(s, "Saved Sessions", 'e', 100, + ssd->editbox = ctrl_editbox(s, "Saved Sessions / Search", 'e', 100, HELPCTX(session_saved), sessionsaver_handler, P(ssd), P(NULL)); ssd->editbox->generic.column = 0;