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_TabbedComponent.cpp 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. namespace TabbedComponentHelpers
  18. {
  19. const Identifier deleteComponentId ("deleteByTabComp_");
  20. static void deleteIfNecessary (Component* const comp)
  21. {
  22. if (comp != nullptr && (bool) comp->getProperties() [deleteComponentId])
  23. delete comp;
  24. }
  25. static Rectangle<int> getTabArea (Rectangle<int>& content, BorderSize<int>& outline,
  26. const TabbedButtonBar::Orientation orientation, const int tabDepth)
  27. {
  28. switch (orientation)
  29. {
  30. case TabbedButtonBar::TabsAtTop: outline.setTop (0); return content.removeFromTop (tabDepth);
  31. case TabbedButtonBar::TabsAtBottom: outline.setBottom (0); return content.removeFromBottom (tabDepth);
  32. case TabbedButtonBar::TabsAtLeft: outline.setLeft (0); return content.removeFromLeft (tabDepth);
  33. case TabbedButtonBar::TabsAtRight: outline.setRight (0); return content.removeFromRight (tabDepth);
  34. default: jassertfalse; break;
  35. }
  36. return Rectangle<int>();
  37. }
  38. }
  39. //==============================================================================
  40. class TabbedComponent::ButtonBar : public TabbedButtonBar
  41. {
  42. public:
  43. ButtonBar (TabbedComponent& owner_, const TabbedButtonBar::Orientation orientation_)
  44. : TabbedButtonBar (orientation_),
  45. owner (owner_)
  46. {
  47. }
  48. void currentTabChanged (int newCurrentTabIndex, const String& newTabName)
  49. {
  50. owner.changeCallback (newCurrentTabIndex, newTabName);
  51. }
  52. void popupMenuClickOnTab (int tabIndex, const String& tabName)
  53. {
  54. owner.popupMenuClickOnTab (tabIndex, tabName);
  55. }
  56. Colour getTabBackgroundColour (const int tabIndex)
  57. {
  58. return owner.tabs->getTabBackgroundColour (tabIndex);
  59. }
  60. TabBarButton* createTabButton (const String& tabName, int tabIndex)
  61. {
  62. return owner.createTabButton (tabName, tabIndex);
  63. }
  64. private:
  65. TabbedComponent& owner;
  66. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonBar)
  67. };
  68. //==============================================================================
  69. TabbedComponent::TabbedComponent (const TabbedButtonBar::Orientation orientation)
  70. : tabDepth (30),
  71. outlineThickness (1),
  72. edgeIndent (0)
  73. {
  74. addAndMakeVisible (tabs = new ButtonBar (*this, orientation));
  75. }
  76. TabbedComponent::~TabbedComponent()
  77. {
  78. clearTabs();
  79. tabs = nullptr;
  80. }
  81. //==============================================================================
  82. void TabbedComponent::setOrientation (const TabbedButtonBar::Orientation orientation)
  83. {
  84. tabs->setOrientation (orientation);
  85. resized();
  86. }
  87. TabbedButtonBar::Orientation TabbedComponent::getOrientation() const noexcept
  88. {
  89. return tabs->getOrientation();
  90. }
  91. void TabbedComponent::setTabBarDepth (const int newDepth)
  92. {
  93. if (tabDepth != newDepth)
  94. {
  95. tabDepth = newDepth;
  96. resized();
  97. }
  98. }
  99. TabBarButton* TabbedComponent::createTabButton (const String& tabName, const int /*tabIndex*/)
  100. {
  101. return new TabBarButton (tabName, *tabs);
  102. }
  103. //==============================================================================
  104. void TabbedComponent::clearTabs()
  105. {
  106. if (panelComponent != nullptr)
  107. {
  108. panelComponent->setVisible (false);
  109. removeChildComponent (panelComponent);
  110. panelComponent = nullptr;
  111. }
  112. tabs->clearTabs();
  113. for (int i = contentComponents.size(); --i >= 0;)
  114. TabbedComponentHelpers::deleteIfNecessary (contentComponents.getReference (i));
  115. contentComponents.clear();
  116. }
  117. void TabbedComponent::addTab (const String& tabName,
  118. Colour tabBackgroundColour,
  119. Component* const contentComponent,
  120. const bool deleteComponentWhenNotNeeded,
  121. const int insertIndex)
  122. {
  123. contentComponents.insert (insertIndex, WeakReference<Component> (contentComponent));
  124. if (deleteComponentWhenNotNeeded && contentComponent != nullptr)
  125. contentComponent->getProperties().set (TabbedComponentHelpers::deleteComponentId, true);
  126. tabs->addTab (tabName, tabBackgroundColour, insertIndex);
  127. resized();
  128. }
  129. void TabbedComponent::setTabName (const int tabIndex, const String& newName)
  130. {
  131. tabs->setTabName (tabIndex, newName);
  132. }
  133. void TabbedComponent::removeTab (const int tabIndex)
  134. {
  135. if (isPositiveAndBelow (tabIndex, contentComponents.size()))
  136. {
  137. TabbedComponentHelpers::deleteIfNecessary (contentComponents.getReference (tabIndex));
  138. contentComponents.remove (tabIndex);
  139. tabs->removeTab (tabIndex);
  140. }
  141. }
  142. int TabbedComponent::getNumTabs() const
  143. {
  144. return tabs->getNumTabs();
  145. }
  146. StringArray TabbedComponent::getTabNames() const
  147. {
  148. return tabs->getTabNames();
  149. }
  150. Component* TabbedComponent::getTabContentComponent (const int tabIndex) const noexcept
  151. {
  152. return contentComponents [tabIndex];
  153. }
  154. Colour TabbedComponent::getTabBackgroundColour (const int tabIndex) const noexcept
  155. {
  156. return tabs->getTabBackgroundColour (tabIndex);
  157. }
  158. void TabbedComponent::setTabBackgroundColour (const int tabIndex, Colour newColour)
  159. {
  160. tabs->setTabBackgroundColour (tabIndex, newColour);
  161. if (getCurrentTabIndex() == tabIndex)
  162. repaint();
  163. }
  164. void TabbedComponent::setCurrentTabIndex (const int newTabIndex, const bool sendChangeMessage)
  165. {
  166. tabs->setCurrentTabIndex (newTabIndex, sendChangeMessage);
  167. }
  168. int TabbedComponent::getCurrentTabIndex() const
  169. {
  170. return tabs->getCurrentTabIndex();
  171. }
  172. String TabbedComponent::getCurrentTabName() const
  173. {
  174. return tabs->getCurrentTabName();
  175. }
  176. void TabbedComponent::setOutline (const int thickness)
  177. {
  178. outlineThickness = thickness;
  179. resized();
  180. repaint();
  181. }
  182. void TabbedComponent::setIndent (const int indentThickness)
  183. {
  184. edgeIndent = indentThickness;
  185. resized();
  186. repaint();
  187. }
  188. void TabbedComponent::paint (Graphics& g)
  189. {
  190. g.fillAll (findColour (backgroundColourId));
  191. Rectangle<int> content (getLocalBounds());
  192. BorderSize<int> outline (outlineThickness);
  193. TabbedComponentHelpers::getTabArea (content, outline, getOrientation(), tabDepth);
  194. g.reduceClipRegion (content);
  195. g.fillAll (tabs->getTabBackgroundColour (getCurrentTabIndex()));
  196. if (outlineThickness > 0)
  197. {
  198. RectangleList<int> rl (content);
  199. rl.subtract (outline.subtractedFrom (content));
  200. g.reduceClipRegion (rl);
  201. g.fillAll (findColour (outlineColourId));
  202. }
  203. }
  204. void TabbedComponent::resized()
  205. {
  206. Rectangle<int> content (getLocalBounds());
  207. BorderSize<int> outline (outlineThickness);
  208. tabs->setBounds (TabbedComponentHelpers::getTabArea (content, outline, getOrientation(), tabDepth));
  209. content = BorderSize<int> (edgeIndent).subtractedFrom (outline.subtractedFrom (content));
  210. for (int i = contentComponents.size(); --i >= 0;)
  211. if (Component* c = contentComponents.getReference(i))
  212. c->setBounds (content);
  213. }
  214. void TabbedComponent::lookAndFeelChanged()
  215. {
  216. for (int i = contentComponents.size(); --i >= 0;)
  217. if (Component* c = contentComponents.getReference(i))
  218. c->lookAndFeelChanged();
  219. }
  220. void TabbedComponent::changeCallback (const int newCurrentTabIndex, const String& newTabName)
  221. {
  222. Component* const newPanelComp = getTabContentComponent (getCurrentTabIndex());
  223. if (newPanelComp != panelComponent)
  224. {
  225. if (panelComponent != nullptr)
  226. {
  227. panelComponent->setVisible (false);
  228. removeChildComponent (panelComponent);
  229. }
  230. panelComponent = newPanelComp;
  231. if (panelComponent != nullptr)
  232. {
  233. // do these ops as two stages instead of addAndMakeVisible() so that the
  234. // component has always got a parent when it gets the visibilityChanged() callback
  235. addChildComponent (panelComponent);
  236. panelComponent->setVisible (true);
  237. panelComponent->toFront (true);
  238. }
  239. repaint();
  240. }
  241. resized();
  242. currentTabChanged (newCurrentTabIndex, newTabName);
  243. }
  244. void TabbedComponent::currentTabChanged (const int, const String&) {}
  245. void TabbedComponent::popupMenuClickOnTab (const int, const String&) {}