forked from sumatrapdfreader/sumatrapdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaption.cpp
More file actions
783 lines (685 loc) · 30.4 KB
/
Copy pathCaption.cpp
File metadata and controls
783 lines (685 loc) · 30.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
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/* Copyright 2015 the SumatraPDF project authors (see AUTHORS file).
License: GPLv3 */
#include "BaseUtil.h"
#include "WinDynCalls.h"
#include "WinUtil.h"
#include "SettingsStructs.h"
#include "Colors.h"
#include "SumatraPDF.h"
#include "WindowInfo.h"
#include "Caption.h"
#include "Tabs.h"
#include "Translations.h"
#include "resource.h"
#include "Menu.h"
using namespace Gdiplus;
#define CUSTOM_CAPTION_CLASS_NAME L"CustomCaption"
#define UNDOCUMENTED_MENU_CLASS_NAME L"#32768"
#define BTN_ID_FIRST 100
// This will prevent menu reopening, if we click the menu button,
// while the menu is open.
#define DO_NOT_REOPEN_MENU_TIMER_ID 1
#define DO_NOT_REOPEN_MENU_DELAY_IN_MS 1
// undocumented caption buttons state
#define CBS_INACTIVE 5
// undocumented window messages
#define WM_NCUAHDRAWCAPTION 0xAE
#define WM_NCUAHDRAWFRAME 0xAF
#define WM_POPUPSYSTEMMENU 0x313
// When a top level window is maximized the window manager checks whether its client
// area covers the entire screen. If it does, the manager assumes that this is a fullscreen
// window and hides the taskbar and any topmost window. A simple workaround is to
// expand the non-client border at the expense of client area.
#define NON_CLIENT_BAND 1
// This non-client-band hack is only needed for maximized non-fullscreen windows:
static inline bool NeedsNonClientBandHack(HWND hwnd) {
return IsZoomed(hwnd) && win::HasCaption(hwnd);
}
// http://withinwindows.com/2010/07/01/retrieving-aero-glass-base-color-for-opaque-surface-rendering/
#define REG_DWM L"Software\\Microsoft\\Windows\\DWM"
// When DWM composition is enabled, this is the ratio between alpha channels of active and inactive caption colors.
#define ACTIVE_INACTIVE_ALPHA_RATIO 2.0f
static void DrawCaptionButton(DRAWITEMSTRUCT* item, WindowInfo* win);
static void PaintCaptionBackground(HDC hdc, WindowInfo* win, bool useDoubleBuffer);
static HMENU GetUpdatedSystemMenu(HWND hwnd);
static void MenuBarAsPopupMenu(WindowInfo* win, int x, int y);
CaptionInfo::CaptionInfo(HWND hwndCaption) : hwnd(hwndCaption), theme(nullptr), isMenuOpen(false) {
UpdateTheme();
UpdateColors(true);
UpdateBackgroundAlpha();
}
CaptionInfo::~CaptionInfo() {
if (theme)
theme::CloseThemeData(theme);
}
void CaptionInfo::UpdateBackgroundAlpha() {
bgAlpha = dwm::IsCompositionEnabled() ? 0 : 255;
}
void CaptionInfo::UpdateTheme() {
if (theme) {
theme::CloseThemeData(theme);
theme = nullptr;
}
if (theme::IsThemeActive())
theme = theme::OpenThemeData(hwnd, L"WINDOW");
}
void CaptionInfo::UpdateColors(bool activeWindow) {
ARGB colorizationColor;
if (dwm::IsCompositionEnabled() &&
// get the color from the Registry and blend it with white background
ReadRegDWORD(HKEY_CURRENT_USER, REG_DWM, L"ColorizationColor", colorizationColor)) {
BYTE A, R, G, B, white;
A = BYTE((colorizationColor >> 24) & 0xff);
if (!activeWindow)
A = (BYTE)floor(A / ACTIVE_INACTIVE_ALPHA_RATIO + 0.5f);
R = BYTE((colorizationColor >> 16) & 0xff);
G = BYTE((colorizationColor >> 8) & 0xff);
B = BYTE(colorizationColor & 0xff);
white = BYTE(255 - A);
float factor = A / 255.0f;
R = BYTE((int)floor(R * factor + 0.5f) + white);
G = BYTE((int)floor(G * factor + 0.5f) + white);
B = BYTE((int)floor(B * factor + 0.5f) + white);
bgColor = RGB(R, G, B);
} else if (!theme ||
!SUCCEEDED(theme::GetThemeColor(theme, WP_CAPTION, 0,
activeWindow ? TMT_FILLCOLORHINT : TMT_BORDERCOLORHINT, &bgColor))) {
bgColor = activeWindow ? GetSysColor(COLOR_GRADIENTACTIVECAPTION) : GetSysColor(COLOR_GRADIENTINACTIVECAPTION);
}
if (!theme ||
!SUCCEEDED(theme::GetThemeColor(
theme, WP_CAPTION, 0,
(activeWindow || dwm::IsCompositionEnabled()) ? TMT_CAPTIONTEXT : TMT_INACTIVECAPTIONTEXT, &textColor))) {
textColor = (activeWindow || dwm::IsCompositionEnabled()) ? GetSysColor(COLOR_CAPTIONTEXT)
: GetSysColor(COLOR_INACTIVECAPTIONTEXT);
}
}
ButtonInfo::ButtonInfo() : hwnd(nullptr), highlighted(false), inactive(false) {
SetMargins(0, 0, 0, 0);
}
void ButtonInfo::SetMargins(LONG left, LONG top, LONG right, LONG bottom) {
margins.left = left;
margins.top = top;
margins.right = right;
margins.bottom = bottom;
}
static LRESULT CALLBACK WndProcCaption(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
WindowInfo* win = FindWindowInfoByHwnd(hwnd);
switch (message) {
case WM_COMMAND:
if (win && BN_CLICKED == HIWORD(wParam)) {
WPARAM cmd;
WORD button = LOWORD(wParam) - BTN_ID_FIRST;
switch (button) {
case CB_MINIMIZE:
cmd = SC_MINIMIZE;
break;
case CB_MAXIMIZE:
cmd = SC_MAXIMIZE;
break;
case CB_RESTORE:
cmd = SC_RESTORE;
break;
case CB_CLOSE:
cmd = SC_CLOSE;
break;
default:
cmd = 0;
break;
}
if (cmd)
PostMessage(win->hwndFrame, WM_SYSCOMMAND, cmd, 0);
if (button == CB_MENU) {
if (!KillTimer(hwnd, DO_NOT_REOPEN_MENU_TIMER_ID) && !win->caption->isMenuOpen) {
HWND hMenuButton = win->caption->btn[CB_MENU].hwnd;
WindowRect wr(hMenuButton);
win->caption->isMenuOpen = true;
if (!lParam) // if the WM_COMMAND message was sent as a result of keyboard command
InvalidateRgn(hMenuButton, nullptr, FALSE);
MenuBarAsPopupMenu(win, wr.x, wr.y + wr.dy);
win->caption->isMenuOpen = false;
if (!lParam)
InvalidateRgn(hMenuButton, nullptr, FALSE);
SetTimer(hwnd, DO_NOT_REOPEN_MENU_TIMER_ID, DO_NOT_REOPEN_MENU_DELAY_IN_MS, nullptr);
}
SetFocus(win->hwndFrame);
}
}
break;
case WM_TIMER:
if (wParam == DO_NOT_REOPEN_MENU_TIMER_ID)
KillTimer(hwnd, DO_NOT_REOPEN_MENU_TIMER_ID);
break;
case WM_SIZE:
if (win)
RelayoutCaption(win);
break;
case WM_NCHITTEST:
return HTTRANSPARENT;
case WM_ERASEBKGND:
if (win)
PaintCaptionBackground((HDC)wParam, win, true);
return TRUE;
case WM_DRAWITEM:
if (win) {
DRAWITEMSTRUCT* dis = (DRAWITEMSTRUCT*)lParam;
int index = dis->CtlID - BTN_ID_FIRST;
if (CB_MENU == index && win->caption->isMenuOpen)
dis->itemState |= ODS_SELECTED;
if (win->caption->btn[index].highlighted)
dis->itemState |= ODS_HOTLIGHT;
else if (win->caption->btn[index].inactive)
dis->itemState |= ODS_INACTIVE;
DrawCaptionButton(dis, win);
}
return TRUE;
case WM_THEMECHANGED:
if (win)
win->caption->UpdateTheme();
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
static WNDPROC DefWndProcButton = nullptr;
static LRESULT CALLBACK WndProcButton(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
WindowInfo* win = FindWindowInfoByHwnd(hwnd);
int index = (int)GetWindowLongPtr(hwnd, GWLP_ID) - BTN_ID_FIRST;
switch (message) {
case WM_MOUSEMOVE: {
ClientRect rc(hwnd);
if (!rc.Contains(PointI(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)))) {
ReleaseCapture();
return 0;
}
if (win) {
if (TrackMouseLeave(hwnd)) {
win->caption->btn[index].highlighted = true;
InvalidateRgn(hwnd, nullptr, FALSE);
}
return 0;
}
} break;
case WM_MOUSELEAVE:
if (win) {
win->caption->btn[index].highlighted = false;
InvalidateRgn(hwnd, nullptr, FALSE);
return 0;
}
break;
case WM_ERASEBKGND:
return TRUE;
case WM_LBUTTONDOWN:
if (CB_MENU == index)
PostMessage(hwnd, WM_LBUTTONUP, 0, lParam);
return CallWindowProc(DefWndProcButton, hwnd, message, wParam, lParam);
case WM_KEYDOWN:
if (CB_MENU == index && win && !win->caption->isMenuOpen &&
(VK_RETURN == wParam || VK_SPACE == wParam || VK_UP == wParam || VK_DOWN == wParam))
PostMessage(hwnd, BM_CLICK, 0, 0);
return CallWindowProc(DefWndProcButton, hwnd, message, wParam, lParam);
}
return CallWindowProc(DefWndProcButton, hwnd, message, wParam, lParam);
}
void CreateCaption(WindowInfo* win) {
win->hwndCaption = CreateWindow(CUSTOM_CAPTION_CLASS_NAME, L"", WS_CHILDWINDOW | WS_CLIPCHILDREN, 0, 0, 0, 0,
win->hwndFrame, (HMENU)0, GetModuleHandle(nullptr), nullptr);
win->caption = new CaptionInfo(win->hwndCaption);
for (UINT_PTR i = CB_BTN_FIRST; i < CB_BTN_COUNT; i++) {
HWND btn = CreateWindowExW(0, L"BUTTON", L"", WS_CHILDWINDOW | WS_VISIBLE | BS_OWNERDRAW, 0, 0, 0, 0,
win->hwndCaption, (HMENU)(BTN_ID_FIRST + i), GetModuleHandle(nullptr), nullptr);
if (!DefWndProcButton)
DefWndProcButton = (WNDPROC)GetWindowLongPtr(btn, GWLP_WNDPROC);
SetWindowLongPtr(btn, GWLP_WNDPROC, (LONG_PTR)WndProcButton);
win->caption->btn[i].hwnd = btn;
}
}
void RegisterCaptionWndClass() {
WNDCLASSEX wcex;
FillWndClassEx(wcex, CUSTOM_CAPTION_CLASS_NAME, WndProcCaption);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
RegisterClassEx(&wcex);
}
void RelayoutCaption(WindowInfo* win) {
ClientRect rc(win->hwndCaption);
CaptionInfo* ci = win->caption;
ButtonInfo* button;
DeferWinPosHelper dh;
if (dwm::IsCompositionEnabled()) {
// hide the buttons, because DWM paints and serves them, when the composition is enabled
for (int i = CB_MINIMIZE; i <= CB_CLOSE; i++) {
ShowWindow(ci->btn[i].hwnd, SW_HIDE);
}
} else {
int xEdge = GetSystemMetrics(SM_CXEDGE);
int yEdge = GetSystemMetrics(SM_CYEDGE);
bool isClassicStyle = win->caption->theme == nullptr;
// Under WIN XP GetSystemMetrics(SM_CXSIZE) returns wrong (previous) value, after theme change
// or font size change. For this to work, I assume that SM_CXSIZE == SM_CYSIZE.
int btnDx = GetSystemMetrics(IsVistaOrGreater() ? SM_CXSIZE : SM_CYSIZE) - xEdge * (isClassicStyle ? 1 : 2);
int btnDy = GetSystemMetrics(SM_CYSIZE) - yEdge * 2;
bool maximized = IsZoomed(win->hwndFrame);
int yPosBtn = rc.y + (maximized ? 0 : yEdge);
int topMargin = maximized ? yEdge : 0;
button = &ci->btn[CB_CLOSE];
rc.dx -= btnDx + xEdge;
int rightMargin = maximized ? xEdge : 0;
dh.SetWindowPos(button->hwnd, nullptr, rc.x + rc.dx, yPosBtn, btnDx + rightMargin, btnDy + topMargin,
SWP_NOZORDER | SWP_SHOWWINDOW);
button->SetMargins(0, topMargin, rightMargin, 0);
button = &ci->btn[CB_RESTORE];
rc.dx -= btnDx + xEdge;
dh.SetWindowPos(button->hwnd, nullptr, rc.x + rc.dx, yPosBtn, btnDx, btnDy + topMargin,
SWP_NOZORDER | (maximized ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));
button->SetMargins(0, topMargin, 0, 0);
button = &ci->btn[CB_MAXIMIZE];
dh.SetWindowPos(button->hwnd, nullptr, rc.x + rc.dx, yPosBtn, btnDx, btnDy + topMargin,
SWP_NOZORDER | (maximized ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
button->SetMargins(0, topMargin, 0, 0);
button = &ci->btn[CB_MINIMIZE];
rc.dx -= btnDx + (isClassicStyle ? 0 : xEdge);
dh.SetWindowPos(button->hwnd, nullptr, rc.x + rc.dx, yPosBtn, btnDx, btnDy + topMargin,
SWP_NOZORDER | SWP_SHOWWINDOW);
button->SetMargins(0, topMargin, 0, 0);
}
button = &ci->btn[CB_MENU];
int tabHeight = GetTabbarHeight(win->hwndFrame);
rc.y += rc.dy - tabHeight;
dh.SetWindowPos(button->hwnd, nullptr, rc.x, rc.y, tabHeight, tabHeight, SWP_NOZORDER);
button->SetMargins(0, 0, 0, 0);
rc.x += tabHeight;
rc.dx -= tabHeight;
dh.SetWindowPos(win->hwndTabBar, nullptr, rc.x, rc.y, rc.dx, tabHeight, SWP_NOZORDER);
dh.End();
}
static void DrawCaptionButton(DRAWITEMSTRUCT* item, WindowInfo* win) {
if (!item || item->CtlType != ODT_BUTTON)
return;
RectI rButton = RectI::FromRECT(item->rcItem);
DoubleBuffer buffer(item->hwndItem, rButton);
HDC memDC = buffer.GetDC();
UINT button = item->CtlID - BTN_ID_FIRST;
ButtonInfo* bi = &win->caption->btn[button];
RectI rc(rButton);
rc.x += bi->margins.left;
rc.y += bi->margins.top;
rc.dx -= bi->margins.left + bi->margins.right;
rc.dy -= bi->margins.top + bi->margins.bottom;
int partId = 0, stateId;
UINT state = (UINT)-1;
switch (button) {
case CB_MINIMIZE:
partId = WP_MINBUTTON;
state = DFCS_CAPTIONMIN;
break;
case CB_MAXIMIZE:
partId = WP_MAXBUTTON;
state = DFCS_CAPTIONMAX;
break;
case CB_RESTORE:
partId = WP_RESTOREBUTTON;
state = DFCS_CAPTIONRESTORE;
break;
case CB_CLOSE:
partId = WP_CLOSEBUTTON;
state = DFCS_CAPTIONCLOSE;
break;
}
if (ODS_SELECTED & item->itemState) {
stateId = CBS_PUSHED;
state |= DFCS_PUSHED;
} else if (ODS_HOTLIGHT & item->itemState) {
stateId = CBS_HOT;
state |= DFCS_HOT;
} else if (ODS_DISABLED & item->itemState) {
stateId = CBS_DISABLED;
state |= DFCS_INACTIVE;
} else if (ODS_INACTIVE & item->itemState)
stateId = CBS_INACTIVE;
else
stateId = CBS_NORMAL;
// draw system button
if (partId) {
if (rc != rButton || theme::IsThemeBackgroundPartiallyTransparent(win->caption->theme, partId, stateId))
PaintCaptionBackground(memDC, win, false);
RECT r = rc.ToRECT();
if (win->caption->theme)
theme::DrawThemeBackground(win->caption->theme, memDC, partId, stateId, &r, nullptr);
else
DrawFrameControl(memDC, &r, DFC_CAPTION, state);
}
// draw menu's button
if (button == CB_MENU) {
PaintCaptionBackground(memDC, win, false);
Graphics gfx(memDC);
if (CBS_PUSHED != stateId && ODS_FOCUS & item->itemState) {
stateId = CBS_HOT;
}
BYTE buttonRGB = 1;
if (CBS_PUSHED == stateId)
buttonRGB = 0;
else if (CBS_HOT == stateId)
buttonRGB = 255;
if (buttonRGB != 1) {
// paint the background
if (GetLightness(win->caption->textColor) > GetLightness(win->caption->bgColor)) {
buttonRGB ^= 0xff;
}
BYTE buttonAlpha = BYTE((255 - abs((int)GetLightness(win->caption->bgColor) - buttonRGB)) / 2);
SolidBrush br(Color(buttonAlpha, buttonRGB, buttonRGB, buttonRGB));
gfx.FillRectangle(&br, rc.x, rc.y, rc.dx, rc.dy);
}
// draw the three lines
COLORREF c = win->caption->textColor;
Pen p(Color(GetRValueSafe(c), GetGValueSafe(c), GetBValueSafe(c)), floor((float)rc.dy / 8.0f));
rc.Inflate(-int(rc.dx * 0.2f + 0.5f), -int(rc.dy * 0.3f + 0.5f));
for (int i = 0; i < 3; i++) {
gfx.DrawLine(&p, rc.x, rc.y + i * rc.dy / 2, rc.x + rc.dx, rc.y + i * rc.dy / 2);
}
}
buffer.Flush(item->hDC);
}
void PaintParentBackground(HWND hwnd, HDC hdc) {
HWND parent = GetParent(hwnd);
POINT pt = {0, 0};
MapWindowPoints(hwnd, parent, &pt, 1);
SetViewportOrgEx(hdc, -pt.x, -pt.y, &pt);
SendMessage(parent, WM_ERASEBKGND, (WPARAM)hdc, 0);
SetViewportOrgEx(hdc, pt.x, pt.y, nullptr);
}
static void PaintCaptionBackground(HDC hdc, WindowInfo* win, bool useDoubleBuffer) {
RECT rClip;
GetClipBox(hdc, &rClip);
RectI r = RectI::FromRECT(rClip);
COLORREF c = win->caption->bgColor;
if (win->caption->bgAlpha == 0) {
PaintParentBackground(win->hwndCaption, hdc);
} else if (win->caption->bgAlpha == 255) {
Graphics gfx(hdc);
SolidBrush br(Color(GetRValueSafe(c), GetGValueSafe(c), GetBValueSafe(c)));
gfx.FillRectangle(&br, r.x, r.y, r.dx, r.dy);
} else {
DoubleBuffer buffer(win->hwndCaption, r);
HDC memDC = useDoubleBuffer ? buffer.GetDC() : hdc;
PaintParentBackground(win->hwndCaption, memDC);
Graphics gfx(memDC);
SolidBrush br(Color(win->caption->bgAlpha, GetRValueSafe(c), GetGValueSafe(c), GetBValueSafe(c)));
gfx.FillRectangle(&br, r.x, r.y, r.dx, r.dy);
if (useDoubleBuffer)
buffer.Flush(hdc);
}
}
static void DrawFrame(HWND hwnd, COLORREF color, bool drawEdge = true) {
HDC hdc = GetWindowDC(hwnd);
RECT rWindow, rClient;
GetWindowRect(hwnd, &rWindow);
GetClientRect(hwnd, &rClient);
// convert the client rectangle to window coordinates and exclude it from the clipping region
MapWindowPoints(hwnd, HWND_DESKTOP, (LPPOINT)&rClient, 2);
OffsetRect(&rClient, -rWindow.left, -rWindow.top);
ExcludeClipRect(hdc, rClient.left, rClient.top, rClient.right, rClient.bottom);
// convert the window rectangle, from screen to window coordinates, and draw the frame
OffsetRect(&rWindow, -rWindow.left, -rWindow.top);
HBRUSH br = CreateSolidBrush(color);
FillRect(hdc, &rWindow, br);
DeleteObject(br);
if (drawEdge)
DrawEdge(hdc, &rWindow, EDGE_RAISED, BF_RECT | BF_FLAT);
ReleaseDC(hwnd, hdc);
}
// accelerator key which was pressed when invoking the "menubar",
// needs to be passed from WM_SYSCOMMAND to WM_INITMENUPOPUP
// (can be static because there can only be one menu active at a time)
static WCHAR gMenuAccelPressed = 0;
LRESULT CustomCaptionFrameProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, bool* callDef, WindowInfo* win) {
if (dwm::IsCompositionEnabled()) {
// Pass the messages to DwmDefWindowProc first. It serves the hit testing for the buttons.
LRESULT res;
if (dwm::DefWindowProc_(hwnd, msg, wParam, lParam, &res)) {
*callDef = false;
return res;
}
switch (msg) {
case WM_NCPAINT:
#if NON_CLIENT_BAND > 0
if (NeedsNonClientBandHack(hwnd))
DrawFrame(hwnd, RGB(0, 0, 0), false);
#endif
break;
case WM_ERASEBKGND: {
// Erase the background only under the extended frame.
*callDef = false;
if (win->extendedFrameHeight == 0)
return TRUE;
ClientRect rc(hwnd);
rc.dy = win->extendedFrameHeight;
HRGN extendedFrameRegion = CreateRectRgn(rc.x, rc.y, rc.x + rc.dx, rc.y + rc.dy);
int newRegionComplexity = ExtSelectClipRgn((HDC)wParam, extendedFrameRegion, RGN_AND);
DeleteObject(extendedFrameRegion);
if (newRegionComplexity == NULLREGION)
return TRUE;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
case WM_SIZE:
// Extend the translucent frame in the client area.
if (wParam == SIZE_MAXIMIZED || wParam == SIZE_RESTORED) {
int frameThickness = 0;
if (win::HasFrameThickness(hwnd)) {
frameThickness = GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER);
}
int captionHeight = 0;
if (win::HasCaption(hwnd)) {
float tabScale = CAPTION_TABBAR_HEIGHT_FACTOR;
if (IsZoomed(hwnd)) {
tabScale = 1.f;
}
captionHeight = GetTabbarHeight(win->hwndFrame, tabScale);
}
int top = frameThickness + captionHeight;
int bottom = NeedsNonClientBandHack(hwnd) ? NON_CLIENT_BAND : 0;
MARGINS margins = {0, 0, top, bottom};
dwm::ExtendFrameIntoClientArea(hwnd, &margins);
win->extendedFrameHeight = frameThickness + captionHeight;
}
break;
case WM_NCACTIVATE:
win->caption->UpdateColors((bool)wParam);
if (!IsIconic(hwnd))
RedrawWindow(win->hwndCaption, nullptr, nullptr,
RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
break;
}
} else {
switch (msg) {
case WM_SETTINGCHANGE:
if (wParam == SPI_SETNONCLIENTMETRICS)
RelayoutCaption(win);
break;
case WM_NCPAINT:
DrawFrame(hwnd, win->caption->bgColor);
*callDef = false;
return 0;
case WM_NCACTIVATE:
win->caption->UpdateColors((bool)wParam);
for (int i = CB_BTN_FIRST; i < CB_BTN_COUNT; i++)
win->caption->btn[i].inactive = wParam == FALSE;
if (!IsIconic(hwnd)) {
DrawFrame(hwnd, win->caption->bgColor);
RedrawWindow(win->hwndCaption, nullptr, nullptr,
RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
*callDef = false;
return TRUE;
}
break;
case WM_NCUAHDRAWCAPTION:
case WM_NCUAHDRAWFRAME:
DrawFrame(hwnd, win->caption->bgColor);
*callDef = false;
return TRUE;
case WM_POPUPSYSTEMMENU:
case WM_SETCURSOR:
case WM_SETTEXT:
case WM_SETICON:
if (!win->caption->theme && IsWindowVisible(hwnd)) {
// Remove the WS_VISIBLE style to prevent DefWindowProc from drawing
// in the caption's area when processing these mesages.
// TODO: can't such drawing be prevented by handling WM_(NC)PAINT instead?
ToggleWindowStyle(hwnd, WS_VISIBLE, false);
LRESULT res = DefWindowProc(hwnd, msg, wParam, lParam);
ToggleWindowStyle(hwnd, WS_VISIBLE, true);
*callDef = false;
return res;
}
break;
}
}
// These messages must be handled in both modes - with or without DWM composition.
switch (msg) {
case WM_NCCALCSIZE: {
// In order to have custom caption, we have to include its area in the client rectangle.
RECT* r = wParam == TRUE ? &((NCCALCSIZE_PARAMS*)lParam)->rgrc[0] : (RECT*)lParam;
RECT rWindow = *r;
// Let DefWindowProc calculate the client rectangle.
DefWindowProc(hwnd, msg, wParam, lParam);
RECT rClient = *r;
// Modify the client rectangle to include the caption's area.
if (dwm::IsCompositionEnabled())
rClient.top = rWindow.top;
else
rClient.top = rWindow.top + rWindow.bottom - rClient.bottom;
// prevents the hiding of the topmost windows, when this window is maximized
if (NeedsNonClientBandHack(hwnd))
rClient.bottom -= NON_CLIENT_BAND;
*r = rClient;
*callDef = false;
}
return 0;
case WM_NCHITTEST: {
// Provide hit testing for the caption.
PointI pt(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
RectI rClient = MapRectToWindow(ClientRect(hwnd), hwnd, HWND_DESKTOP);
WindowRect rCaption(win->hwndCaption);
if (rClient.Contains(pt) && pt.y < rCaption.y + rCaption.dy) {
*callDef = false;
if (pt.y < rCaption.y)
return HTTOP;
return HTCAPTION;
}
} break;
case WM_NCRBUTTONUP:
// Prepare and show the system menu.
if (wParam == HTCAPTION) {
HMENU menu = GetUpdatedSystemMenu(hwnd);
UINT flags = TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD;
if (GetSystemMetrics(SM_MENUDROPALIGNMENT))
flags |= TPM_RIGHTALIGN;
WPARAM cmd = TrackPopupMenu(menu, flags, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, hwnd, nullptr);
if (cmd)
PostMessage(hwnd, WM_SYSCOMMAND, cmd, 0);
*callDef = false;
return 0;
}
break;
case WM_SYSCOMMAND:
if (wParam == SC_KEYMENU) {
// Show the "menu bar" (and the desired submenu)
gMenuAccelPressed = (WCHAR)lParam;
if (' ' == gMenuAccelPressed) {
// map space to the accelerator of the Window menu
if (str::FindChar(_TR("&Window"), '&'))
gMenuAccelPressed = *(str::FindChar(_TR("&Window"), '&') + 1);
}
PostMessage(win->hwndCaption, WM_COMMAND, MAKELONG(BTN_ID_FIRST + CB_MENU, BN_CLICKED), 0);
*callDef = false;
return 0;
}
break;
case WM_INITMENUPOPUP:
if (gMenuAccelPressed) {
// poorly documented hack: find the menu window and send it the accelerator key
HWND hMenu = FindWindow(UNDOCUMENTED_MENU_CLASS_NAME, nullptr);
if (hMenu) {
if ('a' <= gMenuAccelPressed && gMenuAccelPressed <= 'z')
gMenuAccelPressed -= 'a' - 'A';
if ('A' <= gMenuAccelPressed && gMenuAccelPressed <= 'Z')
PostMessage(hMenu, WM_KEYDOWN, gMenuAccelPressed, 0);
else
PostMessage(hMenu, WM_CHAR, gMenuAccelPressed, 0);
}
gMenuAccelPressed = 0;
}
break;
case WM_SYSCOLORCHANGE:
win->caption->UpdateColors(hwnd == GetForegroundWindow());
break;
case WM_DWMCOLORIZATIONCOLORCHANGED:
win->caption->UpdateColors(hwnd == GetForegroundWindow());
if (!IsIconic(hwnd))
RedrawWindow(win->hwndCaption, nullptr, nullptr,
RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
break;
case WM_DWMCOMPOSITIONCHANGED:
win->caption->UpdateBackgroundAlpha();
ClientRect cr(hwnd);
SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE);
if (ClientRect(hwnd) == cr)
SendMessage(hwnd, WM_SIZE, 0, MAKELONG(cr.dx, cr.dy));
*callDef = false;
return 0;
}
*callDef = true;
return 0;
}
static HMENU GetUpdatedSystemMenu(HWND hwnd) {
// don't reset the system menu (in case other applications have added to it)
HMENU menu = GetSystemMenu(hwnd, FALSE);
// prevents drawing in the caption's area
// TODO: how can this even happen?
ToggleWindowStyle(hwnd, WS_VISIBLE, false);
bool maximized = IsZoomed(hwnd);
EnableMenuItem(menu, SC_SIZE, maximized ? MF_GRAYED : MF_ENABLED);
EnableMenuItem(menu, SC_MOVE, maximized ? MF_GRAYED : MF_ENABLED);
EnableMenuItem(menu, SC_MINIMIZE, MF_ENABLED);
EnableMenuItem(menu, SC_MAXIMIZE, maximized ? MF_GRAYED : MF_ENABLED);
EnableMenuItem(menu, SC_CLOSE, MF_ENABLED);
EnableMenuItem(menu, SC_RESTORE, maximized ? MF_ENABLED : MF_GRAYED);
SetMenuDefaultItem(menu, maximized ? SC_RESTORE : SC_MAXIMIZE, FALSE);
ToggleWindowStyle(hwnd, WS_VISIBLE, true);
return menu;
}
static void MenuBarAsPopupMenu(WindowInfo* win, int x, int y) {
int count = GetMenuItemCount(win->menu);
if (count <= 0) {
return;
}
HMENU popup = CreatePopupMenu();
MENUITEMINFO mii = {0};
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_SUBMENU | MIIM_STRING;
for (int i = 0; i < count; i++) {
mii.dwTypeData = nullptr;
GetMenuItemInfo(win->menu, i, TRUE, &mii);
if (!mii.hSubMenu || !mii.cch) {
continue;
}
mii.cch++;
AutoFreeW subMenuName(AllocArray<WCHAR>(mii.cch));
mii.dwTypeData = subMenuName;
GetMenuItemInfo(win->menu, i, TRUE, &mii);
AppendMenu(popup, MF_POPUP | MF_STRING, (UINT_PTR)mii.hSubMenu, subMenuName);
}
AppendMenu(popup, MF_POPUP | MF_STRING, (UINT_PTR)GetUpdatedSystemMenu(win->hwndFrame), _TR("&Window"));
count++;
if (IsUIRightToLeft()) {
x += ClientRect(win->caption->btn[CB_MENU].hwnd).dx;
}
MarkMenuOwnerDraw(popup);
TrackPopupMenu(popup, TPM_LEFTALIGN, x, y, 0, win->hwndFrame, nullptr);
FreeMenuOwnerDrawInfoData(popup);
while (count > 0) {
--count;
RemoveMenu(popup, count, MF_BYPOSITION);
}
DestroyMenu(popup);
}