ElementStyle.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCOREELEMENTSTYLE_H
  28. #define ROCKETCOREELEMENTSTYLE_H
  29. #include "ElementDefinition.h"
  30. #include "../../Include/Rocket/Core/Types.h"
  31. namespace Rocket {
  32. namespace Core {
  33. class DirtyPropertyList {
  34. private:
  35. bool all_dirty = false;
  36. PropertyNameList dirty_list;
  37. public:
  38. DirtyPropertyList(bool all_dirty = false) : all_dirty(all_dirty) {}
  39. void Insert(const String& property_name) {
  40. if (all_dirty) return;
  41. dirty_list.insert(property_name);
  42. }
  43. void Insert(const PropertyNameList& properties) {
  44. if (all_dirty) return;
  45. // @performance: Can be made O(N+M)
  46. dirty_list.insert(properties.begin(), properties.end());
  47. }
  48. void DirtyAll() {
  49. all_dirty = true;
  50. dirty_list.clear();
  51. }
  52. void Clear() {
  53. all_dirty = false;
  54. dirty_list.clear();
  55. }
  56. bool Empty() const {
  57. return !all_dirty && dirty_list.empty();
  58. }
  59. bool Contains(const String & name) const {
  60. if (all_dirty)
  61. return true;
  62. auto it = dirty_list.find(name);
  63. return (it != dirty_list.end());
  64. }
  65. bool AllDirty() const {
  66. return all_dirty;
  67. }
  68. const PropertyNameList& GetList() const {
  69. return dirty_list;
  70. }
  71. };
  72. /**
  73. Manages an element's style and property information.
  74. @author Lloyd Weehuizen
  75. */
  76. class ElementStyle
  77. {
  78. public:
  79. /// Constructor
  80. /// @param[in] element The element this structure belongs to.
  81. ElementStyle(Element* element);
  82. ~ElementStyle();
  83. /// Returns the element's definition, updating if necessary.
  84. const ElementDefinition* GetDefinition();
  85. /// Update this definition if required
  86. void UpdateDefinition();
  87. /// Sets or removes a pseudo-class on the element.
  88. /// @param[in] pseudo_class The pseudo class to activate or deactivate.
  89. /// @param[in] activate True if the pseudo-class is to be activated, false to be deactivated.
  90. void SetPseudoClass(const String& pseudo_class, bool activate);
  91. /// Checks if a specific pseudo-class has been set on the element.
  92. /// @param[in] pseudo_class The name of the pseudo-class to check for.
  93. /// @return True if the pseudo-class is set on the element, false if not.
  94. bool IsPseudoClassSet(const String& pseudo_class) const;
  95. /// Gets a list of the current active pseudo classes
  96. const PseudoClassList& GetActivePseudoClasses() const;
  97. /// Sets or removes a class on the element.
  98. /// @param[in] class_name The name of the class to add or remove from the class list.
  99. /// @param[in] activate True if the class is to be added, false to be removed.
  100. void SetClass(const String& class_name, bool activate);
  101. /// Checks if a class is set on the element.
  102. /// @param[in] class_name The name of the class to check for.
  103. /// @return True if the class is set on the element, false otherwise.
  104. bool IsClassSet(const String& class_name) const;
  105. /// Specifies the entire list of classes for this element. This will replace any others specified.
  106. /// @param[in] class_names The list of class names to set on the style, separated by spaces.
  107. void SetClassNames(const String& class_names);
  108. /// Return the active class list.
  109. /// @return A string containing all the classes on the element, separated by spaces.
  110. String GetClassNames() const;
  111. /// Sets a local property override on the element.
  112. /// @param[in] name The name of the new property.
  113. /// @param[in] property The new property to set.
  114. bool SetProperty(const String& name, const String& value);
  115. /// Sets a local property override on the element to a pre-parsed value.
  116. /// @param[in] name The name of the new property.
  117. /// @param[in] property The parsed property to set.
  118. bool SetProperty(const String& name, const Property& property);
  119. /// Removes a local property override on the element; its value will revert to that defined in
  120. /// the style sheet.
  121. /// @param[in] name The name of the local property definition to remove.
  122. void RemoveProperty(const String& name);
  123. /// Returns one of this element's properties. If this element is not defined this property, or a parent cannot
  124. /// be found that we can inherit the property from, the default value will be returned.
  125. /// @param[in] name The name of the property to fetch the value for.
  126. /// @return The value of this property for this element, or NULL if no property exists with the given name.
  127. const Property* GetProperty(const String& name);
  128. /// Returns one of this element's properties. If this element is not defined this property, NULL will be
  129. /// returned.
  130. /// @param[in] name The name of the property to fetch the value for.
  131. /// @return The value of this property for this element, or NULL if this property has not been explicitly defined for this element.
  132. const Property* GetLocalProperty(const String& name);
  133. /// Returns the local properties, excluding any properties from local class.
  134. /// @return The local properties for this element, or NULL if no properties defined
  135. const PropertyMap* GetLocalProperties() const;
  136. /// Resolves a property with units of length or percentage to 'px'. Percentages are resolved by scaling the base value.
  137. /// @param[in] name The property to resolve the value for.
  138. /// @param[in] base_value The value that is scaled by the percentage value, if it is a percentage.
  139. /// @return The resolved value in 'px' unit.
  140. float ResolveLengthPercentage(const Property *property, float base_value);
  141. /// Resolves a property with units of number, length or percentage. Lengths are resolved to 'px'.
  142. /// Number and percentages are resolved by scaling the size of the specified target.
  143. float ResolveNumberLengthPercentage(const Property* property, RelativeTarget relative_target);
  144. /// Iterates over the properties defined on the element.
  145. /// @param[inout] index Index of the property to fetch. This is incremented to the next valid index after the fetch. Indices are not necessarily incremental.
  146. /// @param[out] pseudo_classes The pseudo-classes the property is defined by.
  147. /// @param[out] name The name of the property at the specified index.
  148. /// @param[out] property The property at the specified index.
  149. /// @return True if a property was successfully fetched.
  150. bool IterateProperties(int& index, String& name, const Property*& property, const PseudoClassList** pseudo_classes = nullptr) const;
  151. /// Returns the active style sheet for this element. This may be NULL.
  152. StyleSheet* GetStyleSheet() const;
  153. /// Mark definition and all children dirty
  154. void DirtyDefinition();
  155. /// Dirty all child definitions
  156. void DirtyChildDefinitions();
  157. /// Dirties rem properties.
  158. void DirtyRemProperties();
  159. /// Dirties dp properties.
  160. void DirtyDpProperties();
  161. /// Returns true if any properties are dirty such that computed values need to be recomputed
  162. bool AnyPropertiesDirty() const;
  163. /// Turns the local and inherited properties into computed values for this element. These values can in turn be used during the layout procedure.
  164. /// Must be called in correct order, always parent before its children.
  165. DirtyPropertyList ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, bool values_are_default_initialized, float dp_ratio);
  166. /// Returns an iterator to the first property defined on this element.
  167. /// Note: Modifying the element's style invalidates its iterators.
  168. ElementStyleIterator begin() const;
  169. /// Returns an iterator to the property following the last property defined on this element.
  170. ElementStyleIterator end() const;
  171. private:
  172. // Sets a single property as dirty.
  173. void DirtyProperty(const String& property);
  174. // Sets a list of properties as dirty.
  175. void DirtyProperties(const PropertyNameList& properties);
  176. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  177. void DirtyInheritedProperties(const PropertyNameList& properties);
  178. static const Property* GetLocalProperty(const String & name, PropertyDictionary * local_properties, ElementDefinition * definition, const PseudoClassList & pseudo_classes);
  179. static const Property* GetProperty(const String & name, Element * element, PropertyDictionary * local_properties, ElementDefinition * definition, const PseudoClassList & pseudo_classes);
  180. static void TransitionPropertyChanges(Element * element, PropertyNameList & properties, PropertyDictionary * local_properties, ElementDefinition * old_definition, ElementDefinition * new_definition,
  181. const PseudoClassList & pseudo_classes_before, const PseudoClassList & pseudo_classes_after);
  182. // Element these properties belong to
  183. Element* element;
  184. // The list of classes applicable to this object.
  185. StringList classes;
  186. // This element's current pseudo-classes.
  187. PseudoClassList pseudo_classes;
  188. // Any properties that have been overridden in this element.
  189. PropertyDictionary* local_properties;
  190. // The definition of this element; if this is NULL one will be fetched from the element's style.
  191. ElementDefinition* definition;
  192. // Set if a new element definition should be fetched from the style.
  193. bool definition_dirty;
  194. DirtyPropertyList dirty_properties;
  195. };
  196. }
  197. }
  198. #endif