juce_FlexBox.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. Represents a FlexBox container, which contains and manages the layout of a set
  23. of FlexItem objects.
  24. To use this class, set its parameters appropriately (you can search online for
  25. more help on exactly how the FlexBox protocol works!), then add your sub-items
  26. to the items array, and call performLayout() in the resized() function of your
  27. Component.
  28. @see FlexItem
  29. @tags{GUI}
  30. */
  31. class JUCE_API FlexBox final
  32. {
  33. public:
  34. /** Possible values for the flexDirection property. */
  35. enum class Direction
  36. {
  37. row, /**< Set the main axis direction from left to right. */
  38. rowReverse, /**< Set the main axis direction from right to left. */
  39. column, /**< Set the main axis direction from top to bottom. */
  40. columnReverse /**< Set the main axis direction from bottom to top. */
  41. };
  42. /** Possible values for the flexWrap property. */
  43. enum class Wrap
  44. {
  45. noWrap, /**< Items are forced into a single line. */
  46. wrap, /**< Items are wrapped onto multiple lines from top to bottom. */
  47. wrapReverse /**< Items are wrapped onto multiple lines from bottom to top. */
  48. };
  49. /** Possible values for the alignContent property. */
  50. enum class AlignContent
  51. {
  52. stretch, /**< Lines of items are stretched from start to end of the cross axis. */
  53. flexStart, /**< Lines of items are aligned towards the start of the cross axis. */
  54. flexEnd, /**< Lines of items are aligned towards the end of the cross axis. */
  55. center, /**< Lines of items are aligned towards the center of the cross axis. */
  56. spaceBetween, /**< Lines of items are evenly spaced along the cross axis with spaces between them. */
  57. spaceAround /**< Lines of items are evenly spaced along the cross axis with spaces around them. */
  58. };
  59. /** Possible values for the alignItems property. */
  60. enum class AlignItems
  61. {
  62. stretch, /**< Items are stretched from start to end of the cross axis. */
  63. flexStart, /**< Items are aligned towards the start of the cross axis. */
  64. flexEnd, /**< Items are aligned towards the end of the cross axis. */
  65. center /**< Items are aligned towards the center of the cross axis. */
  66. };
  67. /** Possible values for the justifyContent property. */
  68. enum class JustifyContent
  69. {
  70. flexStart, /**< Items are justified towards the start of the main axis. */
  71. flexEnd, /**< Items are justified towards the end of the main axis. */
  72. center, /**< Items are justified towards the center of the main axis. */
  73. spaceBetween, /**< Items are evenly spaced along the main axis with spaces between them. */
  74. spaceAround /**< Items are evenly spaced along the main axis with spaces around them. */
  75. };
  76. //==============================================================================
  77. /** Creates an empty FlexBox container with default parameters. */
  78. FlexBox() noexcept;
  79. /** Creates an empty FlexBox container with these parameters. */
  80. FlexBox (Direction, Wrap, AlignContent, AlignItems, JustifyContent) noexcept;
  81. /** Creates an empty FlexBox container with the given content-justification mode. */
  82. FlexBox (JustifyContent) noexcept;
  83. /** Destructor. */
  84. ~FlexBox() noexcept;
  85. //==============================================================================
  86. /** Lays-out the box's items within the given rectangle. */
  87. void performLayout (Rectangle<float> targetArea);
  88. /** Lays-out the box's items within the given rectangle. */
  89. void performLayout (Rectangle<int> targetArea);
  90. //==============================================================================
  91. /** Specifies how flex items are placed in the flex container, and defines the
  92. direction of the main axis.
  93. */
  94. Direction flexDirection = Direction::row;
  95. /** Specifies whether items are forced into a single line or can be wrapped onto multiple lines.
  96. If wrapping is allowed, this property also controls the direction in which lines are stacked.
  97. */
  98. Wrap flexWrap = Wrap::noWrap;
  99. /** Specifies how a flex container's lines are placed within the flex container when
  100. there is extra space on the cross-axis.
  101. This property has no effect on single line layouts.
  102. */
  103. AlignContent alignContent = AlignContent::stretch;
  104. /** Specifies the alignment of flex items along the cross-axis of each line. */
  105. AlignItems alignItems = AlignItems::stretch;
  106. /** Defines how the container distributes space between and around items along the main-axis.
  107. The alignment is done after the lengths and auto margins are applied, so that if there is at
  108. least one flexible element, with flex-grow different from 0, it will have no effect as there
  109. won't be any available space.
  110. */
  111. JustifyContent justifyContent = JustifyContent::flexStart;
  112. /** The set of items to lay-out. */
  113. Array<FlexItem> items;
  114. private:
  115. JUCE_LEAK_DETECTOR (FlexBox)
  116. };
  117. } // namespace juce