The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. For laying out a set of components, where the components have preferred sizes
  21. and size limits, but where they are allowed to stretch to fill the available
  22. space.
  23. For example, if you have a component containing several other components, and
  24. each one should be given a share of the total size, you could use one of these
  25. to resize the child components when the parent component is resized. Then
  26. you could add a StretchableLayoutResizerBar to easily let the user rescale them.
  27. A StretchableLayoutManager operates only in one dimension, so if you have a set
  28. of components stacked vertically on top of each other, you'd use one to manage their
  29. heights. To build up complex arrangements of components, e.g. for applications
  30. with multiple nested panels, you would use more than one StretchableLayoutManager.
  31. E.g. by using two (one vertical, one horizontal), you could create a resizable
  32. spreadsheet-style table.
  33. E.g.
  34. @code
  35. class MyComp : public Component
  36. {
  37. StretchableLayoutManager myLayout;
  38. MyComp()
  39. {
  40. myLayout.setItemLayout (0, // for item 0
  41. 50, 100, // must be between 50 and 100 pixels in size
  42. -0.6); // and its preferred size is 60% of the total available space
  43. myLayout.setItemLayout (1, // for item 1
  44. -0.2, -0.6, // size must be between 20% and 60% of the available space
  45. 50); // and its preferred size is 50 pixels
  46. }
  47. void resized()
  48. {
  49. // make a list of two of our child components that we want to reposition
  50. Component* comps[] = { myComp1, myComp2 };
  51. // this will position the 2 components, one above the other, to fit
  52. // vertically into the rectangle provided.
  53. myLayout.layOutComponents (comps, 2,
  54. 0, 0, getWidth(), getHeight(),
  55. true);
  56. }
  57. };
  58. @endcode
  59. @see StretchableLayoutResizerBar
  60. */
  61. class JUCE_API StretchableLayoutManager
  62. {
  63. public:
  64. //==============================================================================
  65. /** Creates an empty layout.
  66. You'll need to add some item properties to the layout before it can be used
  67. to resize things - see setItemLayout().
  68. */
  69. StretchableLayoutManager();
  70. /** Destructor. */
  71. ~StretchableLayoutManager();
  72. //==============================================================================
  73. /** For a numbered item, this sets its size limits and preferred size.
  74. @param itemIndex the index of the item to change.
  75. @param minimumSize the minimum size that this item is allowed to be - a positive number
  76. indicates an absolute size in pixels. A negative number indicates a
  77. proportion of the available space (e.g -0.5 is 50%)
  78. @param maximumSize the maximum size that this item is allowed to be - a positive number
  79. indicates an absolute size in pixels. A negative number indicates a
  80. proportion of the available space
  81. @param preferredSize the size that this item would like to be, if there's enough room. A
  82. positive number indicates an absolute size in pixels. A negative number
  83. indicates a proportion of the available space
  84. @see getItemLayout
  85. */
  86. void setItemLayout (int itemIndex,
  87. double minimumSize,
  88. double maximumSize,
  89. double preferredSize);
  90. /** For a numbered item, this returns its size limits and preferred size.
  91. @param itemIndex the index of the item.
  92. @param minimumSize the minimum size that this item is allowed to be - a positive number
  93. indicates an absolute size in pixels. A negative number indicates a
  94. proportion of the available space (e.g -0.5 is 50%)
  95. @param maximumSize the maximum size that this item is allowed to be - a positive number
  96. indicates an absolute size in pixels. A negative number indicates a
  97. proportion of the available space
  98. @param preferredSize the size that this item would like to be, if there's enough room. A
  99. positive number indicates an absolute size in pixels. A negative number
  100. indicates a proportion of the available space
  101. @returns false if the item's properties hadn't been set
  102. @see setItemLayout
  103. */
  104. bool getItemLayout (int itemIndex,
  105. double& minimumSize,
  106. double& maximumSize,
  107. double& preferredSize) const;
  108. /** Clears all the properties that have been set with setItemLayout() and resets
  109. this object to its initial state.
  110. */
  111. void clearAllItems();
  112. //==============================================================================
  113. /** Takes a set of components that correspond to the layout's items, and positions
  114. them to fill a space.
  115. This will try to give each item its preferred size, whether that's a relative size
  116. or an absolute one.
  117. @param components an array of components that correspond to each of the
  118. numbered items that the StretchableLayoutManager object
  119. has been told about with setItemLayout()
  120. @param numComponents the number of components in the array that is passed-in. This
  121. should be the same as the number of items this object has been
  122. told about.
  123. @param x the left of the rectangle in which the components should
  124. be laid out
  125. @param y the top of the rectangle in which the components should
  126. be laid out
  127. @param width the width of the rectangle in which the components should
  128. be laid out
  129. @param height the height of the rectangle in which the components should
  130. be laid out
  131. @param vertically if true, the components will be positioned in a vertical stack,
  132. so that they fill the height of the rectangle. If false, they
  133. will be placed side-by-side in a horizontal line, filling the
  134. available width
  135. @param resizeOtherDimension if true, this means that the components will have their
  136. other dimension resized to fit the space - i.e. if the 'vertically'
  137. parameter is true, their x-positions and widths are adjusted to fit
  138. the x and width parameters; if 'vertically' is false, their y-positions
  139. and heights are adjusted to fit the y and height parameters.
  140. */
  141. void layOutComponents (Component** components,
  142. int numComponents,
  143. int x, int y, int width, int height,
  144. bool vertically,
  145. bool resizeOtherDimension);
  146. //==============================================================================
  147. /** Returns the current position of one of the items.
  148. This is only a valid call after layOutComponents() has been called, as it
  149. returns the last position that this item was placed at. If the layout was
  150. vertical, the value returned will be the y position of the top of the item,
  151. relative to the top of the rectangle in which the items were placed (so for
  152. example, item 0 will always have position of 0, even in the rectangle passed
  153. in to layOutComponents() wasn't at y = 0). If the layout was done horizontally,
  154. the position returned is the item's left-hand position, again relative to the
  155. x position of the rectangle used.
  156. @see getItemCurrentSize, setItemPosition
  157. */
  158. int getItemCurrentPosition (int itemIndex) const;
  159. /** Returns the current size of one of the items.
  160. This is only meaningful after layOutComponents() has been called, as it
  161. returns the last size that this item was given. If the layout was done
  162. vertically, it'll return the item's height in pixels; if it was horizontal,
  163. it'll return its width.
  164. @see getItemCurrentRelativeSize
  165. */
  166. int getItemCurrentAbsoluteSize (int itemIndex) const;
  167. /** Returns the current size of one of the items.
  168. This is only meaningful after layOutComponents() has been called, as it
  169. returns the last size that this item was given. If the layout was done
  170. vertically, it'll return a negative value representing the item's height relative
  171. to the last size used for laying the components out; if the layout was done
  172. horizontally it'll be the proportion of its width.
  173. @see getItemCurrentAbsoluteSize
  174. */
  175. double getItemCurrentRelativeSize (int itemIndex) const;
  176. //==============================================================================
  177. /** Moves one of the items, shifting along any other items as necessary in
  178. order to get it to the desired position.
  179. Calling this method will also update the preferred sizes of the items it
  180. shuffles along, so that they reflect their new positions.
  181. (This is the method that a StretchableLayoutResizerBar uses to shift the items
  182. about when it's dragged).
  183. @param itemIndex the item to move
  184. @param newPosition the absolute position that you'd like this item to move
  185. to. The item might not be able to always reach exactly this position,
  186. because other items may have minimum sizes that constrain how
  187. far it can go
  188. */
  189. void setItemPosition (int itemIndex,
  190. int newPosition);
  191. private:
  192. //==============================================================================
  193. struct ItemLayoutProperties
  194. {
  195. int itemIndex;
  196. int currentSize;
  197. double minSize, maxSize, preferredSize;
  198. };
  199. OwnedArray <ItemLayoutProperties> items;
  200. int totalSize;
  201. //==============================================================================
  202. static int sizeToRealSize (double size, int totalSpace);
  203. ItemLayoutProperties* getInfoFor (int itemIndex) const;
  204. void setTotalSize (int newTotalSize);
  205. int fitComponentsIntoSpace (int startIndex, int endIndex, int availableSpace, int startPos);
  206. int getMinimumSizeOfItems (int startIndex, int endIndex) const;
  207. int getMaximumSizeOfItems (int startIndex, int endIndex) const;
  208. void updatePrefSizesToMatchCurrentPositions();
  209. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StretchableLayoutManager)
  210. };