2
0

FloatedBoxSpace.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_LAYOUT_FLOATEDBOXSPACE_H
  29. #define RMLUI_CORE_LAYOUT_FLOATEDBOXSPACE_H
  30. #include "../../../Include/RmlUi/Core/StyleTypes.h"
  31. #include "../../../Include/RmlUi/Core/Types.h"
  32. namespace Rml {
  33. class Element;
  34. class BlockContainer;
  35. enum class FloatedBoxEdge {
  36. Margin, // The box's margin area, used for layout sizing.
  37. Overflow, // Includes the box's border box plus any visible overflow, used to determine overflow and scrolling area.
  38. };
  39. /**
  40. Each block box has a space object for managing the space occupied by its floating elements, and those of its
  41. ancestors as relevant.
  42. */
  43. class FloatedBoxSpace {
  44. public:
  45. FloatedBoxSpace();
  46. ~FloatedBoxSpace();
  47. /// Generates the position for a box of a given size within our block box.
  48. /// @param[out] box_width The available width for the box.
  49. /// @param[in] cursor The ideal vertical position for the box.
  50. /// @param[in] dimensions The minimum available space required for the box.
  51. /// @param[in] nowrap Restrict from wrapping down, returned vertical position always placed at ideal cursor.
  52. /// @return The generated position for the box.
  53. Vector2f NextBoxPosition(const BlockContainer* parent, float& box_width, float cursor, Vector2f dimensions, bool nowrap) const;
  54. /// Determines the position of a floated element within our block box.
  55. /// @param[out] box_width The available width for the box.
  56. /// @param[in] cursor The ideal vertical position for the box.
  57. /// @param[in] dimensions The floated element's margin size.
  58. /// @param[in] float_property The element's computed float property.
  59. /// @param[in] clear_property The element's computed clear property.
  60. /// @return The next placement position for the float at its top-left margin position.
  61. Vector2f NextFloatPosition(const BlockContainer* parent, float& box_width, float cursor, Vector2f dimensions, Style::Float float_property,
  62. Style::Clear clear_property) const;
  63. /// Add a new entry into our list of floated boxes.
  64. void PlaceFloat(Style::Float float_property, Vector2f margin_position, Vector2f margin_size, Vector2f overflow_position, Vector2f overflow_size);
  65. /// Determines the appropriate vertical position for an object that is choosing to clear floating elements to
  66. /// the left or right (or both).
  67. /// @param[in] cursor The ideal vertical position.
  68. /// @param[in] clear_property The value of the clear property of the clearing object.
  69. /// @return The appropriate vertical position for the clearing object.
  70. float DetermineClearPosition(float cursor, Style::Clear clear_property) const;
  71. /// Returns the size of the rectangle encompassing all boxes within the space, relative to the block formatting context space.
  72. /// @param[in] edges Which edge of the boxes to encompass.
  73. Vector2f GetDimensions(FloatedBoxEdge edge) const;
  74. /// Get the width of the floated boxes for calculating the shrink-to-fit width.
  75. float GetShrinkToFitWidth(float edge_left, float edge_right) const;
  76. /// Clear all floating boxes placed in this space.
  77. void Reset()
  78. {
  79. for (auto& box_list : boxes)
  80. box_list.clear();
  81. extent_bottom_right_overflow = {};
  82. extent_bottom_right_overflow = {};
  83. extent_bottom_right_margin = {};
  84. }
  85. void* operator new(size_t size);
  86. void operator delete(void* chunk, size_t size);
  87. private:
  88. enum AnchorEdge { LEFT = 0, RIGHT = 1, NUM_ANCHOR_EDGES = 2 };
  89. // Generates the position for an arbitrary box within our space layout, floated against either the left or right edge.
  90. Vector2f NextBoxPosition(const BlockContainer* parent, float& maximum_box_width, float cursor, Vector2f dimensions, bool nowrap,
  91. Style::Float float_property) const;
  92. struct FloatedBox {
  93. Vector2f offset;
  94. Vector2f dimensions;
  95. };
  96. using FloatedBoxList = Vector<FloatedBox>;
  97. // The boxes floating in our space.
  98. FloatedBoxList boxes[NUM_ANCHOR_EDGES];
  99. // The rectangle encompassing all boxes added specifically into this space, relative to our block formatting context space.
  100. Vector2f extent_top_left_overflow;
  101. Vector2f extent_bottom_right_overflow;
  102. Vector2f extent_bottom_right_margin;
  103. };
  104. } // namespace Rml
  105. #endif