Fix memory leak, fix warnings, replace GetWindowLong with GetWindowLongPtr

Fix #12697, close #12698
This commit is contained in:
ozone10 2022-12-24 10:47:42 +01:00 committed by Don Ho
parent 2fb7da73d7
commit d1565a4705
2 changed files with 64 additions and 56 deletions

View File

@ -41,6 +41,15 @@
#pragma comment(lib, "uxtheme.lib") #pragma comment(lib, "uxtheme.lib")
#endif #endif
constexpr COLORREF HEXRGB(DWORD rrggbb) {
// from 0xRRGGBB like natural #RRGGBB
// to the little-endian 0xBBGGRR
return
((rrggbb & 0xFF0000) >> 16) |
((rrggbb & 0x00FF00)) |
((rrggbb & 0x0000FF) << 16);
}
namespace NppDarkMode namespace NppDarkMode
{ {
struct Brushes struct Brushes
@ -364,7 +373,13 @@ namespace NppDarkMode
initExperimentalDarkMode(); initExperimentalDarkMode();
initAdvancedOptions(); initAdvancedOptions();
if (NppDarkMode::isWindowsModeEnabled() && NppDarkMode::isWindows10()) g_isAtLeastWindows10 = NppDarkMode::isWindows10();
if (!g_isAtLeastWindows10)
{
g_advOptions._enableWindowsMode = false;
}
else if (NppDarkMode::isWindowsModeEnabled())
{ {
NppParameters& nppParam = NppParameters::getInstance(); NppParameters& nppParam = NppParameters::getInstance();
NppGUI& nppGUI = nppParam.getNppGUI(); NppGUI& nppGUI = nppParam.getNppGUI();
@ -375,13 +390,6 @@ namespace NppDarkMode
setDarkMode(_options.enable, true); setDarkMode(_options.enable, true);
g_isAtLeastWindows10 = NppDarkMode::isWindows10();
if (!g_isAtLeastWindows10)
{
g_advOptions._enableWindowsMode = false;
}
using PWINEGETVERSION = const CHAR* (__cdecl *)(void); using PWINEGETVERSION = const CHAR* (__cdecl *)(void);
PWINEGETVERSION pWGV = nullptr; PWINEGETVERSION pWGV = nullptr;
@ -760,14 +768,15 @@ namespace NppDarkMode
case WM_UAHDRAWMENU: case WM_UAHDRAWMENU:
{ {
UAHMENU* pUDM = (UAHMENU*)lParam; UAHMENU* pUDM = (UAHMENU*)lParam;
RECT rc = {}; RECT rc{};
// get the menubar rect // get the menubar rect
{ {
MENUBARINFO mbi = { sizeof(mbi) }; MENUBARINFO mbi{};
mbi.cbSize = sizeof(MENUBARINFO);
GetMenuBarInfo(hWnd, OBJID_MENU, 0, &mbi); GetMenuBarInfo(hWnd, OBJID_MENU, 0, &mbi);
RECT rcWindow; RECT rcWindow{};
GetWindowRect(hWnd, &rcWindow); GetWindowRect(hWnd, &rcWindow);
// the rcBar is offset by the window rect // the rcBar is offset by the window rect
@ -789,8 +798,10 @@ namespace NppDarkMode
// get the menu item string // get the menu item string
wchar_t menuString[256] = { '\0' }; wchar_t menuString[256] = { '\0' };
MENUITEMINFO mii = { sizeof(mii), MIIM_STRING }; MENUITEMINFO mii{};
{ {
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_STRING;
mii.dwTypeData = menuString; mii.dwTypeData = menuString;
mii.cch = (sizeof(menuString) / 2) - 1; mii.cch = (sizeof(menuString) / 2) - 1;
@ -851,7 +862,8 @@ namespace NppDarkMode
{ {
DrawThemeBackground(g_menuTheme, pUDMI->um.hdc, MENU_POPUPITEM, iBackgroundStateID, &pUDMI->dis.rcItem, nullptr); DrawThemeBackground(g_menuTheme, pUDMI->um.hdc, MENU_POPUPITEM, iBackgroundStateID, &pUDMI->dis.rcItem, nullptr);
} }
DTTOPTS dttopts = { sizeof(dttopts) }; DTTOPTS dttopts{};
dttopts.dwSize = sizeof(DTTOPTS);
if (iTextStateID == MPI_NORMAL || iTextStateID == MPI_HOT) if (iTextStateID == MPI_NORMAL || iTextStateID == MPI_HOT)
{ {
dttopts.dwFlags |= DTT_TEXTCOLOR; dttopts.dwFlags |= DTT_TEXTCOLOR;
@ -880,17 +892,18 @@ namespace NppDarkMode
void drawUAHMenuNCBottomLine(HWND hWnd) void drawUAHMenuNCBottomLine(HWND hWnd)
{ {
MENUBARINFO mbi = { sizeof(mbi) }; MENUBARINFO mbi{};
mbi.cbSize = sizeof(MENUBARINFO);
if (!GetMenuBarInfo(hWnd, OBJID_MENU, 0, &mbi)) if (!GetMenuBarInfo(hWnd, OBJID_MENU, 0, &mbi))
{ {
return; return;
} }
RECT rcClient = {}; RECT rcClient{};
GetClientRect(hWnd, &rcClient); GetClientRect(hWnd, &rcClient);
MapWindowPoints(hWnd, nullptr, (POINT*)&rcClient, 2); MapWindowPoints(hWnd, nullptr, (POINT*)&rcClient, 2);
RECT rcWindow = {}; RECT rcWindow{};
GetWindowRect(hWnd, &rcWindow); GetWindowRect(hWnd, &rcWindow);
OffsetRect(&rcClient, -rcWindow.left, -rcWindow.top); OffsetRect(&rcClient, -rcWindow.left, -rcWindow.top);
@ -978,16 +991,16 @@ namespace NppDarkMode
void renderButton(HWND hwnd, HDC hdc, HTHEME hTheme, int iPartID, int iStateID) void renderButton(HWND hwnd, HDC hdc, HTHEME hTheme, int iPartID, int iStateID)
{ {
RECT rcClient = {}; RECT rcClient{};
WCHAR szText[256] = { '\0' }; WCHAR szText[256] = { '\0' };
DWORD nState = static_cast<DWORD>(SendMessage(hwnd, BM_GETSTATE, 0, 0)); DWORD nState = static_cast<DWORD>(SendMessage(hwnd, BM_GETSTATE, 0, 0));
DWORD uiState = static_cast<DWORD>(SendMessage(hwnd, WM_QUERYUISTATE, 0, 0)); DWORD uiState = static_cast<DWORD>(SendMessage(hwnd, WM_QUERYUISTATE, 0, 0));
DWORD nStyle = GetWindowLong(hwnd, GWL_STYLE); auto nStyle = ::GetWindowLongPtr(hwnd, GWL_STYLE);
HFONT hFont = nullptr; HFONT hFont = nullptr;
HFONT hOldFont = nullptr; HFONT hOldFont = nullptr;
HFONT hCreatedFont = nullptr; HFONT hCreatedFont = nullptr;
LOGFONT lf = {}; LOGFONT lf{};
if (SUCCEEDED(GetThemeFont(hTheme, hdc, iPartID, iStateID, TMT_FONT, &lf))) if (SUCCEEDED(GetThemeFont(hTheme, hdc, iPartID, iStateID, TMT_FONT, &lf)))
{ {
hCreatedFont = CreateFontIndirect(&lf); hCreatedFont = CreateFontIndirect(&lf);
@ -1032,7 +1045,9 @@ namespace NppDarkMode
DrawThemeParentBackground(hwnd, hdc, &rcClient); DrawThemeParentBackground(hwnd, hdc, &rcClient);
DrawThemeBackground(hTheme, hdc, iPartID, iStateID, &rcBackground, nullptr); DrawThemeBackground(hTheme, hdc, iPartID, iStateID, &rcBackground, nullptr);
DTTOPTS dtto = { sizeof(DTTOPTS), DTT_TEXTCOLOR }; DTTOPTS dtto{};
dtto.dwSize = sizeof(DTTOPTS);
dtto.dwFlags = DTT_TEXTCOLOR;
dtto.crText = NppDarkMode::getTextColor(); dtto.crText = NppDarkMode::getTextColor();
if (nStyle & WS_DISABLED) if (nStyle & WS_DISABLED)
@ -1094,14 +1109,15 @@ namespace NppDarkMode
return; return;
} }
BP_ANIMATIONPARAMS animParams = { sizeof(animParams) }; BP_ANIMATIONPARAMS animParams{};
animParams.cbSize = sizeof(BP_ANIMATIONPARAMS);
animParams.style = BPAS_LINEAR; animParams.style = BPAS_LINEAR;
if (iStateID != buttonData.iStateID) if (iStateID != buttonData.iStateID)
{ {
GetThemeTransitionDuration(buttonData.hTheme, iPartID, buttonData.iStateID, iStateID, TMT_TRANSITIONDURATIONS, &animParams.dwDuration); GetThemeTransitionDuration(buttonData.hTheme, iPartID, buttonData.iStateID, iStateID, TMT_TRANSITIONDURATIONS, &animParams.dwDuration);
} }
RECT rcClient = {}; RECT rcClient{};
GetClientRect(hwnd, &rcClient); GetClientRect(hwnd, &rcClient);
HDC hdcFrom = nullptr; HDC hdcFrom = nullptr;
@ -1173,7 +1189,7 @@ namespace NppDarkMode
case WM_PAINT: case WM_PAINT:
if (NppDarkMode::isEnabled() && pButtonData->ensureTheme(hWnd)) if (NppDarkMode::isEnabled() && pButtonData->ensureTheme(hWnd))
{ {
PAINTSTRUCT ps = {}; PAINTSTRUCT ps{};
HDC hdc = reinterpret_cast<HDC>(wParam); HDC hdc = reinterpret_cast<HDC>(wParam);
if (!hdc) if (!hdc)
{ {
@ -1218,12 +1234,12 @@ namespace NppDarkMode
void paintGroupbox(HWND hwnd, HDC hdc, ButtonData& buttonData) void paintGroupbox(HWND hwnd, HDC hdc, ButtonData& buttonData)
{ {
DWORD nStyle = GetWindowLong(hwnd, GWL_STYLE); auto nStyle = ::GetWindowLongPtr(hwnd, GWL_STYLE);
bool isDisabled = (nStyle & WS_DISABLED) == WS_DISABLED; bool isDisabled = (nStyle & WS_DISABLED) == WS_DISABLED;
int iPartID = BP_GROUPBOX; int iPartID = BP_GROUPBOX;
int iStateID = isDisabled ? GBS_DISABLED : GBS_NORMAL; int iStateID = isDisabled ? GBS_DISABLED : GBS_NORMAL;
RECT rcClient = {}; RECT rcClient{};
GetClientRect(hwnd, &rcClient); GetClientRect(hwnd, &rcClient);
RECT rcText = rcClient; RECT rcText = rcClient;
@ -1232,7 +1248,7 @@ namespace NppDarkMode
HFONT hFont = nullptr; HFONT hFont = nullptr;
HFONT hOldFont = nullptr; HFONT hOldFont = nullptr;
HFONT hCreatedFont = nullptr; HFONT hCreatedFont = nullptr;
LOGFONT lf = {}; LOGFONT lf{};
if (SUCCEEDED(GetThemeFont(buttonData.hTheme, hdc, iPartID, iStateID, TMT_FONT, &lf))) if (SUCCEEDED(GetThemeFont(buttonData.hTheme, hdc, iPartID, iStateID, TMT_FONT, &lf)))
{ {
hCreatedFont = CreateFontIndirect(&lf); hCreatedFont = CreateFontIndirect(&lf);
@ -1254,7 +1270,7 @@ namespace NppDarkMode
if (szText[0]) if (szText[0])
{ {
SIZE textSize = {}; SIZE textSize{};
GetTextExtentPoint32(hdc, szText, static_cast<int>(wcslen(szText)), &textSize); GetTextExtentPoint32(hdc, szText, static_cast<int>(wcslen(szText)), &textSize);
int centerPosX = isCenter ? ((rcClient.right - rcClient.left - textSize.cx) / 2) : 7; int centerPosX = isCenter ? ((rcClient.right - rcClient.left - textSize.cx) / 2) : 7;
@ -1268,7 +1284,7 @@ namespace NppDarkMode
} }
else else
{ {
SIZE textSize = {}; SIZE textSize{};
GetTextExtentPoint32(hdc, L"M", 1, &textSize); GetTextExtentPoint32(hdc, L"M", 1, &textSize);
rcBackground.top += textSize.cy / 2; rcBackground.top += textSize.cy / 2;
} }
@ -1288,7 +1304,9 @@ namespace NppDarkMode
rcText.right -= 2; rcText.right -= 2;
rcText.left += 2; rcText.left += 2;
DTTOPTS dtto = { sizeof(DTTOPTS), DTT_TEXTCOLOR }; DTTOPTS dtto{};
dtto.dwSize = sizeof(DTTOPTS);
dtto.dwFlags = DTT_TEXTCOLOR;
dtto.crText = isDisabled ? NppDarkMode::getDisabledTextColor() : NppDarkMode::getTextColor(); dtto.crText = isDisabled ? NppDarkMode::getDisabledTextColor() : NppDarkMode::getTextColor();
DWORD textFlags = isCenter ? DT_CENTER : DT_LEFT; DWORD textFlags = isCenter ? DT_CENTER : DT_LEFT;
@ -1337,7 +1355,7 @@ namespace NppDarkMode
case WM_PAINT: case WM_PAINT:
if (NppDarkMode::isEnabled() && pButtonData->ensureTheme(hWnd)) if (NppDarkMode::isEnabled() && pButtonData->ensureTheme(hWnd))
{ {
PAINTSTRUCT ps = {}; PAINTSTRUCT ps{};
HDC hdc = reinterpret_cast<HDC>(wParam); HDC hdc = reinterpret_cast<HDC>(wParam);
if (!hdc) if (!hdc)
{ {
@ -1397,7 +1415,7 @@ namespace NppDarkMode
break; break;
} }
PAINTSTRUCT ps; PAINTSTRUCT ps{};
HDC hdc = ::BeginPaint(hWnd, &ps); HDC hdc = ::BeginPaint(hWnd, &ps);
::FillRect(hdc, &ps.rcPaint, NppDarkMode::getDarkerBackgroundBrush()); ::FillRect(hdc, &ps.rcPaint, NppDarkMode::getDarkerBackgroundBrush());
@ -1413,7 +1431,7 @@ namespace NppDarkMode
HFONT hFont = reinterpret_cast<HFONT>(SendMessage(hWnd, WM_GETFONT, 0, 0)); HFONT hFont = reinterpret_cast<HFONT>(SendMessage(hWnd, WM_GETFONT, 0, 0));
auto hOldFont = SelectObject(hdc, hFont); auto hOldFont = SelectObject(hdc, hFont);
POINT ptCursor = {}; POINT ptCursor{};
::GetCursorPos(&ptCursor); ::GetCursorPos(&ptCursor);
ScreenToClient(hWnd, &ptCursor); ScreenToClient(hWnd, &ptCursor);
@ -1422,11 +1440,11 @@ namespace NppDarkMode
int nSelTab = TabCtrl_GetCurSel(hWnd); int nSelTab = TabCtrl_GetCurSel(hWnd);
for (int i = 0; i < nTabs; ++i) for (int i = 0; i < nTabs; ++i)
{ {
RECT rcItem = {}; RECT rcItem{};
TabCtrl_GetItemRect(hWnd, i, &rcItem); TabCtrl_GetItemRect(hWnd, i, &rcItem);
RECT rcFrame = rcItem; RECT rcFrame = rcItem;
RECT rcIntersect = {}; RECT rcIntersect{};
if (IntersectRect(&rcIntersect, &ps.rcPaint, &rcItem)) if (IntersectRect(&rcIntersect, &ps.rcPaint, &rcItem))
{ {
bool bHot = PtInRect(&rcItem, ptCursor); bool bHot = PtInRect(&rcItem, ptCursor);
@ -1450,7 +1468,7 @@ namespace NppDarkMode
SetBkMode(hdc, TRANSPARENT); SetBkMode(hdc, TRANSPARENT);
TCHAR label[MAX_PATH]{}; TCHAR label[MAX_PATH]{};
TCITEM tci = {}; TCITEM tci{};
tci.mask = TCIF_TEXT; tci.mask = TCIF_TEXT;
tci.pszText = label; tci.pszText = label;
tci.cchTextMax = MAX_PATH - 1; tci.cchTextMax = MAX_PATH - 1;
@ -1713,7 +1731,7 @@ namespace NppDarkMode
break; break;
} }
RECT rc = {}; RECT rc{};
::GetClientRect(hWnd, &rc); ::GetClientRect(hWnd, &rc);
PAINTSTRUCT ps{}; PAINTSTRUCT ps{};
@ -1768,7 +1786,7 @@ namespace NppDarkMode
hasFocus = ::GetFocus() == hwndEdit; hasFocus = ::GetFocus() == hwndEdit;
} }
POINT ptCursor = {}; POINT ptCursor{};
::GetCursorPos(&ptCursor); ::GetCursorPos(&ptCursor);
::ScreenToClient(hWnd, &ptCursor); ::ScreenToClient(hWnd, &ptCursor);
@ -2051,7 +2069,7 @@ namespace NppDarkMode
if ((style & CBS_DROPDOWNLIST) == CBS_DROPDOWNLIST || (style & CBS_DROPDOWN) == CBS_DROPDOWN) if ((style & CBS_DROPDOWNLIST) == CBS_DROPDOWNLIST || (style & CBS_DROPDOWN) == CBS_DROPDOWN)
{ {
COMBOBOXINFO cbi = {}; COMBOBOXINFO cbi{};
cbi.cbSize = sizeof(COMBOBOXINFO); cbi.cbSize = sizeof(COMBOBOXINFO);
BOOL result = ::GetComboBoxInfo(hwnd, &cbi); BOOL result = ::GetComboBoxInfo(hwnd, &cbi);
if (result == TRUE) if (result == TRUE)
@ -2384,7 +2402,7 @@ namespace NppDarkMode
{ {
if (NppDarkMode::isEnabled()) if (NppDarkMode::isEnabled())
{ {
RECT rect = {}; RECT rect{};
GetClientRect(hWnd, &rect); GetClientRect(hWnd, &rect);
::FillRect(reinterpret_cast<HDC>(wParam), &rect, NppDarkMode::getDarkerBackgroundBrush()); ::FillRect(reinterpret_cast<HDC>(wParam), &rect, NppDarkMode::getDarkerBackgroundBrush());
return TRUE; return TRUE;
@ -2503,7 +2521,7 @@ namespace NppDarkMode
{ {
case WM_NCDESTROY: case WM_NCDESTROY:
{ {
::RemoveWindowSubclass(hWnd, PluginDockWindowSubclass, uIdSubclass); ::RemoveWindowSubclass(hWnd, WindowNotifySubclass, uIdSubclass);
break; break;
} }
@ -2592,7 +2610,7 @@ namespace NppDarkMode
rcClient.right, rcClient.bottom rcClient.right, rcClient.bottom
}; };
POINT ptCursor = {}; POINT ptCursor{};
::GetCursorPos(&ptCursor); ::GetCursorPos(&ptCursor);
::ScreenToClient(hWnd, &ptCursor); ::ScreenToClient(hWnd, &ptCursor);
@ -2612,7 +2630,7 @@ namespace NppDarkMode
::FillRect(hdc, &rcArrowRight, isHotRight ? NppDarkMode::getHotBackgroundBrush() : NppDarkMode::getBackgroundBrush()); ::FillRect(hdc, &rcArrowRight, isHotRight ? NppDarkMode::getHotBackgroundBrush() : NppDarkMode::getBackgroundBrush());
} }
LOGFONT lf = {}; LOGFONT lf{};
auto font = reinterpret_cast<HFONT>(SendMessage(hWnd, WM_GETFONT, 0, 0)); auto font = reinterpret_cast<HFONT>(SendMessage(hWnd, WM_GETFONT, 0, 0));
::GetObject(font, sizeof(lf), &lf); ::GetObject(font, sizeof(lf), &lf);
lf.lfHeight = (dpiManager.scaleY(16) - 5) * -1; lf.lfHeight = (dpiManager.scaleY(16) - 5) * -1;
@ -3033,5 +3051,4 @@ namespace NppDarkMode
return result.toRGB(); return result.toRGB();
} }
} }

View File

@ -20,15 +20,6 @@
#include "Common.h" // for generic_string #include "Common.h" // for generic_string
constexpr COLORREF HEXRGB(DWORD rrggbb) {
// from 0xRRGGBB like natural #RRGGBB
// to the little-endian 0xBBGGRR
return
((rrggbb & 0xFF0000) >> 16) |
((rrggbb & 0x00FF00) ) |
((rrggbb & 0x0000FF) << 16);
}
namespace NppDarkMode namespace NppDarkMode
{ {
struct Colors struct Colors