ElementStyle.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 ElementStyleCache;
  34. /**
  35. Manages an element's style and property information.
  36. @author Lloyd Weehuizen
  37. */
  38. class ElementStyle
  39. {
  40. public:
  41. /// Constructor
  42. /// @param[in] element The element this structure belongs to.
  43. ElementStyle(Element* element);
  44. ~ElementStyle();
  45. /// Returns the element's definition, updating if necessary.
  46. const ElementDefinition* GetDefinition();
  47. /// Update this definition if required
  48. void UpdateDefinition();
  49. /// Sets or removes a pseudo-class on the element.
  50. /// @param[in] pseudo_class The pseudo class to activate or deactivate.
  51. /// @param[in] activate True if the pseudo-class is to be activated, false to be deactivated.
  52. void SetPseudoClass(const String& pseudo_class, bool activate);
  53. /// Checks if a specific pseudo-class has been set on the element.
  54. /// @param[in] pseudo_class The name of the pseudo-class to check for.
  55. /// @return True if the pseudo-class is set on the element, false if not.
  56. bool IsPseudoClassSet(const String& pseudo_class) const;
  57. /// Gets a list of the current active pseudo classes
  58. const PseudoClassList& GetActivePseudoClasses() const;
  59. /// Sets or removes a class on the element.
  60. /// @param[in] class_name The name of the class to add or remove from the class list.
  61. /// @param[in] activate True if the class is to be added, false to be removed.
  62. void SetClass(const String& class_name, bool activate);
  63. /// Checks if a class is set on the element.
  64. /// @param[in] class_name The name of the class to check for.
  65. /// @return True if the class is set on the element, false otherwise.
  66. bool IsClassSet(const String& class_name) const;
  67. /// Specifies the entire list of classes for this element. This will replace any others specified.
  68. /// @param[in] class_names The list of class names to set on the style, separated by spaces.
  69. void SetClassNames(const String& class_names);
  70. /// Return the active class list.
  71. /// @return A string containing all the classes on the element, separated by spaces.
  72. String GetClassNames() const;
  73. /// Sets a local property override on the element.
  74. /// @param[in] name The name of the new property.
  75. /// @param[in] property The new property to set.
  76. bool SetProperty(const String& name, const String& value);
  77. /// Sets a local property override on the element to a pre-parsed value.
  78. /// @param[in] name The name of the new property.
  79. /// @param[in] property The parsed property to set.
  80. bool SetProperty(const String& name, const Property& property);
  81. /// Removes a local property override on the element; its value will revert to that defined in
  82. /// the style sheet.
  83. /// @param[in] name The name of the local property definition to remove.
  84. void RemoveProperty(const String& name);
  85. /// Returns one of this element's properties. If this element is not defined this property, or a parent cannot
  86. /// be found that we can inherit the property from, the default value will be returned.
  87. /// @param[in] name The name of the property to fetch the value for.
  88. /// @return The value of this property for this element, or NULL if no property exists with the given name.
  89. const Property* GetProperty(const String& name);
  90. /// Returns one of this element's properties. If this element is not defined this property, NULL will be
  91. /// returned.
  92. /// @param[in] name The name of the property to fetch the value for.
  93. /// @return The value of this property for this element, or NULL if this property has not been explicitly defined for this element.
  94. const Property* GetLocalProperty(const String& name);
  95. /// Returns the local properties, excluding any properties from local class.
  96. /// @return The local properties for this element, or NULL if no properties defined
  97. const PropertyMap* GetLocalProperties() const;
  98. /// Resolves a length property to pixels. Note: This excludes percentages.
  99. float ResolveLength(const Property* property);
  100. /// Resolves an angle to radians
  101. static float ResolveAngle(const Property* property);
  102. /// Resolves a number-length-percentage property to pixels.
  103. float ResolveNumericProperty(const String& property_name, const Property* property);
  104. /// Resolves the canonical unit (pixels) from 'number-length-percent' property.
  105. /// 'percentage' and 'number' gets multiplied by the size of the specified relative reference.
  106. float ResolveNumericProperty(const Property* property, RelativeTarget relative_target);
  107. /// Resolves one of this element's properties. If the value is a number or px, this is returned. If it's a
  108. /// percentage then it is resolved based on the second argument (the base value).
  109. /// If it's an angle, it is returned as radians.
  110. /// @param[in] property Property to resolve the value for.
  111. /// @param[in] base_value The value that is scaled by the percentage value, if it is a percentage.
  112. /// @return The value of this property for this element.
  113. float ResolveProperty(const Property *property, float base_value);
  114. /// Resolves one of this element's properties. If the value is a number or px, this is returned. If it's a
  115. /// percentage then it is resolved based on the second argument (the base value).
  116. /// If it's an angle, it is returned as radians.
  117. /// @param[in] name The name of the property to resolve the value for.
  118. /// @param[in] base_value The value that is scaled by the percentage value, if it is a percentage.
  119. /// @return The value of this property for this element.
  120. float ResolveProperty(const String& name, float base_value);
  121. /// Iterates over the properties defined on the element.
  122. /// @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.
  123. /// @param[out] pseudo_classes The pseudo-classes the property is defined by.
  124. /// @param[out] name The name of the property at the specified index.
  125. /// @param[out] property The property at the specified index.
  126. /// @return True if a property was successfully fetched.
  127. bool IterateProperties(int& index, PseudoClassList& pseudo_classes, String& name, const Property*& property);
  128. /// Returns the active style sheet for this element. This may be NULL.
  129. StyleSheet* GetStyleSheet() const;
  130. /// Mark definition and all children dirty
  131. void DirtyDefinition();
  132. /// Dirty all child definitions
  133. void DirtyChildDefinitions();
  134. // Dirties every property.
  135. void DirtyProperties();
  136. // Dirties em-relative properties.
  137. void DirtyEmProperties();
  138. // Dirties font-size on child elements if appropriate.
  139. void DirtyInheritedEmProperties();
  140. // Dirties rem properties.
  141. void DirtyRemProperties();
  142. // Dirties dp properties.
  143. void DirtyDpProperties();
  144. /// Returns 'top', 'bottom', 'left' and 'right' properties from element's style or local cache.
  145. void GetOffsetProperties(const Property **top, const Property **bottom, const Property **left, const Property **right );
  146. /// Returns 'border-width' properties from element's style or local cache.
  147. void GetBorderWidthProperties(const Property **border_top_width, const Property **border_bottom_width, const Property **border_left_width, const Property **border_right_width);
  148. /// Returns 'margin' properties from element's style or local cache.
  149. void GetMarginProperties(const Property **margin_top, const Property **margin_bottom, const Property **margin_left, const Property **margin_right);
  150. /// Returns 'padding' properties from element's style or local cache.
  151. void GetPaddingProperties(const Property **padding_top, const Property **padding_bottom, const Property **padding_left, const Property **padding_right);
  152. /// Returns 'width' and 'height' properties from element's style or local cache.
  153. void GetDimensionProperties(const Property **width, const Property **height);
  154. /// Returns local 'width' and 'height' properties from element's style or local cache,
  155. /// ignoring default values.
  156. void GetLocalDimensionProperties(const Property **width, const Property **height);
  157. /// Returns 'overflow' properties' values from element's style or local cache.
  158. void GetOverflow(int *overflow_x, int *overflow_y);
  159. /// Returns 'position' property value from element's style or local cache.
  160. int GetPosition();
  161. /// Returns 'float' property value from element's style or local cache.
  162. int GetFloat();
  163. /// Returns 'display' property value from element's style or local cache.
  164. int GetDisplay();
  165. /// Returns 'white-space' property value from element's style or local cache.
  166. int GetWhitespace();
  167. /// Returns 'pointer-events' property value from element's style or local cache.
  168. int GetPointerEvents();
  169. /// Returns 'line-height' property value from element's style or local cache.
  170. const Property *GetLineHeightProperty();
  171. /// Returns 'text-align' property value from element's style or local cache.
  172. int GetTextAlign();
  173. /// Returns 'text-transform' property value from element's style or local cache.
  174. int GetTextTransform();
  175. /// Returns 'vertical-align' property value from element's style or local cache.
  176. const Property *GetVerticalAlignProperty();
  177. /// Returns 'perspective' property value from element's style or local cache.
  178. const Property *GetPerspective();
  179. /// Returns 'perspective-origin-x' property value from element's style or local cache.
  180. const Property *GetPerspectiveOriginX();
  181. /// Returns 'perspective-origin-y' property value from element's style or local cache.
  182. const Property *GetPerspectiveOriginY();
  183. /// Returns 'transform' property value from element's style or local cache.
  184. const Property *GetTransform();
  185. /// Returns 'transform-origin-x' property value from element's style or local cache.
  186. const Property *GetTransformOriginX();
  187. /// Returns 'transform-origin-y' property value from element's style or local cache.
  188. const Property *GetTransformOriginY();
  189. /// Returns 'transform-origin-z' property value from element's style or local cache.
  190. const Property *GetTransformOriginZ();
  191. private:
  192. // Sets a single property as dirty.
  193. void DirtyProperty(const String& property);
  194. // Sets a list of properties as dirty.
  195. void DirtyProperties(const PropertyNameList& properties, bool clear_em_properties = true);
  196. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  197. void DirtyInheritedProperties(const PropertyNameList& properties);
  198. static const Property* GetLocalProperty(const String & name, PropertyDictionary * local_properties, ElementDefinition * definition, const PseudoClassList & pseudo_classes);
  199. static const Property* GetProperty(const String & name, Element * element, PropertyDictionary * local_properties, ElementDefinition * definition, const PseudoClassList & pseudo_classes);
  200. static void TransitionPropertyChanges(Element * element, PropertyNameList & properties, PropertyDictionary * local_properties, ElementDefinition * old_definition, ElementDefinition * new_definition,
  201. const PseudoClassList & pseudo_classes_before, const PseudoClassList & pseudo_classes_after);
  202. // Element these properties belong to
  203. Element* element;
  204. // The list of classes applicable to this object.
  205. StringList classes;
  206. // This element's current pseudo-classes.
  207. PseudoClassList pseudo_classes;
  208. // Any properties that have been overridden in this element.
  209. PropertyDictionary* local_properties;
  210. // All properties (including inherited) that are EM-relative.
  211. PropertyNameList* em_properties;
  212. // The definition of this element; if this is NULL one will be fetched from the element's style.
  213. ElementDefinition* definition;
  214. // Set if a new element definition should be fetched from the style.
  215. bool definition_dirty;
  216. // Set if a child element has a dirty style definition
  217. bool child_definition_dirty;
  218. // cached non-inherited properties
  219. ElementStyleCache *cache;
  220. };
  221. }
  222. }
  223. #endif