]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AirportList.cxx
Fred BOUVIER & Stuart BUCHANAN: make search function case-insensitive
[flightgear.git] / src / GUI / AirportList.cxx
1 #include <locale>
2 #include <Main/globals.hxx>
3 #include <Airports/simple.hxx>
4
5 #include "AirportList.hxx"
6
7
8 AirportList::AirportList(int x, int y, int width, int height) :
9     puaList(x, y, width, height),
10     GUI_ID(FGCLASS_AIRPORTLIST),
11     _airports(globals->get_airports()),
12     _content(0)
13 {
14     create_list();
15 }
16
17
18 AirportList::~AirportList()
19 {
20     destroy_list();
21 }
22
23
24 void
25 AirportList::create_list()
26 {
27     const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(std::locale());
28     int num_apt = _airports->size();
29     char **content = new char *[num_apt + 1];
30
31     int n = 0;
32     for (int i = 0; i < num_apt; i++) {
33         const FGAirport *apt = _airports->getAirport(i);
34         std::string entry(' ' + apt->getName() + "   (" + apt->getId() + ')');
35
36         if (!_filter.empty()) {
37             std::string upper(entry.data());
38             ct.toupper((char *)upper.data(), (char *)upper.data() + upper.size());
39
40             if (upper.find(_filter) == std::string::npos)
41                 continue;
42         }
43
44         content[n] = new char[entry.size() + 1];
45         strcpy(content[n], entry.c_str());
46         n++;
47     }
48     content[n] = 0;
49     // work around plib 2006/04/18 bug: lists with no entries cause crash on arrow-up
50     newList(n > 0 ? content : 0);
51
52     if (_content)
53         destroy_list();
54
55     _content = content;
56 }
57
58
59 void
60 AirportList::destroy_list()
61 {
62     for (char **c = _content; *c; c++) {
63         delete *c;
64         *c = 0;
65     }
66     delete [] _content;
67 }
68
69
70 void
71 AirportList::setValue(const char *s)
72 {
73     std::string filter(s);
74     const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(std::locale());
75     ct.toupper((char *)filter.data(), (char *)filter.data() + filter.size());
76
77     if (filter != _filter) {
78         _filter = filter;
79         create_list();
80     }
81 }
82
83