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.

262 lines
12KB

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