-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
563 lines (456 loc) · 16.7 KB
/
util.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
#include "stdafx.h"
#include "pinwnd.h"
#include "util.h"
#include "resource.h"
bool Util::Wnd::isWndRectEmpty(HWND wnd)
{
RECT rc;
return GetWindowRect(wnd, &rc) && IsRectEmpty(&rc);
}
bool Util::Wnd::isChild(HWND wnd)
{
return (ef::Win::WndH(wnd).getStyle() & WS_CHILD) != 0;
}
HWND Util::Wnd::getNonChildParent(HWND wnd)
{
while (isChild(wnd))
wnd = GetParent(wnd);
return wnd;
}
HWND Util::Wnd::getTopParent(HWND wnd /*, bool mustBeVisible*/)
{
// ------------------------------------------------------
// NOTE: 'mustBeVisible' is not used currently
// ------------------------------------------------------
// By setting the 'mustBeVisible' flag,
// more constraints are applied to the ultimate parent searching:
//
// #1: Stop when parent is invisible (e.g. the shell's Display Properties
// which has a hidden parent from RunDll)
// #2: Stop when parent rect is null. This is the case with VCL apps
// where a parent window (TApplication class) is created as a proxy
// for app functionality (it has WS_VISIBLE, but the width/height are 0)
//
// The pinning engine handles invisible wnds (via the proxy mechanism).
// 'mustBeVisible' is used mostly for user interaction
// (e.g. peeking a window's attributes)
// ------------------------------------------------------
// go up the window chain to find the ultimate top-level wnd
// for WS_CHILD wnds use GetParent()
// for the rest use GetWindow(GW_OWNER)
//
for (;;) {
HWND parent = isChild(wnd)
? GetParent(wnd)
: GetWindow(wnd, GW_OWNER);
if (!parent || parent == wnd) break;
//if (mustBeVisible && !IsWindowVisible(parent) || IsWndRectEmpty(parent))
// break;
wnd = parent;
}
return wnd;
}
bool Util::Wnd::isProgManWnd(HWND wnd)
{
return Util::Text::strimatch(ef::Win::WndH(wnd).getClassName().c_str(), L"ProgMan")
&& Util::Text::strimatch(ef::Win::WndH(wnd).getText().c_str(), L"Program Manager");
}
bool Util::Wnd::isTaskBar(HWND wnd)
{
return Util::Text::strimatch(ef::Win::WndH(wnd).getClassName().c_str(), L"Shell_TrayWnd");
}
bool Util::Wnd::isTopMost(HWND wnd)
{
return (ef::Win::WndH(wnd).getExStyle() & WS_EX_TOPMOST) != 0;
}
bool Util::Wnd::isVCLAppWnd(HWND wnd)
{
return Util::Text::strimatch(L"TApplication", ef::Win::WndH(wnd).getClassName().c_str())
&& Util::Wnd::isWndRectEmpty(wnd);
}
void Util::App::error(HWND wnd, LPCWSTR s)
{
Util::Res::ResStr caption(IDS_ERRBOXTTITLE, 50, reinterpret_cast<DWORD>(::App::APPNAME));
MessageBox(wnd, s, caption, MB_ICONSTOP | MB_TOPMOST);
}
void Util::App::warning(HWND wnd, LPCWSTR s)
{
Util::Res::ResStr caption(IDS_WRNBOXTTITLE, 50, reinterpret_cast<DWORD>(::App::APPNAME));
MessageBox(wnd, s, caption, MB_ICONWARNING | MB_TOPMOST);
}
std::wstring Util::App::getHelpFileDescr(const std::wstring& path, const std::wstring& name)
{
// Data layout:
// {CHM file data...}
// WCHAR data[size] // no NULs
// DWORD size
// DWORD sig
const DWORD CHM_MARKER_SIG = 0xefda7a00; // from chmmark.py
std::wstring ret;
DWORD sig, len;
ef::Win::AutoFileH file = ef::Win::FileH::create(path + name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (file != INVALID_HANDLE_VALUE &&
file.setPosFromEnd32(0) &&
Util::Sys::readFileBack(file, &sig, sizeof(sig)) &&
sig == CHM_MARKER_SIG &&
Util::Sys::readFileBack(file, &len, sizeof(len)))
{
boost::scoped_array<char> buf(new char[len]);
if (Util::Sys::readFileBack(file, buf.get(), len)) {
boost::scoped_array<WCHAR> wbuf(new WCHAR[len]);
if (MultiByteToWideChar(CP_THREAD_ACP, 0, buf.get(), len, wbuf.get(), len))
ret.assign(wbuf.get(), len);
}
}
return ret;
}
bool Util::Sys::getScrSize(SIZE& sz)
{
return ((sz.cx = GetSystemMetrics(SM_CXSCREEN)) != 0 &&
(sz.cy = GetSystemMetrics(SM_CYSCREEN)) != 0);
}
void Util::App::pinWindow(HWND wnd, HWND hitWnd, int trackRate, bool silent)
{
int err = 0, wrn = 0;
if (!hitWnd)
wrn = IDS_ERR_COULDNOTFINDWND;
else if (Util::Wnd::isProgManWnd(hitWnd))
wrn = IDS_ERR_CANNOTPINDESKTOP;
// NOTE: after creating the layer wnd, the taskbar becomes non-topmost;
// use this check to avoid pinning it
else if (Util::Wnd::isTaskBar(hitWnd))
wrn = IDS_ERR_CANNOTPINTASKBAR;
else if (Util::Wnd::isTopMost(hitWnd))
wrn = IDS_ERR_ALREADYTOPMOST;
// hidden wnds are handled by the proxy mechanism
//else if (!IsWindowVisible(hitWnd))
// Error(wnd, "Cannot pin a hidden window");
else {
// create a pin wnd
HWND pin = CreateWindowEx(
WS_EX_TOPMOST | WS_EX_TOOLWINDOW,
PinWnd::className,
L"",
WS_POPUP | WS_VISIBLE,
0, 0, 0, 0, // real pos/size set on wnd assignment
0, 0, app.inst, 0);
if (!pin)
err = IDS_ERR_PINCREATE;
else if (!SendMessage(pin, ::App::WM_PIN_ASSIGNWND, WPARAM(hitWnd), trackRate)) {
err = IDS_ERR_PINWND;
DestroyWindow(pin);
}
}
if (!silent && (err || wrn)) {
if (err)
Util::App::error(wnd, Util::Res::ResStr(err));
else
Util::App::warning(wnd, Util::Res::ResStr(wrn));
}
}
// If the specified window (top parent) is pinned,
// return the pin wnd's handle; otherwise return 0.
//
HWND Util::App::hasPin(HWND wnd)
{
// enumerate all pin windows
HWND pin = 0;
while ((pin = FindWindowEx(0, pin, PinWnd::className, 0)) != 0)
//if (GetParent(pin) == wnd)
if (HWND(SendMessage(pin, ::App::WM_PIN_GETPINNEDWND, 0, 0)) == wnd)
return pin;
return 0;
}
void Util::App::togglePin(HWND wnd, HWND target, int trackRate)
{
target = Util::Wnd::getTopParent(target);
HWND pin = hasPin(target);
if (pin)
DestroyWindow(pin);
else
Util::App::pinWindow(wnd, target, trackRate);
}
void Util::App::markWnd(HWND wnd, bool mode)
{
const int blinkDelay = 50; // msec
// thickness of highlight border
const int width = 3;
// first val can vary; second should be zero
const int flashes = mode ? 1 : 0;
// amount to deflate if wnd is maximized, to make the highlight visible
const int zoomFix = IsZoomed(wnd) ? GetSystemMetrics(SM_CXFRAME) : 0;
// when composition is enabled, drawing on the glass frame is prohibited
// (GetWindowDC() returns a DC that clips the frame); in that case,
// we use the screen DC and draw a simple rect (since GetWindowRgn()
// returns ERROR); this rect overlaps other windows in front of the target,
// but it's better than nothing
const bool composition = app.dwm.isCompositionEnabled();
HDC dc = composition ? GetDC(0) : GetWindowDC(wnd);
if (dc) {
int orgRop2 = SetROP2(dc, R2_XORPEN);
HRGN rgn = CreateRectRgn(0,0,0,0);
bool hasRgn = GetWindowRgn(wnd, rgn) != ERROR;
const int loops = flashes*2+1;
if (hasRgn) {
for (int m = 0; m < loops; ++m) {
FrameRgn(dc, rgn, HBRUSH(GetStockObject(WHITE_BRUSH)), width, width);
GdiFlush();
if (mode && m < loops-1)
Sleep(blinkDelay);
}
}
else {
RECT rc;
GetWindowRect(wnd, &rc);
if (!composition)
OffsetRect(&rc, -rc.left, -rc.top);
InflateRect(&rc, -zoomFix, -zoomFix);
HGDIOBJ orgPen = SelectObject(dc, GetStockObject(WHITE_PEN));
HGDIOBJ orgBrush = SelectObject(dc, GetStockObject(NULL_BRUSH));
RECT tmp;
for (int m = 0; m < loops; ++m) {
CopyRect(&tmp, &rc);
for (int n = 0; n < width; ++n) {
Rectangle(dc, tmp.left, tmp.top, tmp.right, tmp.bottom);
InflateRect(&tmp, -1, -1);
}
GdiFlush();
if (mode && m < loops-1)
Sleep(blinkDelay);
}
SelectObject(dc, orgBrush);
SelectObject(dc, orgPen);
}
SetROP2(dc, orgRop2);
ReleaseDC(composition ? 0 : wnd, dc);
DeleteObject(rgn);
}
}
std::wstring Util::App::getLangFileDescr(const std::wstring& path, const std::wstring& file)
{
HINSTANCE inst = file.empty() ? app.inst : LoadLibrary((path+file).c_str());
WCHAR buf[100];
if (!inst || !LoadString(inst, IDS_LANG, buf, sizeof(buf)))
*buf = '\0';
if (!file.empty() && inst) // release inst if we had to load it
FreeLibrary(inst);
return buf;
}
HMENU Util::Res::LoadLocalizedMenu(LPCTSTR lpMenuName)
{
if (app.resMod) {
HMENU ret = LoadMenu(app.resMod, lpMenuName);
if (ret)
return ret;
}
return LoadMenu(app.inst, lpMenuName);
}
HMENU Util::Res::LoadLocalizedMenu(WORD id)
{
return Util::Res::LoadLocalizedMenu(MAKEINTRESOURCE(id));
}
namespace {
bool isLastErrResNotFound()
{
DWORD e = GetLastError();
return e == ERROR_RESOURCE_DATA_NOT_FOUND ||
e == ERROR_RESOURCE_TYPE_NOT_FOUND ||
e == ERROR_RESOURCE_NAME_NOT_FOUND ||
e == ERROR_RESOURCE_LANG_NOT_FOUND;
}
}
int Util::Res::LocalizedDialogBoxParam(LPCTSTR lpTemplate, HWND hParent, DLGPROC lpDialogFunc, LPARAM dwInit)
{
if (app.resMod) {
int ret = DialogBoxParam(app.resMod, lpTemplate, hParent, lpDialogFunc, dwInit);
if (ret != -1 || !isLastErrResNotFound())
return ret;
}
return DialogBoxParam(app.inst, lpTemplate, hParent, lpDialogFunc, dwInit);
}
int Util::Res::LocalizedDialogBoxParam(WORD id, HWND hParent, DLGPROC lpDialogFunc, LPARAM dwInit)
{
return Util::Res::LocalizedDialogBoxParam(MAKEINTRESOURCE(id), hParent, lpDialogFunc, dwInit);
}
HWND Util::Res::CreateLocalizedDialog(LPCTSTR lpTemplate, HWND hParent, DLGPROC lpDialogFunc)
{
if (app.resMod) {
HWND ret = CreateDialog(app.resMod, lpTemplate, hParent, lpDialogFunc);
if (ret || !isLastErrResNotFound())
return ret;
}
return CreateDialog(app.inst, lpTemplate, hParent, lpDialogFunc);
}
HWND Util::Res::CreateLocalizedDialog(WORD id, HWND hParent, DLGPROC lpDialogFunc)
{
return Util::Res::CreateLocalizedDialog(MAKEINTRESOURCE(id), hParent, lpDialogFunc);
}
void Util::Res::ResStr::init(UINT id, size_t bufLen, DWORD_PTR* params) {
str = new WCHAR[bufLen];
if (!app.resMod || !LoadString(app.resMod, id, str, bufLen))
LoadString(app.inst, id, str, bufLen);
if (params) {
DWORD flags = FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY;
va_list* va = reinterpret_cast<va_list*>(params);
if (!FormatMessage(flags, std::wstring(str).c_str(), 0, 0, str, bufLen, va))
*str = 0;
}
}
bool Util::Gfx::rectContains(const RECT& rc1, const RECT& rc2)
{
return rc2.left >= rc1.left
&& rc2.top >= rc1.top
&& rc2.right <= rc1.right
&& rc2.bottom <= rc1.bottom;
}
// enable/disable all ctrls that lie inside the specified ctrl
// (usually a group, or maybe a tab, etc)
void Util::Wnd::enableGroup(HWND wnd, int id, bool mode)
{
HWND container = GetDlgItem(wnd, id);
RECT rc;
GetWindowRect(container, &rc);
// deflate a bit (4 DLUs) to be on the safe side (do we need this?)
RECT rc2 = {0, 0, 4, 4};
MapDialogRect(wnd, &rc2);
InflateRect(&rc, rc2.left-rc2.right, rc2.top-rc2.bottom);
for (HWND child = GetWindow(wnd, GW_CHILD);
child; child = GetWindow(child, GW_HWNDNEXT)) {
if (child == container)
continue;
GetWindowRect(child, &rc2);
if (Util::Gfx::rectContains(rc, rc2))
EnableWindow(child, mode);
}
}
std::vector<std::wstring> Util::Sys::getFiles(std::wstring mask)
{
std::vector<std::wstring> ret;
for (ef::Win::FileFinder fde(mask, ef::Win::FileFinder::files); fde; ++fde)
ret.push_back(fde.getName());
return ret;
}
bool Util::Sys::readFileBack(HANDLE file, void* buf, int bytes)
{
DWORD read;
return SetFilePointer(file, -bytes, 0, FILE_CURRENT) != -1
&& ReadFile(file, buf, bytes, &read, 0)
&& int(read) == bytes
&& SetFilePointer(file, -bytes, 0, FILE_CURRENT) != -1;
}
COLORREF Util::Clr::light(COLORREF clr)
{
double r = GetRValue(clr) / 255.0;
double g = GetGValue(clr) / 255.0;
double b = GetBValue(clr) / 255.0;
double h, l, s;
ef::RGBtoHLS(r, g, b, h, l, s);
l = min(1, l+0.25);
ef::HLStoRGB(h, l, s, r, g, b);
return RGB(r*255, g*255, b*255);
}
COLORREF Util::Clr::dark(COLORREF clr)
{
double r = GetRValue(clr) / 255.0;
double g = GetGValue(clr) / 255.0;
double b = GetBValue(clr) / 255.0;
double h, l, s;
ef::RGBtoHLS(r, g, b, h, l, s);
l = max(0, l-0.25);
ef::HLStoRGB(h, l, s, r, g, b);
return RGB(r*255, g*255, b*255);
}
BOOL Util::Wnd::moveWindow(HWND wnd, const RECT& rc, BOOL repaint)
{
return MoveWindow(wnd, rc.left, rc.top,
rc.right-rc.left, rc.bottom-rc.top, repaint);
}
BOOL Util::Gfx::rectangle(HDC dc, const RECT& rc)
{
return Rectangle(dc, rc.left, rc.top, rc.right, rc.bottom);
}
bool Util::Wnd::psChanged(HWND page)
{
return !!PropSheet_Changed(GetParent(page), page);
}
// removes the first accelerator prefix ('&')
// from a string and returns the result
std::wstring Util::Text::remAccel(std::wstring s)
{
std::wstring::size_type i = s.find_first_of(L"&");
if (i != std::wstring::npos) s.erase(i, 1);
return s;
}
bool Util::Gfx::getBmpSize(HBITMAP bmp, SIZE& sz)
{
BITMAP bm;
if (!GetObject(bmp, sizeof(bm), &bm))
return false;
sz.cx = bm.bmWidth;
sz.cy = abs(bm.bmHeight);
return true;
}
bool Util::Gfx::getBmpSizeAndBpp(HBITMAP bmp, SIZE& sz, int& bpp)
{
BITMAP bm;
if (!GetObject(bmp, sizeof(bm), &bm))
return false;
sz.cx = bm.bmWidth;
sz.cy = abs(bm.bmHeight);
bpp = bm.bmBitsPixel;
return true;
}
// Convert white/lgray/dgray of bmp to shades of 'clr'.
// NOTE: Bmp must *not* be selected in any DC for this to succeed.
//
bool Util::Gfx::remapBmpColors(HBITMAP bmp, COLORREF clrs[][2], int cnt)
{
bool ok = false;
SIZE sz;
int bpp;
if (getBmpSizeAndBpp(bmp, sz, bpp)) {
if (HDC dc = CreateCompatibleDC(0)) {
if (HBITMAP orgBmp = HBITMAP(SelectObject(dc, bmp))) {
if (bpp == 16) {
// In 16-bpp modes colors get changed,
// e.g. light gray (0xC0C0C0) becomes 0xC6C6C6
// GetNearestColor() only works for paletized modes,
// so we'll have to patch our source colors manually
// using Nearest16BppColor.
ef::Win::Nearest16BppColor nearClr;
for (int n = 0; n < cnt; ++n)
clrs[n][0] = nearClr(clrs[n][0]);
}
for (int y = 0; y < sz.cy; ++y) {
for (int x = 0; x < sz.cx; ++x) {
COLORREF clr = GetPixel(dc, x, y);
for (int n = 0; n < cnt; ++n)
if (clr == clrs[n][0])
SetPixelV(dc, x, y, clrs[n][1]);
}
}
ok = true;
SelectObject(dc, orgBmp);
}
DeleteDC(dc);
}
}
//ostringstream oss;
//oss << hex << setfill('0');
//for (set<COLORREF>::const_iterator it = clrSet.begin(); it != clrSet.end(); ++it)
// oss << setw(6) << *it << "\r\n";
//MessageBoxA(app.mainWnd, oss.str().c_str(), "Unique clrs", 0);
return ok;
}
// Returns the part of a string after the last occurrence of a token.
// Example: substrAfter("foobar", "oo") --> "bar"
// Returns "" on error.
std::wstring Util::Text::substrAfterLast(const std::wstring& s, const std::wstring& delim)
{
std::wstring::size_type i = s.find_last_of(delim);
return i == std::wstring::npos ? L"" : s.substr(i + delim.length());
}
bool Util::Sys::isWin8orGreater()
{
return ef::Win::OsVer().majMin() >= ef::Win::packVer(6, 2);
}