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.

372 lines
14KB

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