From 51207a4fe5c40a1206f64aea3732982c2b15151c Mon Sep 17 00:00:00 2001 From: Don HO Date: Thu, 27 May 2021 03:56:22 +0200 Subject: [PATCH] Refactoring some code of Dark mode --- .../MISC/PluginsManager/Notepad_plus_msgs.h | 2 + .../src/WinControls/StatusBar/StatusBar.cpp | 246 +++++++++--------- .../src/WinControls/StatusBar/StatusBar.h | 2 + PowerEditor/src/WinControls/TabBar/TabBar.cpp | 7 +- 4 files changed, 129 insertions(+), 128 deletions(-) diff --git a/PowerEditor/src/MISC/PluginsManager/Notepad_plus_msgs.h b/PowerEditor/src/MISC/PluginsManager/Notepad_plus_msgs.h index acaae3061..c171c82f1 100644 --- a/PowerEditor/src/MISC/PluginsManager/Notepad_plus_msgs.h +++ b/PowerEditor/src/MISC/PluginsManager/Notepad_plus_msgs.h @@ -147,6 +147,8 @@ enum Platform { PF_UNKNOWN, PF_X86, PF_X64, PF_IA64 }; #define NPPM_ADDTOOLBARICON (NPPMSG + 41) //void NPPM_ADDTOOLBARICON(UINT funcItem[X]._cmdID, toolbarIcons icon) + //2 formats of icon are needed: .ico & .bmp + //Both handles below should be set so the icon will be displayed correctly if toolbar icon sets are changed by users struct toolbarIcons { HBITMAP hToolbarBmp; HICON hToolbarIcon; diff --git a/PowerEditor/src/WinControls/StatusBar/StatusBar.cpp b/PowerEditor/src/WinControls/StatusBar/StatusBar.cpp index 1846667ac..5abe5c224 100644 --- a/PowerEditor/src/WinControls/StatusBar/StatusBar.cpp +++ b/PowerEditor/src/WinControls/StatusBar/StatusBar.cpp @@ -44,7 +44,7 @@ StatusBar::~StatusBar() } -void StatusBar::init(HINSTANCE /*hInst*/, HWND /*hPere*/) +void StatusBar::init(HINSTANCE, HWND) { assert(false and "should never be called"); } @@ -79,141 +79,137 @@ struct StatusBarSubclassInfo constexpr UINT_PTR g_statusBarSubclassID = 42; -LRESULT CALLBACK StatusBarSubclass( - HWND hWnd, - UINT uMsg, - WPARAM wParam, - LPARAM lParam, - UINT_PTR uIdSubclass, - DWORD_PTR dwRefData -) +LRESULT CALLBACK StatusBarSubclass(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) { UNREFERENCED_PARAMETER(uIdSubclass); StatusBarSubclassInfo* pStatusBarInfo = reinterpret_cast(dwRefData); - switch (uMsg) { - case WM_ERASEBKGND: + switch (uMsg) { - if (!NppDarkMode::isEnabled()) + case WM_ERASEBKGND: { - return DefSubclassProc(hWnd, uMsg, wParam, lParam); + if (!NppDarkMode::isEnabled()) + { + return DefSubclassProc(hWnd, uMsg, wParam, lParam); + } + + RECT rc; + GetClientRect(hWnd, &rc); + FillRect((HDC)wParam, &rc, NppDarkMode::getBackgroundBrush()); + return TRUE; } - RECT rc; - GetClientRect(hWnd, &rc); - FillRect((HDC)wParam, &rc, NppDarkMode::getBackgroundBrush()); - return TRUE; - } - case WM_PAINT: - { - if (!NppDarkMode::isEnabled()) + case WM_PAINT: { - return DefSubclassProc(hWnd, uMsg, wParam, lParam); + if (!NppDarkMode::isEnabled()) + { + return DefSubclassProc(hWnd, uMsg, wParam, lParam); + } + + struct { + int horizontal; + int vertical; + int between; + } borders = { 0 }; + + SendMessage(hWnd, SB_GETBORDERS, 0, (LPARAM)&borders); + + DWORD style = GetWindowLong(hWnd, GWL_STYLE); + bool isSizeGrip = style & SBARS_SIZEGRIP; + + PAINTSTRUCT ps; + HDC hdc = BeginPaint(hWnd, &ps); + + HFONT holdFont = (HFONT)::SelectObject(hdc, NppParameters::getInstance().getDefaultUIFont()); + + RECT rcClient; + GetClientRect(hWnd, &rcClient); + + FillRect(hdc, &ps.rcPaint, NppDarkMode::getBackgroundBrush()); + + int nParts = static_cast(SendMessage(hWnd, SB_GETPARTS, 0, 0)); + std::wstring str; + for (int i = 0; i < nParts; ++i) + { + RECT rcPart = { 0 }; + SendMessage(hWnd, SB_GETRECT, i, (LPARAM)&rcPart); + RECT rcIntersect = { 0 }; + if (!IntersectRect(&rcIntersect, &rcPart, &ps.rcPaint)) + { + continue; + } + + RECT rcDivider = { rcPart.right - borders.vertical, rcPart.top, rcPart.right, rcPart.bottom }; + + DWORD cchText = 0; + cchText = LOWORD(SendMessage(hWnd, SB_GETTEXTLENGTH, i, 0)); + str.resize(cchText + 1); + LRESULT lr = SendMessage(hWnd, SB_GETTEXT, i, (LPARAM)str.data()); + bool ownerDraw = false; + if (cchText == 0 && (lr & ~(SBT_NOBORDERS | SBT_POPOUT | SBT_RTLREADING)) != 0) + { + // this is a pointer to the text + ownerDraw = true; + } + SetBkMode(hdc, TRANSPARENT); + SetTextColor(hdc, NppDarkMode::getTextColor()); + + rcPart.left += borders.between; + rcPart.right -= borders.vertical; + + if (ownerDraw) + { + UINT id = GetDlgCtrlID(hWnd); + DRAWITEMSTRUCT dis = { + 0 + , 0 + , static_cast(i) + , ODA_DRAWENTIRE + , id + , hWnd + , hdc + , rcPart + , static_cast(lr) + }; + + SendMessage(GetParent(hWnd), WM_DRAWITEM, id, (LPARAM)&dis); + } + else + { + DrawText(hdc, str.data(), static_cast(str.size()), &rcPart, DT_SINGLELINE | DT_VCENTER | DT_LEFT); + } + + if (!isSizeGrip && i < (nParts - 1)) + { + FillRect(hdc, &rcDivider, NppDarkMode::getSofterBackgroundBrush()); + } + } + + if (isSizeGrip) + { + pStatusBarInfo->ensureTheme(hWnd); + SIZE gripSize = { 0 }; + GetThemePartSize(pStatusBarInfo->hTheme, hdc, SP_GRIPPER, 0, &rcClient, TS_DRAW, &gripSize); + RECT rc = rcClient; + rc.left = rc.right - gripSize.cx; + rc.top = rc.bottom - gripSize.cy; + DrawThemeBackground(pStatusBarInfo->hTheme, hdc, SP_GRIPPER, 0, &rc, nullptr); + } + + ::SelectObject(hdc, holdFont); + + EndPaint(hWnd, &ps); + return FALSE; } - struct { - int horizontal; - int vertical; - int between; - } borders = { 0 }; + case WM_NCDESTROY: + RemoveWindowSubclass(hWnd, StatusBarSubclass, g_statusBarSubclassID); + break; - SendMessage(hWnd, SB_GETBORDERS, 0, (LPARAM)&borders); - - DWORD style = GetWindowLong(hWnd, GWL_STYLE); - bool isSizeGrip = style & SBARS_SIZEGRIP; - - PAINTSTRUCT ps; - HDC hdc = BeginPaint(hWnd, &ps); - - HFONT holdFont = (HFONT)::SelectObject(hdc, NppParameters::getInstance().getDefaultUIFont()); - - RECT rcClient; - GetClientRect(hWnd, &rcClient); - - FillRect(hdc, &ps.rcPaint, NppDarkMode::getBackgroundBrush()); - - int nParts = static_cast(SendMessage(hWnd, SB_GETPARTS, 0, 0)); - std::wstring str; - for (int i = 0; i < nParts; ++i) - { - RECT rcPart = { 0 }; - SendMessage(hWnd, SB_GETRECT, i, (LPARAM)&rcPart); - RECT rcIntersect = { 0 }; - if (!IntersectRect(&rcIntersect, &rcPart, &ps.rcPaint)) - { - continue; - } - - RECT rcDivider = { rcPart.right - borders.vertical, rcPart.top, rcPart.right, rcPart.bottom }; - - DWORD cchText = 0; - cchText = LOWORD(SendMessage(hWnd, SB_GETTEXTLENGTH, i, 0)); - str.resize(cchText + 1); - LRESULT lr = SendMessage(hWnd, SB_GETTEXT, i, (LPARAM)str.data()); - bool ownerDraw = false; - if (cchText == 0 && (lr & ~(SBT_NOBORDERS | SBT_POPOUT | SBT_RTLREADING)) != 0) - { - // this is a pointer to the text - ownerDraw = true; - } - SetBkMode(hdc, TRANSPARENT); - SetTextColor(hdc, NppDarkMode::getTextColor()); - - rcPart.left += borders.between; - rcPart.right -= borders.vertical; - - if (ownerDraw) - { - UINT id = GetDlgCtrlID(hWnd); - DRAWITEMSTRUCT dis = { - 0 - , 0 - , static_cast(i) - , ODA_DRAWENTIRE - , id - , hWnd - , hdc - , rcPart - , static_cast(lr) - }; - - SendMessage(GetParent(hWnd), WM_DRAWITEM, id, (LPARAM)&dis); - } - else - { - DrawText(hdc, str.data(), static_cast(str.size()), &rcPart, DT_SINGLELINE | DT_VCENTER | DT_LEFT); - } - - if (!isSizeGrip && i < (nParts - 1)) - { - FillRect(hdc, &rcDivider, NppDarkMode::getSofterBackgroundBrush()); - } - } - - if (isSizeGrip) - { - pStatusBarInfo->ensureTheme(hWnd); - SIZE gripSize = { 0 }; - GetThemePartSize(pStatusBarInfo->hTheme, hdc, SP_GRIPPER, 0, &rcClient, TS_DRAW, &gripSize); - RECT rc = rcClient; - rc.left = rc.right - gripSize.cx; - rc.top = rc.bottom - gripSize.cy; - DrawThemeBackground(pStatusBarInfo->hTheme, hdc, SP_GRIPPER, 0, &rc, nullptr); - } - - ::SelectObject(hdc, holdFont); - - EndPaint(hWnd, &ps); - return 0; - } - case WM_NCDESTROY: - RemoveWindowSubclass(hWnd, StatusBarSubclass, g_statusBarSubclassID); - delete pStatusBarInfo; - break; - case WM_THEMECHANGED: - pStatusBarInfo->closeTheme(); - break; + case WM_THEMECHANGED: + pStatusBarInfo->closeTheme(); + break; } return DefSubclassProc(hWnd, uMsg, wParam, lParam); } @@ -235,7 +231,8 @@ void StatusBar::init(HINSTANCE hInst, HWND hPere, int nbParts) if (!_hSelf) throw std::runtime_error("StatusBar::init : CreateWindowEx() function return null"); - auto* pStatusBarInfo = new StatusBarSubclassInfo(); + StatusBarSubclassInfo* pStatusBarInfo = new StatusBarSubclassInfo(); + _pStatusBarInfo = pStatusBarInfo; SetWindowSubclass(_hSelf, StatusBarSubclass, g_statusBarSubclassID, reinterpret_cast(pStatusBarInfo)); @@ -268,6 +265,7 @@ bool StatusBar::setPartWidth(int whichPart, int width) void StatusBar::destroy() { ::DestroyWindow(_hSelf); + delete _pStatusBarInfo; } diff --git a/PowerEditor/src/WinControls/StatusBar/StatusBar.h b/PowerEditor/src/WinControls/StatusBar/StatusBar.h index 6f04dfd92..76ec85063 100644 --- a/PowerEditor/src/WinControls/StatusBar/StatusBar.h +++ b/PowerEditor/src/WinControls/StatusBar/StatusBar.h @@ -23,6 +23,7 @@ #include "Common.h" #include +struct StatusBarSubclassInfo; class StatusBar final : public Window @@ -51,4 +52,5 @@ private: std::vector _partWidthArray; int *_lpParts = nullptr; generic_string _lastSetText; + StatusBarSubclassInfo* _pStatusBarInfo = nullptr; }; \ No newline at end of file diff --git a/PowerEditor/src/WinControls/TabBar/TabBar.cpp b/PowerEditor/src/WinControls/TabBar/TabBar.cpp index 6d5a6f131..c30021e4c 100644 --- a/PowerEditor/src/WinControls/TabBar/TabBar.cpp +++ b/PowerEditor/src/WinControls/TabBar/TabBar.cpp @@ -1112,14 +1112,13 @@ void TabBarPlus::drawItem(DRAWITEMSTRUCT *pDrawItemStruct, bool isDarkMode) // 3 status for each inactive tab and selected tab close item : // normal / hover / pushed int idCloseImg; - bool isDM = NppDarkMode::isEnabled(); if (_isCloseHover && (_currentHoverTabItem == nTab) && (_whichCloseClickDown == -1)) // hover - idCloseImg = isDM ? IDR_CLOSETAB_HOVER_DM : IDR_CLOSETAB_HOVER; + idCloseImg = isDarkMode ? IDR_CLOSETAB_HOVER_DM : IDR_CLOSETAB_HOVER; else if (_isCloseHover && (_currentHoverTabItem == nTab) && (_whichCloseClickDown == _currentHoverTabItem)) // pushed - idCloseImg = isDM ? IDR_CLOSETAB_PUSH_DM : IDR_CLOSETAB_PUSH; + idCloseImg = isDarkMode ? IDR_CLOSETAB_PUSH_DM : IDR_CLOSETAB_PUSH; else - idCloseImg = isSelected ? (isDM ? IDR_CLOSETAB_DM : IDR_CLOSETAB) : (isDM ? IDR_CLOSETAB_INACT_DM : IDR_CLOSETAB_INACT); + idCloseImg = isSelected ? (isDarkMode ? IDR_CLOSETAB_DM : IDR_CLOSETAB) : (isDarkMode ? IDR_CLOSETAB_INACT_DM : IDR_CLOSETAB_INACT); HDC hdcMemory; hdcMemory = ::CreateCompatibleDC(hDC);