Make new DPI management support for Find Replace dialog

Handle dpi messages in Find dialog.

ref #14959

Close #15146
This commit is contained in:
ozone10 2024-05-16 20:39:19 +02:00 committed by Don Ho
parent 5123016baa
commit bdc11a47ff
3 changed files with 140 additions and 15 deletions

View File

@ -299,13 +299,30 @@ void FindReplaceDlg::create(int dialogID, bool isRTL, bool msgDestParent, bool t
RECT rcClient{};
getClientRect(rcClient);
RECT rcCount{};
getMappedChildRect(IDCCOUNTALL, rcCount);
RECT rcOk{};
getMappedChildRect(IDOK, rcOk);
RECT rcTransGrpb{};
getMappedChildRect(IDC_TRANSPARENT_GRPBOX, rcTransGrpb);
RECT rcStatusBar{};
::GetWindowRect(_statusBar.getHSelf(), &rcStatusBar);
const LONG gap = (rcCount.top - rcOk.bottom);
_lesssModeHeight = (rcCount.bottom + gap);
const LONG padding = _dpiManager.getSystemMetricsForDpi(SM_CXPADDEDBORDER);
_szBorder.cx = (_dpiManager.getSystemMetricsForDpi(SM_CXFRAME) + padding) * 2;
_szBorder.cy = (_dpiManager.getSystemMetricsForDpi(SM_CYFRAME) + padding) * 2 + _dpiManager.getSystemMetricsForDpi(SM_CYCAPTION);
_szBorder.cy = (_dpiManager.getSystemMetricsForDpi(SM_CYFRAME) + padding) * 2
+ _dpiManager.getSystemMetricsForDpi(SM_CYCAPTION)
+ (rcStatusBar.bottom - rcStatusBar.top);
//fill min dialog size info
_szMinDialog.cx = rcClient.right - rcClient.left;
_szMinDialog.cy = rcClient.bottom - rcClient.top;
_szMinDialog.cy = rcTransGrpb.bottom + gap;
_tab.init(_hInst, _hSelf, false, true);
NppDarkMode::subclassTabControl(_tab.getHSelf());
@ -341,16 +358,6 @@ void FindReplaceDlg::create(int dialogID, bool isRTL, bool msgDestParent, bool t
goToCenter(swpFlags);
}
RECT rcCount{};
getMappedChildRect(IDCCOUNTALL, rcCount);
RECT rcOk{};
getMappedChildRect(IDOK, rcOk);
RECT rcStatusBar{};
::GetClientRect(_statusBar.getHSelf(), &rcStatusBar);
_lesssModeHeight = (rcCount.bottom + (rcCount.top - rcOk.bottom) + (rcStatusBar.bottom - rcStatusBar.top));
if (nppGUI._findWindowLessMode)
{
// reverse the value of _findWindowLessMode because the value will be inversed again in IDD_RESIZE_TOGGLE_BUTTON
@ -1695,7 +1702,108 @@ intptr_t CALLBACK FindReplaceDlg::run_dlgProc(UINT message, WPARAM wParam, LPARA
case NPPM_MODELESSDIALOG :
return ::SendMessage(_hParent, NPPM_MODELESSDIALOG, wParam, lParam);
case WM_COMMAND :
case WM_GETDPISCALEDSIZE:
{
auto newSize = reinterpret_cast<SIZE*>(lParam);
RECT rcClient{};
getClientRect(rcClient);
const UINT newDpi = static_cast<UINT>(wParam);
const UINT prevDpi = _dpiManager.getDpi();
const bool isLessModeOn = NppParameters::getInstance().getNppGUI()._findWindowLessMode;
RECT rcStatusBar{};
::GetWindowRect(_statusBar.getHSelf(), &rcStatusBar);
rcClient.right = _dpiManager.scale(rcClient.right - rcClient.left, newDpi, prevDpi);
rcClient.bottom = _dpiManager.scale((isLessModeOn ? _lesssModeHeight : _szMinDialog.cy) + (rcStatusBar.bottom - rcStatusBar.top), newDpi, prevDpi);
LONG xBorder = 0;
LONG yBorder = 0;
const auto style = static_cast<DWORD>(::GetWindowLongPtr(_hSelf, GWL_STYLE));
const auto exStyle = static_cast<DWORD>(::GetWindowLongPtr(_hSelf, GWL_EXSTYLE));
if (_dpiManager.adjustWindowRectExForDpi(&rcClient, style, FALSE, exStyle, newDpi) == FALSE)
{
const LONG padding = _dpiManager.getSystemMetricsForDpi(SM_CXPADDEDBORDER, newDpi);
xBorder = (_dpiManager.getSystemMetricsForDpi(SM_CXFRAME, newDpi) + padding) * 2;
yBorder = (_dpiManager.getSystemMetricsForDpi(SM_CYFRAME, newDpi) + padding) * 2 + _dpiManager.getSystemMetricsForDpi(SM_CYCAPTION, newDpi);
}
newSize->cx = (rcClient.right - rcClient.left) + xBorder;
newSize->cy = (rcClient.bottom - rcClient.top) + yBorder;
if (prevDpi > newDpi)
{
const auto padding = static_cast<LONG>(_dpiManager.getSystemMetricsForDpi(SM_CXPADDEDBORDER, newDpi));
newSize->cx += padding;
newSize->cy += padding;
}
return TRUE;
}
case WM_DPICHANGED:
{
const UINT prevDpi = _dpiManager.getDpi();
_dpiManager.setDpiWP(wParam);
if (_hLargerBolderFont)
::DeleteObject(_hLargerBolderFont);
if (_hCourrierNewFont)
::DeleteObject(_hCourrierNewFont);
if (_hComboBoxFont)
::DeleteObject(_hComboBoxFont);
_hLargerBolderFont = createFont(TEXT("Courier New"), 14, true, _hSelf);
::SendMessage(_hSwapButton, WM_SETFONT, reinterpret_cast<WPARAM>(_hLargerBolderFont), MAKELPARAM(TRUE, 0));
_hCourrierNewFont = createFont(TEXT("Courier New"), 12, false, _hSelf);
::SendDlgItemMessage(_hSelf, IDD_RESIZE_TOGGLE_BUTTON, WM_SETFONT, reinterpret_cast<WPARAM>(_hCourrierNewFont), MAKELPARAM(TRUE, 0));
LOGFONT lf{};
HFONT font = reinterpret_cast<HFONT>(::SendDlgItemMessage(_hSelf, IDFINDWHAT, WM_GETFONT, 0, 0));
::GetObject(font, sizeof(lf), &lf);
lf.lfHeight = -(_dpiManager.scale(16) - 5);
_hComboBoxFont = ::CreateFontIndirect(&lf);
for (auto idComboBox : { IDFINDWHAT, IDREPLACEWITH, IDD_FINDINFILES_FILTERS_COMBO, IDD_FINDINFILES_DIR_COMBO })
{
::SendDlgItemMessage(_hSelf, idComboBox, WM_SETFONT, reinterpret_cast<WPARAM>(_hComboBoxFont), MAKELPARAM(TRUE, 0));
}
RECT rcStatusBar{};
::GetWindowRect(_statusBar.getHSelf(), &rcStatusBar);
const LONG padding = _dpiManager.getSystemMetricsForDpi(SM_CXPADDEDBORDER);
_szBorder.cx = ((_dpiManager.getSystemMetricsForDpi(SM_CXFRAME) + padding) * 2);
_szBorder.cy = ((_dpiManager.getSystemMetricsForDpi(SM_CYFRAME) + padding) * 2
+ _dpiManager.getSystemMetricsForDpi(SM_CYCAPTION)
+ (rcStatusBar.bottom - rcStatusBar.top));
if (prevDpi > _dpiManager.getDpi())
{
const auto padding = static_cast<LONG>(_dpiManager.getSystemMetricsForDpi(SM_CXPADDEDBORDER));
_szBorder.cx += padding;
_szBorder.cy += padding;
}
const UINT dpi = _dpiManager.getDpi();
_szMinDialog.cx = _dpiManager.scale(_szMinDialog.cx, dpi, prevDpi);
_szMinDialog.cy = _dpiManager.scale(_szMinDialog.cy, dpi, prevDpi);
_lesssModeHeight = _dpiManager.scale(_lesssModeHeight, dpi, prevDpi);
setPositionDpi(lParam, SWP_NOZORDER | SWP_NOACTIVATE);
return TRUE;
}
case WM_COMMAND:
{
bool isMacroRecording = (static_cast<MacroStatus>(::SendMessage(_hParent, NPPM_GETCURRENTMACROSTATUS,0,0)) == MacroStatus::RecordInProgress);
NppParameters& nppParamInst = NppParameters::getInstance();

View File

@ -45,12 +45,16 @@ using fnGetDpiForWindow = UINT (WINAPI*)(HWND hwnd);
using fnGetSystemMetricsForDpi = int (WINAPI*)(int nIndex, UINT dpi);
using fnSystemParametersInfoForDpi = BOOL (WINAPI*)(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, UINT dpi);
using fnSetThreadDpiAwarenessContext = DPI_AWARENESS_CONTEXT (WINAPI*)(DPI_AWARENESS_CONTEXT dpiContext);
using fnAdjustWindowRectExForDpi = BOOL (WINAPI*)(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi);
fnGetDpiForSystem _fnGetDpiForSystem = nullptr;
fnGetDpiForWindow _fnGetDpiForWindow = nullptr;
fnGetSystemMetricsForDpi _fnGetSystemMetricsForDpi = nullptr;
fnSystemParametersInfoForDpi _fnSystemParametersInfoForDpi = nullptr;
fnSetThreadDpiAwarenessContext _fnSetThreadDpiAwarenessContext = nullptr;
fnAdjustWindowRectExForDpi _fnAdjustWindowRectExForDpi = nullptr;
void DPIManagerV2::initDpiAPI()
{
@ -64,6 +68,8 @@ void DPIManagerV2::initDpiAPI()
ptrFn(hUser32, _fnGetSystemMetricsForDpi, "GetSystemMetricsForDpi");
ptrFn(hUser32, _fnSystemParametersInfoForDpi, "SystemParametersInfoForDpi");
ptrFn(hUser32, _fnSetThreadDpiAwarenessContext, "SetThreadDpiAwarenessContext");
ptrFn(hUser32, _fnAdjustWindowRectExForDpi, "AdjustWindowRectExForDpi");
}
}
}
@ -86,6 +92,15 @@ DPI_AWARENESS_CONTEXT DPIManagerV2::setThreadDpiAwarenessContext(DPI_AWARENESS_C
return NULL;
}
BOOL DPIManagerV2::adjustWindowRectExForDpi(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi)
{
if (_fnAdjustWindowRectExForDpi != nullptr)
{
return _fnAdjustWindowRectExForDpi(lpRect, dwStyle, bMenu, dwExStyle, dpi);
}
return FALSE;
}
UINT DPIManagerV2::getDpiForSystem()
{
if (_fnGetDpiForSystem != nullptr)

View File

@ -51,6 +51,8 @@ public:
return getSystemMetricsForDpi(nIndex, _dpi);
}
static DPI_AWARENESS_CONTEXT setThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT dpiContext);
static BOOL adjustWindowRectExForDpi(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi);
static UINT getDpiForSystem();
static UINT getDpiForWindow(HWND hWnd);
@ -85,8 +87,8 @@ public:
static void setPositionDpi(LPARAM lParam, HWND hWnd, UINT flags = SWP_NOZORDER | SWP_NOACTIVATE);
static int scale(int x, UINT dpi, UINT dpi2) {
return MulDiv(x, dpi, dpi2);
static int scale(int x, UINT toDpi, UINT fromDpi) {
return MulDiv(x, toDpi, fromDpi);
}
static int scale(int x, UINT dpi) {