juce_TabbedButtonBar.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. class TabbedButtonBar;
  22. //==============================================================================
  23. /** In a TabbedButtonBar, this component is used for each of the buttons.
  24. If you want to create a TabbedButtonBar with custom tab components, derive
  25. your component from this class, and override the TabbedButtonBar::createTabButton()
  26. method to create it instead of the default one.
  27. @see TabbedButtonBar
  28. @tags{GUI}
  29. */
  30. class JUCE_API TabBarButton : public Button
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates the tab button. */
  35. TabBarButton (const String& name, TabbedButtonBar& ownerBar);
  36. /** Destructor. */
  37. ~TabBarButton() override;
  38. /** Returns the bar that contains this button. */
  39. TabbedButtonBar& getTabbedButtonBar() const { return owner; }
  40. //==============================================================================
  41. /** When adding an extra component to the tab, this indicates which side of
  42. the text it should be placed on. */
  43. enum ExtraComponentPlacement
  44. {
  45. beforeText,
  46. afterText
  47. };
  48. /** Sets an extra component that will be shown in the tab.
  49. This optional component will be positioned inside the tab, either to the left or right
  50. of the text. You could use this to implement things like a close button or a graphical
  51. status indicator. If a non-null component is passed-in, the TabbedButtonBar will take
  52. ownership of it and delete it when required.
  53. */
  54. void setExtraComponent (Component* extraTabComponent,
  55. ExtraComponentPlacement extraComponentPlacement);
  56. /** Returns the custom component, if there is one. */
  57. Component* getExtraComponent() const noexcept { return extraComponent.get(); }
  58. /** Returns the placement of the custom component, if there is one. */
  59. ExtraComponentPlacement getExtraComponentPlacement() const noexcept { return extraCompPlacement; }
  60. /** Returns an area of the component that's safe to draw in.
  61. This deals with the orientation of the tabs, which affects which side is
  62. touching the tabbed box's content component.
  63. */
  64. Rectangle<int> getActiveArea() const;
  65. /** Returns the area of the component that should contain its text. */
  66. Rectangle<int> getTextArea() const;
  67. /** Returns this tab's index in its tab bar. */
  68. int getIndex() const;
  69. /** Returns the colour of the tab. */
  70. Colour getTabBackgroundColour() const;
  71. /** Returns true if this is the frontmost (selected) tab. */
  72. bool isFrontTab() const;
  73. //==============================================================================
  74. /** Chooses the best length for the tab, given the specified depth.
  75. If the tab is horizontal, this should return its width, and the depth
  76. specifies its height. If it's vertical, it should return the height, and
  77. the depth is actually its width.
  78. */
  79. virtual int getBestTabLength (int depth);
  80. //==============================================================================
  81. /** @internal */
  82. void paintButton (Graphics&, bool, bool) override;
  83. /** @internal */
  84. void clicked (const ModifierKeys&) override;
  85. /** @internal */
  86. bool hitTest (int x, int y) override;
  87. /** @internal */
  88. void resized() override;
  89. /** @internal */
  90. void childBoundsChanged (Component*) override;
  91. protected:
  92. friend class TabbedButtonBar;
  93. TabbedButtonBar& owner;
  94. int overlapPixels = 0;
  95. std::unique_ptr<Component> extraComponent;
  96. ExtraComponentPlacement extraCompPlacement = afterText;
  97. private:
  98. using Button::clicked;
  99. void calcAreas (Rectangle<int>&, Rectangle<int>&) const;
  100. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TabBarButton)
  101. };
  102. //==============================================================================
  103. /**
  104. A vertical or horizontal bar containing tabs that you can select.
  105. You can use one of these to generate things like a dialog box that has
  106. tabbed pages you can flip between. Attach a ChangeListener to the
  107. button bar to be told when the user changes the page.
  108. An easier method than doing this is to use a TabbedComponent, which
  109. contains its own TabbedButtonBar and which takes care of the layout
  110. and other housekeeping.
  111. @see TabbedComponent
  112. @tags{GUI}
  113. */
  114. class JUCE_API TabbedButtonBar : public Component,
  115. public ChangeBroadcaster
  116. {
  117. public:
  118. //==============================================================================
  119. /** The placement of the tab-bar
  120. @see setOrientation, getOrientation
  121. */
  122. enum Orientation
  123. {
  124. TabsAtTop,
  125. TabsAtBottom,
  126. TabsAtLeft,
  127. TabsAtRight
  128. };
  129. //==============================================================================
  130. /** Creates a TabbedButtonBar with a given orientation.
  131. You can change the orientation later if you need to.
  132. */
  133. TabbedButtonBar (Orientation orientation);
  134. /** Destructor. */
  135. ~TabbedButtonBar() override;
  136. //==============================================================================
  137. /** Changes the bar's orientation.
  138. This won't change the bar's actual size - you'll need to do that yourself,
  139. but this determines which direction the tabs go in, and which side they're
  140. stuck to.
  141. */
  142. void setOrientation (Orientation orientation);
  143. /** Returns the bar's current orientation.
  144. @see setOrientation
  145. */
  146. Orientation getOrientation() const noexcept { return orientation; }
  147. /** Returns true if the orientation is TabsAtLeft or TabsAtRight. */
  148. bool isVertical() const noexcept { return orientation == TabsAtLeft || orientation == TabsAtRight; }
  149. /** Returns the thickness of the bar, which may be its width or height, depending on the orientation. */
  150. int getThickness() const noexcept { return isVertical() ? getWidth() : getHeight(); }
  151. /** Changes the minimum scale factor to which the tabs can be compressed when trying to
  152. fit a lot of tabs on-screen.
  153. */
  154. void setMinimumTabScaleFactor (double newMinimumScale);
  155. //==============================================================================
  156. /** Deletes all the tabs from the bar.
  157. @see addTab
  158. */
  159. void clearTabs();
  160. /** Adds a tab to the bar.
  161. Tabs are added in left-to-right reading order.
  162. If this is the first tab added, it'll also be automatically selected.
  163. */
  164. void addTab (const String& tabName,
  165. Colour tabBackgroundColour,
  166. int insertIndex);
  167. /** Changes the name of one of the tabs. */
  168. void setTabName (int tabIndex, const String& newName);
  169. /** Gets rid of one of the tabs. */
  170. void removeTab (int tabIndex, bool animate = false);
  171. /** Moves a tab to a new index in the list.
  172. Pass -1 as the index to move it to the end of the list.
  173. */
  174. void moveTab (int currentIndex, int newIndex, bool animate = false);
  175. /** Returns the number of tabs in the bar. */
  176. int getNumTabs() const;
  177. /** Returns a list of all the tab names in the bar. */
  178. StringArray getTabNames() const;
  179. /** Changes the currently selected tab.
  180. This will send a change message and cause a synchronous callback to
  181. the currentTabChanged() method. (But if the given tab is already selected,
  182. nothing will be done).
  183. To deselect all the tabs, use an index of -1.
  184. */
  185. void setCurrentTabIndex (int newTabIndex, bool sendChangeMessage = true);
  186. /** Returns the name of the currently selected tab.
  187. This could be an empty string if none are selected.
  188. */
  189. String getCurrentTabName() const;
  190. /** Returns the index of the currently selected tab.
  191. This could return -1 if none are selected.
  192. */
  193. int getCurrentTabIndex() const noexcept { return currentTabIndex; }
  194. /** Returns the button for a specific tab.
  195. The button that is returned may be deleted later by this component, so don't hang
  196. on to the pointer that is returned. A null pointer may be returned if the index is
  197. out of range.
  198. */
  199. TabBarButton* getTabButton (int index) const;
  200. /** Returns the index of a TabBarButton if it belongs to this bar. */
  201. int indexOfTabButton (const TabBarButton* button) const;
  202. /** Returns the final bounds of this button if it is currently being animated. */
  203. Rectangle<int> getTargetBounds (TabBarButton* button) const;
  204. //==============================================================================
  205. /** Callback method to indicate the selected tab has been changed.
  206. @see setCurrentTabIndex
  207. */
  208. virtual void currentTabChanged (int newCurrentTabIndex,
  209. const String& newCurrentTabName);
  210. /** Callback method to indicate that the user has right-clicked on a tab. */
  211. virtual void popupMenuClickOnTab (int tabIndex, const String& tabName);
  212. /** Returns the colour of a tab.
  213. This is the colour that was specified in addTab().
  214. */
  215. Colour getTabBackgroundColour (int tabIndex);
  216. /** Changes the background colour of a tab.
  217. @see addTab, getTabBackgroundColour
  218. */
  219. void setTabBackgroundColour (int tabIndex, Colour newColour);
  220. //==============================================================================
  221. /** A set of colour IDs to use to change the colour of various aspects of the component.
  222. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  223. methods.
  224. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  225. */
  226. enum ColourIds
  227. {
  228. tabOutlineColourId = 0x1005812, /**< The colour to use to draw an outline around the tabs. */
  229. tabTextColourId = 0x1005813, /**< The colour to use to draw the tab names. If this isn't specified,
  230. the look and feel will choose an appropriate colour. */
  231. frontOutlineColourId = 0x1005814, /**< The colour to use to draw an outline around the currently-selected tab. */
  232. frontTextColourId = 0x1005815, /**< The colour to use to draw the currently-selected tab name. If
  233. this isn't specified, the look and feel will choose an appropriate
  234. colour. */
  235. };
  236. //==============================================================================
  237. /** This abstract base class is implemented by LookAndFeel classes to provide
  238. window drawing functionality.
  239. */
  240. struct JUCE_API LookAndFeelMethods
  241. {
  242. virtual ~LookAndFeelMethods() = default;
  243. virtual int getTabButtonSpaceAroundImage() = 0;
  244. virtual int getTabButtonOverlap (int tabDepth) = 0;
  245. virtual int getTabButtonBestWidth (TabBarButton&, int tabDepth) = 0;
  246. virtual Rectangle<int> getTabButtonExtraComponentBounds (const TabBarButton&, Rectangle<int>& textArea, Component& extraComp) = 0;
  247. virtual void drawTabButton (TabBarButton&, Graphics&, bool isMouseOver, bool isMouseDown) = 0;
  248. virtual Font getTabButtonFont (TabBarButton&, float height) = 0;
  249. virtual void drawTabButtonText (TabBarButton&, Graphics&, bool isMouseOver, bool isMouseDown) = 0;
  250. virtual void drawTabbedButtonBarBackground (TabbedButtonBar&, Graphics&) = 0;
  251. virtual void drawTabAreaBehindFrontButton (TabbedButtonBar&, Graphics&, int w, int h) = 0;
  252. virtual void createTabButtonShape (TabBarButton&, Path& path, bool isMouseOver, bool isMouseDown) = 0;
  253. virtual void fillTabButtonShape (TabBarButton&, Graphics&, const Path& path, bool isMouseOver, bool isMouseDown) = 0;
  254. virtual Button* createTabBarExtrasButton() = 0;
  255. };
  256. //==============================================================================
  257. /** @internal */
  258. void paint (Graphics&) override;
  259. /** @internal */
  260. void resized() override;
  261. /** @internal */
  262. void lookAndFeelChanged() override;
  263. protected:
  264. //==============================================================================
  265. /** This creates one of the tabs.
  266. If you need to use custom tab components, you can override this method and
  267. return your own class instead of the default.
  268. */
  269. virtual TabBarButton* createTabButton (const String& tabName, int tabIndex);
  270. private:
  271. struct TabInfo
  272. {
  273. std::unique_ptr<TabBarButton> button;
  274. String name;
  275. Colour colour;
  276. };
  277. OwnedArray<TabInfo> tabs;
  278. Orientation orientation;
  279. double minimumScale = 0.7;
  280. int currentTabIndex = -1;
  281. class BehindFrontTabComp;
  282. std::unique_ptr<BehindFrontTabComp> behindFrontTab;
  283. std::unique_ptr<Button> extraTabsButton;
  284. void showExtraItemsMenu();
  285. void updateTabPositions (bool animate);
  286. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TabbedButtonBar)
  287. };
  288. } // namespace juce