JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_TabbedComponent.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29namespace TabbedComponentHelpers
30{
31 const Identifier deleteComponentId ("deleteByTabComp_");
32
33 static void deleteIfNecessary (Component* comp)
34 {
35 if (comp != nullptr && (bool) comp->getProperties() [deleteComponentId])
36 delete comp;
37 }
38
39 static Rectangle<int> getTabArea (Rectangle<int>& content, BorderSize<int>& outline,
40 TabbedButtonBar::Orientation orientation, int tabDepth)
41 {
42 switch (orientation)
43 {
44 case TabbedButtonBar::TabsAtTop: outline.setTop (0); return content.removeFromTop (tabDepth);
45 case TabbedButtonBar::TabsAtBottom: outline.setBottom (0); return content.removeFromBottom (tabDepth);
46 case TabbedButtonBar::TabsAtLeft: outline.setLeft (0); return content.removeFromLeft (tabDepth);
47 case TabbedButtonBar::TabsAtRight: outline.setRight (0); return content.removeFromRight (tabDepth);
48 default: jassertfalse; break;
49 }
50
51 return Rectangle<int>();
52 }
53}
54
55//==============================================================================
57{
59 : TabbedButtonBar (o), owner (tabComp)
60 {
61 }
62
64 {
65 owner.changeCallback (newCurrentTabIndex, newTabName);
66 }
67
68 void popupMenuClickOnTab (int tabIndex, const String& tabName) override
69 {
71 }
72
73 Colour getTabBackgroundColour (int tabIndex)
74 {
75 return owner.tabs->getTabBackgroundColour (tabIndex);
76 }
77
79 {
80 return owner.createTabButton (tabName, tabIndex);
81 }
82
83 TabbedComponent& owner;
84
86};
87
88//==============================================================================
90{
91 tabs.reset (new ButtonBar (*this, orientation));
92 addAndMakeVisible (tabs.get());
93}
94
96{
97 clearTabs();
98 tabs.reset();
99}
100
101//==============================================================================
103{
104 tabs->setOrientation (orientation);
105 resized();
106}
107
109{
110 return tabs->getOrientation();
111}
112
114{
115 if (tabDepth != newDepth)
116 {
117 tabDepth = newDepth;
118 resized();
119 }
120}
121
123{
124 return new TabBarButton (tabName, *tabs);
125}
126
127//==============================================================================
129{
130 if (panelComponent != nullptr)
131 {
132 panelComponent->setVisible (false);
133 removeChildComponent (panelComponent.get());
134 panelComponent = nullptr;
135 }
136
137 tabs->clearTabs();
138
139 for (int i = contentComponents.size(); --i >= 0;)
140 TabbedComponentHelpers::deleteIfNecessary (contentComponents.getReference (i));
141
142 contentComponents.clear();
143}
144
147 Component* contentComponent,
149 int insertIndex)
150{
151 contentComponents.insert (insertIndex, WeakReference<Component> (contentComponent));
152
153 if (deleteComponentWhenNotNeeded && contentComponent != nullptr)
154 contentComponent->getProperties().set (TabbedComponentHelpers::deleteComponentId, true);
155
156 tabs->addTab (tabName, tabBackgroundColour, insertIndex);
157 resized();
158}
159
161{
162 tabs->setTabName (tabIndex, newName);
163}
164
166{
167 if (isPositiveAndBelow (tabIndex, contentComponents.size()))
168 {
169 TabbedComponentHelpers::deleteIfNecessary (contentComponents.getReference (tabIndex).get());
170 contentComponents.remove (tabIndex);
171 tabs->removeTab (tabIndex);
172 }
173}
174
176{
177 contentComponents.move (currentIndex, newIndex);
178 tabs->moveTab (currentIndex, newIndex, animate);
179}
180
182{
183 return tabs->getNumTabs();
184}
185
187{
188 return tabs->getTabNames();
189}
190
192{
193 return contentComponents[tabIndex].get();
194}
195
197{
198 return tabs->getTabBackgroundColour (tabIndex);
199}
200
202{
203 tabs->setTabBackgroundColour (tabIndex, newColour);
204
206 repaint();
207}
208
209void TabbedComponent::setCurrentTabIndex (int newTabIndex, bool sendChangeMessage)
210{
211 tabs->setCurrentTabIndex (newTabIndex, sendChangeMessage);
212}
213
215{
216 return tabs->getCurrentTabIndex();
217}
218
220{
221 return tabs->getCurrentTabName();
222}
223
224void TabbedComponent::setOutline (int thickness)
225{
226 outlineThickness = thickness;
227 resized();
228 repaint();
229}
230
232{
233 edgeIndent = indentThickness;
234 resized();
235 repaint();
236}
237
239{
241
242 auto content = getLocalBounds();
243 BorderSize<int> outline (outlineThickness);
244 TabbedComponentHelpers::getTabArea (content, outline, getOrientation(), tabDepth);
245
246 g.reduceClipRegion (content);
247 g.fillAll (tabs->getTabBackgroundColour (getCurrentTabIndex()));
248
249 if (outlineThickness > 0)
250 {
251 RectangleList<int> rl (content);
252 rl.subtract (outline.subtractedFrom (content));
253
256 }
257}
258
260{
261 auto content = getLocalBounds();
262 BorderSize<int> outline (outlineThickness);
263
264 tabs->setBounds (TabbedComponentHelpers::getTabArea (content, outline, getOrientation(), tabDepth));
265 content = BorderSize<int> (edgeIndent).subtractedFrom (outline.subtractedFrom (content));
266
267 for (auto& c : contentComponents)
268 if (auto comp = c.get())
269 comp->setBounds (content);
270}
271
273{
274 for (auto& c : contentComponents)
275 if (auto comp = c.get())
276 comp->lookAndFeelChanged();
277}
278
279void TabbedComponent::changeCallback (int newCurrentTabIndex, const String& newTabName)
280{
282
283 if (newPanelComp != panelComponent)
284 {
285 if (panelComponent != nullptr)
286 {
287 panelComponent->setVisible (false);
288 removeChildComponent (panelComponent);
289 }
290
291 panelComponent = newPanelComp;
292
293 if (panelComponent != nullptr)
294 {
295 // do these ops as two stages instead of addAndMakeVisible() so that the
296 // component has always got a parent when it gets the visibilityChanged() callback
297 addChildComponent (panelComponent);
298 panelComponent->sendLookAndFeelChange();
299 panelComponent->setVisible (true);
300 panelComponent->toFront (true);
301 }
302
303 repaint();
304 }
305
306 resized();
308}
309
312
313//==============================================================================
315{
316 return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::group);
317}
318
319} // namespace juce
Specifies a set of gaps to be left around the sides of a rectangle.
Rectangle< ValueType > subtractedFrom(const Rectangle< ValueType > &original) const noexcept
Returns a rectangle with these borders removed from it.
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
The base class for all JUCE user-interface objects.
void addAndMakeVisible(Component *child, int zOrder=-1)
Adds a child component to this one, and also makes the child visible if it isn't already.
void repaint()
Marks the whole component as needing to be redrawn.
NamedValueSet & getProperties() noexcept
Returns the set of properties that belong to this component.
void removeChildComponent(Component *childToRemove)
Removes one of this component's child-components.
Colour findColour(int colourID, bool inheritFromParent=false) const
Looks for a colour that has been registered with the given colour ID number.
Rectangle< int > getLocalBounds() const noexcept
Returns the component's bounds, relative to its own origin.
void addChildComponent(Component *child, int zOrder=-1)
Adds a child component to this one.
A graphics context, used for drawing a component or image.
bool reduceClipRegion(int x, int y, int width, int height)
Intersects the current clipping region with another region.
void fillAll() const
Fills the context's entire clip region with the current colour or brush.
bool set(const Identifier &name, const var &newValue)
Changes or adds a named value.
Maintains a set of rectangles as a complex region.
void setTop(ValueType newTop) noexcept
Moves the y position, adjusting the height so that the bottom edge remains in the same place.
A special array for holding a list of strings.
The JUCE String class!
Definition juce_String.h:53
In a TabbedButtonBar, this component is used for each of the buttons.
A vertical or horizontal bar containing tabs that you can select.
Orientation
The placement of the tab-bar.
A component with a TabbedButtonBar along one of its sides.
void clearTabs()
Removes all the tabs from the bar.
void addTab(const String &tabName, Colour tabBackgroundColour, Component *contentComponent, bool deleteComponentWhenNotNeeded, int insertIndex=-1)
Adds a tab to the tab-bar.
void paint(Graphics &) override
Components can override this method to draw their content.
~TabbedComponent() override
Destructor.
virtual TabBarButton * createTabButton(const String &tabName, int tabIndex)
This creates one of the tab buttons.
int getCurrentTabIndex() const
Returns the index of the currently selected tab.
void setTabBarDepth(int newDepth)
Specifies how many pixels wide or high the tab-bar should be.
void setOutline(int newThickness)
Specifies the thickness of an outline that should be drawn around the content component.
@ outlineColourId
The colour to use to draw an outline around the content.
@ backgroundColourId
The colour to fill the background behind the tabs.
int getNumTabs() const
Returns the number of tabs in the bar.
virtual void popupMenuClickOnTab(int tabIndex, const String &tabName)
Callback method to indicate that the user has right-clicked on a tab.
virtual void currentTabChanged(int newCurrentTabIndex, const String &newCurrentTabName)
Callback method to indicate the selected tab has been changed.
void removeTab(int tabIndex)
Gets rid of one of the tabs.
void resized() override
Called when this component's size has been changed.
void setCurrentTabIndex(int newTabIndex, bool sendChangeMessage=true)
Changes the currently-selected tab.
void setTabBackgroundColour(int tabIndex, Colour newColour)
Changes the background colour of one of the tabs.
TabbedComponent(TabbedButtonBar::Orientation orientation)
Creates a TabbedComponent, specifying where the tabs should be placed.
Colour getTabBackgroundColour(int tabIndex) const noexcept
Returns the colour of one of the tabs.
void setTabName(int tabIndex, const String &newName)
Changes the name of one of the tabs.
void setIndent(int indentThickness)
Specifies a gap to leave around the edge of the content component.
void setOrientation(TabbedButtonBar::Orientation orientation)
Changes the placement of the tabs.
TabbedButtonBar::Orientation getOrientation() const noexcept
Returns the current tab placement.
StringArray getTabNames() const
Returns a list of all the tab names in the bar.
void lookAndFeelChanged() override
Called to let the component react to a change in the look-and-feel setting.
Component * getTabContentComponent(int tabIndex) const noexcept
Returns the content component that was added for the given index.
std::unique_ptr< AccessibilityHandler > createAccessibilityHandler() override
Override this method to return a custom AccessibilityHandler for this component.
String getCurrentTabName() const
Returns the name of the currently selected tab.
void moveTab(int currentIndex, int newIndex, bool animate=false)
Moves a tab to a new index in the list.
This class acts as a pointer which will automatically become null if the object to which it points is...
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
#define jassertfalse
This will always cause an assertion failure.
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Returns true if a value is at least zero, and also below a specified upper limit.
void currentTabChanged(int newCurrentTabIndex, const String &newTabName) override
Callback method to indicate the selected tab has been changed.
TabBarButton * createTabButton(const String &tabName, int tabIndex) override
This creates one of the tabs.
void popupMenuClickOnTab(int tabIndex, const String &tabName) override
Callback method to indicate that the user has right-clicked on a tab.