iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IVTabbedPagesControl.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
19#include "IControls.h"
20#include "IBubbleControl.h"
21
22#include <utility>
23#include <vector>
24
25BEGIN_IPLUG_NAMESPACE
26BEGIN_IGRAPHICS_NAMESPACE
27
30class IVTabPage : public IContainerBase, public IVectorBase
31{
32public:
33 using TabAttachFunc = std::function<void(IVTabPage* pParent, const IRECT& bounds)>;
34 static constexpr double kDefaultPadding = 10.0;
35
36 static void DefaultResizeFunc(IContainerBase* pTab, const IRECT& r) {
37 if (pTab->NChildren() == 1)
38 {
39 auto innerBounds = r.GetPadded(float(- pTab->As<IVTabPage>()->GetPadding()));
40 pTab->GetChild(0)->SetTargetAndDrawRECTs(innerBounds);
41 }
42 }
43
51 IVTabPage(TabAttachFunc attachFunc = nullptr, ResizeFunc resizeFunc = DefaultResizeFunc, const IVStyle& style = DEFAULT_STYLE, double padding = kDefaultPadding)
52 : IContainerBase(IRECT(), [attachFunc](IContainerBase* pParent, const IRECT& bounds){
53 attachFunc(pParent->As<IVTabPage>(), bounds);
54 }, resizeFunc)
55 , IVectorBase(style)
56 , mPadding(padding)
57 {
58 AttachIControl(this, "");
59 }
60
61 virtual void Draw(IGraphics &g) override
62 {
63 /* NO-OP */
64 }
65
66 double GetPadding() const { return mPadding; }
67
68 void SetPadding(double padding) { mPadding = padding; }
69
70 void OnStyleChanged() override
71 {
72 ForAllChildrenFunc([this](int childIdx, IControl* pChild) {
73 if (auto pVectorBase = pChild->As<IVectorBase>())
74 {
75 pVectorBase->SetStyle(GetStyle());
76 }
77 });
78 }
79
80private:
81 double mPadding = 0.0;
82};
83
85using PageMap = std::vector<std::pair<const char*, IVTabPage*>>;
86
109{
110public:
111 IVTabbedPagesControl(const IRECT& bounds, const PageMap& pages, const char* label = "",
112 const IVStyle& style = DEFAULT_STYLE, float tabBarHeight = 20.0f,
113 float tabBarFrac = 0.5f, EAlign tabsAlign = EAlign::Near)
114 : IContainerBase(bounds)
115 , IVectorBase(style.WithDrawFrame(false).WithDrawShadows(false))
116 , mTabBarHeight(tabBarHeight)
117 , mTabBarFrac(tabBarFrac)
118 , mTabsAlign(tabsAlign)
119 {
120 AttachIControl(this, label);
121
122 for (auto& page : pages)
123 {
124 AddPage(page.first, page.second);
125 }
126 }
127
129 {
130 mPages.Empty(false);
131 }
132
133 void Hide(bool hide) override
134 {
135 if (hide)
136 {
137 ForAllChildrenFunc([hide](int childIdx, IControl* pChild) {
138 pChild->Hide(hide);
139 });
140 }
141 else
142 {
143 ForAllPagesFunc([&](IVTabPage* pPage) {
144 pPage->Hide(true);
145 });
146
147 // Show the tabs and first page
148 GetTabSwitchControl()->Hide(false);
149 GetPage(GetTabSwitchControl()->GetSelectedIdx())->Hide(false);
150 }
151
152 IControl::Hide(hide);
153 }
154
155 void Draw(IGraphics& g) override
156 {
157 DrawLabel(g);
158
159 auto rcr = GetRoundedCornerRadius(GetTabBarArea());
160
161 auto rcrForTabCorners = mTabBarFrac == 1.0f ? 0.0f : rcr;
162
163 g.FillRoundRect(GetColor(kPR), GetPageArea(), mTabsAlign == EAlign::Near ? 0.0f : rcrForTabCorners,
164 mTabsAlign == EAlign::Far ? 0.0f : rcrForTabCorners,
165 rcr, rcr);
166
167 if (mStyle.drawFrame)
168 g.DrawRoundRect(GetColor(kFR), mRECT, rcr);
169 }
170
171 void OnAttached() override
172 {
173 // Set up tabs
174 AddChildControl(new IVTabSwitchControl(GetTabBarArea(),
175 [&](IControl* pCaller) {
176 ShowSelectedPage();
177 }, mPageNames, "", GetStyle().WithWidgetFrac(1.0f)));
178
179 GetTabSwitchControl()->SetShape(EVShape::EndsRounded);
180
181 // Add all pages, set their bounds and hide them
182 ForAllPagesFunc([&](IVTabPage* pPage) {
183 AddChildControl(pPage);
184 pPage->SetTargetAndDrawRECTs(GetPageArea());
185 pPage->Hide(true);
186 });
187
188 // Show the tabs and first page
189 GetTabSwitchControl()->Hide(false);
190 GetPage(0)->Hide(false);
191 }
192
193 void OnStyleChanged() override
194 {
195 ForAllChildrenFunc([this](int childIdx, IControl* pChild) {
196 if (auto pVectorBase = pChild->As<IVectorBase>())
197 {
198 pVectorBase->SetStyle(GetStyle());
199 }
200 });
201
202 auto adjustedStyle = GetStyle().WithDrawFrame(false).WithDrawShadows(false);
203 GetTabSwitchControl()->SetStyle(adjustedStyle.WithWidgetFrac(1.0));
204 GetTabSwitchControl()->SetShape(EVShape::EndsRounded);
205 }
206
207 void OnResize() override
208 {
209 SetTargetRECT(MakeRects(mRECT));
210
211 if (NChildren())
212 {
213 GetTabSwitchControl()->SetTargetAndDrawRECTs(GetTabBarArea());
214
215 ForAllPagesFunc([&](IVTabPage* pPage) {
216 pPage->SetTargetAndDrawRECTs(GetPageArea());
217 });
218 }
219 }
220
221 float GetTabHeight() const { return mTabBarHeight; }
222
223 IRECT GetPageArea() const { return mWidgetBounds.GetReducedFromTop(GetTabHeight()); }
224
225 IRECT GetTabBarArea() const
226 {
227 return mWidgetBounds.GetFromTop(GetTabHeight()).FracRectHorizontal(mTabBarFrac, mTabsAlign == EAlign::Far);
228 }
229
230 int NPages() const { return mPages.GetSize(); }
231
232private:
233 void AddPage(const char* pageName, IVTabPage* pPage)
234 {
235 pPage->SetLabelStr(pageName);
236 mPageNames.push_back(pageName);
237 mPages.Add(pPage);
238 }
239
240 void ForAllPagesFunc(std::function<void(IVTabPage* pControl)> func)
241 {
242 for (int i=0; i<mPages.GetSize(); i++)
243 {
244 func(mPages.Get(i));
245 }
246 }
247
248 IVTabSwitchControl* GetTabSwitchControl() { return GetChild(0)->As<IVTabSwitchControl>(); }
249
250 IVTabPage* GetPage(int pageIdx) { return mPages.Get(pageIdx); }
251
252 void SelectPage(int index)
253 {
254 GetTabSwitchControl()->SetValue(static_cast<double>(index));
255 ShowSelectedPage();
256 }
257
258private:
259 void ShowSelectedPage()
260 {
261 ForAllPagesFunc([&](IVTabPage* pPage) {
262 bool hide = strcmp(GetTabSwitchControl()->GetSelectedLabelStr(),
263 pPage->GetLabelStr());
264 pPage->Hide(hide);
265 });
266
267 if (IBubbleControl* pControl = GetUI()->GetBubbleControl())
268 {
269 pControl->Hide(true);
270 }
271 }
272
273 WDL_PtrList<IVTabPage> mPages;
274 std::vector<const char*> mPageNames;
275 float mTabBarHeight;
276 float mTabBarFrac;
277 EAlign mTabsAlign;
278};
279
280END_IGRAPHICS_NAMESPACE
281END_IPLUG_NAMESPACE
A collection of IControls for common UI widgets, such as knobs, sliders, switches.
std::vector< std::pair< const char *, IVTabPage * > > PageMap
Ordered list of (tab name, page) pairs.
A special control to draw contextual info as a slider etc is moved If used in the main IControl stack...
IContainerBase allows a control to nest sub controls and it clips the drawing of those subcontrols In...
Definition: IControl.h:611
void Hide(bool hide) override
Shows or hides the IControl.
Definition: IControl.h:679
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
IGraphics * GetUI()
Definition: IControl.h:472
virtual void Hide(bool hide)
Shows or hides the IControl.
Definition: IControl.cpp:239
void SetTargetRECT(const IRECT &bounds)
Set the rectangular mouse tracking target area, within the graphics context for this control.
Definition: IControl.h:328
T * As()
Helper function to dynamic cast an IControl to a subclass.
Definition: IControl.h:518
virtual void SetValue(double value, int valIdx=0)
Set one of the control's values.
Definition: IControl.cpp:147
void SetTargetAndDrawRECTs(const IRECT &bounds)
Set BOTH the draw rect and the target area, within the graphics context for this control.
Definition: IControl.h:332
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
virtual void DrawRoundRect(const IColor &color, const IRECT &bounds, float cornerRadius=5.f, const IBlend *pBlend=0, float thickness=1.f)
Draw a rounded rectangle to the graphics context.
Definition: IGraphics.cpp:2524
IBubbleControl * GetBubbleControl(int i=0)
Definition: IGraphics.h:1449
virtual void FillRoundRect(const IColor &color, const IRECT &bounds, float cornerRadius=5.f, const IBlend *pBlend=0)
Fill a rounded rectangle with a color.
Definition: IGraphics.cpp:2596
A control used as the base class for a tabbed page of subcontrols.
void OnStyleChanged() override
Implement if extra changes are required in response to style changing.
virtual void Draw(IGraphics &g) override
Draw the control to the graphics context.
IVTabPage(TabAttachFunc attachFunc=nullptr, ResizeFunc resizeFunc=DefaultResizeFunc, const IVStyle &style=DEFAULT_STYLE, double padding=kDefaultPadding)
Constructor.
A vector "tab" multi switch control.
Definition: IControls.h:148
A control to manage tabbed pages of sub controls Basic usage example:
void OnResize() override
Called when IControl is constructed or resized using SetRect().
void OnAttached() override
Called after the control has been attached, and its delegate and graphics member variable set.
void Hide(bool hide) override
Shows or hides the IControl.
void OnStyleChanged() override
Implement if extra changes are required in response to style changing.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
A base interface to be combined with IControl for vectorial controls "IVControls",...
Definition: IControl.h:762
IRECT MakeRects(const IRECT &parent, bool hasHandle=false)
Calculate the rectangles for the various areas, depending on the style.
Definition: IControl.h:1163
virtual void SetStyle(const IVStyle &style)
Set the Style of this IVControl.
Definition: IControl.h:830
float GetRoundedCornerRadius(const IRECT &bounds) const
Get the radius of rounded corners for a rectangle, based on the style roundness factor.
Definition: IControl.h:858
void AttachIControl(IControl *pControl, const char *label)
Call in the constructor of your IVControl to link the IVectorBase and IControl.
Definition: IControl.h:780
virtual void DrawLabel(IGraphics &g)
Draw the IVControl label text.
Definition: IControl.h:894
const IColor & GetColor(EVColor color) const
Get value of a specific EVColor in the IVControl.
Definition: IControl.h:806
IVStyle GetStyle() const
Get the style of this IVControl.
Definition: IControl.h:839
Used to manage a rectangular area, independent of draw class/platform.
IRECT GetReducedFromTop(float amount) const
Get a subrect of this IRECT reduced in height from the top edge by 'amount'.
IRECT FracRectHorizontal(float frac, bool rhs=false) const
Returns a new IRECT with a width that is multiplied by frac.
IRECT GetFromTop(float amount) const
Get a subrect of this IRECT bounded in Y by the top edge and 'amount'.
IRECT GetPadded(float padding) const
Get a copy of this IRECT with each value padded by padding N.B.
A struct encapsulating a set of properties used to configure IVControls.