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_ConcertinaPanel.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. struct ConcertinaPanel::PanelSizes
  18. {
  19. struct Panel
  20. {
  21. Panel() noexcept {}
  22. Panel (const int sz, const int mn, const int mx) noexcept
  23. : size (sz), minSize (mn), maxSize (mx) {}
  24. int setSize (const int newSize) noexcept
  25. {
  26. jassert (minSize <= maxSize);
  27. const int oldSize = size;
  28. size = jlimit (minSize, maxSize, newSize);
  29. return size - oldSize;
  30. }
  31. int expand (int amount) noexcept
  32. {
  33. amount = jmin (amount, maxSize - size);
  34. size += amount;
  35. return amount;
  36. }
  37. int reduce (int amount) noexcept
  38. {
  39. amount = jmin (amount, size - minSize);
  40. size -= amount;
  41. return amount;
  42. }
  43. bool canExpand() const noexcept { return size < maxSize; }
  44. bool isMinimised() const noexcept { return size <= minSize; }
  45. int size, minSize, maxSize;
  46. };
  47. Array<Panel> sizes;
  48. Panel& get (const int index) const noexcept { return sizes.getReference(index); }
  49. PanelSizes withMovedPanel (const int index, int targetPosition, int totalSpace) const
  50. {
  51. const int num = sizes.size();
  52. totalSpace = jmax (totalSpace, getMinimumSize (0, num));
  53. targetPosition = jmax (targetPosition, totalSpace - getMaximumSize (index, num));
  54. PanelSizes newSizes (*this);
  55. newSizes.stretchRange (0, index, targetPosition - newSizes.getTotalSize (0, index), stretchLast);
  56. newSizes.stretchRange (index, num, totalSpace - newSizes.getTotalSize (0, index) - newSizes.getTotalSize (index, num), stretchFirst);
  57. return newSizes;
  58. }
  59. PanelSizes fittedInto (int totalSpace) const
  60. {
  61. PanelSizes newSizes (*this);
  62. const int num = newSizes.sizes.size();
  63. totalSpace = jmax (totalSpace, getMinimumSize (0, num));
  64. newSizes.stretchRange (0, num, totalSpace - newSizes.getTotalSize (0, num), stretchAll);
  65. return newSizes;
  66. }
  67. PanelSizes withResizedPanel (const int index, int panelHeight, int totalSpace) const
  68. {
  69. PanelSizes newSizes (*this);
  70. if (totalSpace <= 0)
  71. {
  72. newSizes.get(index).size = panelHeight;
  73. }
  74. else
  75. {
  76. const int num = sizes.size();
  77. const int minSize = getMinimumSize (0, num);
  78. totalSpace = jmax (totalSpace, minSize);
  79. newSizes.get(index).setSize (panelHeight);
  80. newSizes.stretchRange (0, index, totalSpace - newSizes.getTotalSize (0, num), stretchLast);
  81. newSizes.stretchRange (index, num, totalSpace - newSizes.getTotalSize (0, num), stretchLast);
  82. newSizes = newSizes.fittedInto (totalSpace);
  83. }
  84. return newSizes;
  85. }
  86. private:
  87. enum ExpandMode
  88. {
  89. stretchAll,
  90. stretchFirst,
  91. stretchLast
  92. };
  93. void growRangeFirst (const int start, const int end, int spaceDiff) noexcept
  94. {
  95. for (int attempts = 4; --attempts >= 0 && spaceDiff > 0;)
  96. for (int i = start; i < end && spaceDiff > 0; ++i)
  97. spaceDiff -= get (i).expand (spaceDiff);
  98. }
  99. void growRangeLast (const int start, const int end, int spaceDiff) noexcept
  100. {
  101. for (int attempts = 4; --attempts >= 0 && spaceDiff > 0;)
  102. for (int i = end; --i >= start && spaceDiff > 0;)
  103. spaceDiff -= get (i).expand (spaceDiff);
  104. }
  105. void growRangeAll (const int start, const int end, int spaceDiff) noexcept
  106. {
  107. Array<Panel*> expandableItems;
  108. for (int i = start; i < end; ++i)
  109. if (get(i).canExpand() && ! get(i).isMinimised())
  110. expandableItems.add (& get(i));
  111. for (int attempts = 4; --attempts >= 0 && spaceDiff > 0;)
  112. for (int i = expandableItems.size(); --i >= 0 && spaceDiff > 0;)
  113. spaceDiff -= expandableItems.getUnchecked(i)->expand (spaceDiff / (i + 1));
  114. growRangeLast (start, end, spaceDiff);
  115. }
  116. void shrinkRangeFirst (const int start, const int end, int spaceDiff) noexcept
  117. {
  118. for (int i = start; i < end && spaceDiff > 0; ++i)
  119. spaceDiff -= get(i).reduce (spaceDiff);
  120. }
  121. void shrinkRangeLast (const int start, const int end, int spaceDiff) noexcept
  122. {
  123. for (int i = end; --i >= start && spaceDiff > 0;)
  124. spaceDiff -= get(i).reduce (spaceDiff);
  125. }
  126. void stretchRange (const int start, const int end, const int amountToAdd,
  127. const ExpandMode expandMode) noexcept
  128. {
  129. if (end > start)
  130. {
  131. if (amountToAdd > 0)
  132. {
  133. if (expandMode == stretchAll) growRangeAll (start, end, amountToAdd);
  134. else if (expandMode == stretchFirst) growRangeFirst (start, end, amountToAdd);
  135. else if (expandMode == stretchLast) growRangeLast (start, end, amountToAdd);
  136. }
  137. else
  138. {
  139. if (expandMode == stretchFirst) shrinkRangeFirst (start, end, -amountToAdd);
  140. else shrinkRangeLast (start, end, -amountToAdd);
  141. }
  142. }
  143. }
  144. int getTotalSize (int start, const int end) const noexcept
  145. {
  146. int tot = 0;
  147. while (start < end) tot += get(start++).size;
  148. return tot;
  149. }
  150. int getMinimumSize (int start, const int end) const noexcept
  151. {
  152. int tot = 0;
  153. while (start < end) tot += get(start++).minSize;
  154. return tot;
  155. }
  156. int getMaximumSize (int start, const int end) const noexcept
  157. {
  158. int tot = 0;
  159. while (start < end)
  160. {
  161. const int mx = get(start++).maxSize;
  162. if (mx > 0x100000)
  163. return mx;
  164. tot += mx;
  165. }
  166. return tot;
  167. }
  168. };
  169. //==============================================================================
  170. class ConcertinaPanel::PanelHolder : public Component
  171. {
  172. public:
  173. PanelHolder (Component* const comp, bool takeOwnership)
  174. : component (comp, takeOwnership)
  175. {
  176. setRepaintsOnMouseActivity (true);
  177. setWantsKeyboardFocus (false);
  178. addAndMakeVisible (comp);
  179. }
  180. void paint (Graphics& g) override
  181. {
  182. const Rectangle<int> area (getWidth(), getHeaderSize());
  183. g.reduceClipRegion (area);
  184. getLookAndFeel().drawConcertinaPanelHeader (g, area, isMouseOver(), isMouseButtonDown(),
  185. getPanel(), *component);
  186. }
  187. void resized() override
  188. {
  189. component->setBounds (getLocalBounds().withTop (getHeaderSize()));
  190. }
  191. void mouseDown (const MouseEvent&) override
  192. {
  193. mouseDownY = getY();
  194. dragStartSizes = getPanel().getFittedSizes();
  195. }
  196. void mouseDrag (const MouseEvent& e) override
  197. {
  198. ConcertinaPanel& panel = getPanel();
  199. panel.setLayout (dragStartSizes.withMovedPanel (panel.holders.indexOf (this),
  200. mouseDownY + e.getDistanceFromDragStartY(),
  201. panel.getHeight()), false);
  202. }
  203. void mouseDoubleClick (const MouseEvent&) override
  204. {
  205. getPanel().panelHeaderDoubleClicked (component);
  206. }
  207. OptionalScopedPointer<Component> component;
  208. private:
  209. PanelSizes dragStartSizes;
  210. int mouseDownY;
  211. int getHeaderSize() const noexcept
  212. {
  213. ConcertinaPanel& panel = getPanel();
  214. const int ourIndex = panel.holders.indexOf (this);
  215. return panel.currentSizes->get(ourIndex).minSize;
  216. }
  217. ConcertinaPanel& getPanel() const
  218. {
  219. ConcertinaPanel* const panel = dynamic_cast<ConcertinaPanel*> (getParentComponent());
  220. jassert (panel != nullptr);
  221. return *panel;
  222. }
  223. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PanelHolder)
  224. };
  225. //==============================================================================
  226. ConcertinaPanel::ConcertinaPanel()
  227. : currentSizes (new PanelSizes()),
  228. headerHeight (20)
  229. {
  230. }
  231. ConcertinaPanel::~ConcertinaPanel() {}
  232. void ConcertinaPanel::addPanel (int insertIndex, Component* component, bool takeOwnership)
  233. {
  234. jassert (component != nullptr); // can't use a null pointer here!
  235. jassert (indexOfComp (component) < 0); // You can't add the same component more than once!
  236. PanelHolder* const holder = new PanelHolder (component, takeOwnership);
  237. holders.insert (insertIndex, holder);
  238. currentSizes->sizes.insert (insertIndex, PanelSizes::Panel (headerHeight, headerHeight, std::numeric_limits<int>::max()));
  239. addAndMakeVisible (holder);
  240. resized();
  241. }
  242. void ConcertinaPanel::removePanel (Component* component)
  243. {
  244. const int index = indexOfComp (component);
  245. if (index >= 0)
  246. {
  247. currentSizes->sizes.remove (index);
  248. holders.remove (index);
  249. resized();
  250. }
  251. }
  252. bool ConcertinaPanel::setPanelSize (Component* panelComponent, int height, const bool animate)
  253. {
  254. const int index = indexOfComp (panelComponent);
  255. jassert (index >= 0); // The specified component doesn't seem to have been added!
  256. height += currentSizes->get(index).minSize;
  257. const int oldSize = currentSizes->get(index).size;
  258. setLayout (currentSizes->withResizedPanel (index, height, getHeight()), animate);
  259. return oldSize != currentSizes->get(index).size;
  260. }
  261. bool ConcertinaPanel::expandPanelFully (Component* component, const bool animate)
  262. {
  263. return setPanelSize (component, getHeight(), animate);
  264. }
  265. void ConcertinaPanel::setMaximumPanelSize (Component* component, int maximumSize)
  266. {
  267. const int index = indexOfComp (component);
  268. jassert (index >= 0); // The specified component doesn't seem to have been added!
  269. if (index >= 0)
  270. {
  271. currentSizes->get(index).maxSize = currentSizes->get(index).minSize + maximumSize;
  272. resized();
  273. }
  274. }
  275. void ConcertinaPanel::setPanelHeaderSize (Component* component, int headerSize)
  276. {
  277. const int index = indexOfComp (component);
  278. jassert (index >= 0); // The specified component doesn't seem to have been added!
  279. if (index >= 0)
  280. {
  281. currentSizes->get(index).minSize = headerSize;
  282. resized();
  283. }
  284. }
  285. void ConcertinaPanel::resized()
  286. {
  287. applyLayout (getFittedSizes(), false);
  288. }
  289. int ConcertinaPanel::indexOfComp (Component* comp) const noexcept
  290. {
  291. for (int i = 0; i < holders.size(); ++i)
  292. if (holders.getUnchecked(i)->component == comp)
  293. return i;
  294. return -1;
  295. }
  296. ConcertinaPanel::PanelSizes ConcertinaPanel::getFittedSizes() const
  297. {
  298. return currentSizes->fittedInto (getHeight());
  299. }
  300. void ConcertinaPanel::applyLayout (const PanelSizes& sizes, const bool animate)
  301. {
  302. if (! animate)
  303. animator.cancelAllAnimations (false);
  304. const int animationDuration = 150;
  305. const int w = getWidth();
  306. int y = 0;
  307. for (int i = 0; i < holders.size(); ++i)
  308. {
  309. PanelHolder& p = *holders.getUnchecked(i);
  310. const int h = sizes.get(i).size;
  311. const Rectangle<int> pos (0, y, w, h);
  312. if (animate)
  313. animator.animateComponent (&p, pos, 1.0f, animationDuration, false, 1.0, 1.0);
  314. else
  315. p.setBounds (pos);
  316. y += h;
  317. }
  318. }
  319. void ConcertinaPanel::setLayout (const PanelSizes& sizes, const bool animate)
  320. {
  321. *currentSizes = sizes;
  322. applyLayout (getFittedSizes(), animate);
  323. }
  324. void ConcertinaPanel::panelHeaderDoubleClicked (Component* component)
  325. {
  326. if (! expandPanelFully (component, true))
  327. setPanelSize (component, 0, true);
  328. }