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 14KB

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