juce_TabbedComponent.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A component with a TabbedButtonBar along one of its sides.
  24. This makes it easy to create a set of tabbed pages, just add a bunch of tabs
  25. with addTab(), and this will take care of showing the pages for you when the
  26. user clicks on a different tab.
  27. @see TabbedButtonBar
  28. @tags{GUI}
  29. */
  30. class JUCE_API TabbedComponent : public Component
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates a TabbedComponent, specifying where the tabs should be placed.
  35. Once created, add some tabs with the addTab() method.
  36. */
  37. explicit TabbedComponent (TabbedButtonBar::Orientation orientation);
  38. /** Destructor. */
  39. ~TabbedComponent() override;
  40. //==============================================================================
  41. /** Changes the placement of the tabs.
  42. This will rearrange the layout to place the tabs along the appropriate
  43. side of this component, and will shift the content component accordingly.
  44. @see TabbedButtonBar::setOrientation
  45. */
  46. void setOrientation (TabbedButtonBar::Orientation orientation);
  47. /** Returns the current tab placement.
  48. @see setOrientation, TabbedButtonBar::getOrientation
  49. */
  50. TabbedButtonBar::Orientation getOrientation() const noexcept;
  51. /** Specifies how many pixels wide or high the tab-bar should be.
  52. If the tabs are placed along the top or bottom, this specified the height
  53. of the bar; if they're along the left or right edges, it'll be the width
  54. of the bar.
  55. */
  56. void setTabBarDepth (int newDepth);
  57. /** Returns the current thickness of the tab bar.
  58. @see setTabBarDepth
  59. */
  60. int getTabBarDepth() const noexcept { return tabDepth; }
  61. /** Specifies the thickness of an outline that should be drawn around the content component.
  62. If this thickness is > 0, a line will be drawn around the three sides of the content
  63. component which don't touch the tab-bar, and the content component will be inset by this amount.
  64. To set the colour of the line, use setColour (outlineColourId, ...).
  65. */
  66. void setOutline (int newThickness);
  67. /** Specifies a gap to leave around the edge of the content component.
  68. Each edge of the content component will be indented by the given number of pixels.
  69. */
  70. void setIndent (int indentThickness);
  71. //==============================================================================
  72. /** Removes all the tabs from the bar.
  73. @see TabbedButtonBar::clearTabs
  74. */
  75. void clearTabs();
  76. /** Adds a tab to the tab-bar.
  77. The component passed in will be shown for the tab. If deleteComponentWhenNotNeeded
  78. is true, then the TabbedComponent will take ownership of the component and will delete
  79. it when the tab is removed or when this object is deleted.
  80. @see TabbedButtonBar::addTab
  81. */
  82. void addTab (const String& tabName,
  83. Colour tabBackgroundColour,
  84. Component* contentComponent,
  85. bool deleteComponentWhenNotNeeded,
  86. int insertIndex = -1);
  87. /** Changes the name of one of the tabs. */
  88. void setTabName (int tabIndex, const String& newName);
  89. /** Gets rid of one of the tabs. */
  90. void removeTab (int tabIndex);
  91. /** Moves a tab to a new index in the list.
  92. Pass -1 as the index to move it to the end of the list.
  93. */
  94. void moveTab (int currentIndex, int newIndex, bool animate = false);
  95. /** Returns the number of tabs in the bar. */
  96. int getNumTabs() const;
  97. /** Returns a list of all the tab names in the bar. */
  98. StringArray getTabNames() const;
  99. /** Returns the content component that was added for the given index.
  100. Be careful not to reposition or delete the components that are returned, as
  101. this will interfere with the TabbedComponent's behaviour.
  102. */
  103. Component* getTabContentComponent (int tabIndex) const noexcept;
  104. /** Returns the colour of one of the tabs. */
  105. Colour getTabBackgroundColour (int tabIndex) const noexcept;
  106. /** Changes the background colour of one of the tabs. */
  107. void setTabBackgroundColour (int tabIndex, Colour newColour);
  108. //==============================================================================
  109. /** Changes the currently-selected tab.
  110. To deselect all the tabs, pass -1 as the index.
  111. @see TabbedButtonBar::setCurrentTabIndex
  112. */
  113. void setCurrentTabIndex (int newTabIndex, bool sendChangeMessage = true);
  114. /** Returns the index of the currently selected tab.
  115. @see addTab, TabbedButtonBar::getCurrentTabIndex()
  116. */
  117. int getCurrentTabIndex() const;
  118. /** Returns the name of the currently selected tab.
  119. @see addTab, TabbedButtonBar::getCurrentTabName()
  120. */
  121. String getCurrentTabName() const;
  122. /** Returns the current component that's filling the panel.
  123. This will return nullptr if there isn't one.
  124. */
  125. Component* getCurrentContentComponent() const noexcept { return panelComponent.get(); }
  126. //==============================================================================
  127. /** Callback method to indicate the selected tab has been changed.
  128. @see setCurrentTabIndex
  129. */
  130. virtual void currentTabChanged (int newCurrentTabIndex, const String& newCurrentTabName);
  131. /** Callback method to indicate that the user has right-clicked on a tab. */
  132. virtual void popupMenuClickOnTab (int tabIndex, const String& tabName);
  133. /** Returns the tab button bar component that is being used. */
  134. TabbedButtonBar& getTabbedButtonBar() const noexcept { return *tabs; }
  135. //==============================================================================
  136. /** A set of colour IDs to use to change the colour of various aspects of the component.
  137. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  138. methods.
  139. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  140. */
  141. enum ColourIds
  142. {
  143. backgroundColourId = 0x1005800, /**< The colour to fill the background behind the tabs. */
  144. outlineColourId = 0x1005801, /**< The colour to use to draw an outline around the content.
  145. (See setOutline) */
  146. };
  147. //==============================================================================
  148. /** @internal */
  149. void paint (Graphics&) override;
  150. /** @internal */
  151. void resized() override;
  152. /** @internal */
  153. void lookAndFeelChanged() override;
  154. protected:
  155. //==============================================================================
  156. /** This creates one of the tab buttons.
  157. If you need to use custom tab components, you can override this method and
  158. return your own class instead of the default.
  159. */
  160. virtual TabBarButton* createTabButton (const String& tabName, int tabIndex);
  161. /** @internal */
  162. std::unique_ptr<TabbedButtonBar> tabs;
  163. private:
  164. //==============================================================================
  165. Array<WeakReference<Component>> contentComponents;
  166. WeakReference<Component> panelComponent;
  167. int tabDepth = 30, outlineThickness = 1, edgeIndent = 0;
  168. struct ButtonBar;
  169. void changeCallback (int newCurrentTabIndex, const String& newTabName);
  170. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TabbedComponent)
  171. };
  172. } // namespace juce