Box.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 ROCKETCOREBOX_H
  28. #define ROCKETCOREBOX_H
  29. #include "Types.h"
  30. namespace Rocket {
  31. namespace Core {
  32. /**
  33. Stores a box with four sized areas; content, padding, a border and margin. See
  34. http://www.w3.org/TR/REC-CSS2/box.html#box-dimensions for a diagram.
  35. @author Peter Curry
  36. */
  37. class ROCKETCORE_API Box
  38. {
  39. public:
  40. enum Area
  41. {
  42. MARGIN = 0,
  43. BORDER = 1,
  44. PADDING = 2,
  45. CONTENT = 3,
  46. NUM_AREAS = 3, // ignores CONTENT
  47. };
  48. enum Edge
  49. {
  50. TOP = 0,
  51. RIGHT = 1,
  52. BOTTOM = 2,
  53. LEFT = 3,
  54. NUM_EDGES = 4
  55. };
  56. /// Initialises a zero-sized box.
  57. Box();
  58. /// Initialises a box with a default content area and no padding, borders and margins.
  59. Box(const Vector2f& content);
  60. ~Box();
  61. /// Returns the offset of this box. This will usually be (0, 0).
  62. /// @return The box's offset.
  63. const Vector2f& GetOffset() const;
  64. /// Returns the top-left position of one of the box's areas, relative to the top-left of the border area. This
  65. /// means the position of the margin area is likely to be negative.
  66. /// @param area[in] The desired area.
  67. /// @return The position of the area.
  68. Vector2f GetPosition(Area area = Box::CONTENT) const;
  69. /// Returns the size of one of the box's areas. This will include all inner areas.
  70. /// @param area[in] The desired area.
  71. /// @return The size of the requested area.
  72. Vector2f GetSize(Area area = Box::CONTENT) const;
  73. /// Sets the offset of the box, relative usually to the owning element. This should only be set for auxiliary
  74. /// boxes of an element.
  75. /// @param offset[in] The offset of the box from the primary box.
  76. void SetOffset(const Vector2f& offset);
  77. /// Sets the size of the content area.
  78. /// @param content[in] The size of the new content area.
  79. void SetContent(const Vector2f& content);
  80. /// Sets the size of one of the edges of one of the box's outer areas.
  81. /// @param area[in] The area to change.
  82. /// @param edge[in] The area edge to change.
  83. /// @param size[in] The new size of the area segment.
  84. void SetEdge(Area area, Edge edge, float size);
  85. /// Returns the size of one of the area edges.
  86. /// @param area[in] The desired area.
  87. /// @param edge[in] The desired edge.
  88. /// @return The size of the requested area edge.
  89. float GetEdge(Area area, Edge edge) const;
  90. /// Returns the cumulative size of one edge up to one of the box's areas.
  91. /// @param area[in] The area to measure up to (and including). So, MARGIN will return the width of the margin, and PADDING will be the sum of the margin, border and padding.
  92. /// @param edge[in] The desired edge.
  93. /// @return The cumulative size of the edge.
  94. float GetCumulativeEdge(Area area, Edge edge) const;
  95. /// Compares the size of the content area and the other area edges.
  96. /// @return True if the boxes represent the same area.
  97. bool operator==(const Box& rhs) const;
  98. /// Compares the size of the content area and the other area edges.
  99. /// @return True if the boxes do not represent the same area.
  100. bool operator!=(const Box& rhs) const;
  101. private:
  102. Vector2f content;
  103. float area_edges[NUM_AREAS][NUM_EDGES];
  104. Vector2f offset;
  105. };
  106. }
  107. }
  108. #endif