summaryrefslogtreecommitdiff
path: root/src/Fl_Search_Chooser.cxx
blob: 62aa7d122517dee81b2ba588a84cd017abe0d120 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Search Chooser widget for the Fast Light Tool Kit (FLTK).
// 
// Copyright by Johannes Hofmann
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
//

#include <stdio.h>
#include <string.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include "Fl_Search_Chooser.H"

int
Fl_Search_Browser::find_prefix(const char *p) {
  int i = find_prefix(p, 1, size());
  if (i == -1) {
    return 1;
  } else {
    deselect();
    middleline(i);
    select(i);
    return 0;
  }
};

int
Fl_Search_Browser::find_prefix(const char *p, int s, int e) {
  if (s < 0 || e > size() || s > e) {
    fprintf(stderr, "Invalid search range %d %d\n", s, e);
    return 1;
  } else if (e - s <= 1) {
    if (strncasecmp(p, text(s), strlen(p)) == 0) {
       return s;
    } else if (strncasecmp(p, text(e), strlen(p)) == 0){
       return e;
    } else {
       return -1;
    }
  } else {
    int med = s + (e - s) / 2;
    if (strncasecmp(p, text(med), strlen(p)) > 0) {
      return find_prefix(p, med, e);
    } else {
      return find_prefix(p, s, med);
    }
  }
} 


static void input_cb(Fl_Input* in, void*c) {
  Fl_Search_Browser *sb = ((Fl_Search_Chooser *) c)->sb;
  sb->find_prefix(in->value()); 
}    


static void ok_cb(Fl_Input* in, void*c) {
  Fl_Search_Chooser *sc = (Fl_Search_Chooser *) c;
  sc->w->hide();
} 


static void cancel_cb(Fl_Input* in, void*c) {
  Fl_Search_Chooser *sc = (Fl_Search_Chooser *) c;
  sc->sb->deselect();
  sc->w->hide();
}    

Fl_Search_Chooser::Fl_Search_Chooser(const char *title) {
  w = new Fl_Window(320, 320, title?title:"Choose");
  Fl_Group *g = new Fl_Group(10, 10, w->w() - 10, w->h() - 10);
  sb = new Fl_Search_Browser(g->x(), g->y(), g->w() , g->h() - 100, NULL);
  sb->type(FL_HOLD_BROWSER);
  Fl_Input *in = new Fl_Input(g->x()+50, g->h()-80, g->w()-80, 20, "Search");
  in->callback((Fl_Callback*) input_cb, this);
  in->when(FL_WHEN_CHANGED);
  Fl_Button *cancel_b = new Fl_Button(g->w()-200, g->h()-30, 80, 25, "Cancel");
  cancel_b->callback((Fl_Callback*) cancel_cb, this);
  Fl_Button *ok_b = new Fl_Button(g->w()-100, g->h()-30, 80, 25, "Ok");
  ok_b->callback((Fl_Callback*) ok_cb, this);
  Fl::focus(in);
  g->end();
  w->end();
}

Fl_Search_Chooser::~Fl_Search_Chooser() {
  delete sb;
  delete w;
}

void
Fl_Search_Chooser::add(const char *t, void *d) {
  sb->add(t, d);
}

void *
Fl_Search_Chooser::data() {
  int v = sb->value();
  if (v) {
    return sb->data(v);
  } else {
    return NULL;
  }
}

void
Fl_Search_Chooser::show() {
  w->show();
}

int
Fl_Search_Chooser::shown() {
  return w->shown();
}