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.

126 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. {
  33. dotSize = 20;
  34. numTabs = 0;
  35. currentTabIndex = 0;
  36. addAndMakeVisible (&slide);
  37. }
  38. SlidingPanelComponent::~SlidingPanelComponent ()
  39. {
  40. for (int i = contentComponents.size(); --i >= 0;)
  41. if (Component* c = contentComponents.getReference(i))
  42. TabbedComponentHelpers::deleteIfNecessary (c);
  43. }
  44. void SlidingPanelComponent::addTab (const String& tabName,
  45. Component* const contentComponent,
  46. const bool deleteComponentWhenNotNeeded,
  47. const int insertIndex)
  48. {
  49. contentComponents.insert (insertIndex, WeakReference<Component> (contentComponent));
  50. tabNames.insert (insertIndex, tabName);
  51. if (deleteComponentWhenNotNeeded && contentComponent != nullptr)
  52. contentComponent->getProperties().set (TabbedComponentHelpers::deleteComponentId, true);
  53. slide.addAndMakeVisible (contentComponent);
  54. numTabs++;
  55. resized();
  56. }
  57. void SlidingPanelComponent::goToTab (int targetTabIndex)
  58. {
  59. int xTranslation = (currentTabIndex - targetTabIndex) * getWidth();
  60. currentTabIndex = targetTabIndex;
  61. Desktop::getInstance().getAnimator().animateComponent (&slide, slide.getBounds().translated(xTranslation, 0), 1.0f, 600, false, 0.0, 0.0);
  62. repaint();
  63. }
  64. void SlidingPanelComponent::paint (Graphics& g)
  65. {
  66. Rectangle<int> dotHolder = getLocalBounds();
  67. dotHolder.reduce ((getWidth() - dotSize * numTabs)/2, 20);
  68. dotHolder = dotHolder.removeFromBottom (dotSize);
  69. g.setColour (Colours::white);
  70. for (int i = 0; i < numTabs; i++)
  71. {
  72. if (i == currentTabIndex)
  73. {
  74. g.fillEllipse (dotHolder.removeFromLeft (dotSize).reduced (5, 5).toFloat());
  75. }
  76. else
  77. {
  78. g.drawEllipse (dotHolder.removeFromLeft (dotSize).reduced (5, 5).toFloat(), 1.0f);
  79. }
  80. }
  81. }
  82. void SlidingPanelComponent::resized()
  83. {
  84. slide.setBounds (-currentTabIndex*getWidth(), slide.getPosition().y, numTabs * getWidth(), getHeight());
  85. Rectangle<int> content (getLocalBounds());
  86. content.removeFromBottom (20 + 2 * dotSize);
  87. for (int i = contentComponents.size(); --i >= 0;)
  88. if (Component* c = contentComponents.getReference(i))
  89. c->setBounds (content.translated (i*content.getWidth(), 0));
  90. }