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.

352 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_TABBEDBUTTONBAR_JUCEHEADER__
  19. #define __JUCE_TABBEDBUTTONBAR_JUCEHEADER__
  20. #include "../buttons/juce_Button.h"
  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. void paintButton (Graphics&, bool isMouseOverButton, bool isButtonDown);
  81. void clicked (const ModifierKeys&);
  82. bool hitTest (int x, int y);
  83. void resized();
  84. void childBoundsChanged (Component*);
  85. protected:
  86. friend class TabbedButtonBar;
  87. TabbedButtonBar& owner;
  88. int overlapPixels;
  89. ScopedPointer<Component> extraComponent;
  90. ExtraComponentPlacement extraCompPlacement;
  91. private:
  92. void calcAreas (Rectangle<int>&, Rectangle<int>&) const;
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TabBarButton);
  94. };
  95. //==============================================================================
  96. /**
  97. A vertical or horizontal bar containing tabs that you can select.
  98. You can use one of these to generate things like a dialog box that has
  99. tabbed pages you can flip between. Attach a ChangeListener to the
  100. button bar to be told when the user changes the page.
  101. An easier method than doing this is to use a TabbedComponent, which
  102. contains its own TabbedButtonBar and which takes care of the layout
  103. and other housekeeping.
  104. @see TabbedComponent
  105. */
  106. class JUCE_API TabbedButtonBar : public Component,
  107. public ChangeBroadcaster
  108. {
  109. public:
  110. //==============================================================================
  111. /** The placement of the tab-bar
  112. @see setOrientation, getOrientation
  113. */
  114. enum Orientation
  115. {
  116. TabsAtTop,
  117. TabsAtBottom,
  118. TabsAtLeft,
  119. TabsAtRight
  120. };
  121. //==============================================================================
  122. /** Creates a TabbedButtonBar with a given placement.
  123. You can change the orientation later if you need to.
  124. */
  125. TabbedButtonBar (Orientation orientation);
  126. /** Destructor. */
  127. ~TabbedButtonBar();
  128. //==============================================================================
  129. /** Changes the bar's orientation.
  130. This won't change the bar's actual size - you'll need to do that yourself,
  131. but this determines which direction the tabs go in, and which side they're
  132. stuck to.
  133. */
  134. void setOrientation (Orientation orientation);
  135. /** Returns the bar's current orientation.
  136. @see setOrientation
  137. */
  138. Orientation getOrientation() const noexcept { return orientation; }
  139. /** Returns true if the orientation is TabsAtLeft or TabsAtRight. */
  140. bool isVertical() const noexcept { return orientation == TabsAtLeft || orientation == TabsAtRight; }
  141. /** Returns the thickness of the bar, which may be its width or height, depending on the orientation. */
  142. int getThickness() const noexcept { return isVertical() ? getWidth() : getHeight(); }
  143. /** Changes the minimum scale factor to which the tabs can be compressed when trying to
  144. fit a lot of tabs on-screen.
  145. */
  146. void setMinimumTabScaleFactor (double newMinimumScale);
  147. //==============================================================================
  148. /** Deletes all the tabs from the bar.
  149. @see addTab
  150. */
  151. void clearTabs();
  152. /** Adds a tab to the bar.
  153. Tabs are added in left-to-right reading order.
  154. If this is the first tab added, it'll also be automatically selected.
  155. */
  156. void addTab (const String& tabName,
  157. const Colour& tabBackgroundColour,
  158. int insertIndex);
  159. /** Changes the name of one of the tabs. */
  160. void setTabName (int tabIndex,
  161. const String& newName);
  162. /** Gets rid of one of the tabs. */
  163. void removeTab (int tabIndex);
  164. /** Moves a tab to a new index in the list.
  165. Pass -1 as the index to move it to the end of the list.
  166. */
  167. void moveTab (int currentIndex, int newIndex);
  168. /** Returns the number of tabs in the bar. */
  169. int getNumTabs() const;
  170. /** Returns a list of all the tab names in the bar. */
  171. StringArray getTabNames() const;
  172. /** Changes the currently selected tab.
  173. This will send a change message and cause a synchronous callback to
  174. the currentTabChanged() method. (But if the given tab is already selected,
  175. nothing will be done).
  176. To deselect all the tabs, use an index of -1.
  177. */
  178. void setCurrentTabIndex (int newTabIndex, bool sendChangeMessage = true);
  179. /** Returns the name of the currently selected tab.
  180. This could be an empty string if none are selected.
  181. */
  182. String getCurrentTabName() const;
  183. /** Returns the index of the currently selected tab.
  184. This could return -1 if none are selected.
  185. */
  186. int getCurrentTabIndex() const noexcept { return currentTabIndex; }
  187. /** Returns the button for a specific tab.
  188. The button that is returned may be deleted later by this component, so don't hang
  189. on to the pointer that is returned. A null pointer may be returned if the index is
  190. out of range.
  191. */
  192. TabBarButton* getTabButton (int index) const;
  193. /** Returns the index of a TabBarButton if it belongs to this bar. */
  194. int indexOfTabButton (const TabBarButton* button) const;
  195. //==============================================================================
  196. /** Callback method to indicate the selected tab has been changed.
  197. @see setCurrentTabIndex
  198. */
  199. virtual void currentTabChanged (int newCurrentTabIndex,
  200. const String& newCurrentTabName);
  201. /** Callback method to indicate that the user has right-clicked on a tab.
  202. (Or ctrl-clicked on the Mac)
  203. */
  204. virtual void popupMenuClickOnTab (int tabIndex, const String& tabName);
  205. /** Returns the colour of a tab.
  206. This is the colour that was specified in addTab().
  207. */
  208. Colour getTabBackgroundColour (int tabIndex);
  209. /** Changes the background colour of a tab.
  210. @see addTab, getTabBackgroundColour
  211. */
  212. void setTabBackgroundColour (int tabIndex, const Colour& newColour);
  213. //==============================================================================
  214. /** A set of colour IDs to use to change the colour of various aspects of the component.
  215. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  216. methods.
  217. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  218. */
  219. enum ColourIds
  220. {
  221. tabOutlineColourId = 0x1005812, /**< The colour to use to draw an outline around the tabs. */
  222. tabTextColourId = 0x1005813, /**< The colour to use to draw the tab names. If this isn't specified,
  223. the look and feel will choose an appropriate colour. */
  224. frontOutlineColourId = 0x1005814, /**< The colour to use to draw an outline around the currently-selected tab. */
  225. frontTextColourId = 0x1005815, /**< The colour to use to draw the currently-selected tab name. If
  226. this isn't specified, the look and feel will choose an appropriate
  227. colour. */
  228. };
  229. //==============================================================================
  230. /** @internal */
  231. void resized();
  232. /** @internal */
  233. void lookAndFeelChanged();
  234. protected:
  235. //==============================================================================
  236. /** This creates one of the tabs.
  237. If you need to use custom tab components, you can override this method and
  238. return your own class instead of the default.
  239. */
  240. virtual TabBarButton* createTabButton (const String& tabName, int tabIndex);
  241. private:
  242. Orientation orientation;
  243. struct TabInfo
  244. {
  245. ScopedPointer<TabBarButton> button;
  246. String name;
  247. Colour colour;
  248. };
  249. OwnedArray <TabInfo> tabs;
  250. double minimumScale;
  251. int currentTabIndex;
  252. class BehindFrontTabComp;
  253. friend class BehindFrontTabComp;
  254. friend class ScopedPointer<BehindFrontTabComp>;
  255. ScopedPointer<BehindFrontTabComp> behindFrontTab;
  256. ScopedPointer<Button> extraTabsButton;
  257. void showExtraItemsMenu();
  258. static void extraItemsMenuCallback (int, TabbedButtonBar*);
  259. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TabbedButtonBar);
  260. };
  261. #endif // __JUCE_TABBEDBUTTONBAR_JUCEHEADER__