From 7843c99d5babe750faf7407e8955a8004be56e71 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 13 Jul 2021 16:59:20 +0100 Subject: [PATCH] Make wxRescaleCoord() private functions They're probably not that useful in application code, which should just use wxDPIChangedEvent::Scale() instead, so move them to a private header instead of making them part of the public API. --- include/wx/event.h | 4 ++-- include/wx/gdicmn.h | 18 --------------- include/wx/private/rescale.h | 43 ++++++++++++++++++++++++++++++++++++ interface/wx/event.h | 3 ++- interface/wx/gdicmn.h | 18 --------------- src/common/event.cpp | 18 +++++++++++++++ src/common/gdicmn.cpp | 10 --------- src/common/wincmn.cpp | 5 +---- src/msw/button.cpp | 1 + 9 files changed, 67 insertions(+), 53 deletions(-) create mode 100644 include/wx/private/rescale.h diff --git a/include/wx/event.h b/include/wx/event.h index 228df9e4f6..ec7fa51d9c 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -3142,8 +3142,8 @@ public: // Scale the value by the ratio between new and old DPIs carried by this // event. - int ScaleX(int x) const { return wxRescaleCoord(x, m_newDPI.x, m_oldDPI.x); } - int ScaleY(int y) const { return wxRescaleCoord(y, m_newDPI.y, m_oldDPI.y); } + int ScaleX(int x) const; + int ScaleY(int y) const; wxSize Scale(wxSize sz) const { return wxSize(ScaleX(sz.x), ScaleY(sz.y)); } diff --git a/include/wx/gdicmn.h b/include/wx/gdicmn.h index e7732db9b4..e29a77627c 100644 --- a/include/wx/gdicmn.h +++ b/include/wx/gdicmn.h @@ -1104,23 +1104,5 @@ extern wxRect WXDLLIMPEXP_CORE wxGetClientDisplayRect(); // set global cursor extern void WXDLLIMPEXP_CORE wxSetCursor(const wxCursor& cursor); -// Scale the given value by the ratio between 2 other values, with rounding. -// Do not scale the value if it's -1, just return it unchanged in this case. -extern int WXDLLIMPEXP_CORE wxRescaleCoord(int n, int newScale, int oldScale); - -inline wxPoint -wxRescaleCoord(wxPoint pt, wxSize newScale, wxSize oldScale) -{ - return wxPoint(wxRescaleCoord(pt.x, newScale.x, oldScale.x), - wxRescaleCoord(pt.y, newScale.y, oldScale.y)); -} - -inline wxSize -wxRescaleCoord(wxSize sz, wxSize newScale, wxSize oldScale) -{ - return wxSize(wxRescaleCoord(sz.x, newScale.x, oldScale.x), - wxRescaleCoord(sz.y, newScale.y, oldScale.y)); -} - #endif // _WX_GDICMNH__ diff --git a/include/wx/private/rescale.h b/include/wx/private/rescale.h new file mode 100644 index 0000000000..45a5471b59 --- /dev/null +++ b/include/wx/private/rescale.h @@ -0,0 +1,43 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: wx/private/rescale.h +// Purpose: Helpers for rescaling coordinates +// Author: Vadim Zeitlin +// Created: 2021-07-13 +// Copyright: (c) 2021 Vadim Zeitlin +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_PRIVATE_RESCALE_H_ +#define _WX_PRIVATE_RESCALE_H_ + +#include "wx/gdicmn.h" +#include "wx/math.h" + +#ifdef __WINDOWS__ + // Required in order to use wxMulDivInt32(). + #include "wx/msw/wrapwin.h" +#endif + + +// Scale the given value by the ratio between 2 other values, with rounding. +// Do not scale the value if it's -1, just return it unchanged in this case. +inline int wxRescaleCoord(int n, int newScale, int oldScale) +{ + return n == -1 ? -1 : wxMulDivInt32(n, newScale, oldScale); +} + +inline wxPoint +wxRescaleCoord(wxPoint pt, wxSize newScale, wxSize oldScale) +{ + return wxPoint(wxRescaleCoord(pt.x, newScale.x, oldScale.x), + wxRescaleCoord(pt.y, newScale.y, oldScale.y)); +} + +inline wxSize +wxRescaleCoord(wxSize sz, wxSize newScale, wxSize oldScale) +{ + return wxSize(wxRescaleCoord(sz.x, newScale.x, oldScale.x), + wxRescaleCoord(sz.y, newScale.y, oldScale.y)); +} + +#endif // _WX_PRIVATE_RESCALE_H_ diff --git a/interface/wx/event.h b/interface/wx/event.h index b6c9eb1829..d9bfe9b417 100644 --- a/interface/wx/event.h +++ b/interface/wx/event.h @@ -3450,7 +3450,8 @@ public: This is a convenience function to use in wxEVT_DPI_CHANGED event handlers, as they often need to update some sizes to the new DPI. - It simply calls wxRescaleCoord() with GetNewDPI() and GetOldDPI(). + It simply calls wxMulDivInt32() with new and old DPI values, but + is more readable and less error-prone. For example, the returned value will be twice bigger than the original one when switching from normal (96) DPI to high (2x, 192) DPI. diff --git a/interface/wx/gdicmn.h b/interface/wx/gdicmn.h index b19164ce7f..bccaa6888f 100644 --- a/interface/wx/gdicmn.h +++ b/interface/wx/gdicmn.h @@ -1344,21 +1344,3 @@ void wxDisplaySizeMM(int* width, int* height); */ wxSize wxGetDisplaySizeMM(); //@} - -/** - Scale the given value by the ratio between 2 other values, with rounding. - - Do not scale the value if it's -1, just return it unchanged in this case. - - This simply calls wxMulDivInt32() with the provided arguments, but provides - a more clear name for this operation. - - @since 3.1.6 - */ -int wxRescaleCoord(int n, int newScale, int oldScale); - -/// @overload -wxPoint wxRescaleCoord(wxPoint pt, wxSize newScale, wxSize oldScale); - -/// @overload -wxSize wxRescaleCoord(wxSize sz, wxSize newScale, wxSize oldScale); diff --git a/src/common/event.cpp b/src/common/event.cpp index 4b1409cb24..415d883d26 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -51,6 +51,10 @@ wxDEFINE_SCOPED_PTR(wxEvent, wxEventPtr) #endif // wxUSE_BASE +#if wxUSE_GUI + #include "wx/private/rescale.h" +#endif + // ---------------------------------------------------------------------------- // wxWin macros // ---------------------------------------------------------------------------- @@ -933,6 +937,20 @@ wxHelpEvent::Origin wxHelpEvent::GuessOrigin(Origin origin) return origin; } +// ---------------------------------------------------------------------------- +// wxDPIChangedEvent +// ---------------------------------------------------------------------------- + +int wxDPIChangedEvent::ScaleX(int x) const +{ + return wxRescaleCoord(x, m_newDPI.x, m_oldDPI.x); +} + +int wxDPIChangedEvent::ScaleY(int y) const +{ + return wxRescaleCoord(y, m_newDPI.y, m_oldDPI.y); +} + #endif // wxUSE_GUI diff --git a/src/common/gdicmn.cpp b/src/common/gdicmn.cpp index 9dc5784a2c..20442bbc73 100644 --- a/src/common/gdicmn.cpp +++ b/src/common/gdicmn.cpp @@ -16,7 +16,6 @@ #include "wx/display.h" #include "wx/gdiobj.h" -#include "wx/math.h" #ifndef WX_PRECOMP #include "wx/log.h" @@ -33,10 +32,6 @@ #include "wx/math.h" #endif -#ifdef __WINDOWS__ - // Required in order to use wxMulDivInt32(). - #include "wx/msw/wrapwin.h" -#endif wxIMPLEMENT_ABSTRACT_CLASS(wxGDIObject, wxObject); @@ -926,8 +921,3 @@ wxResourceCache::~wxResourceCache () node = node->GetNext (); } } - -int wxRescaleCoord(int n, int newScale, int oldScale) -{ - return n == -1 ? -1 : wxMulDivInt32(n, newScale, oldScale); -} diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index d0180a68a7..3ace3de1f6 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -74,12 +74,9 @@ #include "wx/display.h" #include "wx/platinfo.h" #include "wx/recguard.h" +#include "wx/private/rescale.h" #include "wx/private/window.h" -#ifdef __WINDOWS__ - #include "wx/msw/wrapwin.h" -#endif - // Windows List WXDLLIMPEXP_DATA_CORE(wxWindowList) wxTopLevelWindows; diff --git a/src/msw/button.cpp b/src/msw/button.cpp index 7a1a21d913..6bca3e18e2 100644 --- a/src/msw/button.cpp +++ b/src/msw/button.cpp @@ -42,6 +42,7 @@ #include "wx/stockitem.h" #include "wx/msw/private/button.h" #include "wx/msw/private/dc.h" +#include "wx/private/rescale.h" #include "wx/private/window.h" #if wxUSE_MARKUP