ElementStyle.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "../../Include/Rocket/Core/Types.h"
  30. #include "DirtyPropertyList.h"
  31. namespace Rocket {
  32. namespace Core {
  33. class ElementDefinition;
  34. class PropertiesIterator;
  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.
  47. const ElementDefinition* GetDefinition() const;
  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 to a pre-parsed value.
  75. /// @param[in] name The name of the new property.
  76. /// @param[in] property The parsed property to set.
  77. bool SetProperty(PropertyId id, const Property& property);
  78. /// Removes a local property override on the element; its value will revert to that defined in
  79. /// the style sheet.
  80. /// @param[in] name The name of the local property definition to remove.
  81. void RemoveProperty(PropertyId id);
  82. /// Returns one of this element's properties. If this element is not defined this property, or a parent cannot
  83. /// be found that we can inherit the property from, the default value will be returned.
  84. /// @param[in] name The name of the property to fetch the value for.
  85. /// @return The value of this property for this element, or NULL if no property exists with the given name.
  86. const Property* GetProperty(PropertyId id) const;
  87. /// Returns one of this element's properties. If this element is not defined this property, NULL will be
  88. /// returned.
  89. /// @param[in] name The name of the property to fetch the value for.
  90. /// @return The value of this property for this element, or NULL if this property has not been explicitly defined for this element.
  91. const Property* GetLocalProperty(PropertyId id) const;
  92. /// Returns the local style properties, excluding any properties from local class.
  93. const PropertyMap& GetLocalStyleProperties() const;
  94. /// Returns the active style sheet for this element. This may be NULL.
  95. StyleSheet* GetStyleSheet() const;
  96. /// Resolves a property with units of length or percentage to 'px'. Percentages are resolved by scaling the base value.
  97. /// @param[in] name The property to resolve the value for.
  98. /// @param[in] base_value The value that is scaled by the percentage value, if it is a percentage.
  99. /// @return The resolved value in 'px' unit.
  100. float ResolveLengthPercentage(const Property *property, float base_value) const;
  101. /// Resolves a property with units of number, length or percentage. Lengths are resolved to 'px'.
  102. /// Number and percentages are resolved by scaling the size of the specified target.
  103. float ResolveNumberLengthPercentage(const Property* property, RelativeTarget relative_target) const;
  104. /// Mark definition and all children dirty
  105. void DirtyDefinition();
  106. /// Dirties all properties with a given unit on the current element and recursively on all children.
  107. void DirtyPropertiesWithUnitRecursive(Property::Unit unit);
  108. /// Returns true if any properties are dirty such that computed values need to be recomputed
  109. bool AnyPropertiesDirty() const;
  110. /// Turns the local and inherited properties into computed values for this element. These values can in turn be used during the layout procedure.
  111. /// Must be called in correct order, always parent before its children.
  112. DirtyPropertyList ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, bool values_are_default_initialized, float dp_ratio);
  113. /// Returns an iterator for iterating the local properties of this element.
  114. /// Note: Modifying the element's style invalidates its iterator.
  115. PropertiesIterator Iterate() const;
  116. private:
  117. // Dirty all child definitions
  118. void DirtyChildDefinitions();
  119. // Sets a single property as dirty.
  120. void DirtyProperty(PropertyId id);
  121. // Sets a list of properties as dirty.
  122. void DirtyProperties(const PropertyNameList& properties);
  123. // Sets a list of our potentially inherited properties as dirtied by an ancestor.
  124. void DirtyInheritedProperties(const PropertyNameList& properties);
  125. static const Property* GetLocalProperty(PropertyId id, const PropertyDictionary & local_properties, const ElementDefinition * definition);
  126. static const Property* GetProperty(PropertyId id, const Element * element, const PropertyDictionary & local_properties, const ElementDefinition * definition);
  127. static void TransitionPropertyChanges(Element * element, PropertyNameList & properties, const PropertyDictionary & local_properties, const ElementDefinition * old_definition, const ElementDefinition * new_definition);
  128. // Element these properties belong to
  129. Element* element;
  130. // The list of classes applicable to this object.
  131. StringList classes;
  132. // This element's current pseudo-classes.
  133. PseudoClassList pseudo_classes;
  134. // Any properties that have been overridden in this element.
  135. PropertyDictionary local_properties;
  136. // The definition of this element; if this is NULL one will be fetched from the element's style.
  137. ElementDefinition* definition;
  138. // Set if a new element definition should be fetched from the style.
  139. bool definition_dirty;
  140. DirtyPropertyList dirty_properties;
  141. };
  142. }
  143. }
  144. #endif