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
4.0KB

  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. //==============================================================================
  19. // TEMPORARY COPY OF TAB HELPERS FOR SLIDER PROTOTYPE, REMOVE BEFORE RELEASE ///
  20. //==============================================================================
  21. namespace TabbedComponentHelpers
  22. {
  23. const Identifier deleteComponentId ("deleteByTabComp_");
  24. static void deleteIfNecessary (Component* const comp)
  25. {
  26. if (comp != nullptr && (bool) comp->getProperties() [deleteComponentId])
  27. delete comp;
  28. }
  29. }
  30. //==============================================================================
  31. SlidingPanelComponent::SlidingPanelComponent()
  32. : currentTabIndex (0), dotSize (20)
  33. {
  34. addAndMakeVisible (slide);
  35. }
  36. SlidingPanelComponent::~SlidingPanelComponent()
  37. {
  38. for (int i = contentComponents.size(); --i >= 0;)
  39. if (Component* c = contentComponents.getReference(i))
  40. TabbedComponentHelpers::deleteIfNecessary (c);
  41. }
  42. void SlidingPanelComponent::addTab (const String& tabName,
  43. Component* const contentComponent,
  44. const bool deleteComponentWhenNotNeeded,
  45. const int insertIndex)
  46. {
  47. contentComponents.insert (insertIndex, WeakReference<Component> (contentComponent));
  48. tabNames.insert (insertIndex, tabName);
  49. if (deleteComponentWhenNotNeeded && contentComponent != nullptr)
  50. contentComponent->getProperties().set (TabbedComponentHelpers::deleteComponentId, true);
  51. slide.addAndMakeVisible (contentComponent);
  52. resized();
  53. }
  54. void SlidingPanelComponent::goToTab (int targetTabIndex)
  55. {
  56. const int xTranslation = (currentTabIndex - targetTabIndex) * getWidth();
  57. currentTabIndex = targetTabIndex;
  58. Desktop::getInstance().getAnimator()
  59. .animateComponent (&slide, slide.getBounds().translated (xTranslation, 0),
  60. 1.0f, 600, false, 0.0, 0.0);
  61. repaint();
  62. }
  63. void SlidingPanelComponent::paint (Graphics& g)
  64. {
  65. Rectangle<int> dotHolder = getLocalBounds();
  66. dotHolder.reduce ((getWidth() - dotSize * getNumTabs()) / 2, 20);
  67. dotHolder = dotHolder.removeFromBottom (dotSize);
  68. g.setColour (Colours::white);
  69. for (int i = 0; i < getNumTabs(); ++i)
  70. {
  71. const Rectangle<float> r (dotHolder.removeFromLeft (dotSize).reduced (5, 5).toFloat());
  72. if (i == currentTabIndex)
  73. g.fillEllipse (r);
  74. else
  75. g.drawEllipse (r, 1.0f);
  76. }
  77. }
  78. void SlidingPanelComponent::resized()
  79. {
  80. slide.setBounds (-currentTabIndex * getWidth(), slide.getPosition().y,
  81. getNumTabs() * getWidth(), getHeight());
  82. Rectangle<int> content (getLocalBounds());
  83. content.removeFromBottom (20 + 2 * dotSize);
  84. for (int i = contentComponents.size(); --i >= 0;)
  85. if (Component* c = contentComponents.getReference(i))
  86. c->setBounds (content.translated (i * content.getWidth(), 0));
  87. }