]> 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 35e573d91ff12451fc21adaf40f9f31dc1cf478c..ca8e19753f53224aa22df321ec4bcb5d24d1024a 100644 (file)
@@ -59,7 +59,13 @@ int fgPopup::checkKey(int key, int updown)
     if (!object)
         return puPopup::checkKey(key, updown);
 
-    object->invokeCallback() ;
+    // 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;
 }
 
@@ -74,18 +80,19 @@ puObject *fgPopup::getKeyObject(puObject *object, int key)
 
     GUIInfo *info = (GUIInfo *)object->getUserData();
     if (info && info->key == key)
-       return object;
+        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 (getActiveInputField(obj))
-                return obj;
+            if ((ret = getActiveInputField(obj)))
+                return ret;
 
     if (object->getType() & PUCLASS_INPUT && ((puInput *)object)->isAcceptingInput())
         return object;
@@ -425,12 +432,20 @@ FGDialog::display (SGPropertyNode * props)
     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.
@@ -441,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");
 
@@ -684,6 +707,8 @@ FGDialog::setupObject (puObject * object, SGPropertyNode * props)
     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;
@@ -766,6 +791,100 @@ FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
     }
 }
 
+
+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)
+{
+    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;
+            }
+        }
+    }
+    delete[] buf;
+    return key;
+}
+
 char **
 FGDialog::make_char_array (int size)
 {