]> git.mxchange.org Git - flightgear.git/blobdiff - src/GUI/dialog.cxx
Melchior: Make line wrapping in textboxes configurable, and enable it by default
[flightgear.git] / src / GUI / dialog.cxx
index 00fc3de8bec6ef80aa0136276c01c2677d1fcbc3..e67f972eaae4ed86660039e345ac17ac192a0899 100644 (file)
@@ -1,5 +1,7 @@
 // dialog.cxx: implementation of an XML-configurable dialog box.
 
+#include <stdlib.h>            // atof()
+
 #include <Input/input.hxx>
 
 #include "dialog.hxx"
@@ -18,20 +20,15 @@ int fgPopup::checkHit(int button, int updown, int x, int y)
     // tells us is that the pointer is inside the dialog.  So do the
     // intersection test (again) to make sure we don't start a drag
     // when inside controls.
-    if(!result) return result;
-    puObject* child = getFirstChild();
-    if(child) child = child->getNextObject(); // Skip the puFrame
-    while(child) {
-        int cx, cy, cw, ch;
-        child->getAbsolutePosition(&cx, &cy);
-        child->getSize(&cw, &ch);
-        if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
+
+    if(updown == PU_DOWN && !_dragging) {
+        if(!result)
+            return 0;
+
+        int hit = getHitObjects(this, x, y);
+        if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
             return result;
-        child = child->getNextObject();
-    }
 
-    // Finally, handle the mouse event
-    if(updown == PU_DOWN) {
         int px, py;
         getPosition(&px, &py);
         _dragging = true;
@@ -42,9 +39,26 @@ int fgPopup::checkHit(int button, int updown, int x, int y)
     } else {
         _dragging = false;
     }
-    return 1;
+    return result;
 }
 
+int fgPopup::getHitObjects(puObject *object, int x, int y)
+{
+    int type = 0;
+    if(object->getType() & PUCLASS_GROUP)
+        for (puObject *obj = ((puGroup *)object)->getFirstChild();
+                obj; obj = obj->getNextObject())
+            type |= getHitObjects(obj, x, y);
+
+    int cx, cy, cw, ch;
+    object->getAbsolutePosition(&cx, &cy);
+    object->getSize(&cw, &ch);
+    if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
+        type |= object->getType();
+    return type;
+}
+
+
 \f
 ////////////////////////////////////////////////////////////////////////
 // Callbacks.
@@ -82,6 +96,71 @@ action_callback (puObject * object)
 }
 
 
+static void
+format_callback(puObject *obj, int dx, int dy, void *n)
+{
+    SGPropertyNode *node = (SGPropertyNode *)n;
+    const char *format = node->getStringValue("format"), *f = format;
+    bool number, l = false;
+    // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
+    for (; *f; f++) {
+        if (*f == '%') {
+            if (f[1] == '%')
+                f++;
+            else
+                break;
+        }
+    }
+    if (*f++ != '%')
+        return;
+    if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
+        f++;
+    while (*f && isdigit(*f))
+        f++;
+    if (*f == '.') {
+        f++;
+        while (*f && isdigit(*f))
+            f++;
+    }
+    if (*f == 'l')
+        l = true, f++;
+
+    if (*f == 'f')
+        number = true;
+    else if (*f == 's') {
+        if (l)
+            return;
+        number = false;
+    } else
+        return;
+
+    for (++f; *f; f++) {
+        if (*f == '%') {
+            if (f[1] == '%')
+                f++;
+            else
+                return;
+        }
+    }
+
+    char buf[256];
+    const char *src = obj->getLabel();
+
+    if (number) {
+        float value = atof(src);
+        snprintf(buf, 256, format, value);
+    } else {
+        snprintf(buf, 256, format, src);
+    }
+
+    buf[255] = '\0';
+
+    SGPropertyNode *result = node->getNode("formatted", true);
+    result->setStringValue(buf);
+    obj->setLabel(result->getStringValue());
+}
+
+
 \f
 ////////////////////////////////////////////////////////////////////////
 // Static helper functions.
@@ -249,8 +328,13 @@ FGDialog::applyValues ()
 void
 FGDialog::update ()
 {
-    for (unsigned int i = 0; i < _liveObjects.size(); i++)
-        copy_to_pui(_liveObjects[i]->node, _liveObjects[i]->object);
+    for (unsigned int i = 0; i < _liveObjects.size(); i++) {
+        puObject *obj = _liveObjects[i]->object;
+        if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
+            continue;
+
+        copy_to_pui(_liveObjects[i]->node, obj);
+    }
 }
 
 void
@@ -306,6 +390,15 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
     int x = props->getIntValue("x", (parentWidth - width) / 2);
     int y = props->getIntValue("y", (parentHeight - height) / 2);
 
+    sgVec4 color = {0.8, 0.8, 0.9, 0.85};
+    SGPropertyNode *ncs = props->getNode("color", false);
+    if ( ncs ) {
+       color[0] = ncs->getFloatValue("red", 0.8);
+       color[1] = ncs->getFloatValue("green", 0.8);
+       color[2] = ncs->getFloatValue("blue", 0.9);
+       color[3] = ncs->getFloatValue("alpha", 0.85);
+    }
+
     string type = props->getName();
     if (type == "")
         type = "dialog";
@@ -316,11 +409,11 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
             dialog = new puDialogBox(x, y);
         else
             dialog = new fgPopup(x, y);
-        setupGroup(dialog, props, width, height, true);
+        setupGroup(dialog, props, width, height, color, true);
         return dialog;
     } else if (type == "group") {
         puGroup * group = new puGroup(x, y);
-        setupGroup(group, props, width, height, false);
+        setupGroup(group, props, width, height, color, false);
         return group;
     } else if (type == "list") {
         puList * list = new puList(x, y, x + width, y + height);
@@ -337,6 +430,14 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
     } else if (type == "text") {
         puText * text = new puText(x, y);
         setupObject(text, props);
+
+        if (props->getNode("format")) {
+            SGPropertyNode *live = props->getNode("live");
+            if (live && live->getBoolValue())
+                text->setRenderCallback(format_callback, props);
+            else
+                format_callback(text, x, y, props);
+        }
         // Layed-out objects need their size set, and non-layout ones
         // get a different placement.
         if(presetSize) text->setSize(width, height);
@@ -395,9 +496,10 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         return dial;
     } else if (type == "textbox") {
        int slider_width = props->getIntValue("slider", parentHeight);
+       int wrap = props->getBoolValue("wrap", true)==true;
        if (slider_width==0) slider_width=20;
        puLargeInput * puTextBox =
-                     new puLargeInput(x, y, x+width, x+height, 2, slider_width);
+                     new puLargeInput(x, y, x+width, x+height, 2, slider_width, wrap);
        if  (props->hasValue("editable"))
        {
           if (props->getBoolValue("editable")==false)
@@ -469,13 +571,13 @@ FGDialog::setupObject (puObject * object, SGPropertyNode * props)
 
 void
 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
-                    int width, int height, bool makeFrame)
+                    int width, int height, sgVec4 color, bool makeFrame)
 {
     setupObject(group, props);
 
     if (makeFrame) {
         puFrame* f = new puFrame(0, 0, width, height);
-        f->setColorScheme(0.8, 0.8, 0.9, 0.85);
+        f->setColorScheme(color[0], color[1], color[2], color[3]);
     }
 
     int nChildren = props->nChildren();