juce_GridItem.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. /**
  22. Defines an item in a Grid
  23. @see Grid
  24. @tags{GUI}
  25. */
  26. class JUCE_API GridItem
  27. {
  28. public:
  29. enum class Keyword { autoValue };
  30. //==============================================================================
  31. /** Represents a span. */
  32. struct Span
  33. {
  34. explicit Span (int numberToUse) noexcept : number (numberToUse)
  35. {
  36. /* Span must be at least one and positive */
  37. jassert (numberToUse > 0);
  38. }
  39. explicit Span (int numberToUse, const String& nameToUse) : Span (numberToUse)
  40. {
  41. /* Name must not be empty */
  42. jassert (nameToUse.isNotEmpty());
  43. name = nameToUse;
  44. }
  45. explicit Span (const String& nameToUse) : name (nameToUse)
  46. {
  47. /* Name must not be empty */
  48. jassert (nameToUse.isNotEmpty());
  49. }
  50. int number = 1;
  51. String name;
  52. };
  53. //==============================================================================
  54. /** Represents a property. */
  55. struct Property
  56. {
  57. Property() noexcept;
  58. Property (Keyword keyword) noexcept;
  59. Property (const char* lineNameToUse) noexcept;
  60. Property (const String& lineNameToUse) noexcept;
  61. Property (int numberToUse) noexcept;
  62. Property (int numberToUse, const String& lineNameToUse) noexcept;
  63. Property (Span spanToUse) noexcept;
  64. bool hasSpan() const noexcept { return isSpan && ! isAuto; }
  65. bool hasAbsolute() const noexcept { return ! (isSpan || isAuto); }
  66. bool hasAuto() const noexcept { return isAuto; }
  67. bool hasName() const noexcept { return name.isNotEmpty(); }
  68. const String& getName() const noexcept { return name; }
  69. int getNumber() const noexcept { return number; }
  70. private:
  71. String name;
  72. int number = 1; /** Either an absolute line number or number of lines to span across. */
  73. bool isSpan = false;
  74. bool isAuto = false;
  75. };
  76. //==============================================================================
  77. /** Represents start and end properties. */
  78. struct StartAndEndProperty { Property start, end; };
  79. //==============================================================================
  80. /** Possible values for the justifySelf property. */
  81. enum class JustifySelf : int
  82. {
  83. start = 0, /**< Content inside the item is justified towards the left. */
  84. end, /**< Content inside the item is justified towards the right. */
  85. center, /**< Content inside the item is justified towards the center. */
  86. stretch, /**< Content inside the item is stretched from left to right. */
  87. autoValue /**< Follows the Grid container's justifyItems property. */
  88. };
  89. /** Possible values for the alignSelf property. */
  90. enum class AlignSelf : int
  91. {
  92. start = 0, /**< Content inside the item is aligned towards the top. */
  93. end, /**< Content inside the item is aligned towards the bottom. */
  94. center, /**< Content inside the item is aligned towards the center. */
  95. stretch, /**< Content inside the item is stretched from top to bottom. */
  96. autoValue /**< Follows the Grid container's alignItems property. */
  97. };
  98. /** Creates an item with default parameters. */
  99. GridItem() noexcept;
  100. /** Creates an item with a given Component to use. */
  101. GridItem (Component& componentToUse) noexcept;
  102. /** Creates an item with a given Component to use. */
  103. GridItem (Component* componentToUse) noexcept;
  104. /** Destructor. */
  105. ~GridItem() noexcept;
  106. //==============================================================================
  107. /** If this is non-null, it represents a Component whose bounds are controlled by this item. */
  108. Component* associatedComponent = nullptr;
  109. //==============================================================================
  110. /** Determines the order used to lay out items in their grid container. */
  111. int order = 0;
  112. /** This is the justify-self property of the item.
  113. This determines the alignment of the item along the row.
  114. */
  115. JustifySelf justifySelf = JustifySelf::autoValue;
  116. /** This is the align-self property of the item.
  117. This determines the alignment of the item along the column.
  118. */
  119. AlignSelf alignSelf = AlignSelf::autoValue;
  120. /** These are the start and end properties of the column. */
  121. StartAndEndProperty column = { Keyword::autoValue, Keyword::autoValue };
  122. /** These are the start and end properties of the row. */
  123. StartAndEndProperty row = { Keyword::autoValue, Keyword::autoValue };
  124. /** */
  125. String area;
  126. //==============================================================================
  127. enum
  128. {
  129. useDefaultValue = -2, /* TODO: useDefaultValue should be named useAuto */
  130. notAssigned = -1
  131. };
  132. /* TODO: move all of this into a common class that is shared with the FlexItem */
  133. float width = notAssigned;
  134. float minWidth = 0.0f;
  135. float maxWidth = notAssigned;
  136. float height = notAssigned;
  137. float minHeight = 0.0f;
  138. float maxHeight = notAssigned;
  139. /** Represents a margin. */
  140. struct Margin
  141. {
  142. Margin() noexcept;
  143. Margin (int size) noexcept;
  144. Margin (float size) noexcept;
  145. Margin (float top, float right, float bottom, float left) noexcept; /**< Creates a margin with these sizes. */
  146. float left;
  147. float right;
  148. float top;
  149. float bottom;
  150. };
  151. /** The margin to leave around this item. */
  152. Margin margin;
  153. /** The item's current bounds. */
  154. Rectangle<float> currentBounds;
  155. /** Short-hand */
  156. void setArea (Property rowStart, Property columnStart, Property rowEnd, Property columnEnd);
  157. /** Short-hand, span of 1 by default */
  158. void setArea (Property rowStart, Property columnStart);
  159. /** Short-hand */
  160. void setArea (const String& areaName);
  161. /** Short-hand */
  162. GridItem withArea (Property rowStart, Property columnStart, Property rowEnd, Property columnEnd) const noexcept;
  163. /** Short-hand, span of 1 by default */
  164. GridItem withArea (Property rowStart, Property columnStart) const noexcept;
  165. /** Short-hand */
  166. GridItem withArea (const String& areaName) const noexcept;
  167. /** Returns a copy of this object with a new row property. */
  168. GridItem withRow (StartAndEndProperty row) const noexcept;
  169. /** Returns a copy of this object with a new column property. */
  170. GridItem withColumn (StartAndEndProperty column) const noexcept;
  171. /** Returns a copy of this object with a new alignSelf property. */
  172. GridItem withAlignSelf (AlignSelf newAlignSelf) const noexcept;
  173. /** Returns a copy of this object with a new justifySelf property. */
  174. GridItem withJustifySelf (JustifySelf newJustifySelf) const noexcept;
  175. /** Returns a copy of this object with a new width. */
  176. GridItem withWidth (float newWidth) const noexcept;
  177. /** Returns a copy of this object with a new height. */
  178. GridItem withHeight (float newHeight) const noexcept;
  179. /** Returns a copy of this object with a new size. */
  180. GridItem withSize (float newWidth, float newHeight) const noexcept;
  181. /** Returns a copy of this object with a new margin. */
  182. GridItem withMargin (Margin newMargin) const noexcept;
  183. /** Returns a copy of this object with a new order. */
  184. GridItem withOrder (int newOrder) const noexcept;
  185. };
  186. } // namespace juce