]> git.mxchange.org Git - flightgear.git/blobdiff - src/GUI/dialog.cxx
<input> elements that are <live> always update their input field from the
[flightgear.git] / src / GUI / dialog.cxx
index a76dbe1c782fb545ebea879522ecd7f6527161cb..30c3004036330e120371c2cebb8d21d2d289ec54 100644 (file)
@@ -1,8 +1,13 @@
 // dialog.cxx: implementation of an XML-configurable dialog box.
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include <stdlib.h>            // atof()
 
 #include <Input/input.hxx>
+#include <Scripting/NasalSys.hxx>
 
 #include "dialog.hxx"
 #include "new_gui.hxx"
@@ -299,7 +304,9 @@ copy_from_pui (puObject * object, SGPropertyNode * node)
         // Special case to handle lists, as getStringValue cannot be overridden
         if(object->getType() & PUCLASS_LIST)
         {
-            node->setStringValue(((puList *) object)->getListStringValue());
+            const char *s = ((puList *) object)->getListStringValue();
+            if (s)
+                node->setStringValue(s);
         }
         else
         {
@@ -317,21 +324,37 @@ copy_from_pui (puObject * object, SGPropertyNode * node)
 
 FGDialog::FGDialog (SGPropertyNode * props)
     : _object(0),
-      _gui((NewGUI *)globals->get_subsystem("gui"))
+      _gui((NewGUI *)globals->get_subsystem("gui")),
+      _props(props)
 {
-    char* envp = ::getenv( "FG_FONTS" );
-    if ( envp != NULL ) {
-        _font_path.set( envp );
-    } else {
-        _font_path.set( globals->get_fg_root() );
-        _font_path.append( "Fonts" );
+    _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
+    SGPropertyNode *nasal = props->getNode("nasal");
+    if (nasal) {
+        _nasal_close = nasal->getNode("close");
+        SGPropertyNode *open = nasal->getNode("open");
+        if (open) {
+            const char *s = open->getStringValue();
+            FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
+            nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s));
+        }
     }
-
     display(props);
 }
 
 FGDialog::~FGDialog ()
 {
+    int x, y;
+    _object->getAbsolutePosition(&x, &y);
+    _props->setIntValue("lastx", x);
+    _props->setIntValue("lasty", y);
+
+    FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
+    if (_nasal_close) {
+        const char *s = _nasal_close->getStringValue();
+        nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s));
+    }
+    nas->deleteModule(_module.c_str());
+
     puDeleteObject(_object);
 
     unsigned int i;
@@ -428,8 +451,14 @@ FGDialog::display (SGPropertyNode * props)
     // Let the layout widget work in the same property subtree.
     LayoutWidget wid(props);
 
-    puFont *fnt = _gui->getDefaultFont();
-    wid.setDefaultFont(fnt, int(fnt->getPointSize()));
+    SGPropertyNode *fontnode = props->getNode("font");
+    if (fontnode) {
+        FGFontCache *fc = _gui->get_fontcache();
+        _font = fc->get(fontnode);
+    } else {
+        _font = _gui->getDefaultFont();
+    }
+    wid.setDefaultFont(_font, int(_font->getPointSize()));
 
     int pw=0, ph=0;
     int px, py, savex, savey;
@@ -442,9 +471,9 @@ FGDialog::display (SGPropertyNode * props)
 
     // Negative x/y coordinates are interpreted as distance from the top/right
     // corner rather than bottom/left.
-    if (px < 0)
+    if (userx && px < 0)
         px = screenw - pw + px;
-    if (py < 0)
+    if (usery && py < 0)
         py = screenh - ph + py;
 
     // Define "x", "y", "width" and/or "height" in the property tree if they
@@ -530,13 +559,18 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         for (unsigned int i = 0; i < value_nodes.size(); i++)
             entries[i] = strdup((char *)value_nodes[i]->getStringValue());
 
-        puList * obj = new puList(x, y, x + width, y + height, entries);
+        int slider_width = props->getIntValue("slider", 20);
+        puList * obj = new puList(x, y, x + width, y + height, entries, slider_width);
+        if (presetSize)
+            obj->setSize(width, height);
         setupObject(obj, props);
         setColor(obj, props);
         return obj;
 
     } else if (type == "airport-list") {
         AirportList * obj = new AirportList(x, y, x + width, y + height);
+        if (presetSize)
+            obj->setSize(width, height);
         setupObject(obj, props);
         setColor(obj, props);
         return obj;
@@ -560,8 +594,10 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         }
         // Layed-out objects need their size set, and non-layout ones
         // get a different placement.
-        if(presetSize) obj->setSize(width, height);
-        else obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
+        if (presetSize)
+            obj->setSize(width, height);
+        else
+            obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
         setColor(obj, props, LABEL);
         return obj;
 
@@ -586,7 +622,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
             obj = new puOneShot(x, y, legend);
         else
             obj = new puButton(x, y, legend);
-        if(presetSize)
+        if (presetSize)
             obj->setSize(width, height);
         setupObject(obj, props);
         setColor(obj, props);
@@ -614,7 +650,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         obj->setMinValue(props->getFloatValue("min", 0.0));
         obj->setMaxValue(props->getFloatValue("max", 1.0));
         setupObject(obj, props);
-        if(presetSize)
+        if (presetSize)
             obj->setSize(width, height);
         setColor(obj, props, FOREGROUND|LABEL);
         return obj;
@@ -629,9 +665,8 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         return obj;
 
     } else if (type == "textbox") {
-        int slider_width = props->getIntValue("slider", parentHeight);
+        int slider_width = props->getIntValue("slider", 20);
         int wrap = props->getBoolValue("wrap", true);
-        if (slider_width==0) slider_width=20;
         puLargeInput * obj = new puLargeInput(x, y,
                 x+width, x+height, 2, slider_width, wrap);
 
@@ -641,6 +676,8 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
             else
                 obj->enableInput();
         }
+        if (presetSize)
+            obj->setSize(width, height);
         setupObject(obj, props);
         setColor(obj, props, FOREGROUND|LABEL);
         return obj;
@@ -674,6 +711,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
 void
 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
 {
+    string type = props->getName();
     object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
 
     if (props->hasValue("legend"))
@@ -689,6 +727,8 @@ FGDialog::setupObject (puObject * object, SGPropertyNode * props)
        FGFontCache *fc = _gui->get_fontcache();
        puFont *lfnt = fc->get(nft);
        object->setLabelFont(*lfnt);
+    } else {
+       object->setLabelFont(*_font);
     }
 
     if (props->hasValue("property")) {
@@ -718,11 +758,19 @@ FGDialog::setupObject (puObject * object, SGPropertyNode * props)
             while (dest->getChild("binding", j))
                 j++;
 
+            const char *cmd = bindings[i]->getStringValue("command");
+            if (!strcmp(cmd, "nasal"))
+                bindings[i]->setStringValue("module", _module.c_str());
+
             binding = dest->getChild("binding", j, true);
             copyProperties(bindings[i], binding);
             info->bindings.push_back(new FGBinding(binding));
         }
         object->setCallback(action_callback);
+
+        if (type == "input" && props->getBoolValue("live"))
+            object->setDownCallback(action_callback);
+
         object->setUserData(info);
         _info.push_back(info);
     }
@@ -803,6 +851,7 @@ static struct {
     const char *name;
     int key;
 } keymap[] = {
+    {"backspace", 8},
     {"tab", 9},
     {"return", 13},
     {"enter", 13},