-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwnd.cpp
528 lines (437 loc) · 16.2 KB
/
mainwnd.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
#include "stdafx.h"
#include "common.h"
#include "options.h"
#include "optionsdlg.h"
#include "optpins.h"
#include "optautopin.h"
#include "opthotkeys.h"
#include "optlang.h"
#include "apputils.h"
#include "util.h"
#include "pinlayerwnd.h"
#include "pinwnd.h"
#include "mainwnd.h"
LPCWSTR MainWnd::className = L"EFDeskPins";
// About dialog.
//
class AboutDlg {
public:
static BOOL CALLBACK proc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
static HFONT boldGUIFont, underlineGUIFont;
switch (msg) {
case WM_INITDIALOG:
return evInitDlg(wnd, boldGUIFont, underlineGUIFont);
case WM_DESTROY:
evTermDlg(wnd, boldGUIFont, underlineGUIFont);
return true;
case App::WM_PINSTATUS:
updateStatusMessage(wnd);
return true;
//case WM_ACTIVATE:
// app.activeModelessDlg = (LOWORD(wparam) == WA_INACTIVE) ? 0 : wnd;
// return false;
case WM_COMMAND: {
int id = LOWORD(wparam);
int code = HIWORD(wparam);
switch (id) {
case IDOK:
case IDCANCEL:
DestroyWindow(wnd);
return true;
case IDC_LOGO:
if (code == STN_DBLCLK) {
showSpecialInfo(wnd);
return true;
}
}
return false;
}
}
return false;
}
static void updateStatusMessage(HWND wnd)
{
SetDlgItemInt(wnd, IDC_STATUS, app.pinsUsed, false);
}
static bool evInitDlg(HWND wnd, HFONT& boldGUIFont, HFONT& underlineGUIFont)
{
app.aboutDlg = wnd;
updateStatusMessage(wnd);
boldGUIFont = ef::Win::FontH::create(ef::Win::FontH::getStockDefaultGui(), 0, ef::Win::FontH::bold);
underlineGUIFont = ef::Win::FontH::create(ef::Win::FontH::getStockDefaultGui(), 0, ef::Win::FontH::underline);
if (boldGUIFont) {
// make status and its label bold
ef::Win::WndH status = GetDlgItem(wnd, IDC_STATUS);
status.setFont(boldGUIFont);
status.getWindow(GW_HWNDPREV).setFont(boldGUIFont);
}
// set dlg icons
HICON appIcon = LoadIcon(app.inst, MAKEINTRESOURCE(IDI_APP));
SendMessage(wnd, WM_SETICON, ICON_BIG, LPARAM(appIcon));
SendMessage(wnd, WM_SETICON, ICON_SMALL, LPARAM(app.smIcon));
// set static icon if dlg was loaded from lang dll
if (app.resMod)
SendDlgItemMessage(wnd,IDC_LOGO,STM_SETIMAGE,IMAGE_ICON,LPARAM(appIcon));
struct Link {
int id;
LPCWSTR title;
LPCWSTR url;
};
Link links[] = {
{ IDC_MAIL, L"[email protected]", L"mailto:[email protected]" },
{ IDC_WEB, L"Deskpins webpage", L"http://efotinis.neocities.org/deskpins/index.html" }
};
ef::Win::FontH font = ef::Win::FontH::getStockDefaultGui();
BOOST_FOREACH(const Link& link, links) {
SetDlgItemText(wnd, link.id, link.title);
using ef::Win::CustomControls::LinkCtrl;
LinkCtrl* ctl = LinkCtrl::subclass(wnd, link.id);
ctl->setFonts(font, underlineGUIFont ? underlineGUIFont : font, font);
ctl->setColors(RGB(0,0,255), RGB(255,0,0), RGB(128,0,128));
ctl->setUrl(link.url);
}
//#ifdef TEST_OPTIONS_PAGE
// SendMessage(wnd, WM_COMMAND, CM_OPTIONS, 0);
//#endif
return true;
}
static void evTermDlg(HWND wnd, HFONT& boldGUIFont, HFONT& underlineGUIFont)
{
app.aboutDlg = 0;
if (boldGUIFont) {
DeleteObject(boldGUIFont);
boldGUIFont = 0;
}
if (underlineGUIFont) {
DeleteObject(underlineGUIFont);
underlineGUIFont = 0;
}
}
static void showSpecialInfo(HWND parent)
{
#if defined(_DEBUG)
LPCWSTR build = L"Debug";
#else
LPCWSTR build = L"Release";
#endif
// TODO: remove build info and add something more useful (e.g. "portable")
WCHAR buf[1000];
wsprintf(buf, L"Build: %s", build);
MessageBox(parent, buf, L"Info", MB_ICONINFORMATION);
}
};
ATOM MainWnd::registerClass()
{
WNDCLASS wc = {};
wc.lpfnWndProc = proc;
wc.hInstance = app.inst;
wc.lpszClassName = className;
return RegisterClass(&wc);
}
LRESULT CALLBACK MainWnd::proc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
static UINT taskbarMsg = RegisterWindowMessage(L"TaskbarCreated");
static PendingWindows pendWnds;
static WindowCreationMonitor* winCreMon;
static Options* opt;
switch (msg) {
case WM_CREATE: {
CREATESTRUCT* cs = (CREATESTRUCT*)lparam;
if (!cs || !cs->lpCreateParams)
return -1;
opt = (Options*)cs->lpCreateParams;
app.mainWnd = wnd;
//winCreMon = new HookDllWindowCreationMonitor();
winCreMon = new EventHookWindowCreationMonitor();
if (opt->autoPinOn && !winCreMon->init(wnd, App::WM_QUEUEWINDOW)) {
Util::App::error(wnd, Util::Res::ResStr(IDS_ERR_HOOKDLL));
opt->autoPinOn = false;
}
app.smIcon = HICON(LoadImage(app.inst, MAKEINTRESOURCE(IDI_APP), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR));
app.createSmClrIcon(opt->pinClr);
// first set the wnd (this can only be done once)...
app.trayIcon.setWnd(wnd);
// .. and then create the notification icon
app.trayIcon.create(app.smClrIcon, app.trayIconTip().c_str());
// setup hotkeys
if (opt->hotkeysOn) {
bool allKeysSet = true;
allKeysSet &= opt->hotEnterPin.set(wnd);
allKeysSet &= opt->hotTogglePin.set(wnd);
if (!allKeysSet)
Util::App::error(wnd, Util::Res::ResStr(IDS_ERR_HOTKEYSSET));
}
if (opt->autoPinOn)
SetTimer(wnd, App::TIMERID_AUTOPIN, opt->autoPinDelay.value, 0);
// init pin image/shape/dims
app.pinShape.initShape();
app.pinShape.initImage(opt->pinClr);
break;
}
case WM_DESTROY: {
app.mainWnd = 0;
winCreMon->term();
delete winCreMon;
winCreMon = 0;
SendMessage(wnd, WM_COMMAND, CM_REMOVEPINS, 0);
// remove hotkeys
opt->hotEnterPin.unset(wnd);
opt->hotTogglePin.unset(wnd);
PostQuitMessage(0);
break;
}
case App::WM_TRAYICON:
evTrayIcon(wnd, wparam, lparam, opt);
break;
case App::WM_PINREQ:
evPinReq(wnd, int(wparam), int(lparam), opt);
break;
case WM_HOTKEY:
evHotkey(wnd, wparam, opt);
break;
case WM_TIMER:
if (wparam == App::TIMERID_AUTOPIN) pendWnds.check(wnd, *opt);
break;
case App::WM_QUEUEWINDOW:
pendWnds.add(HWND(wparam));
break;
case App::WM_PINSTATUS:
if (lparam) ++app.pinsUsed; else --app.pinsUsed;
if (app.aboutDlg) SendMessage(app.aboutDlg, App::WM_PINSTATUS, 0, 0);
app.trayIcon.setTip(app.trayIconTip().c_str());
break;
case WM_COMMAND:
switch (LOWORD(wparam)) {
case IDHELP:
app.help.show(wnd);
break;
case CM_ABOUT:
if (app.aboutDlg)
SetForegroundWindow(app.aboutDlg);
else {
Util::Res::CreateLocalizedDialog(IDD_ABOUT, 0, AboutDlg::proc);
ShowWindow(app.aboutDlg, SW_SHOW);
}
break;
case CM_NEWPIN:
cmNewPin(wnd);
break;
case CM_REMOVEPINS:
cmRemovePins(wnd);
break;
case CM_OPTIONS:
cmOptions(wnd, *winCreMon, opt);
break;
case CM_CLOSE:
DestroyWindow(wnd);
break;
//case ID_TEST:
// CmTest(wnd);
// break;
default:
break;
}
break;
case WM_ENDSESSION: {
if (wparam)
opt->save();
return 0;
}
//case WM_SETTINGSCHANGE:
//case WM_DISPLAYCHANGE:
case WM_DWMCOMPOSITIONCHANGED: {
app.dwm.wmDwmCompositionChanged();
return 0;
}
default:
if (msg == taskbarMsg) {
// taskbar recreated; reset the tray icon
app.trayIcon.create(app.smClrIcon, app.trayIconTip().c_str());
break;
}
return DefWindowProc(wnd, msg, wparam, lparam);
}
return 0;
}
void MainWnd::evHotkey(HWND wnd, int idHotKey, Options* opt)
{
// ignore if there's an active pin layer
if (app.layerWnd) return;
switch (idHotKey) {
case App::HOTID_ENTERPINMODE:
PostMessage(wnd, WM_COMMAND, CM_NEWPIN, 0);
break;
case App::HOTID_TOGGLEPIN:
Util::App::togglePin(wnd, GetForegroundWindow(), opt->trackRate.value);
break;
}
}
void MainWnd::evPinReq(HWND wnd, int x, int y, Options* opt)
{
POINT pt = {x,y};
HWND hitWnd = Util::Wnd::getTopParent(WindowFromPoint(pt));
Util::App::pinWindow(wnd, hitWnd, opt->trackRate.value);
}
namespace
{
// Set and manage specific tray menu images.
//
class TrayMenuDecorations : boost::noncopyable {
public:
TrayMenuDecorations(HMENU menu)
{
int w = GetSystemMetrics(SM_CXMENUCHECK);
int h = GetSystemMetrics(SM_CYMENUCHECK);
HDC memDC = CreateCompatibleDC(0);
HFONT fnt = ef::Win::FontH::create(L"Marlett", h, SYMBOL_CHARSET, ef::Win::FontH::noStyle);
HBRUSH bkBrush = HBRUSH(GetStockObject(WHITE_BRUSH));
HGDIOBJ orgFnt = GetCurrentObject(memDC, OBJ_FONT);
HGDIOBJ orgBmp = GetCurrentObject(memDC, OBJ_BITMAP);
bmpClose = makeBmp(memDC, w, h, bkBrush, menu, CM_CLOSE, fnt, L"r");
bmpAbout = makeBmp(memDC, w, h, bkBrush, menu, CM_ABOUT, fnt, L"s");
SelectObject(memDC, orgBmp);
SelectObject(memDC, orgFnt);
DeleteObject(fnt);
DeleteDC(memDC);
}
~TrayMenuDecorations()
{
if (bmpClose) DeleteObject(bmpClose);
if (bmpAbout) DeleteObject(bmpAbout);
}
protected:
HBITMAP bmpClose, bmpAbout;
HBITMAP makeBmp(HDC dc, int w, int h, HBRUSH bkBrush, HMENU menu, int idCmd, HFONT fnt, const TCHAR* c)
{
HBITMAP retBmp = CreateBitmap(w, h, 1, 1, 0);
if (retBmp) {
RECT rc = {0, 0, w, h};
SelectObject(dc, fnt);
SelectObject(dc, retBmp);
FillRect(dc, &rc, bkBrush);
DrawText(dc, c, 1, &rc, DT_CENTER | DT_NOPREFIX);
SetMenuItemBitmaps(menu, idCmd, MF_BYCOMMAND, retBmp, retBmp);
}
return retBmp;
}
};
}
void MainWnd::evTrayIcon(HWND wnd, WPARAM id, LPARAM msg, Options* opt)
{
static bool gotLButtonDblClk = false;
if (id != 0) return;
switch (msg) {
case WM_LBUTTONUP: {
if (!opt->dblClkTray || gotLButtonDblClk) {
SendMessage(wnd, WM_COMMAND, CM_NEWPIN, 0);
gotLButtonDblClk = false;
}
break;
}
case WM_LBUTTONDBLCLK: {
gotLButtonDblClk = true;
break;
}
case WM_RBUTTONDOWN: {
SetForegroundWindow(wnd);
HMENU dummy = Util::Res::LoadLocalizedMenu(IDM_TRAY);
HMENU menu = GetSubMenu(dummy,0);
SetMenuDefaultItem(menu, CM_NEWPIN, false);
TrayMenuDecorations tmd(menu);
POINT pt;
GetCursorPos(&pt);
TrackPopupMenu(menu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, wnd, 0);
DestroyMenu(dummy);
SendMessage(wnd, WM_NULL, 0, 0);
break;
}
}
}
void MainWnd::cmNewPin(HWND wnd)
{
// avoid re-entrancy
if (app.layerWnd) return;
// NOTE: fix for Win8+ (the top-left corner doesn't work)
const POINT layerWndPos = Util::Sys::isWin8orGreater() ? ef::Win::Point(100, 100) : ef::Win::Point(0, 0);
const SIZE layerWndSize = { 1, 1 };
app.layerWnd = CreateWindowEx(
WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW,
PinLayerWnd::className,
L"DeskPin",
WS_POPUP | WS_VISIBLE,
layerWndPos.x, layerWndPos.y, layerWndSize.cx, layerWndSize.cy,
wnd, 0, app.inst, 0);
if (!app.layerWnd) return;
ShowWindow(app.layerWnd, SW_SHOW);
// synthesize a WM_LBUTTONDOWN on layerWnd, so that it captures the mouse
POINT pt;
GetCursorPos(&pt);
SetCursorPos(layerWndPos.x, layerWndPos.y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
SetCursorPos(pt.x, pt.y);
}
void MainWnd::cmRemovePins(HWND wnd)
{
HWND pin;
while ((pin = FindWindow(PinWnd::className, 0)) != 0)
DestroyWindow(pin);
}
void MainWnd::cmOptions(HWND wnd, WindowCreationMonitor& winCreMon, Options* opt)
{
// sentry to avoid multiple dialogs
static bool isOptDlgOn;
if (isOptDlgOn) return;
isOptDlgOn = true;
const int pageCnt = 4;
PROPSHEETPAGE psp[pageCnt];
PROPSHEETHEADER psh;
int dlgIds[pageCnt] = {
IDD_OPT_PINS, IDD_OPT_AUTOPIN, IDD_OPT_HOTKEYS, IDD_OPT_LANG
};
DLGPROC dlgProcs[pageCnt] = {
OptPins::dlgProc, OptAutoPin::dlgProc, OptHotKeys::dlgProc, OptLang::dlgProc
};
// This must remain in scope until PropertySheet() call,
// because we use a char* from it in BuildOptPropSheet().
Util::Res::ResStr capStr(IDS_OPTIONSTITLE);
OptionsPropSheetData data = { *opt, winCreMon };
OptionsDlg::buildOptPropSheet(psh, psp, dlgIds, dlgProcs, pageCnt, 0, data, capStr);
// HACK: ensure tab pages creation, even if lang change is applied
//
// If there's a loaded res DLL, we reload it to increase its ref count
// and then free it again when PropertySheet() returns.
// This is in case the user changes the lang and hits Apply.
// That causes the current res mod to change, but the struct passed
// to PropertySheet() describes the prop pages using the initial
// res mod. Unless we do this ref-count trick, any pages that were
// not loaded before the lang change apply would fail to be created.
//
HINSTANCE curResMod = 0;
if (!opt->uiFile.empty()) {
WCHAR buf[MAX_PATH];
GetModuleFileName(app.resMod, buf, sizeof(buf));
curResMod = LoadLibrary(buf);
}
//#ifdef TEST_OPTIONS_PAGE
// psh.nStartPage = TEST_OPTIONS_PAGE;
//#endif
if (PropertySheet(&psh) == -1) {
std::wstring msg = Util::Res::ResStr(IDS_ERR_DLGCREATE);
msg += L"\r\n";
msg += ef::Win::getLastErrorStr();
Util::App::error(wnd, msg.c_str());
}
if (curResMod) {
FreeLibrary(curResMod);
curResMod = 0;
}
// reset tray tip, in case lang was changed
app.trayIcon.setTip(app.trayIconTip().c_str());
isOptDlgOn = false;
//#ifdef TEST_OPTIONS_PAGE
// // PostMessage() stalls the debugger a bit...
// //PostMessage(wnd, WM_COMMAND, CM_CLOSE, 0);
// DestroyWindow(wnd);
//#endif
}