juce_StretchableLayoutManager.h 12 KB

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