From bff4c3fe22a255046d2210c87c975461111ffb86 Mon Sep 17 00:00:00 2001 From: mfranz Date: Fri, 31 Mar 2006 10:17:43 +0000 Subject: [PATCH] support filtering to show only a subset of entries --- src/GUI/AirportList.cxx | 67 +++++++++++++++++++++++++++-------------- src/GUI/AirportList.hxx | 6 +++- 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/GUI/AirportList.cxx b/src/GUI/AirportList.cxx index 5b28e6bf2..9e2496c1e 100644 --- a/src/GUI/AirportList.cxx +++ b/src/GUI/AirportList.cxx @@ -7,35 +7,49 @@ AirportList::AirportList (int x, int y, int width, int height) - : puList(x, y, width, height) + : puList(x, y, width, height), + _airports(globals->get_airports()), + _content(0) { - char buf[1024]; + create_list(); +} + +AirportList::~AirportList () +{ + destroy_list(); +} + +void +AirportList::create_list () +{ + if (_content) + destroy_list(); - _airports = globals->get_airports(); - _nAirports = _airports->size(); + int num_apt = _airports->size(); + _content = new char *[num_apt + 1]; - _content = new char *[_nAirports+1]; - for (int i = 0; i < _nAirports; i++) { - const FGAirport *airport = _airports->getAirport(i); - snprintf(buf, 1023, "%s (%s)", - airport->getName().c_str(), - airport->getId().c_str()); + int n = 0; + for (int i = 0; i < num_apt; i++) { + const FGAirport *apt = _airports->getAirport(i); + STD::string entry(apt->getName() + " (" + apt->getId() + ')'); - unsigned int buf_len = (strlen(buf) > 1023) ? 1023 : strlen(buf); + if (!_filter.empty() && entry.find(_filter) == STD::string::npos) + continue; - _content[i] = new char[buf_len+1]; - memcpy(_content[i], buf, buf_len); - _content[i][buf_len] = '\0'; + _content[n] = new char[entry.size() + 1]; + strcpy(_content[n], entry.c_str()); + n++; } - _content[_nAirports] = 0; + _content[n] = 0; newList(_content); } -AirportList::~AirportList () +void +AirportList::destroy_list () { - for (int i = 0; i < _nAirports; i++) { - delete _content[i]; - _content[i] = 0; + for (char **c = _content; *c; c++) { + delete *c; + *c = 0; } delete [] _content; } @@ -44,10 +58,17 @@ char * AirportList::getListStringValue () { int i = getListIntegerValue(); - if (i >= 0) - return (char *)_airports->getAirport(i)->getId().c_str(); - else - return ""; + return i < 0 ? 0 : _content[i]; +} + +void +AirportList::setValue (const char *s) +{ + STD::string filter(s); + if (filter != _filter) { + _filter = filter; + create_list(); + } } // end of AirportList.cxx diff --git a/src/GUI/AirportList.hxx b/src/GUI/AirportList.hxx index 2f3ad87ca..16a8e8522 100644 --- a/src/GUI/AirportList.hxx +++ b/src/GUI/AirportList.hxx @@ -14,13 +14,17 @@ class AirportList : public puList AirportList (int x, int y, int width, int height); virtual ~AirportList (); + virtual void create_list(); + virtual void destroy_list(); + // FIXME: add other string value functions virtual char * getListStringValue (); + virtual void setValue (const char *); private: FGAirportList * _airports; - int _nAirports; char ** _content; + STD::string _filter; }; #endif // __AIRPORTLIST_HXX -- 2.39.5