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.

117 lines
3.7KB

  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. #include "jucer_SlidingPanelComponent.h"
  18. struct SlidingPanelComponent::DotButton : public Button
  19. {
  20. DotButton (SlidingPanelComponent& sp, int pageIndex)
  21. : Button (String()), owner (sp), index (pageIndex) {}
  22. void paintButton (Graphics& g, bool /*isMouseOverButton*/, bool /*isButtonDown*/) override
  23. {
  24. g.setColour (Colours::white);
  25. const Rectangle<float> r (getLocalBounds().reduced (getWidth() / 4).toFloat());
  26. if (index == owner.getCurrentTabIndex())
  27. g.fillEllipse (r);
  28. else
  29. g.drawEllipse (r, 1.0f);
  30. }
  31. void clicked() override
  32. {
  33. owner.goToTab (index);
  34. }
  35. SlidingPanelComponent& owner;
  36. int index;
  37. };
  38. //==============================================================================
  39. SlidingPanelComponent::SlidingPanelComponent()
  40. : currentIndex (0), dotSize (20)
  41. {
  42. addAndMakeVisible (pageHolder);
  43. }
  44. SlidingPanelComponent::~SlidingPanelComponent()
  45. {
  46. }
  47. SlidingPanelComponent::PageInfo::~PageInfo()
  48. {
  49. if (shouldDelete)
  50. content.deleteAndZero();
  51. }
  52. void SlidingPanelComponent::addTab (const String& tabName,
  53. Component* const contentComponent,
  54. const bool deleteComponentWhenNotNeeded,
  55. const int insertIndex)
  56. {
  57. PageInfo* page = new PageInfo();
  58. pages.insert (insertIndex, page);
  59. page->content = contentComponent;
  60. addAndMakeVisible (page->dotButton = new DotButton (*this, pages.indexOf (page)));
  61. page->name = tabName;
  62. page->shouldDelete = deleteComponentWhenNotNeeded;
  63. pageHolder.addAndMakeVisible (contentComponent);
  64. resized();
  65. }
  66. void SlidingPanelComponent::goToTab (int targetTabIndex)
  67. {
  68. const int xTranslation = (currentIndex - targetTabIndex) * getWidth();
  69. currentIndex = targetTabIndex;
  70. Desktop::getInstance().getAnimator()
  71. .animateComponent (&pageHolder, pageHolder.getBounds().translated (xTranslation, 0),
  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. }