]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AirportList.cxx
support filtering to show only a subset of entries
[flightgear.git] / src / GUI / AirportList.cxx
1 #include <string.h>     // strncpy()
2
3 #include <Main/globals.hxx>
4 #include <Airports/simple.hxx>
5
6 #include "AirportList.hxx"
7
8
9 AirportList::AirportList (int x, int y, int width, int height)
10     : puList(x, y, width, height),
11       _airports(globals->get_airports()),
12       _content(0)
13 {
14     create_list();
15 }
16
17 AirportList::~AirportList ()
18 {
19     destroy_list();
20 }
21
22 void
23 AirportList::create_list ()
24 {
25     if (_content)
26         destroy_list();
27
28     int num_apt = _airports->size();
29     _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() && entry.find(_filter) == STD::string::npos)
37             continue;
38
39         _content[n] = new char[entry.size() + 1];
40         strcpy(_content[n], entry.c_str());
41         n++;
42     }
43     _content[n] = 0;
44     newList(_content);
45 }
46
47 void
48 AirportList::destroy_list ()
49 {
50     for (char **c = _content; *c; c++) {
51         delete *c;
52         *c = 0;
53     }
54     delete [] _content;
55 }
56
57 char *
58 AirportList::getListStringValue ()
59 {
60     int i = getListIntegerValue();
61     return i < 0 ? 0 : _content[i];
62 }
63
64 void
65 AirportList::setValue (const char *s)
66 {
67     STD::string filter(s);
68     if (filter != _filter) {
69         _filter = filter;
70         create_list();
71     }
72 }
73
74 // end of AirportList.cxx
75