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.

366 lines
14KB

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