Skip to content
Navigation Menu
{{ message }}
forked from solvespace/solvespace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw32main.cpp
More file actions
1554 lines (1342 loc) · 47.9 KB
/
Copy pathw32main.cpp
File metadata and controls
1554 lines (1342 loc) · 47.9 KB
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
//-----------------------------------------------------------------------------
// Our WinMain() functions, and Win32-specific stuff to set up our windows
// and otherwise handle our interface to the operating system. Everything
// outside platform/... should be standard C++ and gl.
//
// Copyright 2008-2013 Jonathan Westhues.
//-----------------------------------------------------------------------------
#include <time.h>
#include "config.h"
#include "solvespace.h"
// Include after solvespace.h to avoid identifier clashes.
#include <windows.h>
#include <shellapi.h>
#include <commctrl.h>
#include <commdlg.h>
#ifdef HAVE_SPACEWARE
# include <si.h>
# include <siapp.h>
# undef uint32_t // thanks but no thanks
#endif
#if HAVE_OPENGL == 2
#define EGLAPI /*static linkage*/
#include <EGL/egl.h>
#endif
HINSTANCE Instance;
HWND TextWnd;
HWND TextWndScrollBar;
HWND TextEditControl;
#if HAVE_OPENGL == 2
EGLDisplay TextGlDisplay;
EGLSurface TextGlSurface;
EGLContext TextGlContext;
#else
HGLRC TextGl;
#endif
HWND GraphicsWnd;
HWND GraphicsEditControl;
#if HAVE_OPENGL == 2
EGLDisplay GraphicsGlDisplay;
EGLSurface GraphicsGlSurface;
EGLContext GraphicsGlContext;
#else
HGLRC GraphicsGl;
#endif
static struct {
int x, y;
} LastMousePos;
HMENU SubMenus[100];
HMENU RecentOpenMenu, RecentImportMenu;
HMENU ContextMenu, ContextSubmenu;
int ClientIsSmallerBy;
HFONT FixedFont;
#ifdef HAVE_SPACEWARE
// The 6-DOF input device.
SiHdl SpaceNavigator = SI_NO_HANDLE;
#endif
//-----------------------------------------------------------------------------
// Routines to display message boxes on screen. Do our own, instead of using
// MessageBox, because that is not consistent from version to version and
// there's word wrap problems.
//-----------------------------------------------------------------------------
HWND MessageWnd, OkButton;
bool MessageDone;
int MessageWidth, MessageHeight;
const char *MessageString;
static LRESULT CALLBACK MessageProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam)
{
switch (msg) {
case WM_COMMAND:
if((HWND)lParam == OkButton && wParam == BN_CLICKED) {
MessageDone = true;
}
break;
case WM_CLOSE:
case WM_DESTROY:
MessageDone = true;
break;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, FixedFont);
SetTextColor(hdc, 0x000000);
SetBkMode(hdc, TRANSPARENT);
RECT rc;
SetRect(&rc, 10, 10, MessageWidth, MessageHeight);
std::wstring text = Widen(MessageString);
DrawText(hdc, text.c_str(), text.length(), &rc, DT_LEFT | DT_WORDBREAK);
EndPaint(hwnd, &ps);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 1;
}
HWND CreateWindowClient(DWORD exStyle, const wchar_t *className, const wchar_t *windowName,
DWORD style, int x, int y, int width, int height, HWND parent,
HMENU menu, HINSTANCE instance, void *param)
{
HWND h = CreateWindowExW(exStyle, className, windowName, style, x, y,
width, height, parent, menu, instance, param);
RECT r;
GetClientRect(h, &r);
width = width - (r.right - width);
height = height - (r.bottom - height);
SetWindowPos(h, HWND_TOP, x, y, width, height, 0);
return h;
}
void SolveSpace::DoMessageBox(const char *str, int rows, int cols, bool error)
{
EnableWindow(GraphicsWnd, false);
EnableWindow(TextWnd, false);
// Register the window class for our dialog.
WNDCLASSEX wc = {};
wc.cbSize = sizeof(wc);
wc.style = CS_BYTEALIGNCLIENT | CS_BYTEALIGNWINDOW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)MessageProc;
wc.hInstance = Instance;
wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
wc.lpszClassName = L"MessageWnd";
wc.lpszMenuName = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = (HICON)LoadImage(Instance, MAKEINTRESOURCE(4000),
IMAGE_ICON, 32, 32, 0);
wc.hIconSm = (HICON)LoadImage(Instance, MAKEINTRESOURCE(4000),
IMAGE_ICON, 16, 16, 0);
RegisterClassEx(&wc);
// Create the window.
MessageString = str;
RECT r;
GetWindowRect(GraphicsWnd, &r);
const char *title = error ? "SolveSpace - Error" : "SolveSpace - Message";
int width = cols*SS.TW.CHAR_WIDTH + 20,
height = rows*SS.TW.LINE_HEIGHT + 60;
MessageWidth = width;
MessageHeight = height;
MessageWnd = CreateWindowClient(0, L"MessageWnd", Widen(title).c_str(),
WS_OVERLAPPED | WS_SYSMENU,
r.left + 100, r.top + 100, width, height, NULL, NULL, Instance, NULL);
OkButton = CreateWindowExW(0, WC_BUTTON, L"OK",
WS_CHILD | WS_TABSTOP | WS_CLIPSIBLINGS | WS_VISIBLE | BS_DEFPUSHBUTTON,
(width - 70)/2, rows*SS.TW.LINE_HEIGHT + 20,
70, 25, MessageWnd, NULL, Instance, NULL);
SendMessage(OkButton, WM_SETFONT, (WPARAM)FixedFont, true);
ShowWindow(MessageWnd, true);
SetFocus(OkButton);
MSG msg;
DWORD ret;
MessageDone = false;
while((ret = GetMessage(&msg, NULL, 0, 0)) != 0 && !MessageDone) {
if((msg.message == WM_KEYDOWN &&
(msg.wParam == VK_RETURN ||
msg.wParam == VK_ESCAPE)) ||
(msg.message == WM_KEYUP &&
(msg.wParam == VK_SPACE)))
{
MessageDone = true;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
MessageString = NULL;
EnableWindow(TextWnd, true);
EnableWindow(GraphicsWnd, true);
SetForegroundWindow(GraphicsWnd);
DestroyWindow(MessageWnd);
}
void SolveSpace::AddContextMenuItem(const char *label, ContextCommand cmd)
{
if(!ContextMenu) ContextMenu = CreatePopupMenu();
if(cmd == ContextCommand::SUBMENU) {
AppendMenuW(ContextMenu, MF_STRING | MF_POPUP,
(UINT_PTR)ContextSubmenu, Widen(label).c_str());
ContextSubmenu = NULL;
} else {
HMENU m = ContextSubmenu ? ContextSubmenu : ContextMenu;
if(cmd == ContextCommand::SEPARATOR) {
AppendMenuW(m, MF_SEPARATOR, 0, L"");
} else {
AppendMenuW(m, MF_STRING, (uint32_t)cmd, Widen(label).c_str());
}
}
}
void SolveSpace::CreateContextSubmenu()
{
ContextSubmenu = CreatePopupMenu();
}
ContextCommand SolveSpace::ShowContextMenu()
{
POINT p;
GetCursorPos(&p);
int r = TrackPopupMenu(ContextMenu,
TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_TOPALIGN,
p.x, p.y, 0, GraphicsWnd, NULL);
DestroyMenu(ContextMenu);
ContextMenu = NULL;
return (ContextCommand)r;
}
void CALLBACK TimerCallback(HWND hwnd, UINT msg, UINT_PTR id, DWORD time)
{
// The timer is periodic, so needs to be killed explicitly.
KillTimer(GraphicsWnd, 1);
SS.GW.TimerCallback();
SS.TW.TimerCallback();
}
void SolveSpace::SetTimerFor(int milliseconds)
{
SetTimer(GraphicsWnd, 1, milliseconds, TimerCallback);
}
void SolveSpace::ScheduleLater()
{
}
static void CALLBACK AutosaveCallback(HWND hwnd, UINT msg, UINT_PTR id, DWORD time)
{
KillTimer(GraphicsWnd, 1);
SS.Autosave();
}
void SolveSpace::SetAutosaveTimerFor(int minutes)
{
SetTimer(GraphicsWnd, 2, minutes * 60 * 1000, AutosaveCallback);
}
static void GetWindowSize(HWND hwnd, int *w, int *h)
{
RECT r;
GetClientRect(hwnd, &r);
*w = r.right - r.left;
*h = r.bottom - r.top;
}
void SolveSpace::GetGraphicsWindowSize(int *w, int *h)
{
GetWindowSize(GraphicsWnd, w, h);
}
void SolveSpace::GetTextWindowSize(int *w, int *h)
{
GetWindowSize(TextWnd, w, h);
}
double SolveSpace::GetScreenDpi() {
HDC hdc = GetDC(NULL);
double dpi = GetDeviceCaps(hdc, LOGPIXELSX);
ReleaseDC(NULL, hdc);
return dpi;
}
void SolveSpace::OpenWebsite(const char *url) {
ShellExecuteW(GraphicsWnd, L"open", Widen(url).c_str(), NULL, NULL, SW_SHOWNORMAL);
}
void SolveSpace::ExitNow() {
PostQuitMessage(0);
}
//-----------------------------------------------------------------------------
// Helpers so that we can read/write registry keys from the platform-
// independent code.
//-----------------------------------------------------------------------------
inline int CLAMP(int v, int a, int b) {
// Clamp it to the range [a, b]
if(v <= a) return a;
if(v >= b) return b;
return v;
}
static HKEY GetRegistryKey()
{
HKEY Software;
if(RegOpenKeyExW(HKEY_CURRENT_USER, L"Software", 0,
KEY_ALL_ACCESS, &Software) != ERROR_SUCCESS)
return NULL;
HKEY SolveSpace;
if(RegCreateKeyExW(Software, L"SolveSpace", 0, NULL, 0,
KEY_ALL_ACCESS, NULL, &SolveSpace, NULL) != ERROR_SUCCESS)
return NULL;
RegCloseKey(Software);
return SolveSpace;
}
void SolveSpace::CnfFreezeInt(uint32_t val, const std::string &name)
{
HKEY SolveSpace = GetRegistryKey();
RegSetValueExW(SolveSpace, &Widen(name)[0], 0,
REG_DWORD, (const BYTE*) &val, sizeof(DWORD));
RegCloseKey(SolveSpace);
}
void SolveSpace::CnfFreezeFloat(float val, const std::string &name)
{
static_assert(sizeof(float) == sizeof(DWORD),
"sizes of float and DWORD must match");
HKEY SolveSpace = GetRegistryKey();
RegSetValueExW(SolveSpace, &Widen(name)[0], 0,
REG_DWORD, (const BYTE*) &val, sizeof(DWORD));
RegCloseKey(SolveSpace);
}
void SolveSpace::CnfFreezeString(const std::string &str, const std::string &name)
{
HKEY SolveSpace = GetRegistryKey();
std::wstring strW = Widen(str);
RegSetValueExW(SolveSpace, &Widen(name)[0], 0,
REG_SZ, (const BYTE*) &strW[0], (strW.length() + 1) * 2);
RegCloseKey(SolveSpace);
}
static void FreezeWindowPos(HWND hwnd, const std::string &name)
{
RECT r;
GetWindowRect(hwnd, &r);
CnfFreezeInt(r.left, name + "_left");
CnfFreezeInt(r.right, name + "_right");
CnfFreezeInt(r.top, name + "_top");
CnfFreezeInt(r.bottom, name + "_bottom");
CnfFreezeInt(IsZoomed(hwnd), name + "_maximized");
}
uint32_t SolveSpace::CnfThawInt(uint32_t val, const std::string &name)
{
HKEY SolveSpace = GetRegistryKey();
DWORD type, newval, len = sizeof(DWORD);
LONG result = RegQueryValueEx(SolveSpace, &Widen(name)[0], NULL,
&type, (BYTE*) &newval, &len);
RegCloseKey(SolveSpace);
if(result == ERROR_SUCCESS && type == REG_DWORD)
return newval;
else
return val;
}
float SolveSpace::CnfThawFloat(float val, const std::string &name)
{
HKEY SolveSpace = GetRegistryKey();
DWORD type, len = sizeof(DWORD);
float newval;
LONG result = RegQueryValueExW(SolveSpace, &Widen(name)[0], NULL,
&type, (BYTE*) &newval, &len);
RegCloseKey(SolveSpace);
if(result == ERROR_SUCCESS && type == REG_DWORD)
return newval;
else
return val;
}
std::string SolveSpace::CnfThawString(const std::string &val, const std::string &name)
{
HKEY SolveSpace = GetRegistryKey();
DWORD type, len;
if(RegQueryValueExW(SolveSpace, &Widen(name)[0], NULL,
&type, NULL, &len) != ERROR_SUCCESS || type != REG_SZ) {
RegCloseKey(SolveSpace);
return val;
}
std::wstring newval;
newval.resize(len / 2 - 1);
if(RegQueryValueExW(SolveSpace, &Widen(name)[0], NULL,
NULL, (BYTE*) &newval[0], &len) != ERROR_SUCCESS) {
RegCloseKey(SolveSpace);
return val;
}
RegCloseKey(SolveSpace);
return Narrow(newval);
}
static void ThawWindowPos(HWND hwnd, const std::string &name)
{
RECT r;
GetWindowRect(hwnd, &r);
r.left = CnfThawInt(r.left, name + "_left");
r.right = CnfThawInt(r.right, name + "_right");
r.top = CnfThawInt(r.top, name + "_top");
r.bottom = CnfThawInt(r.bottom, name + "_bottom");
HMONITOR hMonitor = MonitorFromRect(&r, MONITOR_DEFAULTTONEAREST);;
MONITORINFO mi;
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);
// If it somehow ended up off-screen, then put it back.
RECT dr = mi.rcMonitor;
r.left = CLAMP(r.left, dr.left, dr.right);
r.right = CLAMP(r.right, dr.left, dr.right);
r.top = CLAMP(r.top, dr.top, dr.bottom);
r.bottom = CLAMP(r.bottom, dr.top, dr.bottom);
MoveWindow(hwnd, r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE);
if(CnfThawInt(FALSE, name + "_maximized"))
ShowWindow(hwnd, SW_MAXIMIZE);
}
void SolveSpace::SetCurrentFilename(const std::string &filename) {
if(!filename.empty()) {
SetWindowTextW(GraphicsWnd, Widen("SolveSpace - " + filename).c_str());
} else {
SetWindowTextW(GraphicsWnd, L"SolveSpace - (not yet saved)");
}
}
void SolveSpace::SetMousePointerToHand(bool yes) {
SetCursor(LoadCursor(NULL, yes ? IDC_HAND : IDC_ARROW));
}
static void PaintTextWnd()
{
#if HAVE_OPENGL == 2
eglMakeCurrent(TextGlDisplay, TextGlSurface, TextGlSurface, TextGlContext);
SS.TW.Paint();
eglSwapBuffers(TextGlDisplay, TextGlSurface);
// Leave the graphics window context active, except when we're painting
// this text window.
eglMakeCurrent(GraphicsGlDisplay, GraphicsGlSurface, GraphicsGlSurface, GraphicsGlContext);
#else
wglMakeCurrent(GetDC(TextWnd), TextGl);
SS.TW.Paint();
SwapBuffers(GetDC(TextWnd));
// Leave the graphics window context active, except when we're painting
// this text window.
wglMakeCurrent(GetDC(GraphicsWnd), GraphicsGl);
#endif
}
void SolveSpace::MoveTextScrollbarTo(int pos, int maxPos, int page)
{
SCROLLINFO si = {};
si.cbSize = sizeof(si);
si.fMask = SIF_DISABLENOSCROLL | SIF_ALL;
si.nMin = 0;
si.nMax = maxPos;
si.nPos = pos;
si.nPage = page;
SetScrollInfo(TextWndScrollBar, SB_CTL, &si, true);
}
void HandleTextWindowScrollBar(WPARAM wParam, LPARAM lParam)
{
int maxPos, minPos, pos;
GetScrollRange(TextWndScrollBar, SB_CTL, &minPos, &maxPos);
pos = GetScrollPos(TextWndScrollBar, SB_CTL);
switch(LOWORD(wParam)) {
case SB_LINEUP: pos--; break;
case SB_PAGEUP: pos -= 4; break;
case SB_LINEDOWN: pos++; break;
case SB_PAGEDOWN: pos += 4; break;
case SB_TOP: pos = 0; break;
case SB_BOTTOM: pos = maxPos; break;
case SB_THUMBTRACK:
case SB_THUMBPOSITION: pos = HIWORD(wParam); break;
}
SS.TW.ScrollbarEvent(pos);
}
static void MouseWheel(int thisDelta) {
static int DeltaAccum;
int delta = 0;
// Handle mouse deltas of less than 120 (like from an un-detented mouse
// wheel) correctly, even though no one ever uses those.
DeltaAccum += thisDelta;
while(DeltaAccum >= 120) {
DeltaAccum -= 120;
delta += 120;
}
while(DeltaAccum <= -120) {
DeltaAccum += 120;
delta -= 120;
}
if(delta == 0) return;
POINT pt;
GetCursorPos(&pt);
HWND hw = WindowFromPoint(pt);
// Make the mousewheel work according to which window the mouse is
// over, not according to which window is active.
bool inTextWindow;
if(hw == TextWnd) {
inTextWindow = true;
} else if(hw == GraphicsWnd) {
inTextWindow = false;
} else if(GetForegroundWindow() == TextWnd) {
inTextWindow = true;
} else {
inTextWindow = false;
}
if(inTextWindow) {
int i;
for(i = 0; i < abs(delta/40); i++) {
HandleTextWindowScrollBar(delta > 0 ? SB_LINEUP : SB_LINEDOWN, 0);
}
} else {
SS.GW.MouseScroll(LastMousePos.x, LastMousePos.y, delta);
}
}
LRESULT CALLBACK TextWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_ERASEBKGND:
break;
case WM_CLOSE:
case WM_DESTROY:
SolveSpaceUI::MenuFile(Command::EXIT);
break;
case WM_PAINT: {
// Actually paint the text window, with gl.
PaintTextWnd();
// And then just make Windows happy.
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
break;
}
case WM_SIZING: {
RECT *r = (RECT *)lParam;
int hc = (r->bottom - r->top) - ClientIsSmallerBy;
int extra = hc % (SS.TW.LINE_HEIGHT/2);
switch(wParam) {
case WMSZ_BOTTOM:
case WMSZ_BOTTOMLEFT:
case WMSZ_BOTTOMRIGHT:
r->bottom -= extra;
break;
case WMSZ_TOP:
case WMSZ_TOPLEFT:
case WMSZ_TOPRIGHT:
r->top += extra;
break;
}
int tooNarrow = (SS.TW.MIN_COLS*SS.TW.CHAR_WIDTH) -
(r->right - r->left);
if(tooNarrow >= 0) {
switch(wParam) {
case WMSZ_RIGHT:
case WMSZ_BOTTOMRIGHT:
case WMSZ_TOPRIGHT:
r->right += tooNarrow;
break;
case WMSZ_LEFT:
case WMSZ_BOTTOMLEFT:
case WMSZ_TOPLEFT:
r->left -= tooNarrow;
break;
}
}
break;
}
case WM_MOUSELEAVE:
SS.TW.MouseLeave();
break;
case WM_LBUTTONDOWN:
case WM_MOUSEMOVE: {
// We need this in order to get the WM_MOUSELEAVE
TRACKMOUSEEVENT tme = {};
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = TextWnd;
TrackMouseEvent(&tme);
// And process the actual message
int x = LOWORD(lParam);
int y = HIWORD(lParam);
SS.TW.MouseEvent(msg == WM_LBUTTONDOWN, wParam & MK_LBUTTON, x, y);
break;
}
case WM_SIZE: {
RECT r;
GetWindowRect(TextWndScrollBar, &r);
int sw = r.right - r.left;
GetClientRect(hwnd, &r);
MoveWindow(TextWndScrollBar, r.right - sw, r.top, sw,
(r.bottom - r.top), true);
// If the window is growing, then the scrollbar position may
// be moving, so it's as if we're dragging the scrollbar.
HandleTextWindowScrollBar((WPARAM)-1, -1);
InvalidateRect(TextWnd, NULL, false);
break;
}
case WM_MOUSEWHEEL:
MouseWheel(GET_WHEEL_DELTA_WPARAM(wParam));
break;
case WM_VSCROLL:
HandleTextWindowScrollBar(wParam, lParam);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 1;
}
static std::string EditControlText(HWND hwnd)
{
std::wstring result;
result.resize(GetWindowTextLength(hwnd));
GetWindowTextW(hwnd, &result[0], result.length() + 1);
return Narrow(result);
}
static bool ProcessKeyDown(WPARAM wParam)
{
if(GraphicsEditControlIsVisible() && wParam != VK_ESCAPE) {
if(wParam == VK_RETURN) {
SS.GW.EditControlDone(EditControlText(GraphicsEditControl).c_str());
return true;
} else {
return false;
}
}
if(TextEditControlIsVisible() && wParam != VK_ESCAPE) {
if(wParam == VK_RETURN) {
SS.TW.EditControlDone(EditControlText(TextEditControl).c_str());
} else {
return false;
}
}
int c;
switch(wParam) {
case VK_OEM_PLUS: c = '+'; break;
case VK_OEM_MINUS: c = '-'; break;
case VK_ESCAPE: c = 27; break;
case VK_OEM_1: c = ';'; break;
case VK_OEM_3: c = '`'; break;
case VK_OEM_4: c = '['; break;
case VK_OEM_6: c = ']'; break;
case VK_OEM_5: c = '\\'; break;
case VK_OEM_PERIOD: c = '.'; break;
case VK_SPACE: c = ' '; break;
case VK_DELETE: c = 127; break;
case VK_TAB: c = '\t'; break;
case VK_BROWSER_BACK:
case VK_BACK: c = '\b'; break;
case VK_F1:
case VK_F2:
case VK_F3:
case VK_F4:
case VK_F5:
case VK_F6:
case VK_F7:
case VK_F8:
case VK_F9:
case VK_F10:
case VK_F11:
case VK_F12: c = ((int)wParam - VK_F1) + 0xf1; break;
// These overlap with some character codes that I'm using, so
// don't let them trigger by accident.
case VK_F16:
case VK_INSERT:
case VK_EXECUTE:
case VK_APPS:
case VK_LWIN:
case VK_RWIN: return false;
default:
c = (int)wParam;
break;
}
if(GetAsyncKeyState(VK_SHIFT) & 0x8000) c |= GraphicsWindow::SHIFT_MASK;
if(GetAsyncKeyState(VK_CONTROL) & 0x8000) c |= GraphicsWindow::CTRL_MASK;
switch(c) {
case GraphicsWindow::SHIFT_MASK | '.': c = '>'; break;
}
for(int i = 0; SS.GW.menu[i].level >= 0; i++) {
if(c == SS.GW.menu[i].accel) {
(SS.GW.menu[i].fn)((Command)SS.GW.menu[i].id);
break;
}
}
if(SS.GW.KeyDown(c)) return true;
// No accelerator; process the key as normal.
return false;
}
void SolveSpace::ToggleMenuBar()
{
// Implement me
}
bool SolveSpace::MenuBarIsVisible()
{
// Implement me
return true;
}
void SolveSpace::ShowTextWindow(bool visible)
{
ShowWindow(TextWnd, visible ? SW_SHOWNOACTIVATE : SW_HIDE);
}
const bool SolveSpace::FLIP_FRAMEBUFFER = false;
#if HAVE_OPENGL == 2
static void CreateGlContext(HWND hwnd, EGLDisplay *eglDisplay, EGLSurface *eglSurface,
EGLContext *eglContext) {
ssassert(eglBindAPI(EGL_OPENGL_ES_API), "Cannot bind EGL API");
*eglDisplay = eglGetDisplay(GetDC(hwnd));
ssassert(eglInitialize(*eglDisplay, NULL, NULL), "Cannot initialize EGL");
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, hwnd, 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");
eglMakeCurrent(*eglDisplay, *eglSurface, *eglSurface, *eglContext);
}
#else
static void CreateGlContext(HWND hwnd, HGLRC *glrc)
{
HDC hdc = GetDC(hwnd);
PIXELFORMATDESCRIPTOR pfd = {};
int pixelFormat;
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;
pixelFormat = ChoosePixelFormat(hdc, &pfd);
ssassert(pixelFormat != 0, "Expected a valid pixel format to be chosen");
ssassert(SetPixelFormat(hdc, pixelFormat, &pfd), "Cannot set pixel format");
*glrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, *glrc);
}
#endif
void SolveSpace::PaintGraphics()
{
SS.GW.Paint();
#if HAVE_OPENGL == 2
eglSwapBuffers(GraphicsGlDisplay, GraphicsGlSurface);
#else
SwapBuffers(GetDC(GraphicsWnd));
#endif
}
void SolveSpace::InvalidateGraphics()
{
InvalidateRect(GraphicsWnd, NULL, false);
}
void SolveSpace::ToggleFullScreen()
{
// Implement me
}
bool SolveSpace::FullScreenIsActive()
{
// Implement me
return false;
}
void SolveSpace::InvalidateText()
{
InvalidateRect(TextWnd, NULL, false);
}
static void ShowEditControl(HWND h, int x, int y, int fontHeight, int minWidthChars,
bool isMonospace, const std::wstring &s) {
static HFONT hf;
if(hf) DeleteObject(hf);
hf = CreateFontW(-fontHeight, 0, 0, 0,
FW_REGULAR, false, false, false, ANSI_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, FF_DONTCARE, isMonospace ? L"Lucida Console" : L"Arial");
if(hf) SendMessage(h, WM_SETFONT, (WPARAM)hf, false);
else SendMessage(h, WM_SETFONT, (WPARAM)(HFONT)GetStockObject(SYSTEM_FONT), false);
SendMessage(h, EM_SETMARGINS, EC_LEFTMARGIN|EC_RIGHTMARGIN, 0);
HDC hdc = GetDC(h);
TEXTMETRICW tm;
SIZE ts;
SelectObject(hdc, hf);
GetTextMetrics(hdc, &tm);
GetTextExtentPoint32W(hdc, s.c_str(), s.length(), &ts);
ReleaseDC(h, hdc);
RECT rc;
rc.left = x;
rc.top = y - tm.tmAscent;
// Add one extra char width to avoid scrolling.
rc.right = x + std::max(tm.tmAveCharWidth * minWidthChars,
ts.cx + tm.tmAveCharWidth);
rc.bottom = y + tm.tmDescent;
AdjustWindowRectEx(&rc, 0, false, WS_EX_CLIENTEDGE);
MoveWindow(h, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, true);
ShowWindow(h, SW_SHOW);
if(!s.empty()) {
SendMessage(h, WM_SETTEXT, 0, (LPARAM)s.c_str());
SendMessage(h, EM_SETSEL, 0, s.length());
SetFocus(h);
}
}
void SolveSpace::ShowTextEditControl(int x, int y, const std::string &str)
{
if(GraphicsEditControlIsVisible()) return;
ShowEditControl(TextEditControl, x, y, TextWindow::CHAR_HEIGHT, 30,
/*isMonospace=*/true, Widen(str));
}
void SolveSpace::HideTextEditControl()
{
ShowWindow(TextEditControl, SW_HIDE);
}
bool SolveSpace::TextEditControlIsVisible()
{
return IsWindowVisible(TextEditControl) ? true : false;
}
void SolveSpace::ShowGraphicsEditControl(int x, int y, int fontHeight, int minWidthChars,
const std::string &str, bool forDock)
{
if(GraphicsEditControlIsVisible()) return;
ShowEditControl(GraphicsEditControl, x, y, fontHeight, minWidthChars,
/*isMonospace=*/forDock, Widen(str));
}
void SolveSpace::HideGraphicsEditControl()
{
ShowWindow(GraphicsEditControl, SW_HIDE);
}
bool SolveSpace::GraphicsEditControlIsVisible()
{
return IsWindowVisible(GraphicsEditControl) ? true : false;
}
LRESULT CALLBACK GraphicsWndProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam)
{
switch (msg) {
case WM_ERASEBKGND:
break;
case WM_SIZE:
InvalidateRect(GraphicsWnd, NULL, false);
break;
case WM_PAINT: {
// Actually paint the window, with gl.
PaintGraphics();
// And make Windows happy.
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
break;
}
case WM_MOUSELEAVE:
SS.GW.MouseLeave();
break;
case WM_MOUSEMOVE:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_MBUTTONDOWN: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
// We need this in order to get the WM_MOUSELEAVE
TRACKMOUSEEVENT tme = {};
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = GraphicsWnd;
TrackMouseEvent(&tme);
LastMousePos.x = x;
LastMousePos.y = y;
if(msg == WM_LBUTTONDOWN) {
SS.GW.MouseLeftDown(x, y);
} else if(msg == WM_LBUTTONUP) {
SS.GW.MouseLeftUp(x, y);
} else if(msg == WM_LBUTTONDBLCLK) {
SS.GW.MouseLeftDoubleClick(x, y);
} else if(msg == WM_MBUTTONDOWN || msg == WM_RBUTTONDOWN) {
SS.GW.MouseMiddleOrRightDown(x, y);
} else if(msg == WM_RBUTTONUP) {
SS.GW.MouseRightUp(x, y);
} else if(msg == WM_MOUSEMOVE) {
SS.GW.MouseMoved(x, y,
!!(wParam & MK_LBUTTON),
!!(wParam & MK_MBUTTON),
!!(wParam & MK_RBUTTON),
!!(wParam & MK_SHIFT),
!!(wParam & MK_CONTROL));
}
break;
}
case WM_MOUSEWHEEL:
MouseWheel(GET_WHEEL_DELTA_WPARAM(wParam));
break;
case WM_COMMAND: {
if(HIWORD(wParam) == 0) {
Command id = (Command)LOWORD(wParam);
if(((uint32_t)id >= (uint32_t)Command::RECENT_OPEN &&
(uint32_t)id < ((uint32_t)Command::RECENT_OPEN + MAX_RECENT))) {
You can’t perform that action at this time.
