-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.cpp
617 lines (557 loc) · 12.3 KB
/
gui.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
#include "gui.h"
#include<chrono>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <Windows.h>
namespace gui {
bool g_Hotkeys[512];
bool g_KeysPressed[512];
bool g_KeysReleased[512];
bool g_KeysDown[512];
HWND g_CurrentFocused;
HWND g_MainWindow;
bool try_close = false;
HWND g_LastFocused;
WNDPROC originalEditProc;
LRESULT CALLBACK Edit_Prc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_CHAR && wParam == 1)
{
SendMessage(hwnd, EM_SETSEL, 0, -1);
return 1;
}
else
{
return CallWindowProc(originalEditProc, hwnd, msg, wParam, lParam);
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_SETFOCUS: {
SetFocus(g_CurrentFocused);
break;
}
case WM_CLOSE: {
try_close = true;
return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
WNDCLASSEXW wc;
HWND show_window(std::wstring title)
{
for (auto i = 0; i < 256; i++)
{
g_KeysReleased[i] = true;
}
if (IsWindow(g_MainWindow)) {
ShowWindow(g_MainWindow, SW_SHOW);
return g_MainWindow;
}
HINSTANCE hInstance = GetModuleHandle(NULL);
wc.cbSize = sizeof(WNDCLASSEXW);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = L"NGTApp";
wc.hIconSm = NULL;
if (!RegisterClassExW(&wc))
{
return nullptr;
}
HWND hwnd = CreateWindowExW(256, L"NGTApp", title.c_str(), WS_VISIBLE | WS_CAPTION | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
return nullptr;
}
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
SetForegroundWindow(hwnd);
g_MainWindow = hwnd;
return hwnd;
}
bool hide_window(HWND window) {
if (window == nullptr)return false;
return ShowWindow(window, SW_HIDE);
}
void update_window(HWND window, bool wait_event)
{
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
if (wait_event)
GetMessage(&msg, NULL, 0, 0);
else
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
if (msg.message != WM_NULL) {
switch (msg.message) {
case WM_HOTKEY:
if (msg.wParam < 512) {
g_Hotkeys[msg.wParam] = true;
}
break;
case WM_KEYDOWN:
if (msg.wParam < 512) {
g_KeysDown[msg.wParam] = true;
if (!g_KeysPressed[msg.wParam] && g_KeysReleased[msg.wParam]) {
g_KeysPressed[msg.wParam] = true;
g_KeysReleased[msg.wParam] = false;
}
else if (!g_KeysReleased[msg.wParam]) {
g_KeysPressed[msg.wParam] = false;
}
}
break;
case WM_KEYUP:
if (msg.wParam < 512) {
g_KeysDown[msg.wParam] = false;
g_KeysPressed[msg.wParam] = false;
g_KeysReleased[msg.wParam] = true;
}
break;
default:
break;
}
if (!IsDialogMessage(window, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if (GetForegroundWindow() == window) {
g_CurrentFocused = GetFocus();
}
}
}
}
bool key_pressed(int keyCode)
{
if (g_KeysPressed[keyCode] == true)
{
g_KeysPressed[keyCode] = false;
return true;
}
else
{
return false;
}
}
bool key_released(int keyCode)
{
if (g_KeysPressed[keyCode] == false && g_KeysReleased[keyCode] == true)
{
g_KeysReleased[keyCode] = g_KeysPressed[keyCode];
return true;
}
else
{
return false;
}
}
bool key_down(int keyCode)
{
return g_KeysDown[keyCode];
}
int alert(std::wstring title, std::wstring text, int flags)
{
HWND windowHandle = GetForegroundWindow();
return MessageBoxW(windowHandle, text.c_str(), title.c_str(), flags);
}
int get_vk_code(const std::string& key) {
static const std::unordered_map<std::string, int> keyMap = {
{ "Backspace", VK_BACK },
{ "Tab", VK_TAB },
{ "Enter", VK_RETURN },
{ "Shift", VK_SHIFT },
{ "Control", VK_CONTROL },
{ "Alt", VK_MENU },
{ "Pause", VK_PAUSE },
{ "CapsLock", VK_CAPITAL },
{ "Escape", VK_ESCAPE },
{ "Space", VK_SPACE },
{ "PageUp", VK_PRIOR },
{ "PageDown", VK_NEXT },
{ "End", VK_END },
{ "Home", VK_HOME },
{ "Left", VK_LEFT },
{ "Up", VK_UP },
{ "Right", VK_RIGHT },
{ "Down", VK_DOWN },
{ "Insert", VK_INSERT },
{ "Delete", VK_DELETE },
{ "F1", VK_F1 },
{ "F2", VK_F2 },
{ "F3", VK_F3 },
{ "F4", VK_F4 },
{ "F5", VK_F5 },
{ "F6", VK_F6 },
{ "F7", VK_F7 },
{ "F8", VK_F8 },
{ "F9", VK_F9 },
{ "F10", VK_F10 },
{ "F11", VK_F11 },
{ "F12", VK_F12 },
{ "0", '0' },
{ "1", '1' },
{ "2", '2' },
{ "3", '3' },
{ "4", '4' },
{ "5", '5' },
{ "6", '6' },
{ "7", '7' },
{ "8", '8' },
{ "9", '9' },
{ "A", 'A' },
{ "B", 'B' },
{ "C", 'C' },
{ "D", 'D' },
{ "E", 'E' },
{ "F", 'F' },
{ "G", 'G' },
{ "H", 'H' },
{ "I", 'I' },
{ "J", 'J' },
{ "K", 'K' },
{ "L", 'L' },
{ "M", 'M' },
{ "N", 'N' },
{ "O", 'O' },
{ "P", 'P' },
{ "Q", 'Q' },
{ "R", 'R' },
{ "S", 'S' },
{ "T", 'T' },
{ "U", 'U' },
{ "V", 'V' },
{ "W", 'W' },
{ "X", 'X' },
{ "Y", 'Y' },
{ "Z", 'Z' },
{ "`", VK_OEM_3 },
{ "-", VK_OEM_MINUS },
{ "=", VK_OEM_PLUS },
{ "[", VK_OEM_4 },
{ "]", VK_OEM_5 },
{ "\\\\", VK_OEM_6 },
{ ";", VK_OEM_1 },
{ "'", VK_OEM_7 },
{ ",", VK_OEM_COMMA },
{ ".", VK_OEM_PERIOD },
{ "/", VK_OEM_2 }
};
auto it = keyMap.find(key);
if (it != keyMap.end()) {
return it->second;
}
return 0;
}
DWORD get_mod_code(const std::string& key) {
if (key == "Ctrl") {
return MOD_CONTROL;
}
else if (key == "Alt") {
return MOD_ALT;
}
else if (key == "Shift") {
return MOD_SHIFT;
}
else if (key == "Windows") {
return MOD_WIN;
}
return 0; // No modifier
}
// Function to parse hotkey string
bool parse_hotkey(const std::string& hotkeyStr, DWORD& modKeys, int& vkKey) {
std::stringstream ss(hotkeyStr);
std::string keyPart;
modKeys = 0;
vkKey = 0;
while (std::getline(ss, keyPart, '+')) {
DWORD mod = get_mod_code(keyPart);
if (mod != 0) {
modKeys |= mod;
}
else {
vkKey = get_vk_code(keyPart);
if (vkKey == 0) {
return false; // Invalid key
}
}
}
return true; // Success
}
bool hotkey_pressed(int id) {
if (g_Hotkeys[id] == true) {
g_Hotkeys[id] = false;
return true;
}
return false;
}
void wait(int time) {
timer t;
for (int64_t i = 0; i < time; ++i) {
if (t.elapsed() > time)break;
Sleep(1);
update_window(g_MainWindow, false);
}
update_window(g_MainWindow, false);
}
timer::timer()
{
restart();
}
int timer::elapsed()
{
if (paused != 0)
{
return paused;
}
else
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - initTime).count();
}
}
void timer::elapsed(int amount)
{
if (paused == 0)
{
initTime = std::chrono::steady_clock::now() - std::chrono::milliseconds(amount);
}
else
{
paused = amount;
}
}
void timer::restart()
{
initTime = std::chrono::steady_clock::now();
paused = 0;
}
void timer::pause()
{
paused = elapsed();
}
void timer::resume()
{
initTime = std::chrono::steady_clock::now() - std::chrono::milliseconds(paused);
paused = 0;
}
void add_list_item(HWND hwndList, LPCTSTR lpszItem)
{
SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)lpszItem);
}
void add_text(HWND hwndEdit, LPCTSTR lpszText)
{
int nLength = GetWindowTextLength(hwndEdit);
SendMessage(hwndEdit, EM_SETSEL, (WPARAM)nLength, (LPARAM)nLength);
SendMessage(hwndEdit, EM_REPLACESEL, 0, (LPARAM)lpszText);
}
std::wstring get_text(HWND hwndEdit)
{
int nLength = GetWindowTextLength(hwndEdit);
std::wstring text;
if (nLength > 0)
{
text.resize(nLength + 1);
GetWindowText(hwndEdit, &text[0], nLength + 1);
}
return text;
}
void clear_list(HWND hwndList)
{
SendMessage(hwndList, LB_RESETCONTENT, 0, 0);
}
HWND create_button(HWND hwndParent, LPCTSTR lpszCaption, int x, int y, int width, int height, int id)
{
return CreateWindow(
TEXT("BUTTON"),
lpszCaption,
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | WS_TABSTOP,
x,
y,
width,
height,
hwndParent,
(HMENU)id,
NULL,
NULL
);
}
HWND create_text(HWND hwndParent, LPCTSTR lpszCaption, int x, int y, int width, int height, int id)
{
return CreateWindow(
TEXT("STATIC"),
lpszCaption,
WS_VISIBLE | WS_CHILD,
x,
y,
width,
height,
hwndParent,
(HMENU)id,
NULL,
NULL
);
}
HWND create_checkbox(HWND hwndParent, LPCTSTR lpszCaption, int x, int y, int width, int height, int id)
{
return CreateWindow(
TEXT("BUTTON"),
lpszCaption,
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | WS_TABSTOP,
x,
y,
width,
height,
hwndParent,
(HMENU)id,
NULL,
NULL
);
}
HWND create_input_box(HWND hwndParent, bool secure, bool multiline, int x, int y, int width, int height, int id)
{
DWORD style = WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL | WS_TABSTOP;
if (secure)
{
style |= ES_PASSWORD;
}
if (multiline)
style |= ES_MULTILINE | ES_AUTOVSCROLL;
HWND window = CreateWindow(
TEXT("EDIT"),
NULL,
style,
x,
y,
width,
height,
hwndParent,
(HMENU)id,
NULL,
NULL
);
originalEditProc = (WNDPROC)SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)Edit_Prc);
return window;
}
HWND create_list(HWND hwndParent, int x, int y, int width, int height, int id)
{
return CreateWindow(
TEXT("LISTBOX"),
NULL,
WS_VISIBLE | WS_CHILD | WS_TABSTOP,
x,
y,
width,
height,
hwndParent,
(HMENU)id,
NULL,
NULL
);
}
void delete_control(HWND hwnd)
{
DestroyWindow(hwnd);
}
void delete_list_item(HWND hwndList, int index)
{
SendMessage(hwndList, LB_DELETESTRING, (WPARAM)index, 0);
}
void delete_list_selections(HWND hwndList)
{
int count = SendMessage(hwndList, LB_GETSELCOUNT, 0, 0);
if (count > 0)
{
std::vector<int> selections(count);
SendMessage(hwndList, LB_GETSELITEMS, (WPARAM)count, (LPARAM)selections.data());
for (int i = count - 1; i >= 0; i--)
{
SendMessage(hwndList, LB_DELETESTRING, (WPARAM)selections[i], 0);
}
}
}
void edit_list_item(HWND hwndList, int index, LPCTSTR lpszItem)
{
SendMessage(hwndList, LB_DELETESTRING, (WPARAM)index, 0);
SendMessage(hwndList, LB_INSERTSTRING, (WPARAM)index, (LPARAM)lpszItem);
}
void edit_text(HWND hwndEdit, LPCTSTR lpszText)
{
SetWindowText(hwndEdit, lpszText);
}
void focus(HWND hwnd)
{
SetFocus(hwnd);
}
HWND get_current_focus()
{
return GetFocus();
}
int get_list_position(HWND hwndList)
{
return SendMessage(hwndList, LB_GETCURSEL, 0, 0);
}
std::wstring get_focused_list_item_name(HWND hwndList)
{
int focusedIndex = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
if (focusedIndex != LB_ERR)
{
int textLength = SendMessage(hwndList, LB_GETTEXTLEN, focusedIndex, 0);
if (textLength != LB_ERR)
{
std::wstring listItemText;
listItemText.resize(textLength + 1); // Add 1 for null terminator
SendMessage(hwndList, LB_GETTEXT, focusedIndex, reinterpret_cast<LPARAM>(listItemText.data()));
listItemText.pop_back(); // Remove the null terminator
return listItemText;
}
}
return L""; // Return an empty string if there's an error
}
bool is_checked(HWND hwndCheckbox)
{
return SendMessage(hwndCheckbox, BM_GETCHECK, 0, 0) == BST_CHECKED;
}
bool is_enabled(HWND hwnd)
{
return IsWindowEnabled(hwnd) != FALSE;
}
bool is_multiline(HWND hwndEdit)
{
return (GetWindowLong(hwndEdit, GWL_STYLE) & ES_MULTILINE) != 0;
}
bool is_pressed(HWND hwndButton)
{
return (SendMessage(hwndButton, BM_GETSTATE, 0, 0) & BST_PUSHED) or (GetFocus() == hwndButton && key_pressed(VK_RETURN));
}
bool is_readOnly(HWND hwndEdit)
{
return (GetWindowLong(hwndEdit, GWL_STYLE) & ES_READONLY) != 0;
}
bool is_visible(HWND hwnd)
{
return IsWindowVisible(hwnd) != FALSE;
}
void set_checkboxMark(HWND hwndCheckbox, bool checked)
{
SendMessage(hwndCheckbox, BM_SETCHECK, checked ? BST_CHECKED : BST_UNCHECKED, 0);
}
void set_list_position(HWND hwndList, int index)
{
SendMessage(hwndList, LB_SETCURSEL, (WPARAM)index, 0);
}
void set_text(HWND hwndEdit, LPCTSTR lpszText)
{
SetWindowText(hwndEdit, lpszText);
}
}