]> git.mxchange.org Git - flightgear.git/commitdiff
support filtering to show only a subset of entries
authormfranz <mfranz>
Fri, 31 Mar 2006 10:17:43 +0000 (10:17 +0000)
committermfranz <mfranz>
Fri, 31 Mar 2006 10:17:43 +0000 (10:17 +0000)
src/GUI/AirportList.cxx
src/GUI/AirportList.hxx

index 5b28e6bf29fc4fbd6213e64f197d0e95079b8e71..9e2496c1e5e184cd214360820d8c4e12e6646dec 100644 (file)
@@ -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
index 2f3ad87ca89681c8e9b1f3b17a9846f01e003564..16a8e8522be91cbf08919813a481c16d74e97658 100644 (file)
@@ -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