-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
MainDlg.cpp
669 lines (550 loc) · 14.6 KB
/
MainDlg.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
#include "stdafx.h"
#include "resource.h"
#include "MainDlg.h"
#include "TextDlg.h"
#include "SettingsDlg.h"
#include "Functions.h"
#include "update.h"
#include "version.h"
BOOL CMainDlg::OnInitDialog(CWindow wndFocus, LPARAM lInitParam)
{
// Center the dialog on the screen.
CenterWindow();
// Set icons.
ReloadMainIcon();
// Load and apply config.
m_config.emplace();
ApplyUiLanguage();
ApplyMouseAndKeyboardHotKeys();
ConfigToGui();
if(lInitParam || m_config->m_hideWndOnStartup)
{
m_hideDialog = true;
// Setting WS_EX_NOACTIVATE is a workaround for preventing the dialog
// from stealing focus on startup.
ModifyStyleEx(0, WS_EX_NOACTIVATE);
}
// Init and show tray icon.
InitNotifyIconData();
if(!m_config->m_hideTrayIcon)
{
Shell_NotifyIcon(NIM_ADD, &m_notifyIconData);
}
// Start timer to check for updates.
if(m_config->m_checkForUpdates)
{
SetTimer(TIMER_UPDATE_CHECK, 1000 * 10, NULL); // 10sec
}
return TRUE;
}
void CMainDlg::OnDestroy()
{
UninitMouseAndKeyboardHotKeys();
if(!m_config->m_hideTrayIcon)
{
Shell_NotifyIcon(NIM_DELETE, &m_notifyIconData);
}
if(m_config->m_checkForUpdates)
{
KillTimer(TIMER_UPDATE_CHECK);
}
// From GDI handle checks, not all icons are freed automatically.
::DestroyIcon(SetIcon(nullptr, TRUE));
::DestroyIcon(SetIcon(nullptr, FALSE));
::DestroyIcon(CStatic(GetDlgItem(IDC_MAIN_ICON)).SetIcon(nullptr));
}
void CMainDlg::OnWindowPosChanging(LPWINDOWPOS lpWndPos)
{
if(m_hideDialog && (lpWndPos->flags & SWP_SHOWWINDOW))
{
ModifyStyleEx(WS_EX_NOACTIVATE, 0);
lpWndPos->flags &= ~SWP_SHOWWINDOW;
m_hideDialog = false;
}
}
LRESULT CMainDlg::OnNotify(int idCtrl, LPNMHDR pnmh)
{
switch(pnmh->idFrom)
{
case IDC_MAIN_SYSLINK:
switch(pnmh->code)
{
case NM_CLICK:
case NM_RETURN:
if((int)ShellExecute(m_hWnd, L"open", ((PNMLINK)pnmh)->item.szUrl, NULL, NULL, SW_SHOWNORMAL) <= 32)
{
CString title;
title.LoadString(IDS_ERROR);
CString text;
text.LoadString(IDS_ERROR_OPEN_ADDRESS);
text += L"\n";
text += ((PNMLINK)pnmh)->item.szUrl;
MessageBox(text, title, MB_ICONERROR);
}
break;
}
break;
}
SetMsgHandled(FALSE);
return 0;
}
void CMainDlg::OnHotKey(int nHotKeyID, UINT uModifiers, UINT uVirtKey)
{
if(nHotKeyID == 1)
{
CPoint pt;
GetCursorPos(&pt);
CTextDlg dlgText(*m_config);
dlgText.DoModal(NULL, reinterpret_cast<LPARAM>(&pt));
}
}
void CMainDlg::OnTimer(UINT_PTR nIDEvent)
{
if(nIDEvent == TIMER_UPDATE_CHECK)
{
KillTimer(TIMER_UPDATE_CHECK);
if(UpdateCheckInit(m_hWnd, UWM_UPDATE_CHECKED))
{
if(UpdateCheckQueue())
{
m_checkingForUpdates = true;
}
else
{
UpdateCheckCleanup();
}
}
if(!m_checkingForUpdates)
{
SetTimer(TIMER_UPDATE_CHECK, 1000 * 60 * 60, NULL); // 1h
}
}
}
LRESULT CMainDlg::OnDpiChanged(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
ReloadMainIcon();
return 0;
}
void CMainDlg::OnOK(UINT uNotifyCode, int nID, CWindow wndCtl)
{
bool ctrlKey = (CButton(GetDlgItem(IDC_CHECK_CTRL)).GetCheck() != BST_UNCHECKED);
bool altKey = (CButton(GetDlgItem(IDC_CHECK_ALT)).GetCheck() != BST_UNCHECKED);
bool shiftKey = (CButton(GetDlgItem(IDC_CHECK_SHIFT)).GetCheck() != BST_UNCHECKED);
if(!ctrlKey && !altKey && !shiftKey)
{
CString title;
title.LoadString(IDS_MAINDLG_WARNING_MODIFIER_TITLE);
CString text;
text.LoadString(IDS_MAINDLG_WARNING_MODIFIER_TEXT);
if(MessageBox(text, title, MB_ICONWARNING | MB_YESNO) != IDYES)
{
return;
}
}
int mouseKey;
auto keysComboWnd = CComboBox(GetDlgItem(IDC_COMBO_KEYS));
switch(keysComboWnd.GetCurSel())
{
case 0:
mouseKey = VK_LBUTTON;
break;
case 1:
mouseKey = VK_RBUTTON;
break;
case 2:
mouseKey = VK_MBUTTON;
break;
}
HotKey& mouseHotKey = m_config->m_mouseHotKey;
mouseHotKey.ctrl = ctrlKey;
mouseHotKey.alt = altKey;
mouseHotKey.shift = shiftKey;
mouseHotKey.key = mouseKey;
m_config->SaveToIniFile();
ApplyMouseAndKeyboardHotKeys();
CButton(GetDlgItem(IDOK)).EnableWindow(FALSE);
}
void CMainDlg::OnCancel(UINT uNotifyCode, int nID, CWindow wndCtl)
{
ShowWindow(SW_HIDE);
}
void CMainDlg::OnShowIni(UINT uNotifyCode, int nID, CWindow wndCtl)
{
CSettingsDlg settingsDlg;
INT_PTR nRet = settingsDlg.DoModal();
if(nRet == IDOK)
{
LANGID oldUiLanguage = m_config->m_uiLanguage;
bool oldCheckForUpdates = m_config->m_checkForUpdates;
bool oldHideTrayIcon = m_config->m_hideTrayIcon;
m_config.emplace();
LANGID newUiLanguage = m_config->m_uiLanguage;
bool newCheckForUpdates = m_config->m_checkForUpdates;
bool newHideTrayIcon = m_config->m_hideTrayIcon;
if(oldUiLanguage != newUiLanguage)
{
ApplyUiLanguage();
}
ApplyMouseAndKeyboardHotKeys();
ConfigToGui();
if(newHideTrayIcon != oldHideTrayIcon)
{
Shell_NotifyIcon(newHideTrayIcon ? NIM_DELETE : NIM_ADD, &m_notifyIconData);
}
if(newCheckForUpdates != oldCheckForUpdates && !m_checkingForUpdates)
{
if(newCheckForUpdates)
{
SetTimer(TIMER_UPDATE_CHECK, 1000 * 10, NULL); // 10sec
}
else
{
KillTimer(TIMER_UPDATE_CHECK);
}
}
}
}
void CMainDlg::OnExitButton(UINT uNotifyCode, int nID, CWindow wndCtl)
{
Exit();
}
void CMainDlg::OnConfigChanged(UINT uNotifyCode, int nID, CWindow wndCtl)
{
CButton(GetDlgItem(IDOK)).EnableWindow();
}
LRESULT CMainDlg::OnMouseHookClicked(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
//CPoint ptEvent{ static_cast<int>(wParam), static_cast<int>(lParam) };
// Instead of using the mouse hook coordinates, we query the cursor position
// again, because the hooked coordinates end up incorrect for DPI-unaware
// applications in high DPI environment.
CPoint pt;
GetCursorPos(&pt);
CTextDlg dlgText(*m_config);
dlgText.DoModal(NULL, reinterpret_cast<LPARAM>(&pt));
return 0;
}
LRESULT CMainDlg::OnTaskbarCreated(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(!m_config->m_hideTrayIcon)
{
// Reload icon since the DPI might have changed. From the documentation:
// "On Windows 10, the taskbar also broadcasts this message when the DPI of
// the primary display changes."
m_trayIcon = LoadTrayIcon();
m_notifyIconData.hIcon = m_trayIcon;
Shell_NotifyIcon(NIM_ADD, &m_notifyIconData);
// Necessary to apply the newly loaded icon in Windows 11 22H2.
Shell_NotifyIcon(NIM_MODIFY, &m_notifyIconData);
}
return 0;
}
LRESULT CMainDlg::OnCustomTextifyMsg(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(lParam)
{
case 1:
Exit();
break;
}
return 0;
}
LRESULT CMainDlg::OnNotifyIcon(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(wParam == 1)
{
switch(lParam)
{
case WM_LBUTTONUP:
ShowWindow(SW_SHOWNORMAL);
::SetForegroundWindow(GetLastActivePopup());
break;
case WM_RBUTTONUP:
if(IsWindowEnabled())
{
::SetForegroundWindow(m_hWnd);
NotifyIconRightClickMenu();
}
else
{
ShowWindow(SW_SHOWNORMAL);
::SetForegroundWindow(GetLastActivePopup());
}
break;
}
}
return 0;
}
LRESULT CMainDlg::OnBringToFront(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
ShowWindow(SW_SHOWNORMAL);
::SetForegroundWindow(GetLastActivePopup());
return 0;
}
LRESULT CMainDlg::OnUpdateChecked(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
UpdateCheckCleanup();
if(m_config->m_checkForUpdates)
{
if(lParam == ERROR_SUCCESS)
{
DWORD dwUpdateVersion = UpdateCheckGetVersionLong();
if(dwUpdateVersion && dwUpdateVersion > VER_FILE_VERSION_LONG)
{
HWND hPopup = IsWindowEnabled() ? m_hWnd : GetLastActivePopup();
UpdateTaskDialog(hPopup, UpdateCheckGetVersion());
}
UpdateCheckFreeVersion();
SetTimer(TIMER_UPDATE_CHECK, 1000 * 60 * 60 * 24, NULL); // 24h
}
else
{
SetTimer(TIMER_UPDATE_CHECK, 1000 * 60 * 60, NULL); // 1h
}
}
m_checkingForUpdates = false;
if(m_closeWhenUpdateCheckDone)
{
::PostQuitMessage(0);
}
return 0;
}
LRESULT CMainDlg::OnExit(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
Exit();
return 0;
}
void CMainDlg::ReloadMainIcon()
{
UINT dpi = GetDpiForWindowWithFallback(m_hWnd);
HICON mainIcon = LoadIconWithScaleDownWithFallback(
ModuleHelper::GetResourceInstance(),
MAKEINTRESOURCE(IDR_MAINFRAME),
GetSystemMetricsForDpiWithFallback(SM_CXICON, dpi),
GetSystemMetricsForDpiWithFallback(SM_CYICON, dpi));
CIcon prevMainIcon = SetIcon(mainIcon, TRUE);
CIcon prevDlgMainIcon = CStatic(GetDlgItem(IDC_MAIN_ICON)).SetIcon(CopyIcon(mainIcon));
HICON mainIconSmall = LoadIconWithScaleDownWithFallback(
ModuleHelper::GetResourceInstance(),
MAKEINTRESOURCE(IDR_MAINFRAME),
GetSystemMetricsForDpiWithFallback(SM_CXSMICON, dpi),
GetSystemMetricsForDpiWithFallback(SM_CYSMICON, dpi));
CIcon prevMainIconSmall = SetIcon(mainIconSmall, FALSE);
}
void CMainDlg::ApplyUiLanguage()
{
LANGID uiLanguage = m_config->m_uiLanguage;
if(!uiLanguage)
{
CRegKey regKey;
DWORD dwError = regKey.Open(HKEY_CURRENT_USER, L"Software\\Textify", KEY_QUERY_VALUE);
if(dwError == ERROR_SUCCESS)
{
DWORD dwLanguage;
dwError = regKey.QueryDWORDValue(L"language", dwLanguage);
if(dwError == ERROR_SUCCESS)
{
uiLanguage = static_cast<LANGID>(dwLanguage);
}
}
}
if(uiLanguage)
{
OSVERSIONINFO osvi = { sizeof(OSVERSIONINFO) };
if(GetVersionEx(&osvi) && osvi.dwMajorVersion >= 6)
{
::SetThreadUILanguage(uiLanguage);
}
else
{
::SetThreadLocale(uiLanguage);
}
}
CString str;
str.LoadString(IDS_MAINDLG_TITLE);
SetWindowText(str);
str.LoadString(IDS_MAINDLG_HOMEPAGE);
CString headerStr;
headerStr.LoadString(IDS_MAINDLG_HEADER);
headerStr.Replace(L"%s", VER_FILE_VERSION_WSTR);
headerStr += L"\n<a href=\"https://ramensoftware.com/textify\">" + str + L"</a>";
SetDlgItemText(IDC_MAIN_SYSLINK, headerStr);
str.LoadString(IDS_MAINDLG_MOUSE_SHORTCUT);
SetDlgItemText(IDC_MOUSE_SHORTCUT, str);
str.LoadString(IDS_MAINDLG_CTRL);
SetDlgItemText(IDC_CHECK_CTRL, str);
str.LoadString(IDS_MAINDLG_ALT);
SetDlgItemText(IDC_CHECK_ALT, str);
str.LoadString(IDS_MAINDLG_SHIFT);
SetDlgItemText(IDC_CHECK_SHIFT, str);
CComboBox keysComboWnd(GetDlgItem(IDC_COMBO_KEYS));
int keysComboCurSel = keysComboWnd.GetCurSel();
keysComboWnd.ResetContent();
str.LoadString(IDS_MAINDLG_MOUSE_LEFT);
keysComboWnd.AddString(str);
str.LoadString(IDS_MAINDLG_MOUSE_RIGHT);
keysComboWnd.AddString(str);
str.LoadString(IDS_MAINDLG_MOUSE_MIDDLE);
keysComboWnd.AddString(str);
keysComboWnd.SetCurSel(keysComboCurSel);
str.LoadString(IDS_MAINDLG_APPLY);
SetDlgItemText(IDOK, str);
str.LoadString(IDS_MAINDLG_ADVANCED);
SetDlgItemText(IDC_ADVANCED, str);
str.LoadString(IDS_MAINDLG_MORE_SETTINGS);
SetDlgItemText(IDC_SHOW_INI, str);
str.LoadString(IDS_MAINDLG_EXIT);
SetDlgItemText(IDC_EXIT, str);
}
void CMainDlg::ApplyMouseAndKeyboardHotKeys()
{
if(m_config->m_mouseHotKey.key != 0)
{
_ATLTRY
{
const HotKey & mouseHotKey = m_config->m_mouseHotKey;
m_mouseGlobalHook.emplace(m_hWnd, UWM_MOUSEHOOKCLICKED,
mouseHotKey.key, mouseHotKey.ctrl, mouseHotKey.alt, mouseHotKey.shift,
m_config->m_excludedPrograms);
}
_ATLCATCH(e)
{
CString str = AtlGetErrorDescription(e);
MessageBox(
L"The following error has occurred during the initialization of Textify:\n" + str,
L"Textify mouse hotkey initialization error", MB_ICONERROR);
}
}
else
{
m_mouseGlobalHook.reset();
}
if(m_config->m_keybdHotKey.key != 0 && !m_registeredHotKey)
{
m_registeredHotKey = RegisterConfiguredKeybdHotKey(m_config->m_keybdHotKey);
if(!m_registeredHotKey)
{
CString str = AtlGetErrorDescription(HRESULT_FROM_WIN32(GetLastError()));
MessageBox(
L"The following error has occurred during the initialization of Textify:\n" + str,
L"Textify keyboard hotkey initialization error", MB_ICONERROR);
}
}
else if(m_config->m_keybdHotKey.key == 0 && m_registeredHotKey)
{
::UnregisterHotKey(m_hWnd, 1);
m_registeredHotKey = false;
}
}
void CMainDlg::UninitMouseAndKeyboardHotKeys()
{
if(m_registeredHotKey)
{
::UnregisterHotKey(m_hWnd, 1);
m_registeredHotKey = false;
}
m_mouseGlobalHook.reset();
}
bool CMainDlg::RegisterConfiguredKeybdHotKey(const HotKey& keybdHotKey)
{
UINT hotKeyModifiers = 0;
if(keybdHotKey.ctrl)
hotKeyModifiers |= MOD_CONTROL;
if(keybdHotKey.alt)
hotKeyModifiers |= MOD_ALT;
if(keybdHotKey.shift)
hotKeyModifiers |= MOD_SHIFT;
// Set MOD_NOREPEAT only for Windows 7 and newer.
OSVERSIONINFO osvi = { sizeof(OSVERSIONINFO) };
if(GetVersionEx(&osvi))
{
if(osvi.dwMajorVersion > 6 || (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion >= 1))
{
hotKeyModifiers |= 0x4000 /*MOD_NOREPEAT*/;
}
}
return ::RegisterHotKey(m_hWnd, 1, hotKeyModifiers, keybdHotKey.key) != FALSE;
}
void CMainDlg::ConfigToGui()
{
const HotKey& mouseHotKey = m_config->m_mouseHotKey;
CButton(GetDlgItem(IDC_CHECK_CTRL)).SetCheck(mouseHotKey.ctrl ? BST_CHECKED : BST_UNCHECKED);
CButton(GetDlgItem(IDC_CHECK_ALT)).SetCheck(mouseHotKey.alt ? BST_CHECKED : BST_UNCHECKED);
CButton(GetDlgItem(IDC_CHECK_SHIFT)).SetCheck(mouseHotKey.shift ? BST_CHECKED : BST_UNCHECKED);
auto keysComboWnd = CComboBox(GetDlgItem(IDC_COMBO_KEYS));
switch(mouseHotKey.key)
{
case VK_LBUTTON:
keysComboWnd.SetCurSel(0);
break;
case VK_RBUTTON:
keysComboWnd.SetCurSel(1);
break;
case VK_MBUTTON:
keysComboWnd.SetCurSel(2);
break;
default:
keysComboWnd.SetCurSel(-1);
break;
}
CButton(GetDlgItem(IDOK)).EnableWindow(FALSE);
}
void CMainDlg::InitNotifyIconData()
{
m_trayIcon = LoadTrayIcon();
m_notifyIconData.cbSize = NOTIFYICONDATA_V1_SIZE;
m_notifyIconData.hWnd = m_hWnd;
m_notifyIconData.uID = 1;
m_notifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
m_notifyIconData.uCallbackMessage = UWM_NOTIFYICON;
m_notifyIconData.hIcon = m_trayIcon;
CString sWindowText;
GetWindowText(sWindowText);
wcscpy_s(m_notifyIconData.szTip, sWindowText);
}
HICON CMainDlg::LoadTrayIcon()
{
HWND hTaskbarWnd = FindWindow(L"Shell_TrayWnd", NULL);
UINT dpi = GetDpiForWindowWithFallback(hTaskbarWnd ? hTaskbarWnd : m_hWnd);
return LoadIconWithScaleDownWithFallback(
ModuleHelper::GetResourceInstance(),
MAKEINTRESOURCE(IDR_MAINFRAME),
GetSystemMetricsForDpiWithFallback(SM_CXSMICON, dpi),
GetSystemMetricsForDpiWithFallback(SM_CYSMICON, dpi));
}
void CMainDlg::NotifyIconRightClickMenu()
{
CMenu menu;
menu.CreatePopupMenu();
CString str;
str.LoadString(IDS_TRAY_TEXTIFY);
menu.AppendMenu(MF_STRING, RCMENU_SHOW, str);
menu.AppendMenu(MF_SEPARATOR);
str.LoadString(IDS_TRAY_EXIT);
menu.AppendMenu(MF_STRING, RCMENU_EXIT, str);
CPoint point;
GetCursorPos(&point);
int nCmd = menu.TrackPopupMenu(TPM_RIGHTBUTTON | TPM_RETURNCMD, point.x, point.y, m_hWnd);
switch(nCmd)
{
case RCMENU_SHOW:
ShowWindow(SW_SHOWNORMAL);
::SetForegroundWindow(GetLastActivePopup());
break;
case RCMENU_EXIT:
Exit();
break;
}
}
void CMainDlg::Exit()
{
if(m_checkingForUpdates)
{
UpdateCheckAbort();
m_closeWhenUpdateCheckDone = true;
}
else
{
::PostQuitMessage(0);
}
}