Refactoring some code of Dark mode

This commit is contained in:
Don HO 2021-05-27 03:56:22 +02:00
parent 9a2dcaa5f8
commit 51207a4fe5
4 changed files with 129 additions and 128 deletions

View File

@ -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;

View File

@ -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<StatusBarSubclassInfo*>(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<int>(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<UINT>(i)
, ODA_DRAWENTIRE
, id
, hWnd
, hdc
, rcPart
, static_cast<ULONG_PTR>(lr)
};
SendMessage(GetParent(hWnd), WM_DRAWITEM, id, (LPARAM)&dis);
}
else
{
DrawText(hdc, str.data(), static_cast<int>(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<int>(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<UINT>(i)
, ODA_DRAWENTIRE
, id
, hWnd
, hdc
, rcPart
, static_cast<ULONG_PTR>(lr)
};
SendMessage(GetParent(hWnd), WM_DRAWITEM, id, (LPARAM)&dis);
}
else
{
DrawText(hdc, str.data(), static_cast<int>(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<DWORD_PTR>(pStatusBarInfo));
@ -268,6 +265,7 @@ bool StatusBar::setPartWidth(int whichPart, int width)
void StatusBar::destroy()
{
::DestroyWindow(_hSelf);
delete _pStatusBarInfo;
}

View File

@ -23,6 +23,7 @@
#include "Common.h"
#include <vector>
struct StatusBarSubclassInfo;
class StatusBar final : public Window
@ -51,4 +52,5 @@ private:
std::vector<int> _partWidthArray;
int *_lpParts = nullptr;
generic_string _lastSetText;
StatusBarSubclassInfo* _pStatusBarInfo = nullptr;
};

View File

@ -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);