The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

504 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. struct ConcertinaPanel::PanelSizes
  21. {
  22. struct Panel
  23. {
  24. Panel() = default;
  25. Panel (int sz, int mn, int mx) noexcept
  26. : size (sz), minSize (mn), maxSize (mx) {}
  27. int setSize (int newSize) noexcept
  28. {
  29. jassert (minSize <= maxSize);
  30. auto oldSize = size;
  31. size = jlimit (minSize, maxSize, newSize);
  32. return size - oldSize;
  33. }
  34. int expand (int amount) noexcept
  35. {
  36. amount = jmin (amount, maxSize - size);
  37. size += amount;
  38. return amount;
  39. }
  40. int reduce (int amount) noexcept
  41. {
  42. amount = jmin (amount, size - minSize);
  43. size -= amount;
  44. return amount;
  45. }
  46. bool canExpand() const noexcept { return size < maxSize; }
  47. bool isMinimised() const noexcept { return size <= minSize; }
  48. int size, minSize, maxSize;
  49. };
  50. Array<Panel> sizes;
  51. Panel& get (int index) noexcept { return sizes.getReference (index); }
  52. const Panel& get (int index) const noexcept { return sizes.getReference (index); }
  53. PanelSizes withMovedPanel (int index, int targetPosition, int totalSpace) const
  54. {
  55. auto 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. auto newSizes (*this);
  66. auto 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 (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. auto num = sizes.size();
  81. auto 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 (int start, 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 (int start, 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 (int start, 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 (int start, 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 (int start, 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 (int start, int end, int amountToAdd, ExpandMode expandMode) noexcept
  131. {
  132. if (end > start)
  133. {
  134. if (amountToAdd > 0)
  135. {
  136. if (expandMode == stretchAll) growRangeAll (start, end, amountToAdd);
  137. else if (expandMode == stretchFirst) growRangeFirst (start, end, amountToAdd);
  138. else if (expandMode == stretchLast) growRangeLast (start, end, amountToAdd);
  139. }
  140. else
  141. {
  142. if (expandMode == stretchFirst) shrinkRangeFirst (start, end, -amountToAdd);
  143. else shrinkRangeLast (start, end, -amountToAdd);
  144. }
  145. }
  146. }
  147. int getTotalSize (int start, int end) const noexcept
  148. {
  149. int tot = 0;
  150. while (start < end) tot += get (start++).size;
  151. return tot;
  152. }
  153. int getMinimumSize (int start, int end) const noexcept
  154. {
  155. int tot = 0;
  156. while (start < end) tot += get (start++).minSize;
  157. return tot;
  158. }
  159. int getMaximumSize (int start, int end) const noexcept
  160. {
  161. int tot = 0;
  162. while (start < end)
  163. {
  164. auto mx = get (start++).maxSize;
  165. if (mx > 0x100000)
  166. return mx;
  167. tot += mx;
  168. }
  169. return tot;
  170. }
  171. };
  172. //==============================================================================
  173. class ConcertinaPanel::PanelHolder final : public Component
  174. {
  175. public:
  176. PanelHolder (Component* comp, bool takeOwnership)
  177. : component (comp, takeOwnership)
  178. {
  179. setRepaintsOnMouseActivity (true);
  180. setWantsKeyboardFocus (false);
  181. addAndMakeVisible (comp);
  182. }
  183. void paint (Graphics& g) override
  184. {
  185. if (customHeader.get() != nullptr)
  186. return;
  187. const Rectangle<int> area (getWidth(), getHeaderSize());
  188. g.reduceClipRegion (area);
  189. getLookAndFeel().drawConcertinaPanelHeader (g, area, isMouseOver(), isMouseButtonDown(),
  190. getPanel(), *component);
  191. }
  192. void resized() override
  193. {
  194. auto bounds = getLocalBounds();
  195. auto headerBounds = bounds.removeFromTop (getHeaderSize());
  196. if (customHeader.get() != nullptr)
  197. customHeader.get()->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. if (e.mouseWasDraggedSinceMouseDown())
  208. {
  209. auto& panel = getPanel();
  210. panel.setLayout (dragStartSizes.withMovedPanel (panel.holders.indexOf (this),
  211. mouseDownY + e.getDistanceFromDragStartY(),
  212. panel.getHeight()), false);
  213. }
  214. }
  215. void mouseDoubleClick (const MouseEvent&) override
  216. {
  217. getPanel().panelHeaderDoubleClicked (component);
  218. }
  219. void setCustomHeaderComponent (Component* headerComponent, bool shouldTakeOwnership)
  220. {
  221. customHeader = CustomHeader (this, OptionalScopedPointer (headerComponent, shouldTakeOwnership));
  222. addAndMakeVisible (headerComponent);
  223. }
  224. OptionalScopedPointer<Component> component;
  225. private:
  226. PanelSizes dragStartSizes;
  227. int mouseDownY;
  228. struct CustomHeader
  229. {
  230. CustomHeader() = default;
  231. CustomHeader (MouseListener* l, OptionalScopedPointer<Component> c)
  232. : listener (l),
  233. customHeaderComponent (std::move (c))
  234. {
  235. if (customHeaderComponent != nullptr)
  236. customHeaderComponent->addMouseListener (listener, false);
  237. }
  238. CustomHeader (CustomHeader&& other) noexcept
  239. : listener (std::exchange (other.listener, nullptr)),
  240. customHeaderComponent (std::exchange (other.customHeaderComponent, {})) {}
  241. CustomHeader& operator= (CustomHeader&& other) noexcept
  242. {
  243. std::swap (other.listener, listener);
  244. std::swap (other.customHeaderComponent, customHeaderComponent);
  245. return *this;
  246. }
  247. CustomHeader (const CustomHeader& other) = delete;
  248. CustomHeader& operator= (const CustomHeader& other) = delete;
  249. ~CustomHeader() noexcept
  250. {
  251. if (customHeaderComponent != nullptr)
  252. customHeaderComponent->removeMouseListener (listener);
  253. }
  254. Component* get() const { return customHeaderComponent.get(); }
  255. private:
  256. MouseListener* listener = nullptr;
  257. OptionalScopedPointer<Component> customHeaderComponent;
  258. };
  259. CustomHeader customHeader;
  260. int getHeaderSize() const noexcept
  261. {
  262. ConcertinaPanel& panel = getPanel();
  263. auto ourIndex = panel.holders.indexOf (this);
  264. return panel.currentSizes->get (ourIndex).minSize;
  265. }
  266. ConcertinaPanel& getPanel() const
  267. {
  268. auto panel = dynamic_cast<ConcertinaPanel*> (getParentComponent());
  269. jassert (panel != nullptr);
  270. return *panel;
  271. }
  272. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PanelHolder)
  273. };
  274. //==============================================================================
  275. ConcertinaPanel::ConcertinaPanel()
  276. : currentSizes (new PanelSizes()),
  277. headerHeight (20)
  278. {
  279. }
  280. ConcertinaPanel::~ConcertinaPanel() = default;
  281. int ConcertinaPanel::getNumPanels() const noexcept
  282. {
  283. return holders.size();
  284. }
  285. Component* ConcertinaPanel::getPanel (int index) const noexcept
  286. {
  287. if (PanelHolder* h = holders[index])
  288. return h->component;
  289. return nullptr;
  290. }
  291. void ConcertinaPanel::addPanel (int insertIndex, Component* component, bool takeOwnership)
  292. {
  293. jassert (component != nullptr); // can't use a null pointer here!
  294. jassert (indexOfComp (component) < 0); // You can't add the same component more than once!
  295. auto holder = new PanelHolder (component, takeOwnership);
  296. holders.insert (insertIndex, holder);
  297. currentSizes->sizes.insert (insertIndex, PanelSizes::Panel (headerHeight, headerHeight, std::numeric_limits<int>::max()));
  298. addAndMakeVisible (holder);
  299. resized();
  300. }
  301. void ConcertinaPanel::removePanel (Component* component)
  302. {
  303. auto index = indexOfComp (component);
  304. if (index >= 0)
  305. {
  306. currentSizes->sizes.remove (index);
  307. holders.remove (index);
  308. resized();
  309. }
  310. }
  311. bool ConcertinaPanel::setPanelSize (Component* panelComponent, int height, bool animate)
  312. {
  313. auto index = indexOfComp (panelComponent);
  314. jassert (index >= 0); // The specified component doesn't seem to have been added!
  315. height += currentSizes->get (index).minSize;
  316. auto oldSize = currentSizes->get (index).size;
  317. setLayout (currentSizes->withResizedPanel (index, height, getHeight()), animate);
  318. return oldSize != currentSizes->get (index).size;
  319. }
  320. bool ConcertinaPanel::expandPanelFully (Component* component, bool animate)
  321. {
  322. return setPanelSize (component, getHeight(), animate);
  323. }
  324. void ConcertinaPanel::setMaximumPanelSize (Component* component, int maximumSize)
  325. {
  326. auto index = indexOfComp (component);
  327. jassert (index >= 0); // The specified component doesn't seem to have been added!
  328. if (index >= 0)
  329. {
  330. currentSizes->get (index).maxSize = currentSizes->get (index).minSize + maximumSize;
  331. resized();
  332. }
  333. }
  334. void ConcertinaPanel::setPanelHeaderSize (Component* component, int headerSize)
  335. {
  336. auto index = indexOfComp (component);
  337. jassert (index >= 0); // The specified component doesn't seem to have been added!
  338. if (index >= 0)
  339. {
  340. auto oldMin = currentSizes->get (index).minSize;
  341. currentSizes->get (index).minSize = headerSize;
  342. currentSizes->get (index).size += headerSize - oldMin;
  343. resized();
  344. }
  345. }
  346. void ConcertinaPanel::setCustomPanelHeader (Component* component, Component* customComponent, bool takeOwnership)
  347. {
  348. OptionalScopedPointer<Component> optional (customComponent, takeOwnership);
  349. auto index = indexOfComp (component);
  350. jassert (index >= 0); // The specified component doesn't seem to have been added!
  351. if (index >= 0)
  352. holders.getUnchecked (index)->setCustomHeaderComponent (optional.release(), takeOwnership);
  353. }
  354. void ConcertinaPanel::resized()
  355. {
  356. applyLayout (getFittedSizes(), false);
  357. }
  358. int ConcertinaPanel::indexOfComp (Component* comp) const noexcept
  359. {
  360. for (int i = 0; i < holders.size(); ++i)
  361. if (holders.getUnchecked (i)->component == comp)
  362. return i;
  363. return -1;
  364. }
  365. ConcertinaPanel::PanelSizes ConcertinaPanel::getFittedSizes() const
  366. {
  367. return currentSizes->fittedInto (getHeight());
  368. }
  369. void ConcertinaPanel::applyLayout (const PanelSizes& sizes, bool animate)
  370. {
  371. if (! animate)
  372. animator.cancelAllAnimations (false);
  373. const int animationDuration = 150;
  374. auto w = getWidth();
  375. int y = 0;
  376. for (int i = 0; i < holders.size(); ++i)
  377. {
  378. PanelHolder& p = *holders.getUnchecked (i);
  379. auto h = sizes.get (i).size;
  380. const Rectangle<int> pos (0, y, w, h);
  381. if (animate)
  382. animator.animateComponent (&p, pos, 1.0f, animationDuration, false, 1.0, 1.0);
  383. else
  384. p.setBounds (pos);
  385. y += h;
  386. }
  387. }
  388. void ConcertinaPanel::setLayout (const PanelSizes& sizes, bool animate)
  389. {
  390. *currentSizes = sizes;
  391. applyLayout (getFittedSizes(), animate);
  392. }
  393. void ConcertinaPanel::panelHeaderDoubleClicked (Component* component)
  394. {
  395. if (! expandPanelFully (component, true))
  396. setPanelSize (component, 0, true);
  397. }
  398. //==============================================================================
  399. std::unique_ptr<AccessibilityHandler> ConcertinaPanel::createAccessibilityHandler()
  400. {
  401. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::group);
  402. }
  403. } // namespace juce