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