ElementUtilities.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 ROCKETCOREELEMENTUTILITIES_H
  28. #define ROCKETCOREELEMENTUTILITIES_H
  29. #include "Header.h"
  30. #include "Box.h"
  31. #include "WString.h"
  32. #include "Types.h"
  33. namespace Rocket {
  34. namespace Core {
  35. class Context;
  36. class FontFaceHandle;
  37. class RenderInterface;
  38. /**
  39. Utility functions for dealing with elements.
  40. @author Lloyd Weehuizen
  41. */
  42. class ROCKETCORE_API ElementUtilities
  43. {
  44. public:
  45. enum PositionAnchor
  46. {
  47. TOP = 1 << 0,
  48. BOTTOM = 1 << 1,
  49. LEFT = 1 << 2,
  50. RIGHT = 1 << 3,
  51. TOP_LEFT = TOP | LEFT,
  52. TOP_RIGHT = TOP | RIGHT,
  53. BOTTOM_LEFT = BOTTOM | LEFT,
  54. BOTTOM_RIGHT = BOTTOM | RIGHT
  55. };
  56. /// Get the element with the given id.
  57. /// @param[in] root_element First element to check.
  58. /// @param[in] id ID of the element to look for.
  59. static Element* GetElementById(Element* root_element, const String& id);
  60. /// Get all elements with the given tag.
  61. /// @param[out] elements Resulting elements.
  62. /// @param[in] root_element First element to check.
  63. /// @param[in] tag Tag to search for.
  64. static void GetElementsByTagName(ElementList& elements, Element* root_element, const String& tag);
  65. /// Get all elements with the given class set on them.
  66. /// @param[out] elements Resulting elements.
  67. /// @param[in] root_element First element to check.
  68. /// @param[in] tag Class name to search for.
  69. static void GetElementsByClassName(ElementList& elements, Element* root_element, const String& class_name);
  70. /// Returns an element's font face.
  71. /// @param[in] element The element to determine the font face for.
  72. /// @return The element's font face. This will be NULL if no valid RCSS font styles have been set up for this element.
  73. static FontFaceHandle* GetFontFaceHandle(Element* element);
  74. /// Returns an element's font size, if it has a font defined.
  75. /// @param[in] element The element to determine the font size for.
  76. /// @return The font size as determined by the element's font, or 0 if it has no font specified.
  77. static int GetFontSize(Element* element);
  78. /// Returns an element's line height, if it has a font defined.
  79. /// @param[in] element The element to determine the line height for.
  80. /// @return The line height as specified by the element's font and line height styles.
  81. static int GetLineHeight(Element* element);
  82. /// Returns the width of a string rendered within the context of the given element.
  83. /// @param[in] element The element to measure the string from.
  84. /// @param[in] string The string to measure.
  85. /// @return The string width, in pixels.
  86. static int GetStringWidth(Element* element, const WString& string);
  87. /// Bind and instance all event attributes on the given element onto the element
  88. /// @param element Element to bind events on
  89. static void BindEventAttributes(Element* element);
  90. /// Generates the clipping region for an element.
  91. /// @param[out] clip_origin The origin, in context coordinates, of the origin of the element's clipping window.
  92. /// @param[out] clip_dimensions The size, in context coordinates, of the element's clipping window.
  93. /// @param[in] element The element to generate the clipping region for.
  94. /// @return True if a clipping region exists for the element and clip_origin and clip_window were set, false if not.
  95. static bool GetClippingRegion(Vector2i& clip_origin, Vector2i& clip_dimensions, Element* element);
  96. /// Sets the clipping region from an element and its ancestors.
  97. /// @param[in] element The element to generate the clipping region from.
  98. /// @param[in] context The context of the element; if this is not supplied, it will be derived from the element.
  99. /// @return The visibility of the given element within its clipping region.
  100. static bool SetClippingRegion(Element* element, Context* context = NULL);
  101. /// Applies the clip region from the render interface to the renderer
  102. /// @param[in] context The context to read the clip region from
  103. /// @param[in] render_interface The render interface to update.
  104. static void ApplyActiveClipRegion(Context* context, RenderInterface* render_interface);
  105. /// Formats the contents of an element. This does not need to be called for ordinary elements, but can be useful
  106. /// for non-DOM elements of custom elements.
  107. /// @param[in] element The element to lay out.
  108. /// @param[in] containing_block The size of the element's containing block.
  109. static bool FormatElement(Element* element, const Vector2f& containing_block);
  110. /// Generates the box for an element.
  111. /// @param[out] box The box to be built.
  112. /// @param[in] containing_block The dimensions of the content area of the block containing the element.
  113. /// @param[in] element The element to build the box for.
  114. /// @param[in] inline_element True if the element is placed in an inline context, false if not.
  115. static void BuildBox(Box& box, const Vector2f& containing_block, Element* element, bool inline_element = false);
  116. /// Sizes and positions an element within its parent. Any relative values will be evaluated against the size of the
  117. /// element parent's content area.
  118. /// @param element[in] The element to size and position.
  119. /// @param offset[in] The offset of the element inside its parent's content area.
  120. static bool PositionElement(Element* element, const Vector2f& offset);
  121. /// Sizes an element, and positions it within its parent offset from the borders of its content area. Any relative
  122. /// values will be evaluated against the size of the element parent's content area.
  123. /// @param element[in] The element to size and position.
  124. /// @param offset[in] The offset from the parent's borders.
  125. /// @param anchor[in] Defines which corner or edge the border is to be positioned relative to.
  126. static bool PositionElement(Element* element, const Vector2f& offset, PositionAnchor anchor);
  127. };
  128. }
  129. }
  130. #endif