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.

454 lines
14KB

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