Repository navigation
Expand file tree
/
Copy pathUIManager.cpp
More file actions
612 lines (525 loc) · 15.4 KB
/
Copy pathUIManager.cpp
File metadata and controls
612 lines (525 loc) · 15.4 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
#include "UIManager.h"
#include "NetworkManager.h"
#include "ConfigManager.h"
#include "Commctrl.h"
UIManager* UIManager::instance = NULL;
NetworkManager* UIManager::netManager = NULL;
ConfigManager* UIManager::configManager = NULL;
#define DL_TEXT_COLOUR RGB(225, 225, 225)
#define UL_TEXT_COLOUR RGB(10, 225, 10)
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
UIManager::UIManager(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
instance = this;
netManager = new NetworkManager();
INITCOMMONCONTROLSEX icc = { sizeof(icc), ICC_LINK_CLASS };
InitCommonControlsEx(&icc);
//Convert args to ansi as ConfigManager uses ansi for everything else...
//First get required buffer size
int reqBufSize = WideCharToMultiByte(CP_UTF8, 0, lpCmdLine, -1, NULL, 0, NULL, NULL);
//Alocate
ansiArgs = (char*)malloc(reqBufSize);
char* dirStart = nullptr;
if (ansiArgs)
{
//Actually perform the conversion
WideCharToMultiByte(CP_UTF8, 0, lpCmdLine, _tcslen(lpCmdLine), &ansiArgs[0], reqBufSize, NULL, NULL);
ansiArgs[reqBufSize - 1] = 0;
if (!strncmp(ansiArgs, "-configdir=", 11)) //If we passed in an argument
{
dirStart = &ansiArgs[11]; //Get the path
}
}
configManager = new ConfigManager(dirStart); //Passing in potential nullptr is safe as configmanager expects one
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_DOWNLOAD_APP, szWindowClass, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_CHILD_STATIC_NAME, szChildStaticWindowClass, MAX_LOADSTRING);
RegisterWindowClass(hInstance);
ATOM a = RegisterChildWindowClass(hInstance);
roothWnd = InitInstance(hInstance, nCmdShow);
// Perform application initialization:
if (roothWnd == NULL)
{
MessageBox(NULL, L"Failed to create window, shutting down!", L"Error", MB_OK);
return;
}
if (configManager->failedToInit) //A bit lazy but if we don't have a valid config path, kill the program
{
MessageBox(NULL, L"Failed to initialise config, is the supplied path valid?", L"Error", MB_OK);
SendMessage(roothWnd, WM_CLOSE, NULL, NULL);
return;
}
//Show the first run message if it's our first time running
if(!configManager->shownFirstRunMsg)
{
DialogBox(instance->hInst, MAKEINTRESOURCE(IDD_FIRSTRUN), roothWnd, AboutProc); //Just re-use this proc as it does the same thing
configManager->UpdateShowFirstRunMsg(true);
}
dlChildWindow = CreateWindow(szChildStaticWindowClass, L"DL_SPEED", WS_VISIBLE | WS_CHILDWINDOW | WS_BORDER | WS_EX_CLIENTEDGE, 10, 4, 100, 20, roothWnd, NULL, hInstance, NULL);
ulChildWindow = CreateWindow(szChildStaticWindowClass, L"UL_SPEED", WS_VISIBLE | WS_CHILDWINDOW | WS_BORDER | WS_EX_CLIENTEDGE, 110, 4, 100, 20, roothWnd, NULL, hInstance, NULL);
running = true;
std::thread mainThread = std::thread(UpdateInfo);
mainThread.detach();
//Move to last known pos
SetWindowPos(roothWnd, HWND_TOPMOST, configManager->lastX, configManager->lastY, 0, 0, SWP_NOSIZE);
}
UIManager::~UIManager()
{
free(ansiArgs);
Shell_NotifyIcon(NIM_DELETE, &trayIcon);
running = false;
delete netManager;
delete configManager;
}
void UIManager::UpdateInfo()
{
WCHAR dlBuf[100];
WCHAR ulBuf[100];
PMIB_IF_TABLE2* interfaces = (PMIB_IF_TABLE2*)malloc(sizeof(PMIB_IF_TABLE2));
if (!interfaces)
{
SendMessage(instance->roothWnd, WM_CLOSE, NULL, NULL);
return;
}
while (instance->running)
{
std::tuple<double, double> speedInfo = netManager->GetAdaptorInfo(instance->roothWnd, interfaces);
double dl = std::get<0>(speedInfo);
double ul = std::get<1>(speedInfo);
double dlMbps = dl / DIV_FACTOR;
double ulMbps = ul / DIV_FACTOR;
bool dlKbps = false;
bool ulKbps = false;
//If <1 Mbps, display as Kbps
if (dlMbps < 1.0)
{
dl /= KILOBYTE;
dlKbps = true;
}
else
{
dl = dlMbps;
}
if (ulMbps < 1.0)
{
ul /= KILOBYTE;
ulKbps = true;
}
else
{
ul = ulMbps;
}
swprintf_s(dlBuf, L"↓ %.1lf %s", dl, dlKbps ? L"kbps" : L"Mbps");
swprintf_s(ulBuf, L"↑ %.1lf %s", ul, ulKbps ? L"kbps" : L"Mbps");
SetWindowText(instance->dlChildWindow, dlBuf);
SetWindowText(instance->ulChildWindow, ulBuf);
memset(dlBuf, 0, 100);
memset(ulBuf, 0, 100);
std::this_thread::sleep_for(std::chrono::microseconds(ONE_SECOND));
}
free(interfaces);
}
ATOM UIManager::RegisterWindowClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LOGO));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wcex.lpszMenuName = L"";//MAKEINTRESOURCEW(IDC_NAME);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_LOGO));
return RegisterClassEx(&wcex);
}
ATOM UIManager::RegisterChildWindowClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | WS_EX_CLIENTEDGE;
wcex.lpfnWndProc = ChildProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LOGO));
wcex.hCursor = NULL;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wcex.lpszMenuName = L"";//MAKEINTRESOURCEW(IDC_NAME);
wcex.lpszClassName = szChildStaticWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_LOGO));
return RegisterClassEx(&wcex);
}
HWND UIManager::InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE, szWindowClass, szTitle, WS_POPUP,
CW_USEDEFAULT, 0, 220, 28, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return NULL;
}
UpdateOpacity(hWnd);
DWORD currentStyle = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLong(hWnd, GWL_STYLE, (currentStyle & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX));
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
//Tray icon
trayIcon.cbSize = sizeof(NOTIFYICONDATA);
trayIcon.hWnd = hWnd;
trayIcon.uID = 1;
trayIcon.uCallbackMessage = WM_TRAYMESSAGE;
trayIcon.uVersion = NOTIFYICON_VERSION;
trayIcon.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_LOGO));
LoadString(hInst, IDS_APP_TITLE, trayIcon.szTip, 128);
trayIcon.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
Shell_NotifyIcon(NIM_ADD, &trayIcon);
return hWnd;
}
void UIManager::UpdateOpacity(HWND hWnd)
{
SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), configManager->opacity, LWA_ALPHA);
}
void UIManager::OnSelectItem(int sel)
{
//0 == No selection
if (sel == EXIT)
{
SendMessage(roothWnd, WM_CLOSE, NULL, NULL);
}
if (sel == ABOUT)
{
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), roothWnd, AboutProc);
}
if (sel == SETTINGS)
{
DialogBox(hInst, MAKEINTRESOURCE(IDD_SETTINGS), roothWnd, SettingsProc);
}
}
//For child windows which hold DL/UL texts
LRESULT CALLBACK UIManager::ChildProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
break;
}
//Pass these on to the root window
case WM_LBUTTONDOWN:
WndProc(instance->roothWnd, message, wParam, lParam);
break;
case WM_LBUTTONUP:
WndProc(instance->roothWnd, message, wParam, lParam);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps); //CHILD WINDOW BACKGROUND COLOUR
HBRUSH brush = CreateSolidBrush(*instance->configManager->childColour);
FillRect(hdc, &ps.rcPaint, brush);
SetBkMode(hdc, TRANSPARENT);
if (hWnd == instance->dlChildWindow)
{
SetTextColor(hdc, DL_TEXT_COLOUR);
DrawText(hdc, instance->dlBuf, lstrlenW(instance->dlBuf), &ps.rcPaint, DT_CENTER | DT_VCENTER);
}
else if (hWnd == instance->ulChildWindow)
{
SetTextColor(hdc, UL_TEXT_COLOUR);
DrawText(hdc, instance->ulBuf, lstrlenW(instance->ulBuf), &ps.rcPaint, DT_CENTER | DT_VCENTER);
}
EndPaint(hWnd, &ps);
DeleteObject(brush);
break;
}
case WM_SETTEXT:
{
//Intercept this message and handle it ourselves
LPWSTR msg = (LPWSTR)lParam;
if (hWnd == instance->dlChildWindow)
{
ZeroMemory(instance->dlBuf, 200);
wcscpy(instance->dlBuf, msg);
}
else if (hWnd == instance->ulChildWindow)
{
ZeroMemory(instance->ulBuf, 200);
wcscpy(instance->ulBuf, msg);
}
RECT rc;
GetClientRect(hWnd, &rc);
//Force a repaint (Will call WM_PAINT)
InvalidateRect(hWnd, &rc, FALSE);
break;
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
LRESULT CALLBACK UIManager::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_TRAYMESSAGE:
{
switch (lParam)
{
case WM_RBUTTONDOWN:
{
//Forward onto WM_CONTEXTMENU, as it does the same thing
WndProc(hWnd, WM_CONTEXTMENU, wParam, lParam);
break;
}
default:
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
}
case WM_CONTEXTMENU:
{
POINT point;
GetCursorPos(&point);
HMENU menu = CreatePopupMenu();
AppendMenu(menu, MF_STRING, EXIT, L"Exit");
AppendMenu(menu, MF_STRING, ABOUT, L"About");
AppendMenu(menu, MF_STRING, SETTINGS, L"Settings");
SetForegroundWindow(instance->roothWnd); //TrackPopupMenu requires the parent window to be in foreground, otherwise the popupmenu won't be destroyed when clicking off it
instance->OnSelectItem(TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, point.x, point.y, 0, instance->roothWnd, NULL));
break;
}
case WM_LBUTTONDOWN:
{
//Restrict mouse input to current window
SetCapture(hWnd);
break;
}
case WM_LBUTTONUP:
{
//Window no longer requires all mouse input
ReleaseCapture();
instance->WriteWindowPos();
break;
}
case WM_MOUSEMOVE:
{
if (GetCapture() == hWnd) //Check if this window has mouse input
{
RECT windowRect;
POINT mousePos;
GetWindowRect(hWnd, &windowRect);
//Get the current mouse coordinates
mousePos.x = (int)(short)LOWORD(lParam);
mousePos.y = (int)(short)HIWORD(lParam);
int windowHeight = windowRect.bottom - windowRect.top;
int windowWidth = windowRect.right - windowRect.left;
ClientToScreen(hWnd, &mousePos);
//Set the window's new screen position
MoveWindow(hWnd, mousePos.x - (windowWidth / 2), mousePos.y, windowWidth, windowHeight, TRUE);
}
}
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
{
DialogBox(instance->hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), instance->roothWnd, AboutProc);
break;
}
case IDM_EXIT:
{
DestroyWindow(hWnd);
break;
}
default:
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps); //OUTER/PRIMARY COLOUR
HBRUSH brush = CreateSolidBrush(*instance->configManager->foregroundColour);
FillRect(hdc, &ps.rcPaint, brush);
EndPaint(hWnd, &ps);
DeleteObject(brush);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
void UIManager::WriteWindowPos()
{
RECT rect = { NULL };
GetWindowRect(roothWnd, &rect);
int x = rect.left;
int y = rect.top;
configManager->UpdateWindowPos(x, y);
}
INT_PTR CALLBACK UIManager::AboutProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
{
return (INT_PTR)TRUE;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
case WM_NOTIFY:
{
//Handle hyperlink
switch (((LPNMHDR)lParam)->code)
{
case NM_CLICK: // Fall through
case NM_RETURN:
{
PNMLINK pNMLink = (PNMLINK)lParam;
LITEM item = pNMLink->item;
ShellExecute(NULL, L"open", item.szUrl, NULL, NULL, SW_SHOW);
break;
}
}
break;
}
}
return (INT_PTR)FALSE;
}
Vector2 UIManager::GetCentredXYPos(HWND hWnd)
{
int currDPI = GetDpiForWindow(hWnd);
int screenWidth = GetSystemMetricsForDpi(SM_CXSCREEN, currDPI);
int screenHeight = GetSystemMetricsForDpi(SM_CYSCREEN, currDPI);
RECT rc;
GetWindowRect(hWnd, &rc);
int winWidth = rc.right - rc.left;
int winHeight = rc.bottom - rc.top;
int xPos = (screenWidth - winWidth) / 2;
int yPos = (screenHeight - winHeight) / 2;
return Vector2(xPos, yPos);
}
INT_PTR CALLBACK UIManager::SettingsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
{
return (INT_PTR)TRUE;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == IDCLOSE || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
else if (LOWORD(wParam) == IDC_PRIMARY_COLOUR || LOWORD(wParam) == IDC_SECONDARY_COLOUR)
{
bool isPrimary = LOWORD(wParam) == IDC_PRIMARY_COLOUR;
CHOOSECOLOR colorStruct = { 0 };
colorStruct.hwndOwner = hDlg;
colorStruct.lStructSize = sizeof(CHOOSECOLOR);
colorStruct.rgbResult = isPrimary ? *instance->configManager->foregroundColour : *instance->configManager->childColour;
colorStruct.lpCustColors = instance->configManager->customColBuf;
colorStruct.Flags = CC_ANYCOLOR | CC_RGBINIT;
ChooseColor(&colorStruct);
if (isPrimary)
{
instance->configManager->UpdateForegroundColour(colorStruct.rgbResult);
}
else
{
instance->configManager->UpdateChildColour(colorStruct.rgbResult);
}
//Force repaint to apply colours
instance->ForceRepaint();
}
else if (LOWORD(wParam) == IDC_RESET_COLOURS)
{
instance->configManager->ResetConfig();
instance->UpdateOpacity(instance->roothWnd);
instance->ForceRepaint();
}
else if (LOWORD(wParam) == IDC_OPACITY)
{
DialogBox(instance->hInst, MAKEINTRESOURCE(IDD_OPACITY), hDlg, OpacityProc);
}
break;
}
}
return (INT_PTR)FALSE;
}
INT_PTR CALLBACK UIManager::OpacityProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
{
HWND sliderCtrl = GetDlgItem(hDlg, IDC_OPACITY_SLIDER);
SendMessage(sliderCtrl, TBM_SETRANGE, (WPARAM)TRUE, (LPARAM)MAKELPARAM(MIN_OPACITY, MAX_OPACITY));
SendMessage(sliderCtrl, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)instance->configManager->opacity);
return (INT_PTR)TRUE;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == IDOK)
{
HWND sliderCtrl = GetDlgItem(hDlg, IDC_OPACITY_SLIDER);
int newVal = SendMessage(sliderCtrl, TBM_GETPOS, NULL, NULL);
if (newVal <= MAX_OPACITY && newVal >= MIN_OPACITY)
{
instance->configManager->UpdateOpacity(newVal);
instance->UpdateOpacity(instance->roothWnd);
instance->ForceRepaint();
}
else
{
DialogBox(instance->hInst, MAKEINTRESOURCE(IDD_INVALID_INPUT), hDlg, AboutProc); //Just re-use this proc as it does the same thing
}
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
}
return (INT_PTR)FALSE;
}
void UIManager::ForceRepaint()
{
RECT rc;
GetClientRect(roothWnd, &rc);
InvalidateRect(roothWnd, &rc, FALSE);
}