]> git.mxchange.org Git - flightgear.git/blobdiff - src/GUI/dialog.cxx
Merge branch 'rj/ttw' into next
[flightgear.git] / src / GUI / dialog.cxx
index 6ca629438976e5f8228da16c2627d6960916f225..f540bfec914020c7659d4ff3d15c391a3f37d01a 100644 (file)
@@ -84,22 +84,19 @@ struct GUIInfo
 {
     GUIInfo(FGDialog * d);
     virtual ~GUIInfo();
-    char *format(SGPropertyNode *);
+    void apply_format(SGPropertyNode *);
 
     FGDialog * dialog;
     vector <SGBinding *> bindings;
     int key;
-    char *text;
-    char *fmt_string;
+    string label, legend, text, format;
     format_type fmt_type;
 };
 
-GUIInfo::GUIInfo (FGDialog * d)
-    : dialog(d),
-      key(-1),
-      text(0),
-      fmt_string(0),
-      fmt_type(f_INVALID)
+GUIInfo::GUIInfo (FGDialog * d) :
+    dialog(d),
+    key(-1),
+    fmt_type(f_INVALID)
 {
 }
 
@@ -109,25 +106,24 @@ GUIInfo::~GUIInfo ()
         delete bindings[i];
         bindings[i] = 0;
     }
-    delete [] text;
-    delete [] fmt_string;
 }
 
-char *GUIInfo::format(SGPropertyNode *n)
+void GUIInfo::apply_format(SGPropertyNode *n)
 {
+    char buf[FORMAT_BUFSIZE + 1];
     if (fmt_type == f_INT)
-        snprintf(text, FORMAT_BUFSIZE, fmt_string, n->getIntValue());
+        snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getIntValue());
     else if (fmt_type == f_LONG)
-        snprintf(text, FORMAT_BUFSIZE, fmt_string, n->getLongValue());
+        snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getLongValue());
     else if (fmt_type == f_FLOAT)
-        snprintf(text, FORMAT_BUFSIZE, fmt_string, n->getFloatValue());
+        snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getFloatValue());
     else if (fmt_type == f_DOUBLE)
-        snprintf(text, FORMAT_BUFSIZE, fmt_string, n->getDoubleValue());
+        snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getDoubleValue());
     else
-        snprintf(text, FORMAT_BUFSIZE, fmt_string, n->getStringValue());
+        snprintf(buf, FORMAT_BUFSIZE, format.c_str(), n->getStringValue());
 
-    text[FORMAT_BUFSIZE] = '\0';
-    return text;
+    buf[FORMAT_BUFSIZE] = '\0';
+    text = buf;
 }
 
 
@@ -281,14 +277,21 @@ action_callback (puObject * object)
 static void
 copy_to_pui (SGPropertyNode * node, puObject * object)
 {
+    GUIInfo *info = (GUIInfo *)object->getUserData();
+    if (!info) {
+        SG_LOG(SG_GENERAL, SG_ALERT, "dialog: widget without GUIInfo!");
+        return;   // this can't really happen
+    }
+
     // Treat puText objects specially, so their "values" can be set
     // from properties.
     if (object->getType() & PUCLASS_TEXT) {
-        GUIInfo *info = (GUIInfo *)object->getUserData();
-        if (info && info->fmt_string)
-            object->setLabel(info->format(node));
+        if (info->fmt_type != f_INVALID)
+            info->apply_format(node);
         else
-            object->setLabel(node->getStringValue());
+            info->text = node->getStringValue();
+
+        object->setLabel(info->text.c_str());
         return;
     }
 
@@ -303,7 +306,8 @@ copy_to_pui (SGPropertyNode * node, puObject * object)
         object->setValue(node->getFloatValue());
         break;
     default:
-        object->setValue(node->getStringValue());
+        info->text = node->getStringValue();
+        object->setValue(info->text.c_str());
         break;
     }
 }
@@ -327,17 +331,9 @@ copy_from_pui (puObject * object, SGPropertyNode * node)
         node->setFloatValue(object->getFloatValue());
         break;
     default:
-        // Special case to handle lists, as getStringValue cannot be overridden
-        if(object->getType() & PUCLASS_LIST)
-        {
-            const char *s = ((puList *) object)->getListStringValue();
-            if (s)
-                node->setStringValue(s);
-        }
-        else
-        {
-            node->setStringValue(object->getStringValue());
-        }
+        const char *s = object->getStringValue();
+        if (s)
+            node->setStringValue(s);
         break;
     }
 }
@@ -525,7 +521,7 @@ FGDialog::display (SGPropertyNode * props)
 puObject *
 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
 {
-    if (props->getBoolValue("hide"))
+    if (!props->getBoolValue("enabled", true))
         return 0;
 
     bool presetSize = props->hasValue("width") && props->hasValue("height");
@@ -642,11 +638,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         fgComboBox * obj = new fgComboBox(x, y, x + width, y + height, props,
                            props->getBoolValue("editable", false));
         setupObject(obj, props);
-#ifdef PUCOL_EDITFIELD  // plib > 0.8.4
         setColor(obj, props, EDITFIELD);
-#else
-        setColor(obj, props, FOREGROUND|LABEL);
-#endif
         return obj;
 
     } else if (type == "slider") {
@@ -690,11 +682,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
     } else if (type == "select") {
         fgSelectBox * obj = new fgSelectBox(x, y, x + width, y + height, props);
         setupObject(obj, props);
-#ifdef PUCOL_EDITFIELD  // plib > 0.8.4
         setColor(obj, props, EDITFIELD);
-#else
-        setColor(obj, props, FOREGROUND|LABEL);
-#endif
         return obj;
     } else {
         return 0;
@@ -704,14 +692,21 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
 void
 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
 {
-    string type = props->getName();
+    GUIInfo *info = new GUIInfo(this);
+    object->setUserData(info);
+    _info.push_back(info);
     object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
+    object->makeReturnDefault(props->getBoolValue("default"));
 
-    if (props->hasValue("legend"))
-        object->setLegend(props->getStringValue("legend"));
+    if (props->hasValue("legend")) {
+        info->legend = props->getStringValue("legend");
+        object->setLegend(info->legend.c_str());
+    }
 
-    if (props->hasValue("label"))
-        object->setLabel(props->getStringValue("label"));
+    if (props->hasValue("label")) {
+        info->label = props->getStringValue("label");
+        object->setLabel(info->label.c_str());
+    }
 
     if (props->hasValue("border"))
         object->setBorderThickness( props->getIntValue("border", 2) );
@@ -740,8 +735,7 @@ FGDialog::setupObject (puObject * object, SGPropertyNode * props)
 
     SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
     vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
-    if (type == "text" || bindings.size() > 0) {
-        GUIInfo * info = new GUIInfo(this);
+    if (bindings.size() > 0) {
         info->key = props->getIntValue("keynum", -1);
         if (props->hasValue("key"))
             info->key = getKeyCode(props->getStringValue("key", ""));
@@ -761,30 +755,23 @@ FGDialog::setupObject (puObject * object, SGPropertyNode * props)
             info->bindings.push_back(new SGBinding(binding, globals->get_props()));
         }
         object->setCallback(action_callback);
+    }
 
-        if (type == "input" && props->getBoolValue("live"))
-            object->setDownCallback(action_callback);
-
-        if (type == "text") {
-            const char *format = props->getStringValue("format", 0);
-            if (format) {
-                info->fmt_type = validate_format(props->getStringValue("format", 0));
-                if (info->fmt_type != f_INVALID) {
-                    info->text = new char[FORMAT_BUFSIZE + 1];
-                    info->fmt_string = new char[strlen(format) + 1];
-                    strcpy(info->fmt_string, format);
-                } else {
-                    SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
-                            << format << '\'');
-                }
-            }
+    string type = props->getName();
+    if (type == "input" && props->getBoolValue("live"))
+        object->setDownCallback(action_callback);
+
+    if (type == "text") {
+        const char *format = props->getStringValue("format", 0);
+        if (format) {
+            info->fmt_type = validate_format(format);
+            if (info->fmt_type != f_INVALID)
+                info->format = format;
+            else
+                SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
+                        << format << '\'');
         }
-
-        object->setUserData(info);
-        _info.push_back(info);
     }
-
-    object->makeReturnDefault(props->getBoolValue("default"));
 }
 
 void
@@ -831,9 +818,7 @@ FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
         { LABEL,      PUCOL_LABEL,      "label",      "color-label" },
         { LEGEND,     PUCOL_LEGEND,     "legend",     "color-legend" },
         { MISC,       PUCOL_MISC,       "misc",       "color-misc" },
-#ifdef PUCOL_EDITFIELD  // plib > 0.8.4
         { EDITFIELD,  PUCOL_EDITFIELD,  "editfield",  "color-editfield" },
-#endif
     };
 
     const int numcol = sizeof(pucol) / sizeof(pucol[0]);
@@ -1019,7 +1004,9 @@ void
 fgList::update()
 {
     fgValueList::update();
+    int top = getTopItem();
     newList(_list);
+    setTopItem(top);
 }
 
 // end of dialog.cxx