ElementStyle.h 13 KB

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