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.

459 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. 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