forked from juce-framework/JUCE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
juce_win32_Windowing.cpp
5597 lines (4489 loc) · 198 KB
/
juce_win32_Windowing.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
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
#if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client
#include <juce_audio_plugin_client/AAX/juce_AAX_Modifier_Injector.h>
#endif
namespace juce
{
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wcast-function-type")
#undef GetSystemMetrics // multimon overrides this for some reason and causes a mess..
// these are in the windows SDK, but need to be repeated here for GCC..
#ifndef GET_APPCOMMAND_LPARAM
#define GET_APPCOMMAND_LPARAM(lParam) ((short) (HIWORD (lParam) & ~FAPPCOMMAND_MASK))
#define FAPPCOMMAND_MASK 0xF000
#define APPCOMMAND_MEDIA_NEXTTRACK 11
#define APPCOMMAND_MEDIA_PREVIOUSTRACK 12
#define APPCOMMAND_MEDIA_STOP 13
#define APPCOMMAND_MEDIA_PLAY_PAUSE 14
#endif
#ifndef WM_APPCOMMAND
#define WM_APPCOMMAND 0x0319
#endif
void juce_repeatLastProcessPriority();
bool juce_isRunningInWine();
using CheckEventBlockedByModalComps = bool (*) (const MSG&);
extern CheckEventBlockedByModalComps isEventBlockedByModalComps;
static bool shouldDeactivateTitleBar = true;
void* getUser32Function (const char*);
#if JUCE_DEBUG
int numActiveScopedDpiAwarenessDisablers = 0;
static bool isInScopedDPIAwarenessDisabler() { return numActiveScopedDpiAwarenessDisablers > 0; }
extern HWND juce_messageWindowHandle;
#endif
struct ScopedDeviceContext
{
explicit ScopedDeviceContext (HWND h)
: hwnd (h), dc (GetDC (hwnd))
{
}
~ScopedDeviceContext()
{
ReleaseDC (hwnd, dc);
}
HWND hwnd;
HDC dc;
JUCE_DECLARE_NON_COPYABLE (ScopedDeviceContext)
JUCE_DECLARE_NON_MOVEABLE (ScopedDeviceContext)
};
//==============================================================================
#ifndef WM_TOUCH
enum
{
WM_TOUCH = 0x0240,
TOUCHEVENTF_MOVE = 0x0001,
TOUCHEVENTF_DOWN = 0x0002,
TOUCHEVENTF_UP = 0x0004
};
typedef HANDLE HTOUCHINPUT;
typedef HANDLE HGESTUREINFO;
struct TOUCHINPUT
{
LONG x;
LONG y;
HANDLE hSource;
DWORD dwID;
DWORD dwFlags;
DWORD dwMask;
DWORD dwTime;
ULONG_PTR dwExtraInfo;
DWORD cxContact;
DWORD cyContact;
};
struct GESTUREINFO
{
UINT cbSize;
DWORD dwFlags;
DWORD dwID;
HWND hwndTarget;
POINTS ptsLocation;
DWORD dwInstanceID;
DWORD dwSequenceID;
ULONGLONG ullArguments;
UINT cbExtraArgs;
};
#endif
#ifndef WM_NCPOINTERUPDATE
enum
{
WM_NCPOINTERUPDATE = 0x241,
WM_NCPOINTERDOWN = 0x242,
WM_NCPOINTERUP = 0x243,
WM_POINTERUPDATE = 0x245,
WM_POINTERDOWN = 0x246,
WM_POINTERUP = 0x247,
WM_POINTERENTER = 0x249,
WM_POINTERLEAVE = 0x24A,
WM_POINTERACTIVATE = 0x24B,
WM_POINTERCAPTURECHANGED = 0x24C,
WM_TOUCHHITTESTING = 0x24D,
WM_POINTERWHEEL = 0x24E,
WM_POINTERHWHEEL = 0x24F,
WM_POINTERHITTEST = 0x250
};
enum
{
PT_TOUCH = 0x00000002,
PT_PEN = 0x00000003
};
enum POINTER_BUTTON_CHANGE_TYPE
{
POINTER_CHANGE_NONE,
POINTER_CHANGE_FIRSTBUTTON_DOWN,
POINTER_CHANGE_FIRSTBUTTON_UP,
POINTER_CHANGE_SECONDBUTTON_DOWN,
POINTER_CHANGE_SECONDBUTTON_UP,
POINTER_CHANGE_THIRDBUTTON_DOWN,
POINTER_CHANGE_THIRDBUTTON_UP,
POINTER_CHANGE_FOURTHBUTTON_DOWN,
POINTER_CHANGE_FOURTHBUTTON_UP,
POINTER_CHANGE_FIFTHBUTTON_DOWN,
POINTER_CHANGE_FIFTHBUTTON_UP
};
enum
{
PEN_MASK_NONE = 0x00000000,
PEN_MASK_PRESSURE = 0x00000001,
PEN_MASK_ROTATION = 0x00000002,
PEN_MASK_TILT_X = 0x00000004,
PEN_MASK_TILT_Y = 0x00000008
};
enum
{
TOUCH_MASK_NONE = 0x00000000,
TOUCH_MASK_CONTACTAREA = 0x00000001,
TOUCH_MASK_ORIENTATION = 0x00000002,
TOUCH_MASK_PRESSURE = 0x00000004
};
enum
{
POINTER_FLAG_NONE = 0x00000000,
POINTER_FLAG_NEW = 0x00000001,
POINTER_FLAG_INRANGE = 0x00000002,
POINTER_FLAG_INCONTACT = 0x00000004,
POINTER_FLAG_FIRSTBUTTON = 0x00000010,
POINTER_FLAG_SECONDBUTTON = 0x00000020,
POINTER_FLAG_THIRDBUTTON = 0x00000040,
POINTER_FLAG_FOURTHBUTTON = 0x00000080,
POINTER_FLAG_FIFTHBUTTON = 0x00000100,
POINTER_FLAG_PRIMARY = 0x00002000,
POINTER_FLAG_CONFIDENCE = 0x00004000,
POINTER_FLAG_CANCELED = 0x00008000,
POINTER_FLAG_DOWN = 0x00010000,
POINTER_FLAG_UPDATE = 0x00020000,
POINTER_FLAG_UP = 0x00040000,
POINTER_FLAG_WHEEL = 0x00080000,
POINTER_FLAG_HWHEEL = 0x00100000,
POINTER_FLAG_CAPTURECHANGED = 0x00200000,
POINTER_FLAG_HASTRANSFORM = 0x00400000
};
typedef DWORD POINTER_INPUT_TYPE;
typedef UINT32 POINTER_FLAGS;
typedef UINT32 PEN_FLAGS;
typedef UINT32 PEN_MASK;
typedef UINT32 TOUCH_FLAGS;
typedef UINT32 TOUCH_MASK;
struct POINTER_INFO
{
POINTER_INPUT_TYPE pointerType;
UINT32 pointerId;
UINT32 frameId;
POINTER_FLAGS pointerFlags;
HANDLE sourceDevice;
HWND hwndTarget;
POINT ptPixelLocation;
POINT ptHimetricLocation;
POINT ptPixelLocationRaw;
POINT ptHimetricLocationRaw;
DWORD dwTime;
UINT32 historyCount;
INT32 InputData;
DWORD dwKeyStates;
UINT64 PerformanceCount;
POINTER_BUTTON_CHANGE_TYPE ButtonChangeType;
};
struct POINTER_TOUCH_INFO
{
POINTER_INFO pointerInfo;
TOUCH_FLAGS touchFlags;
TOUCH_MASK touchMask;
RECT rcContact;
RECT rcContactRaw;
UINT32 orientation;
UINT32 pressure;
};
struct POINTER_PEN_INFO
{
POINTER_INFO pointerInfo;
PEN_FLAGS penFlags;
PEN_MASK penMask;
UINT32 pressure;
UINT32 rotation;
INT32 tiltX;
INT32 tiltY;
};
#define GET_POINTERID_WPARAM(wParam) (LOWORD(wParam))
#endif
#ifndef MONITOR_DPI_TYPE
enum Monitor_DPI_Type
{
MDT_Effective_DPI = 0,
MDT_Angular_DPI = 1,
MDT_Raw_DPI = 2,
MDT_Default = MDT_Effective_DPI
};
#endif
#ifndef DPI_AWARENESS
enum DPI_Awareness
{
DPI_Awareness_Invalid = -1,
DPI_Awareness_Unaware = 0,
DPI_Awareness_System_Aware = 1,
DPI_Awareness_Per_Monitor_Aware = 2
};
#endif
#ifndef USER_DEFAULT_SCREEN_DPI
#define USER_DEFAULT_SCREEN_DPI 96
#endif
#ifndef _DPI_AWARENESS_CONTEXTS_
typedef HANDLE DPI_AWARENESS_CONTEXT;
#define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT) - 1)
#define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT) - 2)
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT) - 3)
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT) - 4)
#endif
// Some versions of the Windows 10 SDK define _DPI_AWARENESS_CONTEXTS_ but not
// DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
#ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT) - 4)
#endif
//==============================================================================
using RegisterTouchWindowFunc = BOOL (WINAPI*) (HWND, ULONG);
using GetTouchInputInfoFunc = BOOL (WINAPI*) (HTOUCHINPUT, UINT, TOUCHINPUT*, int);
using CloseTouchInputHandleFunc = BOOL (WINAPI*) (HTOUCHINPUT);
using GetGestureInfoFunc = BOOL (WINAPI*) (HGESTUREINFO, GESTUREINFO*);
static RegisterTouchWindowFunc registerTouchWindow = nullptr;
static GetTouchInputInfoFunc getTouchInputInfo = nullptr;
static CloseTouchInputHandleFunc closeTouchInputHandle = nullptr;
static GetGestureInfoFunc getGestureInfo = nullptr;
static bool hasCheckedForMultiTouch = false;
static bool canUseMultiTouch()
{
if (registerTouchWindow == nullptr && ! hasCheckedForMultiTouch)
{
hasCheckedForMultiTouch = true;
registerTouchWindow = (RegisterTouchWindowFunc) getUser32Function ("RegisterTouchWindow");
getTouchInputInfo = (GetTouchInputInfoFunc) getUser32Function ("GetTouchInputInfo");
closeTouchInputHandle = (CloseTouchInputHandleFunc) getUser32Function ("CloseTouchInputHandle");
getGestureInfo = (GetGestureInfoFunc) getUser32Function ("GetGestureInfo");
}
return registerTouchWindow != nullptr;
}
//==============================================================================
using GetPointerTypeFunc = BOOL (WINAPI*) (UINT32, POINTER_INPUT_TYPE*);
using GetPointerTouchInfoFunc = BOOL (WINAPI*) (UINT32, POINTER_TOUCH_INFO*);
using GetPointerPenInfoFunc = BOOL (WINAPI*) (UINT32, POINTER_PEN_INFO*);
static GetPointerTypeFunc getPointerTypeFunction = nullptr;
static GetPointerTouchInfoFunc getPointerTouchInfo = nullptr;
static GetPointerPenInfoFunc getPointerPenInfo = nullptr;
static bool canUsePointerAPI = false;
static void checkForPointerAPI()
{
getPointerTypeFunction = (GetPointerTypeFunc) getUser32Function ("GetPointerType");
getPointerTouchInfo = (GetPointerTouchInfoFunc) getUser32Function ("GetPointerTouchInfo");
getPointerPenInfo = (GetPointerPenInfoFunc) getUser32Function ("GetPointerPenInfo");
canUsePointerAPI = (getPointerTypeFunction != nullptr
&& getPointerTouchInfo != nullptr
&& getPointerPenInfo != nullptr);
}
//==============================================================================
using SetProcessDPIAwareFunc = BOOL (WINAPI*) ();
using SetProcessDPIAwarenessContextFunc = BOOL (WINAPI*) (DPI_AWARENESS_CONTEXT);
using SetProcessDPIAwarenessFunc = HRESULT (WINAPI*) (DPI_Awareness);
using SetThreadDPIAwarenessContextFunc = DPI_AWARENESS_CONTEXT (WINAPI*) (DPI_AWARENESS_CONTEXT);
using GetDPIForWindowFunc = UINT (WINAPI*) (HWND);
using GetDPIForMonitorFunc = HRESULT (WINAPI*) (HMONITOR, Monitor_DPI_Type, UINT*, UINT*);
using GetSystemMetricsForDpiFunc = int (WINAPI*) (int, UINT);
using GetProcessDPIAwarenessFunc = HRESULT (WINAPI*) (HANDLE, DPI_Awareness*);
using GetWindowDPIAwarenessContextFunc = DPI_AWARENESS_CONTEXT (WINAPI*) (HWND);
using GetThreadDPIAwarenessContextFunc = DPI_AWARENESS_CONTEXT (WINAPI*) ();
using GetAwarenessFromDpiAwarenessContextFunc = DPI_Awareness (WINAPI*) (DPI_AWARENESS_CONTEXT);
using EnableNonClientDPIScalingFunc = BOOL (WINAPI*) (HWND);
static SetProcessDPIAwareFunc setProcessDPIAware = nullptr;
static SetProcessDPIAwarenessContextFunc setProcessDPIAwarenessContext = nullptr;
static SetProcessDPIAwarenessFunc setProcessDPIAwareness = nullptr;
static SetThreadDPIAwarenessContextFunc setThreadDPIAwarenessContext = nullptr;
static GetDPIForMonitorFunc getDPIForMonitor = nullptr;
static GetDPIForWindowFunc getDPIForWindow = nullptr;
static GetProcessDPIAwarenessFunc getProcessDPIAwareness = nullptr;
static GetWindowDPIAwarenessContextFunc getWindowDPIAwarenessContext = nullptr;
static GetThreadDPIAwarenessContextFunc getThreadDPIAwarenessContext = nullptr;
static GetAwarenessFromDpiAwarenessContextFunc getAwarenessFromDPIAwarenessContext = nullptr;
static EnableNonClientDPIScalingFunc enableNonClientDPIScaling = nullptr;
static bool hasCheckedForDPIAwareness = false;
static void loadDPIAwarenessFunctions()
{
setProcessDPIAware = (SetProcessDPIAwareFunc) getUser32Function ("SetProcessDPIAware");
constexpr auto shcore = "SHCore.dll";
LoadLibraryA (shcore);
const auto shcoreModule = GetModuleHandleA (shcore);
if (shcoreModule == nullptr)
return;
getDPIForMonitor = (GetDPIForMonitorFunc) GetProcAddress (shcoreModule, "GetDpiForMonitor");
setProcessDPIAwareness = (SetProcessDPIAwarenessFunc) GetProcAddress (shcoreModule, "SetProcessDpiAwareness");
#if JUCE_WIN_PER_MONITOR_DPI_AWARE
getDPIForWindow = (GetDPIForWindowFunc) getUser32Function ("GetDpiForWindow");
getProcessDPIAwareness = (GetProcessDPIAwarenessFunc) GetProcAddress (shcoreModule, "GetProcessDpiAwareness");
getWindowDPIAwarenessContext = (GetWindowDPIAwarenessContextFunc) getUser32Function ("GetWindowDpiAwarenessContext");
setThreadDPIAwarenessContext = (SetThreadDPIAwarenessContextFunc) getUser32Function ("SetThreadDpiAwarenessContext");
getThreadDPIAwarenessContext = (GetThreadDPIAwarenessContextFunc) getUser32Function ("GetThreadDpiAwarenessContext");
getAwarenessFromDPIAwarenessContext = (GetAwarenessFromDpiAwarenessContextFunc) getUser32Function ("GetAwarenessFromDpiAwarenessContext");
setProcessDPIAwarenessContext = (SetProcessDPIAwarenessContextFunc) getUser32Function ("SetProcessDpiAwarenessContext");
enableNonClientDPIScaling = (EnableNonClientDPIScalingFunc) getUser32Function ("EnableNonClientDpiScaling");
#endif
}
static void setDPIAwareness()
{
if (hasCheckedForDPIAwareness)
return;
hasCheckedForDPIAwareness = true;
if (! JUCEApplicationBase::isStandaloneApp())
return;
loadDPIAwarenessFunctions();
if (setProcessDPIAwarenessContext != nullptr
&& setProcessDPIAwarenessContext (DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2))
return;
if (setProcessDPIAwareness != nullptr && enableNonClientDPIScaling != nullptr
&& SUCCEEDED (setProcessDPIAwareness (DPI_Awareness::DPI_Awareness_Per_Monitor_Aware)))
return;
if (setProcessDPIAwareness != nullptr && getDPIForMonitor != nullptr
&& SUCCEEDED (setProcessDPIAwareness (DPI_Awareness::DPI_Awareness_System_Aware)))
return;
if (setProcessDPIAware != nullptr)
setProcessDPIAware();
}
static bool isPerMonitorDPIAwareProcess()
{
#if ! JUCE_WIN_PER_MONITOR_DPI_AWARE
return false;
#else
static bool dpiAware = []() -> bool
{
setDPIAwareness();
if (! JUCEApplication::isStandaloneApp())
return false;
if (getProcessDPIAwareness == nullptr)
return false;
DPI_Awareness context;
getProcessDPIAwareness (nullptr, &context);
return context == DPI_Awareness::DPI_Awareness_Per_Monitor_Aware;
}();
return dpiAware;
#endif
}
static bool isPerMonitorDPIAwareWindow (HWND nativeWindow)
{
#if ! JUCE_WIN_PER_MONITOR_DPI_AWARE
ignoreUnused (nativeWindow);
return false;
#else
setDPIAwareness();
if (getWindowDPIAwarenessContext != nullptr
&& getAwarenessFromDPIAwarenessContext != nullptr)
{
return (getAwarenessFromDPIAwarenessContext (getWindowDPIAwarenessContext (nativeWindow))
== DPI_Awareness::DPI_Awareness_Per_Monitor_Aware);
}
return isPerMonitorDPIAwareProcess();
#endif
}
static bool isPerMonitorDPIAwareThread (GetThreadDPIAwarenessContextFunc getThreadDPIAwarenessContextIn = getThreadDPIAwarenessContext,
GetAwarenessFromDpiAwarenessContextFunc getAwarenessFromDPIAwarenessContextIn = getAwarenessFromDPIAwarenessContext)
{
#if ! JUCE_WIN_PER_MONITOR_DPI_AWARE
return false;
#else
setDPIAwareness();
if (getThreadDPIAwarenessContextIn != nullptr
&& getAwarenessFromDPIAwarenessContextIn != nullptr)
{
return (getAwarenessFromDPIAwarenessContextIn (getThreadDPIAwarenessContextIn())
== DPI_Awareness::DPI_Awareness_Per_Monitor_Aware);
}
return isPerMonitorDPIAwareProcess();
#endif
}
static double getGlobalDPI()
{
setDPIAwareness();
ScopedDeviceContext deviceContext { nullptr };
return (GetDeviceCaps (deviceContext.dc, LOGPIXELSX) + GetDeviceCaps (deviceContext.dc, LOGPIXELSY)) / 2.0;
}
//==============================================================================
class ScopedThreadDPIAwarenessSetter::NativeImpl
{
public:
explicit NativeImpl (HWND nativeWindow)
{
ignoreUnused (nativeWindow);
#if JUCE_WIN_PER_MONITOR_DPI_AWARE
if (auto* functionSingleton = FunctionSingleton::getInstance())
{
if (! functionSingleton->isLoaded())
return;
auto dpiAwareWindow = (functionSingleton->getAwarenessFromContext (functionSingleton->getWindowAwareness (nativeWindow))
== DPI_Awareness::DPI_Awareness_Per_Monitor_Aware);
auto dpiAwareThread = (functionSingleton->getAwarenessFromContext (functionSingleton->getThreadAwareness())
== DPI_Awareness::DPI_Awareness_Per_Monitor_Aware);
if (dpiAwareWindow && ! dpiAwareThread)
oldContext = functionSingleton->setThreadAwareness (DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
else if (! dpiAwareWindow && dpiAwareThread)
oldContext = functionSingleton->setThreadAwareness (DPI_AWARENESS_CONTEXT_UNAWARE);
}
#endif
}
~NativeImpl()
{
if (oldContext != nullptr)
if (auto* functionSingleton = FunctionSingleton::getInstance())
functionSingleton->setThreadAwareness (oldContext);
}
private:
struct FunctionSingleton : public DeletedAtShutdown
{
FunctionSingleton() = default;
~FunctionSingleton() override { clearSingletonInstance(); }
SetThreadDPIAwarenessContextFunc setThreadAwareness = (SetThreadDPIAwarenessContextFunc) getUser32Function ("SetThreadDpiAwarenessContext");
GetWindowDPIAwarenessContextFunc getWindowAwareness = (GetWindowDPIAwarenessContextFunc) getUser32Function ("GetWindowDpiAwarenessContext");
GetThreadDPIAwarenessContextFunc getThreadAwareness = (GetThreadDPIAwarenessContextFunc) getUser32Function ("GetThreadDpiAwarenessContext");
GetAwarenessFromDpiAwarenessContextFunc getAwarenessFromContext = (GetAwarenessFromDpiAwarenessContextFunc) getUser32Function ("GetAwarenessFromDpiAwarenessContext");
bool isLoaded() const noexcept
{
return setThreadAwareness != nullptr
&& getWindowAwareness != nullptr
&& getThreadAwareness != nullptr
&& getAwarenessFromContext != nullptr;
}
JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL (FunctionSingleton)
JUCE_DECLARE_NON_COPYABLE (FunctionSingleton)
JUCE_DECLARE_NON_MOVEABLE (FunctionSingleton)
};
DPI_AWARENESS_CONTEXT oldContext = nullptr;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeImpl)
JUCE_DECLARE_NON_MOVEABLE (NativeImpl)
};
JUCE_IMPLEMENT_SINGLETON (ScopedThreadDPIAwarenessSetter::NativeImpl::FunctionSingleton)
ScopedThreadDPIAwarenessSetter::ScopedThreadDPIAwarenessSetter (void* nativeWindow)
{
pimpl = std::make_unique<NativeImpl> ((HWND) nativeWindow);
}
ScopedThreadDPIAwarenessSetter::~ScopedThreadDPIAwarenessSetter() = default;
ScopedDPIAwarenessDisabler::ScopedDPIAwarenessDisabler()
{
static auto localGetThreadDpiAwarenessContext = (GetThreadDPIAwarenessContextFunc) getUser32Function ("GetThreadDpiAwarenessContext");
static auto localGetAwarenessFromDpiAwarenessContextFunc = (GetAwarenessFromDpiAwarenessContextFunc) getUser32Function ("GetAwarenessFromDpiAwarenessContext");
if (! isPerMonitorDPIAwareThread (localGetThreadDpiAwarenessContext, localGetAwarenessFromDpiAwarenessContextFunc))
return;
static auto localSetThreadDPIAwarenessContext = (SetThreadDPIAwarenessContextFunc) getUser32Function ("SetThreadDpiAwarenessContext");
if (localSetThreadDPIAwarenessContext != nullptr)
{
previousContext = localSetThreadDPIAwarenessContext (DPI_AWARENESS_CONTEXT_UNAWARE);
#if JUCE_DEBUG
++numActiveScopedDpiAwarenessDisablers;
#endif
}
}
ScopedDPIAwarenessDisabler::~ScopedDPIAwarenessDisabler()
{
if (previousContext != nullptr)
{
static auto localSetThreadDPIAwarenessContext = (SetThreadDPIAwarenessContextFunc) getUser32Function ("SetThreadDpiAwarenessContext");
if (localSetThreadDPIAwarenessContext != nullptr)
localSetThreadDPIAwarenessContext ((DPI_AWARENESS_CONTEXT) previousContext);
#if JUCE_DEBUG
--numActiveScopedDpiAwarenessDisablers;
#endif
}
}
//==============================================================================
using SettingChangeCallbackFunc = void (*)(void);
extern SettingChangeCallbackFunc settingChangeCallback;
//==============================================================================
static Rectangle<int> rectangleFromRECT (RECT r) noexcept { return { r.left, r.top, r.right - r.left, r.bottom - r.top }; }
static RECT RECTFromRectangle (Rectangle<int> r) noexcept { return { r.getX(), r.getY(), r.getRight(), r.getBottom() }; }
static Point<int> pointFromPOINT (POINT p) noexcept { return { p.x, p.y }; }
static POINT POINTFromPoint (Point<int> p) noexcept { return { p.x, p.y }; }
//==============================================================================
static const Displays::Display* getCurrentDisplayFromScaleFactor (HWND hwnd);
template <typename ValueType>
static Rectangle<ValueType> convertPhysicalScreenRectangleToLogical (Rectangle<ValueType> r, HWND h) noexcept
{
if (isPerMonitorDPIAwareWindow (h))
return Desktop::getInstance().getDisplays().physicalToLogical (r, getCurrentDisplayFromScaleFactor (h));
return r;
}
template <typename ValueType>
static Rectangle<ValueType> convertLogicalScreenRectangleToPhysical (Rectangle<ValueType> r, HWND h) noexcept
{
if (isPerMonitorDPIAwareWindow (h))
return Desktop::getInstance().getDisplays().logicalToPhysical (r, getCurrentDisplayFromScaleFactor (h));
return r;
}
static Point<int> convertPhysicalScreenPointToLogical (Point<int> p, HWND h) noexcept
{
if (isPerMonitorDPIAwareWindow (h))
return Desktop::getInstance().getDisplays().physicalToLogical (p, getCurrentDisplayFromScaleFactor (h));
return p;
}
static Point<int> convertLogicalScreenPointToPhysical (Point<int> p, HWND h) noexcept
{
if (isPerMonitorDPIAwareWindow (h))
return Desktop::getInstance().getDisplays().logicalToPhysical (p, getCurrentDisplayFromScaleFactor (h));
return p;
}
JUCE_API double getScaleFactorForWindow (HWND h);
JUCE_API double getScaleFactorForWindow (HWND h)
{
// NB. Using a local function here because we need to call this method from the plug-in wrappers
// which don't load the DPI-awareness functions on startup
static GetDPIForWindowFunc localGetDPIForWindow = nullptr;
static bool hasChecked = false;
if (! hasChecked)
{
hasChecked = true;
if (localGetDPIForWindow == nullptr)
localGetDPIForWindow = (GetDPIForWindowFunc) getUser32Function ("GetDpiForWindow");
}
if (localGetDPIForWindow != nullptr)
return (double) localGetDPIForWindow (h) / USER_DEFAULT_SCREEN_DPI;
return 1.0;
}
//==============================================================================
static void setWindowPos (HWND hwnd, Rectangle<int> bounds, UINT flags, bool adjustTopLeft = false)
{
ScopedThreadDPIAwarenessSetter setter { hwnd };
if (isPerMonitorDPIAwareWindow (hwnd))
{
if (adjustTopLeft)
bounds = convertLogicalScreenRectangleToPhysical (bounds, hwnd)
.withPosition (Desktop::getInstance().getDisplays().logicalToPhysical (bounds.getTopLeft()));
else
bounds = convertLogicalScreenRectangleToPhysical (bounds, hwnd);
}
SetWindowPos (hwnd, nullptr, bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(), flags);
}
static RECT getWindowScreenRect (HWND hwnd)
{
ScopedThreadDPIAwarenessSetter setter { hwnd };
RECT rect;
GetWindowRect (hwnd, &rect);
return rect;
}
static RECT getWindowClientRect (HWND hwnd)
{
auto rect = getWindowScreenRect (hwnd);
if (auto parentH = GetParent (hwnd))
{
ScopedThreadDPIAwarenessSetter setter { hwnd };
MapWindowPoints (HWND_DESKTOP, parentH, (LPPOINT) &rect, 2);
}
return rect;
}
static void setWindowZOrder (HWND hwnd, HWND insertAfter)
{
SetWindowPos (hwnd, insertAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
}
//==============================================================================
#if ! JUCE_MINGW
extern RTL_OSVERSIONINFOW getWindowsVersionInfo();
#endif
double Desktop::getDefaultMasterScale()
{
if (! JUCEApplicationBase::isStandaloneApp() || isPerMonitorDPIAwareProcess())
return 1.0;
return getGlobalDPI() / USER_DEFAULT_SCREEN_DPI;
}
bool Desktop::canUseSemiTransparentWindows() noexcept
{
return true;
}
class Desktop::NativeDarkModeChangeDetectorImpl
{
public:
NativeDarkModeChangeDetectorImpl()
{
#if ! JUCE_MINGW
const auto winVer = getWindowsVersionInfo();
if (winVer.dwMajorVersion >= 10 && winVer.dwBuildNumber >= 17763)
{
const auto uxtheme = "uxtheme.dll";
LoadLibraryA (uxtheme);
const auto uxthemeModule = GetModuleHandleA (uxtheme);
if (uxthemeModule != nullptr)
{
shouldAppsUseDarkMode = (ShouldAppsUseDarkModeFunc) GetProcAddress (uxthemeModule, MAKEINTRESOURCEA (132));
if (shouldAppsUseDarkMode != nullptr)
darkModeEnabled = shouldAppsUseDarkMode() && ! isHighContrast();
}
}
#endif
}
~NativeDarkModeChangeDetectorImpl()
{
UnhookWindowsHookEx (hook);
}
bool isDarkModeEnabled() const noexcept { return darkModeEnabled; }
private:
static bool isHighContrast()
{
HIGHCONTRASTW highContrast {};
if (SystemParametersInfoW (SPI_GETHIGHCONTRAST, sizeof (highContrast), &highContrast, false))
return highContrast.dwFlags & HCF_HIGHCONTRASTON;
return false;
}
static LRESULT CALLBACK callWndProc (int nCode, WPARAM wParam, LPARAM lParam)
{
auto* params = reinterpret_cast<CWPSTRUCT*> (lParam);
if (nCode >= 0
&& params != nullptr
&& params->message == WM_SETTINGCHANGE
&& params->lParam != 0
&& CompareStringOrdinal (reinterpret_cast<LPWCH> (params->lParam), -1, L"ImmersiveColorSet", -1, true) == CSTR_EQUAL)
{
Desktop::getInstance().nativeDarkModeChangeDetectorImpl->colourSetChanged();
}
return CallNextHookEx ({}, nCode, wParam, lParam);
}
void colourSetChanged()
{
if (shouldAppsUseDarkMode != nullptr)
{
const auto wasDarkModeEnabled = std::exchange (darkModeEnabled, shouldAppsUseDarkMode() && ! isHighContrast());
if (darkModeEnabled != wasDarkModeEnabled)
Desktop::getInstance().darkModeChanged();
}
}
using ShouldAppsUseDarkModeFunc = bool (WINAPI*)();
ShouldAppsUseDarkModeFunc shouldAppsUseDarkMode = nullptr;
bool darkModeEnabled = false;
HHOOK hook { SetWindowsHookEx (WH_CALLWNDPROC,
callWndProc,
(HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
GetCurrentThreadId()) };
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeDarkModeChangeDetectorImpl)
};
std::unique_ptr<Desktop::NativeDarkModeChangeDetectorImpl> Desktop::createNativeDarkModeChangeDetectorImpl()
{
return std::make_unique<NativeDarkModeChangeDetectorImpl>();
}
bool Desktop::isDarkModeActive() const
{
return nativeDarkModeChangeDetectorImpl->isDarkModeEnabled();
}
Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
{
return upright;
}
int64 getMouseEventTime();
int64 getMouseEventTime()
{
static int64 eventTimeOffset = 0;
static LONG lastMessageTime = 0;
const LONG thisMessageTime = GetMessageTime();
if (thisMessageTime < lastMessageTime || lastMessageTime == 0)
{
lastMessageTime = thisMessageTime;
eventTimeOffset = Time::currentTimeMillis() - thisMessageTime;
}
return eventTimeOffset + thisMessageTime;
}
//==============================================================================
const int extendedKeyModifier = 0x10000;
const int KeyPress::spaceKey = VK_SPACE;
const int KeyPress::returnKey = VK_RETURN;
const int KeyPress::escapeKey = VK_ESCAPE;
const int KeyPress::backspaceKey = VK_BACK;
const int KeyPress::deleteKey = VK_DELETE | extendedKeyModifier;
const int KeyPress::insertKey = VK_INSERT | extendedKeyModifier;
const int KeyPress::tabKey = VK_TAB;
const int KeyPress::leftKey = VK_LEFT | extendedKeyModifier;
const int KeyPress::rightKey = VK_RIGHT | extendedKeyModifier;
const int KeyPress::upKey = VK_UP | extendedKeyModifier;
const int KeyPress::downKey = VK_DOWN | extendedKeyModifier;
const int KeyPress::homeKey = VK_HOME | extendedKeyModifier;
const int KeyPress::endKey = VK_END | extendedKeyModifier;
const int KeyPress::pageUpKey = VK_PRIOR | extendedKeyModifier;
const int KeyPress::pageDownKey = VK_NEXT | extendedKeyModifier;
const int KeyPress::F1Key = VK_F1 | extendedKeyModifier;
const int KeyPress::F2Key = VK_F2 | extendedKeyModifier;
const int KeyPress::F3Key = VK_F3 | extendedKeyModifier;
const int KeyPress::F4Key = VK_F4 | extendedKeyModifier;
const int KeyPress::F5Key = VK_F5 | extendedKeyModifier;
const int KeyPress::F6Key = VK_F6 | extendedKeyModifier;
const int KeyPress::F7Key = VK_F7 | extendedKeyModifier;
const int KeyPress::F8Key = VK_F8 | extendedKeyModifier;
const int KeyPress::F9Key = VK_F9 | extendedKeyModifier;
const int KeyPress::F10Key = VK_F10 | extendedKeyModifier;
const int KeyPress::F11Key = VK_F11 | extendedKeyModifier;
const int KeyPress::F12Key = VK_F12 | extendedKeyModifier;
const int KeyPress::F13Key = VK_F13 | extendedKeyModifier;
const int KeyPress::F14Key = VK_F14 | extendedKeyModifier;
const int KeyPress::F15Key = VK_F15 | extendedKeyModifier;
const int KeyPress::F16Key = VK_F16 | extendedKeyModifier;
const int KeyPress::F17Key = VK_F17 | extendedKeyModifier;
const int KeyPress::F18Key = VK_F18 | extendedKeyModifier;
const int KeyPress::F19Key = VK_F19 | extendedKeyModifier;
const int KeyPress::F20Key = VK_F20 | extendedKeyModifier;
const int KeyPress::F21Key = VK_F21 | extendedKeyModifier;
const int KeyPress::F22Key = VK_F22 | extendedKeyModifier;
const int KeyPress::F23Key = VK_F23 | extendedKeyModifier;
const int KeyPress::F24Key = VK_F24 | extendedKeyModifier;
const int KeyPress::F25Key = 0x31000; // Windows doesn't support F-keys 25 or higher
const int KeyPress::F26Key = 0x31001;
const int KeyPress::F27Key = 0x31002;
const int KeyPress::F28Key = 0x31003;
const int KeyPress::F29Key = 0x31004;
const int KeyPress::F30Key = 0x31005;
const int KeyPress::F31Key = 0x31006;
const int KeyPress::F32Key = 0x31007;
const int KeyPress::F33Key = 0x31008;
const int KeyPress::F34Key = 0x31009;
const int KeyPress::F35Key = 0x3100a;
const int KeyPress::numberPad0 = VK_NUMPAD0 | extendedKeyModifier;
const int KeyPress::numberPad1 = VK_NUMPAD1 | extendedKeyModifier;
const int KeyPress::numberPad2 = VK_NUMPAD2 | extendedKeyModifier;
const int KeyPress::numberPad3 = VK_NUMPAD3 | extendedKeyModifier;
const int KeyPress::numberPad4 = VK_NUMPAD4 | extendedKeyModifier;
const int KeyPress::numberPad5 = VK_NUMPAD5 | extendedKeyModifier;
const int KeyPress::numberPad6 = VK_NUMPAD6 | extendedKeyModifier;
const int KeyPress::numberPad7 = VK_NUMPAD7 | extendedKeyModifier;
const int KeyPress::numberPad8 = VK_NUMPAD8 | extendedKeyModifier;
const int KeyPress::numberPad9 = VK_NUMPAD9 | extendedKeyModifier;
const int KeyPress::numberPadAdd = VK_ADD | extendedKeyModifier;
const int KeyPress::numberPadSubtract = VK_SUBTRACT | extendedKeyModifier;
const int KeyPress::numberPadMultiply = VK_MULTIPLY | extendedKeyModifier;
const int KeyPress::numberPadDivide = VK_DIVIDE | extendedKeyModifier;
const int KeyPress::numberPadSeparator = VK_SEPARATOR | extendedKeyModifier;
const int KeyPress::numberPadDecimalPoint = VK_DECIMAL | extendedKeyModifier;
const int KeyPress::numberPadEquals = 0x92 /*VK_OEM_NEC_EQUAL*/ | extendedKeyModifier;
const int KeyPress::numberPadDelete = VK_DELETE | extendedKeyModifier;
const int KeyPress::playKey = 0x30000;
const int KeyPress::stopKey = 0x30001;
const int KeyPress::fastForwardKey = 0x30002;
const int KeyPress::rewindKey = 0x30003;
//==============================================================================
class WindowsBitmapImage : public ImagePixelData
{
public:
WindowsBitmapImage (const Image::PixelFormat format,
const int w, const int h, const bool clearImage)
: ImagePixelData (format, w, h)
{
jassert (format == Image::RGB || format == Image::ARGB);
static bool alwaysUse32Bits = isGraphicsCard32Bit(); // NB: for 32-bit cards, it's faster to use a 32-bit image.
pixelStride = (alwaysUse32Bits || format == Image::ARGB) ? 4 : 3;
lineStride = -((w * pixelStride + 3) & ~3);
zerostruct (bitmapInfo);
bitmapInfo.bV4Size = sizeof (BITMAPV4HEADER);
bitmapInfo.bV4Width = w;
bitmapInfo.bV4Height = h;
bitmapInfo.bV4Planes = 1;
bitmapInfo.bV4CSType = 1;
bitmapInfo.bV4BitCount = (unsigned short) (pixelStride * 8);
if (format == Image::ARGB)
{
bitmapInfo.bV4AlphaMask = 0xff000000;
bitmapInfo.bV4RedMask = 0xff0000;
bitmapInfo.bV4GreenMask = 0xff00;
bitmapInfo.bV4BlueMask = 0xff;
bitmapInfo.bV4V4Compression = BI_BITFIELDS;
}
else
{
bitmapInfo.bV4V4Compression = BI_RGB;
}
{
ScopedDeviceContext deviceContext { nullptr };
hdc = CreateCompatibleDC (deviceContext.dc);
}
SetMapMode (hdc, MM_TEXT);
hBitmap = CreateDIBSection (hdc, (BITMAPINFO*) &(bitmapInfo), DIB_RGB_COLORS,
(void**) &bitmapData, nullptr, 0);
if (hBitmap != nullptr)
previousBitmap = SelectObject (hdc, hBitmap);
if (format == Image::ARGB && clearImage)
zeromem (bitmapData, (size_t) std::abs (h * lineStride));
imageData = bitmapData - (lineStride * (h - 1));
}
~WindowsBitmapImage() override
{
SelectObject (hdc, previousBitmap); // Selecting the previous bitmap before deleting the DC avoids a warning in BoundsChecker
DeleteDC (hdc);
DeleteObject (hBitmap);
}
std::unique_ptr<ImageType> createType() const override { return std::make_unique<NativeImageType>(); }