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.

115 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #include "../jucer_Headers.h"
  18. #include "jucer_SlidingPanelComponent.h"
  19. struct SlidingPanelComponent::DotButton : public Button
  20. {
  21. DotButton (SlidingPanelComponent& sp, int pageIndex)
  22. : Button (String()), owner (sp), index (pageIndex) {}
  23. void paintButton (Graphics& g, bool /*isMouseOverButton*/, bool /*isButtonDown*/) override
  24. {
  25. g.setColour (findColour (defaultButtonBackgroundColourId));
  26. const auto r = getLocalBounds().reduced (getWidth() / 4).toFloat();
  27. if (index == owner.getCurrentTabIndex())
  28. g.fillEllipse (r);
  29. else
  30. g.drawEllipse (r, 1.0f);
  31. }
  32. void clicked() override
  33. {
  34. owner.goToTab (index);
  35. }
  36. SlidingPanelComponent& owner;
  37. int index;
  38. };
  39. //==============================================================================
  40. SlidingPanelComponent::SlidingPanelComponent()
  41. : currentIndex (0), dotSize (20)
  42. {
  43. addAndMakeVisible (pageHolder);
  44. }
  45. SlidingPanelComponent::~SlidingPanelComponent()
  46. {
  47. }
  48. SlidingPanelComponent::PageInfo::~PageInfo()
  49. {
  50. if (shouldDelete)
  51. content.deleteAndZero();
  52. }
  53. void SlidingPanelComponent::addTab (const String& tabName,
  54. Component* const contentComponent,
  55. const bool deleteComponentWhenNotNeeded,
  56. const int insertIndex)
  57. {
  58. PageInfo* page = new PageInfo();
  59. pages.insert (insertIndex, page);
  60. page->content = contentComponent;
  61. addAndMakeVisible (page->dotButton = new DotButton (*this, pages.indexOf (page)));
  62. page->name = tabName;
  63. page->shouldDelete = deleteComponentWhenNotNeeded;
  64. pageHolder.addAndMakeVisible (contentComponent);
  65. resized();
  66. }
  67. void SlidingPanelComponent::goToTab (int targetTabIndex)
  68. {
  69. currentIndex = targetTabIndex;
  70. Desktop::getInstance().getAnimator()
  71. .animateComponent (&pageHolder, pageHolder.getBounds().withX (-targetTabIndex * getWidth()),
  72. 1.0f, 600, false, 0.0, 0.0);
  73. repaint();
  74. }
  75. void SlidingPanelComponent::resized()
  76. {
  77. pageHolder.setBounds (-currentIndex * getWidth(), pageHolder.getPosition().y,
  78. getNumTabs() * getWidth(), getHeight());
  79. Rectangle<int> content (getLocalBounds());
  80. Rectangle<int> dotHolder = content.removeFromBottom (20 + dotSize)
  81. .reduced ((content.getWidth() - dotSize * getNumTabs()) / 2, 10);
  82. for (int i = 0; i < getNumTabs(); ++i)
  83. pages.getUnchecked(i)->dotButton->setBounds (dotHolder.removeFromLeft (dotSize));
  84. for (int i = pages.size(); --i >= 0;)
  85. if (Component* c = pages.getUnchecked(i)->content)
  86. c->setBounds (content.translated (i * content.getWidth(), 0));
  87. }