-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathui.cpp
1194 lines (1033 loc) · 31.4 KB
/
ui.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
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#include "asspull.h"
#include "SDL_syswm.h"
#include <commdlg.h>
#include <Uxtheme.h>
#include <vsstyle.h>
#include <time.h>
#include <vector>
extern int decodePNG(std::vector<unsigned char>& out_image, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, size_t in_size, bool convert_to_rgba32 = true);
extern int langID;
namespace UI
{
WCHAR settingsFile[FILENAME_MAX];
WCHAR startingPath[FILENAME_MAX];
WCHAR lastPath[FILENAME_MAX];
HWND hWndMain;
HINSTANCE hInstance;
HWND hWndStatusBar;
HMENU menuBar;
int statusBarHeight = 0;
bool hideUI = false;
bool startFullscreen = false;
int uiCommand;
WCHAR uiString[512];
bool mouseLocked = false;
int statusTimer = 0;
WCHAR uiStatus[512];
bool fpsVisible = false, fpsCap = false, reloadROM, reloadIMG;
int oldPause = pauseTurnedOff;
int theme = 0;
int diskIconTimer = 0, hddIconTimer = 0;
bool ShowFileDlg(bool toSave, WCHAR* target, size_t max, const WCHAR* filter);
namespace Presentation
{
HFONT headerFont = NULL, monoFont = NULL, statusFont = NULL;
HBRUSH hbrBack = NULL, hbrStripe = NULL, hbrList = NULL;
HPEN hpnStripe = NULL;
COLORREF rgbBack = NULL, rgbStripe = NULL, rgbText = NULL, rgbTextD = NULL;
COLORREF rgbHeader = NULL, rgbList = NULL, rgbListBk = NULL;
void DrawWindowBk(HWND hwndDlg, bool stripe)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwndDlg, &ps);
DrawWindowBk(hwndDlg, stripe, &ps, hdc);
EndPaint(hwndDlg, &ps);
}
void DrawWindowBk(HWND hwndDlg, bool stripe, PAINTSTRUCT* ps, HDC hdc)
{
if (stripe)
{
RECT rect = { 0 };
rect.bottom = 7 + 14 + 7; //margin, button, padding
MapDialogRect(hwndDlg, &rect);
auto h = rect.bottom;
GetClientRect(hwndDlg, &rect);
rect.bottom -= h;
FillRect(hdc, &ps->rcPaint, hbrStripe);
FillRect(hdc, &rect, hbrBack);
}
else
{
FillRect(hdc, &ps->rcPaint, hbrBack);
}
return;
}
void DrawCheckbox(HWND hwndDlg, LPNMCUSTOMDRAW nmc)
{
int idFrom = nmc->hdr.idFrom;
switch (nmc->dwDrawStage)
{
case CDDS_PREERASE:
{
SetBkColor(nmc->hdc, rgbBack);
SetTextColor(nmc->hdc, nmc->uItemState & CDIS_DISABLED ? rgbTextD : rgbText);
HTHEME hTheme = OpenThemeData(nmc->hdr.hwndFrom, L"BUTTON");
if (!hTheme)
{
CloseThemeData(hTheme);
SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, (LONG_PTR)CDRF_DODEFAULT);
return;
}
auto checked = IsDlgButtonChecked(hwndDlg, idFrom);
int stateID = checked ? CBS_CHECKEDNORMAL : CBS_UNCHECKEDNORMAL;
if (nmc->uItemState & CDIS_DISABLED)
stateID = checked ? CBS_CHECKEDDISABLED : CBS_UNCHECKEDDISABLED;
else if (nmc->uItemState & CDIS_SELECTED)
stateID = checked ? CBS_CHECKEDPRESSED : CBS_UNCHECKEDPRESSED;
else if (nmc->uItemState & CDIS_HOT)
stateID = checked ? CBS_CHECKEDHOT : CBS_UNCHECKEDHOT;
RECT r;
SIZE s;
GetThemePartSize(hTheme, nmc->hdc, BP_CHECKBOX, stateID, NULL, TS_TRUE, &s);
r.left = nmc->rc.left;
r.top = nmc->rc.top + 3;
r.right = r.left + s.cx;
r.bottom = r.top + s.cy;
DrawThemeBackground(hTheme, nmc->hdc, BP_CHECKBOX, stateID, &r, NULL);
if (theme == 1) //dark
{
InflateRect(&r, -1, -1);
InvertRect(nmc->hdc, &r);
InflateRect(&r, 1, 1);
}
nmc->rc.left += 3 + s.cx;
WCHAR text[256];
GetDlgItemText(hwndDlg, idFrom, text, 255);
DrawText(nmc->hdc, text, -1, &nmc->rc, DT_SINGLELINE | DT_VCENTER);
CloseThemeData(hTheme);
SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, (LONG_PTR)CDRF_SKIPDEFAULT);
return;
}
}
}
bool DrawButton(HWND hwndDlg, LPNMCUSTOMDRAW nmc)
{
if (theme == 0)
return false; //don't draw jack
int idFrom = nmc->hdr.idFrom;
switch (nmc->dwDrawStage)
{
case CDDS_PREPAINT:
{
SetBkColor(nmc->hdc, rgbBack);
SetTextColor(nmc->hdc, nmc->uItemState & CDIS_DISABLED ? rgbTextD : rgbText);
HBRUSH border = CreateSolidBrush(rgbText);
FillRect(nmc->hdc, &nmc->rc, border);
InflateRect(&nmc->rc, -1, -1);
if (nmc->uItemState & CDIS_DEFAULT)
InflateRect(&nmc->rc, -1, -1);
FillRect(nmc->hdc, &nmc->rc, (nmc->uItemState & CDIS_SELECTED) ? hbrList : hbrBack);
if (nmc->uItemState & CDIS_FOCUS)
{
InflateRect(&nmc->rc, -1, -1);
DrawFocusRect(nmc->hdc, &nmc->rc);
}
DeleteObject(border);
SetBkColor(nmc->hdc, (nmc->uItemState & CDIS_SELECTED) ? rgbListBk : rgbBack);
WCHAR text[256];
GetDlgItemText(hwndDlg, idFrom, text, 255);
DrawText(nmc->hdc, text, -1, &nmc->rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, (LONG_PTR)CDRF_SKIPDEFAULT);
}
}
return true;
}
bool DrawComboBox(LPDRAWITEMSTRUCT dis)
{
if (dis->itemID == -1)
return false;
SetTextColor(dis->hDC, dis->itemState & ODS_SELECTED ? GetSysColor(COLOR_HIGHLIGHTTEXT) : rgbList);
SetBkColor(dis->hDC, dis->itemState & ODS_SELECTED ? GetSysColor(COLOR_HIGHLIGHT) : rgbListBk);
TEXTMETRIC tm;
GetTextMetrics(dis->hDC, &tm);
int y = (dis->rcItem.bottom + dis->rcItem.top - tm.tmHeight) / 2;
int x = LOWORD(GetDialogBaseUnits()) / 4;
if (dis->itemState & ODS_COMBOBOXEDIT)
x += 2;
WCHAR text[256] = { 0 };
SendMessage(dis->hwndItem, CB_GETLBTEXT, dis->itemID, (LPARAM)text);
ExtTextOut(dis->hDC, x, y, ETO_CLIPPED | ETO_OPAQUE, &dis->rcItem, text, (UINT)wcslen(text), NULL);
return true;
}
void GetIconPos(HWND hwndDlg, int ctlID, RECT* iconRect, int leftOffset, int topOffset, int size)
{
POINT t;
GetWindowRect(GetDlgItem(hwndDlg, ctlID), iconRect);
t.x = iconRect->left;
t.y = iconRect->top;
ScreenToClient(hwndDlg, &t);
iconRect->left = t.x + leftOffset;
iconRect->top = t.y + topOffset;
iconRect->right = iconRect->left + size;
iconRect->bottom = iconRect->top + size;
}
namespace Windows10
{
int MatchTheme()
{
DWORD nResult;
HKEY key;
auto result = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", 0, KEY_READ, &key);
if (result != ERROR_SUCCESS)
return -1;
DWORD dwBufferSize = sizeof(DWORD);
result = RegQueryValueEx(key, L"AppsUseLightTheme", 0, NULL, (LPBYTE)&nResult, &dwBufferSize);
RegCloseKey(key);
if (result != ERROR_SUCCESS)
return -1;
//AppsUseLightTheme == 0 means light, and that's the inverse of our list.
return !nResult;
}
bool IsWin10()
{
return MatchTheme() != -1;
}
COLORREF GetAccent(COLORREF not10)
{
DWORD nResult;
HKEY key;
if (RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\DWM", 0, KEY_READ, &key))
return not10;
DWORD dwBufferSize = sizeof(DWORD);
if (RegQueryValueEx(key, L"AccentColor", 0, NULL, (LPBYTE)&nResult, &dwBufferSize))
nResult = not10;
RegCloseKey(key);
nResult &= 0xFFFFFF;
int r = (unsigned char)nResult;
int g = (unsigned char)(nResult >> 8);
int b = (unsigned char)(nResult >> 16);
//the one helpful thing from MS Docs on darkmode :3
int brightness = (5 * g) + (2 * r) + b;
if (brightness < (8 * 128))
nResult = not10; //fall back
return nResult;
}
namespace DarkMenuBar
{
#pragma region Bar
bool UAHWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* lr);
#define WM_UAHDESTROYWINDOW 0x0090
#define WM_UAHDRAWMENU 0x0091
#define WM_UAHDRAWMENUITEM 0x0092
#define WM_UAHINITMENU 0x0093
#define WM_UAHMEASUREMENUITEM 0x0094
#define WM_UAHNCPAINTMENUPOPUP 0x0095
typedef union tagUAHMENUITEMMETRICS
{
struct {
DWORD cx;
DWORD cy;
} rgsizeBar[2];
struct {
DWORD cx;
DWORD cy;
} rgsizePopup[4];
} UAHMENUITEMMETRICS;
typedef struct tagUAHMENUPOPUPMETRICS
{
DWORD rgcx[4];
DWORD fUpdateMaxWidths : 2;
} UAHMENUPOPUPMETRICS;
typedef struct tagUAHMENU
{
HMENU hmenu;
HDC hdc;
DWORD dwFlags;
} UAHMENU;
typedef struct tagUAHMENUITEM
{
int iPosition;
UAHMENUITEMMETRICS umim;
UAHMENUPOPUPMETRICS umpm;
} UAHMENUITEM;
typedef struct UAHDRAWMENUITEM
{
DRAWITEMSTRUCT dis; // itemID looks uninitialized
UAHMENU um;
UAHMENUITEM umi;
} UAHDRAWMENUITEM;
typedef struct tagUAHMEASUREMENUITEM
{
MEASUREITEMSTRUCT mis;
UAHMENU um;
UAHMENUITEM umi;
} UAHMEASUREMENUITEM;
static HTHEME g_menuTheme = nullptr;
void UAHDrawMenuNCBottomLine(HWND hWnd)
{
MENUBARINFO mbi = { sizeof(mbi) };
if (!GetMenuBarInfo(hWnd, OBJID_MENU, 0, &mbi))
{
return;
}
RECT rcClient = { 0 };
GetClientRect(hWnd, &rcClient);
MapWindowPoints(hWnd, nullptr, (POINT*)&rcClient, 2);
RECT rcWindow = { 0 };
GetWindowRect(hWnd, &rcWindow);
OffsetRect(&rcClient, -rcWindow.left, -rcWindow.top);
// the rcBar is offset by the window rect
RECT rcAnnoyingLine = rcClient;
rcAnnoyingLine.bottom = rcAnnoyingLine.top;
rcAnnoyingLine.top--;
HDC hdc = GetWindowDC(hWnd);
FillRect(hdc, &rcAnnoyingLine, hbrStripe);
ReleaseDC(hWnd, hdc);
}
bool UAHWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* lr)
{
switch (message)
{
case WM_UAHDRAWMENU:
{
UAHMENU* pUDM = (UAHMENU*)lParam;
RECT rc = { 0 };
{
MENUBARINFO mbi = { sizeof(mbi) };
GetMenuBarInfo(hWnd, OBJID_MENU, 0, &mbi);
RECT rcWindow;
GetWindowRect(hWnd, &rcWindow);
rc = mbi.rcBar;
OffsetRect(&rc, -rcWindow.left, -rcWindow.top);
}
FillRect(pUDM->hdc, &rc, hbrBack);
return true;
}
case WM_UAHDRAWMENUITEM:
{
UAHDRAWMENUITEM* pUDMI = (UAHDRAWMENUITEM*)lParam;
HBRUSH* pbrBackground = &hbrBack;
wchar_t menuString[256] = { 0 };
MENUITEMINFO mii = { sizeof(mii), MIIM_STRING };
{
mii.dwTypeData = menuString;
mii.cch = (sizeof(menuString) / 2) - 1;
GetMenuItemInfo(pUDMI->um.hmenu, pUDMI->umi.iPosition, TRUE, &mii);
}
DWORD dwFlags = DT_CENTER | DT_SINGLELINE | DT_VCENTER;
int iTextStateID = 0;
int iBackgroundStateID = 0;
{
if ((pUDMI->dis.itemState & ODS_INACTIVE) | (pUDMI->dis.itemState & ODS_DEFAULT))
{
iTextStateID = MPI_NORMAL;
iBackgroundStateID = MPI_NORMAL;
}
if (pUDMI->dis.itemState & ODS_HOTLIGHT)
{
iTextStateID = MPI_HOT;
iBackgroundStateID = MPI_HOT;
pbrBackground = &hbrStripe;
}
if (pUDMI->dis.itemState & ODS_SELECTED)
{
iTextStateID = MPI_HOT;
iBackgroundStateID = MPI_HOT;
pbrBackground = &hbrStripe;
}
if ((pUDMI->dis.itemState & ODS_GRAYED) || (pUDMI->dis.itemState & ODS_DISABLED))
{
iTextStateID = MPI_DISABLED;
iBackgroundStateID = MPI_DISABLED;
}
if (pUDMI->dis.itemState & ODS_NOACCEL)
{
dwFlags |= DT_HIDEPREFIX;
}
}
if (!g_menuTheme)
g_menuTheme = OpenThemeData(hWnd, L"Menu");
DTTOPTS opts = { sizeof(opts), DTT_TEXTCOLOR, iTextStateID != MPI_DISABLED ? rgbText : rgbTextD };
FillRect(pUDMI->um.hdc, &pUDMI->dis.rcItem, *pbrBackground);
DrawThemeTextEx(g_menuTheme, pUDMI->um.hdc, MENU_BARITEM, MBI_NORMAL, menuString, mii.cch, dwFlags, &pUDMI->dis.rcItem, &opts);
return true;
}
case WM_UAHMEASUREMENUITEM:
{
UAHMEASUREMENUITEM* pMmi = (UAHMEASUREMENUITEM*)lParam;
*lr = DefWindowProc(hWnd, message, wParam, lParam);
//pMmi->mis.itemWidth = (pMmi->mis.itemWidth * 4) / 3;
return true;
}
case WM_THEMECHANGED:
{
if (g_menuTheme)
{
CloseThemeData(g_menuTheme);
g_menuTheme = nullptr;
}
return false;
}
case WM_NCPAINT:
case WM_NCACTIVATE:
*lr = DefWindowProc(hWnd, message, wParam, lParam);
UAHDrawMenuNCBottomLine(hWnd);
return true;
break;
default:
return false;
}
}
#pragma endregion
#pragma region Dropdown
enum PreferredAppMode
{
Default,
AllowDark,
ForceDark,
ForceLight,
Max
};
using fnAllowDarkModeForWindow = bool (WINAPI*)(HWND hWnd, bool allow); // ordinal 133
using fnSetPreferredAppMode = PreferredAppMode(WINAPI*)(PreferredAppMode appMode); // ordinal 135, in 1903
using fnFlushMenuThemes = void (WINAPI*)(void); //ordinal 136
#pragma endregion
void HandleMenu()
{
if (!IsWin10())
return;
auto hUxtheme = LoadLibraryExW(L"uxtheme.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (hUxtheme == nullptr)
return;
auto AllowDarkModeForWindow = (fnAllowDarkModeForWindow)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(133));
auto SetPreferredAppMode = (fnSetPreferredAppMode)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(135));
auto FlushMenuThemes = (fnFlushMenuThemes)GetProcAddress(hUxtheme, MAKEINTRESOURCEA(136));
AllowDarkModeForWindow(hWndMain, true);
SetPreferredAppMode(theme ? PreferredAppMode::ForceDark : PreferredAppMode::ForceLight);
FlushMenuThemes();
SendMessageW(hWndMain, WM_THEMECHANGED, 0, 0);
FreeLibrary(hUxtheme);
}
}
}
void SetThemeColors()
{
theme = ini.GetLongValue(L"misc", L"theme", 0);
if (theme == 2) //match
theme = Windows10::MatchTheme();
switch (theme)
{
case 0: //light
{
rgbBack = RGB(255, 255, 255);
rgbStripe = RGB(240, 240, 240);
rgbText = RGB(0, 0, 0);
rgbTextD = RGB(131, 131, 131);
rgbList = RGB(0, 0, 0);
rgbHeader = Windows10::GetAccent(RGB(0x00, 0x33, 0x99));
rgbListBk = RGB(255, 255, 255);
break;
}
case 1: //dark
{
rgbBack = RGB(43, 43, 43);
rgbStripe = RGB(65, 65, 65);
rgbText = RGB(255, 255, 255);
rgbTextD = RGB(109, 109, 109);
rgbList = RGB(255, 255, 255);
rgbHeader = Windows10::GetAccent(RGB(0x8E, 0xCA, 0xF8));
rgbListBk = RGB(65, 65, 65);
}
//this space for rent
}
if (hbrBack != NULL) DeleteObject(hbrBack);
if (hbrStripe != NULL) DeleteObject(hbrStripe);
if (hbrList != NULL) DeleteObject(hbrList);
if (hpnStripe != NULL) DeleteObject(hpnStripe);
hbrBack = CreateSolidBrush(rgbBack);
hbrStripe = CreateSolidBrush(rgbStripe);
hbrList = CreateSolidBrush(rgbListBk);
hpnStripe = CreatePen(PS_SOLID, 1, rgbStripe);
Windows10::DarkMenuBar::HandleMenu();
RedrawWindow(hWndMain, NULL, NULL, RDW_INVALIDATE | RDW_FRAME);
if (About::hWnd != NULL) RedrawWindow(About::hWnd, NULL, NULL, RDW_INVALIDATE);
if (DeviceManager::hWnd != NULL) RedrawWindow(DeviceManager::hWnd, NULL, NULL, RDW_INVALIDATE);
if (MemoryViewer::hWnd != NULL) RedrawWindow(MemoryViewer::hWnd, NULL, NULL, RDW_INVALIDATE);
if (Options::hWnd != NULL) RedrawWindow(Options::hWnd, NULL, NULL, RDW_INVALIDATE);
if (ButtonMaps::hWnd != NULL) RedrawWindow(ButtonMaps::hWnd, NULL, NULL, RDW_INVALIDATE);
}
}
namespace Images
{
HIMAGELIST hIml = NULL;
HBITMAP GetImageListImage(int index)
{
//Step one, prepare a canvas.
auto scrDC = GetDC(0);
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biWidth = bmi.bmiHeader.biHeight = 16;
void* throwAway; //only here to satisfy code analysis :shrug:
auto hbm = CreateDIBSection(scrDC, &bmi, DIB_RGB_COLORS, &throwAway, NULL, 0);
ReleaseDC(0, scrDC);
if (hbm == NULL) return NULL;
//Step two, draw on it.
auto dc = CreateCompatibleDC(0);
auto oldBm = SelectObject(dc, hbm);
ImageList_Draw(hIml, index, dc, 0, 0, ILD_NORMAL);
SelectObject(dc, oldBm);
ReleaseDC(0, dc);
return hbm;
}
std::vector<unsigned char> LoadPNGResource(int id, unsigned long *width, unsigned long *height)
{
unsigned long size;
auto find = FindResourceEx(NULL, L"PNG", MAKEINTRESOURCE(id), langID);
if (find == NULL) return { 0 };
auto res = LoadResource(NULL, find);
if (res == NULL) return { 0 };
auto lock = LockResource(res);
size = SizeofResource(NULL, find);
std::vector<unsigned char> image;
decodePNG(image, *width, *height, (const unsigned char*)lock, size);
for (unsigned long i = 0; i < (*width * *height * 4); i += 4)
{
auto r = image[i + 0];
auto b = image[i + 2];
image[i + 0] = b;
image[i + 2] = r;
//clean up messy alpha channels
if (image[i + 3] == 0)
{
image[i + 0] = 0;
image[i + 1] = 0;
image[i + 2] = 0;
}
}
return image;
}
HBITMAP LoadPNGResource(int id)
{
unsigned long width, height;
auto image = LoadPNGResource(id, &width, &height);
auto bitmap = CreateBitmap(width, height, 1, 32, &image[0]);
return bitmap;
}
}
namespace Tooltips
{
HWND hWndTooltips;
void Initialize()
{
hWndTooltips = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX /* | TTS_BALLOON */, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hWndMain, NULL, hInstance, NULL);
SetWindowPos(hWndTooltips, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
void CreateTooltips(HWND hWndClient, ...)
{
va_list args;
va_start(args, hWndClient);
int dlgItem = va_arg(args, int);
while (dlgItem)
{
TOOLINFO ti =
{
sizeof(TOOLINFO), TTF_IDISHWND | TTF_SUBCLASS, hWndClient,
(UINT_PTR)GetDlgItem(hWndClient, dlgItem),
{ 0, 0, 0, 0 }, hInstance,
MAKEINTRESOURCE(IDS_TOOLTIPS + dlgItem)
};
SendMessage(hWndTooltips, TTM_ADDTOOL, 0, (LPARAM)&ti);
dlgItem = va_arg(args, int);
}
va_end(args);
}
void DestroyTooltips()
{
DestroyWindow(hWndTooltips);
}
}
WNDPROC SDLWinProc = NULL;
LRESULT WndProc(HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
{
//We get to do a lotta fancy fuckery now that this is a Win32 wndproc and not
//SDL's watered-down one.
LRESULT lr = 0;
if (theme == 1 && Presentation::Windows10::DarkMenuBar::UAHWndProc(hWnd, message, wParam, lParam, &lr)) {
return lr;
}
switch (message)
{
case WM_COMMAND:
{
if (wParam > 1000 && wParam < 2000)
{
uiCommand = (int)(wParam - 1000);
if (uiCommand >= cmdMemViewer && uiCommand <= cmdButtonMapper)
{
void(*commands[])(void) = {
MemoryViewer::Show, PalViewer::Show, About::Show,
Options::Show, Shaders::Show, DeviceManager::Show,
TileViewer::Show, nullptr, nullptr, nullptr, nullptr,
nullptr, UI::ButtonMaps::Show,
};
if (commands[uiCommand - cmdMemViewer] != nullptr)
{
commands[uiCommand - cmdMemViewer]();
uiCommand = 0;
}
}
return 0;
}
}
case WM_SETTINGCHANGE:
{
if (!strcmp((const char*)lParam, "ImmersiveColorSet"))
{
if (ini.GetLongValue(L"misc", L"theme", 0) == 2)
{
//Lightly throttle the five or six identical WM.
static __time64_t lastColorChange = 0;
__time64_t now;
_time64(&now);
if (now >= lastColorChange + 1)
{
lastColorChange = now;
Presentation::SetThemeColors();
}
}
}
}
default:
return CallWindowProc(SDLWinProc, hWnd, message, wParam, lParam);
}
}
WCHAR statusFPS[16], statusText[512];
HDC statusRealDC = NULL;
HDC statusDC = NULL;
HBITMAP statusBmp = NULL;
void DrawStatusBar();
void ResizeStatusBar()
{
RECT sbRect;
GetClientRect(hWndMain, &sbRect);
SetWindowPos(hWndStatusBar, HWND_NOTOPMOST, 0, sbRect.bottom - statusBarHeight, sbRect.right, statusBarHeight, SWP_NOZORDER);
if (statusRealDC) ReleaseDC(hWndMain, statusRealDC);
if (statusDC) DeleteDC(statusDC);
if (statusBmp) DeleteObject(statusBmp);
statusRealDC = GetDC(hWndStatusBar);
statusDC = CreateCompatibleDC(statusRealDC);
statusBmp = CreateCompatibleBitmap(statusRealDC, sbRect.right, sbRect.bottom);
SelectObject(statusDC, GetStockObject(DC_PEN));
SelectObject(statusDC, Presentation::statusFont);
SelectObject(statusDC, statusBmp);
DrawStatusBar();
}
void DrawStatusBar()
{
RECT sbRect;
GetClientRect(hWndMain, &sbRect);
sbRect.bottom = statusBarHeight;
if (hideUI)
{
//FillRect(statusDC, &sbRect, (HBRUSH)GetStockObject(BLACK_BRUSH));
BitBlt(statusRealDC, 0, 0, sbRect.right, sbRect.bottom, NULL, 0, 0, BLACKNESS);
return;
}
auto hdc = statusDC;
FillRect(hdc, &sbRect, Presentation::hbrBack);
SetDCPenColor(hdc, Presentation::rgbStripe);
MoveToEx(hdc, 0, 0, NULL);
LineTo(hdc, sbRect.right, 0);
MoveToEx(hdc, 40, 3, NULL);
LineTo(hdc, 40, sbRect.bottom - 3);
SetBkColor(hdc, Presentation::rgbBack);
SetTextColor(hdc, Presentation::rgbText);
RECT sbText = { 4, 4, 32, sbRect.bottom - 4 };
DrawText(hdc, statusFPS, wcslen(statusFPS), &sbText, DT_RIGHT);
sbText.left = 48;
sbText.right = sbRect.right - 48;
DrawText(hdc, statusText, wcslen(statusText), &sbText, DT_END_ELLIPSIS);
int x = sbRect.right - 24;
ImageList_Draw(Images::hIml, IML_PAUSESTATUS + pauseState, hdc, x, 3, ILD_NORMAL);
x -= 20;
if (::key2joy)
{
ImageList_Draw(Images::hIml, IML_GAMEPAD, hdc, x, 3, ILD_NORMAL);
x -= 20;
}
if (UI::mouseLocked)
{
ImageList_Draw(Images::hIml, IML_MOUSE, hdc, x, 3, ILD_NORMAL);
x -= 20;
}
if (hddIconTimer)
{
ImageList_Draw(Images::hIml, IML_HARDDRIVE, hdc, x, 3, ILD_NORMAL);
x -= 20;
}
if (diskIconTimer)
{
ImageList_Draw(Images::hIml, IML_DISKETTE, hdc, x, 3, ILD_NORMAL);
x -= 20;
}
BitBlt(statusRealDC, 0, 0, sbRect.right, sbRect.bottom, hdc, 0, 0, SRCCOPY);
}
void Update()
{
if (statusTimer)
statusTimer--;
else
statusText[0] = 0;
if (diskIconTimer)
diskIconTimer--;
if (hddIconTimer)
hddIconTimer--;
if (!hideUI)
DrawStatusBar();
}
void Initialize()
{
//SetProcessDPIAware();
//GetCurrentDirectory(FILENAME_MAX, startingPath);
GetModuleFileName(NULL, startingPath, 256);
WCHAR* lastSlash = wcsrchr(startingPath, L'\\');
*lastSlash = 0;
if (!wcsncmp(lastSlash - 5, L"Debug",6))
{
lastSlash = wcsrchr(startingPath, L'\\');
*lastSlash = 0;
}
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
if (SDL_GetWindowWMInfo(Video::sdlWindow, &info))
{
if (info.subsystem != SDL_SYSWM_WINDOWS)
return;
hWndMain = info.info.win.window;
hInstance = info.info.win.hinstance;
SDLWinProc = (WNDPROC)GetWindowLongPtr(hWndMain, GWLP_WNDPROC);
SetWindowLongPtr(hWndMain, GWLP_WNDPROC, (LONG_PTR)WndProc);
InitCommonControls();
Images::hIml = ImageList_Create(16, 16, ILC_COLOR32, 32, 0);
ImageList_Add(Images::hIml, Images::LoadPNGResource(IDB_ICONS), NULL);
menuBar = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MAINMENU));
bool drunk = false;
{
using fnWineGetVersion = const char *(CDECL*)(void);
HMODULE hntdll = GetModuleHandle(L"ntdll.dll");
if (hntdll)
{
auto wineGetVersion = (fnWineGetVersion)GetProcAddress(hntdll, "wine_get_version");
if (wineGetVersion)
drunk = true;
}
}
if (!drunk)
{
MENUINFO mainInfo =
{
sizeof(MENUINFO),
MIM_APPLYTOSUBMENUS | MIM_STYLE,
/* MNS_MODELESS | */ MNS_AUTODISMISS,
0,
NULL,
NULL,
0
};
SetMenuInfo(menuBar, &mainInfo);
MENUITEMINFO miInfo = { sizeof(MENUITEMINFO), MIIM_BITMAP };
for (int i = 0; i < 24; i++)
{
//auto hBmp = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(1000 + i), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_CREATEDIBSECTION);
auto hBmp = Images::GetImageListImage(IML_MENUSTART + i);
if (hBmp == NULL)
continue;
miInfo.hbmpItem = hBmp;
SetMenuItemInfo(menuBar, 1000 + i, FALSE, &miInfo);
}
}
SetMenu(hWndMain, menuBar);
hWndStatusBar = CreateWindowEx(0, L"STATIC", NULL, WS_CHILD | WS_VISIBLE | SS_OWNERDRAW, 0, 0, 0, 0, hWndMain, 0, hInstance, NULL);
statusBarHeight = 23;
wcscpy_s(statusFPS, 16, L" ");
auto scrDC = GetDC(0);
auto dpiY = GetDeviceCaps(scrDC, LOGPIXELSY);
auto headerSize = MulDiv(13, dpiY, 72);
ReleaseDC(0, scrDC);
Presentation::headerFont = CreateFont(headerSize, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Segoe UI");
Presentation::monoFont = CreateFont(16, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Courier New");
Presentation::statusFont = CreateFont(14, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Segoe UI");
Presentation::SetThemeColors();
Tooltips::Initialize();
SetTitle(NULL);
if (startFullscreen)
HideUI(true);
}
return;
}
bool ShowFileDlg(bool toSave, WCHAR* target, size_t max, const WCHAR* filter)
{
WCHAR sFilter[512];
wcscpy_s(sFilter, 512, filter);
WCHAR* f = sFilter;
while (*f)
{
if (*f == L'|')
*f = 0;
f++;
}
*f++ = 0;
*f++ = 0;
OPENFILENAME ofn = {
sizeof(OPENFILENAME),
hWndMain, hInstance,
sFilter, NULL, 0, 1,
target, max,
NULL, 0,
NULL,
NULL,
OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST
};
ofn.lpstrFile[0] = '\0';
WCHAR cwd[MAX_PATH];
GetCurrentDirectory(MAX_PATH, cwd);
if ((!toSave && (GetOpenFileName(&ofn) != FALSE)) || (toSave && (GetSaveFileName(&ofn) != FALSE)))
{
SetCurrentDirectory(cwd);
wcscpy_s(target, max, ofn.lpstrFile);
return true;
}
SetCurrentDirectory(cwd);
return false;
}
void SetStatus(const WCHAR* text)
{
wcscpy_s(statusText, 512, text);
statusTimer = 4 * wcslen(text);
}
void SetStatus(int stab)
{
WCHAR b[256];
LoadString(hInstance, stab, b, 256);
SetStatus(b);
}
void SetFPS(int fps)
{
if (fpsVisible)
_itow_s(fps, statusFPS, 16, 10);
else
wcscpy_s(statusFPS, 16, L" ");
}
static WCHAR getStringBuffer[512];
WCHAR* GetString(int stab)
{
LoadString(hInstance, stab, getStringBuffer, 512);
return getStringBuffer;
}
#define MAXSNOW 256
int snowData[MAXSNOW * 2];
int snowTimer = -1;
void LetItSnow()
{
if (snowTimer == -1)
{
//Prepare
srand(0xC001FACE);
for (int i = 0; i < MAXSNOW; i++)
{
snowData[(i * 2) + 0] = rand() % 639;
snowData[(i * 2) + 1] = rand() % 479;
}
snowTimer = 1;
}
snowTimer--;
if (snowTimer == 0)
{
snowTimer = 4;
for (int i = 0; i < MAXSNOW; i++)
{
snowData[(i * 2) + 0] += -1 + (rand() % 4);
snowData[(i * 2) + 1] += rand() % 2;