From fc52cd3f4fff2133d45d4fe4314ffee9bf6b3a99 Mon Sep 17 00:00:00 2001 From: ozone10 Date: Sat, 6 Apr 2024 16:35:51 +0200 Subject: [PATCH] Use dpi scaled fonts for some controls - prepare static dialog for hiDPI ref: #14959 Close #14947 --- PowerEditor/src/NppDarkMode.cpp | 71 ---------------- PowerEditor/src/NppDarkMode.h | 2 - .../src/ScintillaComponent/FindReplaceDlg.cpp | 2 +- .../src/WinControls/AboutDlg/URLCtrl.cpp | 2 +- .../WinControls/ColourPicker/WordStyleDlg.cpp | 13 ++- .../WinControls/StaticDialog/StaticDialog.cpp | 13 ++- .../WinControls/StaticDialog/StaticDialog.h | 17 +++- .../src/WinControls/StatusBar/StatusBar.cpp | 14 +++- PowerEditor/src/dpiManagerV2.cpp | 82 +++++++++++++++++++ PowerEditor/src/dpiManagerV2.h | 2 + 10 files changed, 130 insertions(+), 88 deletions(-) diff --git a/PowerEditor/src/NppDarkMode.cpp b/PowerEditor/src/NppDarkMode.cpp index 4ed8cde4c..19c2cf40f 100644 --- a/PowerEditor/src/NppDarkMode.cpp +++ b/PowerEditor/src/NppDarkMode.cpp @@ -2981,77 +2981,6 @@ namespace NppDarkMode SetWindowSubclass(hwnd, WindowNotifySubclass, g_windowNotifySubclassID, 0); } - // currently send message only to selected buttons; listbox and edit controls with scrollbars - void sendMessageToChildControls(HWND hwndParent, UINT msg, WPARAM wParam, LPARAM lParam) - { - struct WMessage - { - UINT _msg = 0; - WPARAM _wParam = 0; - LPARAM _lParam = 0; - }; - - struct WMessage p { msg, wParam, lParam }; - - ::EnumChildWindows(hwndParent, [](HWND hwnd, LPARAM childLParam) WINAPI_LAMBDA->BOOL{ - auto & p = *reinterpret_cast(childLParam); - constexpr size_t classNameLen = 32; - TCHAR className[classNameLen]{}; - ::GetClassName(hwnd, className, classNameLen); - auto style = ::GetWindowLongPtr(hwnd, GWL_STYLE); - - if (wcscmp(className, WC_BUTTON) == 0) - { - switch (style & BS_TYPEMASK) - { - case BS_CHECKBOX: - case BS_AUTOCHECKBOX: - case BS_3STATE: - case BS_AUTO3STATE: - case BS_RADIOBUTTON: - case BS_AUTORADIOBUTTON: - { - if ((style & BS_PUSHLIKE) != BS_PUSHLIKE) - { - ::SendMessage(hwnd, p._msg, p._wParam, p._lParam); - } - break; - } - - default: - { - break; - } - } - return TRUE; - } - - if (wcscmp(className, WC_EDIT) == 0) - { - bool hasScrollBar = ((style & WS_HSCROLL) == WS_HSCROLL) || ((style & WS_VSCROLL) == WS_VSCROLL); - if (hasScrollBar) - { - ::SendMessage(hwnd, p._msg, p._wParam, p._lParam); - } - return TRUE; - } - - if (wcscmp(className, WC_LISTBOX) == 0) - { - if ((style & LBS_COMBOBOX) != LBS_COMBOBOX) - { - bool hasScrollBar = ((style & WS_HSCROLL) == WS_HSCROLL) || ((style & WS_VSCROLL) == WS_VSCROLL); - if (hasScrollBar) - { - ::SendMessage(hwnd, p._msg, p._wParam, p._lParam); - } - } - return TRUE; - } - return TRUE; - }, reinterpret_cast(&p)); - } - void setDarkTitleBar(HWND hwnd) { constexpr DWORD win10Build2004 = 19041; diff --git a/PowerEditor/src/NppDarkMode.h b/PowerEditor/src/NppDarkMode.h index ffe9a6b06..f14abca51 100644 --- a/PowerEditor/src/NppDarkMode.h +++ b/PowerEditor/src/NppDarkMode.h @@ -222,8 +222,6 @@ namespace NppDarkMode ULONG autoSubclassAndThemePlugin(HWND hwnd, ULONG dmFlags); void autoSubclassAndThemeWindowNotify(HWND hwnd); - void sendMessageToChildControls(HWND hwndParent, UINT msg, WPARAM wParam, LPARAM lParam); - void setDarkTitleBar(HWND hwnd); void setDarkExplorerTheme(HWND hwnd); void setDarkScrollBar(HWND hwnd); diff --git a/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp b/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp index b312a8ecc..f7a327d69 100644 --- a/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp +++ b/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp @@ -5907,7 +5907,7 @@ int Progress::createProgressWindow() if (_hFont == nullptr) { - LOGFONT lf{ NppParameters::getDefaultGUIFont() }; + LOGFONT lf{ DPIManagerV2::getDefaultGUIFontForDpi(_hwnd) }; _hFont = ::CreateFontIndirect(&lf); } diff --git a/PowerEditor/src/WinControls/AboutDlg/URLCtrl.cpp b/PowerEditor/src/WinControls/AboutDlg/URLCtrl.cpp index 3d3582f8c..5b4053bcb 100644 --- a/PowerEditor/src/WinControls/AboutDlg/URLCtrl.cpp +++ b/PowerEditor/src/WinControls/AboutDlg/URLCtrl.cpp @@ -162,7 +162,7 @@ LRESULT URLCtrl::runProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) if (_hfUnderlined == nullptr) { // Get the default GUI font - LOGFONT lf{ NppParameters::getDefaultGUIFont() }; + LOGFONT lf{ DPIManagerV2::getDefaultGUIFontForDpi(::GetParent(hwnd)) }; lf.lfUnderline = TRUE; // Create a new font diff --git a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp index 9edcb3370..82be25321 100644 --- a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp +++ b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp @@ -45,10 +45,10 @@ LRESULT CALLBACK ColourStaticTextHooker::colourStaticProc(HWND hwnd, UINT Messag } // Get the default GUI font - LOGFONT lf{ NppParameters::getDefaultGUIFont() }; + LOGFONT lf{ DPIManagerV2::getDefaultGUIFontForDpi(hwnd) }; HFONT hf = ::CreateFontIndirect(&lf); - HANDLE hOld = SelectObject(hdc, hf); + HANDLE hOld = ::SelectObject(hdc, hf); // Draw the text! TCHAR text[MAX_PATH]{}; @@ -136,11 +136,10 @@ intptr_t CALLBACK WordStyleDlg::run_dlgProc(UINT Message, WPARAM wParam, LPARAM _pFgColour->init(_hInst, _hSelf); _pBgColour->init(_hInst, _hSelf); - int cpDynamicalWidth = NppParameters::getInstance()._dpiManager.scaleX(25); - int cpDynamicalHeight = NppParameters::getInstance()._dpiManager.scaleY(25); + const int cpDynamicalSize = DPIManagerV2::scale(25); - move2CtrlRight(IDC_FG_STATIC, _pFgColour->getHSelf(), cpDynamicalWidth, cpDynamicalHeight); - move2CtrlRight(IDC_BG_STATIC, _pBgColour->getHSelf(), cpDynamicalWidth, cpDynamicalHeight); + move2CtrlRight(IDC_FG_STATIC, _pFgColour->getHSelf(), cpDynamicalSize, cpDynamicalSize); + move2CtrlRight(IDC_BG_STATIC, _pBgColour->getHSelf(), cpDynamicalSize, cpDynamicalSize); _pFgColour->display(); _pBgColour->display(); @@ -568,7 +567,7 @@ void WordStyleDlg::move2CtrlRight(int ctrlID, HWND handle2Move, int handle2MoveW RECT rc{}; ::GetWindowRect(::GetDlgItem(_hSelf, ctrlID), &rc); - p.x = rc.right + NppParameters::getInstance()._dpiManager.scaleX(5); + p.x = rc.right + DPIManagerV2::scale(5); p.y = rc.top + ((rc.bottom - rc.top) / 2) - handle2MoveHeight / 2; ::ScreenToClient(_hSelf, &p); diff --git a/PowerEditor/src/WinControls/StaticDialog/StaticDialog.cpp b/PowerEditor/src/WinControls/StaticDialog/StaticDialog.cpp index c110336e3..16526e387 100644 --- a/PowerEditor/src/WinControls/StaticDialog/StaticDialog.cpp +++ b/PowerEditor/src/WinControls/StaticDialog/StaticDialog.cpp @@ -18,7 +18,7 @@ #include #include "StaticDialog.h" #include "Common.h" -#include "NppDarkMode.h" +//#include "NppDarkMode.h" StaticDialog::~StaticDialog() { @@ -257,11 +257,22 @@ void StaticDialog::create(int dialogID, bool isRTL, bool msgDestParent) } NppDarkMode::setDarkTitleBar(_hSelf); + setDpi(); // if the destination of message NPPM_MODELESSDIALOG is not its parent, then it's the grand-parent ::SendMessage(msgDestParent ? _hParent : (::GetParent(_hParent)), NPPM_MODELESSDIALOG, MODELESSDIALOGADD, reinterpret_cast(_hSelf)); } +void StaticDialog::createForDpi(int dialogID, bool isRTL, bool msgDestParent, DPI_AWARENESS_CONTEXT dpiAContext) +{ + const auto dpiContext = setThreadDpiAwarenessContext(dpiAContext); + create(dialogID, isRTL, msgDestParent); + if (dpiContext != NULL) + { + setThreadDpiAwarenessContext(dpiContext); + } +} + intptr_t CALLBACK StaticDialog::dlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) diff --git a/PowerEditor/src/WinControls/StaticDialog/StaticDialog.h b/PowerEditor/src/WinControls/StaticDialog/StaticDialog.h index d545d2e71..048f55494 100644 --- a/PowerEditor/src/WinControls/StaticDialog/StaticDialog.h +++ b/PowerEditor/src/WinControls/StaticDialog/StaticDialog.h @@ -13,7 +13,9 @@ // // You should have received a copy of the GNU General Public License // along with this program. If not, see . + #pragma once +#include "dpiManagerV2.h" #include "Notepad_plus_msgs.h" #include "Window.h" @@ -36,12 +38,13 @@ struct DLGTEMPLATEEX // The structure has more fields but are variable length }; -class StaticDialog : public Window +class StaticDialog : public Window, public DPIManagerV2 { public : virtual ~StaticDialog(); virtual void create(int dialogID, bool isRTL = false, bool msgDestParent = true); + virtual void createForDpi(int dialogID, bool isRTL = false, bool msgDestParent = true, DPI_AWARENESS_CONTEXT dpiAContext = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); virtual bool isCreated() const { return (_hSelf != nullptr); @@ -69,6 +72,18 @@ public : ::SendDlgItemMessage(_hSelf, checkControlID, BM_SETCHECK, checkOrNot ? BST_CHECKED : BST_UNCHECKED, 0); } + void setDpi() { + DPIManagerV2::setDpi(_hSelf); + } + + void setPositionDpi(LPARAM lParam) { + DPIManagerV2::setPositionDpi(lParam, _hSelf); + } + + void sendDpiMsgToChildCtrls(WPARAM wParam = 0, LPARAM lParam = 0) { + DPIManagerV2::sendMessageToChildControls(_hSelf, WM_DPICHANGED, wParam, lParam); + } + void destroy() override; protected: diff --git a/PowerEditor/src/WinControls/StatusBar/StatusBar.cpp b/PowerEditor/src/WinControls/StatusBar/StatusBar.cpp index 9f0bc7edd..b1194f486 100644 --- a/PowerEditor/src/WinControls/StatusBar/StatusBar.cpp +++ b/PowerEditor/src/WinControls/StatusBar/StatusBar.cpp @@ -65,7 +65,7 @@ struct StatusBarSubclassInfo { if (!hTheme) { - hTheme = OpenThemeData(hwnd, L"Status"); + hTheme = ::OpenThemeData(hwnd, VSCLASS_STATUS); } return hTheme != nullptr; } @@ -99,7 +99,7 @@ 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) +static LRESULT CALLBACK StatusBarSubclass(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) { StatusBarSubclassInfo* pStatusBarInfo = reinterpret_cast(dwRefData); @@ -240,11 +240,17 @@ LRESULT CALLBACK StatusBarSubclass(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l break; } + case WM_DPICHANGED: case WM_THEMECHANGED: { pStatusBarInfo->closeTheme(); - LOGFONT lf{ NppParameters::getDefaultGUIFont(NppParameters::DefaultFontType::status) }; + LOGFONT lf{ DPIManagerV2::getDefaultGUIFontForDpi(::GetParent(hWnd), DPIManagerV2::FontType::status) }; pStatusBarInfo->setFont(::CreateFontIndirect(&lf)); + + if (uMsg == WM_DPICHANGED) + { + return 0; + } break; } } @@ -269,7 +275,7 @@ void StatusBar::init(HINSTANCE hInst, HWND hPere, int nbParts) if (!_hSelf) throw std::runtime_error("StatusBar::init : CreateWindowEx() function return null"); - LOGFONT lf{ NppParameters::getDefaultGUIFont(NppParameters::DefaultFontType::status) }; + LOGFONT lf{ DPIManagerV2::getDefaultGUIFontForDpi(_hParent, DPIManagerV2::FontType::status) }; StatusBarSubclassInfo* pStatusBarInfo = new StatusBarSubclassInfo(::CreateFontIndirect(&lf)); _pStatusBarInfo = pStatusBarInfo; diff --git a/PowerEditor/src/dpiManagerV2.cpp b/PowerEditor/src/dpiManagerV2.cpp index f9d28288a..0a967ba99 100644 --- a/PowerEditor/src/dpiManagerV2.cpp +++ b/PowerEditor/src/dpiManagerV2.cpp @@ -17,6 +17,17 @@ #include "dpiManagerV2.h" +#include + + +#if defined(__GNUC__) && __GNUC__ > 8 +#define WINAPI_LAMBDA_RETURN(return_t) -> return_t WINAPI +#elif defined(__GNUC__) +#define WINAPI_LAMBDA_RETURN(return_t) WINAPI -> return_t +#else +#define WINAPI_LAMBDA_RETURN(return_t) -> return_t +#endif + template bool ptrFn(HMODULE handle, P& pointer, const char* name) { @@ -182,3 +193,74 @@ LOGFONT DPIManagerV2::getDefaultGUIFontForDpi(UINT dpi, FontType type) return lf; } + +// currently send message only to selected buttons; listbox and edit controls with scrollbars +void DPIManagerV2::sendMessageToChildControls(HWND hwndParent, UINT msg, WPARAM wParam, LPARAM lParam) +{ + struct WMessage + { + UINT _msg = 0; + WPARAM _wParam = 0; + LPARAM _lParam = 0; + }; + + struct WMessage p { msg, wParam, lParam }; + + ::EnumChildWindows(hwndParent, [](HWND hwnd, LPARAM childLParam) WINAPI_LAMBDA_RETURN(BOOL) { + auto & p = *reinterpret_cast(childLParam); + constexpr size_t classNameLen = 32; + TCHAR className[classNameLen]{}; + ::GetClassName(hwnd, className, classNameLen); + auto style = ::GetWindowLongPtr(hwnd, GWL_STYLE); + + if (wcscmp(className, WC_BUTTON) == 0) + { + switch (style & BS_TYPEMASK) + { + case BS_CHECKBOX: + case BS_AUTOCHECKBOX: + case BS_3STATE: + case BS_AUTO3STATE: + case BS_RADIOBUTTON: + case BS_AUTORADIOBUTTON: + { + if ((style & BS_PUSHLIKE) != BS_PUSHLIKE) + { + ::SendMessage(hwnd, p._msg, p._wParam, p._lParam); + } + break; + } + + default: + { + break; + } + } + return TRUE; + } + + if (wcscmp(className, WC_EDIT) == 0) + { + bool hasScrollBar = ((style & WS_HSCROLL) == WS_HSCROLL) || ((style & WS_VSCROLL) == WS_VSCROLL); + if (hasScrollBar) + { + ::SendMessage(hwnd, p._msg, p._wParam, p._lParam); + } + return TRUE; + } + + if (wcscmp(className, WC_LISTBOX) == 0) + { + if ((style & LBS_COMBOBOX) != LBS_COMBOBOX) + { + bool hasScrollBar = ((style & WS_HSCROLL) == WS_HSCROLL) || ((style & WS_VSCROLL) == WS_VSCROLL); + if (hasScrollBar) + { + ::SendMessage(hwnd, p._msg, p._wParam, p._lParam); + } + } + return TRUE; + } + return TRUE; + }, reinterpret_cast(&p)); +} diff --git a/PowerEditor/src/dpiManagerV2.h b/PowerEditor/src/dpiManagerV2.h index 1ac12059f..9513f1eee 100644 --- a/PowerEditor/src/dpiManagerV2.h +++ b/PowerEditor/src/dpiManagerV2.h @@ -115,6 +115,8 @@ public: return getDefaultGUIFontForDpi(getDpiForWindow(hWnd), type); } + static void sendMessageToChildControls(HWND hwndParent, UINT msg, WPARAM wParam, LPARAM lParam); + private: UINT _dpi = USER_DEFAULT_SCREEN_DPI; };