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.

juce_StretchableLayoutManager.h 13KB

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