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

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