/
testappdelegate.cpp
345 lines (311 loc) · 10.6 KB
/
testappdelegate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// This file is part of VSTGUI. It is subject to the license terms
// in the LICENSE file found in the top-level directory of this
// distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
#include "testappdelegate.h"
#include "testmodel.h"
#include "testabout.h"
#include "AlertBoxDesign.h"
#include "vstgui/standalone/include/helpers/appdelegate.h"
#include "vstgui/standalone/include/helpers/windowlistener.h"
#include "vstgui/standalone/include/helpers/uidesc/customization.h"
#include "vstgui/standalone/include/iapplication.h"
#include "vstgui/standalone/include/iwindow.h"
#include "vstgui/standalone/include/icommand.h"
#include "vstgui/standalone/include/iuidescwindow.h"
#include "vstgui/standalone/include/ialertbox.h"
#include "vstgui/uidescription/delegationcontroller.h"
#include "vstgui/lib/cframe.h"
#include "vstgui/lib/crect.h"
#include "vstgui/lib/iviewlistener.h"
#include "vstgui/lib/controls/ccontrol.h"
#include "vstgui/lib/controls/clistcontrol.h"
#include "vstgui/standalone/source/genericalertbox.h"
#include "vstgui/uidescription/uiattributes.h"
#include "vstgui/uidescription/iuidescription.h"
#include "vstgui/lib/cexternalview.h"
#include "vstgui/contrib/datepicker.h"
#include "vstgui/contrib/evbutton.h"
#include <memory>
#if MAC
#include "metalwindow.h"
#elif WINDOWS
#include "direct3dwindow.h"
#endif
//------------------------------------------------------------------------
namespace MyApp {
using namespace VSTGUI;
using namespace VSTGUI::Standalone;
//------------------------------------------------------------------------
class Delegate : public Application::DelegateAdapter,
public ICommandHandler,
public WindowListenerAdapter
{
public:
Delegate ();
// Application::IDelegate
void finishLaunching () override;
void showAboutDialog () override;
bool hasAboutDialog () override;
UTF8StringPtr getSharedUIResourceFilename () const override;
// ICommandHandler
bool canHandleCommand (const Command& command) override;
bool handleCommand (const Command& command) override;
// WindowListenerAdapter
void onClosed (const IWindow& window) override;
private:
std::shared_ptr<TestModel> model;
};
//------------------------------------------------------------------------
Application::Init gAppDelegate (std::make_unique<Delegate> (),
{{Application::ConfigKey::ShowCommandsInContextMenu, 1}});
static Command NewPopup {CommandGroup::File, "New Popup"};
static Command ShowAlertBoxDesign {CommandGroup::File, "Show AlertBox Design"};
#if MAC
static Command NewMetalExampleWindow {CommandGroup::File, "New Metal Example Window"};
#elif WINDOWS
static Command NewDirect3DExampleWindow {CommandGroup::File, "New Direct3D Example Window"};
#endif
//------------------------------------------------------------------------
class DisabledControlsController : public DelegationController,
public ViewListenerAdapter
{
public:
DisabledControlsController (IController* parent) : DelegationController (parent) {}
~DisabledControlsController ()
{
for (auto control : controls)
{
control->unregisterViewListener (this);
}
controls.clear ();
}
CView* verifyView (CView* view, const UIAttributes& attributes,
const IUIDescription* description) override
{
if (auto control = dynamic_cast<CControl*> (view))
{
control->registerViewListener (this);
controls.push_back (control);
}
return controller->verifyView (view, attributes, description);
}
void viewOnMouseEnabled (CView* view, bool state) override
{
view->setAlphaValue (state ? 1.f : 0.5f);
}
void viewWillDelete (CView* view) override
{
if (auto control = dynamic_cast<CControl*> (view))
{
auto it = std::find (controls.begin (), controls.end (), control);
if (it != controls.end ())
{
control->unregisterViewListener (this);
controls.erase (it);
}
}
}
std::vector<CControl*> controls;
};
//------------------------------------------------------------------------
class WeekdaysListConfigurator : public StaticListControlConfigurator
{
public:
WeekdaysListConfigurator (const StaticListControlConfigurator& c)
: StaticListControlConfigurator (c.getRowHeight (), c.getFlags ())
{
}
CListControlRowDesc getRowDesc (int32_t row) const override
{
if (row == 0)
return {getRowHeight () * 2., 0};
return {getRowHeight (), getFlags ()};
}
};
//------------------------------------------------------------------------
class WeekdaysController : public DelegationController
{
public:
WeekdaysController (IController* parent) : DelegationController (parent) {}
CView* verifyView (CView* view, const UIAttributes& attributes,
const IUIDescription* description) override
{
if (auto listControl = dynamic_cast<CListControl*> (view))
{
auto configurator =
dynamic_cast<StaticListControlConfigurator*> (listControl->getConfigurator ());
if (configurator)
{
listControl->setConfigurator (makeOwned<WeekdaysListConfigurator> (*configurator));
}
}
return controller->verifyView (view, attributes, description);
}
};
//------------------------------------------------------------------------
class DatePickerController : public DelegationController
{
public:
DatePickerController (IController* parent) : DelegationController (parent) {}
#if MAC || WINDOWS
CView* createView (const UIAttributes& attributes, const IUIDescription* description) override
{
if (auto customViewName = attributes.getAttributeValue (IUIDescription::kCustomViewName))
{
if (*customViewName == "DatePicker")
{
auto datePicker = std::make_shared<ExternalView::DatePicker> ();
datePicker->setDate ({12, 8, 2023});
datePicker->setChangeCallback ([] (auto date) {
#if DEBUG
DebugPrint ("%d.%d.%d\n", date.day, date.month, date.year);
#endif
});
return new CExternalView (CRect (), datePicker);
}
if (*customViewName == "Native Checkbox")
{
auto checkbox = std::make_shared<ExternalView::Button> (
ExternalView::Button::Type::Checkbox, "Checkbox");
return new CExternalControl (CRect (), checkbox);
}
if (*customViewName == "Native Push Button")
{
auto checkbox = std::make_shared<ExternalView::Button> (
ExternalView::Button::Type::Push, "Push");
return new CExternalControl (CRect (), checkbox);
}
if (*customViewName == "Native OnOff Button")
{
auto checkbox = std::make_shared<ExternalView::Button> (
ExternalView::Button::Type::OnOff, "OnOff");
return new CExternalControl (CRect (), checkbox);
}
if (*customViewName == "Native Radio Button")
{
auto checkbox = std::make_shared<ExternalView::Button> (
ExternalView::Button::Type::Radio, "Radio");
return new CExternalControl (CRect (), checkbox);
}
}
return controller->createView (attributes, description);
}
#endif
};
//------------------------------------------------------------------------
Delegate::Delegate ()
: Application::DelegateAdapter ({"VSTGUI Standalone", "1.0.0", VSTGUI_STANDALONE_APP_URI})
{
}
//------------------------------------------------------------------------
void Delegate::finishLaunching ()
{
model = std::make_shared<TestModel> ();
IApplication::instance ().registerCommand (Commands::NewDocument, 'n');
IApplication::instance ().registerCommand (NewPopup, 'N');
IApplication::instance ().registerCommand (ShowAlertBoxDesign, 'b');
#if MAC
IApplication::instance ().registerCommand (NewMetalExampleWindow, 'M');
#elif WINDOWS
IApplication::instance ().registerCommand (NewDirect3DExampleWindow, 'D');
#endif
handleCommand (Commands::NewDocument);
}
//------------------------------------------------------------------------
void Delegate::onClosed (const IWindow& window)
{
if (IApplication::instance ().getWindows ().empty ())
{
IApplication::instance ().quit ();
}
}
//------------------------------------------------------------------------
bool Delegate::canHandleCommand (const Command& command)
{
#if MAC
if (command == NewMetalExampleWindow)
return true;
#elif WINDOWS
if (command == NewDirect3DExampleWindow)
return true;
#endif
return command == Commands::NewDocument || command == NewPopup || command == ShowAlertBoxDesign;
}
//------------------------------------------------------------------------
bool Delegate::handleCommand (const Command& command)
{
if (command == Commands::NewDocument || command == NewPopup)
{
UIDesc::Config config;
config.windowConfig.title = "Test Window";
config.windowConfig.autoSaveFrameName = "TestWindowFrame";
config.windowConfig.style.close ();
config.windowConfig.size = {100, 100};
config.viewName = "view";
config.modelBinding = model;
if (command == NewPopup)
{
config.uiDescFileName = "testpopup.uidesc";
config.windowConfig.type = WindowType::Popup;
config.windowConfig.style.movableByWindowBackground ();
}
else
{
config.uiDescFileName = "test.uidesc";
config.windowConfig.style.border ().size ();
config.windowConfig.style.movableByWindowBackground ();
auto customization = UIDesc::Customization::make ();
customization->addCreateViewControllerFunc (
"DisabledControlsController",
[] (const UTF8StringView&, IController* parent, const IUIDescription*) {
return new DisabledControlsController (parent);
});
customization->addCreateViewControllerFunc (
"WeekdaysController",
[] (const UTF8StringView&, IController* parent, const IUIDescription*) {
return new WeekdaysController (parent);
});
customization->addCreateViewControllerFunc (
"DatePickerController",
[] (const UTF8StringView&, IController* parent, const IUIDescription*) {
return new DatePickerController (parent);
});
config.customization = customization;
}
if (auto window = UIDesc::makeWindow (config))
{
window->show ();
window->registerWindowListener (this);
}
return true;
}
else if (command == ShowAlertBoxDesign)
{
if (auto window = VSTGUI::Standalone::makeAlertBoxDesignWindow ())
window->show ();
return true;
}
#if MAC
else if (command == NewMetalExampleWindow)
{
if (auto window = makeNewMetalExampleWindow ())
window->show ();
return true;
}
#elif WINDOWS
else if (command == NewDirect3DExampleWindow)
{
if (auto window = makeNewDirect3DExampleWindow ())
window->show ();
return true;
}
#endif
return false;
}
//------------------------------------------------------------------------
void Delegate::showAboutDialog () { About::show (); }
//------------------------------------------------------------------------
bool Delegate::hasAboutDialog () { return true; }
//------------------------------------------------------------------------
VSTGUI::UTF8StringPtr Delegate::getSharedUIResourceFilename () const { return "resources.uidesc"; }
} // MyApp