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.

308 lines
9.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. namespace TabbedComponentHelpers
  19. {
  20. const Identifier deleteComponentId ("deleteByTabComp_");
  21. void deleteIfNecessary (Component* const comp)
  22. {
  23. if (comp != nullptr && (bool) comp->getProperties() [deleteComponentId])
  24. delete comp;
  25. }
  26. Rectangle<int> getTabArea (Rectangle<int>& content, BorderSize<int>& outline,
  27. const TabbedButtonBar::Orientation orientation, const int tabDepth)
  28. {
  29. switch (orientation)
  30. {
  31. case TabbedButtonBar::TabsAtTop: outline.setTop (0); return content.removeFromTop (tabDepth);
  32. case TabbedButtonBar::TabsAtBottom: outline.setBottom (0); return content.removeFromBottom (tabDepth);
  33. case TabbedButtonBar::TabsAtLeft: outline.setLeft (0); return content.removeFromLeft (tabDepth);
  34. case TabbedButtonBar::TabsAtRight: outline.setRight (0); return content.removeFromRight (tabDepth);
  35. default: jassertfalse; break;
  36. }
  37. return Rectangle<int>();
  38. }
  39. }
  40. //==============================================================================
  41. class TabbedComponent::ButtonBar : public TabbedButtonBar
  42. {
  43. public:
  44. ButtonBar (TabbedComponent& owner_, const TabbedButtonBar::Orientation orientation_)
  45. : TabbedButtonBar (orientation_),
  46. owner (owner_)
  47. {
  48. }
  49. void currentTabChanged (int newCurrentTabIndex, const String& newTabName)
  50. {
  51. owner.changeCallback (newCurrentTabIndex, newTabName);
  52. }
  53. void popupMenuClickOnTab (int tabIndex, const String& tabName)
  54. {
  55. owner.popupMenuClickOnTab (tabIndex, tabName);
  56. }
  57. Colour getTabBackgroundColour (const int tabIndex)
  58. {
  59. return owner.tabs->getTabBackgroundColour (tabIndex);
  60. }
  61. TabBarButton* createTabButton (const String& tabName, int tabIndex)
  62. {
  63. return owner.createTabButton (tabName, tabIndex);
  64. }
  65. private:
  66. TabbedComponent& owner;
  67. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonBar);
  68. };
  69. //==============================================================================
  70. TabbedComponent::TabbedComponent (const TabbedButtonBar::Orientation orientation)
  71. : tabDepth (30),
  72. outlineThickness (1),
  73. edgeIndent (0)
  74. {
  75. addAndMakeVisible (tabs = new ButtonBar (*this, orientation));
  76. }
  77. TabbedComponent::~TabbedComponent()
  78. {
  79. clearTabs();
  80. tabs = nullptr;
  81. }
  82. //==============================================================================
  83. void TabbedComponent::setOrientation (const TabbedButtonBar::Orientation orientation)
  84. {
  85. tabs->setOrientation (orientation);
  86. resized();
  87. }
  88. TabbedButtonBar::Orientation TabbedComponent::getOrientation() const noexcept
  89. {
  90. return tabs->getOrientation();
  91. }
  92. void TabbedComponent::setTabBarDepth (const int newDepth)
  93. {
  94. if (tabDepth != newDepth)
  95. {
  96. tabDepth = newDepth;
  97. resized();
  98. }
  99. }
  100. TabBarButton* TabbedComponent::createTabButton (const String& tabName, const int /*tabIndex*/)
  101. {
  102. return new TabBarButton (tabName, *tabs);
  103. }
  104. //==============================================================================
  105. void TabbedComponent::clearTabs()
  106. {
  107. if (panelComponent != nullptr)
  108. {
  109. panelComponent->setVisible (false);
  110. removeChildComponent (panelComponent);
  111. panelComponent = nullptr;
  112. }
  113. tabs->clearTabs();
  114. for (int i = contentComponents.size(); --i >= 0;)
  115. TabbedComponentHelpers::deleteIfNecessary (contentComponents.getReference (i));
  116. contentComponents.clear();
  117. }
  118. void TabbedComponent::addTab (const String& tabName,
  119. const Colour& tabBackgroundColour,
  120. Component* const contentComponent,
  121. const bool deleteComponentWhenNotNeeded,
  122. const int insertIndex)
  123. {
  124. contentComponents.insert (insertIndex, WeakReference<Component> (contentComponent));
  125. if (deleteComponentWhenNotNeeded && contentComponent != nullptr)
  126. contentComponent->getProperties().set (TabbedComponentHelpers::deleteComponentId, true);
  127. tabs->addTab (tabName, tabBackgroundColour, insertIndex);
  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, const 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 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 (contentComponents.getReference (i) != nullptr)
  212. contentComponents.getReference (i)->setBounds (content);
  213. }
  214. void TabbedComponent::lookAndFeelChanged()
  215. {
  216. for (int i = contentComponents.size(); --i >= 0;)
  217. if (contentComponents.getReference (i) != nullptr)
  218. contentComponents.getReference (i)->lookAndFeelChanged();
  219. }
  220. void TabbedComponent::changeCallback (const int newCurrentTabIndex, const String& newTabName)
  221. {
  222. if (panelComponent != nullptr)
  223. {
  224. panelComponent->setVisible (false);
  225. removeChildComponent (panelComponent);
  226. panelComponent = nullptr;
  227. }
  228. if (getCurrentTabIndex() >= 0)
  229. {
  230. panelComponent = getTabContentComponent (getCurrentTabIndex());
  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&) {}