]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AirportList.cxx
- work around plib bug that crashes fgfs if no airport was found (empty
[flightgear.git] / src / GUI / AirportList.cxx
1
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     : puList(x, y, width, height),
10       _airports(globals->get_airports()),
11       _content(0)
12 {
13     create_list();
14 }
15
16 AirportList::~AirportList ()
17 {
18     destroy_list();
19 }
20
21 void
22 AirportList::create_list ()
23 {
24     int num_apt = _airports->size();
25     char **content = new char *[num_apt + 1];
26
27     int n = 0;
28     for (int i = 0; i < num_apt; i++) {
29         const FGAirport *apt = _airports->getAirport(i);
30         STD::string entry(apt->getName() + "   (" + apt->getId() + ')');
31
32         if (!_filter.empty() && entry.find(_filter) == STD::string::npos)
33             continue;
34
35         content[n] = new char[entry.size() + 1];
36         strcpy(content[n], entry.c_str());
37         n++;
38     }
39     content[n] = 0;
40     // work around plib 2006/04/18 bug: lists with no entries cause crash on arrow-up
41     newList(n > 0 ? content : 0);
42
43     if (_content)
44         destroy_list();
45
46     _content = content;
47 }
48
49 void
50 AirportList::destroy_list ()
51 {
52     for (char **c = _content; *c; c++) {
53         delete *c;
54         *c = 0;
55     }
56     delete [] _content;
57 }
58
59 char *
60 AirportList::getListStringValue ()
61 {
62     int i = getListIntegerValue();
63     return i < 0 ? 0 : _content[i];
64 }
65
66 void
67 AirportList::setValue (const char *s)
68 {
69     STD::string filter(s);
70     if (filter != _filter) {
71         _filter = filter;
72         create_list();
73     }
74 }
75
76 // end of AirportList.cxx
77