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.

120 lines
3.9KB

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