juce_TabbedButtonBar.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. TabBarButton::TabBarButton (const String& name, TabbedButtonBar& bar)
  22. : Button (name), owner (bar)
  23. {
  24. setWantsKeyboardFocus (false);
  25. }
  26. TabBarButton::~TabBarButton() {}
  27. int TabBarButton::getIndex() const { return owner.indexOfTabButton (this); }
  28. Colour TabBarButton::getTabBackgroundColour() const { return owner.getTabBackgroundColour (getIndex()); }
  29. bool TabBarButton::isFrontTab() const { return getToggleState(); }
  30. void TabBarButton::paintButton (Graphics& g, const bool shouldDrawButtonAsHighlighted, const bool shouldDrawButtonAsDown)
  31. {
  32. getLookAndFeel().drawTabButton (*this, g, shouldDrawButtonAsHighlighted, shouldDrawButtonAsDown);
  33. }
  34. void TabBarButton::clicked (const ModifierKeys& mods)
  35. {
  36. if (mods.isPopupMenu())
  37. owner.popupMenuClickOnTab (getIndex(), getButtonText());
  38. else
  39. owner.setCurrentTabIndex (getIndex());
  40. }
  41. bool TabBarButton::hitTest (int mx, int my)
  42. {
  43. auto area = getActiveArea();
  44. if (owner.isVertical())
  45. {
  46. if (isPositiveAndBelow (mx, getWidth())
  47. && my >= area.getY() + overlapPixels && my < area.getBottom() - overlapPixels)
  48. return true;
  49. }
  50. else
  51. {
  52. if (isPositiveAndBelow (my, getHeight())
  53. && mx >= area.getX() + overlapPixels && mx < area.getRight() - overlapPixels)
  54. return true;
  55. }
  56. Path p;
  57. getLookAndFeel().createTabButtonShape (*this, p, false, false);
  58. return p.contains ((float) (mx - area.getX()),
  59. (float) (my - area.getY()));
  60. }
  61. int TabBarButton::getBestTabLength (const int depth)
  62. {
  63. return getLookAndFeel().getTabButtonBestWidth (*this, depth);
  64. }
  65. void TabBarButton::calcAreas (Rectangle<int>& extraComp, Rectangle<int>& textArea) const
  66. {
  67. auto& lf = getLookAndFeel();
  68. textArea = getActiveArea();
  69. auto depth = owner.isVertical() ? textArea.getWidth() : textArea.getHeight();
  70. auto overlap = lf.getTabButtonOverlap (depth);
  71. if (overlap > 0)
  72. {
  73. if (owner.isVertical())
  74. textArea.reduce (0, overlap);
  75. else
  76. textArea.reduce (overlap, 0);
  77. }
  78. if (extraComponent != nullptr)
  79. {
  80. extraComp = lf.getTabButtonExtraComponentBounds (*this, textArea, *extraComponent);
  81. auto orientation = owner.getOrientation();
  82. if (orientation == TabbedButtonBar::TabsAtLeft || orientation == TabbedButtonBar::TabsAtRight)
  83. {
  84. if (extraComp.getCentreY() > textArea.getCentreY())
  85. textArea.setBottom (jmin (textArea.getBottom(), extraComp.getY()));
  86. else
  87. textArea.setTop (jmax (textArea.getY(), extraComp.getBottom()));
  88. }
  89. else
  90. {
  91. if (extraComp.getCentreX() > textArea.getCentreX())
  92. textArea.setRight (jmin (textArea.getRight(), extraComp.getX()));
  93. else
  94. textArea.setLeft (jmax (textArea.getX(), extraComp.getRight()));
  95. }
  96. }
  97. }
  98. Rectangle<int> TabBarButton::getTextArea() const
  99. {
  100. Rectangle<int> extraComp, textArea;
  101. calcAreas (extraComp, textArea);
  102. return textArea;
  103. }
  104. Rectangle<int> TabBarButton::getActiveArea() const
  105. {
  106. auto r = getLocalBounds();
  107. auto spaceAroundImage = getLookAndFeel().getTabButtonSpaceAroundImage();
  108. auto orientation = owner.getOrientation();
  109. if (orientation != TabbedButtonBar::TabsAtLeft) r.removeFromRight (spaceAroundImage);
  110. if (orientation != TabbedButtonBar::TabsAtRight) r.removeFromLeft (spaceAroundImage);
  111. if (orientation != TabbedButtonBar::TabsAtBottom) r.removeFromTop (spaceAroundImage);
  112. if (orientation != TabbedButtonBar::TabsAtTop) r.removeFromBottom (spaceAroundImage);
  113. return r;
  114. }
  115. void TabBarButton::setExtraComponent (Component* comp, ExtraComponentPlacement placement)
  116. {
  117. jassert (extraCompPlacement == beforeText || extraCompPlacement == afterText);
  118. extraCompPlacement = placement;
  119. extraComponent.reset (comp);
  120. addAndMakeVisible (extraComponent.get());
  121. resized();
  122. }
  123. void TabBarButton::childBoundsChanged (Component* c)
  124. {
  125. if (c == extraComponent.get())
  126. {
  127. owner.resized();
  128. resized();
  129. }
  130. }
  131. void TabBarButton::resized()
  132. {
  133. if (extraComponent != nullptr)
  134. {
  135. Rectangle<int> extraComp, textArea;
  136. calcAreas (extraComp, textArea);
  137. if (! extraComp.isEmpty())
  138. extraComponent->setBounds (extraComp);
  139. }
  140. }
  141. //==============================================================================
  142. class TabbedButtonBar::BehindFrontTabComp : public Component
  143. {
  144. public:
  145. BehindFrontTabComp (TabbedButtonBar& tb) : owner (tb)
  146. {
  147. setInterceptsMouseClicks (false, false);
  148. }
  149. void paint (Graphics& g) override
  150. {
  151. getLookAndFeel().drawTabAreaBehindFrontButton (owner, g, getWidth(), getHeight());
  152. }
  153. void enablementChanged() override
  154. {
  155. repaint();
  156. }
  157. TabbedButtonBar& owner;
  158. JUCE_DECLARE_NON_COPYABLE (BehindFrontTabComp)
  159. };
  160. //==============================================================================
  161. TabbedButtonBar::TabbedButtonBar (Orientation orientationToUse)
  162. : orientation (orientationToUse)
  163. {
  164. setInterceptsMouseClicks (false, true);
  165. behindFrontTab.reset (new BehindFrontTabComp (*this));
  166. addAndMakeVisible (behindFrontTab.get());
  167. setFocusContainer (true);
  168. }
  169. TabbedButtonBar::~TabbedButtonBar()
  170. {
  171. tabs.clear();
  172. extraTabsButton.reset();
  173. }
  174. //==============================================================================
  175. void TabbedButtonBar::setOrientation (const Orientation newOrientation)
  176. {
  177. orientation = newOrientation;
  178. for (auto* child : getChildren())
  179. child->resized();
  180. resized();
  181. }
  182. TabBarButton* TabbedButtonBar::createTabButton (const String& name, const int /*index*/)
  183. {
  184. return new TabBarButton (name, *this);
  185. }
  186. void TabbedButtonBar::setMinimumTabScaleFactor (double newMinimumScale)
  187. {
  188. minimumScale = newMinimumScale;
  189. resized();
  190. }
  191. //==============================================================================
  192. void TabbedButtonBar::clearTabs()
  193. {
  194. tabs.clear();
  195. extraTabsButton.reset();
  196. setCurrentTabIndex (-1);
  197. }
  198. void TabbedButtonBar::addTab (const String& tabName,
  199. Colour tabBackgroundColour,
  200. int insertIndex)
  201. {
  202. jassert (tabName.isNotEmpty()); // you have to give them all a name..
  203. if (tabName.isNotEmpty())
  204. {
  205. if (! isPositiveAndBelow (insertIndex, tabs.size()))
  206. insertIndex = tabs.size();
  207. auto* currentTab = tabs[currentTabIndex];
  208. auto* newTab = new TabInfo();
  209. newTab->name = tabName;
  210. newTab->colour = tabBackgroundColour;
  211. newTab->button.reset (createTabButton (tabName, insertIndex));
  212. jassert (newTab->button != nullptr);
  213. tabs.insert (insertIndex, newTab);
  214. currentTabIndex = tabs.indexOf (currentTab);
  215. addAndMakeVisible (newTab->button.get(), insertIndex);
  216. resized();
  217. if (currentTabIndex < 0)
  218. setCurrentTabIndex (0);
  219. }
  220. }
  221. void TabbedButtonBar::setTabName (int tabIndex, const String& newName)
  222. {
  223. if (auto* tab = tabs[tabIndex])
  224. {
  225. if (tab->name != newName)
  226. {
  227. tab->name = newName;
  228. tab->button->setButtonText (newName);
  229. resized();
  230. }
  231. }
  232. }
  233. void TabbedButtonBar::removeTab (const int indexToRemove, const bool animate)
  234. {
  235. if (isPositiveAndBelow (indexToRemove, tabs.size()))
  236. {
  237. auto oldSelectedIndex = currentTabIndex;
  238. if (indexToRemove == currentTabIndex)
  239. oldSelectedIndex = -1;
  240. else if (indexToRemove < oldSelectedIndex)
  241. --oldSelectedIndex;
  242. tabs.remove (indexToRemove);
  243. setCurrentTabIndex (oldSelectedIndex);
  244. updateTabPositions (animate);
  245. }
  246. }
  247. void TabbedButtonBar::moveTab (const int currentIndex, const int newIndex, const bool animate)
  248. {
  249. auto* currentTab = tabs[currentTabIndex];
  250. tabs.move (currentIndex, newIndex);
  251. currentTabIndex = tabs.indexOf (currentTab);
  252. updateTabPositions (animate);
  253. }
  254. int TabbedButtonBar::getNumTabs() const
  255. {
  256. return tabs.size();
  257. }
  258. String TabbedButtonBar::getCurrentTabName() const
  259. {
  260. if (auto* tab = tabs [currentTabIndex])
  261. return tab->name;
  262. return {};
  263. }
  264. StringArray TabbedButtonBar::getTabNames() const
  265. {
  266. StringArray names;
  267. for (auto* t : tabs)
  268. names.add (t->name);
  269. return names;
  270. }
  271. void TabbedButtonBar::setCurrentTabIndex (int newIndex, bool shouldSendChangeMessage)
  272. {
  273. if (currentTabIndex != newIndex)
  274. {
  275. if (! isPositiveAndBelow (newIndex, tabs.size()))
  276. newIndex = -1;
  277. currentTabIndex = newIndex;
  278. for (int i = 0; i < tabs.size(); ++i)
  279. tabs.getUnchecked(i)->button->setToggleState (i == newIndex, dontSendNotification);
  280. resized();
  281. if (shouldSendChangeMessage)
  282. sendChangeMessage();
  283. currentTabChanged (newIndex, getCurrentTabName());
  284. }
  285. }
  286. TabBarButton* TabbedButtonBar::getTabButton (const int index) const
  287. {
  288. if (auto* tab = tabs[index])
  289. return static_cast<TabBarButton*> (tab->button.get());
  290. return nullptr;
  291. }
  292. int TabbedButtonBar::indexOfTabButton (const TabBarButton* button) const
  293. {
  294. for (int i = tabs.size(); --i >= 0;)
  295. if (tabs.getUnchecked(i)->button.get() == button)
  296. return i;
  297. return -1;
  298. }
  299. Rectangle<int> TabbedButtonBar::getTargetBounds (TabBarButton* button) const
  300. {
  301. if (button == nullptr || indexOfTabButton (button) == -1)
  302. return {};
  303. auto& animator = Desktop::getInstance().getAnimator();
  304. return animator.isAnimating (button) ? animator.getComponentDestination (button)
  305. : button->getBounds();
  306. }
  307. void TabbedButtonBar::lookAndFeelChanged()
  308. {
  309. extraTabsButton.reset();
  310. resized();
  311. }
  312. void TabbedButtonBar::paint (Graphics& g)
  313. {
  314. getLookAndFeel().drawTabbedButtonBarBackground (*this, g);
  315. }
  316. void TabbedButtonBar::resized()
  317. {
  318. updateTabPositions (false);
  319. }
  320. //==============================================================================
  321. void TabbedButtonBar::updateTabPositions (bool animate)
  322. {
  323. auto& lf = getLookAndFeel();
  324. auto depth = getWidth();
  325. auto length = getHeight();
  326. if (! isVertical())
  327. std::swap (depth, length);
  328. auto overlap = lf.getTabButtonOverlap (depth) + lf.getTabButtonSpaceAroundImage() * 2;
  329. auto totalLength = jmax (0, overlap);
  330. auto numVisibleButtons = tabs.size();
  331. for (int i = 0; i < tabs.size(); ++i)
  332. {
  333. auto* tb = tabs.getUnchecked(i)->button.get();
  334. totalLength += tb->getBestTabLength (depth) - overlap;
  335. tb->overlapPixels = jmax (0, overlap / 2);
  336. }
  337. double scale = 1.0;
  338. if (totalLength > length)
  339. scale = jmax (minimumScale, length / (double) totalLength);
  340. const bool isTooBig = (int) (totalLength * scale) > length;
  341. int tabsButtonPos = 0;
  342. if (isTooBig)
  343. {
  344. if (extraTabsButton == nullptr)
  345. {
  346. extraTabsButton.reset (lf.createTabBarExtrasButton());
  347. addAndMakeVisible (extraTabsButton.get());
  348. extraTabsButton->setAlwaysOnTop (true);
  349. extraTabsButton->setTriggeredOnMouseDown (true);
  350. extraTabsButton->onClick = [this] { showExtraItemsMenu(); };
  351. }
  352. auto buttonSize = jmin (proportionOfWidth (0.7f), proportionOfHeight (0.7f));
  353. extraTabsButton->setSize (buttonSize, buttonSize);
  354. if (isVertical())
  355. {
  356. tabsButtonPos = getHeight() - buttonSize / 2 - 1;
  357. extraTabsButton->setCentrePosition (getWidth() / 2, tabsButtonPos);
  358. }
  359. else
  360. {
  361. tabsButtonPos = getWidth() - buttonSize / 2 - 1;
  362. extraTabsButton->setCentrePosition (tabsButtonPos, getHeight() / 2);
  363. }
  364. totalLength = 0;
  365. for (int i = 0; i < tabs.size(); ++i)
  366. {
  367. auto* tb = tabs.getUnchecked(i)->button.get();
  368. auto newLength = totalLength + tb->getBestTabLength (depth);
  369. if (i > 0 && newLength * minimumScale > tabsButtonPos)
  370. {
  371. totalLength += overlap;
  372. break;
  373. }
  374. numVisibleButtons = i + 1;
  375. totalLength = newLength - overlap;
  376. }
  377. scale = jmax (minimumScale, tabsButtonPos / (double) totalLength);
  378. }
  379. else
  380. {
  381. extraTabsButton.reset();
  382. }
  383. int pos = 0;
  384. TabBarButton* frontTab = nullptr;
  385. auto& animator = Desktop::getInstance().getAnimator();
  386. for (int i = 0; i < tabs.size(); ++i)
  387. {
  388. if (auto* tb = getTabButton (i))
  389. {
  390. auto bestLength = roundToInt (scale * tb->getBestTabLength (depth));
  391. if (i < numVisibleButtons)
  392. {
  393. auto newBounds = isVertical() ? Rectangle<int> (0, pos, getWidth(), bestLength)
  394. : Rectangle<int> (pos, 0, bestLength, getHeight());
  395. if (animate)
  396. {
  397. animator.animateComponent (tb, newBounds, 1.0f, 200, false, 3.0, 0.0);
  398. }
  399. else
  400. {
  401. animator.cancelAnimation (tb, false);
  402. tb->setBounds (newBounds);
  403. }
  404. tb->toBack();
  405. if (i == currentTabIndex)
  406. frontTab = tb;
  407. tb->setVisible (true);
  408. }
  409. else
  410. {
  411. tb->setVisible (false);
  412. }
  413. pos += bestLength - overlap;
  414. }
  415. }
  416. behindFrontTab->setBounds (getLocalBounds());
  417. if (frontTab != nullptr)
  418. {
  419. frontTab->toFront (false);
  420. behindFrontTab->toBehind (frontTab);
  421. }
  422. }
  423. //==============================================================================
  424. Colour TabbedButtonBar::getTabBackgroundColour (int tabIndex)
  425. {
  426. if (auto* tab = tabs[tabIndex])
  427. return tab->colour;
  428. return Colours::transparentBlack;
  429. }
  430. void TabbedButtonBar::setTabBackgroundColour (int tabIndex, Colour newColour)
  431. {
  432. if (auto* tab = tabs [tabIndex])
  433. {
  434. if (tab->colour != newColour)
  435. {
  436. tab->colour = newColour;
  437. repaint();
  438. }
  439. }
  440. }
  441. void TabbedButtonBar::showExtraItemsMenu()
  442. {
  443. PopupMenu m;
  444. for (int i = 0; i < tabs.size(); ++i)
  445. {
  446. auto* tab = tabs.getUnchecked(i);
  447. if (! tab->button->isVisible())
  448. m.addItem (PopupMenu::Item (tab->name)
  449. .setTicked (i == currentTabIndex)
  450. .setAction ([this, i] { setCurrentTabIndex (i); }));
  451. }
  452. m.showMenuAsync (PopupMenu::Options()
  453. .withDeletionCheck (*this)
  454. .withTargetComponent (extraTabsButton.get()));
  455. }
  456. //==============================================================================
  457. void TabbedButtonBar::currentTabChanged (int, const String&) {}
  458. void TabbedButtonBar::popupMenuClickOnTab (int, const String&) {}
  459. } // namespace juce