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.

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