Audio plugin host https://kx.studio/carla
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.

255 lines
12KB

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