iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
TestPopupMenuControl.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
18#include "IControl.h"
19#include "IControls.h"
20#include "IPopupMenuControl.h"
21
28{
29public:
30 TestPopupMenuControl(const IRECT& bounds)
31 : IContainerBase(bounds)
32 {
33 SetTooltip("TestPopupMenuControl");
34 mIgnoreMouse = false;
35
36 // A list of options, one of which is ticked. The menu asks for two items per column
37 for (const char* str : {"Sine", "Triangle", "Sawtooth", "Square", "Noise", "Sample"})
38 mSimpleMenu.AddItem(str);
39
40 mSimpleMenu.SetNItemsPerColumn(2);
41
42 mSimpleMenu.CheckItemAlone(1);
43
44 // Sub-menus two levels deep, a title, separators, a disabled item, and a sub-menu where any number of items can be ticked
45 IPopupMenu* pMoreBassMenu = new IPopupMenu("More Bass", {"Reese", "Wobble", "FM Bass"});
46 IPopupMenu* pBassMenu = new IPopupMenu("Bass", {"Sub", "Acid", "Pluck"});
47 pBassMenu->AddSeparator();
48 pBassMenu->AddItem("More", pMoreBassMenu);
49 IPopupMenu* pPadsMenu = new IPopupMenu("Pads", {"Warm", "Glass", "Choir", "Strings"});
50 IPopupMenu* pOptionsMenu = new IPopupMenu("Options", 0, true, {"Oversample", "Limiter", "Mono"});
51 pOptionsMenu->CheckItem(1, true);
52 IPopupMenu* pEmptyMenu = new IPopupMenu("User");
53 pEmptyMenu->AddItem("No presets", -1, IPopupMenu::Item::kDisabled);
54
55 mSubMenusMenu.AddItem("Presets", -1, IPopupMenu::Item::kTitle);
56 mSubMenusMenu.AddItem("Init Patch");
57 mSubMenusMenu.AddItem("Bass", pBassMenu);
58 mSubMenusMenu.AddItem("Pads", pPadsMenu);
59 mSubMenusMenu.AddItem("User", pEmptyMenu);
60 mSubMenusMenu.AddSeparator();
61 mSubMenusMenu.AddItem("Options", pOptionsMenu);
62 mSubMenusMenu.AddSeparator();
63 mSubMenusMenu.AddItem("Paste", -1, IPopupMenu::Item::kDisabled);
64 mSubMenusMenu.AddItem("Save Preset As...");
65
66 // More items than fit, so that the menu scrolls, numbered with a prefix
67 for (int i = 0; i < 200; i++)
68 {
69 WDL_String str;
70 str.SetFormatted(32, "Preset %c%i", 'A' + (i / 25), (i % 25) + 1);
71 mLongMenu.AddItem(str.Get());
72 }
73
74 mLongMenu.SetPrefix(3);
75 mLongMenu.CheckItemAlone(120);
76
77 // Wide items, in a corner where there is no room for them beside the button
78 mWideMenu.AddItem("A menu item with a rather long piece of text in it");
79 mWideMenu.AddItem("Short");
80 mWideMenu.AddItem("Sub-menu that has to open on the left", new IPopupMenu("", {"One", "Two", "Three with some more text"}));
81
82 for (const char* str : {"Cut", "Copy", "Paste"})
83 mContextMenu.AddItem(str);
84
85 mContextMenu.AddSeparator();
86 mContextMenu.AddItem("Select All");
87 }
88
90 {
91 // The other tests expect platform menus
92 if (GetUI())
94 }
95
96 void OnAttached() override
97 {
98 const IVStyle style = DEFAULT_STYLE.WithLabelText(IText(14.f, EVAlign::Middle));
99 const IVStyle toggleStyle = DEFAULT_STYLE.WithLabelText(IText(12.f)).WithValueText(IText(12.f));
100
101 auto addMenuButton = [&](const char* str, IPopupMenu& menu) {
102 AddChildControl(new IVButtonControl(IRECT(), [this, &menu](IControl* pCaller) {
103 SplashClickActionFunc(pCaller);
104 GetUI()->CreatePopupMenu(*this, menu, pCaller->GetRECT());
105 }, str, style, true, false));
106 };
107
108 auto addToggle = [&](const char* str, bool& option) {
109 AddChildControl(new IVToggleControl(IRECT(), [this, &option](IControl* pCaller) {
110 option = pCaller->GetValue() > 0.5;
111 ApplyOptions();
112 }, str, toggleStyle, "OFF", "ON", option));
113 };
114
115 // N.B. OnResize() relies on this order
116 addMenuButton("Simple", mSimpleMenu);
117 addMenuButton("Sub-menus", mSubMenusMenu);
118 addMenuButton("Long, numbered", mLongMenu);
119 addMenuButton("Wide", mWideMenu);
120 addToggle("Callout", mCallout);
121 addToggle("Beside", mBeside);
122 addToggle("Dark", mDark);
123 addToggle("Large font", mLargeFont);
124 addToggle("Columns", mColumns);
125 addToggle("Platform", mPlatform);
126
127 ApplyOptions();
128 OnResize();
129 }
130
131 void OnResize() override
132 {
133 if (NChildren() < kNumChildren)
134 return;
135
136 const IRECT bounds = mRECT.GetPadded(-10.f);
137 const float w = 130.f;
138 const float h = 30.f;
139
140 GetChild(0)->SetTargetAndDrawRECTs(bounds.GetFromTLHC(w, h));
141 GetChild(1)->SetTargetAndDrawRECTs(bounds.GetFromTRHC(w, h));
142 GetChild(2)->SetTargetAndDrawRECTs(bounds.GetFromBLHC(w, h));
143 GetChild(3)->SetTargetAndDrawRECTs(bounds.GetFromBRHC(w, h));
144
145 const int nToggles = kNumChildren - 4;
146 const IRECT toggles = bounds.GetCentredInside(nToggles * 90.f, 50.f);
147
148 for (int i = 0; i < nToggles; i++)
149 GetChild(4 + i)->SetTargetAndDrawRECTs(toggles.GetGridCell(0, i, 1, nToggles).GetPadded(-5.f));
150
151 mResultRect = toggles.GetVShifted(-60.f);
152 mHintRect = toggles.GetVShifted(60.f);
153 }
154
155 void Draw(IGraphics& g) override
156 {
157 g.DrawDottedRect(COLOR_BLACK, mRECT);
158 g.DrawText(IText(16.f), mResult.Get(), mResultRect);
159 g.DrawText(IText(12.f, COLOR_DARK_GRAY), "Right-click the background for a context menu. Tab reaches the buttons, to try the keyboard.", mHintRect);
160 }
161
162 void OnMouseDown(float x, float y, const IMouseMod& mod) override
163 {
164 if (mod.R)
165 GetUI()->CreatePopupMenu(*this, mContextMenu, x, y);
166 }
167
168 void OnPopupMenuSelection(IPopupMenu* pMenu, int valIdx) override
169 {
170 IPopupMenu::Item* pItem = pMenu ? pMenu->GetChosenItem() : nullptr;
171
172 if (pItem)
173 {
174 if (pMenu->GetCanMultiCheck())
175 pItem->SetChecked(!pItem->GetChecked());
176 else if (pMenu == &mSimpleMenu || pMenu == &mLongMenu)
177 pMenu->CheckItemAlone(pMenu->GetChosenItemIdx());
178
179 mResult.SetFormatted(256, "Chose \"%s\" (item %i of \"%s\")", pItem->GetText(), pMenu->GetChosenItemIdx(), pMenu->GetRootTitle());
180 }
181 else
182 mResult.Set("Dismissed");
183
184 SetDirty(false);
185 }
186
187private:
189 void ApplyOptions()
190 {
191 IGraphics* pUI = GetUI();
192
193 // N.B. the control is made with its text, so changing the font means making a new one
195
196 if (mPlatform)
197 return;
198
199 pUI->AttachPopupMenuControl(mLargeFont ? IText(22.f) : DEFAULT_TEXT);
200
201 IPopupMenuControl* pMenuControl = pUI->GetPopupMenuControl();
202 pMenuControl->SetCallout(mCallout);
203 pMenuControl->SetMenuForcedSouth(!mBeside);
204 pMenuControl->SetWrapIntoColumns(mColumns);
205
206 if (mDark)
207 {
208 pMenuControl->SetPanelColor(IColor(255, 40, 40, 44));
209 pMenuControl->SetPanelBorderColor(IColor(60, 255, 255, 255));
210 pMenuControl->SetItemColor(IColor(255, 235, 235, 235));
211 pMenuControl->SetDisabledItemColor(IColor(255, 120, 120, 125));
212 pMenuControl->SetSeparatorColor(IColor(40, 255, 255, 255));
213 pMenuControl->SetCellBackgroundColor(IColor(255, 10, 132, 255));
214 }
215 }
216
217 enum { kNumChildren = 10 };
218
219 IPopupMenu mSimpleMenu {"Waveform"};
220 IPopupMenu mSubMenusMenu {"Presets"};
221 IPopupMenu mLongMenu {"All Presets"};
222 IPopupMenu mWideMenu {"Wide"};
223 IPopupMenu mContextMenu {"Edit"};
224 WDL_String mResult {"Nothing chosen yet"};
225 IRECT mResultRect;
226 IRECT mHintRect;
227 bool mCallout = false;
228 bool mBeside = false;
229 bool mDark = false;
230 bool mLargeFont = false;
231 bool mColumns = false; // menus that are too tall continue in another column, instead of scrolling
232 bool mPlatform = false;
233};
This file contains the base IControl implementation, along with some base classes for specific types ...
A collection of IControls for common UI widgets, such as knobs, sliders, switches.
IContainerBase allows a control to nest sub controls and it clips the drawing of those subcontrols In...
Definition: IControl.h:662
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
IGraphics * GetUI()
Definition: IControl.h:472
const IRECT & GetRECT() const
Get the rectangular draw area for this control, within the graphics context.
Definition: IControl.h:316
IControl * SetTooltip(const char *str)
Set a tooltip for the control.
Definition: IControl.h:221
double GetValue(int valIdx=0) const
Get the control's value.
Definition: IControl.cpp:153
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
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:198
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:87
void AttachPopupMenuControl(const IText &text=DEFAULT_TEXT, const IRECT &bounds=IRECT())
Attach a control for pop-up menus, to override platform style menus.
Definition: IGraphics.cpp:404
void DrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend=0)
Draw some text to the graphics context in a specific rectangle.
Definition: IGraphics.cpp:732
void CreatePopupMenu(IControl &control, IPopupMenu &menu, const IRECT &bounds, int valIdx=0)
Shows a pop up/contextual menu in relation to a rectangular region of the graphics context.
Definition: IGraphics.cpp:2318
virtual void DrawDottedRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f, float dashLen=2.f)
Draw a dotted rectangle to the graphics context.
Definition: IGraphics.cpp:2899
void RemovePopupMenuControl()
Remove the IGraphics popup menu, use platform popup menu if available.
Definition: IGraphics.cpp:413
IPopupMenuControl * GetPopupMenuControl()
Definition: IGraphics.h:1485
A class to specify an item of a pop up menu.
A base control for a pop-up menu/drop-down list that stays within the bounds of the IGraphics context...
void SetSeparatorColor(IColor color)
Call this to set the Separator color on menu panels.
void SetMenuForcedSouth(bool isForcedSouth)
If set true (the default), the menu appears below the area it was opened for, if there is room for it...
void SetItemColor(IColor color)
Call this to set the color of enabled text items, ticks and arrows on menu panels.
void SetCellBackgroundColor(IColor color)
Call this to set the Cell color when mouse is over it.
void SetCallout(bool callout)
Set if the menu is shifted away from where the control is created with a callout arrow (for fat finge...
void SetPanelBorderColor(IColor color)
Call this to set the color of the outline of the panels.
void SetWrapIntoColumns(bool wrap)
Set what happens to a menu that is too tall to fit.
void SetDisabledItemColor(IColor color)
Call this to set the text color of disabled items and titles on menu panels.
void SetPanelColor(IColor color)
Call this to set the Panel color.
A class for setting the contents of a pop up menu.
A vector button/momentary switch control.
Definition: IControls.h:54
A vector toggle control.
Definition: IControls.h:92
Control to test IPopupMenuControl, the pop-up menu that is drawn by IGraphics.
void OnAttached() override
Called after the control has been attached, and its delegate and graphics member variable set.
void OnResize() override
Called when IControl is constructed or resized using SetRect().
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
void OnPopupMenuSelection(IPopupMenu *pMenu, int valIdx) override
Implement this method to handle popup menu selection after IGraphics::CreatePopupMenu/IControlPromptU...
void SplashClickActionFunc(IControl *pCaller)
The splash click action function is used by IVControls to start SplashAnimationFunc.
Definition: IControl.cpp:47
Used to manage color data, independent of draw class/platform.
Used to manage mouse modifiers i.e.
Used to manage a rectangular area, independent of draw class/platform.
IRECT GetFromTRHC(float w, float h) const
Get a subrect of this IRECT expanding from the top-right corner.
IRECT GetFromTLHC(float w, float h) const
Get a subrect of this IRECT expanding from the top-left corner.
IRECT GetCentredInside(const IRECT &sr) const
Get a rectangle the size of sr but with the same center point as this rectangle.
IRECT GetFromBLHC(float w, float h) const
Get a subrect of this IRECT expanding from the bottom-left corner.
IRECT GetGridCell(int row, int col, int nRows, int nColumns) const
Get a subrect (by row, column) of this IRECT which is a cell in a grid of size (nRows * nColumns)
IRECT GetVShifted(float y) const
Get a copy of this rectangle translated on the Y axis.
IRECT GetFromBRHC(float w, float h) const
Get a subrect of this IRECT expanding from the bottom-right corner.
IRECT GetPadded(float padding) const
Get a copy of this IRECT with each value padded by padding N.B.
IText is used to manage font and text/text entry style for a piece of text on the UI,...
A struct encapsulating a set of properties used to configure IVControls.