Skip to content

Commit

Permalink
custom draw toolbar to allow changing text color
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Aug 8, 2024
1 parent 651de85 commit 3e0f33e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ static int gCurrThemeIndex = 0;
static Theme* gCurrentTheme = nullptr;
static Theme* gThemeLight = nullptr;

int GetCurrentThemeIndex() {
return gCurrThemeIndex;
bool IsCurrentThemeDefault() {
return gCurrThemeIndex == 0;
}

void CreateThemeCommands() {
Expand Down
2 changes: 1 addition & 1 deletion src/Theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ void SetTheme(const char* name);
void SetCurrentThemeFromSettings();
void SelectNextTheme();
void CreateThemeCommands();
int GetCurrentThemeCmdId(int* firstId, int* lastId);

COLORREF ThemeDocumentColors(COLORREF&);
COLORREF ThemeMainWindowBackgroundColor();
Expand All @@ -23,6 +22,7 @@ COLORREF ThemeNotificationsHighlightColor();
COLORREF ThemeNotificationsHighlightTextColor();
COLORREF ThemeNotificationsProgressColor();
bool ThemeColorizeControls();
bool IsCurrentThemeDefault();

extern int gFirstSetThemeCmdId;
extern int gLastSetThemeCmdId;
Expand Down
75 changes: 61 additions & 14 deletions src/Toolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ void UpdateFindbox(MainWindow* win) {
}
}

LRESULT CALLBACK BgSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass,
LRESULT CALLBACK ReBarWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass,
DWORD_PTR dwRefData) {
if (WM_ERASEBKGND == uMsg && ThemeColorizeControls()) {
HDC hdc = (HDC)wParam;
Expand All @@ -393,8 +393,46 @@ LRESULT CALLBACK BgSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPar
DeleteObject(bgBrush);
return 1;
}
if (WM_NOTIFY == uMsg) {
auto win = FindMainWindowByHwnd(hWnd);
NMHDR* hdr = (NMHDR*)lParam;
HWND chwnd = hdr->hwndFrom;
if (hdr->code == NM_CUSTOMDRAW)
{
if (win && win->hwndToolbar == chwnd) {
NMTBCUSTOMDRAW* custDraw = (NMTBCUSTOMDRAW*)hdr;
switch (custDraw->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
return CDRF_NOTIFYITEMDRAW;

case CDDS_ITEMPREPAINT:
{
auto col = ThemeWindowTextColor();
//col = RGB(255, 0, 0);
//SetTextColor(custDraw->nmcd.hdc, col);
UINT itemState = custDraw->nmcd.uItemState;
if (itemState & CDIS_DISABLED) {
// TODO: this doesn't work
col = ThemeWindowTextDisabledColor();
//col = RGB(255, 0, 0);
custDraw->clrText = col;
} else if (false && itemState & CDIS_SELECTED) {
custDraw->clrText = RGB(0, 255, 0);
} else if (false && itemState & CDIS_GRAYED) {
custDraw->clrText = RGB(0, 0, 255);
} else {
custDraw->clrText = col;
}
return CDRF_DODEFAULT;
//return CDRF_NEWFONT;
}
}
}
}
}
if (WM_NCDESTROY == uMsg) {
RemoveWindowSubclass(hWnd, BgSubclassProc, uIdSubclass);
RemoveWindowSubclass(hWnd, ReBarWndProc, uIdSubclass);
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
Expand Down Expand Up @@ -961,19 +999,39 @@ void UpdateToolbarAfterThemeChange(MainWindow* win) {
HwndScheduleRepaint(win->hwndToolbar);
}

#pragma comment(lib, "UxTheme.lib")

// https://docs.microsoft.com/en-us/windows/win32/controls/toolbar-control-reference
void CreateToolbar(MainWindow* win) {
kButtonSpacingX = 0;
HINSTANCE hinst = GetModuleHandle(nullptr);
HWND hwndParent = win->hwndFrame;

DWORD dwStyle = WS_CHILD | WS_CLIPCHILDREN | WS_BORDER | RBS_VARHEIGHT | RBS_BANDBORDERS;
dwStyle |= CCS_NODIVIDER | CCS_NOPARENTALIGN | WS_VISIBLE;
win->hwndReBar = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAME, nullptr, dwStyle, 0, 0, 0, 0, hwndParent,
(HMENU)IDC_REBAR, hinst, nullptr);
SetWindowSubclass(win->hwndReBar, ReBarWndProc, 0, 0);

REBARINFO rbi{};
rbi.cbSize = sizeof(REBARINFO);
rbi.fMask = 0;
rbi.himl = (HIMAGELIST) nullptr;
SendMessageW(win->hwndReBar, RB_SETBARINFO, 0, (LPARAM)&rbi);

DWORD style = WS_CHILD | WS_CLIPSIBLINGS | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT;
style |= TBSTYLE_LIST | CCS_NODIVIDER | CCS_NOPARENTALIGN;
const WCHAR* cls = TOOLBARCLASSNAME;
HMENU cmd = (HMENU)IDC_TOOLBAR;
HWND hwndToolbar = CreateWindowExW(0, cls, nullptr, style, 0, 0, 0, 0, hwndParent, cmd, hinst, nullptr);
HWND hwndToolbar = CreateWindowExW(0, cls, nullptr, style, 0, 0, 0, 0, win->hwndReBar, cmd, hinst, nullptr);
win->hwndToolbar = hwndToolbar;
SendMessageW(hwndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);

if (!IsCurrentThemeDefault()) {
// without this custom draw code doesn't work
SetWindowTheme(hwndToolbar, L"", L"");
}

int iconSize = SetToolbarIconsImageList(win);

TBMETRICS tbMetrics{};
Expand Down Expand Up @@ -1044,17 +1102,6 @@ void CreateToolbar(MainWindow* win) {
}

ShowWindow(hwndToolbar, SW_SHOW);
DWORD dwStyle = WS_CHILD | WS_CLIPCHILDREN | WS_BORDER | RBS_VARHEIGHT | RBS_BANDBORDERS;
dwStyle |= CCS_NODIVIDER | CCS_NOPARENTALIGN | WS_VISIBLE;
win->hwndReBar = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAME, nullptr, dwStyle, 0, 0, 0, 0, hwndParent,
(HMENU)IDC_REBAR, hinst, nullptr);
SetWindowSubclass(win->hwndReBar, BgSubclassProc, 0, 0);

REBARINFO rbi{};
rbi.cbSize = sizeof(REBARINFO);
rbi.fMask = 0;
rbi.himl = (HIMAGELIST) nullptr;
SendMessageW(win->hwndReBar, RB_SETBARINFO, 0, (LPARAM)&rbi);

REBARBANDINFOW rbBand{};
rbBand.cbSize = sizeof(REBARBANDINFOW);
Expand Down

0 comments on commit 3e0f33e

Please sign in to comment.