Audio plugin host https://kx.studio/carla
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.

juce_TabbedButtonBar.h 14KB

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