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;
}
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
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