]> git.mxchange.org Git - flightgear.git/blobdiff - src/GUI/dialog.cxx
don't destroy iterated map entries; delete _menubar; restore closed
[flightgear.git] / src / GUI / dialog.cxx
index 874a842549e7b7f4a2a60794aca706cd00479c94..ca8e19753f53224aa22df321ec4bcb5d24d1024a 100644 (file)
 #include "AirportList.hxx"
 #include "layout.hxx"
 
+////////////////////////////////////////////////////////////////////////
+// Implementation of GUIInfo.
+////////////////////////////////////////////////////////////////////////
+
+/**
+ * User data for a GUI object.
+ */
+struct GUIInfo
+{
+    GUIInfo (FGDialog * d);
+    virtual ~GUIInfo ();
+
+    FGDialog * dialog;
+    vector <FGBinding *> bindings;
+    int key;
+};
+
+GUIInfo::GUIInfo (FGDialog * d)
+    : dialog(d),
+      key(-1)
+{
+}
+
+GUIInfo::~GUIInfo ()
+{
+    for (unsigned int i = 0; i < bindings.size(); i++) {
+        delete bindings[i];
+        bindings[i] = 0;
+    }
+}
+
+\f
+/**
+ * Key handler.
+ */
+int fgPopup::checkKey(int key, int updown)
+{
+    if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
+        return false;
+
+    puObject *input = getActiveInputField(this);
+    if (input)
+        return input->checkKey(key, updown);
+
+    puObject *object = getKeyObject(this, key);
+    if (!object)
+        return puPopup::checkKey(key, updown);
+
+    // invokeCallback() isn't enough; we need to simulate a mouse button press
+    object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
+            (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
+            (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
+    object->checkHit(PU_LEFT_BUTTON, PU_UP,
+            (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
+            (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
+    return true;
+}
+
+puObject *fgPopup::getKeyObject(puObject *object, int key)
+{
+    puObject *ret;
+    if(object->getType() & PUCLASS_GROUP)
+        for (puObject *obj = ((puGroup *)object)->getFirstChild();
+                obj; obj = obj->getNextObject())
+            if ((ret = getKeyObject(obj, key)))
+                return ret;
+
+    GUIInfo *info = (GUIInfo *)object->getUserData();
+    if (info && info->key == key)
+        return object;
+
+    return 0;
+}
+
+puObject *fgPopup::getActiveInputField(puObject *object)
+{
+    puObject *ret;
+    if(object->getType() & PUCLASS_GROUP)
+        for (puObject *obj = ((puGroup *)object)->getFirstChild();
+                obj; obj = obj->getNextObject())
+            if ((ret = getActiveInputField(obj)))
+                return ret;
+
+    if (object->getType() & PUCLASS_INPUT && ((puInput *)object)->isAcceptingInput())
+        return object;
+
+    return 0;
+}
+
+/**
+ * Mouse handler.
+ */
 int fgPopup::checkHit(int button, int updown, int x, int y)
 {
     int result = puPopup::checkHit(button, updown, x, y);
@@ -67,19 +159,6 @@ int fgPopup::getHitObjects(puObject *object, int x, int y)
 // Callbacks.
 ////////////////////////////////////////////////////////////////////////
 
-/**
- * User data for a GUI object.
- */
-struct GUIInfo
-{
-    GUIInfo (FGDialog * d);
-    virtual ~GUIInfo ();
-
-    FGDialog * dialog;
-    vector <FGBinding *> bindings;
-};
-
-
 /**
  * Action callback.
  */
@@ -231,25 +310,6 @@ copy_from_pui (puObject * object, SGPropertyNode * node)
 }
 
 
-\f
-////////////////////////////////////////////////////////////////////////
-// Implementation of GUIInfo.
-////////////////////////////////////////////////////////////////////////
-
-GUIInfo::GUIInfo (FGDialog * d)
-    : dialog(d)
-{
-}
-
-GUIInfo::~GUIInfo ()
-{
-    for (unsigned int i = 0; i < bindings.size(); i++) {
-        delete bindings[i];
-        bindings[i] = 0;
-    }
-}
-
-
 \f
 ////////////////////////////////////////////////////////////////////////
 // Implementation of FGDialog.
@@ -365,19 +425,27 @@ FGDialog::display (SGPropertyNode * props)
     bool userw = props->hasValue("width");
     bool userh = props->hasValue("height");
 
-    // Expand some elements. Has to be done before the layouter sees them.
-//    preprocess(props);
-
     // Let the layout widget work in the same property subtree.
     LayoutWidget wid(props);
 
+    puFont *fnt = _gui->getDefaultFont();
+    wid.setDefaultFont(fnt, int(fnt->getPointSize()));
+
     int pw=0, ph=0;
+    int px, py, savex, savey;
     if(!userw || !userh)
         wid.calcPrefSize(&pw, &ph);
     pw = props->getIntValue("width", pw);
     ph = props->getIntValue("height", ph);
-    int px = props->getIntValue("x", (screenw - pw) / 2);
-    int py = props->getIntValue("y", (screenh - ph) / 2);
+    px = savex = props->getIntValue("x", (screenw - pw) / 2);
+    py = savey = props->getIntValue("y", (screenh - ph) / 2);
+
+    // Negative x/y coordinates are interpreted as distance from the top/right
+    // corner rather than bottom/left.
+    if (px < 0)
+        px = screenw - pw + px;
+    if (py < 0)
+        py = screenh - ph + py;
 
     // Define "x", "y", "width" and/or "height" in the property tree if they
     // are not specified in the configuration file.
@@ -388,9 +456,17 @@ FGDialog::display (SGPropertyNode * props)
     _object = makeObject(props, screenw, screenh);
 
     // Remove automatically generated properties, so the layout looks
-    // the same next time around.
-    if(!userx) props->removeChild("x");
-    if(!usery) props->removeChild("y");
+    // the same next time around, or restore x and y to preserve negative coords.
+    if(userx)
+        props->setIntValue("x", savex);
+    else
+        props->removeChild("x");
+
+    if(usery)
+        props->setIntValue("y", savey);
+    else
+        props->removeChild("y");
+
     if(!userw) props->removeChild("width");
     if(!userh) props->removeChild("height");
 
@@ -406,6 +482,9 @@ FGDialog::display (SGPropertyNode * props)
 puObject *
 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
 {
+    if (props->getBoolValue("hide"))
+        return 0;
+
     bool presetSize = props->hasValue("width") && props->hasValue("height");
     int width = props->getIntValue("width", parentWidth);
     int height = props->getIntValue("height", parentHeight);
@@ -424,43 +503,43 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         else
             obj = new fgPopup(x, y, draggable);
         setupGroup(obj, props, width, height, true);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props);
         return obj;
 
     } else if (type == "group") {
         puGroup * obj = new puGroup(x, y);
         setupGroup(obj, props, width, height, false);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props);
         return obj;
 
     } else if (type == "frame") {
         puGroup * obj = new puGroup(x, y);
         setupGroup(obj, props, width, height, true);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props);
         return obj;
 
     } else if (type == "hrule" || type == "vrule") {
         puFrame * obj = new puFrame(x, y, x + width, y + height);
         obj->setBorderThickness(0);
-        setColor(obj, props, FOREGROUND);
+        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
         return obj;
 
     } else if (type == "list") {
         puList * obj = new puList(x, y, x + width, y + height);
         setupObject(obj, props);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props);
         return obj;
 
     } else if (type == "airport-list") {
         AirportList * obj = new AirportList(x, y, x + width, y + height);
         setupObject(obj, props);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props);
         return obj;
 
     } else if (type == "input") {
         puInput * obj = new puInput(x, y, x + width, y + height);
         setupObject(obj, props);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props, FOREGROUND|LABEL);
         return obj;
 
     } else if (type == "text") {
@@ -485,14 +564,14 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         puButton * obj;
         obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
         setupObject(obj, props);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props, FOREGROUND|LABEL);
         return obj;
 
     } else if (type == "radio") {
         puButton * obj;
         obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
         setupObject(obj, props);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props, FOREGROUND|LABEL);
         return obj;
 
     } else if (type == "button") {
@@ -505,7 +584,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         if(presetSize)
             obj->setSize(width, height);
         setupObject(obj, props);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props);
         return obj;
 
     } else if (type == "combo") {
@@ -518,7 +597,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
                            props->getBoolValue("editable", false));
         setupObject(obj, props);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props, FOREGROUND|LABEL);
         return obj;
 
     } else if (type == "slider") {
@@ -529,7 +608,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         setupObject(obj, props);
         if(presetSize)
             obj->setSize(width, height);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props, FOREGROUND|LABEL);
         return obj;
 
     } else if (type == "dial") {
@@ -538,7 +617,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         obj->setMaxValue(props->getFloatValue("max", 1.0));
         obj->setWrap(props->getBoolValue("wrap", true));
         setupObject(obj, props);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props, FOREGROUND|LABEL);
         return obj;
 
     } else if (type == "textbox") {
@@ -548,14 +627,14 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         puLargeInput * obj = new puLargeInput(x, y,
                 x+width, x+height, 2, slider_width, wrap);
 
-        if  (props->hasValue("editable")) {
+        if (props->hasValue("editable")) {
             if (props->getBoolValue("editable")==false)
                 obj->disableInput();
             else
                 obj->enableInput();
         }
         setupObject(obj, props);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props, FOREGROUND|LABEL);
         return obj;
 
     } else if (type == "select") {
@@ -574,7 +653,7 @@ FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
         puSelectBox * obj =
             new puSelectBox(x, y, x + width, y + height, entries);
         setupObject(obj, props);
-        setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(obj, props, FOREGROUND|LABEL);
         return obj;
     } else {
         return 0;
@@ -627,6 +706,9 @@ FGDialog::setupObject (puObject * object, SGPropertyNode * props)
     vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
     if (bindings.size() > 0) {
         GUIInfo * info = new GUIInfo(this);
+        info->key = props->getIntValue("keynum", -1);
+        if (props->hasValue("key"))
+            info->key = getKeyCode(props->getStringValue("key", ""));
 
         for (unsigned int i = 0; i < bindings.size(); i++) {
             unsigned int j = 0;
@@ -654,7 +736,7 @@ FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
 
     if (makeFrame) {
         puFrame* f = new puFrame(0, 0, width, height);
-        setColor(f, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
+        setColor(f, props);
     }
 
     int nChildren = props->nChildren();
@@ -667,59 +749,140 @@ void
 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
 {
     string type = props->getName();
-    // first set whole color scheme (see below for a description)
-    FGColor c = _gui->getColor("background");
-    c.merge(_gui->getColor(type));
-    c.merge(props->getNode("color"));
-    object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
-
-    // now look for the 6 plib color qualities
-    const int numcol = 6;
+    if (type == "")
+        type = "dialog";
+
+    FGColor *c = new FGColor(_gui->getColor("background"));
+    c->merge(_gui->getColor(type));
+    c->merge(props->getNode("color"));
+    if (c->isValid())
+        object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
+
     const struct {
         int mask;
         int id;
         const char *name;
-    } pucol[numcol] = {
-        BACKGROUND, PUCOL_BACKGROUND, "background",
-        FOREGROUND, PUCOL_FOREGROUND, "foreground",
-        HIGHLIGHT,  PUCOL_HIGHLIGHT,  "highlight",
-        LABEL,      PUCOL_LABEL,      "label",
-        LEGEND,     PUCOL_LEGEND,     "legend",
-        MISC,       PUCOL_MISC,       "misc",
+        const char *cname;
+    } pucol[] = {
+        { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
+        { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
+        { HIGHLIGHT,  PUCOL_HIGHLIGHT,  "highlight",  "color-highlight" },
+        { LABEL,      PUCOL_LABEL,      "label",      "color-label" },
+        { LEGEND,     PUCOL_LEGEND,     "legend",     "color-legend" },
+        { MISC,       PUCOL_MISC,       "misc",       "color-misc" }
     };
 
+    const int numcol = sizeof(pucol) / sizeof(pucol[0]);
+
     for (int i = 0; i < numcol; i++) {
-        // merge in object color quality components (e.g. "button-background")
-        FGColor c(_gui->getColor(type));
-        c.merge(_gui->getColor(type + '-' + pucol[i].name));
-        if (c.isValid()) {
-            // merge in explicit color components from the XML structure (<color>)
-            if (which & pucol[i].mask)
-                c.merge(props->getNode("color"));
-            object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
-        }
+        bool dirty = false;
+        c->clear();
+        c->setAlpha(1.0);
+
+        dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
+        if (which & pucol[i].mask)
+            dirty |= c->merge(props->getNode("color"));
+
+        if ((pucol[i].mask == LABEL) && !c->isValid())
+            dirty |= c->merge(_gui->getColor("label"));
+
+        if (c->isValid() && dirty)
+            object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
     }
 }
 
-void
-FGDialog::preprocess (SGPropertyNode * prop)
+
+static struct {
+    const char *name;
+    int key;
+} keymap[] = {
+    {"tab", 9},
+    {"return", 13},
+    {"enter", 13},
+    {"esc", 27},
+    {"escape", 27},
+    {"space", ' '},
+    {"&amp;", '&'},
+    {"and", '&'},
+    {"&lt;", '<'},
+    {"&gt;", '>'},
+    {"f1", PU_KEY_F1},
+    {"f2", PU_KEY_F2},
+    {"f3", PU_KEY_F3},
+    {"f4", PU_KEY_F4},
+    {"f5", PU_KEY_F5},
+    {"f6", PU_KEY_F6},
+    {"f7", PU_KEY_F7},
+    {"f8", PU_KEY_F8},
+    {"f9", PU_KEY_F9},
+    {"f10", PU_KEY_F10},
+    {"f11", PU_KEY_F11},
+    {"f12", PU_KEY_F12},
+    {"left", PU_KEY_LEFT},
+    {"up", PU_KEY_UP},
+    {"right", PU_KEY_RIGHT},
+    {"down", PU_KEY_DOWN},
+    {"pageup", PU_KEY_PAGE_UP},
+    {"pagedn", PU_KEY_PAGE_DOWN},
+    {"home", PU_KEY_HOME},
+    {"end", PU_KEY_END},
+    {"insert", PU_KEY_INSERT},
+    {0, -1},
+};
+
+int
+FGDialog::getKeyCode(const char *str)
 {
-    for (int i = 0; i < prop->nChildren(); i++) {
-        SGPropertyNode *child = prop->getChild(i);
-        const char *name = child->getName();
-
-        if (!strcmp(name, "title")) {
-            prop->removeChild("title", child->getIndex(), false);
-            SGPropertyNode *tit = fgGetNode("/sim/gui/title");
-            if (!tit) {
-                fprintf(stderr, "no tits here!\n");
-                continue;
+    enum {
+        CTRL = 0x1,
+        SHIFT = 0x2,
+        ALT = 0x4,
+    };
+
+    while (*str == ' ')
+        str++;
+
+    char *buf = new char[strlen(str) + 1];
+    strcpy(buf, str);
+    char *s = buf + strlen(buf);
+    while (s > str && s[-1] == ' ')
+        s--;
+    *s = 0;
+    s = buf;
+
+    int mod = 0;
+    while (1) {
+        if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
+            s += 5, mod |= CTRL;
+        else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
+            s += 6, mod |= SHIFT;
+        else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
+            s += 4, mod |= ALT;
+        else
+            break;
+    }
+
+    int key = -1;
+    if (strlen(s) == 1 && isascii(*s)) {
+        key = *s;
+        if (mod & SHIFT)
+            key = toupper(key);
+        if (mod & CTRL)
+            key = toupper(key) - 64;
+        if (mod & ALT)
+            ;   // Alt not propagated to the gui
+    } else {
+        for (char *t = s; *t; t++)
+            *t = tolower(*t);
+        for (int i = 0; keymap[i].name; i++) {
+            if (!strcmp(s, keymap[i].name)) {
+                key = keymap[i].key;
+                break;
             }
-            copyProperties(tit, prop->getChild(child->getIndex()));
-            writeProperties(cerr, prop, true);
-        } else
-            preprocess(prop->getChild(i));
+        }
     }
+    delete[] buf;
+    return key;
 }
 
 char **