]> git.mxchange.org Git - flightgear.git/blobdiff - src/GUI/new_gui.cxx
Melchior: Make line wrapping in textboxes configurable, and enable it by default
[flightgear.git] / src / GUI / new_gui.cxx
index 21c6dca0bb44b384c34ce2e1a292948bcafb6e5e..fce518055bbf85648093610d04729d5d3a5479e6 100644 (file)
@@ -5,12 +5,15 @@
 #include <plib/pu.h>
 #include <plib/ul.h>
 
-#include <simgear/misc/exception.hxx>
+#include <simgear/compiler.h>
+#include <simgear/structure/exception.hxx>
+
 #include <Main/fg_props.hxx>
 
 #include "menubar.hxx"
 #include "dialog.hxx"
 
+SG_USING_STD(map);
 
 \f
 ////////////////////////////////////////////////////////////////////////
 
 NewGUI::NewGUI ()
     : _menubar(new FGMenuBar),
-      _current_widget(0)
+      _active_dialog(0)
 {
 }
 
 NewGUI::~NewGUI ()
 {
-    delete _menubar;
+    clear();
 }
 
 void
@@ -34,12 +37,20 @@ NewGUI::init ()
 {
     char path1[1024];
     char path2[1024];
-    ulMakePath(path1, getenv("FG_ROOT"), "gui");
+    ulMakePath(path1, globals->get_fg_root().c_str(), "gui");
     ulMakePath(path2, path1, "dialogs");
     readDir(path2);
-#if !defined(FG_OLD_MENUBAR)
     _menubar->init();
-#endif
+}
+
+void
+NewGUI::reinit ()
+{
+    unbind();
+    clear();
+    _menubar = new FGMenuBar;
+    init();
+    bind();
 }
 
 void
@@ -58,28 +69,70 @@ NewGUI::unbind ()
 void
 NewGUI::update (double delta_time_sec)
 {
-    // NO OP
+    map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
+    for(/**/; iter != _active_dialogs.end(); iter++)
+        iter->second->update();
 }
 
-void
-NewGUI::display (const string &name)
+bool
+NewGUI::showDialog (const string &name)
 {
-    if (_widgets.find(name) == _widgets.end())
+    if (_dialog_props.find(name) == _dialog_props.end()) {
         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
-    else
-        new FGDialog(_widgets[name]);
+        return false;
+    } else {
+        if(!_active_dialogs[name])
+            _active_dialogs[name] = new FGDialog(_dialog_props[name]);
+        return true;
+    }
+}
+
+bool
+NewGUI::closeActiveDialog ()
+{
+    if (_active_dialog == 0)
+        return false;
+
+    // Kill any entries in _active_dialogs...  Is there an STL
+    // algorithm to do (delete map entries by value, not key)?  I hate
+    // the STL :) -Andy
+    map<string,FGDialog *>::iterator iter = _active_dialogs.begin();
+    for(/**/; iter != _active_dialogs.end(); iter++) {
+        if(iter->second == _active_dialog) {
+            _active_dialogs.erase(iter);
+            // iter is no longer valid
+            break;
+        }
+    }
+
+    delete _active_dialog;
+    _active_dialog = 0;
+    return true;
+}
+
+bool
+NewGUI::closeDialog (const string& name)
+{
+    if(_active_dialogs.find(name) != _active_dialogs.end()) {
+        if(_active_dialog == _active_dialogs[name])
+            _active_dialog = 0;
+        delete _active_dialogs[name];
+        _active_dialogs.erase(name);
+        return true;
+    }
+    return false; // dialog wasn't open...
 }
 
 void
-NewGUI::setCurrentWidget (FGDialog * widget)
+NewGUI::setActiveDialog (FGDialog * dialog)
 {
-    _current_widget = widget;
+    _active_dialog = dialog;
 }
 
 FGDialog *
-NewGUI::getCurrentWidget ()
+NewGUI::getActiveDialog ()
 {
-    return _current_widget;
+    return _active_dialog;
 }
 
 FGMenuBar *
@@ -103,6 +156,40 @@ NewGUI::setMenuBarVisible (bool visible)
         _menubar->hide();
 }
 
+void
+NewGUI::clear ()
+{
+    delete _menubar;
+    _menubar = 0;
+    _dialog_props.clear();
+}
+
+static bool
+test_extension (const char * path, const char * ext)
+{
+    int pathlen = strlen(path);
+    int extlen = strlen(ext);
+    
+    for (int i = 1; i <= pathlen && i <= extlen; i++) {
+        if (path[pathlen-i] != ext[extlen-i])
+            return false;
+    }
+    return true;
+}
+
+void
+NewGUI::newDialog (SGPropertyNode* props)
+{
+    const char* cname = props->getStringValue("name");
+    if(!cname) {
+        SG_LOG(SG_GENERAL, SG_ALERT, "New dialog has no <name> property");
+        return;
+    }
+    string name = cname;
+    if(!_active_dialogs[name])
+        _dialog_props[name] = props;
+}
+
 void
 NewGUI::readDir (const char * path)
 {
@@ -114,32 +201,37 @@ NewGUI::readDir (const char * path)
         return;
     }
 
-    ulDirEnt * dirEnt = ulReadDir(dir);
-    while (dirEnt != 0) {
+    for (ulDirEnt * dirEnt = ulReadDir(dir);
+         dirEnt != 0;
+         dirEnt = ulReadDir(dir)) {
+
         char subpath[1024];
 
         ulMakePath(subpath, path, dirEnt->d_name);
 
-        if (dirEnt->d_isdir && dirEnt->d_name[0] != '.') {
-            readDir(subpath);
-        } else {
-            SGPropertyNode_ptr props = new SGPropertyNode;
+        if (!dirEnt->d_isdir && test_extension(subpath, ".xml")) {
+            SGPropertyNode * props = new SGPropertyNode;
             try {
                 readProperties(subpath, props);
-            } catch (const sg_exception &ex) {
-                SG_LOG(SG_INPUT, SG_ALERT, "Error parsing GUI file "
+            } catch (const sg_exception &) {
+                SG_LOG(SG_INPUT, SG_ALERT, "Error parsing dialog "
                        << subpath);
+                delete props;
+                continue;
             }
-            if (!props->hasValue("name")) {
-                SG_LOG(SG_INPUT, SG_WARN, "GUI file " << subpath
+            SGPropertyNode *nameprop = props->getNode("name");
+            if (!nameprop) {
+                SG_LOG(SG_INPUT, SG_WARN, "dialog " << subpath
                    << " has no name; skipping.");
-            } else {
-                string name = props->getStringValue("name");
-                SG_LOG(SG_INPUT, SG_BULK, "Saving GUI node " << name);
-                _widgets[name] = props;
+                delete props;
+                continue;
             }
+            string name = nameprop->getStringValue();
+            if (_dialog_props[name])
+                delete (SGPropertyNode *)_dialog_props[name];
+
+            _dialog_props[name] = props;
         }
-        dirEnt = ulReadDir(dir);
     }
     ulCloseDir(dir);
 }