]> git.mxchange.org Git - flightgear.git/blobdiff - src/GUI/AirportList.cxx
show verbose mode (see $FG_ROOT/Docs/README.gui -> property-list)
[flightgear.git] / src / GUI / AirportList.cxx
index 5b28e6bf29fc4fbd6213e64f197d0e95079b8e71..133b2b9ea65f147f20c88880f795e64d5f495df6 100644 (file)
@@ -1,54 +1,87 @@
-#include <string.h>    // strncpy()
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
 
+#include <locale>
 #include <Main/globals.hxx>
 #include <Airports/simple.hxx>
 
 #include "AirportList.hxx"
 
 
-AirportList::AirportList (int x, int y, int width, int height)
-    : puList(x, y, width, height)
+AirportList::AirportList(int x, int y, int width, int height) :
+    puaList(x, y, width, height),
+    GUI_ID(FGCLASS_AIRPORTLIST),
+    _airports(globals->get_airports()),
+    _content(0)
 {
-    char buf[1024];
+    create_list();
+}
+
+
+AirportList::~AirportList()
+{
+    destroy_list();
+}
+
+
+void
+AirportList::create_list()
+{
+    const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(std::locale());
+    int num_apt = _airports->size();
+    char **content = new char *[num_apt + 1];
 
-    _airports = globals->get_airports();
-    _nAirports = _airports->size();
+    int n = 0;
+    for (int i = 0; i < num_apt; i++) {
+        const FGAirport *apt = _airports->getAirport(i);
+        std::string entry(' ' + apt->getName() + "   (" + apt->getId() + ')');
 
-    _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());
+        if (!_filter.empty()) {
+            std::string upper(entry.data());
+            ct.toupper((char *)upper.data(), (char *)upper.data() + upper.size());
 
-        unsigned int buf_len = (strlen(buf) > 1023) ? 1023 : strlen(buf);
+            if (upper.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;
-    newList(_content);
+    content[n] = 0;
+    // work around plib 2006/04/18 bug: lists with no entries cause crash on arrow-up
+    newList(n > 0 ? content : 0);
+
+    if (_content)
+        destroy_list();
+
+    _content = 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;
 }
 
-char *
-AirportList::getListStringValue ()
+
+void
+AirportList::setValue(const char *s)
 {
-    int i = getListIntegerValue();
-    if (i >= 0)
-        return (char *)_airports->getAirport(i)->getId().c_str();
-    else
-        return "";
+    std::string filter(s);
+    const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(std::locale());
+    ct.toupper((char *)filter.data(), (char *)filter.data() + filter.size());
+
+    if (filter != _filter) {
+        _filter = filter;
+        create_list();
+    }
 }
 
-// end of AirportList.cxx