/
WindowPrivateData.cpp
1202 lines (998 loc) · 33.1 KB
/
WindowPrivateData.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DISTRHO Plugin Framework (DPF)
* Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "WindowPrivateData.hpp"
#include "TopLevelWidgetPrivateData.hpp"
#include "pugl.hpp"
#include "../../distrho/extra/String.hpp"
#ifdef DISTRHO_OS_WINDOWS
# include <direct.h>
# include <winsock2.h>
# include <windows.h>
# include <vector>
#else
# include <unistd.h>
#endif
#define DGL_DEBUG_EVENTS
#if defined(DEBUG) && defined(DGL_DEBUG_EVENTS)
# include <cinttypes>
#endif
START_NAMESPACE_DGL
#if defined(DEBUG) && defined(DGL_DEBUG_EVENTS)
# define DGL_DBG(msg) std::fprintf(stderr, "%s", msg);
# define DGL_DBGp(...) std::fprintf(stderr, __VA_ARGS__);
# define DGL_DBGF std::fflush(stderr);
#else
# define DGL_DBG(msg)
# define DGL_DBGp(...)
# define DGL_DBGF
#endif
#define DEFAULT_WIDTH 640
#define DEFAULT_HEIGHT 480
#define FOR_EACH_TOP_LEVEL_WIDGET(it) \
for (std::list<TopLevelWidget*>::iterator it = topLevelWidgets.begin(); it != topLevelWidgets.end(); ++it)
#define FOR_EACH_TOP_LEVEL_WIDGET_INV(rit) \
for (std::list<TopLevelWidget*>::reverse_iterator rit = topLevelWidgets.rbegin(); rit != topLevelWidgets.rend(); ++rit)
// -----------------------------------------------------------------------
#ifdef DISTRHO_OS_WINDOWS
// static pointer used for direct comparisons
static const char* const kWin32SelectedFileCancelled = "__dpf_cancelled__";
#endif
static double getDesktopScaleFactor(const PuglView* const view)
{
// allow custom scale for testing
if (const char* const scale = getenv("DPF_SCALE_FACTOR"))
return std::max(1.0, std::atof(scale));
if (view != nullptr)
return puglGetDesktopScaleFactor(view);
return 1.0;
}
// -----------------------------------------------------------------------
Window::PrivateData::PrivateData(Application& a, Window* const s)
: app(a),
appData(a.pData),
self(s),
view(puglNewView(appData->world)),
transientParentView(nullptr),
topLevelWidgets(),
isClosed(true),
isVisible(false),
isEmbed(false),
scaleFactor(getDesktopScaleFactor(view)),
autoScaling(false),
autoScaleFactor(1.0),
minWidth(0),
minHeight(0),
keepAspectRatio(false),
#ifdef DISTRHO_OS_WINDOWS
win32SelectedFile(nullptr),
#endif
modal()
{
initPre(DEFAULT_WIDTH, DEFAULT_HEIGHT, false);
}
Window::PrivateData::PrivateData(Application& a, Window* const s, PrivateData* const ppData)
: app(a),
appData(a.pData),
self(s),
view(puglNewView(appData->world)),
transientParentView(ppData->view),
topLevelWidgets(),
isClosed(true),
isVisible(false),
isEmbed(false),
scaleFactor(ppData->scaleFactor),
autoScaling(false),
autoScaleFactor(1.0),
minWidth(0),
minHeight(0),
keepAspectRatio(false),
#ifdef DISTRHO_OS_WINDOWS
win32SelectedFile(nullptr),
#endif
modal(ppData)
{
puglBackendLeave(transientParentView);
puglSetTransientFor(view, puglGetNativeWindow(transientParentView));
initPre(DEFAULT_WIDTH, DEFAULT_HEIGHT, false);
}
Window::PrivateData::PrivateData(Application& a, Window* const s,
const uintptr_t parentWindowHandle,
const double scale, const bool resizable)
: app(a),
appData(a.pData),
self(s),
view(puglNewView(appData->world)),
transientParentView(nullptr),
topLevelWidgets(),
isClosed(parentWindowHandle == 0),
isVisible(parentWindowHandle != 0),
isEmbed(parentWindowHandle != 0),
scaleFactor(scale != 0.0 ? scale : getDesktopScaleFactor(view)),
autoScaling(false),
autoScaleFactor(1.0),
minWidth(0),
minHeight(0),
keepAspectRatio(false),
#ifdef DISTRHO_OS_WINDOWS
win32SelectedFile(nullptr),
#endif
modal()
{
if (isEmbed)
puglSetParentWindow(view, parentWindowHandle);
initPre(DEFAULT_WIDTH, DEFAULT_HEIGHT, resizable);
}
Window::PrivateData::PrivateData(Application& a, Window* const s,
const uintptr_t parentWindowHandle,
const uint width, const uint height,
const double scale, const bool resizable)
: app(a),
appData(a.pData),
self(s),
view(appData->world != nullptr ? puglNewView(appData->world) : nullptr),
transientParentView(nullptr),
topLevelWidgets(),
isClosed(parentWindowHandle == 0),
isVisible(parentWindowHandle != 0 && view != nullptr),
isEmbed(parentWindowHandle != 0),
scaleFactor(scale != 0.0 ? scale : getDesktopScaleFactor(view)),
autoScaling(false),
autoScaleFactor(1.0),
minWidth(0),
minHeight(0),
keepAspectRatio(false),
#ifdef DISTRHO_OS_WINDOWS
win32SelectedFile(nullptr),
#endif
modal()
{
if (isEmbed)
puglSetParentWindow(view, parentWindowHandle);
initPre(width != 0 ? width : DEFAULT_WIDTH, height != 0 ? height : DEFAULT_HEIGHT, resizable);
}
Window::PrivateData::~PrivateData()
{
appData->idleCallbacks.remove(this);
appData->windows.remove(self);
if (view == nullptr)
return;
if (isEmbed)
{
#ifdef HAVE_X11
sofdFileDialogClose();
#endif
puglHide(view);
appData->oneWindowClosed();
isClosed = true;
isVisible = false;
}
#ifdef DISTRHO_OS_WINDOWS
if (win32SelectedFile != nullptr && win32SelectedFile != kWin32SelectedFileCancelled)
std::free(const_cast<char*>(win32SelectedFile));
#endif
puglFreeView(view);
}
// -----------------------------------------------------------------------
void Window::PrivateData::initPre(const uint width, const uint height, const bool resizable)
{
appData->windows.push_back(self);
appData->idleCallbacks.push_back(this);
memset(graphicsContext, 0, sizeof(graphicsContext));
if (view == nullptr)
{
d_stderr2("Failed to create Pugl view, everything will fail!");
return;
}
puglSetMatchingBackendForCurrentBuild(view);
puglClearMinSize(view);
puglSetWindowSize(view, width, height);
puglSetHandle(view, this);
puglSetViewHint(view, PUGL_RESIZABLE, resizable ? PUGL_TRUE : PUGL_FALSE);
puglSetViewHint(view, PUGL_IGNORE_KEY_REPEAT, PUGL_FALSE);
puglSetViewHint(view, PUGL_DEPTH_BITS, 16);
puglSetViewHint(view, PUGL_STENCIL_BITS, 8);
#ifdef DGL_USE_OPENGL3
puglSetViewHint(view, PUGL_USE_COMPAT_PROFILE, PUGL_FALSE);
puglSetViewHint(view, PUGL_CONTEXT_VERSION_MAJOR, 3);
#endif
// PUGL_SAMPLES ??
puglSetEventFunc(view, puglEventCallback);
}
bool Window::PrivateData::initPost()
{
if (view == nullptr)
return false;
// create view now, as a few methods we allow devs to use require it
if (puglRealize(view) != PUGL_SUCCESS)
{
view = nullptr;
d_stderr2("Failed to realize Pugl view, everything will fail!");
return false;
}
if (isEmbed)
{
appData->oneWindowShown();
puglShow(view);
}
// give context back to transient parent window
if (transientParentView != nullptr)
puglBackendEnter(transientParentView);
return true;
}
// -----------------------------------------------------------------------
void Window::PrivateData::close()
{
DGL_DBG("Window close\n");
// DGL_DBGp("Window close DBG %i %i %p\n", isEmbed, isClosed, appData);
if (isEmbed || isClosed)
return;
isClosed = true;
hide();
appData->oneWindowClosed();
}
// -----------------------------------------------------------------------
void Window::PrivateData::show()
{
if (isVisible)
{
DGL_DBG("Window show matches current visible state, ignoring request\n");
return;
}
if (isEmbed)
{
DGL_DBG("Window show cannot be called when embedded\n");
return;
}
DGL_DBG("Window show called\n");
if (view == nullptr)
return;
if (isClosed)
{
isClosed = false;
appData->oneWindowShown();
// FIXME
PuglRect rect = puglGetFrame(view);
puglSetWindowSize(view, static_cast<uint>(rect.width), static_cast<uint>(rect.height));
#ifdef DISTRHO_OS_WINDOWS
puglWin32ShowWindowCentered(view);
#else
puglShow(view);
#endif
}
else
{
#ifdef DISTRHO_OS_WINDOWS
puglWin32RestoreWindow(view);
#else
puglShow(view);
#endif
}
isVisible = true;
}
void Window::PrivateData::hide()
{
if (isEmbed)
{
DGL_DBG("Window hide cannot be called when embedded\n");
return;
}
if (! isVisible)
{
DGL_DBG("Window hide matches current visible state, ignoring request\n");
return;
}
DGL_DBG("Window hide called\n");
if (modal.enabled)
stopModal();
#ifdef HAVE_X11
sofdFileDialogClose();
#endif
puglHide(view);
isVisible = false;
}
// -----------------------------------------------------------------------
void Window::PrivateData::focus()
{
if (view == nullptr)
return;
if (! isEmbed)
puglRaiseWindow(view);
#ifdef HAVE_X11
puglX11GrabFocus(view);
#else
puglGrabFocus(view);
#endif
}
// -----------------------------------------------------------------------
void Window::PrivateData::setResizable(const bool resizable)
{
DISTRHO_SAFE_ASSERT_RETURN(! isEmbed,);
DGL_DBG("Window setResizable called\n");
puglSetViewHint(view, PUGL_RESIZABLE, resizable ? PUGL_TRUE : PUGL_FALSE);
#ifdef DISTRHO_OS_WINDOWS
puglWin32SetWindowResizable(view, resizable);
#endif
}
// -----------------------------------------------------------------------
void Window::PrivateData::idleCallback()
{
#ifndef DGL_FILE_BROWSER_DISABLED
# ifdef DISTRHO_OS_WINDOWS
if (const char* path = win32SelectedFile)
{
win32SelectedFile = nullptr;
if (path == kWin32SelectedFileCancelled)
path = nullptr;
self->onFileSelected(path);
std::free(const_cast<char*>(path));
}
# endif
# ifdef HAVE_X11
if (sofdFileDialogIdle(view))
self->onFileSelected(sofdFileDialogGetPath());
# endif
#endif
}
// -----------------------------------------------------------------------
// idle callback stuff
bool Window::PrivateData::addIdleCallback(IdleCallback* const callback, const uint timerFrequencyInMs)
{
if (timerFrequencyInMs == 0)
{
appData->idleCallbacks.push_back(callback);
return true;
}
return puglStartTimer(view, (uintptr_t)callback, static_cast<double>(timerFrequencyInMs) / 1000.0) == PUGL_SUCCESS;
}
bool Window::PrivateData::removeIdleCallback(IdleCallback* const callback)
{
if (std::find(appData->idleCallbacks.begin(),
appData->idleCallbacks.end(), callback) != appData->idleCallbacks.end())
{
appData->idleCallbacks.remove(callback);
return true;
}
return puglStopTimer(view, (uintptr_t)callback) == PUGL_SUCCESS;
}
#ifndef DGL_FILE_BROWSER_DISABLED
// -----------------------------------------------------------------------
// file handling
bool Window::PrivateData::openFileBrowser(const Window::FileBrowserOptions& options)
{
using DISTRHO_NAMESPACE::String;
// --------------------------------------------------------------------------
// configure start dir
// TODO: get abspath if needed
// TODO: cross-platform
String startDir(options.startDir);
if (startDir.isEmpty())
{
// TESTING verify this whole thing...
# ifdef DISTRHO_OS_WINDOWS
if (char* const cwd = _getcwd(nullptr, 0))
{
startDir = cwd;
std::free(cwd);
}
# else
if (char* const cwd = getcwd(nullptr, 0))
{
startDir = cwd;
std::free(cwd);
}
# endif
}
DISTRHO_SAFE_ASSERT_RETURN(startDir.isNotEmpty(), false);
if (! startDir.endsWith(DISTRHO_OS_SEP))
startDir += DISTRHO_OS_SEP_STR;
// --------------------------------------------------------------------------
// configure title
String title(options.title);
if (title.isEmpty())
{
title = puglGetWindowTitle(view);
if (title.isEmpty())
title = "FileBrowser";
}
// --------------------------------------------------------------------------
// show
# ifdef DISTRHO_OS_MAC
uint flags = 0x0;
if (options.buttons.listAllFiles == FileBrowserOptions::kButtonVisibleChecked)
flags |= 0x001;
else if (options.buttons.listAllFiles == FileBrowserOptions::kButtonVisibleUnchecked)
flags |= 0x002;
if (options.buttons.showHidden == FileBrowserOptions::kButtonVisibleChecked)
flags |= 0x010;
else if (options.buttons.showHidden == FileBrowserOptions::kButtonVisibleUnchecked)
flags |= 0x020;
if (options.buttons.showPlaces == FileBrowserOptions::kButtonVisibleChecked)
flags |= 0x100;
else if (options.buttons.showPlaces == FileBrowserOptions::kButtonVisibleUnchecked)
flags |= 0x200;
return puglMacOSFilePanelOpen(view, startDir, title, flags, openPanelCallback);
# endif
# ifdef DISTRHO_OS_WINDOWS
// the old and compatible dialog API
OPENFILENAMEW ofn;
memset(&ofn, 0, sizeof(ofn));
if (win32SelectedFile != nullptr && win32SelectedFile != kWin32SelectedFileCancelled)
std::free(const_cast<char*>(win32SelectedFile));
win32SelectedFile = nullptr;
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = (HWND)puglGetNativeWindow(view);
// set start directory in UTF-16 encoding
std::vector<WCHAR> startDirW;
startDirW.resize(startDir.length() + 1);
if (MultiByteToWideChar(CP_UTF8, 0, startDir.buffer(), -1, startDirW.data(), startDirW.size()))
ofn.lpstrInitialDir = startDirW.data();
// set title in UTF-16 encoding
std::vector<WCHAR> titleW;
titleW.resize(title.length() + 1);
if (MultiByteToWideChar(CP_UTF8, 0, title.buffer(), -1, titleW.data(), titleW.size()))
ofn.lpstrTitle = titleW.data();
// prepare a buffer to receive the result
std::vector<WCHAR> fileNameW(32768); // the Unicode maximum
ofn.lpstrFile = fileNameW.data();
ofn.nMaxFile = (DWORD)fileNameW.size();
// flags
ofn.Flags = OFN_PATHMUSTEXIST;
if (options.buttons.showHidden == FileBrowserOptions::kButtonVisibleChecked)
ofn.Flags |= OFN_FORCESHOWHIDDEN;
if (options.buttons.showPlaces == FileBrowserOptions::kButtonInvisible)
ofn.FlagsEx |= OFN_EX_NOPLACESBAR;
// TODO synchronous only, can't do better with WinAPI native dialogs.
// threading might work, if someone is motivated to risk it.
if (GetOpenFileNameW(&ofn))
{
// back to UTF-8
std::vector<char> fileNameA(4 * 32768);
if (WideCharToMultiByte(CP_UTF8, 0, fileNameW.data(), -1,
fileNameA.data(), (int)fileNameA.size(),
nullptr, nullptr))
{
// handle it during the next idle cycle (fake async)
win32SelectedFile = strdup(fileNameA.data());
}
}
if (win32SelectedFile == nullptr)
win32SelectedFile = kWin32SelectedFileCancelled;
return true;
# endif
# ifdef HAVE_X11
uint flags = 0x0;
if (options.buttons.listAllFiles == FileBrowserOptions::kButtonVisibleChecked)
flags |= 0x001;
else if (options.buttons.listAllFiles == FileBrowserOptions::kButtonVisibleUnchecked)
flags |= 0x002;
if (options.buttons.showHidden == FileBrowserOptions::kButtonVisibleChecked)
flags |= 0x010;
else if (options.buttons.showHidden == FileBrowserOptions::kButtonVisibleUnchecked)
flags |= 0x020;
if (options.buttons.showPlaces == FileBrowserOptions::kButtonVisibleChecked)
flags |= 0x100;
else if (options.buttons.showPlaces == FileBrowserOptions::kButtonVisibleUnchecked)
flags |= 0x200;
return sofdFileDialogShow(view, startDir, title, flags, autoScaling ? autoScaleFactor : scaleFactor);
# endif
return false;
}
# ifdef DISTRHO_OS_MAC
void Window::PrivateData::openPanelCallback(PuglView* const view, const char* const path)
{
((Window::PrivateData*)puglGetHandle(view))->self->onFileSelected(path);
}
# endif
#endif // ! DGL_FILE_BROWSER_DISABLED
// -----------------------------------------------------------------------
// modal handling
void Window::PrivateData::startModal()
{
DGL_DBG("Window modal loop starting..."); DGL_DBGF;
DISTRHO_SAFE_ASSERT_RETURN(modal.parent != nullptr, show());
// activate modal mode for this window
modal.enabled = true;
// make parent give focus to us
modal.parent->modal.child = this;
// make sure both parent and ourselves are visible
modal.parent->show();
show();
#ifdef DISTRHO_OS_MAC
puglMacOSAddChildWindow(modal.parent->view, view);
#endif
DGL_DBG("Ok\n");
}
void Window::PrivateData::stopModal()
{
DGL_DBG("Window modal loop stopping..."); DGL_DBGF;
// deactivate modal mode
modal.enabled = false;
// safety checks, make sure we have a parent and we are currently active as the child to give focus to
if (modal.parent == nullptr)
return;
if (modal.parent->modal.child != this)
return;
#ifdef DISTRHO_OS_MAC
puglMacOSRemoveChildWindow(modal.parent->view, view);
#endif
// stop parent from giving focus to us, so it behaves like normal
modal.parent->modal.child = nullptr;
// refocus main window after closing child
if (! modal.parent->isClosed)
{
const Widget::MotionEvent ev;
modal.parent->onPuglMotion(ev);
modal.parent->focus();
}
DGL_DBG("Ok\n");
}
void Window::PrivateData::runAsModal(const bool blockWait)
{
DGL_DBGp("Window::PrivateData::runAsModal %i\n", blockWait);
startModal();
if (blockWait)
{
DISTRHO_SAFE_ASSERT_RETURN(appData->isStandalone,);
while (isVisible && modal.enabled)
appData->idle(10);
stopModal();
}
else
{
appData->idle(0);
}
}
// -----------------------------------------------------------------------
// pugl events
void Window::PrivateData::onPuglConfigure(const double width, const double height)
{
DISTRHO_SAFE_ASSERT_INT2_RETURN(width > 1 && height > 1, width, height,);
DGL_DBGp("PUGL: onReshape : %f %f\n", width, height);
if (autoScaling)
{
const double scaleHorizontal = width / static_cast<double>(minWidth);
const double scaleVertical = height / static_cast<double>(minHeight);
autoScaleFactor = scaleHorizontal < scaleVertical ? scaleHorizontal : scaleVertical;
}
const uint uwidth = static_cast<uint>(width + 0.5);
const uint uheight = static_cast<uint>(height + 0.5);
self->onReshape(uwidth, uheight);
#ifndef DPF_TEST_WINDOW_CPP
FOR_EACH_TOP_LEVEL_WIDGET(it)
{
TopLevelWidget* const widget(*it);
/* Some special care here, we call Widget::setSize instead of the TopLevelWidget one.
* This is because we want TopLevelWidget::setSize to handle both window and widget size,
* but we dont want to change window size here, because we are the window..
* So we just call the Widget specific method manually.
*
* Alternatively, we could expose a resize function on the pData, like done with the display function.
* But there is nothing extra we need to do in there, so this works fine.
*/
((Widget*)widget)->setSize(uwidth, uheight);
}
#endif
// always repaint after a resize
puglPostRedisplay(view);
}
void Window::PrivateData::onPuglExpose()
{
DGL_DBGp("PUGL: onPuglExpose\n");
puglOnDisplayPrepare(view);
#ifndef DPF_TEST_WINDOW_CPP
FOR_EACH_TOP_LEVEL_WIDGET(it)
{
TopLevelWidget* const widget(*it);
if (widget->isVisible())
widget->pData->display();
}
#endif
}
void Window::PrivateData::onPuglClose()
{
DGL_DBG("PUGL: onClose\n");
#ifndef DISTRHO_OS_MAC
// if we are running as standalone we can prevent closing in certain conditions
if (appData->isStandalone)
{
// a child window is active, gives focus to it
if (modal.child != nullptr)
return modal.child->focus();
// ask window if we should close
if (! self->onClose())
return;
}
#endif
if (modal.enabled)
stopModal();
if (modal.child != nullptr)
{
modal.child->close();
modal.child = nullptr;
}
close();
}
void Window::PrivateData::onPuglFocus(const bool focus, const CrossingMode mode)
{
DGL_DBGp("onPuglFocus : %i %i | %i\n", focus, mode, isClosed);
if (isClosed)
return;
if (modal.child != nullptr)
return modal.child->focus();
self->onFocus(focus, mode);
}
void Window::PrivateData::onPuglKey(const Widget::KeyboardEvent& ev)
{
DGL_DBGp("onPuglKey : %i %u %u\n", ev.press, ev.key, ev.keycode);
if (modal.child != nullptr)
return modal.child->focus();
#ifndef DPF_TEST_WINDOW_CPP
FOR_EACH_TOP_LEVEL_WIDGET_INV(rit)
{
TopLevelWidget* const widget(*rit);
if (widget->isVisible() && widget->pData->keyboardEvent(ev))
break;
}
#endif
}
void Window::PrivateData::onPuglSpecial(const Widget::SpecialEvent& ev)
{
DGL_DBGp("onPuglSpecial : %i %u\n", ev.press, ev.key);
if (modal.child != nullptr)
return modal.child->focus();
#ifndef DPF_TEST_WINDOW_CPP
FOR_EACH_TOP_LEVEL_WIDGET_INV(rit)
{
TopLevelWidget* const widget(*rit);
if (widget->isVisible() && widget->pData->specialEvent(ev))
break;
}
#endif
}
void Window::PrivateData::onPuglText(const Widget::CharacterInputEvent& ev)
{
DGL_DBGp("onPuglText : %u %u %s\n", ev.keycode, ev.character, ev.string);
if (modal.child != nullptr)
return modal.child->focus();
#ifndef DPF_TEST_WINDOW_CPP
FOR_EACH_TOP_LEVEL_WIDGET_INV(rit)
{
TopLevelWidget* const widget(*rit);
if (widget->isVisible() && widget->pData->characterInputEvent(ev))
break;
}
#endif
}
void Window::PrivateData::onPuglMouse(const Widget::MouseEvent& ev)
{
DGL_DBGp("onPuglMouse : %i %i %f %f\n", ev.button, ev.press, ev.pos.getX(), ev.pos.getY());
if (modal.child != nullptr)
return modal.child->focus();
#ifndef DPF_TEST_WINDOW_CPP
FOR_EACH_TOP_LEVEL_WIDGET_INV(rit)
{
TopLevelWidget* const widget(*rit);
if (widget->isVisible() && widget->pData->mouseEvent(ev))
break;
}
#endif
}
void Window::PrivateData::onPuglMotion(const Widget::MotionEvent& ev)
{
DGL_DBGp("onPuglMotion : %f %f\n", ev.pos.getX(), ev.pos.getY());
if (modal.child != nullptr)
return modal.child->focus();
#ifndef DPF_TEST_WINDOW_CPP
FOR_EACH_TOP_LEVEL_WIDGET_INV(rit)
{
TopLevelWidget* const widget(*rit);
if (widget->isVisible() && widget->pData->motionEvent(ev))
break;
}
#endif
}
void Window::PrivateData::onPuglScroll(const Widget::ScrollEvent& ev)
{
DGL_DBGp("onPuglScroll : %f %f %f %f\n", ev.pos.getX(), ev.pos.getY(), ev.delta.getX(), ev.delta.getY());
if (modal.child != nullptr)
return modal.child->focus();
#ifndef DPF_TEST_WINDOW_CPP
FOR_EACH_TOP_LEVEL_WIDGET_INV(rit)
{
TopLevelWidget* const widget(*rit);
if (widget->isVisible() && widget->pData->scrollEvent(ev))
break;
}
#endif
}
#if defined(DEBUG) && defined(DGL_DEBUG_EVENTS)
static int printEvent(const PuglEvent* event, const char* prefix, const bool verbose);
#endif
PuglStatus Window::PrivateData::puglEventCallback(PuglView* const view, const PuglEvent* const event)
{
Window::PrivateData* const pData = (Window::PrivateData*)puglGetHandle(view);
#if defined(DEBUG) && defined(DGL_DEBUG_EVENTS)
printEvent(event, "pugl event: ", true);
#endif
switch (event->type)
{
///< No event
case PUGL_NOTHING:
break;
///< View created, a #PuglEventCreate
case PUGL_CREATE:
#ifdef HAVE_X11
if (! pData->isEmbed)
puglX11SetWindowTypeAndPID(view);
#endif
break;
///< View destroyed, a #PuglEventDestroy
case PUGL_DESTROY:
break;
///< View moved/resized, a #PuglEventConfigure
case PUGL_CONFIGURE:
// unused x, y (double)
pData->onPuglConfigure(event->configure.width, event->configure.height);
break;
///< View made visible, a #PuglEventMap
case PUGL_MAP:
break;
///< View made invisible, a #PuglEventUnmap
case PUGL_UNMAP:
break;
///< View ready to draw, a #PuglEventUpdate
case PUGL_UPDATE:
break;
///< View must be drawn, a #PuglEventExpose
case PUGL_EXPOSE:
// unused x, y, width, height (double)
pData->onPuglExpose();
break;
///< View will be closed, a #PuglEventClose
case PUGL_CLOSE:
pData->onPuglClose();
break;
///< Keyboard focus entered view, a #PuglEventFocus
case PUGL_FOCUS_IN:
///< Keyboard focus left view, a #PuglEventFocus
case PUGL_FOCUS_OUT:
pData->onPuglFocus(event->type == PUGL_FOCUS_IN,
static_cast<CrossingMode>(event->focus.mode));
break;
///< Key pressed, a #PuglEventKey
case PUGL_KEY_PRESS:
///< Key released, a #PuglEventKey
case PUGL_KEY_RELEASE:
{
// unused x, y, xRoot, yRoot (double)
// TODO special keys?
Widget::KeyboardEvent ev;
ev.mod = event->key.state;
ev.flags = event->key.flags;
ev.time = static_cast<uint>(event->key.time * 1000.0 + 0.5);
ev.press = event->type == PUGL_KEY_PRESS;
ev.key = event->key.key;
ev.keycode = event->key.keycode;
if ((ev.mod & kModifierShift) != 0 && ev.key >= 'a' && ev.key <= 'z')
ev.key -= 'a' - 'A'; // a-z -> A-Z
pData->onPuglKey(ev);
break;
}
///< Character entered, a #PuglEventText
case PUGL_TEXT:
{
// unused x, y, xRoot, yRoot (double)
Widget::CharacterInputEvent ev;
ev.mod = event->text.state;
ev.flags = event->text.flags;
ev.time = static_cast<uint>(event->text.time * 1000.0 + 0.5);
ev.keycode = event->text.keycode;
ev.character = event->text.character;
std::strncpy(ev.string, event->text.string, sizeof(ev.string));
pData->onPuglText(ev);
break;
}
///< Pointer entered view, a #PuglEventCrossing
case PUGL_POINTER_IN:
break;
///< Pointer left view, a #PuglEventCrossing
case PUGL_POINTER_OUT:
break;