]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/gui/guiFormSpecMenu.cpp
Add Block Formspec Hack
[dragonfireclient.git] / src / gui / guiFormSpecMenu.cpp
index ed3445b5b58efd49c068040e2180b96ae3751075..601c5c18ee4018f5d14523d1c17a9b81b0776fdc 100644 (file)
@@ -24,8 +24,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <limits>
 #include <sstream>
 #include "guiFormSpecMenu.h"
-#include "guiScrollBar.h"
-#include "guiTable.h"
 #include "constants.h"
 #include "gamedef.h"
 #include "client/keycode.h"
@@ -55,13 +53,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/string.h" // for parseColorString()
 #include "irrlicht_changes/static_text.h"
 #include "client/guiscalingfilter.h"
+#include "guiAnimatedImage.h"
 #include "guiBackgroundImage.h"
 #include "guiBox.h"
 #include "guiButton.h"
+#include "guiButtonImage.h"
+#include "guiButtonItemImage.h"
 #include "guiEditBoxWithScrollbar.h"
+#include "guiInventoryList.h"
 #include "guiItemImage.h"
-#include "guiScrollBar.h"
-#include "guiTable.h"
+#include "guiScrollContainer.h"
 #include "intlGUIEditBox.h"
 #include "guiHyperText.h"
 
@@ -94,29 +95,21 @@ inline u32 clamp_u8(s32 value)
 GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
                gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
                Client *client, ISimpleTextureSource *tsrc, IFormSource *fsrc, TextDest *tdst,
-               const std::string &formspecPrepend,
-               bool remap_dbl_click):
-       GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr),
+               const std::string &formspecPrepend, bool remap_dbl_click):
+       GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr, remap_dbl_click),
        m_invmgr(client),
        m_tsrc(tsrc),
        m_client(client),
        m_formspec_prepend(formspecPrepend),
        m_form_src(fsrc),
        m_text_dst(tdst),
-       m_joystick(joystick),
-       m_remap_dbl_click(remap_dbl_click)
+       m_joystick(joystick)
 {
        current_keys_pending.key_down = false;
        current_keys_pending.key_up = false;
        current_keys_pending.key_enter = false;
        current_keys_pending.key_escape = false;
 
-       m_doubleclickdetect[0].time = 0;
-       m_doubleclickdetect[1].time = 0;
-
-       m_doubleclickdetect[0].pos = v2s32(0, 0);
-       m_doubleclickdetect[1].pos = v2s32(0, 0);
-
        m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
        m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
 }
@@ -128,7 +121,7 @@ GUIFormSpecMenu::~GUIFormSpecMenu()
        for (auto &table_it : m_tables)
                table_it.second->drop();
        for (auto &inventorylist_it : m_inventorylists)
-               inventorylist_it.e->drop();
+               inventorylist_it->drop();
        for (auto &checkbox_it : m_checkboxes)
                checkbox_it.second->drop();
        for (auto &scrollbar_it : m_scrollbars)
@@ -137,6 +130,10 @@ GUIFormSpecMenu::~GUIFormSpecMenu()
                background_it->drop();
        for (auto &tooltip_rect_it : m_tooltip_rects)
                tooltip_rect_it.first->drop();
+       for (auto &clickthrough_it : m_clickthrough_elements)
+               clickthrough_it->drop();
+       for (auto &scroll_container_it : m_scroll_containers)
+               scroll_container_it.second->drop();
 
        delete m_selected_item;
        delete m_form_src;
@@ -345,7 +342,103 @@ void GUIFormSpecMenu::parseContainerEnd(parserData* data)
        }
 }
 
-void GUIFormSpecMenu::parseList(parserData* data, const std::string &element)
+void GUIFormSpecMenu::parseScrollContainer(parserData *data, const std::string &element)
+{
+       std::vector<std::string> parts = split(element, ';');
+
+       if (parts.size() < 4 ||
+                       (parts.size() > 5 && m_formspec_version <= FORMSPEC_API_VERSION)) {
+               errorstream << "Invalid scroll_container start element (" << parts.size()
+                               << "): '" << element << "'" << std::endl;
+               return;
+       }
+
+       std::vector<std::string> v_pos  = split(parts[0], ',');
+       std::vector<std::string> v_geom = split(parts[1], ',');
+       std::string scrollbar_name = parts[2];
+       std::string orientation = parts[3];
+       f32 scroll_factor = 0.1f;
+       if (parts.size() >= 5 && !parts[4].empty())
+               scroll_factor = stof(parts[4]);
+
+       MY_CHECKPOS("scroll_container", 0);
+       MY_CHECKGEOM("scroll_container", 1);
+
+       v2s32 pos = getRealCoordinateBasePos(v_pos);
+       v2s32 geom = getRealCoordinateGeometry(v_geom);
+
+       if (orientation == "vertical")
+               scroll_factor *= -imgsize.Y;
+       else if (orientation == "horizontal")
+               scroll_factor *= -imgsize.X;
+       else
+               warningstream << "GUIFormSpecMenu::parseScrollContainer(): "
+                               << "Invalid scroll_container orientation: " << orientation
+                               << std::endl;
+
+       // old parent (at first: this)
+       // ^ is parent of clipper
+       // ^ is parent of mover
+       // ^ is parent of other elements
+
+       // make clipper
+       core::rect<s32> rect_clipper = core::rect<s32>(pos, pos + geom);
+
+       gui::IGUIElement *clipper = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
+                       data->current_parent, 0, rect_clipper);
+
+       // make mover
+       FieldSpec spec_mover(
+               "",
+               L"",
+               L"",
+               258 + m_fields.size()
+       );
+
+       core::rect<s32> rect_mover = core::rect<s32>(0, 0, geom.X, geom.Y);
+
+       GUIScrollContainer *mover = new GUIScrollContainer(Environment,
+                       clipper, spec_mover.fid, rect_mover, orientation, scroll_factor);
+
+       data->current_parent = mover;
+
+       m_scroll_containers.emplace_back(scrollbar_name, mover);
+
+       m_fields.push_back(spec_mover);
+
+       clipper->drop();
+
+       // remove interferring offset of normal containers
+       container_stack.push(pos_offset);
+       pos_offset.X = 0.0f;
+       pos_offset.Y = 0.0f;
+}
+
+void GUIFormSpecMenu::parseScrollContainerEnd(parserData *data)
+{
+       if (data->current_parent == this || data->current_parent->getParent() == this ||
+                       container_stack.empty()) {
+               errorstream << "Invalid scroll_container end element, "
+                               << "no matching scroll_container start element" << std::endl;
+               return;
+       }
+
+       if (pos_offset.getLengthSQ() != 0.0f) {
+               // pos_offset is only set by containers and scroll_containers.
+               // scroll_containers always set it to 0,0 which means that if it is
+               // not 0,0, it is a normal container that was opened last, not a
+               // scroll_container
+               errorstream << "Invalid scroll_container end element, "
+                               << "an inner container was left open" << std::endl;
+               return;
+       }
+
+       data->current_parent = data->current_parent->getParent()->getParent();
+       pos_offset = container_stack.top();
+       container_stack.pop();
+}
+
+void GUIFormSpecMenu::parseList(parserData *data, const std::string &element)
 {
        if (m_client == 0) {
                warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
@@ -384,38 +477,10 @@ void GUIFormSpecMenu::parseList(parserData* data, const std::string &element)
                        start_i = stoi(startindex);
 
                if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
-                       errorstream<< "Invalid list element: '" << element << "'"  << std::endl;
-                       return;
-               }
-
-               // check for the existence of inventory and list
-               Inventory *inv = m_invmgr->getInventory(loc);
-               if (!inv) {
-                       warningstream << "GUIFormSpecMenu::parseList(): "
-                                       << "The inventory location "
-                                       << "\"" << loc.dump() << "\" doesn't exist"
-                                       << std::endl;
-                       return;
-               }
-               InventoryList *ilist = inv->getList(listname);
-               if (!ilist) {
-                       warningstream << "GUIFormSpecMenu::parseList(): "
-                                       << "The inventory list \"" << listname << "\" "
-                                       << "@ \"" << loc.dump() << "\" doesn't exist"
-                                       << std::endl;
+                       errorstream << "Invalid list element: '" << element << "'"  << std::endl;
                        return;
                }
 
-               // trim geom if it is larger than the actual inventory size
-               s32 list_size = (s32)ilist->getSize();
-               if (list_size < geom.X * geom.Y + start_i) {
-                       list_size -= MYMAX(start_i, 0);
-                       geom.Y = list_size / geom.X;
-                       geom.Y += list_size % geom.X > 0 ? 1 : 0;
-                       if (geom.Y <= 1)
-                               geom.X = list_size;
-               }
-
                if (!data->explicit_size)
                        warningstream << "invalid use of list without a size[] element" << std::endl;
 
@@ -427,33 +492,28 @@ void GUIFormSpecMenu::parseList(parserData* data, const std::string &element)
                        3
                );
 
-               v2s32 pos;
-               core::rect<s32> rect;
+               v2f32 slot_spacing = data->real_coordinates ?
+                               v2f32(imgsize.X * 1.25f, imgsize.Y * 1.25f) : spacing;
 
-               if (data->real_coordinates) {
-                       pos = getRealCoordinateBasePos(v_pos);
-                       rect = core::rect<s32>(pos.X, pos.Y,
-                                       pos.X + (geom.X - 1) * (imgsize.X * 1.25) + imgsize.X,
-                                       pos.Y + (geom.Y - 1) * (imgsize.Y * 1.25) + imgsize.Y);
-               } else {
-                       pos = getElementBasePos(&v_pos);
-                       rect = core::rect<s32>(pos.X, pos.Y,
-                                       pos.X + (geom.X - 1) * spacing.X + imgsize.X,
-                                       pos.Y + (geom.Y - 1) * spacing.Y + imgsize.Y);
-               }
+               v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos)
+                               : getElementBasePos(&v_pos);
 
-               gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
-                               this, spec.fid, rect);
+               core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
+                               pos.X + (geom.X - 1) * slot_spacing.X + imgsize.X,
+                               pos.Y + (geom.Y - 1) * slot_spacing.Y + imgsize.Y);
 
-               m_inventorylists.emplace_back(loc, listname, e, geom, start_i,
-                               data->real_coordinates);
+               GUIInventoryList *e = new GUIInventoryList(Environment, data->current_parent,
+                               spec.fid, rect, m_invmgr, loc, listname, geom, start_i, imgsize,
+                               slot_spacing, this, data->inventorylist_options, m_font);
+
+               m_inventorylists.push_back(e);
                m_fields.push_back(spec);
                return;
        }
        errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'"  << std::endl;
 }
 
-void GUIFormSpecMenu::parseListRing(parserDatadata, const std::string &element)
+void GUIFormSpecMenu::parseListRing(parserData *data, const std::string &element)
 {
        if (m_client == 0) {
                errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
@@ -480,10 +540,10 @@ void GUIFormSpecMenu::parseListRing(parserData* data, const std::string &element
        if (element.empty() && m_inventorylists.size() > 1) {
                size_t siz = m_inventorylists.size();
                // insert the last two inv list elements into the list ring
-               const ListDrawSpec &spa = m_inventorylists[siz - 2];
-               const ListDrawSpec &spb = m_inventorylists[siz - 1];
-               m_inventory_rings.emplace_back(spa.inventoryloc, spa.listname);
-               m_inventory_rings.emplace_back(spb.inventoryloc, spb.listname);
+               const GUIInventoryList *spa = m_inventorylists[siz - 2];
+               const GUIInventoryList *spb = m_inventorylists[siz - 1];
+               m_inventory_rings.emplace_back(spa->getInventoryloc(), spa->getListname());
+               m_inventory_rings.emplace_back(spb->getInventoryloc(), spb->getListname());
                return;
        }
 
@@ -549,13 +609,13 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element
 
                spec.ftype = f_CheckBox;
 
-               gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect, this,
-                                       spec.fid, spec.flabel.c_str());
+               gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect,
+                               data->current_parent, spec.fid, spec.flabel.c_str());
 
-               auto style = getStyleForElement("checkbox", name);
+               auto style = getDefaultStyleForElement("checkbox", name);
                e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
 
-               if (spec.fname == data->focused_fieldname) {
+               if (spec.fname == m_focused_element) {
                        Environment->setFocus(e);
                }
 
@@ -609,10 +669,10 @@ void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &elemen
 
                spec.ftype = f_ScrollBar;
                spec.send  = true;
-               GUIScrollBar *e = new GUIScrollBar(Environment, this, spec.fid, rect,
-                               is_horizontal, true);
+               GUIScrollBar *e = new GUIScrollBar(Environment, data->current_parent,
+                               spec.fid, rect, is_horizontal, true);
 
-               auto style = getStyleForElement("scrollbar", name);
+               auto style = getDefaultStyleForElement("scrollbar", name);
                e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
                e->setArrowsVisible(data->scrollbar_options.arrow_visiblity);
 
@@ -631,6 +691,10 @@ void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &elemen
 
                e->setPageSize(scrollbar_size * (max - min + 1) / data->scrollbar_options.thumb_size);
 
+               if (spec.fname == m_focused_element) {
+                       Environment->setFocus(e);
+               }
+
                m_scrollbars.emplace_back(spec,e);
                m_fields.push_back(spec);
                return;
@@ -736,12 +800,17 @@ void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
                        1
                );
                core::rect<s32> rect(pos, pos + geom);
-               gui::IGUIImage *e = Environment->addImage(rect, this, spec.fid, 0, true);
+               gui::IGUIImage *e = Environment->addImage(rect, data->current_parent,
+                               spec.fid, 0, true);
                e->setImage(texture);
                e->setScaleImage(true);
-               e->setNotClipped(true);
+               auto style = getDefaultStyleForElement("image", spec.fname);
+               e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
                m_fields.push_back(spec);
 
+               // images should let events through
+               e->grab();
+               m_clickthrough_elements.push_back(e);
                return;
        }
 
@@ -769,16 +838,82 @@ void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
                        L"",
                        258 + m_fields.size()
                );
-               gui::IGUIImage *e = Environment->addImage(texture, pos, true, this,
-                               spec.fid, 0);
-               e->setNotClipped(true);
+               gui::IGUIImage *e = Environment->addImage(texture, pos, true,
+                               data->current_parent, spec.fid, 0);
+               auto style = getDefaultStyleForElement("image", spec.fname);
+               e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
                m_fields.push_back(spec);
 
+               // images should let events through
+               e->grab();
+               m_clickthrough_elements.push_back(e);
                return;
        }
        errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'"  << std::endl;
 }
 
+void GUIFormSpecMenu::parseAnimatedImage(parserData *data, const std::string &element)
+{
+       std::vector<std::string> parts = split(element, ';');
+
+       if (parts.size() != 6 && parts.size() != 7 &&
+                       !(parts.size() > 7 && m_formspec_version > FORMSPEC_API_VERSION)) {
+               errorstream << "Invalid animated_image element(" << parts.size()
+                       << "): '" << element << "'" << std::endl;
+               return;
+       }
+
+       std::vector<std::string> v_pos  = split(parts[0], ',');
+       std::vector<std::string> v_geom = split(parts[1], ',');
+       std::string name = parts[2];
+       std::string texture_name = unescape_string(parts[3]);
+       s32 frame_count = stoi(parts[4]);
+       s32 frame_duration = stoi(parts[5]);
+
+       MY_CHECKPOS("animated_image", 0);
+       MY_CHECKGEOM("animated_image", 1);
+
+       v2s32 pos;
+       v2s32 geom;
+
+       if (data->real_coordinates) {
+               pos = getRealCoordinateBasePos(v_pos);
+               geom = getRealCoordinateGeometry(v_geom);
+       } else {
+               pos = getElementBasePos(&v_pos);
+               geom.X = stof(v_geom[0]) * (float)imgsize.X;
+               geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
+       }
+
+       if (!data->explicit_size)
+               warningstream << "Invalid use of animated_image without a size[] element" << std::endl;
+
+       FieldSpec spec(
+               name,
+               L"",
+               L"",
+               258 + m_fields.size()
+       );
+       spec.ftype = f_AnimatedImage;
+       spec.send = true;
+
+       core::rect<s32> rect = core::rect<s32>(pos, pos + geom);
+
+       GUIAnimatedImage *e = new GUIAnimatedImage(Environment, this, spec.fid,
+               rect, texture_name, frame_count, frame_duration, m_tsrc);
+
+       if (parts.size() >= 7)
+               e->setFrameIndex(stoi(parts[6]) - 1);
+
+       auto style = getDefaultStyleForElement("animated_image", spec.fname, "image");
+       e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+
+       // Animated images should let events through
+       m_clickthrough_elements.push_back(e);
+
+       m_fields.push_back(spec);
+}
+
 void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
 {
        std::vector<std::string> parts = split(element,';');
@@ -817,9 +952,13 @@ void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &elemen
                );
                spec.ftype = f_ItemImage;
 
-               GUIItemImage *e = new GUIItemImage(Environment, this, spec.fid,
-                               core::rect<s32>(pos, pos + geom), name, m_font, m_client);
-               e->drop();
+               GUIItemImage *e = new GUIItemImage(Environment, data->current_parent, spec.fid,
+                               core::rect<s32>(pos, pos + geom), name, m_font, m_client);
+               auto style = getDefaultStyleForElement("item_image", spec.fname);
+               e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+
+               // item images should let events through
+               m_clickthrough_elements.push_back(e);
 
                m_fields.push_back(spec);
                return;
@@ -876,54 +1015,13 @@ void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
                if(type == "button_exit")
                        spec.is_exit = true;
 
-               GUIButton *e = GUIButton::addButton(Environment, rect, this, spec.fid, spec.flabel.c_str());
+               GUIButton *e = GUIButton::addButton(Environment, rect, m_tsrc,
+                               data->current_parent, spec.fid, spec.flabel.c_str());
 
                auto style = getStyleForElement(type, name, (type != "button") ? "button" : "");
-               if (style.isNotDefault(StyleSpec::BGCOLOR)) {
-                       e->setColor(style.getColor(StyleSpec::BGCOLOR));
-               }
-               if (style.isNotDefault(StyleSpec::BGCOLOR_HOVERED)) {
-                       e->setHoveredColor(style.getColor(StyleSpec::BGCOLOR_HOVERED));
-               }
-               if (style.isNotDefault(StyleSpec::BGCOLOR_PRESSED)) {
-                       e->setPressedColor(style.getColor(StyleSpec::BGCOLOR_PRESSED));
-               }
+               e->setStyles(style);
 
-               if (style.isNotDefault(StyleSpec::TEXTCOLOR)) {
-                       e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR));
-               }
-               e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
-               e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
-
-               if (style.isNotDefault(StyleSpec::BGIMG)) {
-                       std::string image_name = style.get(StyleSpec::BGIMG, "");
-                       std::string hovered_image_name = style.get(StyleSpec::BGIMG_HOVERED, "");
-                       std::string pressed_image_name = style.get(StyleSpec::BGIMG_PRESSED, "");
-
-                       video::ITexture *texture = 0;
-                       video::ITexture *hovered_texture = 0;
-                       video::ITexture *pressed_texture = 0;
-                       texture = m_tsrc->getTexture(image_name);
-                       if (!hovered_image_name.empty())
-                               hovered_texture = m_tsrc->getTexture(hovered_image_name);
-                       else
-                               hovered_texture = texture;
-                       if (!pressed_image_name.empty())
-                               pressed_texture = m_tsrc->getTexture(pressed_image_name);
-                       else
-                               pressed_texture = texture;
-
-                       e->setUseAlphaChannel(style.getBool(StyleSpec::ALPHA, true));
-                       e->setImage(guiScalingImageButton(
-                                       Environment->getVideoDriver(), texture, geom.X, geom.Y));
-                       e->setHoveredImage(guiScalingImageButton(
-                                       Environment->getVideoDriver(), hovered_texture, geom.X, geom.Y));
-                       e->setPressedImage(guiScalingImageButton(
-                                       Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
-                       e->setScaleImage(true);
-               }
-
-               if (spec.fname == data->focused_fieldname) {
+               if (spec.fname == m_focused_element) {
                        Environment->setFocus(e);
                }
 
@@ -1109,9 +1207,10 @@ void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
                }
 
                //now really show table
-               GUITable *e = new GUITable(Environment, this, spec.fid, rect, m_tsrc);
+               GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
+                               rect, m_tsrc);
 
-               if (spec.fname == data->focused_fieldname) {
+               if (spec.fname == m_focused_element) {
                        Environment->setFocus(e);
                }
 
@@ -1124,7 +1223,7 @@ void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
                if (!str_initial_selection.empty() && str_initial_selection != "0")
                        e->setSelected(stoi(str_initial_selection));
 
-               auto style = getStyleForElement("table", name);
+               auto style = getDefaultStyleForElement("table", name);
                e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
 
                m_tables.emplace_back(spec, e);
@@ -1185,9 +1284,10 @@ void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element
                }
 
                //now really show list
-               GUITable *e = new GUITable(Environment, this, spec.fid, rect, m_tsrc);
+               GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
+                               rect, m_tsrc);
 
-               if (spec.fname == data->focused_fieldname) {
+               if (spec.fname == m_focused_element) {
                        Environment->setFocus(e);
                }
 
@@ -1200,7 +1300,7 @@ void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element
                if (!str_initial_selection.empty() && str_initial_selection != "0")
                        e->setSelected(stoi(str_initial_selection));
 
-               auto style = getStyleForElement("textlist", name);
+               auto style = getDefaultStyleForElement("textlist", name);
                e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
 
                m_tables.emplace_back(spec, e);
@@ -1210,19 +1310,20 @@ void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element
        errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'"  << std::endl;
 }
 
-
 void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
 {
-       std::vector<std::string> parts = split(element,';');
+       std::vector<std::string> parts = split(element, ';');
 
-       if ((parts.size() == 5) ||
-               ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
+       if (parts.size() == 5 || parts.size() == 6 ||
+               (parts.size() > 6 && m_formspec_version > FORMSPEC_API_VERSION))
        {
-               std::vector<std::string> v_pos = split(parts[0],',');
+               std::vector<std::string> v_pos = split(parts[0], ',');
                std::string name = parts[2];
-               std::vector<std::string> items = split(parts[3],',');
-               std::string str_initial_selection;
-               str_initial_selection = parts[4];
+               std::vector<std::string> items = split(parts[3], ',');
+               std::string str_initial_selection = parts[4];
+
+               if (parts.size() >= 6 && is_yes(parts[5]))
+                       m_dropdown_index_event[name] = true;
 
                MY_CHECKPOS("dropdown",0);
 
@@ -1261,9 +1362,10 @@ void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element
                spec.send = true;
 
                //now really show list
-               gui::IGUIComboBox *e = Environment->addComboBox(rect, this, spec.fid);
+               gui::IGUIComboBox *e = Environment->addComboBox(rect, data->current_parent,
+                               spec.fid);
 
-               if (spec.fname == data->focused_fieldname) {
+               if (spec.fname == m_focused_element) {
                        Environment->setFocus(e);
                }
 
@@ -1275,7 +1377,7 @@ void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element
                if (!str_initial_selection.empty())
                        e->setSelected(stoi(str_initial_selection)-1);
 
-               auto style = getStyleForElement("dropdown", name);
+               auto style = getDefaultStyleForElement("dropdown", name);
                e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
 
                m_fields.push_back(spec);
@@ -1288,8 +1390,8 @@ void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element
 
                return;
        }
-       errorstream << "Invalid dropdown element(" << parts.size() << "): '"
-                               << element << "'"  << std::endl;
+       errorstream << "Invalid dropdown element(" << parts.size() << "): '" << element
+               << "'" << std::endl;
 }
 
 void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
@@ -1305,8 +1407,8 @@ void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element
 {
        std::vector<std::string> parts = split(element,';');
 
-       if ((parts.size() == 4) || (parts.size() == 5) ||
-               ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
+       if (parts.size() == 4 ||
+               (parts.size() > 4 && m_formspec_version > FORMSPEC_API_VERSION))
        {
                std::vector<std::string> v_pos = split(parts[0],',');
                std::vector<std::string> v_geom = split(parts[1],',');
@@ -1341,13 +1443,16 @@ void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element
                        name,
                        wlabel,
                        L"",
-                       258 + m_fields.size()
+                       258 + m_fields.size(),
+                       0,
+                       ECI_IBEAM
                        );
 
                spec.send = true;
-               gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
+               gui::IGUIEditBox *e = Environment->addEditBox(0, rect, true,
+                               data->current_parent, spec.fid);
 
-               if (spec.fname == data->focused_fieldname) {
+               if (spec.fname == m_focused_element) {
                        Environment->setFocus(e);
                }
 
@@ -1356,15 +1461,16 @@ void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element
                        rect.UpperLeftCorner.Y -= font_height;
                        rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
                        gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
-                               this, 0);
+                               data->current_parent, 0);
                }
 
                e->setPasswordBox(true,L'*');
 
-               auto style = getStyleForElement("pwdfield", name, "field");
+               auto style = getDefaultStyleForElement("pwdfield", name, "field");
                e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
                e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
                e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
+               e->setOverrideFont(style.getFont());
 
                irr::SEvent evt;
                evt.EventType            = EET_KEY_INPUT_EVENT;
@@ -1375,12 +1481,8 @@ void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element
                evt.KeyInput.PressedDown = true;
                e->OnEvent(evt);
 
-               if (parts.size() >= 5) {
-                       // TODO: remove after 2016-11-03
-                       warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
-                                       " instead of the 5th param" << std::endl;
-                       field_close_on_enter[name] = is_yes(parts[4]);
-               }
+               // Note: Before 5.2.0 "parts.size() >= 5" resulted in a
+               // warning referring to field_close_on_enter[]!
 
                m_fields.push_back(spec);
                return;
@@ -1395,7 +1497,7 @@ void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
        if (!is_editable && !is_multiline) {
                // spec field id to 0, this stops submit searching for a value that isn't there
                gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
-                               this, spec.fid);
+                               data->current_parent, 0);
                return;
        }
 
@@ -1413,22 +1515,22 @@ void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
 
        if (use_intl_edit_box && g_settings->getBool("freetype")) {
                e = new gui::intlGUIEditBox(spec.fdefault.c_str(), true, Environment,
-                               this, spec.fid, rect, is_editable, is_multiline);
+                               data->current_parent, spec.fid, rect, is_editable, is_multiline);
        } else {
                if (is_multiline) {
-                       e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true,
-                                       Environment, this, spec.fid, rect, is_editable, true);
+                       e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true, Environment,
+                                       data->current_parent, spec.fid, rect, is_editable, true);
                } else if (is_editable) {
-                       e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this,
-                                       spec.fid);
+                       e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
+                                       data->current_parent, spec.fid);
                        e->grab();
                }
        }
 
-       auto style = getStyleForElement(is_multiline ? "textarea" : "field", spec.fname);
+       auto style = getDefaultStyleForElement(is_multiline ? "textarea" : "field", spec.fname);
 
        if (e) {
-               if (is_editable && spec.fname == data->focused_fieldname)
+               if (is_editable && spec.fname == m_focused_element)
                        Environment->setFocus(e);
 
                if (is_multiline) {
@@ -1452,6 +1554,7 @@ void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
                if (style.get(StyleSpec::BGCOLOR, "") == "transparent") {
                        e->setDrawBackground(false);
                }
+               e->setOverrideFont(style.getFont());
 
                e->drop();
        }
@@ -1461,7 +1564,7 @@ void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
                rect.UpperLeftCorner.Y -= font_height;
                rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
                IGUIElement *t = gui::StaticText::add(Environment, spec.flabel.c_str(),
-                               rect, false, true, this, 0);
+                               rect, false, true, data->current_parent, 0);
 
                if (t)
                        t->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
@@ -1500,7 +1603,9 @@ void GUIFormSpecMenu::parseSimpleField(parserData *data,
                name,
                wlabel,
                utf8_to_wide(unescape_string(default_val)),
-               258 + m_fields.size()
+               258 + m_fields.size(),
+               0,
+               ECI_IBEAM
        );
 
        createTextField(data, spec, rect, false);
@@ -1562,17 +1667,15 @@ void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>&
                name,
                wlabel,
                utf8_to_wide(unescape_string(default_val)),
-               258 + m_fields.size()
+               258 + m_fields.size(),
+               0,
+               ECI_IBEAM
        );
 
        createTextField(data, spec, rect, type == "textarea");
 
-       if (parts.size() >= 6) {
-               // TODO: remove after 2016-11-03
-               warningstream << "field/textarea: use field_close_on_enter[name, enabled]" <<
-                               " instead of the 6th param" << std::endl;
-               field_close_on_enter[name] = is_yes(parts[5]);
-       }
+       // Note: Before 5.2.0 "parts.size() >= 6" resulted in a
+       // warning referring to field_close_on_enter[]!
 
        m_fields.push_back(spec);
 }
@@ -1587,8 +1690,8 @@ void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
                return;
        }
 
-       if ((parts.size() == 5) || (parts.size() == 6) ||
-               ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
+       if ((parts.size() == 5) ||
+               ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
        {
                parseTextArea(data,parts,type);
                return;
@@ -1623,11 +1726,9 @@ void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &elemen
                pos = getElementBasePos(&v_pos);
                pos -= padding;
 
-               pos.X += stof(v_pos[0]) * spacing.X;
-               pos.Y += stof(v_pos[1]) * spacing.Y + (m_btn_height * 2);
-
                geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
-               geom.Y = (stof(v_geom[1]) * imgsize.Y) - (spacing.Y - imgsize.Y);
+               geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y - imgsize.Y);
+               pos.Y += m_btn_height;
        }
 
        core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X + geom.X, pos.Y + geom.Y);
@@ -1642,9 +1743,10 @@ void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &elemen
                258 + m_fields.size()
        );
 
-       spec.ftype = f_Unknown;
-       new GUIHyperText(
-               spec.flabel.c_str(), Environment, this, spec.fid, rect, m_client, m_tsrc);
+       spec.ftype = f_HyperText;
+       GUIHyperText *e = new GUIHyperText(spec.flabel.c_str(), Environment,
+                       data->current_parent, spec.fid, rect, m_client, m_tsrc);
+       e->drop();
 
        m_fields.push_back(spec);
 }
@@ -1666,6 +1768,11 @@ void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
 
                std::vector<std::string> lines = split(text, '\n');
 
+               auto style = getDefaultStyleForElement("label", "");
+               gui::IGUIFont *font = style.getFont();
+               if (!font)
+                       font = m_font;
+
                for (unsigned int i = 0; i != lines.size(); i++) {
                        std::wstring wlabel_colors = translate_string(
                                utf8_to_wide(unescape_string(lines[i])));
@@ -1687,7 +1794,7 @@ void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
 
                                rect = core::rect<s32>(
                                        pos.X, pos.Y,
-                                       pos.X + m_font->getDimension(wlabel_plain.c_str()).Width,
+                                       pos.X + font->getDimension(wlabel_plain.c_str()).Width,
                                        pos.Y + imgsize.Y);
 
                        } else {
@@ -1709,7 +1816,7 @@ void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
 
                                rect = core::rect<s32>(
                                        pos.X, pos.Y - m_btn_height,
-                                       pos.X + m_font->getDimension(wlabel_plain.c_str()).Width,
+                                       pos.X + font->getDimension(wlabel_plain.c_str()).Width,
                                        pos.Y + m_btn_height);
                        }
 
@@ -1721,14 +1828,19 @@ void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
                                4
                        );
                        gui::IGUIStaticText *e = gui::StaticText::add(Environment,
-                                       spec.flabel.c_str(), rect, false, false, this, spec.fid);
+                                       spec.flabel.c_str(), rect, false, false, data->current_parent,
+                                       spec.fid);
                        e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
 
-                       auto style = getStyleForElement("label", spec.fname);
                        e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
                        e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
+                       e->setOverrideFont(font);
 
                        m_fields.push_back(spec);
+
+                       // labels should let events through
+                       e->grab();
+                       m_clickthrough_elements.push_back(e);
                }
 
                return;
@@ -1750,6 +1862,11 @@ void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &elemen
 
                MY_CHECKPOS("vertlabel",1);
 
+               auto style = getDefaultStyleForElement("vertlabel", "", "label");
+               gui::IGUIFont *font = style.getFont();
+               if (!font)
+                       font = m_font;
+
                v2s32 pos;
                core::rect<s32> rect;
 
@@ -1763,7 +1880,7 @@ void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &elemen
                        // isn't quite tall enough and cuts off the text.
                        rect = core::rect<s32>(pos.X, pos.Y,
                                pos.X + imgsize.X,
-                               pos.Y + font_line_height(m_font) *
+                               pos.Y + font_line_height(font) *
                                (text.length() + 1));
 
                } else {
@@ -1775,7 +1892,7 @@ void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &elemen
                        rect = core::rect<s32>(
                                pos.X, pos.Y+((imgsize.Y/2) - m_btn_height),
                                pos.X+15, pos.Y +
-                                       font_line_height(m_font) *
+                                       font_line_height(font) *
                                        (text.length() + 1) +
                                        ((imgsize.Y/2) - m_btn_height));
                }
@@ -1797,14 +1914,18 @@ void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &elemen
                        258 + m_fields.size()
                );
                gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(),
-                               rect, false, false, this, spec.fid);
+                               rect, false, false, data->current_parent, spec.fid);
                e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
 
-               auto style = getStyleForElement("vertlabel", spec.fname, "label");
                e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
                e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
+               e->setOverrideFont(font);
 
                m_fields.push_back(spec);
+
+               // vertlabels should let events through
+               e->grab();
+               m_clickthrough_elements.push_back(e);
                return;
        }
        errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'"  << std::endl;
@@ -1827,17 +1948,8 @@ void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &elem
                MY_CHECKPOS("imagebutton",0);
                MY_CHECKGEOM("imagebutton",1);
 
-               bool noclip     = false;
-               bool drawborder = true;
                std::string pressed_image_name;
 
-               if (parts.size() >= 7) {
-                       if (parts[5] == "true")
-                               noclip = true;
-                       if (parts[6] == "false")
-                               drawborder = false;
-               }
-
                if (parts.size() >= 8) {
                        pressed_image_name = parts[7];
                }
@@ -1875,36 +1987,30 @@ void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &elem
                if (type == "image_button_exit")
                        spec.is_exit = true;
 
-               video::ITexture *texture = 0;
-               video::ITexture *pressed_texture = 0;
-               texture = m_tsrc->getTexture(image_name);
-               if (!pressed_image_name.empty())
-                       pressed_texture = m_tsrc->getTexture(pressed_image_name);
-               else
-                       pressed_texture = texture;
-
-               GUIButton *e = GUIButton::addButton(Environment, rect, this, spec.fid, spec.flabel.c_str());
+               GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, m_tsrc,
+                               data->current_parent, spec.fid, spec.flabel.c_str());
 
-               if (spec.fname == data->focused_fieldname) {
+               if (spec.fname == m_focused_element) {
                        Environment->setFocus(e);
                }
 
                auto style = getStyleForElement("image_button", spec.fname);
 
-               e->setUseAlphaChannel(style.getBool(StyleSpec::ALPHA, true));
-               e->setImage(guiScalingImageButton(
-                       Environment->getVideoDriver(), texture, geom.X, geom.Y));
-               e->setPressedImage(guiScalingImageButton(
-                       Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
-               e->setScaleImage(true);
+               // Override style properties with values specified directly in the element
+               if (!image_name.empty())
+                       style[StyleSpec::STATE_DEFAULT].set(StyleSpec::FGIMG, image_name);
+
+               if (!pressed_image_name.empty())
+                       style[StyleSpec::STATE_PRESSED].set(StyleSpec::FGIMG, pressed_image_name);
+
                if (parts.size() >= 7) {
-                       e->setNotClipped(noclip);
-                       e->setDrawBorder(drawborder);
-               } else {
-                       e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
-                       e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
+                       style[StyleSpec::STATE_DEFAULT].set(StyleSpec::NOCLIP, parts[5]);
+                       style[StyleSpec::STATE_DEFAULT].set(StyleSpec::BORDER, parts[6]);
                }
 
+               e->setStyles(style);
+               e->setScaleImage(true);
+
                m_fields.push_back(spec);
                return;
        }
@@ -1926,7 +2032,7 @@ void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &elemen
                // Width is not here because tabs are the width of the text, and
                // there's no reason to change that.
                unsigned int i = 0;
-               std::vector<std::string> v_geom = {"1", "0.75"}; // Dummy width and default height
+               std::vector<std::string> v_geom = {"1", "1"}; // Dummy width and height
                bool auto_width = true;
                if (parts.size() == 7) {
                        i++;
@@ -1970,6 +2076,9 @@ void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &elemen
                        pos = getRealCoordinateBasePos(v_pos);
 
                        geom = getRealCoordinateGeometry(v_geom);
+                       // Set default height
+                       if (parts.size() <= 6)
+                               geom.Y = m_btn_height * 2;
                        pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
                        if (auto_width)
                                geom.X = DesiredRect.getWidth(); // Set automatic width
@@ -1988,17 +2097,13 @@ void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &elemen
                core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
                                pos.Y+geom.Y);
 
-               gui::IGUITabControl *e = Environment->addTabControl(rect, this,
-                               show_background, show_border, spec.fid);
+               gui::IGUITabControl *e = Environment->addTabControl(rect,
+                               data->current_parent, show_background, show_border, spec.fid);
                e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
                                irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
                e->setTabHeight(geom.Y);
 
-               if (spec.fname == data->focused_fieldname) {
-                       Environment->setFocus(e);
-               }
-
-               auto style = getStyleForElement("tabheader", name);
+               auto style = getDefaultStyleForElement("tabheader", name);
                e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
 
                for (const std::string &button : buttons) {
@@ -2024,7 +2129,6 @@ void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &elemen
 
 void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
 {
-
        if (m_client == 0) {
                warningstream << "invalid use of item_image_button with m_client==0"
                        << std::endl;
@@ -2083,13 +2187,14 @@ void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &
                        2
                );
 
-               gui::IGUIButton *e_btn = GUIButton::addButton(Environment, rect, this, spec_btn.fid, L"");
+               GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment,
+                               rect, m_tsrc, data->current_parent, spec_btn.fid, spec_btn.flabel.c_str(),
+                               item_name, m_client);
 
                auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button");
-               e_btn->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
-               e_btn->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
+               e_btn->setStyles(style);
 
-               if (spec_btn.fname == data->focused_fieldname) {
+               if (spec_btn.fname == m_focused_element) {
                        Environment->setFocus(e_btn);
                }
 
@@ -2097,24 +2202,6 @@ void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &
                rect += data->basepos-padding;
                spec_btn.rect = rect;
                m_fields.push_back(spec_btn);
-
-               // the spec for the item-image
-               FieldSpec spec_img(
-                       name,
-                       L"",
-                       L"",
-                       258 + m_fields.size(),
-                       2
-               );
-
-               GUIItemImage *e_img = new GUIItemImage(Environment, e_btn, spec_img.fid,
-                               core::rect<s32>(0, 0, geom.X, geom.Y), item_name, m_font, m_client);
-
-               e_img->setText(utf8_to_wide(label).c_str());
-
-               e_img->drop();
-
-               m_fields.push_back(spec_img);
                return;
        }
        errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'"  << std::endl;
@@ -2159,9 +2246,11 @@ void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
 
                        core::rect<s32> rect(pos, pos + geom);
 
-                       GUIBox *e = new GUIBox(Environment, this, spec.fid, rect, tmp_color);
+                       GUIBox *e = new GUIBox(Environment, data->current_parent, spec.fid,
+                                       rect, tmp_color);
 
-                       e->setNotClipped(true);
+                       auto style = getDefaultStyleForElement("box", spec.fname);
+                       e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
 
                        e->drop();
 
@@ -2217,12 +2306,13 @@ void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &eleme
        if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
                ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
        {
-               parseColorString(parts[0], m_slotbg_n, false);
-               parseColorString(parts[1], m_slotbg_h, false);
+               parseColorString(parts[0], data->inventorylist_options.slotbg_n, false);
+               parseColorString(parts[1], data->inventorylist_options.slotbg_h, false);
 
                if (parts.size() >= 3) {
-                       if (parseColorString(parts[2], m_slotbordercolor, false)) {
-                               m_slotborder = true;
+                       if (parseColorString(parts[2], data->inventorylist_options.slotbordercolor,
+                                       false)) {
+                               data->inventorylist_options.slotborder = true;
                        }
                }
                if (parts.size() == 5) {
@@ -2233,6 +2323,14 @@ void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &eleme
                        if (parseColorString(parts[4], tmp_color, false))
                                m_default_tooltip_color = tmp_color;
                }
+
+               // update all already parsed inventorylists
+               for (GUIInventoryList *e : m_inventorylists) {
+                       e->setSlotBGColors(data->inventorylist_options.slotbg_n,
+                                       data->inventorylist_options.slotbg_h);
+                       e->setSlotBorders(data->inventorylist_options.slotborder,
+                                       data->inventorylist_options.slotbordercolor);
+               }
                return;
        }
        errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'"  << std::endl;
@@ -2301,7 +2399,10 @@ void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
                core::rect<s32> rect(pos, pos + geom);
 
                gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
-                               this, fieldspec.fid, rect);
+                               data->current_parent, fieldspec.fid, rect);
+
+               // the element the rect tooltip is bound to should not block mouse-clicks
+               e->setVisible(false);
 
                m_fields.push_back(fieldspec);
                m_tooltip_rects.emplace_back(e, spec);
@@ -2323,7 +2424,7 @@ bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
                return false;
        }
 
-       if (parts[0] != "formspec_version") {
+       if (trim(parts[0]) != "formspec_version") {
                return false;
        }
 
@@ -2352,7 +2453,7 @@ bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &eleme
                return false;
 
        if (type == "invsize")
-               log_deprecated("Deprecated formspec element \"invsize\" is used");
+               warningstream << "Deprecated formspec element \"invsize\" is used" << std::endl;
 
        parseSize(data, description);
 
@@ -2438,15 +2539,9 @@ bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, b
                return false;
        }
 
-       std::string selector = trim(parts[0]);
-       if (selector.empty()) {
-               errorstream << "Invalid style element (Selector required): '" << element
-                                       << "'" << std::endl;
-               return false;
-       }
-
        StyleSpec spec;
 
+       // Parse properties
        for (size_t i = 1; i < parts.size(); i++) {
                size_t equal_pos = parts[i].find('=');
                if (equal_pos == std::string::npos) {
@@ -2468,21 +2563,129 @@ bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, b
                                                << "'" << std::endl;
                                property_warned.insert(propname);
                        }
-                       return false;
+                       continue;
                }
 
                spec.set(prop, value);
        }
 
-       if (style_type) {
-               theme_by_type[selector] |= spec;
-       } else {
-               theme_by_name[selector] |= spec;
+       std::vector<std::string> selectors = split(parts[0], ',');
+       for (size_t sel = 0; sel < selectors.size(); sel++) {
+               std::string selector = trim(selectors[sel]);
+
+               // Copy the style properties to a new StyleSpec
+               // This allows a separate state mask per-selector
+               StyleSpec selector_spec = spec;
+
+               // Parse state information, if it exists
+               bool state_valid = true;
+               size_t state_pos = selector.find(':');
+               if (state_pos != std::string::npos) {
+                       std::string state_str = selector.substr(state_pos + 1);
+                       selector = selector.substr(0, state_pos);
+
+                       if (state_str.empty()) {
+                               errorstream << "Invalid style element (Invalid state): '" << element
+                                       << "'" << std::endl;
+                               state_valid = false;
+                       } else {
+                               std::vector<std::string> states = split(state_str, '+');
+                               for (std::string &state : states) {
+                                       StyleSpec::State converted = StyleSpec::getStateByName(state);
+                                       if (converted == StyleSpec::STATE_INVALID) {
+                                               infostream << "Unknown style state " << state <<
+                                                       " in element '" << element << "'" << std::endl;
+                                               state_valid = false;
+                                               break;
+                                       }
+
+                                       selector_spec.addState(converted);
+                               }
+                       }
+               }
+
+               if (!state_valid) {
+                       // Skip this selector
+                       continue;
+               }
+
+               if (style_type) {
+                       theme_by_type[selector].push_back(selector_spec);
+               } else {
+                       theme_by_name[selector].push_back(selector_spec);
+               }
+
+               // Backwards-compatibility for existing _hovered/_pressed properties
+               if (selector_spec.hasProperty(StyleSpec::BGCOLOR_HOVERED)
+                               || selector_spec.hasProperty(StyleSpec::BGIMG_HOVERED)
+                               || selector_spec.hasProperty(StyleSpec::FGIMG_HOVERED)) {
+                       StyleSpec hover_spec;
+                       hover_spec.addState(StyleSpec::STATE_HOVERED);
+
+                       if (selector_spec.hasProperty(StyleSpec::BGCOLOR_HOVERED)) {
+                               hover_spec.set(StyleSpec::BGCOLOR, selector_spec.get(StyleSpec::BGCOLOR_HOVERED, ""));
+                       }
+                       if (selector_spec.hasProperty(StyleSpec::BGIMG_HOVERED)) {
+                               hover_spec.set(StyleSpec::BGIMG, selector_spec.get(StyleSpec::BGIMG_HOVERED, ""));
+                       }
+                       if (selector_spec.hasProperty(StyleSpec::FGIMG_HOVERED)) {
+                               hover_spec.set(StyleSpec::FGIMG, selector_spec.get(StyleSpec::FGIMG_HOVERED, ""));
+                       }
+
+                       if (style_type) {
+                               theme_by_type[selector].push_back(hover_spec);
+                       } else {
+                               theme_by_name[selector].push_back(hover_spec);
+                       }
+               }
+               if (selector_spec.hasProperty(StyleSpec::BGCOLOR_PRESSED)
+                               || selector_spec.hasProperty(StyleSpec::BGIMG_PRESSED)
+                               || selector_spec.hasProperty(StyleSpec::FGIMG_PRESSED)) {
+                       StyleSpec press_spec;
+                       press_spec.addState(StyleSpec::STATE_PRESSED);
+
+                       if (selector_spec.hasProperty(StyleSpec::BGCOLOR_PRESSED)) {
+                               press_spec.set(StyleSpec::BGCOLOR, selector_spec.get(StyleSpec::BGCOLOR_PRESSED, ""));
+                       }
+                       if (selector_spec.hasProperty(StyleSpec::BGIMG_PRESSED)) {
+                               press_spec.set(StyleSpec::BGIMG, selector_spec.get(StyleSpec::BGIMG_PRESSED, ""));
+                       }
+                       if (selector_spec.hasProperty(StyleSpec::FGIMG_PRESSED)) {
+                               press_spec.set(StyleSpec::FGIMG, selector_spec.get(StyleSpec::FGIMG_PRESSED, ""));
+                       }
+
+                       if (style_type) {
+                               theme_by_type[selector].push_back(press_spec);
+                       } else {
+                               theme_by_name[selector].push_back(press_spec);
+                       }
+               }
        }
 
        return true;
 }
 
+void GUIFormSpecMenu::parseSetFocus(const std::string &element)
+{
+       std::vector<std::string> parts = split(element, ';');
+
+       if (parts.size() <= 2 ||
+               (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION))
+       {
+               if (m_is_form_regenerated)
+                       return; // Never focus on resizing
+
+               bool force_focus = parts.size() >= 2 && is_yes(parts[1]);
+               if (force_focus || m_text_dst->m_formname != m_last_formname)
+                       setFocus(parts[0]);
+
+               return;
+       }
+
+       errorstream << "Invalid set_focus element (" << parts.size() << "): '" << element
+               << "'" << std::endl;
+}
+
 void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
 {
        //some prechecks
@@ -2492,24 +2695,12 @@ void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
        if (parseVersionDirect(element))
                return;
 
-       std::vector<std::string> parts = split(element,'[');
-
-       // ugly workaround to keep compatibility
-       if (parts.size() > 2) {
-               if (trim(parts[0]) == "image") {
-                       for (unsigned int i=2;i< parts.size(); i++) {
-                               parts[1] += "[" + parts[i];
-                       }
-               }
-               else { return; }
-       }
-
-       if (parts.size() < 2) {
+       size_t pos = element.find('[');
+       if (pos == std::string::npos)
                return;
-       }
 
-       std::string type = trim(parts[0]);
-       std::string description = trim(parts[1]);
+       std::string type = trim(element.substr(0, pos));
+       std::string description = element.substr(pos+1);
 
        if (type == "container") {
                parseContainer(data, description);
@@ -2541,6 +2732,11 @@ void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
                return;
        }
 
+       if (type == "animated_image") {
+               parseAnimatedImage(data, description);
+               return;
+       }
+
        if (type == "item_image") {
                parseItemImage(data, description);
                return;
@@ -2671,6 +2867,21 @@ void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
                return;
        }
 
+       if (type == "scroll_container") {
+               parseScrollContainer(data, description);
+               return;
+       }
+
+       if (type == "scroll_container_end") {
+               parseScrollContainerEnd(data);
+               return;
+       }
+
+       if (type == "set_focus") {
+               parseSetFocus(description);
+               return;
+       }
+
        // Ignore others
        infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
                        << std::endl;
@@ -2678,36 +2889,38 @@ void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
 
 void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
 {
-       /* useless to regenerate without a screensize */
+       // Useless to regenerate without a screensize
        if ((screensize.X <= 0) || (screensize.Y <= 0)) {
                return;
        }
 
        parserData mydata;
 
-       //preserve tables
-       for (auto &m_table : m_tables) {
-               std::string tablename = m_table.first.fname;
-               GUITable *table = m_table.second;
-               mydata.table_dyndata[tablename] = table->getDynamicData();
-       }
-
-       //set focus
-       if (!m_focused_element.empty())
-               mydata.focused_fieldname = m_focused_element;
-
-       //preserve focus
-       gui::IGUIElement *focused_element = Environment->getFocus();
-       if (focused_element && focused_element->getParent() == this) {
-               s32 focused_id = focused_element->getID();
-               if (focused_id > 257) {
-                       for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
-                               if (field.fid == focused_id) {
-                                       mydata.focused_fieldname = field.fname;
-                                       break;
+       // Preserve stuff only on same form, not on a new form.
+       if (m_text_dst->m_formname == m_last_formname) {
+               // Preserve tables/textlists
+               for (auto &m_table : m_tables) {
+                       std::string tablename = m_table.first.fname;
+                       GUITable *table = m_table.second;
+                       mydata.table_dyndata[tablename] = table->getDynamicData();
+               }
+
+               // Preserve focus
+               gui::IGUIElement *focused_element = Environment->getFocus();
+               if (focused_element && focused_element->getParent() == this) {
+                       s32 focused_id = focused_element->getID();
+                       if (focused_id > 257) {
+                               for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
+                                       if (field.fid == focused_id) {
+                                               m_focused_element = field.fname;
+                                               break;
+                                       }
                                }
                        }
                }
+       } else {
+               // Don't keep old focus value
+               m_focused_element = "";
        }
 
        // Remove children
@@ -2716,7 +2929,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
        for (auto &table_it : m_tables)
                table_it.second->drop();
        for (auto &inventorylist_it : m_inventorylists)
-               inventorylist_it.e->drop();
+               inventorylist_it->drop();
        for (auto &checkbox_it : m_checkboxes)
                checkbox_it.second->drop();
        for (auto &scrollbar_it : m_scrollbars)
@@ -2725,8 +2938,12 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
                background_it->drop();
        for (auto &tooltip_rect_it : m_tooltip_rects)
                tooltip_rect_it.first->drop();
+       for (auto &clickthrough_it : m_clickthrough_elements)
+               clickthrough_it->drop();
+       for (auto &scroll_container_it : m_scroll_containers)
+               scroll_container_it.second->drop();
 
-       mydata.size= v2s32(100,100);
+       mydata.size = v2s32(100, 100);
        mydata.screensize = screensize;
        mydata.offset = v2f32(0.5f, 0.5f);
        mydata.anchor = v2f32(0.5f, 0.5f);
@@ -2735,7 +2952,8 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
        // Base position of contents of form
        mydata.basepos = getBasePos();
 
-       /* Convert m_init_draw_spec to m_inventorylists */
+       // the parent for the parsed elements
+       mydata.current_parent = this;
 
        m_inventorylists.clear();
        m_backgrounds.clear();
@@ -2747,8 +2965,12 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
        m_tooltip_rects.clear();
        m_inventory_rings.clear();
        m_dropdowns.clear();
+       m_scroll_containers.clear();
        theme_by_name.clear();
        theme_by_type.clear();
+       m_clickthrough_elements.clear();
+       field_close_on_enter.clear();
+       m_dropdown_index_event.clear();
 
        m_bgnonfullscreen = true;
        m_bgfullscreen = false;
@@ -2775,15 +2997,9 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
                );
        }
 
-       m_slotbg_n = video::SColor(255,128,128,128);
-       m_slotbg_h = video::SColor(255,192,192,192);
-
        m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
        m_default_tooltip_color = video::SColor(255,255,255,255);
 
-       m_slotbordercolor = video::SColor(200,0,0,0);
-       m_slotborder = false;
-
        // Add tooltip
        {
                assert(!m_tooltip_element);
@@ -3018,11 +3234,24 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
                parseElement(&mydata, elements[i]);
        }
 
-       if (!container_stack.empty()) {
+       if (mydata.current_parent != this) {
+               errorstream << "Invalid formspec string: scroll_container was never closed!"
+                       << std::endl;
+       } else if (!container_stack.empty()) {
                errorstream << "Invalid formspec string: container was never closed!"
                        << std::endl;
        }
 
+       // get the scrollbar elements for scroll_containers
+       for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers) {
+               for (const std::pair<FieldSpec, GUIScrollBar *> &b : m_scrollbars) {
+                       if (c.first == b.first.fname) {
+                               c.second->setScrollBar(b.second);
+                               break;
+                       }
+               }
+       }
+
        // If there are fields without explicit size[], add a "Proceed"
        // button and adjust size to fit all the fields.
        if (mydata.simple_field_count > 0 && !mydata.explicit_size) {
@@ -3047,13 +3276,13 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
                                        size.X / 2 - 70 + 140, pos.Y + m_btn_height * 2
                        );
                        const wchar_t *text = wgettext("Proceed");
-                       GUIButton::addButton(Environment, mydata.rect, this, 257, text);
+                       GUIButton::addButton(Environment, mydata.rect, m_tsrc, this, 257, text);
                        delete[] text;
                }
        }
 
-       //set initial focus if parser didn't set it
-       focused_element = Environment->getFocus();
+       // Set initial focus if parser didn't set it
+       gui::IGUIElement *focused_element = Environment->getFocus();
        if (!focused_element
                        || !isMyChild(focused_element)
                        || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
@@ -3064,6 +3293,13 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
        // legacy sorting
        if (m_formspec_version < 3)
                legacySortElements(legacy_sort_start);
+
+       // Formname and regeneration setting
+       if (!m_is_form_regenerated) {
+               // Only set previous form name if we purposefully showed a new formspec
+               m_last_formname = m_text_dst->m_formname;
+               m_is_form_regenerated = true;
+       }
 }
 
 void GUIFormSpecMenu::legacySortElements(core::list<IGUIElement *>::Iterator from)
@@ -3092,7 +3328,7 @@ void GUIFormSpecMenu::legacySortElements(core::list<IGUIElement *>::Iterator fro
                elements.emplace_back(*it);
 
        // 2: Sort the container
-       std::sort(elements.begin(), elements.end(),
+       std::stable_sort(elements.begin(), elements.end(),
                        [this] (const IGUIElement *a, const IGUIElement *b) -> bool {
                const FieldSpec *spec_a = getSpecByID(a->getID());
                const FieldSpec *spec_b = getSpecByID(b->getID());
@@ -3113,167 +3349,41 @@ bool GUIFormSpecMenu::getAndroidUIInput()
        if (!hasAndroidUIInput())
                return false;
 
+       // still waiting
+       if (porting::getInputDialogState() == -1)
+               return true;
+
        std::string fieldname = m_jni_field_name;
        m_jni_field_name.clear();
 
-       for(std::vector<FieldSpec>::iterator iter =  m_fields.begin();
-                       iter != m_fields.end(); ++iter) {
-
-               if (iter->fname != fieldname) {
+       for (const FieldSpec &field : m_fields) {
+               if (field.fname != fieldname)
                        continue;
-               }
-               IGUIElement *tochange = getElementFromId(iter->fid, true);
 
-               if (tochange == 0) {
-                       return false;
-               }
+               IGUIElement *element = getElementFromId(field.fid, true);
 
-               if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
+               if (!element || element->getType() != irr::gui::EGUIET_EDIT_BOX)
                        return false;
-               }
 
                std::string text = porting::getInputDialogValue();
-
-               ((gui::IGUIEditBox *)tochange)->setText(utf8_to_wide(text).c_str());
+               ((gui::IGUIEditBox *)element)->setText(utf8_to_wide(text).c_str());
        }
        return false;
 }
 #endif
 
-GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
+GUIInventoryList::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
 {
        core::rect<s32> imgrect(0, 0, imgsize.X, imgsize.Y);
 
-       for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
-               core::rect<s32> clipping_rect = s.e->getAbsoluteClippingRect();
-               v2s32 base_pos = s.e->getAbsolutePosition().UpperLeftCorner;
-               for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
-                       s32 item_i = i + s.start_item_i;
-
-                       s32 x;
-                       s32 y;
-                       if (s.real_coordinates) {
-                               x = (i%s.geom.X) * (imgsize.X * 1.25);
-                               y = (i/s.geom.X) * (imgsize.Y * 1.25);
-                       } else {
-                               x = (i%s.geom.X) * spacing.X;
-                               y = (i/s.geom.X) * spacing.Y;
-                       }
-                       v2s32 p0(x,y);
-                       core::rect<s32> rect = imgrect + base_pos + p0;
-                       rect.clipAgainst(clipping_rect);
-                       if (rect.getArea() > 0 && rect.isPointInside(p))
-                               return ItemSpec(s.inventoryloc, s.listname, item_i);
-               }
+       for (const GUIInventoryList *e : m_inventorylists) {
+               s32 item_index = e->getItemIndexAtPos(p);
+               if (item_index != -1)
+                       return GUIInventoryList::ItemSpec(e->getInventoryloc(), e->getListname(),
+                                       item_index);
        }
 
-       return ItemSpec(InventoryLocation(), "", -1);
-}
-
-void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int layer,
-               bool &item_hovered)
-{
-       video::IVideoDriver* driver = Environment->getVideoDriver();
-
-       Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
-       if (!inv) {
-               warningstream<<"GUIFormSpecMenu::drawList(): "
-                               << "The inventory location "
-                               << "\"" << s.inventoryloc.dump() << "\" doesn't exist anymore"
-                               << std::endl;
-               return;
-       }
-       InventoryList *ilist = inv->getList(s.listname);
-       if (!ilist) {
-               warningstream << "GUIFormSpecMenu::drawList(): "
-                               << "The inventory list \"" << s.listname << "\" @ \""
-                               << s.inventoryloc.dump() << "\" doesn't exist anymore"
-                               << std::endl;
-               return;
-       }
-
-       core::rect<s32> imgrect(0, 0, imgsize.X, imgsize.Y);
-       core::rect<s32> clipping_rect = s.e->getAbsoluteClippingRect();
-       v2s32 base_pos = s.e->getAbsolutePosition().UpperLeftCorner;
-
-       for (s32 i = 0; i < s.geom.X * s.geom.Y; i++) {
-               s32 item_i = i + s.start_item_i;
-               if (item_i >= (s32)ilist->getSize())
-                       break;
-
-               s32 x;
-               s32 y;
-               if (s.real_coordinates) {
-                       x = (i%s.geom.X) * (imgsize.X * 1.25);
-                       y = (i/s.geom.X) * (imgsize.Y * 1.25);
-               } else {
-                       x = (i%s.geom.X) * spacing.X;
-                       y = (i/s.geom.X) * spacing.Y;
-               }
-               v2s32 p(x,y);
-               core::rect<s32> rect = imgrect + base_pos + p;
-               ItemStack item = ilist->getItem(item_i);
-
-               bool selected = m_selected_item
-                       && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
-                       && m_selected_item->listname == s.listname
-                       && m_selected_item->i == item_i;
-               core::rect<s32> clipped_rect(rect);
-               clipped_rect.clipAgainst(clipping_rect);
-               bool hovering = clipped_rect.getArea() > 0 &&
-                               clipped_rect.isPointInside(m_pointer);
-               ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
-                       (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
-
-               if (layer == 0) {
-                       if (hovering) {
-                               item_hovered = true;
-                               driver->draw2DRectangle(m_slotbg_h, rect, &clipping_rect);
-                       } else {
-                               driver->draw2DRectangle(m_slotbg_n, rect, &clipping_rect);
-                       }
-               }
-
-               //Draw inv slot borders
-               if (m_slotborder) {
-                       s32 x1 = rect.UpperLeftCorner.X;
-                       s32 y1 = rect.UpperLeftCorner.Y;
-                       s32 x2 = rect.LowerRightCorner.X;
-                       s32 y2 = rect.LowerRightCorner.Y;
-                       s32 border = 1;
-                       driver->draw2DRectangle(m_slotbordercolor,
-                               core::rect<s32>(v2s32(x1 - border, y1 - border),
-                                                               v2s32(x2 + border, y1)), &clipping_rect);
-                       driver->draw2DRectangle(m_slotbordercolor,
-                               core::rect<s32>(v2s32(x1 - border, y2),
-                                                               v2s32(x2 + border, y2 + border)), &clipping_rect);
-                       driver->draw2DRectangle(m_slotbordercolor,
-                               core::rect<s32>(v2s32(x1 - border, y1),
-                                                               v2s32(x1, y2)), &clipping_rect);
-                       driver->draw2DRectangle(m_slotbordercolor,
-                               core::rect<s32>(v2s32(x2, y1),
-                                                               v2s32(x2 + border, y2)), &clipping_rect);
-               }
-
-               if (layer == 1) {
-                       if (selected)
-                               item.takeItem(m_selected_amount);
-
-                       if (!item.empty()) {
-                               // Draw item stack
-                               drawItemStack(driver, m_font, item,
-                                       rect, &clipping_rect, m_client, rotation_kind);
-                               // Draw tooltip
-                               if (hovering && !m_selected_item) {
-                                       std::string tooltip = item.getDescription(m_client->idef());
-                                       if (m_tooltip_append_itemname)
-                                               tooltip += "\n[" + item.name + "]";
-                                       showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
-                                                       m_default_tooltip_bgcolor);
-                               }
-                       }
-               }
-       }
+       return GUIInventoryList::ItemSpec(InventoryLocation(), "", -1);
 }
 
 void GUIFormSpecMenu::drawSelectedItem()
@@ -3307,6 +3417,7 @@ void GUIFormSpecMenu::drawMenu()
                const std::string &newform = m_form_src->getForm();
                if (newform != m_formspec_string) {
                        m_formspec_string = newform;
+                       m_is_form_regenerated = false;
                        regenerateGui(m_screensize_old);
                }
        }
@@ -3316,6 +3427,8 @@ void GUIFormSpecMenu::drawMenu()
        gui::IGUIFont *old_font = skin->getFont();
        skin->setFont(m_font);
 
+       m_hovered_item_tooltips.clear();
+
        updateSelectedItem();
 
        video::IVideoDriver* driver = Environment->getVideoDriver();
@@ -3356,23 +3469,26 @@ void GUIFormSpecMenu::drawMenu()
                e->setVisible(false);
        }
 
+       // Some elements are only visible while being drawn
+       for (gui::IGUIElement *e : m_clickthrough_elements)
+               e->setVisible(true);
+
        /*
                Call base class
+               (This is where all the drawing happens.)
        */
        gui::IGUIElement::draw();
 
-       /*
-               Draw items
-               Layer 0: Item slot rectangles
-               Layer 1: Item images; prepare tooltip
-       */
-       bool item_hovered = false;
-       for (int layer = 0; layer < 2; layer++) {
-               for (const GUIFormSpecMenu::ListDrawSpec &spec : m_inventorylists) {
-                       drawList(spec, layer, item_hovered);
-               }
+       for (gui::IGUIElement *e : m_clickthrough_elements)
+               e->setVisible(false);
+
+       // Draw hovered item tooltips
+       for (const std::string &tooltip : m_hovered_item_tooltips) {
+               showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
+                               m_default_tooltip_bgcolor);
        }
-       if (!item_hovered) {
+
+       if (m_hovered_item_tooltips.empty()) {
                // reset rotation time
                drawItemStack(driver, m_font, ItemStack(),
                        core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
@@ -3385,14 +3501,25 @@ void GUIFormSpecMenu::drawMenu()
 #endif
 
        /*
-               Draw fields/buttons tooltips
+               Draw fields/buttons tooltips and update the mouse cursor
        */
        gui::IGUIElement *hovered =
                        Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
 
+#ifndef HAVE_TOUCHSCREENGUI
+       gui::ICursorControl *cursor_control = RenderingEngine::get_raw_device()->
+                       getCursorControl();
+       gui::ECURSOR_ICON current_cursor_icon = cursor_control->getActiveIcon();
+#endif
+       bool hovered_element_found = false;
+
        if (hovered != NULL) {
-               s32 id = hovered->getID();
+               if (m_show_debug) {
+                       core::rect<s32> rect = hovered->getAbsoluteClippingRect();
+                       driver->draw2DRectangle(0x22FFFF00, rect, &rect);
+               }
 
+               s32 id = hovered->getID();
                u64 delta = 0;
                if (id == -1) {
                        m_old_tooltip_id = id;
@@ -3405,23 +3532,41 @@ void GUIFormSpecMenu::drawMenu()
                        }
                }
 
-               // Find and update the current tooltip
-               if (id != -1 && delta >= m_tooltip_show_delay) {
+               // Find and update the current tooltip and cursor icon
+               if (id != -1) {
                        for (const FieldSpec &field : m_fields) {
 
                                if (field.fid != id)
                                        continue;
 
-                               const std::wstring &text = m_tooltips[field.fname].tooltip;
-                               if (!text.empty())
-                                       showTooltip(text, m_tooltips[field.fname].color,
-                                               m_tooltips[field.fname].bgcolor);
+                               if (delta >= m_tooltip_show_delay) {
+                                       const std::wstring &text = m_tooltips[field.fname].tooltip;
+                                       if (!text.empty())
+                                               showTooltip(text, m_tooltips[field.fname].color,
+                                                       m_tooltips[field.fname].bgcolor);
+                               }
+
+#ifndef HAVE_TOUCHSCREENGUI
+                               if (field.ftype != f_HyperText && // Handled directly in guiHyperText
+                                               current_cursor_icon != field.fcursor_icon)
+                                       cursor_control->setActiveIcon(field.fcursor_icon);
+#endif
+
+                               hovered_element_found = true;
 
                                break;
                        }
                }
        }
 
+       if (!hovered_element_found) {
+               // no element is hovered
+#ifndef HAVE_TOUCHSCREENGUI
+               if (current_cursor_icon != ECI_NORMAL)
+                       cursor_control->setActiveIcon(ECI_NORMAL);
+#endif
+       }
+
        m_tooltip_element->draw();
 
        /*
@@ -3436,19 +3581,16 @@ void GUIFormSpecMenu::drawMenu()
 void GUIFormSpecMenu::showTooltip(const std::wstring &text,
        const irr::video::SColor &color, const irr::video::SColor &bgcolor)
 {
-       const std::wstring ntext = translate_string(text);
-       m_tooltip_element->setOverrideColor(color);
-       m_tooltip_element->setBackgroundColor(bgcolor);
-       setStaticText(m_tooltip_element, ntext.c_str());
+       EnrichedString ntext(text);
+       ntext.setDefaultColor(color);
+       ntext.setBackground(bgcolor);
+
+       setStaticText(m_tooltip_element, ntext);
 
        // Tooltip size and offset
        s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
-#if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
-       std::vector<std::wstring> text_rows = str_split(ntext, L'\n');
-       s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
-#else
        s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
-#endif
+
        v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
        int tooltip_offset_x = m_btn_height;
        int tooltip_offset_y = m_btn_height;
@@ -3457,6 +3599,10 @@ void GUIFormSpecMenu::showTooltip(const std::wstring &text,
        tooltip_offset_y  = 0;
        if (m_pointer.X > (s32)screenSize.X / 2)
                tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
+
+       // Hide tooltip after ETIE_LEFT_UP
+       if (m_pointer.X == 0)
+               return;
 #endif
 
        // Calculate and set the tooltip position
@@ -3485,11 +3631,11 @@ void GUIFormSpecMenu::updateSelectedItem()
 
        // If craftresult is nonempty and nothing else is selected, select it now.
        if (!m_selected_item) {
-               for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
-                       if (s.listname != "craftpreview")
+               for (const GUIInventoryList *e : m_inventorylists) {
+                       if (e->getListname() != "craftpreview")
                                continue;
 
-                       Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
+                       Inventory *inv = m_invmgr->getInventory(e->getInventoryloc());
                        if (!inv)
                                continue;
 
@@ -3503,8 +3649,8 @@ void GUIFormSpecMenu::updateSelectedItem()
                                continue;
 
                        // Grab selected item from the crafting result list
-                       m_selected_item = new ItemSpec;
-                       m_selected_item->inventoryloc = s.inventoryloc;
+                       m_selected_item = new GUIInventoryList::ItemSpec;
+                       m_selected_item->inventoryloc = e->getInventoryloc();
                        m_selected_item->listname = "craftresult";
                        m_selected_item->i = 0;
                        m_selected_amount = item.count;
@@ -3525,16 +3671,12 @@ ItemStack GUIFormSpecMenu::verifySelectedItem()
        // If the selected stack has become smaller, adjust m_selected_amount.
        // Return the selected stack.
 
-       if(m_selected_item)
-       {
-               if(m_selected_item->isValid())
-               {
+       if (m_selected_item) {
+               if (m_selected_item->isValid()) {
                        Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
-                       if(inv)
-                       {
+                       if (inv) {
                                InventoryList *list = inv->getList(m_selected_item->listname);
-                               if(list && (u32) m_selected_item->i < list->getSize())
-                               {
+                               if (list && (u32) m_selected_item->i < list->getSize()) {
                                        ItemStack stack = list->getItem(m_selected_item->i);
                                        if (!m_selected_swap.empty()) {
                                                if (m_selected_swap.name == stack.name &&
@@ -3552,7 +3694,7 @@ ItemStack GUIFormSpecMenu::verifySelectedItem()
 
                // selection was not valid
                delete m_selected_item;
-               m_selected_item = NULL;
+               m_selected_item = nullptr;
                m_selected_amount = 0;
                m_selected_dragging = false;
        }
@@ -3601,7 +3743,7 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
                }
 
                for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
-                       if(s.send) {
+                       if (s.send) {
                                std::string name = s.fname;
                                if (s.ftype == f_Button) {
                                        fields[name] = wide_to_utf8(s.flabel);
@@ -3610,14 +3752,13 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
                                        if (table) {
                                                fields[name] = table->checkEvent();
                                        }
-                               }
-                               else if(s.ftype == f_DropDown) {
-                                       // no dynamic cast possible due to some distributions shipped
-                                       // without rtti support in irrlicht
+                               } else if (s.ftype == f_DropDown) {
+                                       // No dynamic cast possible due to some distributions shipped
+                                       // without rtti support in Irrlicht
                                        IGUIElement *element = getElementFromId(s.fid, true);
                                        gui::IGUIComboBox *e = NULL;
                                        if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
-                                               e = static_cast<gui::IGUIComboBox*>(element);
+                                               e = static_cast<gui::IGUIComboBox *>(element);
                                        } else {
                                                warningstream << "GUIFormSpecMenu::acceptInput: dropdown "
                                                                << "field without dropdown element" << std::endl;
@@ -3625,16 +3766,19 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
                                        }
                                        s32 selected = e->getSelected();
                                        if (selected >= 0) {
-                                               std::vector<std::string> *dropdown_values =
-                                                       getDropDownValues(s.fname);
-                                               if (dropdown_values && selected < (s32)dropdown_values->size()) {
-                                                       fields[name] = (*dropdown_values)[selected];
+                                               if (m_dropdown_index_event.find(s.fname) !=
+                                                               m_dropdown_index_event.end()) {
+                                                       fields[name] = std::to_string(selected + 1);
+                                               } else {
+                                                       std::vector<std::string> *dropdown_values =
+                                                               getDropDownValues(s.fname);
+                                                       if (dropdown_values && selected < (s32)dropdown_values->size())
+                                                               fields[name] = (*dropdown_values)[selected];
                                                }
                                        }
-                               }
-                               else if (s.ftype == f_TabHeader) {
-                                       // no dynamic cast possible due to some distributions shipped
-                                       // without rttzi support in irrlicht
+                               } else if (s.ftype == f_TabHeader) {
+                                       // No dynamic cast possible due to some distributions shipped
+                                       // without rtti support in Irrlicht
                                        IGUIElement *element = getElementFromId(s.fid, true);
                                        gui::IGUITabControl *e = nullptr;
                                        if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
@@ -3646,10 +3790,9 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
                                                ss << (e->getActiveTab() +1);
                                                fields[name] = ss.str();
                                        }
-                               }
-                               else if (s.ftype == f_CheckBox) {
-                                       // no dynamic cast possible due to some distributions shipped
-                                       // without rtti support in irrlicht
+                               } else if (s.ftype == f_CheckBox) {
+                                       // No dynamic cast possible due to some distributions shipped
+                                       // without rtti support in Irrlicht
                                        IGUIElement *element = getElementFromId(s.fid, true);
                                        gui::IGUICheckBox *e = nullptr;
                                        if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
@@ -3662,10 +3805,9 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
                                                else
                                                        fields[name] = "false";
                                        }
-                               }
-                               else if (s.ftype == f_ScrollBar) {
-                                       // no dynamic cast possible due to some distributions shipped
-                                       // without rtti support in irrlicht
+                               } else if (s.ftype == f_ScrollBar) {
+                                       // No dynamic cast possible due to some distributions shipped
+                                       // without rtti support in Irrlicht
                                        IGUIElement *element = getElementFromId(s.fid, true);
                                        GUIScrollBar *e = nullptr;
                                        if (element && element->getType() == gui::EGUIET_ELEMENT)
@@ -3679,8 +3821,17 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
                                                else
                                                        fields[name] = "VAL:" + os.str();
                                        }
-                               }
-                               else {
+                               } else if (s.ftype == f_AnimatedImage) {
+                                       // No dynamic cast possible due to some distributions shipped
+                                       // without rtti support in Irrlicht
+                                       IGUIElement *element = getElementFromId(s.fid, true);
+                                       GUIAnimatedImage *e = nullptr;
+                                       if (element && element->getType() == gui::EGUIET_ELEMENT)
+                                               e = static_cast<GUIAnimatedImage *>(element);
+
+                                       if (e)
+                                               fields[name] = std::to_string(e->getFrameIndex() + 1);
+                               } else {
                                        IGUIElement *e = getElementFromId(s.fid, true);
                                        if (e)
                                                fields[name] = wide_to_utf8(e->getText());
@@ -3692,17 +3843,6 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
        }
 }
 
-static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
-{
-       while(tocheck != NULL) {
-               if (tocheck == parent) {
-                       return true;
-               }
-               tocheck = tocheck->getParent();
-       }
-       return false;
-}
-
 bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
 {
        // The IGUITabControl renders visually using the skin's selected
@@ -3731,8 +3871,8 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
        }
 
        // Fix Esc/Return key being eaten by checkboxen and tables
-       if(event.EventType==EET_KEY_INPUT_EVENT) {
-               KeyPress kp(event.KeyInput);
+       if (event.EventType == EET_KEY_INPUT_EVENT) {
+                       KeyPress kp(event.KeyInput);
                if (kp == EscapeKey || kp == CancelKey
                                || kp == getKeySetting("keymap_inventory")
                                || event.KeyInput.Key==KEY_RETURN) {
@@ -3750,7 +3890,8 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
        // Mouse wheel and move events: send to hovered element instead of focused
        if (event.EventType == EET_MOUSE_INPUT_EVENT &&
                        (event.MouseInput.Event == EMIE_MOUSE_WHEEL ||
-                        event.MouseInput.Event == EMIE_MOUSE_MOVED)) {
+                       (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
+                       event.MouseInput.ButtonStates == 0))) {
                s32 x = event.MouseInput.X;
                s32 y = event.MouseInput.Y;
                gui::IGUIElement *hovered =
@@ -3762,22 +3903,6 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
                }
        }
 
-       if (event.EventType == EET_MOUSE_INPUT_EVENT) {
-               s32 x = event.MouseInput.X;
-               s32 y = event.MouseInput.Y;
-               gui::IGUIElement *hovered =
-                       Environment->getRootGUIElement()->getElementFromPoint(
-                               core::position2d<s32>(x, y));
-               if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
-                       m_old_tooltip_id = -1;
-               }
-               if (!isChild(hovered,this)) {
-                       if (DoubleClickDetection(event)) {
-                               return true;
-                       }
-               }
-       }
-
        if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
                /* TODO add a check like:
                if (event.JoystickEvent != joystick_we_listen_for)
@@ -3800,64 +3925,6 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
        return GUIModalMenu::preprocessEvent(event);
 }
 
-/******************************************************************************/
-bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
-{
-       /* The following code is for capturing double-clicks of the mouse button
-        * and translating the double-click into an EET_KEY_INPUT_EVENT event
-        * -- which closes the form -- under some circumstances.
-        *
-        * There have been many github issues reporting this as a bug even though it
-        * was an intended feature.  For this reason, remapping the double-click as
-        * an ESC must be explicitly set when creating this class via the
-        * /p remap_dbl_click parameter of the constructor.
-        */
-
-       if (!m_remap_dbl_click)
-               return false;
-
-       if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
-               m_doubleclickdetect[0].pos  = m_doubleclickdetect[1].pos;
-               m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
-
-               m_doubleclickdetect[1].pos  = m_pointer;
-               m_doubleclickdetect[1].time = porting::getTimeMs();
-       }
-       else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
-               u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
-               if (delta > 400) {
-                       return false;
-               }
-
-               double squaredistance =
-                               m_doubleclickdetect[0].pos
-                               .getDistanceFromSQ(m_doubleclickdetect[1].pos);
-
-               if (squaredistance > (30*30)) {
-                       return false;
-               }
-
-               SEvent* translated = new SEvent();
-               assert(translated != 0);
-               //translate doubleclick to escape
-               memset(translated, 0, sizeof(SEvent));
-               translated->EventType = irr::EET_KEY_INPUT_EVENT;
-               translated->KeyInput.Key         = KEY_ESCAPE;
-               translated->KeyInput.Control     = false;
-               translated->KeyInput.Shift       = false;
-               translated->KeyInput.PressedDown = true;
-               translated->KeyInput.Char        = 0;
-               OnEvent(*translated);
-
-               // no need to send the key up event as we're already deleted
-               // and no one else did notice this event
-               delete translated;
-               return true;
-       }
-
-       return false;
-}
-
 void GUIFormSpecMenu::tryClose()
 {
        if (m_allowclose) {
@@ -3897,6 +3964,10 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                                (kp == getKeySetting("keymap_screenshot"))) {
                        m_client->makeScreenshot();
                }
+
+               if (event.KeyInput.PressedDown && kp == getKeySetting("keymap_toggle_debug"))
+                       m_show_debug = !m_show_debug;
+
                if (event.KeyInput.PressedDown &&
                        (event.KeyInput.Key==KEY_RETURN ||
                         event.KeyInput.Key==KEY_UP ||
@@ -3942,7 +4013,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
 
                m_old_tooltip_id = -1;
                updateSelectedItem();
-               ItemSpec s = getItemAtPos(m_pointer);
+               GUIInventoryList::ItemSpec s = getItemAtPos(m_pointer);
 
                Inventory *inv_selected = NULL;
                Inventory *inv_s = NULL;
@@ -4044,8 +4115,9 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                case BET_DOWN:
                        // Some mouse button has been pressed
 
-                       //infostream<<"Mouse button "<<button<<" pressed at p=("
-                       //      <<p.X<<","<<p.Y<<")"<<std::endl;
+                       //infostream << "Mouse button " << button << " pressed at p=("
+                       //      << event.MouseInput.X << "," << event.MouseInput.Y << ")"
+                       //      << std::endl;
 
                        m_selected_dragging = false;
 
@@ -4055,7 +4127,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                        } else if (!m_selected_item) {
                                if (s_count && button != BET_WHEEL_UP) {
                                        // Non-empty stack has been clicked: select or shift-move it
-                                       m_selected_item = new ItemSpec(s);
+                                       m_selected_item = new GUIInventoryList::ItemSpec(s);
 
                                        u32 count;
                                        if (button == BET_RIGHT)
@@ -4283,7 +4355,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
 
                        // if there are no items selected or the selected item
                        // belongs to craftresult list, proceed with crafting
-                       if (m_selected_item == NULL ||
+                       if (!m_selected_item ||
                                        !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
 
                                assert(inv_s);
@@ -4301,7 +4373,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                if (m_selected_amount == 0) {
                        m_selected_swap.clear();
                        delete m_selected_item;
-                       m_selected_item = NULL;
+                       m_selected_item = nullptr;
                        m_selected_amount = 0;
                        m_selected_dragging = false;
                }
@@ -4335,9 +4407,9 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                                (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
                                (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
                                (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
-                       unsigned int btn_id = event.GUIEvent.Caller->getID();
+                       s32 caller_id = event.GUIEvent.Caller->getID();
 
-                       if (btn_id == 257) {
+                       if (caller_id == 257) {
                                if (m_allowclose) {
                                        acceptInput(quit_mode_accept);
                                        quitMenu();
@@ -4353,8 +4425,11 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                        for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
                                // if its a button, set the send field so
                                // lua knows which button was pressed
-                               if ((s.ftype == f_Button || s.ftype == f_CheckBox) &&
-                                               s.fid == event.GUIEvent.Caller->getID()) {
+
+                               if (caller_id != s.fid)
+                                       continue;
+
+                               if (s.ftype == f_Button || s.ftype == f_CheckBox) {
                                        s.send = true;
                                        if (s.is_exit) {
                                                if (m_allowclose) {
@@ -4370,8 +4445,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                                        s.send = false;
                                        return true;
 
-                               } else if ((s.ftype == f_DropDown) &&
-                                               (s.fid == event.GUIEvent.Caller->getID())) {
+                               } else if (s.ftype == f_DropDown) {
                                        // only send the changed dropdown
                                        for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
                                                if (s2.ftype == f_DropDown) {
@@ -4389,13 +4463,11 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                                                }
                                        }
                                        return true;
-                               } else if ((s.ftype == f_ScrollBar) &&
-                                               (s.fid == event.GUIEvent.Caller->getID())) {
+                               } else if (s.ftype == f_ScrollBar) {
                                        s.fdefault = L"Changed";
                                        acceptInput(quit_mode_no);
                                        s.fdefault = L"";
-                               } else if ((s.ftype == f_Unknown) &&
-                                               (s.fid == event.GUIEvent.Caller->getID())) {
+                               } else if (s.ftype == f_Unknown || s.ftype == f_HyperText) {
                                        s.send = true;
                                        acceptInput();
                                        s.send = false;
@@ -4403,6 +4475,12 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                        }
                }
 
+               if (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED) {
+                       // move scroll_containers
+                       for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers)
+                               c.second->onScrollEvent(event.GUIEvent.Caller);
+               }
+
                if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
                        if (event.GUIEvent.Caller->getID() > 257) {
                                bool close_on_enter = true;
@@ -4491,25 +4569,46 @@ std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
        return L"";
 }
 
-StyleSpec GUIFormSpecMenu::getStyleForElement(const std::string &type,
+StyleSpec GUIFormSpecMenu::getDefaultStyleForElement(const std::string &type,
                const std::string &name, const std::string &parent_type) {
-       StyleSpec ret;
+       return getStyleForElement(type, name, parent_type)[StyleSpec::STATE_DEFAULT];
+}
+
+std::array<StyleSpec, StyleSpec::NUM_STATES> GUIFormSpecMenu::getStyleForElement(
+       const std::string &type, const std::string &name, const std::string &parent_type)
+{
+       std::array<StyleSpec, StyleSpec::NUM_STATES> ret;
+
+       auto it = theme_by_type.find("*");
+       if (it != theme_by_type.end()) {
+               for (const StyleSpec &spec : it->second)
+                       ret[(u32)spec.getState()] |= spec;
+       }
+
+       it = theme_by_name.find("*");
+       if (it != theme_by_name.end()) {
+               for (const StyleSpec &spec : it->second)
+                       ret[(u32)spec.getState()] |= spec;
+       }
 
        if (!parent_type.empty()) {
-               auto it = theme_by_type.find(parent_type);
+               it = theme_by_type.find(parent_type);
                if (it != theme_by_type.end()) {
-                       ret |= it->second;
+                       for (const StyleSpec &spec : it->second)
+                               ret[(u32)spec.getState()] |= spec;
                }
        }
 
-       auto it = theme_by_type.find(type);
+       it = theme_by_type.find(type);
        if (it != theme_by_type.end()) {
-               ret |= it->second;
+               for (const StyleSpec &spec : it->second)
+                       ret[(u32)spec.getState()] |= spec;
        }
 
        it = theme_by_name.find(name);
        if (it != theme_by_name.end()) {
-               ret |= it->second;
+               for (const StyleSpec &spec : it->second)
+                       ret[(u32)spec.getState()] |= spec;
        }
 
        return ret;