FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
solvespace/src/platform/guiwin.cpp at python · KmolYuan/solvespace · GitHub
KmolYuan
solvespace
Repository navigation
Code
Issues
Pull requests
1
(1)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
solvespace
/
src
/
platform
/
guiwin.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1722 lines (1453 loc) · 60.5 KB
Breadcrumbs
solvespace
/
src
/
platform
/
guiwin.cpp
Copy path
File metadata and controls
1722 lines (1453 loc) · 60.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
-----------------------------------------------------------------------------
//
The Win32-based implementation of platform-dependent GUI functionality.
//
//
Copyright 2018 whitequark
//
-----------------------------------------------------------------------------
#
include
"
config.h
"
#
include
"
solvespace.h
"
//
Include after solvespace.h to avoid identifier clashes.
#
include
<
windows.h
>
#
include
<
windowsx.h
>
#
include
<
commctrl.h
>
#
include
<
commdlg.h
>
#
include
<
shellapi.h
>
//
Macros to compile under XP
#
if
!defined(LSTATUS)
#
define
LSTATUS
LONG
#
endif
#
if
!defined(MAPVK_VK_TO_CHAR)
#
define
MAPVK_VK_TO_CHAR
2
#
endif
#
if
!defined(USER_DEFAULT_SCREEN_DPI)
#
define
USER_DEFAULT_SCREEN_DPI
96
#
endif
#
if
!defined(TTM_POPUP)
#
define
TTM_POPUP
(
WM_USER
+
34
)
#
endif
//
End macros to compile under XP
#
if
!defined(WM_DPICHANGED)
#
define
WM_DPICHANGED
0x02E0
#
endif
//
These interfere with our identifiers.
#
undef
CreateWindow
#
undef
ERROR
#
if
HAVE_OPENGL == 3
#
define
EGLAPI
/*
static linkage
*/
#
define
EGL_EGLEXT_PROTOTYPES
#
include
<
EGL/egl.h
>
#
include
<
EGL/eglext.h
>
#
endif
#
if
defined(HAVE_SPACEWARE)
#
include
<
si.h
>
#
include
<
siapp.h
>
#
undef
uint32_t
#
endif
#
if
defined(__GNUC__)
//
Disable bogus warning emitted by GCC on GetProcAddress, since there seems to be no way
//
of restructuring the code to easily disable it just at the call site.
#
pragma
GCC diagnostic ignored "-Wcast-function-type"
#
endif
namespace
SolveSpace
{
namespace
Platform
{
//
-----------------------------------------------------------------------------
//
Windows API bridging
//
-----------------------------------------------------------------------------
#
define
sscheck
(
expr
)
do
{ \
SetLastError
(
0
); \
if
(!(expr)) \
CheckLastError
(__FILE__, __LINE__, __func__, #expr); \
}
while
(
0
)
void
CheckLastError
(
const
char
*file,
int
line,
const
char
*function,
const
char
*expr) {
if
(
GetLastError
() !=
S_OK
) {
LPWSTR
messageW;
FormatMessageW
(
FORMAT_MESSAGE_ALLOCATE_BUFFER
|
FORMAT_MESSAGE_FROM_SYSTEM
,
NULL
,
GetLastError
(),
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
),
(
LPWSTR
)&messageW,
0
,
NULL
);
std::string message;
message +=
ssprintf
(
"
File %s, line %u, function %s:
\n
"
, file, line, function);
message +=
ssprintf
(
"
Win32 API call failed: %s.
\n
"
, expr);
message +=
ssprintf
(
"
Error: %s
"
,
Narrow
(messageW).
c_str
());
FatalError
(message);
}
}
typedef
UINT
(
WINAPI
*
LPFNGETDPIFORWINDOW
)(
HWND
);
UINT
ssGetDpiForWindow
(
HWND
hwnd) {
static
bool
checked;
static
LPFNGETDPIFORWINDOW
lpfnGetDpiForWindow;
if
(!checked) {
checked =
true
;
lpfnGetDpiForWindow = (
LPFNGETDPIFORWINDOW
)
GetProcAddress
(
GetModuleHandleW
(
L"
user32.dll
"
),
"
GetDpiForWindow
"
);
}
if
(lpfnGetDpiForWindow) {
return
lpfnGetDpiForWindow
(hwnd);
}
else
{
HDC
hDc;
sscheck
(hDc =
GetDC
(
HWND_DESKTOP
));
UINT
dpi;
sscheck
(dpi =
GetDeviceCaps
(hDc,
LOGPIXELSX
));
sscheck
(
ReleaseDC
(
HWND_DESKTOP
, hDc));
return
dpi;
}
}
typedef
BOOL
(
WINAPI
*
LPFNADJUSTWINDOWRECTEXFORDPI
)(
LPRECT
,
DWORD
,
BOOL
,
DWORD
,
UINT
);
BOOL
ssAdjustWindowRectExForDpi
(
LPRECT
lpRect,
DWORD
dwStyle,
BOOL
bMenu,
DWORD
dwExStyle,
UINT
dpi) {
static
bool
checked;
static
LPFNADJUSTWINDOWRECTEXFORDPI
lpfnAdjustWindowRectExForDpi;
if
(!checked) {
checked =
true
;
lpfnAdjustWindowRectExForDpi = (
LPFNADJUSTWINDOWRECTEXFORDPI
)
GetProcAddress
(
GetModuleHandleW
(
L"
user32.dll
"
),
"
AdjustWindowRectExForDpi
"
);
}
if
(lpfnAdjustWindowRectExForDpi) {
return
lpfnAdjustWindowRectExForDpi
(lpRect, dwStyle, bMenu, dwExStyle, dpi);
}
else
{
return
AdjustWindowRectEx
(lpRect, dwStyle, bMenu, dwExStyle);
}
}
//
-----------------------------------------------------------------------------
//
Utility functions
//
-----------------------------------------------------------------------------
static
std::wstring
PrepareTitle
(
const
std::string &s) {
return
Widen
(
"
SolveSpace -
"
+ s);
}
static
std::string
NegateMnemonics
(
const
std::string &label) {
std::string newLabel;
for
(
char
c : label) {
newLabel.
push_back
(c);
if
(c ==
'
&
'
) newLabel.
push_back
(c);
}
return
newLabel;
}
static
int
Clamp
(
int
x,
int
a,
int
b,
int
brda,
int
brdb) {
//
If we are outside of an edge of the monitor
//
and a "border" is requested "move in" from that edge
//
by "b/brdX" (the "b" parameter is the resolution)
if
((x <= a) && (brda)) {
a += b / brda;
//
yes "b/brda" since b is the size
}
if
(((x >= b) && brdb)) {
b -= b / brdb;
}
return
max
(a,
min
(x, b));
}
//
-----------------------------------------------------------------------------
//
Fatal errors
//
-----------------------------------------------------------------------------
bool
handlingFatalError =
false
;
void
FatalError
(
const
std::string &message) {
//
Indicate that we're handling a fatal error, to avoid re-entering application code
//
and potentially crashing even harder.
handlingFatalError =
true
;
switch
(
MessageBoxW
(
NULL
,
Platform::Widen
(message +
"
\n
Generate debug report?
"
).
c_str
(),
L"
Fatal error — SolveSpace
"
,
MB_ICONERROR
|
MB_TASKMODAL
|
MB_SETFOREGROUND
|
MB_TOPMOST
|
MB_OKCANCEL
|
MB_DEFBUTTON2
)) {
case
IDOK
:
abort
();
case
IDCANCEL
:
default
: {
WCHAR
appPath[
MAX_PATH
] = {};
GetModuleFileNameW
(
NULL
, appPath,
sizeof
(appPath));
ShellExecuteW
(
NULL
,
L"
open
"
, appPath,
NULL
,
NULL
,
SW_SHOW
);
_exit
(
1
);
}
}
}
//
-----------------------------------------------------------------------------
//
Settings
//
-----------------------------------------------------------------------------
class
SettingsImplWin32
final
: public Settings {
public:
HKEY
hKey =
NULL
;
HKEY
GetKey
() {
if
(hKey ==
NULL
) {
sscheck
(
ERROR_SUCCESS
==
RegCreateKeyExW
(
HKEY_CURRENT_USER
,
L"
Software
\\
SolveSpace
"
,
0
,
NULL
,
0
,
KEY_ALL_ACCESS
,
NULL
, &hKey,
NULL
));
}
return
hKey;
}
~SettingsImplWin32
() {
if
(hKey !=
NULL
) {
sscheck
(
ERROR_SUCCESS
==
RegCloseKey
(hKey));
}
}
void
FreezeInt
(
const
std::string &key,
uint32_t
value) {
sscheck
(
ERROR_SUCCESS
==
RegSetValueExW
(
GetKey
(), &
Widen
(key)[
0
],
0
,
REG_DWORD
, (
const
BYTE
*)&value,
sizeof
(value)));
}
uint32_t
ThawInt
(
const
std::string &key,
uint32_t
defaultValue) {
DWORD
value;
DWORD
type, length =
sizeof
(value);
LSTATUS
result =
RegQueryValueExW
(
GetKey
(), &
Widen
(key)[
0
],
0
,
&type, (
BYTE
*)&value, &length);
if
(result ==
ERROR_SUCCESS
&& type ==
REG_DWORD
) {
return
value;
}
return
defaultValue;
}
void
FreezeFloat
(
const
std::string &key,
double
value) {
sscheck
(
ERROR_SUCCESS
==
RegSetValueExW
(
GetKey
(), &
Widen
(key)[
0
],
0
,
REG_QWORD
, (
const
BYTE
*)&value,
sizeof
(value)));
}
double
ThawFloat
(
const
std::string &key,
double
defaultValue) {
double
value;
DWORD
type, length =
sizeof
(value);
LSTATUS
result =
RegQueryValueExW
(
GetKey
(), &
Widen
(key)[
0
],
0
,
&type, (
BYTE
*)&value, &length);
if
(result ==
ERROR_SUCCESS
&& type ==
REG_QWORD
) {
return
value;
}
return
defaultValue;
}
void
FreezeString
(
const
std::string &key,
const
std::string &value) {
ssassert
(value.
length
() ==
strlen
(value.
c_str
()),
"
illegal null byte in middle of a string setting
"
);
std::wstring valueW =
Widen
(value);
sscheck
(
ERROR_SUCCESS
==
RegSetValueExW
(
GetKey
(), &
Widen
(key)[
0
],
0
,
REG_SZ
, (
const
BYTE
*)&valueW[
0
], (valueW.
length
() +
1
) *
2
));
}
std::string
ThawString
(
const
std::string &key,
const
std::string &defaultValue) {
DWORD
type, length =
0
;
LSTATUS
result =
RegQueryValueExW
(
GetKey
(), &
Widen
(key)[
0
],
0
,
&type,
NULL
, &length);
if
(result ==
ERROR_SUCCESS
&& type ==
REG_SZ
) {
std::wstring valueW;
valueW.
resize
(length /
2
-
1
);
sscheck
(
ERROR_SUCCESS
==
RegQueryValueExW
(
GetKey
(), &
Widen
(key)[
0
],
0
,
&type, (
BYTE
*)&valueW[
0
], &length));
return
Narrow
(valueW);
}
return
defaultValue;
}
};
SettingsRef
GetSettings
() {
return
std::make_shared<SettingsImplWin32>();
}
//
-----------------------------------------------------------------------------
//
Timers
//
-----------------------------------------------------------------------------
class
TimerImplWin32
final
: public Timer {
public:
static
HWND
WindowHandle
() {
static
HWND
hTimerWnd;
if
(hTimerWnd ==
NULL
) {
sscheck
(hTimerWnd =
CreateWindowExW
(
0
,
L"
Message
"
,
NULL
,
0
,
0
,
0
,
0
,
0
,
HWND_MESSAGE
,
NULL
,
NULL
,
NULL
));
}
return
hTimerWnd;
}
static
void
CALLBACK
TimerFunc
(
HWND
hwnd,
UINT
msg,
UINT_PTR
event,
DWORD
time) {
sscheck
(
KillTimer
(
WindowHandle
(), event));
TimerImplWin32 *timer = (TimerImplWin32*)event;
if
(timer->
onTimeout
) {
timer->
onTimeout
();
}
}
void
RunAfter
(
unsigned
milliseconds)
override
{
//
FIXME(platform/gui): use SetCoalescableTimer when it's available (8+)
sscheck
(
SetTimer
(
WindowHandle
(), (
UINT_PTR
)
this
,
milliseconds, &TimerImplWin32::TimerFunc));
}
~TimerImplWin32
() {
//
FIXME(platform/gui): there's a race condition here--WM_TIMER messages already
//
posted to the queue are not removed, so this destructor is at most "best effort".
KillTimer
(
WindowHandle
(), (
UINT_PTR
)
this
);
}
};
TimerRef
CreateTimer
() {
return
std::make_shared<TimerImplWin32>();
}
//
-----------------------------------------------------------------------------
//
Menus
//
-----------------------------------------------------------------------------
class
MenuImplWin32
;
class
MenuItemImplWin32
final
: public MenuItem {
public:
std::shared_ptr<MenuImplWin32> menu;
HMENU
Handle
();
MENUITEMINFOW
GetInfo
(
UINT
mask) {
MENUITEMINFOW
mii = {};
mii.
cbSize
=
sizeof
(mii);
mii.
fMask
= mask;
sscheck
(
GetMenuItemInfoW
(
Handle
(), (
UINT_PTR
)
this
,
FALSE
, &mii));
return
mii;
}
void
SetAccelerator
(KeyboardEvent accel)
override
{
MENUITEMINFOW
mii =
GetInfo
(
MIIM_TYPE
);
std::wstring
nameW
(mii.
cch
, L
'
\0
'
);
mii.
dwTypeData
= &nameW[
0
];
mii.
cch
++;
sscheck
(
GetMenuItemInfoW
(
Handle
(), (
UINT_PTR
)
this
,
FALSE
, &mii));
std::string name =
Narrow
(nameW);
if
(name.
find
(
'
\t
'
) != std::string::npos) {
name = name.
substr
(
0
, name.
find
(
'
\t
'
));
}
name +=
'
\t
'
;
name +=
AcceleratorDescription
(accel);
nameW =
Widen
(name);
mii.
fMask
=
MIIM_STRING
;
mii.
dwTypeData
= &nameW[
0
];
sscheck
(
SetMenuItemInfoW
(
Handle
(), (
UINT_PTR
)
this
,
FALSE
, &mii));
}
void
SetIndicator
(Indicator type)
override
{
MENUITEMINFOW
mii =
GetInfo
(
MIIM_FTYPE
);
switch
(type) {
case
Indicator::
NONE
:
case
Indicator::
CHECK_MARK
:
mii.
fType
&= ~
MFT_RADIOCHECK
;
break
;
case
Indicator::
RADIO_MARK
:
mii.
fType
|=
MFT_RADIOCHECK
;
break
;
}
sscheck
(
SetMenuItemInfoW
(
Handle
(), (
UINT_PTR
)
this
,
FALSE
, &mii));
}
void
SetActive
(
bool
active)
override
{
MENUITEMINFOW
mii =
GetInfo
(
MIIM_STATE
);
if
(active) {
mii.
fState
|=
MFS_CHECKED
;
}
else
{
mii.
fState
&= ~
MFS_CHECKED
;
}
sscheck
(
SetMenuItemInfoW
(
Handle
(), (
UINT_PTR
)
this
,
FALSE
, &mii));
}
void
SetEnabled
(
bool
enabled)
override
{
MENUITEMINFOW
mii =
GetInfo
(
MIIM_STATE
);
if
(enabled) {
mii.
fState
&= ~(
MFS_DISABLED
|
MFS_GRAYED
);
}
else
{
mii.
fState
|=
MFS_DISABLED
|
MFS_GRAYED
;
}
sscheck
(
SetMenuItemInfoW
(
Handle
(), (
UINT_PTR
)
this
,
FALSE
, &mii));
}
};
int64_t
contextMenuPopTime =
0
;
class
MenuImplWin32
final
: public Menu {
public:
HMENU
hMenu;
std::weak_ptr<MenuImplWin32> weakThis;
std::vector<std::shared_ptr<MenuItemImplWin32>> menuItems;
std::vector<std::shared_ptr<MenuImplWin32>> subMenus;
MenuImplWin32
() {
sscheck
(hMenu =
CreatePopupMenu
());
}
MenuItemRef
AddItem
(
const
std::string &label,
std::function<
void
()> onTrigger = NULL,
bool mnemonics = true)
override
{
auto
menuItem = std::make_shared<MenuItemImplWin32>();
menuItem->
menu
= weakThis.
lock
();
menuItem->
onTrigger
= onTrigger;
menuItems.
push_back
(menuItem);
sscheck
(
AppendMenuW
(hMenu,
MF_STRING
, (
UINT_PTR
)menuItem.
get
(),
Widen
(mnemonics ? label :
NegateMnemonics
(label)).
c_str
()));
//
uID is just an UINT, which isn't large enough to hold a pointer on 64-bit Windows,
//
so we use dwItemData, which is.
MENUITEMINFOW
mii = {};
mii.
cbSize
=
sizeof
(mii);
mii.
fMask
=
MIIM_DATA
;
mii.
dwItemData
= (
LONG_PTR
)menuItem.
get
();
sscheck
(
SetMenuItemInfoW
(hMenu, (
UINT_PTR
)menuItem.
get
(),
FALSE
, &mii));
return
menuItem;
}
MenuRef
AddSubMenu
(
const
std::string &label)
override
{
auto
subMenu = std::make_shared<MenuImplWin32>();
subMenu->
weakThis
= subMenu;
subMenus.
push_back
(subMenu);
sscheck
(
AppendMenuW
(hMenu,
MF_STRING
|
MF_POPUP
,
(
UINT_PTR
)subMenu->
hMenu
,
Widen
(label).
c_str
()));
return
subMenu;
}
void
AddSeparator
()
override
{
sscheck
(
AppendMenuW
(hMenu,
MF_SEPARATOR
,
0
,
L"
"
));
}
void
PopUp
()
override
{
MENUINFO
mi = {};
mi.
cbSize
=
sizeof
(mi);
mi.
fMask
=
MIM_APPLYTOSUBMENUS
|
MIM_STYLE
;
mi.
dwStyle
=
MNS_NOTIFYBYPOS
;
sscheck
(
SetMenuInfo
(hMenu, &mi));
POINT
pt;
sscheck
(
GetCursorPos
(&pt));
sscheck
(
TrackPopupMenu
(hMenu,
TPM_TOPALIGN
, pt.
x
, pt.
y
,
0
,
GetActiveWindow
(),
NULL
));
contextMenuPopTime =
GetMilliseconds
();
}
void
Clear
()
override
{
for
(
int
n =
GetMenuItemCount
(hMenu) -
1
; n >=
0
; n--) {
sscheck
(
RemoveMenu
(hMenu, n,
MF_BYPOSITION
));
}
menuItems.
clear
();
subMenus.
clear
();
}
~MenuImplWin32
() {
Clear
();
sscheck
(
DestroyMenu
(hMenu));
}
};
HMENU
MenuItemImplWin32::Handle
() {
return
menu->
hMenu
;
}
MenuRef
CreateMenu
() {
auto
menu = std::make_shared<MenuImplWin32>();
//
std::enable_shared_from_this fails for some reason, not sure why
menu->
weakThis
= menu;
return
menu;
}
class
MenuBarImplWin32
final
: public MenuBar {
public:
HMENU
hMenuBar;
std::vector<std::shared_ptr<MenuImplWin32>> subMenus;
MenuBarImplWin32
() {
sscheck
(hMenuBar = ::
CreateMenu
());
}
MenuRef
AddSubMenu
(
const
std::string &label)
override
{
auto
subMenu = std::make_shared<MenuImplWin32>();
subMenu->
weakThis
= subMenu;
subMenus.
push_back
(subMenu);
sscheck
(
AppendMenuW
(hMenuBar,
MF_STRING
|
MF_POPUP
,
(
UINT_PTR
)subMenu->
hMenu
,
Widen
(label).
c_str
()));
return
subMenu;
}
void
Clear
()
override
{
for
(
int
n =
GetMenuItemCount
(hMenuBar) -
1
; n >=
0
; n--) {
sscheck
(
RemoveMenu
(hMenuBar, n,
MF_BYPOSITION
));
}
subMenus.
clear
();
}
~MenuBarImplWin32
() {
Clear
();
sscheck
(
DestroyMenu
(hMenuBar));
}
};
MenuBarRef
GetOrCreateMainMenu
(
bool
*unique) {
*unique =
false
;
return
std::make_shared<MenuBarImplWin32>();
}
//
-----------------------------------------------------------------------------
//
Windows
//
-----------------------------------------------------------------------------
#
define
SCROLLBAR_UNIT
65536
class
WindowImplWin32
final
: public Window {
public:
HWND
hWindow =
NULL
;
HWND
hTooltip =
NULL
;
HWND
hEditor =
NULL
;
WNDPROC
editorWndProc =
NULL
;
#
if
HAVE_OPENGL == 1
HGLRC
hGlRc =
NULL
;
#
elif
HAVE_OPENGL == 3
static
EGLDisplay eglDisplay;
EGLSurface eglSurface =
EGL_NO_SURFACE
;
EGLContext eglContext =
EGL_NO_CONTEXT
;
#
endif
WINDOWPLACEMENT
placement = {};
int
minWidth =
0
, minHeight =
0
;
#
if
defined(HAVE_SPACEWARE)
SiOpenData sod = {};
SiHdl hSpaceWare =
SI_NO_HANDLE
;
#
endif
std::shared_ptr<MenuBarImplWin32> menuBar;
std::string tooltipText;
bool
scrollbarVisible =
false
;
static
void
RegisterWindowClass
() {
static
bool
registered;
if
(registered)
return
;
WNDCLASSEXW
wc = {};
wc.
cbSize
=
sizeof
(wc);
wc.
style
=
CS_BYTEALIGNCLIENT
|
CS_BYTEALIGNWINDOW
|
CS_OWNDC
|
CS_DBLCLKS
;
wc.
lpfnWndProc
= WndProc;
wc.
cbWndExtra
=
sizeof
(WindowImplWin32 *);
wc.
hIcon
= (
HICON
)
LoadImage
(
GetModuleHandle
(
NULL
),
MAKEINTRESOURCE
(
4000
),
IMAGE_ICON
,
32
,
32
,
0
);
wc.
hIconSm
= (
HICON
)
LoadImage
(
GetModuleHandle
(
NULL
),
MAKEINTRESOURCE
(
4000
),
IMAGE_ICON
,
16
,
16
,
0
);
wc.
hCursor
=
LoadCursorW
(
NULL
,
IDC_ARROW
);
wc.
lpszClassName
=
L"
SolveSpace
"
;
sscheck
(
RegisterClassExW
(&wc));
registered =
true
;
}
WindowImplWin32
(Window::Kind kind, std::shared_ptr<WindowImplWin32> parentWindow) {
placement.
length
=
sizeof
(placement);
RegisterWindowClass
();
HWND
hParentWindow =
NULL
;
if
(parentWindow) {
hParentWindow = parentWindow->
hWindow
;
}
DWORD
style =
WS_SIZEBOX
|
WS_CLIPCHILDREN
;
switch
(kind) {
case
Window::Kind::
TOPLEVEL
:
style |=
WS_OVERLAPPEDWINDOW
|
WS_CLIPSIBLINGS
;
break
;
case
Window::Kind::
TOOL
:
style |=
WS_POPUPWINDOW
|
WS_CAPTION
;
break
;
}
sscheck
(hWindow =
CreateWindowExW
(
0
,
L"
SolveSpace
"
,
L"
"
, style,
CW_USEDEFAULT
,
CW_USEDEFAULT
,
CW_USEDEFAULT
,
CW_USEDEFAULT
,
hParentWindow,
NULL
,
NULL
,
NULL
));
sscheck
(
SetWindowLongPtr
(hWindow,
0
, (
LONG_PTR
)
this
));
sscheck
(hTooltip =
CreateWindowExW
(
0
,
TOOLTIPS_CLASS
,
NULL
,
WS_POPUP
|
TTS_NOPREFIX
|
TTS_ALWAYSTIP
,
CW_USEDEFAULT
,
CW_USEDEFAULT
,
CW_USEDEFAULT
,
CW_USEDEFAULT
,
hWindow,
NULL
,
NULL
,
NULL
));
sscheck
(
SetWindowPos
(hTooltip,
HWND_TOPMOST
,
0
,
0
,
0
,
0
,
SWP_NOMOVE
|
SWP_NOSIZE
|
SWP_NOACTIVATE
));
TOOLINFOW
ti = {};
ti.
cbSize
=
sizeof
(ti);
ti.
uFlags
=
TTF_SUBCLASS
;
ti.
hwnd
= hWindow;
ti.
lpszText
= (
LPWSTR
)
L"
"
;
sscheck
(
SendMessageW
(hTooltip,
TTM_ADDTOOLW
,
0
, (
LPARAM
)&ti));
sscheck
(
SendMessageW
(hTooltip,
TTM_ACTIVATE
,
FALSE
,
0
));
DWORD
editorStyle =
WS_CLIPSIBLINGS
|
WS_CHILD
|
WS_TABSTOP
|
ES_AUTOHSCROLL
;
sscheck
(hEditor =
CreateWindowExW
(
WS_EX_CLIENTEDGE
,
WC_EDIT
,
L"
"
, editorStyle,
0
,
0
,
0
,
0
, hWindow,
NULL
,
NULL
,
NULL
));
sscheck
(editorWndProc =
(
WNDPROC
)
SetWindowLongPtr
(hEditor,
GWLP_WNDPROC
, (
LONG_PTR
)EditorWndProc));
HDC
hDc;
sscheck
(hDc =
GetDC
(hWindow));
#
if
HAVE_OPENGL == 1
PIXELFORMATDESCRIPTOR
pfd = {};
pfd.
nSize
=
sizeof
(
PIXELFORMATDESCRIPTOR
);
pfd.
nVersion
=
1
;
pfd.
dwFlags
=
PFD_DRAW_TO_WINDOW
|
PFD_SUPPORT_OPENGL
|
PFD_DOUBLEBUFFER
;
pfd.
dwLayerMask
=
PFD_MAIN_PLANE
;
pfd.
iPixelType
=
PFD_TYPE_RGBA
;
pfd.
cColorBits
=
32
;
pfd.
cDepthBits
=
24
;
pfd.
cAccumBits
=
0
;
pfd.
cStencilBits
=
0
;
int
pixelFormat;
sscheck
(pixelFormat =
ChoosePixelFormat
(hDc, &pfd));
sscheck
(
SetPixelFormat
(hDc, pixelFormat, &pfd));
sscheck
(hGlRc =
wglCreateContext
(hDc));
#
elif
HAVE_OPENGL == 3
if
(eglDisplay ==
EGL_NO_DISPLAY
) {
ssassert
(
eglBindAPI
(
EGL_OPENGL_ES_API
),
"
Cannot bind EGL API
"
);
EGLBoolean initialized =
EGL_FALSE
;
for
(
auto
&platformType : {
//
Try platform types from least to most amount of software translation required.
std::make_pair
(
"
OpenGL ES
"
,
EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE
),
std::make_pair
(
"
OpenGL
"
,
EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE
),
std::make_pair
(
"
Direct3D 11
"
,
EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE
),
std::make_pair
(
"
Direct3D 9
"
,
EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE
),
}) {
dbp
(
"
Initializing ANGLE with %s backend
"
, platformType.
first
);
EGLint displayAttributes[] = {
EGL_PLATFORM_ANGLE_TYPE_ANGLE
, platformType.
second
,
EGL_NONE
};
eglDisplay =
eglGetPlatformDisplayEXT
(
EGL_PLATFORM_ANGLE_ANGLE
, hDc,
displayAttributes);
if
(eglDisplay !=
EGL_NO_DISPLAY
) {
initialized =
eglInitialize
(eglDisplay,
NULL
,
NULL
);
if
(initialized)
break
;
eglTerminate
(eglDisplay);
}
}
ssassert
(initialized,
"
Cannot find a suitable EGL display
"
);
}
EGLint configAttributes[] = {
EGL_COLOR_BUFFER_TYPE
,
EGL_RGB_BUFFER
,
EGL_RED_SIZE
,
8
,
EGL_GREEN_SIZE
,
8
,
EGL_BLUE_SIZE
,
8
,
EGL_DEPTH_SIZE
,
24
,
EGL_RENDERABLE_TYPE
,
EGL_OPENGL_ES2_BIT
,
EGL_SURFACE_TYPE
,
EGL_WINDOW_BIT
,
EGL_NONE
};
EGLint numConfigs;
EGLConfig windowConfig;
ssassert
(
eglChooseConfig
(eglDisplay, configAttributes, &windowConfig,
1
, &numConfigs),
"
Cannot choose EGL configuration
"
);
EGLint surfaceAttributes[] = {
EGL_NONE
};
eglSurface =
eglCreateWindowSurface
(eglDisplay, windowConfig, hWindow, surfaceAttributes);
ssassert
(eglSurface !=
EGL_NO_SURFACE
,
"
Cannot create EGL window surface
"
);
EGLint contextAttributes[] = {
EGL_CONTEXT_CLIENT_VERSION
,
2
,
EGL_NONE
};
eglContext =
eglCreateContext
(eglDisplay, windowConfig,
NULL
, contextAttributes);
ssassert
(eglContext !=
EGL_NO_CONTEXT
,
"
Cannot create EGL context
"
);
#
endif
sscheck
(
ReleaseDC
(hWindow, hDc));
}
~WindowImplWin32
() {
//
Make sure any of our child windows get destroyed before we call DestroyWindow, or their
//
own destructors may fail.
menuBar.
reset
();
sscheck
(
DestroyWindow
(hWindow));
#
if
defined(HAVE_SPACEWARE)
if
(hSpaceWare !=
SI_NO_HANDLE
) {
SiClose
(hSpaceWare);
}
#
endif
}
static
LRESULT
CALLBACK
WndProc
(
HWND
h,
UINT
msg,
WPARAM
wParam,
LPARAM
lParam) {
if
(handlingFatalError)
return
TRUE
;
WindowImplWin32 *window;
sscheck
(window = (WindowImplWin32 *)
GetWindowLongPtr
(h,
0
));
//
The wndproc may be called from within CreateWindowEx, and before we've associated
//
the window with the WindowImplWin32. In that case, just defer to the default wndproc.
if
(window ==
NULL
) {
return
DefWindowProcW
(h, msg, wParam, lParam);
}
#
if
defined(HAVE_SPACEWARE)
if
(window->
hSpaceWare
!=
SI_NO_HANDLE
) {
SiGetEventData sged;
SiGetEventWinInit
(&sged, msg, wParam, lParam);
SiSpwEvent sse;
if
(
SiGetEvent
(window->
hSpaceWare
,
0
, &sged, &sse) ==
SI_IS_EVENT
) {
SixDofEvent event = {};
event.
shiftDown
= ((
GetAsyncKeyState
(
VK_SHIFT
) &
0x8000
) !=
0
);
event.
controlDown
= ((
GetAsyncKeyState
(
VK_SHIFT
) &
0x8000
) !=
0
);
if
(sse.
type
==
SI_MOTION_EVENT
) {
//
The Z axis translation and rotation are both
//
backwards in the default mapping.
event.
type
= SixDofEvent::Type::
MOTION
;
event.
translationX
= sse.
u
.
spwData
.
mData
[
SI_TX
]*
1.0
,
event.
translationY
= sse.
u
.
spwData
.
mData
[
SI_TY
]*
1.0
,
event.
translationZ
= -sse.
u
.
spwData
.
mData
[
SI_TZ
]*
1.0
,
event.
rotationX
= sse.
u
.
spwData
.
mData
[
SI_RX
]*
0.001
,
event.
rotationY
= sse.
u
.
spwData
.
mData
[
SI_RY
]*
0.001
,
event.
rotationZ
= -sse.
u
.
spwData
.
mData
[
SI_RZ
]*
0.001
;
}
else
if
(sse.
type
==
SI_BUTTON_EVENT
) {
if
(
SiButtonPressed
(&sse) ==
SI_APP_FIT_BUTTON
) {
event.
type
= SixDofEvent::Type::
PRESS
;
event.
button
= SixDofEvent::Button::
FIT
;
}
if
(
SiButtonReleased
(&sse) ==
SI_APP_FIT_BUTTON
) {
event.
type
= SixDofEvent::Type::
RELEASE
;
event.
button
= SixDofEvent::Button::
FIT
;
}
}
else
{
return
0
;
}
if
(window->
onSixDofEvent
) {
window->
onSixDofEvent
(event);
}
return
0
;
}
}
#
endif
switch
(msg) {
case
WM_ERASEBKGND
:
break
;
case
WM_PAINT
: {
PAINTSTRUCT
ps;
HDC
hDc =
BeginPaint
(window->
hWindow
, &ps);
if
(window->
onRender
) {
#
if
HAVE_OPENGL == 1
wglMakeCurrent
(hDc, window->
hGlRc
);
#
elif
HAVE_OPENGL == 3
eglMakeCurrent
(window->
eglDisplay
, window->
eglSurface
,
window->
eglSurface
, window->
eglContext
);
#
endif
window->
onRender
();
#
if
HAVE_OPENGL == 1
SwapBuffers
(hDc);
#
elif
HAVE_OPENGL == 3
eglSwapBuffers
(window->
eglDisplay
, window->
eglSurface
);
(
void
)hDc;
#
endif
}
EndPaint
(window->
hWindow
, &ps);
break
;
}
case
WM_CLOSE
:
if
(window->
onClose
) {
window->
onClose
();
}
break
;
case
WM_SIZE
:
window->
Invalidate
();
break
;
case
WM_SIZING
: {
double
pixelRatio = window->
GetDevicePixelRatio
();
RECT
rcw, rcc;
sscheck
(
GetWindowRect
(window->
hWindow
, &rcw));
sscheck
(
GetClientRect
(window->
hWindow
, &rcc));
int
nonClientWidth = (rcw.
right
- rcw.
left
) - (rcc.
right
- rcc.
left
);
int
nonClientHeight = (rcw.
bottom
- rcw.
top
) - (rcc.
bottom
- rcc.
top
);
RECT
*rc = (
RECT
*)lParam;
int
adjWidth = rc->
right
- rc->
left
;
int
adjHeight = rc->
bottom
- rc->
top
;
adjWidth -= nonClientWidth;
adjWidth =
max
((
int
)(window->
minWidth
* pixelRatio), adjWidth);
adjWidth += nonClientWidth;
adjHeight -= nonClientHeight;
adjHeight =
max
((
int
)(window->
minHeight
* pixelRatio), adjHeight);
adjHeight += nonClientHeight;
switch
(wParam) {
case
WMSZ_RIGHT
:
case
WMSZ_BOTTOMRIGHT
:
case
WMSZ_TOPRIGHT
:
rc->
right
= rc->
left
+ adjWidth;
break
;
case
WMSZ_LEFT
:
case
WMSZ_BOTTOMLEFT
:
case
WMSZ_TOPLEFT
:
rc->
left
= rc->
right
- adjWidth;
break
;
}
switch
(wParam) {
case
WMSZ_BOTTOM
:
case
WMSZ_BOTTOMLEFT
:
case
WMSZ_BOTTOMRIGHT
:
rc->
bottom
= rc->
top
+ adjHeight;
break
;
case
WMSZ_TOP
:
case
WMSZ_TOPLEFT
:
case
WMSZ_TOPRIGHT
:
rc->
top
= rc->
bottom
- adjHeight;
break
;
}
break
;
}
case
WM_DPICHANGED
: {
RECT
rc = *(
RECT
*)lParam;
sscheck
(
SendMessage
(window->
hWindow
,
WM_SIZING
,
WMSZ_BOTTOMRIGHT
, (
LPARAM
)&rc));
sscheck
(
SetWindowPos
(window->
hWindow
,
NULL
, rc.
left
, rc.
top
,
rc.
right
- rc.
left
, rc.
bottom
- rc.
top
,
SWP_NOZORDER
|
SWP_NOACTIVATE
));
window->
Invalidate
();
break
;
}
case
WM_LBUTTONDOWN
:
case
WM_MBUTTONDOWN
:
case
WM_RBUTTONDOWN
:
case
WM_LBUTTONDBLCLK
:
case
WM_MBUTTONDBLCLK
:
case
WM_RBUTTONDBLCLK
:
case
WM_LBUTTONUP
:
case
WM_MBUTTONUP
:
case
WM_RBUTTONUP
:
if
(
GetMilliseconds
() - Platform::contextMenuPopTime <
100
) {
//
Ignore the mouse click that dismisses a context menu, to avoid
//
(e.g.) clearing a selection.
return
0
;
}
//
fallthrough
case
WM_MOUSEMOVE
:
case
WM_MOUSEWHEEL
:
case
WM_MOUSELEAVE
: {
double
pixelRatio = window->
GetDevicePixelRatio
();
MouseEvent event = {};
event.
x
=
GET_X_LPARAM
(lParam) / pixelRatio;
event.
y
=
GET_Y_LPARAM
(lParam) / pixelRatio;
event.
button
= MouseEvent::Button::
NONE
;
event.
shiftDown
= (wParam &
MK_SHIFT
) !=
0
;
event.
controlDown
= (wParam &
MK_CONTROL
) !=
0
;
bool
consumed =
false
;
switch
(msg) {
case
WM_LBUTTONDOWN
:
event.
button
= MouseEvent::Button::
LEFT
;
event.
type
= MouseEvent::Type::
PRESS
;
break
;
case
WM_MBUTTONDOWN
:
event.
button
= MouseEvent::Button::
MIDDLE
;
event.
type
= MouseEvent::Type::
PRESS
;
break
;
case
WM_RBUTTONDOWN
:
event.
button
= MouseEvent::Button::
RIGHT
;
event.
type
= MouseEvent::Type::
PRESS
;
break
;
case
WM_LBUTTONDBLCLK
:
event.
button
= MouseEvent::Button::
LEFT
;
event.
type
= MouseEvent::Type::
DBL_PRESS
;
break
;
case
WM_MBUTTONDBLCLK
:
event.
button
= MouseEvent::Button::
MIDDLE
;
event.
type
= MouseEvent::Type::
DBL_PRESS
;
break
;
case
WM_RBUTTONDBLCLK
:
event.
button
= MouseEvent::Button::
RIGHT
;
event.
type
= MouseEvent::Type::
DBL_PRESS
;
break
;
case
WM_LBUTTONUP
:
event.
button
= MouseEvent::Button::
LEFT
;
event.
type
= MouseEvent::Type::
RELEASE
;
break
;
case
WM_MBUTTONUP
:
event.
button
= MouseEvent::Button::
MIDDLE
;
event.
type
= MouseEvent::Type::
RELEASE
;
break
;
case
WM_RBUTTONUP
:
event.
button
= MouseEvent::Button::
RIGHT
;
event.
type
= MouseEvent::Type::
RELEASE
;
break
;
case
WM_MOUSEWHEEL
:
//
Make the mousewheel work according to which window the mouse is
//
over, not according to which window is active.
POINT
pt;
pt.
x
=
GET_X_LPARAM
(lParam);
pt.
y
=
GET_Y_LPARAM
(lParam);
HWND
hWindowUnderMouse;
sscheck
(hWindowUnderMouse =
WindowFromPoint
(pt));
if
(hWindowUnderMouse && hWindowUnderMouse != h) {
SendMessageW
(hWindowUnderMouse, msg, wParam, lParam);
consumed =
true
;
break
;
}
//
Convert the mouse coordinates from screen to client area so that
//
scroll wheel zooming remains centered irrespective of the window
//
position.
ScreenToClient
(hWindowUnderMouse, &pt);
event.
x
= pt.
x
/ pixelRatio;
event.
y
= pt.
y
/ pixelRatio;
event.
type
= MouseEvent::Type::
SCROLL_VERT
;
event.
scrollDelta
=
GET_WHEEL_DELTA_WPARAM
(wParam) / (
double
)
WHEEL_DELTA
;
break
;
case
WM_MOUSELEAVE
:
event.
type
= MouseEvent::Type::
LEAVE
;
break
;
case
WM_MOUSEMOVE
: {
event.
type
= MouseEvent::Type::
MOTION
;
if
(wParam &
MK_LBUTTON
) {
event.
button
= MouseEvent::Button::
LEFT
;
}
else
if
(wParam &
MK_MBUTTON
) {
event.
button
= MouseEvent::Button::
MIDDLE
;
}
else
if
(wParam &
MK_RBUTTON
) {
event.
button
= MouseEvent::Button::
RIGHT
;
}
//
We need this in order to get the WM_MOUSELEAVE
TRACKMOUSEEVENT
tme = {};
tme.
cbSize
=
sizeof
(tme);
tme.
dwFlags
=
TME_LEAVE
;
tme.
hwndTrack
= window->
hWindow
;
sscheck
(
TrackMouseEvent
(&tme));
break
;
}
}
if
(!consumed && window->
onMouseEvent
) {
window->
onMouseEvent
(event);
}
break
;
}
case
WM_KEYDOWN
:
case
WM_KEYUP
: {
Platform::KeyboardEvent event = {};
if
(msg ==
WM_KEYDOWN
) {
event.
type
= Platform::KeyboardEvent::Type::
PRESS
;
}
else
if
(msg ==
WM_KEYUP
) {
event.
type
= Platform::KeyboardEvent::Type::
RELEASE
;
}
if
(
GetKeyState
(
VK_SHIFT
) &
0x8000
)
event.
shiftDown
=
true
;
if
(
GetKeyState
(
VK_CONTROL
) &
0x8000
)
event.
controlDown
=
true
;
if
(wParam >=
VK_F1
&& wParam <=
VK_F12
) {
event.
key
= Platform::KeyboardEvent::Key::
FUNCTION
;
event.
num
= wParam -
VK_F1
+
1
;
}
else
{
event.
key
= Platform::KeyboardEvent::Key::
CHARACTER
;
event.
chr
=
tolower
(
MapVirtualKeyW
(wParam,
MAPVK_VK_TO_CHAR
));
if
(event.
chr
==
0
) {
if
(wParam ==
VK_DELETE
) {
event.
chr
=
'
\x7f
'
;
}
else
{
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL