]> git.ipfire.org Git - people/ms/putty.git/commitdiff
Add search to connection list box. master
authorMichael Tremer <m.tremer@btnet.de>
Wed, 6 Oct 2010 10:20:43 +0000 (12:20 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 18 Jul 2011 11:43:31 +0000 (13:43 +0200)
config.c

index a1454b4dc103b7deaa821eac69d79411cd0cb4af..8085ebe951a1a12238ffe926834ed0815a2c9dfb 100644 (file)
--- a/config.c
+++ b/config.c
@@ -4,6 +4,7 @@
  */
 
 #include <assert.h>
+#include <ctype.h>
 #include <stdlib.h>
 
 #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;