2
0

ElementStyle.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_ELEMENTSTYLE_H
  29. #define RMLUI_CORE_ELEMENTSTYLE_H
  30. #include "../../Include/RmlUi/Core/ComputedValues.h"
  31. #include "../../Include/RmlUi/Core/PropertyDictionary.h"
  32. #include "../../Include/RmlUi/Core/PropertyIdSet.h"
  33. #include "../../Include/RmlUi/Core/Types.h"
  34. namespace Rml {
  35. class ElementDefinition;
  36. class PropertiesIterator;
  37. enum class RelativeTarget;
  38. enum class PseudoClassState : uint8_t { Clear = 0, Set = 1, Override = 2 };
  39. using PseudoClassMap = SmallUnorderedMap<String, PseudoClassState>;
  40. /**
  41. Manages an element's style and property information.
  42. @author Lloyd Weehuizen
  43. */
  44. class ElementStyle {
  45. public:
  46. /// Constructor
  47. /// @param[in] element The element this structure belongs to.
  48. ElementStyle(Element* element);
  49. /// Update this definition if required
  50. void UpdateDefinition();
  51. /// Sets or removes a pseudo-class on the element.
  52. /// @param[in] pseudo_class The pseudo class to activate or deactivate.
  53. /// @param[in] activate True if the pseudo class is to be activated, false to be deactivated.
  54. /// @param[in] override_class True to activate or deactivate the override state of the pseudo class, for advanced use cases.
  55. /// @note An overridden pseudo class means that it will act as if activated even when it has been cleared the normal way.
  56. /// @return True if the pseudo class was changed.
  57. bool SetPseudoClass(const String& pseudo_class, bool activate, bool override_class = false);
  58. /// Checks if a specific pseudo-class has been set on the element.
  59. /// @param[in] pseudo_class The name of the pseudo-class to check for.
  60. /// @return True if the pseudo-class is set on the element, false if not.
  61. bool IsPseudoClassSet(const String& pseudo_class) const;
  62. /// Gets a list of the current active pseudo classes
  63. const PseudoClassMap& GetActivePseudoClasses() const;
  64. /// Sets or removes a class on the element.
  65. /// @param[in] class_name The name of the class to add or remove from the class list.
  66. /// @param[in] activate True if the class is to be added, false to be removed.
  67. /// @return True if the class was changed, false otherwise.
  68. bool SetClass(const String& class_name, bool activate);
  69. /// Checks if a class is set on the element.
  70. /// @param[in] class_name The name of the class to check for.
  71. /// @return True if the class is set on the element, false otherwise.
  72. bool IsClassSet(const String& class_name) const;
  73. /// Specifies the entire list of classes for this element. This will replace any others specified.
  74. /// @param[in] class_names The list of class names to set on the style, separated by spaces.
  75. void SetClassNames(const String& class_names);
  76. /// Return the active class list.
  77. /// @return A string containing all the classes on the element, separated by spaces.
  78. String GetClassNames() const;
  79. /// Return the active class list.
  80. const StringList& GetClassNameList() const;
  81. /// Sets a local property override on the element to a pre-parsed value.
  82. /// @param[in] id The ID of the new property.
  83. /// @param[in] property The parsed property to set.
  84. bool SetProperty(PropertyId id, const Property& property);
  85. /// Removes a local property override on the element; its value will revert to that defined in
  86. /// the style sheet.
  87. /// @param[in] id The ID of the local property definition to remove.
  88. void RemoveProperty(PropertyId id);
  89. /// Returns one of this element's properties. If this element is not defined this property, or a parent cannot
  90. /// be found that we can inherit the property from, the default value will be returned.
  91. /// @param[in] id The ID of the property to fetch the value for.
  92. /// @return The value of this property for this element, or nullptr if no property exists with the given name.
  93. const Property* GetProperty(PropertyId id) const;
  94. /// Returns one of this element's properties. If this element is not defined this property, nullptr will be
  95. /// returned.
  96. /// @param[in] id The ID of the property to fetch the value for.
  97. /// @return The value of this property for this element, or nullptr if this property has not been explicitly defined for this element.
  98. const Property* GetLocalProperty(PropertyId id) const;
  99. /// Returns the local style properties, excluding any properties from local class.
  100. const PropertyMap& GetLocalStyleProperties() const;
  101. /// Resolves a numeric value with units of number, percentage, length, or angle to their canonical unit (unit-less, 'px', or 'rad').
  102. /// @param[in] value The value to be resolved.
  103. /// @param[in] base_value The value that is scaled by the number or percentage value, if applicable.
  104. /// @return The resolved value in their canonical unit, or zero if it could not be resolved.
  105. float ResolveNumericValue(NumericValue value, float base_value) const;
  106. /// Resolves a property with units of number, length, or percentage to a length in 'px' units.
  107. /// Numbers and percentages are resolved by scaling the size of the specified target.
  108. float ResolveRelativeLength(NumericValue value, RelativeTarget relative_target) const;
  109. /// Mark inherited properties dirty.
  110. /// Inherited properties will automatically be set when parent inherited properties are changed. However,
  111. /// some operations may require to dirty these manually, such as when moving an element into another.
  112. void DirtyInheritedProperties();
  113. // Sets a single property as dirty.
  114. void DirtyProperty(PropertyId id);
  115. /// Dirties all properties with any of the given units (OR-ed together) on the current element (*not* recursive).
  116. void DirtyPropertiesWithUnits(Units units);
  117. /// Dirties all properties with any of the given units (OR-ed together) on the current element and recursively on all children.
  118. void DirtyPropertiesWithUnitsRecursive(Units units);
  119. /// Returns true if any properties are dirty such that computed values need to be recomputed
  120. bool AnyPropertiesDirty() const;
  121. /// Turns the local and inherited properties into computed values for this element. These values can in turn be used during the layout procedure.
  122. /// Must be called in correct order, always parent before its children.
  123. PropertyIdSet ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values,
  124. const Style::ComputedValues* document_values, bool values_are_default_initialized, float dp_ratio, Vector2f vp_dimensions);
  125. /// Returns an iterator for iterating the local properties of this element.
  126. /// Note: Modifying the element's style invalidates its iterator.
  127. PropertiesIterator Iterate() const;
  128. private:
  129. // Sets a list of properties as dirty.
  130. void DirtyProperties(const PropertyIdSet& properties);
  131. static const Property* GetLocalProperty(PropertyId id, const PropertyDictionary& inline_properties, const ElementDefinition* definition);
  132. static const Property* GetProperty(PropertyId id, const Element* element, const PropertyDictionary& inline_properties,
  133. const ElementDefinition* definition);
  134. static void TransitionPropertyChanges(Element* element, PropertyIdSet& properties, const PropertyDictionary& inline_properties,
  135. const ElementDefinition* old_definition, const ElementDefinition* new_definition);
  136. // Element these properties belong to
  137. Element* element;
  138. // The list of classes applicable to this object.
  139. StringList classes;
  140. // This element's current pseudo-classes.
  141. PseudoClassMap pseudo_classes;
  142. // Any properties that have been overridden in this element.
  143. PropertyDictionary inline_properties;
  144. // The definition of this element, provides applicable properties from the stylesheet.
  145. SharedPtr<const ElementDefinition> definition;
  146. PropertyIdSet dirty_properties;
  147. };
  148. } // namespace Rml
  149. #endif