Browse Source

Add label element

Michael Ragazzon 4 years ago
parent
commit
b50e8d192e

+ 2 - 0
CMake/FileList.cmake

@@ -29,6 +29,7 @@ set(Core_HDR_FILES
     ${PROJECT_SOURCE_DIR}/Source/Core/ElementDefinition.h
     ${PROJECT_SOURCE_DIR}/Source/Core/ElementDefinition.h
     ${PROJECT_SOURCE_DIR}/Source/Core/ElementHandle.h
     ${PROJECT_SOURCE_DIR}/Source/Core/ElementHandle.h
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementImage.h
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementImage.h
+    ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementLabel.h
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementTextSelection.h
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementTextSelection.h
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/InputType.h
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/InputType.h
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/InputTypeButton.h
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/InputTypeButton.h
@@ -282,6 +283,7 @@ set(Core_SRC_FILES
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementFormControlSelect.cpp
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementFormControlSelect.cpp
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementFormControlTextArea.cpp
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementFormControlTextArea.cpp
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementImage.cpp
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementImage.cpp
+    ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementLabel.cpp
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementProgressBar.cpp
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementProgressBar.cpp
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementTabSet.cpp
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementTabSet.cpp
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementTextSelection.cpp
     ${PROJECT_SOURCE_DIR}/Source/Core/Elements/ElementTextSelection.cpp

+ 1 - 1
Include/RmlUi/Core/Context.h

@@ -281,7 +281,7 @@ private:
 
 
 	// Root of the element tree.
 	// Root of the element tree.
 	ElementPtr root;
 	ElementPtr root;
-	// The element that current has input focus.
+	// The element that currently has input focus.
 	Element* focus;
 	Element* focus;
 	// The top-most element being hovered over.
 	// The top-most element being hovered over.
 	Element* hover;
 	Element* hover;

+ 13 - 3
Include/RmlUi/Core/Element.h

@@ -283,12 +283,12 @@ public:
 	/// @return True if the pseudo-class is set on the element, false if not.
 	/// @return True if the pseudo-class is set on the element, false if not.
 	bool IsPseudoClassSet(const String& pseudo_class) const;
 	bool IsPseudoClassSet(const String& pseudo_class) const;
 	/// Checks if a complete set of pseudo-classes are set on the element.
 	/// Checks if a complete set of pseudo-classes are set on the element.
-	/// @param[in] pseudo_classes The set of pseudo-classes to check for.
+	/// @param[in] pseudo_classes The list of pseudo-classes to check for.
 	/// @return True if all of the pseudo-classes are set, false if not.
 	/// @return True if all of the pseudo-classes are set, false if not.
-	bool ArePseudoClassesSet(const PseudoClassList& pseudo_classes) const;
+	bool ArePseudoClassesSet(const StringList& pseudo_classes) const;
 	/// Gets a list of the current active pseudo-classes.
 	/// Gets a list of the current active pseudo-classes.
 	/// @return The list of active pseudo-classes.
 	/// @return The list of active pseudo-classes.
-	const PseudoClassList& GetActivePseudoClasses() const;
+	StringList GetActivePseudoClasses() const;
 	//@}
 	//@}
 
 
 	/** @name Attributes
 	/** @name Attributes
@@ -615,6 +615,10 @@ protected:
 	/// Called when properties on the element are changed.
 	/// Called when properties on the element are changed.
 	/// @param[in] changed_properties The properties changed on the element.
 	/// @param[in] changed_properties The properties changed on the element.
 	virtual void OnPropertyChange(const PropertyIdSet& changed_properties);
 	virtual void OnPropertyChange(const PropertyIdSet& changed_properties);
+	/// Called when a pseudo class on the element is changed.
+	/// @param[in] pseudo_class The pseudo class changed on the element.
+	/// @param[in] activate True if the pseudo class was activated.
+	virtual void OnPseudoClassChange(const String& pseudo_class, bool activate);
 
 
 	/// Called when a child node has been added up to two levels below us in the hierarchy.
 	/// Called when a child node has been added up to two levels below us in the hierarchy.
 	/// @param[in] child The element that has been added. This may be this element.
 	/// @param[in] child The element that has been added. This may be this element.
@@ -633,6 +637,12 @@ protected:
 	/// @param[out] content The content of this element and those under it, in XML form.
 	/// @param[out] content The content of this element and those under it, in XML form.
 	virtual void GetRML(String& content);
 	virtual void GetRML(String& content);
 
 
+	/// Sets or removes an overriding pseudo-class on the element.
+	/// @param[in] target_element The element to set or remove the pseudo class on.
+	/// @param[in] pseudo_class The pseudo class to activate or deactivate.
+	/// @param[in] activate True if the pseudo-class is to be activated, false to be deactivated.
+	static void OverridePseudoClass(Element* target_element, const String& pseudo_class, bool activate);
+
 	void SetOwnerDocument(ElementDocument* document);
 	void SetOwnerDocument(ElementDocument* document);
 
 
 	void Release() override;
 	void Release() override;

+ 0 - 1
Include/RmlUi/Core/Types.h

@@ -108,7 +108,6 @@ using OwnedElementList = Vector< ElementPtr >;
 using VariantList = Vector< Variant >;
 using VariantList = Vector< Variant >;
 using ElementAnimationList = Vector< ElementAnimation >;
 using ElementAnimationList = Vector< ElementAnimation >;
 
 
-using PseudoClassList = SmallUnorderedSet< String >;
 using AttributeNameList = SmallUnorderedSet< String >;
 using AttributeNameList = SmallUnorderedSet< String >;
 using PropertyMap = UnorderedMap< PropertyId, Property >;
 using PropertyMap = UnorderedMap< PropertyId, Property >;
 
 

+ 41 - 14
Source/Core/Element.cpp

@@ -328,11 +328,11 @@ String Element::GetAddress(bool include_pseudo_classes, bool include_parents) co
 
 
 	if (include_pseudo_classes)
 	if (include_pseudo_classes)
 	{
 	{
-		const PseudoClassList& pseudo_classes = meta->style.GetActivePseudoClasses();		
-		for (PseudoClassList::const_iterator i = pseudo_classes.begin(); i != pseudo_classes.end(); ++i)
+		const PseudoClassMap& pseudo_classes = meta->style.GetActivePseudoClasses();
+		for (auto& pseudo_class : pseudo_classes)
 		{
 		{
 			address += ":";
 			address += ":";
-			address += (*i);
+			address += pseudo_class.first;
 		}
 		}
 	}
 	}
 
 
@@ -746,7 +746,8 @@ PropertiesIteratorView Element::IterateLocalProperties() const
 // Sets or removes a pseudo-class on the element.
 // Sets or removes a pseudo-class on the element.
 void Element::SetPseudoClass(const String& pseudo_class, bool activate)
 void Element::SetPseudoClass(const String& pseudo_class, bool activate)
 {
 {
-	meta->style.SetPseudoClass(pseudo_class, activate);
+	if (meta->style.SetPseudoClass(pseudo_class, activate, false))
+		OnPseudoClassChange(pseudo_class, activate);
 }
 }
 
 
 // Checks if a specific pseudo-class has been set on the element.
 // Checks if a specific pseudo-class has been set on the element.
@@ -756,11 +757,11 @@ bool Element::IsPseudoClassSet(const String& pseudo_class) const
 }
 }
 
 
 // Checks if a complete set of pseudo-classes are set on the element.
 // Checks if a complete set of pseudo-classes are set on the element.
-bool Element::ArePseudoClassesSet(const PseudoClassList& pseudo_classes) const
+bool Element::ArePseudoClassesSet(const StringList& pseudo_classes) const
 {
 {
-	for (PseudoClassList::const_iterator i = pseudo_classes.begin(); i != pseudo_classes.end(); ++i)
+	for (const String& pseudo_class : pseudo_classes)
 	{
 	{
-		if (!IsPseudoClassSet(*i))
+		if (!IsPseudoClassSet(pseudo_class))
 			return false;
 			return false;
 	}
 	}
 
 
@@ -768,9 +769,23 @@ bool Element::ArePseudoClassesSet(const PseudoClassList& pseudo_classes) const
 }
 }
 
 
 // Gets a list of the current active pseudo classes
 // Gets a list of the current active pseudo classes
-const PseudoClassList& Element::GetActivePseudoClasses() const
+StringList Element::GetActivePseudoClasses() const
 {
 {
-	return meta->style.GetActivePseudoClasses();
+	const PseudoClassMap& pseudo_classes = meta->style.GetActivePseudoClasses();
+	StringList names;
+	names.reserve(pseudo_classes.size());
+	for (auto& pseudo_class : pseudo_classes)
+	{
+		names.push_back(pseudo_class.first);
+	}
+
+	return names;
+}
+
+void Element::OverridePseudoClass(Element* element, const String& pseudo_class, bool activate)
+{
+	RMLUI_ASSERT(element);
+	element->GetStyle()->SetPseudoClass(pseudo_class, activate, true);
 }
 }
 
 
 /// Get the named attribute
 /// Get the named attribute
@@ -1476,7 +1491,9 @@ void Element::GetElementsByClassName(ElementList& elements, const String& class_
 
 
 static Element* QuerySelectorMatchRecursive(const StyleSheetNodeListRaw& nodes, Element* element)
 static Element* QuerySelectorMatchRecursive(const StyleSheetNodeListRaw& nodes, Element* element)
 {
 {
-	for (int i = 0; i < element->GetNumChildren(); i++)
+	const int num_children = element->GetNumChildren();
+
+	for (int i = 0; i < num_children; i++)
 	{
 	{
 		Element* child = element->GetChild(i);
 		Element* child = element->GetChild(i);
 
 
@@ -1496,7 +1513,9 @@ static Element* QuerySelectorMatchRecursive(const StyleSheetNodeListRaw& nodes,
 
 
 static void QuerySelectorAllMatchRecursive(ElementList& matching_elements, const StyleSheetNodeListRaw& nodes, Element* element)
 static void QuerySelectorAllMatchRecursive(ElementList& matching_elements, const StyleSheetNodeListRaw& nodes, Element* element)
 {
 {
-	for (int i = 0; i < element->GetNumChildren(); i++)
+	const int num_children = element->GetNumChildren();
+
+	for (int i = 0; i < num_children; i++)
 	{
 	{
 		Element* child = element->GetChild(i);
 		Element* child = element->GetChild(i);
 
 
@@ -1842,6 +1861,10 @@ void Element::OnPropertyChange(const PropertyIdSet& changed_properties)
 	}
 	}
 }
 }
 
 
+void Element::OnPseudoClassChange(const String& /*pseudo_class*/, bool /*activate*/)
+{
+}
+
 // Called when a child node has been added somewhere in the hierarchy
 // Called when a child node has been added somewhere in the hierarchy
 void Element::OnChildAdd(Element* /*child*/)
 void Element::OnChildAdd(Element* /*child*/)
 {
 {
@@ -1871,9 +1894,13 @@ bool Element::IsLayoutDirty()
 
 
 void Element::ProcessDefaultAction(Event& event)
 void Element::ProcessDefaultAction(Event& event)
 {
 {
-	if (event == EventId::Mousedown && IsPointWithinElement(Vector2f(event.GetParameter< float >("mouse_x", 0), event.GetParameter< float >("mouse_y", 0))) &&
-		event.GetParameter< int >("button", 0) == 0)
-		SetPseudoClass("active", true);
+	if (event == EventId::Mousedown)
+	{
+		const Vector2f mouse_pos(event.GetParameter("mouse_x", 0.f), event.GetParameter("mouse_y", 0.f));
+
+		if (IsPointWithinElement(mouse_pos) && event.GetParameter("button", 0) == 0)
+			SetPseudoClass("active", true);
+	}
 
 
 	if (event == EventId::Mousescroll)
 	if (event == EventId::Mousescroll)
 	{
 	{

+ 32 - 8
Source/Core/ElementStyle.cpp

@@ -51,6 +51,16 @@
 
 
 namespace Rml {
 namespace Rml {
 
 
+// Bitwise operations on the PseudoClassState.
+inline PseudoClassState operator|(PseudoClassState lhs, PseudoClassState rhs)
+{
+	return PseudoClassState(int(lhs) | int(rhs));
+}
+inline PseudoClassState operator&(PseudoClassState lhs, PseudoClassState rhs)
+{
+	return PseudoClassState(int(lhs) & int(rhs));
+}
+
 ElementStyle::ElementStyle(Element* _element)
 ElementStyle::ElementStyle(Element* _element)
 {
 {
 	definition = nullptr;
 	definition = nullptr;
@@ -220,22 +230,36 @@ void ElementStyle::UpdateDefinition()
 	}
 	}
 }
 }
 
 
-
-
 // Sets or removes a pseudo-class on the element.
 // Sets or removes a pseudo-class on the element.
-void ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate)
+bool ElementStyle::SetPseudoClass(const String& pseudo_class, bool activate, bool override_class)
 {
 {
 	bool changed = false;
 	bool changed = false;
 
 
 	if (activate)
 	if (activate)
-		changed = pseudo_classes.insert(pseudo_class).second;
+	{
+		PseudoClassState& state = pseudo_classes[pseudo_class];
+		changed = (state == PseudoClassState::Clear);
+		state = (state | (override_class ? PseudoClassState::Override : PseudoClassState::Set));
+	}
 	else
 	else
-		changed = (pseudo_classes.erase(pseudo_class) == 1);
+	{
+		auto it = pseudo_classes.find(pseudo_class);
+		if (it != pseudo_classes.end())
+		{
+			PseudoClassState& state = it->second;
+			state = (state & (override_class ? PseudoClassState::Set : PseudoClassState::Override));
+			if (state == PseudoClassState::Clear)
+			{
+				pseudo_classes.erase(it);
+				changed = true;
+			}
+		}
+	}
 
 
 	if (changed)
 	if (changed)
-	{
 		DirtyDefinition();
 		DirtyDefinition();
-	}
+
+	return changed;
 }
 }
 
 
 // Checks if a specific pseudo-class has been set on the element.
 // Checks if a specific pseudo-class has been set on the element.
@@ -244,7 +268,7 @@ bool ElementStyle::IsPseudoClassSet(const String& pseudo_class) const
 	return (pseudo_classes.count(pseudo_class) == 1);
 	return (pseudo_classes.count(pseudo_class) == 1);
 }
 }
 
 
-const PseudoClassList& ElementStyle::GetActivePseudoClasses() const
+const PseudoClassMap& ElementStyle::GetActivePseudoClasses() const
 {
 {
 	return pseudo_classes;
 	return pseudo_classes;
 }
 }

+ 11 - 4
Source/Core/ElementStyle.h

@@ -40,6 +40,10 @@ class ElementDefinition;
 class PropertiesIterator;
 class PropertiesIterator;
 enum class RelativeTarget;
 enum class RelativeTarget;
 
 
+enum class PseudoClassState : std::uint8_t { Clear = 0, Set = 1, Override = 2 };
+using PseudoClassMap = SmallUnorderedMap< String, PseudoClassState >;
+
+
 /**
 /**
 	Manages an element's style and property information.
 	Manages an element's style and property information.
 	@author Lloyd Weehuizen
 	@author Lloyd Weehuizen
@@ -60,14 +64,17 @@ public:
 
 
 	/// Sets or removes a pseudo-class on the element.
 	/// Sets or removes a pseudo-class on the element.
 	/// @param[in] pseudo_class The pseudo class to activate or deactivate.
 	/// @param[in] pseudo_class The pseudo class to activate or deactivate.
-	/// @param[in] activate True if the pseudo-class is to be activated, false to be deactivated.
-	void SetPseudoClass(const String& pseudo_class, bool activate);
+	/// @param[in] activate True if the pseudo class is to be activated, false to be deactivated.
+	/// @param[in] override_class True to activate or deactivate the override state of the pseudo class, for advanced use cases.
+	/// @note An overriden pseudo class means that it will act as if activated even when it has been cleared the normal way.
+	/// @return True if the pseudo class was changed.
+	bool SetPseudoClass(const String& pseudo_class, bool activate, bool override_class = false);
 	/// Checks if a specific pseudo-class has been set on the element.
 	/// Checks if a specific pseudo-class has been set on the element.
 	/// @param[in] pseudo_class The name of the pseudo-class to check for.
 	/// @param[in] pseudo_class The name of the pseudo-class to check for.
 	/// @return True if the pseudo-class is set on the element, false if not.
 	/// @return True if the pseudo-class is set on the element, false if not.
 	bool IsPseudoClassSet(const String& pseudo_class) const;
 	bool IsPseudoClassSet(const String& pseudo_class) const;
 	/// Gets a list of the current active pseudo classes
 	/// Gets a list of the current active pseudo classes
-	const PseudoClassList& GetActivePseudoClasses() const;
+	const PseudoClassMap& GetActivePseudoClasses() const;
 
 
 	/// Sets or removes a class on the element.
 	/// Sets or removes a class on the element.
 	/// @param[in] class_name The name of the class to add or remove from the class list.
 	/// @param[in] class_name The name of the class to add or remove from the class list.
@@ -154,7 +161,7 @@ private:
 	// The list of classes applicable to this object.
 	// The list of classes applicable to this object.
 	StringList classes;
 	StringList classes;
 	// This element's current pseudo-classes.
 	// This element's current pseudo-classes.
-	PseudoClassList pseudo_classes;
+	PseudoClassMap pseudo_classes;
 
 
 	// Any properties that have been overridden in this element.
 	// Any properties that have been overridden in this element.
 	PropertyDictionary inline_properties;
 	PropertyDictionary inline_properties;

+ 115 - 0
Source/Core/Elements/ElementLabel.cpp

@@ -0,0 +1,115 @@
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "ElementLabel.h"
+
+namespace Rml {
+
+ElementLabel::ElementLabel(const String& tag) : Element(tag)
+{
+	AddEventListener(EventId::Click, this, true);
+}
+
+ElementLabel::~ElementLabel()
+{
+	RemoveEventListener(EventId::Click, this, true);
+}
+
+void ElementLabel::OnPseudoClassChange(const String& pseudo_class, bool activate)
+{
+	if (pseudo_class == "active" || pseudo_class == "hover")
+	{
+		if (Element* target = GetTarget())
+		{
+			OverridePseudoClass(target, pseudo_class, activate);
+		}
+	}
+}
+
+void ElementLabel::ProcessEvent(Event& event)
+{
+	// Forward clicks to the target.
+	if (event == EventId::Click && !disable_click)
+	{
+		if (event.GetPhase() == EventPhase::Capture || event.GetPhase() == EventPhase::Target)
+		{
+			if (Element* target = GetTarget())
+			{
+				// Temporarily disable click captures to avoid infinite recursion in case this element is on the path to the target element.
+				disable_click = true;
+				event.StopPropagation();
+				target->Focus();
+				target->Click();
+				disable_click = false;
+			}
+		}
+	}
+}
+
+// Get the first descending element whose tag name matches one of tags.
+static Element* TagMatchRecursive(const StringList& tags, Element* element)
+{
+	const int num_children = element->GetNumChildren();
+
+	for (int i = 0; i < num_children; i++)
+	{
+		Element* child = element->GetChild(i);
+
+		for (const String& tag : tags)
+		{
+			if (child->GetTagName() == tag)
+				return child;
+		}
+
+		Element* matching_element = TagMatchRecursive(tags, child);
+		if (matching_element)
+			return matching_element;
+	}
+
+	return nullptr;
+}
+
+Element* ElementLabel::GetTarget()
+{
+	const String target_id = GetAttribute<String>("for", "");
+
+	if (target_id.empty())
+	{
+		const StringList matching_tags = { "button", "input", "textarea", "progressbar", "select" };
+
+		return TagMatchRecursive(matching_tags, this);
+	}
+	else
+	{
+		return GetElementById(target_id);
+	}
+
+	return nullptr;
+}
+
+} // namespace Rml

+ 65 - 0
Source/Core/Elements/ElementLabel.h

@@ -0,0 +1,65 @@
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#ifndef RMLUI_CORE_ELEMENTS_ELEMENTLABEL_H
+#define RMLUI_CORE_ELEMENTS_ELEMENTLABEL_H
+
+#include "../../../Include/RmlUi/Core/Header.h"
+#include "../../../Include/RmlUi/Core/Element.h"
+#include "../../../Include/RmlUi/Core/EventListener.h"
+
+
+namespace Rml {
+
+/**
+	A specialisation of the generic Core::Element representing a label element.
+	
+ */
+
+class ElementLabel : public Element, public EventListener
+{
+public:
+	RMLUI_RTTI_DefineWithParent(ElementLabel, Element)
+
+	ElementLabel(const String& tag);
+	virtual ~ElementLabel();
+	
+protected:
+	void OnPseudoClassChange(const String& pseudo_class, bool activate) override;
+
+	void ProcessEvent(Event& event) override;
+
+private:
+	Element* GetTarget();
+
+	bool disable_click = false;
+};
+
+} // namespace Rml
+
+#endif

+ 3 - 0
Source/Core/Factory.cpp

@@ -79,6 +79,7 @@
 #include "XMLParseTools.h"
 #include "XMLParseTools.h"
 
 
 #include "Elements/ElementImage.h"
 #include "Elements/ElementImage.h"
+#include "Elements/ElementLabel.h"
 #include "Elements/ElementTextSelection.h"
 #include "Elements/ElementTextSelection.h"
 #include "Elements/XMLNodeHandlerDataGrid.h"
 #include "Elements/XMLNodeHandlerDataGrid.h"
 #include "Elements/XMLNodeHandlerTabSet.h"
 #include "Elements/XMLNodeHandlerTabSet.h"
@@ -142,6 +143,7 @@ struct DefaultInstancers {
 	ElementInstancerGeneric<ElementFormControlInput> input;
 	ElementInstancerGeneric<ElementFormControlInput> input;
 	ElementInstancerGeneric<ElementFormControlDataSelect> dataselect;
 	ElementInstancerGeneric<ElementFormControlDataSelect> dataselect;
 	ElementInstancerGeneric<ElementFormControlSelect> select;
 	ElementInstancerGeneric<ElementFormControlSelect> select;
+	ElementInstancerGeneric<ElementLabel> element_label;
 
 
 	ElementInstancerGeneric<ElementFormControlTextArea> textarea;
 	ElementInstancerGeneric<ElementFormControlTextArea> textarea;
 	ElementInstancerGeneric<ElementTextSelection> selection;
 	ElementInstancerGeneric<ElementTextSelection> selection;
@@ -234,6 +236,7 @@ bool Factory::Initialise()
 	RegisterElementInstancer("input", &default_instancers->input);
 	RegisterElementInstancer("input", &default_instancers->input);
 	RegisterElementInstancer("dataselect", &default_instancers->dataselect);
 	RegisterElementInstancer("dataselect", &default_instancers->dataselect);
 	RegisterElementInstancer("select", &default_instancers->select);
 	RegisterElementInstancer("select", &default_instancers->select);
+	RegisterElementInstancer("label", &default_instancers->element_label);
 
 
 	RegisterElementInstancer("textarea", &default_instancers->textarea);
 	RegisterElementInstancer("textarea", &default_instancers->textarea);
 	RegisterElementInstancer("#selection", &default_instancers->selection);
 	RegisterElementInstancer("#selection", &default_instancers->selection);

+ 12 - 4
Source/Debugger/ElementInfo.cpp

@@ -364,10 +364,18 @@ void ElementInfo::UpdateSourceElement()
 	// Set the pseudo classes
 	// Set the pseudo classes
 	if (Element* pseudo = GetElementById("pseudo"))
 	if (Element* pseudo = GetElementById("pseudo"))
 	{
 	{
-		PseudoClassList list;
+		StringList list;
 		if (source_element)
 		if (source_element)
 			list = source_element->GetActivePseudoClasses();
 			list = source_element->GetActivePseudoClasses();
 
 
+		auto EraseFromList = [](StringList& list, const String& value) {
+			auto it = std::find(list.begin(), list.end(), value);
+			if (it == list.end())
+				return false;
+			list.erase(it);
+			return true;
+		};
+
 		// There are some fixed pseudo classes that we always show and iterate through to determine if they are set.
 		// There are some fixed pseudo classes that we always show and iterate through to determine if they are set.
 		// We also want to show other pseudo classes when they are set, they are added under the #extra element last.
 		// We also want to show other pseudo classes when they are set, they are added under the #extra element last.
 		for (int i = 0; i < pseudo->GetNumChildren(); i++)
 		for (int i = 0; i < pseudo->GetNumChildren(); i++)
@@ -377,17 +385,17 @@ void ElementInfo::UpdateSourceElement()
 
 
 			if (!name.empty())
 			if (!name.empty())
 			{
 			{
-				bool active = (list.erase(name) == 1);
+				bool active = EraseFromList(list, name);
 				child->SetClass("active", active);
 				child->SetClass("active", active);
 			}
 			}
-			else if(child->GetId() == "extra")
+			else if (child->GetId() == "extra")
 			{
 			{
 				// First, we iterate through the extra elements and remove those that are no longer active.
 				// First, we iterate through the extra elements and remove those that are no longer active.
 				for (int j = 0; j < child->GetNumChildren(); j++)
 				for (int j = 0; j < child->GetNumChildren(); j++)
 				{
 				{
 					Element* grandchild = child->GetChild(j);
 					Element* grandchild = child->GetChild(j);
 					const String grandchild_name = grandchild->GetAttribute<String>("name", "");
 					const String grandchild_name = grandchild->GetAttribute<String>("name", "");
-					bool active = (list.erase(grandchild_name) == 1);
+					bool active = (EraseFromList(list, grandchild_name) == 1);
 					if(!active)
 					if(!active)
 						child->RemoveChild(grandchild);
 						child->RemoveChild(grandchild);
 				}
 				}

+ 1 - 0
changelog.md

@@ -113,6 +113,7 @@ Use the RCSS `display` property to enable table formatting. See the style sheet
 ### New RML elements
 ### New RML elements
 
 
 - Added [Lottie plugin](https://mikke89.github.io/RmlUiDoc/pages/cpp_manual/lottie.html) for displaying vector animations using the `<lottie>` element [#134](https://github.com/mikke89/RmlUi/pull/134) (thanks @diamondhat).
 - Added [Lottie plugin](https://mikke89.github.io/RmlUiDoc/pages/cpp_manual/lottie.html) for displaying vector animations using the `<lottie>` element [#134](https://github.com/mikke89/RmlUi/pull/134) (thanks @diamondhat).
+- Added `<label>` element for associating a caption with an input element.
 
 
 ### Element improvements
 ### Element improvements