forked from m417z/Textify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainDlg.cpp
373 lines (308 loc) · 8.8 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
#include "stdafx.h"
#include "resource.h"
#include "MainDlg.h"
#include "TextDlg.h"
#include "SettingsDlg.h"
#include "version.h"
BOOL CMainDlg::OnInitDialog(CWindow wndFocus, LPARAM lInitParam)
{
m_hideDialog = (lInitParam != 0);
// Center the dialog on the screen.
CenterWindow();
// Set icons.
HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON));
SetIcon(hIcon, TRUE);
HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON));
SetIcon(hIconSmall, FALSE);
// Init dialog.
CString introText;
GetDlgItemText(IDC_MAIN_SYSLINK, introText);
introText.Replace(L"%s", VER_FILE_VERSION_WSTR);
SetDlgItemText(IDC_MAIN_SYSLINK, introText);
auto keysComboWnd = CComboBox(GetDlgItem(IDC_COMBO_KEYS));
keysComboWnd.AddString(L"Left mouse button");
keysComboWnd.AddString(L"Right mouse button");
keysComboWnd.AddString(L"Middle mouse button");
// Load and apply config.
m_config = std::make_unique<UserConfig>();
InitMouseAndKeyboardHotKeys();
ConfigToGui();
// Init and show tray icon.
InitNotifyIconData();
if(!m_config->m_hideTrayIcon)
{
Shell_NotifyIcon(NIM_ADD, &m_notifyIconData);
}
return TRUE;
}
void CMainDlg::OnDestroy()
{
UninitMouseAndKeyboardHotKeys();
if(!m_config->m_hideTrayIcon)
{
Shell_NotifyIcon(NIM_DELETE, &m_notifyIconData);
}
}
void CMainDlg::OnWindowPosChanging(LPWINDOWPOS lpWndPos)
{
if(m_hideDialog && (lpWndPos->flags & SWP_SHOWWINDOW))
{
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 str = L"An error occurred while trying to open the following website address:\n";
str += ((PNMLINK)pnmh)->item.szUrl;
MessageBox(str, NULL, MB_ICONHAND);
}
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->m_webButtonInfos, m_config->m_autoCopySelection, m_config->m_unicodeSpacesToAscii);
dlgText.DoModal(NULL, reinterpret_cast<LPARAM>(&pt));
}
}
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)
{
if(MessageBox(L"You are about to set a mouse shortcut without modifier keys.\n"
L"Are you sure that you'd like to proceed?", L"Please confirm", 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();
m_mouseGlobalHook->SetNewHotkey(mouseKey, ctrlKey, altKey, shiftKey);
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)
{
bool oldHideTrayIcon = m_config->m_hideTrayIcon;
m_config = std::make_unique<UserConfig>();
UninitMouseAndKeyboardHotKeys();
InitMouseAndKeyboardHotKeys();
ConfigToGui();
bool newHideTrayIcon = m_config->m_hideTrayIcon;
if(newHideTrayIcon != oldHideTrayIcon)
{
Shell_NotifyIcon(newHideTrayIcon ? NIM_DELETE : NIM_ADD, &m_notifyIconData);
}
}
}
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->m_webButtonInfos, m_config->m_autoCopySelection, m_config->m_unicodeSpacesToAscii);
return dlgText.DoModal(NULL, reinterpret_cast<LPARAM>(&pt));
}
LRESULT CMainDlg::OnTaskbarCreated(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(!m_config->m_hideTrayIcon)
{
Shell_NotifyIcon(NIM_ADD, &m_notifyIconData);
}
return 0;
}
LRESULT CMainDlg::OnCustomTextifyMsg(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(lParam)
{
case 1:
EndDialog(0);
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::OnExit(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
EndDialog(0);
return 0;
}
void CMainDlg::InitMouseAndKeyboardHotKeys()
{
_ATLTRY
{
const HotKey& mouseHotKey = m_config->m_mouseHotKey;
m_mouseGlobalHook = std::make_unique<MouseGlobalHook>(m_hWnd, UWM_MOUSEHOOKCLICKED,
mouseHotKey.key, mouseHotKey.ctrl, mouseHotKey.alt, mouseHotKey.shift);
}
_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);
}
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);
}
}
void CMainDlg::UninitMouseAndKeyboardHotKeys()
{
if(m_registeredHotKey)
::UnregisterHotKey(m_hWnd, 1);
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;
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;
}
CButton(GetDlgItem(IDOK)).EnableWindow(FALSE);
}
void CMainDlg::InitNotifyIconData()
{
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 = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON));
CString sWindowText;
GetWindowText(sWindowText);
wcscpy_s(m_notifyIconData.szTip, sWindowText);
}
void CMainDlg::NotifyIconRightClickMenu()
{
CMenu menu;
menu.CreatePopupMenu();
menu.AppendMenu(MF_STRING, RCMENU_SHOW, L"Textify");
menu.AppendMenu(MF_SEPARATOR);
menu.AppendMenu(MF_STRING, RCMENU_EXIT, L"Exit");
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:
EndDialog(0);
break;
}
}