From 24e20d80e3df1476e895fe7c8c3b59395462e22f Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Tue, 19 Apr 2022 18:47:39 +0200 Subject: [PATCH 01/29] CMake: Enable building with Cairo renderer on MSW without GTK --- build/cmake/init.cmake | 11 ++++ build/cmake/lib/core/CMakeLists.txt | 4 ++ build/cmake/modules/FindCairo.cmake | 81 +++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 build/cmake/modules/FindCairo.cmake diff --git a/build/cmake/init.cmake b/build/cmake/init.cmake index d376b4c85e..12644fb883 100644 --- a/build/cmake/init.cmake +++ b/build/cmake/init.cmake @@ -574,6 +574,17 @@ if(wxUSE_GUI) wx_option_force_value(wxUSE_SPELLCHECK OFF) endif() endif() + + if(wxUSE_CAIRO AND NOT WXGTK) + find_package(Cairo) + if(NOT CAIRO_FOUND) + message(WARNING "Cairo not found, Cairo renderer won't be available") + wx_option_force_value(wxUSE_CAIRO OFF) + else() + #TODO only if build-in png? + list(FILTER CAIRO_INCLUDE_DIRS EXCLUDE REGEX ".*libpng.*") + endif() + endif() endif() # test if precompiled headers are supported using the cotire test project diff --git a/build/cmake/lib/core/CMakeLists.txt b/build/cmake/lib/core/CMakeLists.txt index acdf31a1b3..5080f20cf6 100644 --- a/build/cmake/lib/core/CMakeLists.txt +++ b/build/cmake/lib/core/CMakeLists.txt @@ -103,3 +103,7 @@ if(wxUSE_LIBNOTIFY) wx_lib_include_directories(wxcore ${LIBNOTIFY_INCLUDE_DIRS}) wx_lib_link_libraries(wxcore PUBLIC ${LIBNOTIFY_LIBRARIES}) endif() +if(wxUSE_CAIRO AND NOT WXGTK) + wx_lib_include_directories(wxcore ${CAIRO_INCLUDE_DIRS}) + # no libs, cairo is loaded dynamically +endif() diff --git a/build/cmake/modules/FindCairo.cmake b/build/cmake/modules/FindCairo.cmake new file mode 100644 index 0000000000..1ee147bd8d --- /dev/null +++ b/build/cmake/modules/FindCairo.cmake @@ -0,0 +1,81 @@ +# - Try to find Cairo +# Once done, this will define +# +# CAIRO_FOUND - system has Cairo +# CAIRO_INCLUDE_DIRS - the Cairo include directories +# CAIRO_LIBRARIES - link these to use Cairo +# +# Copyright (C) 2012 Raphael Kubo da Costa +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND ITS CONTRIBUTORS ``AS +# IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ITS +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +FIND_PACKAGE(PkgConfig) +PKG_CHECK_MODULES(PC_CAIRO QUIET cairo) + +FIND_PATH(CAIRO_INCLUDE_DIRS + NAMES cairo.h + HINTS ${PC_CAIRO_INCLUDEDIR} + ${PC_CAIRO_INCLUDE_DIRS} + PATH_SUFFIXES cairo +) + +FIND_LIBRARY(CAIRO_LIBRARIES + NAMES cairo + HINTS ${PC_CAIRO_LIBDIR} + ${PC_CAIRO_LIBRARY_DIRS} +) + +IF (CAIRO_INCLUDE_DIRS) + IF (EXISTS "${CAIRO_INCLUDE_DIRS}/cairo-version.h") + FILE(READ "${CAIRO_INCLUDE_DIRS}/cairo-version.h" CAIRO_VERSION_CONTENT) + + STRING(REGEX MATCH "#define +CAIRO_VERSION_MAJOR +([0-9]+)" _dummy "${CAIRO_VERSION_CONTENT}") + SET(CAIRO_VERSION_MAJOR "${CMAKE_MATCH_1}") + + STRING(REGEX MATCH "#define +CAIRO_VERSION_MINOR +([0-9]+)" _dummy "${CAIRO_VERSION_CONTENT}") + SET(CAIRO_VERSION_MINOR "${CMAKE_MATCH_1}") + + STRING(REGEX MATCH "#define +CAIRO_VERSION_MICRO +([0-9]+)" _dummy "${CAIRO_VERSION_CONTENT}") + SET(CAIRO_VERSION_MICRO "${CMAKE_MATCH_1}") + + SET(CAIRO_VERSION "${CAIRO_VERSION_MAJOR}.${CAIRO_VERSION_MINOR}.${CAIRO_VERSION_MICRO}") + ENDIF () +ENDIF () + +# FIXME: Should not be needed anymore once we start depending on CMake 2.8.3 +SET(VERSION_OK TRUE) +IF (Cairo_FIND_VERSION) + IF (Cairo_FIND_VERSION_EXACT) + IF ("${Cairo_FIND_VERSION}" VERSION_EQUAL "${CAIRO_VERSION}") + # FIXME: Use IF (NOT ...) with CMake 2.8.2+ to get rid of the ELSE block + ELSE () + SET(VERSION_OK FALSE) + ENDIF () + ELSE () + IF ("${Cairo_FIND_VERSION}" VERSION_GREATER "${CAIRO_VERSION}") + SET(VERSION_OK FALSE) + ENDIF () + ENDIF () +ENDIF () + +INCLUDE(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(Cairo DEFAULT_MSG CAIRO_INCLUDE_DIRS CAIRO_LIBRARIES VERSION_OK) From a2ddfcf80b62fb8ec841648e689960c52970a917 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Tue, 19 Apr 2022 18:52:06 +0200 Subject: [PATCH 02/29] CMake: Fix warning when copying inplace config Quote all parameters to ensure paths are handled properly. --- build/cmake/config.cmake | 4 ++-- build/cmake/lib/webview/CMakeLists.txt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/build/cmake/config.cmake b/build/cmake/config.cmake index 52ae69d3f6..b359560bc0 100644 --- a/build/cmake/config.cmake +++ b/build/cmake/config.cmake @@ -75,8 +75,8 @@ function(wx_write_config_inplace) endif() execute_process( COMMAND - ${CMAKE_COMMAND} -E ${COPY_CMD} - "lib/wx/config/inplace-${TOOLCHAIN_FULLNAME}" + "${CMAKE_COMMAND}" -E ${COPY_CMD} + "${CMAKE_CURRENT_BINARY_DIR}/lib/wx/config/inplace-${TOOLCHAIN_FULLNAME}" "${CMAKE_CURRENT_BINARY_DIR}/wx-config" ) endfunction() diff --git a/build/cmake/lib/webview/CMakeLists.txt b/build/cmake/lib/webview/CMakeLists.txt index 0408ee7741..d42f6649d2 100644 --- a/build/cmake/lib/webview/CMakeLists.txt +++ b/build/cmake/lib/webview/CMakeLists.txt @@ -71,8 +71,8 @@ elseif(WXMSW) EXPECTED_HASH SHA256=${WEBVIEW2_SHA256}) file(MAKE_DIRECTORY ${WEBVIEW2_PACKAGE_DIR}) execute_process(COMMAND - ${CMAKE_COMMAND} -E tar x ${CMAKE_BINARY_DIR}/webview2.nuget - WORKING_DIRECTORY ${WEBVIEW2_PACKAGE_DIR} + "${CMAKE_COMMAND}" -E tar x "${CMAKE_BINARY_DIR}/webview2.nuget" + WORKING_DIRECTORY "${WEBVIEW2_PACKAGE_DIR}" ) endif() set(WEBVIEW2_PACKAGE_DIR ${WEBVIEW2_PACKAGE_DIR} CACHE INTERNAL "" FORCE) From afe2d55b3ed28cb2b5895d86433cc64a556cd12e Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 00:43:06 +0200 Subject: [PATCH 03/29] Recreate DC buffer when content scale factor changes --- src/common/dcbufcmn.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/common/dcbufcmn.cpp b/src/common/dcbufcmn.cpp index 0dbfcf48de..9b1c1f3159 100644 --- a/src/common/dcbufcmn.cpp +++ b/src/common/dcbufcmn.cpp @@ -52,7 +52,8 @@ public: if ( !ms_buffer || w > ms_buffer->GetLogicalWidth() || - h > ms_buffer->GetLogicalHeight() ) + h > ms_buffer->GetLogicalHeight() || + (dc && dc->GetContentScaleFactor() != ms_buffer->GetScaleFactor()) ) { delete ms_buffer; From acc32082e83d3f1a2ed5e40c44ac4245afd09a19 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 00:44:31 +0200 Subject: [PATCH 04/29] Improve DPI support in wxMSW wxCairoRenderer Drawing sample looks correct on Windows and Linux GTK3 at 200% scaling, and correct DPI is returned. --- src/generic/graphicc.cpp | 60 ++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index 84c6d84ad5..b7e96fb471 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -40,6 +40,7 @@ bool wxCairoInit(); #include "wx/private/graphics.h" #include "wx/rawbmp.h" #include "wx/vector.h" +#include "wx/display.h" #ifdef __WXMSW__ #include "wx/msw/enhmeta.h" #endif @@ -332,7 +333,10 @@ protected: class wxCairoFontData : public wxGraphicsObjectRefData { public: - wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col ); + wxCairoFontData( wxGraphicsRenderer* renderer, + const wxFont &font, + const wxRealPoint& dpi, + const wxColour& col ); wxCairoFontData(wxGraphicsRenderer* renderer, double sizeInPixels, const wxString& facename, @@ -508,6 +512,8 @@ public: virtual void PopState() wxOVERRIDE; virtual void Flush() wxOVERRIDE; + void GetDPI(wxDouble* dpiX, wxDouble* dpiY) const wxOVERRIDE; + virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height, wxDouble *descent, wxDouble *externalLeading ) const wxOVERRIDE; virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const wxOVERRIDE; @@ -1124,7 +1130,7 @@ wxCairoFontData::InitFontComponents(const wxString& facename, } wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, - const wxColour& col ) + const wxRealPoint& dpi, const wxColour& col ) : wxGraphicsObjectRefData(renderer) #ifdef __WXGTK__ , m_wxfont(font) @@ -1132,7 +1138,14 @@ wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &fo { InitColour(col); +#ifndef __WXMAC__ + m_size = !dpi.y + ? double(font.GetPixelSize().GetHeight()) + : double(font.GetFractionalPointSize() * dpi.y / 72); +#else + wxUnusedVar(dpi); m_size = font.GetPointSize(); +#endif #ifdef __WXMAC__ m_font = cairo_quartz_font_face_create_for_cgfont( font.OSXGetCGFont() ); @@ -2749,6 +2762,31 @@ void wxCairoContext::Flush() #endif } +void wxCairoContext::GetDPI(wxDouble* dpiX, wxDouble* dpiY) const +{ + if ( GetWindow() ) + { + const wxSize dpi = GetWindow()->GetDPI(); + + if ( dpiX ) + *dpiX = dpi.x; + if ( dpiY ) + *dpiY = dpi.y; + } + else + { + double dpi = (double)wxDisplay::GetStdPPIValue(); +#ifndef __WXMAC__ + dpi *= GetContentScaleFactor(); +#endif + + if ( dpiX ) + *dpiX = dpi; + if ( dpiY ) + *dpiY = dpi; + } +} + void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) { wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp); @@ -3377,13 +3415,7 @@ wxCairoRenderer::CreateRadialGradientBrush(wxDouble startX, wxDouble startY, wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col ) { - wxGraphicsFont p; - ENSURE_LOADED_OR_RETURN(p); - if ( font.IsOk() ) - { - p.SetRefData(new wxCairoFontData( this , font, col )); - } - return p; + return CreateFontAtDPI(font, wxRealPoint(), col); } wxGraphicsFont @@ -3400,10 +3432,16 @@ wxCairoRenderer::CreateFont(double sizeInPixels, wxGraphicsFont wxCairoRenderer::CreateFontAtDPI(const wxFont& font, - const wxRealPoint& WXUNUSED(dpi), + const wxRealPoint& dpi, const wxColour& col) { - return CreateFont(font, col); + wxGraphicsFont p; + ENSURE_LOADED_OR_RETURN(p); + if ( font.IsOk() ) + { + p.SetRefData(new wxCairoFontData( this, font, dpi, col )); + } + return p; } wxGraphicsBitmap wxCairoRenderer::CreateBitmap( const wxBitmap& bmp ) From fda41cdd1bbb13f4773e7c136ce97b772d295c15 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 00:49:49 +0200 Subject: [PATCH 05/29] Remove GetDPIScaleFactor from wxDC and wxGraphicsContext This reverts most of the changes from ee2b02614e (Add GetDPIScaleFactor to wxDC and wxGraphicsContext, 2022-04-16). This is not supposed to be used to scale pixels, FromDIP will be added instead. Temporary use scale=1 in the drawing sample until FromDIP is added. --- include/wx/dc.h | 5 ----- include/wx/dcgraph.h | 1 - include/wx/graphics.h | 1 - include/wx/msw/dc.h | 1 - include/wx/msw/dcmemory.h | 1 - samples/drawing/drawing.cpp | 2 +- src/common/dcgraph.cpp | 5 ----- src/common/graphcmn.cpp | 7 ------- src/msw/dc.cpp | 5 ----- src/msw/dcmemory.cpp | 5 ----- 10 files changed, 1 insertion(+), 32 deletions(-) diff --git a/include/wx/dc.h b/include/wx/dc.h index d6c2881ef2..8a3e796114 100644 --- a/include/wx/dc.h +++ b/include/wx/dc.h @@ -539,8 +539,6 @@ public: virtual double GetContentScaleFactor() const { return m_contentScaleFactor; } - virtual double GetDPIScaleFactor() const { return 1.0; } - #ifdef __WXMSW__ // Native Windows functions using the underlying HDC don't honour GDI+ // transformations which may be applied to it. Using this function we can @@ -829,9 +827,6 @@ public: double GetContentScaleFactor() const { return m_pimpl->GetContentScaleFactor(); } - double GetDPIScaleFactor() const - { return m_pimpl->GetDPIScaleFactor(); } - // Right-To-Left (RTL) modes void SetLayoutDirection(wxLayoutDirection dir) diff --git a/include/wx/dcgraph.h b/include/wx/dcgraph.h index a57e8168e1..29671a841b 100644 --- a/include/wx/dcgraph.h +++ b/include/wx/dcgraph.h @@ -101,7 +101,6 @@ public: virtual bool CanGetTextExtent() const wxOVERRIDE; virtual int GetDepth() const wxOVERRIDE; virtual wxSize GetPPI() const wxOVERRIDE; - virtual double GetDPIScaleFactor() const wxOVERRIDE; virtual void SetLogicalFunction(wxRasterOperationMode function) wxOVERRIDE; diff --git a/include/wx/graphics.h b/include/wx/graphics.h index 849e5353e9..5defa492d9 100644 --- a/include/wx/graphics.h +++ b/include/wx/graphics.h @@ -880,7 +880,6 @@ public: void SetContentScaleFactor(double contentScaleFactor); double GetContentScaleFactor() const { return m_contentScaleFactor; } - double GetDPIScaleFactor() const; #ifdef __WXMSW__ virtual WXHDC GetNativeHDC() = 0; diff --git a/include/wx/msw/dc.h b/include/wx/msw/dc.h index ae04c80566..6af3637c97 100644 --- a/include/wx/msw/dc.h +++ b/include/wx/msw/dc.h @@ -77,7 +77,6 @@ public: virtual bool CanGetTextExtent() const wxOVERRIDE; virtual int GetDepth() const wxOVERRIDE; virtual wxSize GetPPI() const wxOVERRIDE; - virtual double GetDPIScaleFactor() const wxOVERRIDE; virtual void SetMapMode(wxMappingMode mode) wxOVERRIDE; virtual void SetUserScale(double x, double y) wxOVERRIDE; diff --git a/include/wx/msw/dcmemory.h b/include/wx/msw/dcmemory.h index 88547f314d..7449e27885 100644 --- a/include/wx/msw/dcmemory.h +++ b/include/wx/msw/dcmemory.h @@ -27,7 +27,6 @@ public: virtual void DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) wxOVERRIDE; virtual void DoGetSize(int* width, int* height) const wxOVERRIDE; virtual void DoSelect(const wxBitmap& bitmap) wxOVERRIDE; - virtual double GetDPIScaleFactor() const wxOVERRIDE; virtual wxBitmap DoGetAsBitmap(const wxRect* subrect) const wxOVERRIDE { return subrect == NULL ? GetSelectedBitmap() : GetSelectedBitmap().GetSubBitmapOfHDC(*subrect, GetHDC() );} diff --git a/samples/drawing/drawing.cpp b/samples/drawing/drawing.cpp index 7329efa2ff..de0e63f8e1 100644 --- a/samples/drawing/drawing.cpp +++ b/samples/drawing/drawing.cpp @@ -574,7 +574,7 @@ void MyCanvas::DrawTestPoly(wxDC& dc) wxBrush brushHatch(*wxRED, wxBRUSHSTYLE_FDIAGONAL_HATCH); dc.SetBrush(brushHatch); - double scale = dc.GetDPIScaleFactor(); + double scale = 1.0; wxPoint star[5]; star[0] = scale * wxPoint(100, 60); diff --git a/src/common/dcgraph.cpp b/src/common/dcgraph.cpp index 368ddfed83..13eb330aa1 100644 --- a/src/common/dcgraph.cpp +++ b/src/common/dcgraph.cpp @@ -490,11 +490,6 @@ wxSize wxGCDCImpl::GetPPI() const return wxDisplay::GetStdPPI(); } -double wxGCDCImpl::GetDPIScaleFactor() const -{ - return m_graphicContext ? m_graphicContext->GetDPIScaleFactor() : 1.0; -} - int wxGCDCImpl::GetDepth() const { return 32; diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index 59803b8789..1bbb3e0a65 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -613,13 +613,6 @@ void wxGraphicsContext::SetContentScaleFactor(double contentScaleFactor) m_contentScaleFactor = contentScaleFactor; } - double wxGraphicsContext::GetDPIScaleFactor() const -{ - wxDouble x, y; - GetDPI(&x, &y); - return x / (double)wxDisplay::GetStdPPIValue(); -} - #if 0 void wxGraphicsContext::SetAlpha( wxDouble WXUNUSED(alpha) ) { diff --git a/src/msw/dc.cpp b/src/msw/dc.cpp index 33cc1cf537..6dbca59f88 100644 --- a/src/msw/dc.cpp +++ b/src/msw/dc.cpp @@ -2661,11 +2661,6 @@ wxSize wxMSWDCImpl::GetPPI() const return ppi; } -double wxMSWDCImpl::GetDPIScaleFactor() const -{ - return GetPPI().x / 96.0; -} - // ---------------------------------------------------------------------------- // DC caching // ---------------------------------------------------------------------------- diff --git a/src/msw/dcmemory.cpp b/src/msw/dcmemory.cpp index 83799ff4ed..7218fa34eb 100644 --- a/src/msw/dcmemory.cpp +++ b/src/msw/dcmemory.cpp @@ -143,11 +143,6 @@ void wxMemoryDCImpl::DoSelect( const wxBitmap& bitmap ) SetFont(GetFont()); } -double wxMemoryDCImpl::GetDPIScaleFactor() const -{ - return m_contentScaleFactor; -} - void wxMemoryDCImpl::SetFont(const wxFont& font) { // We need to adjust the font size by the ratio between the scale factor we From 15a37b91fb3673b830d321572d11284695adcf1f Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 00:50:39 +0200 Subject: [PATCH 06/29] Add FromDIP to wxDC and wxGraphicsContext Using the same implementation as wxWindow has. --- include/wx/dc.h | 12 ++++++++++++ include/wx/graphics.h | 11 +++++++++++ src/common/dcbase.cpp | 13 +++++++++++++ src/common/graphcmn.cpp | 13 +++++++++++++ 4 files changed, 49 insertions(+) diff --git a/include/wx/dc.h b/include/wx/dc.h index 8a3e796114..c1f978f2c4 100644 --- a/include/wx/dc.h +++ b/include/wx/dc.h @@ -539,6 +539,8 @@ public: virtual double GetContentScaleFactor() const { return m_contentScaleFactor; } + virtual wxSize FromDIP(const wxSize& sz) const; + #ifdef __WXMSW__ // Native Windows functions using the underlying HDC don't honour GDI+ // transformations which may be applied to it. Using this function we can @@ -827,6 +829,16 @@ public: double GetContentScaleFactor() const { return m_pimpl->GetContentScaleFactor(); } + wxSize FromDIP(const wxSize& sz) const + { return m_pimpl->FromDIP(sz); } + wxPoint FromDIP(const wxPoint& pt) const + { + const wxSize sz = FromDIP(wxSize(pt.x, pt.y)); + return wxPoint(sz.x, sz.y); + } + int FromDIP(int d) const + { return FromDIP(wxSize(d, 0)).x; } + // Right-To-Left (RTL) modes void SetLayoutDirection(wxLayoutDirection dir) diff --git a/include/wx/graphics.h b/include/wx/graphics.h index 5defa492d9..935f06ac53 100644 --- a/include/wx/graphics.h +++ b/include/wx/graphics.h @@ -739,6 +739,17 @@ public: // returns the resolution of the graphics context in device points per inch virtual void GetDPI( wxDouble* dpiX, wxDouble* dpiY) const; + wxSize FromDIP(const wxSize& sz) const; + wxPoint FromDIP(const wxPoint& pt) const + { + const wxSize sz = FromDIP(wxSize(pt.x, pt.y)); + return wxPoint(sz.x, sz.y); + } + int FromDIP(int d) const + { + return FromDIP(wxSize(d, 0)).x; + } + #if 0 // sets the current alpha on this context virtual void SetAlpha( wxDouble alpha ); diff --git a/src/common/dcbase.cpp b/src/common/dcbase.cpp index 82be5c669d..f718a1163a 100644 --- a/src/common/dcbase.cpp +++ b/src/common/dcbase.cpp @@ -36,6 +36,8 @@ #endif #include "wx/private/textmeasure.h" +#include "wx/private/rescale.h" +#include "wx/display.h" #ifdef __WXMSW__ #include "wx/msw/dcclient.h" @@ -604,6 +606,17 @@ void wxDCImpl::SetAxisOrientation( bool xLeftRight, bool yBottomUp ) ComputeScaleAndOrigin(); } +wxSize wxDCImpl::FromDIP(const wxSize& sz) const +{ +#ifdef wxHAS_DPI_INDEPENDENT_PIXELS + return sz; +#else + const wxSize dpi = GetPPI(); + const wxSize baseline = wxDisplay::GetStdPPI(); + return wxRescaleCoord(sz).From(baseline).To(dpi); +#endif // wxHAS_DPI_INDEPENDENT_PIXELS +} + bool wxDCImpl::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const { wxTextMeasure tm(GetOwner(), &m_font); diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index 1bbb3e0a65..179e0f2fc6 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -34,6 +34,7 @@ #endif #include "wx/private/graphics.h" +#include "wx/private/rescale.h" #include "wx/display.h" //----------------------------------------------------------------------------- @@ -641,6 +642,18 @@ void wxGraphicsContext::GetDPI( wxDouble* dpiX, wxDouble* dpiY) const } } +wxSize wxGraphicsContext::FromDIP(const wxSize& sz) const +{ +#ifdef wxHAS_DPI_INDEPENDENT_PIXELS + return sz; +#else + wxRealPoint dpi; + GetDPI(&dpi.x, &dpi.y); + const wxSize baseline = wxDisplay::GetStdPPI(); + return wxRescaleCoord(sz).From(baseline).To(wxSize((int)dpi.x, (int)dpi.y)); +#endif // wxHAS_DPI_INDEPENDENT_PIXELS +} + // sets the pen void wxGraphicsContext::SetPen( const wxGraphicsPen& pen ) { From 3587b3469f9e5695e6c6694aa3c72f2ac15b2286 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 00:58:09 +0200 Subject: [PATCH 07/29] Make the polygon page in the drawing sample DPI aware Remove unneeded wxGCDC constructor argument. Make showing a clipping region, textured background, and changing origin DPI aware. --- samples/drawing/drawing.cpp | 55 +++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/samples/drawing/drawing.cpp b/samples/drawing/drawing.cpp index de0e63f8e1..d9836109c0 100644 --- a/samples/drawing/drawing.cpp +++ b/samples/drawing/drawing.cpp @@ -574,40 +574,38 @@ void MyCanvas::DrawTestPoly(wxDC& dc) wxBrush brushHatch(*wxRED, wxBRUSHSTYLE_FDIAGONAL_HATCH); dc.SetBrush(brushHatch); - double scale = 1.0; - wxPoint star[5]; - star[0] = scale * wxPoint(100, 60); - star[1] = scale * wxPoint(60, 150); - star[2] = scale * wxPoint(160, 100); - star[3] = scale * wxPoint(40, 100); - star[4] = scale * wxPoint(140, 150); + star[0] = dc.FromDIP(wxPoint(100, 60)); + star[1] = dc.FromDIP(wxPoint(60, 150)); + star[2] = dc.FromDIP(wxPoint(160, 100)); + star[3] = dc.FromDIP(wxPoint(40, 100)); + star[4] = dc.FromDIP(wxPoint(140, 150)); dc.DrawText("You should see two (irregular) stars below, the left one " - "hatched", scale * 10, scale * 10); + "hatched", dc.FromDIP(10), dc.FromDIP(10)); dc.DrawText("except for the central region and the right " - "one entirely hatched", scale * 10, scale * 30); - dc.DrawText("The third star only has a hatched outline", scale * 10, scale* 50); + "one entirely hatched", dc.FromDIP(10), dc.FromDIP(30)); + dc.DrawText("The third star only has a hatched outline", dc.FromDIP(10), dc.FromDIP(50)); - dc.DrawPolygon(WXSIZEOF(star), star, scale * 0, scale * 30); - dc.DrawPolygon(WXSIZEOF(star), star, scale * 160, scale * 30, wxWINDING_RULE); + dc.DrawPolygon(WXSIZEOF(star), star, dc.FromDIP(0), dc.FromDIP(30)); + dc.DrawPolygon(WXSIZEOF(star), star, dc.FromDIP(160), dc.FromDIP(30), wxWINDING_RULE); wxBrush brushHatchGreen(*wxGREEN, wxBRUSHSTYLE_FDIAGONAL_HATCH); dc.SetBrush(brushHatchGreen); wxPoint star2[10]; - star2[0] = scale * wxPoint(0, 100); - star2[1] = scale * wxPoint(-59, -81); - star2[2] = scale * wxPoint(95, 31); - star2[3] = scale * wxPoint(-95, 31); - star2[4] = scale * wxPoint(59, -81); - star2[5] = scale * wxPoint(0, 80); - star2[6] = scale * wxPoint(-47, -64); - star2[7] = scale * wxPoint(76, 24); - star2[8] = scale * wxPoint(-76, 24); - star2[9] = scale * wxPoint(47, -64); + star2[0] = dc.FromDIP(wxPoint(0, 100)); + star2[1] = dc.FromDIP(wxPoint(-59, -81)); + star2[2] = dc.FromDIP(wxPoint(95, 31)); + star2[3] = dc.FromDIP(wxPoint(-95, 31)); + star2[4] = dc.FromDIP(wxPoint(59, -81)); + star2[5] = dc.FromDIP(wxPoint(0, 80)); + star2[6] = dc.FromDIP(wxPoint(-47, -64)); + star2[7] = dc.FromDIP(wxPoint(76, 24)); + star2[8] = dc.FromDIP(wxPoint(-76, 24)); + star2[9] = dc.FromDIP(wxPoint(47, -64)); int count[2] = {5, 5}; - dc.DrawPolyPolygon(WXSIZEOF(count), count, star2, scale * 450, scale * 150); + dc.DrawPolyPolygon(WXSIZEOF(count), count, star2, dc.FromDIP(450), dc.FromDIP(150)); } void MyCanvas::DrawTestLines( int x, int y, int width, wxDC &dc ) @@ -1836,7 +1834,7 @@ void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event)) void MyCanvas::Draw(wxDC& pdc) { #if wxUSE_GRAPHICS_CONTEXT - wxGCDC gdc(this); + wxGCDC gdc; if ( m_renderer ) { @@ -1896,7 +1894,10 @@ void MyCanvas::Draw(wxDC& pdc) } if ( m_clip ) - dc.SetClippingRegion(100, 100, 100, 100); + { + int clipSize = dc.FromDIP(100); + dc.SetClippingRegion(clipSize, clipSize, clipSize, clipSize); + } dc.Clear(); @@ -1904,7 +1905,7 @@ void MyCanvas::Draw(wxDC& pdc) { dc.SetPen(*wxMEDIUM_GREY_PEN); for ( int i = 0; i < 200; i++ ) - dc.DrawLine(0, i*10, i*10, 0); + dc.DrawLine(0, dc.FromDIP(i*10), dc.FromDIP(i*10), 0); } switch ( m_show ) @@ -2726,7 +2727,7 @@ void MyFrame::PrepareDC(wxDC& dc) dc.SetTransformMatrix(mtx); } #endif // wxUSE_DC_TRANSFORM_MATRIX - dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin ); + dc.SetLogicalOrigin( dc.FromDIP(m_xLogicalOrigin), dc.FromDIP(m_yLogicalOrigin) ); dc.SetAxisOrientation( !m_xAxisReversed, m_yAxisReversed ); dc.SetUserScale( m_xUserScale, m_yUserScale ); dc.SetMapMode( m_mapMode ); From 93231968c77ee681ac39b8628022f691e8266495 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 00:58:52 +0200 Subject: [PATCH 08/29] Remove trailing whitespace in macOS graphics code --- src/osx/carbon/graphics.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/osx/carbon/graphics.cpp b/src/osx/carbon/graphics.cpp index cd19f3c928..d308c32492 100644 --- a/src/osx/carbon/graphics.cpp +++ b/src/osx/carbon/graphics.cpp @@ -501,7 +501,7 @@ protected: bool m_isShading; CGFunctionRef m_gradientFunction; CGShadingRef m_shading; - wxMacCoreGraphicsMatrixData* m_shadingMatrix; + wxMacCoreGraphicsMatrixData* m_shadingMatrix; // information about a single gradient component struct GradientComponent @@ -557,7 +557,7 @@ wxMacCoreGraphicsPenBrushDataBase::~wxMacCoreGraphicsPenBrushDataBase() if ( m_shadingMatrix ) delete m_shadingMatrix; - + // an eventual existing m_gradientComponents will be deallocated via the CGFunction callback } @@ -579,9 +579,9 @@ wxMacCoreGraphicsPenBrushDataBase::CreateLinearGradientShading( const wxGraphicsMatrix& matrix) { m_gradientFunction = CreateGradientFunction(stops); - m_shading = CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), + m_shading = CGShadingCreateAxial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat) x1, (CGFloat) y1), - CGPointMake((CGFloat) x2, (CGFloat) y2), + CGPointMake((CGFloat) x2, (CGFloat) y2), m_gradientFunction, true, true ); m_isShading = true; if ( !matrix.IsNull() ) @@ -600,9 +600,9 @@ wxMacCoreGraphicsPenBrushDataBase::CreateRadialGradientShading( const wxGraphicsMatrix& matrix) { m_gradientFunction = CreateGradientFunction(stops); - m_shading = CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), + m_shading = CGShadingCreateRadial( wxMacGetGenericRGBColorSpace(), CGPointMake((CGFloat) startX, (CGFloat) startY), 0, - CGPointMake((CGFloat) endX, (CGFloat) endY), (CGFloat) radius, + CGPointMake((CGFloat) endX, (CGFloat) endY), (CGFloat) radius, m_gradientFunction, true, true ); m_isShading = true; if ( !matrix.IsNull() ) @@ -666,7 +666,7 @@ CGFunctionRef wxMacCoreGraphicsPenBrushDataBase::CreateGradientFunction(const wxGraphicsGradientStops& stops) { m_gradientComponents = new GradientComponents(); - + static const CGFunctionCallbacks callbacks = { 0, &CalculateShadingValues, &ReleaseComponents }; static const CGFloat input_value_range [2] = { 0, 1 }; static const CGFloat output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 }; @@ -1029,12 +1029,12 @@ protected: wxMacCoreGraphicsColour m_cgColor; }; -wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer) : +wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData( wxGraphicsRenderer* renderer) : wxMacCoreGraphicsPenBrushDataBase( renderer ) { } -wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer* renderer, const wxBrush &brush) : +wxMacCoreGraphicsBrushData::wxMacCoreGraphicsBrushData(wxGraphicsRenderer* renderer, const wxBrush &brush) : wxMacCoreGraphicsPenBrushDataBase( renderer ), m_cgColor( brush ) { @@ -1089,7 +1089,7 @@ private : #endif }; -wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col) +wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col) : wxGraphicsObjectRefData( renderer ), m_colour(col) { @@ -2238,8 +2238,8 @@ void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath &path ) else { CGContextAddPath( m_cgContext, (CGPathRef)path.GetNativePath() ); - CGContextStrokePath( m_cgContext ); - } + CGContextStrokePath( m_cgContext ); + } CheckInvariants(); } From ef28ac392c1a46fe857bdf858c67b6340d324c22 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 01:00:45 +0200 Subject: [PATCH 09/29] Override GetDPI in wxMacCoreGraphicsContext Let it return the DPI of the associated window, or (just like wxWindowMac::GetDPI) return the DPI derived from the content scale factor. --- src/osx/carbon/graphics.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/osx/carbon/graphics.cpp b/src/osx/carbon/graphics.cpp index d308c32492..4a7f083d68 100644 --- a/src/osx/carbon/graphics.cpp +++ b/src/osx/carbon/graphics.cpp @@ -1402,6 +1402,8 @@ public: virtual void Flush() wxOVERRIDE; + void GetDPI(wxDouble* dpiX, wxDouble* dpiY) const wxOVERRIDE; + // push the current state of the context, ie the transformation matrix on a stack virtual void PushState() wxOVERRIDE; @@ -1758,6 +1760,28 @@ void wxMacCoreGraphicsContext::Flush() CGContextFlush(m_cgContext); } +void wxMacCoreGraphicsContext::GetDPI(wxDouble* dpiX, wxDouble* dpiY) const +{ + if ( GetWindow() ) + { + const wxSize dpi = GetWindow()->GetDPI(); + + if ( dpiX ) + *dpiX = dpi.x; + if ( dpiY ) + *dpiY = dpi.y; + } + else + { + // see wxWindowMac::GetDPI + const double dpi = GetContentScaleFactor() * 72.0; + if ( dpiX ) + *dpiX = dpi; + if ( dpiY ) + *dpiY = dpi; + } +} + bool wxMacCoreGraphicsContext::EnsureIsValid() { CheckInvariants(); From cb05935f529cb270f5c0856b2649e4b0c7db830f Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 01:03:15 +0200 Subject: [PATCH 10/29] Check if GetDPI parameters are not NULL And combine the same code for assigning the variables. --- src/common/graphcmn.cpp | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index 179e0f2fc6..1143fe4a7b 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -627,19 +627,12 @@ wxDouble wxGraphicsContext::GetAlpha() const void wxGraphicsContext::GetDPI( wxDouble* dpiX, wxDouble* dpiY) const { - if ( m_window ) - { - const wxSize ppi = m_window->GetDPI(); - *dpiX = ppi.x; - *dpiY = ppi.y; - } - else - { - // Use some standard DPI value, it doesn't make much sense for the - // contexts not using any pixels anyhow. - *dpiX = wxDisplay::GetStdPPIValue(); - *dpiY = wxDisplay::GetStdPPIValue(); - } + const wxSize dpi = GetWindow() ? GetWindow()->GetDPI() : wxDisplay::GetStdPPI(); + + if (dpiX) + *dpiX = dpi.x; + if (dpiY) + *dpiY = dpi.y; } wxSize wxGraphicsContext::FromDIP(const wxSize& sz) const From 3da3103eb5c8b38ef696c542a7adcfbf1b43151b Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 20:23:37 +0200 Subject: [PATCH 11/29] Override FromDIP in wxSVGFileDC and wxPostScriptDC These are DPI independent. --- include/wx/dcsvg.h | 5 +++++ include/wx/generic/dcpsg.h | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/include/wx/dcsvg.h b/include/wx/dcsvg.h index 16cbd185ba..91e04c4449 100644 --- a/include/wx/dcsvg.h +++ b/include/wx/dcsvg.h @@ -248,6 +248,11 @@ private: virtual wxSize GetPPI() const wxOVERRIDE; + virtual wxSize FromDIP(const wxSize& sz) const wxOVERRIDE + { + return sz; + } + void Init(const wxString& filename, int width, int height, double dpi, const wxString& title); diff --git a/include/wx/generic/dcpsg.h b/include/wx/generic/dcpsg.h index 21c615781d..45628064ba 100644 --- a/include/wx/generic/dcpsg.h +++ b/include/wx/generic/dcpsg.h @@ -76,6 +76,11 @@ public: // Resolution in pixels per logical inch wxSize GetPPI() const wxOVERRIDE; + virtual wxSize FromDIP(const wxSize& sz) const wxOVERRIDE + { + return sz; + } + virtual void ComputeScaleAndOrigin() wxOVERRIDE; void SetBackgroundMode(int WXUNUSED(mode)) wxOVERRIDE { } From 1a1d19e93b51c060625fb12a872e2b507e9ef255 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 20:24:31 +0200 Subject: [PATCH 12/29] Improve print preview and printing with high DPI All print related scaling is based on 96DPI, so always let the printing contexts return this, and also adjust the font to this DPI. --- include/wx/msw/dcprint.h | 7 +++++++ src/common/prntbase.cpp | 5 +++-- src/msw/dcprint.cpp | 8 ++++++++ src/msw/enhmeta.cpp | 14 ++++++++++++++ src/msw/graphics.cpp | 6 ++---- src/msw/printwin.cpp | 3 ++- src/osx/core/printmac.cpp | 6 ++---- 7 files changed, 38 insertions(+), 11 deletions(-) diff --git a/include/wx/msw/dcprint.h b/include/wx/msw/dcprint.h index adbfb0c632..0b9a9af7fa 100644 --- a/include/wx/msw/dcprint.h +++ b/include/wx/msw/dcprint.h @@ -36,6 +36,13 @@ public: virtual wxRect GetPaperRect() const wxOVERRIDE; + virtual wxSize FromDIP(const wxSize& sz) const wxOVERRIDE + { + return sz; + } + + void SetFont(const wxFont& font) wxOVERRIDE; + protected: virtual void DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask = false) wxOVERRIDE; diff --git a/src/common/prntbase.cpp b/src/common/prntbase.cpp index 96f4ef857c..3089d00202 100644 --- a/src/common/prntbase.cpp +++ b/src/common/prntbase.cpp @@ -45,6 +45,7 @@ #include "wx/print.h" #include "wx/dcprint.h" #include "wx/artprov.h" +#include "wx/display.h" #include #include @@ -638,7 +639,7 @@ bool wxPrintout::SetUp(wxDC& dc) { wxCHECK_MSG( dc.IsOk(), false, "should have a valid DC to set up" ); - SetPPIScreen(wxGetDisplayPPI()); + SetPPIScreen(wxDisplay::GetStdPPI()); // We need to know printer PPI. In most ports, this can be retrieved from // the printer DC, but in others it is computed (probably for legacy @@ -1613,7 +1614,7 @@ void wxPreviewControlBar::CreateButtons() }; int n = WXSIZEOF(choices); - m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,wxDefaultCoord), n, choices, 0 ); + m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(FromDIP(70),wxDefaultCoord), n, choices, 0 ); sizer.Add(m_zoomControl); SetZoomControl(m_printPreview->GetZoom()); diff --git a/src/msw/dcprint.cpp b/src/msw/dcprint.cpp index 1a8af53d0d..294e7ebe84 100644 --- a/src/msw/dcprint.cpp +++ b/src/msw/dcprint.cpp @@ -41,6 +41,7 @@ #endif #include "wx/printdlg.h" +#include "wx/display.h" #include "wx/msw/printdlg.h" // mingw32 defines GDI_ERROR incorrectly @@ -230,6 +231,13 @@ wxRect wxPrinterDCImpl::GetPaperRect() const return wxRect(x, y, w, h); } +void wxPrinterDCImpl::SetFont(const wxFont& font) +{ + wxFont scaledFont = font; + if ( scaledFont.IsOk() ) + scaledFont.WXAdjustToPPI(wxDisplay::GetStdPPI()); + wxMSWDCImpl::SetFont(scaledFont); +} #if !wxUSE_PS_PRINTING diff --git a/src/msw/enhmeta.cpp b/src/msw/enhmeta.cpp index aed7dbaad5..62e9105e35 100644 --- a/src/msw/enhmeta.cpp +++ b/src/msw/enhmeta.cpp @@ -33,6 +33,7 @@ #include "wx/metafile.h" #include "wx/clipbrd.h" +#include "wx/display.h" #include "wx/msw/private.h" @@ -223,6 +224,19 @@ public: const wxString& description ); virtual ~wxEnhMetaFileDCImpl(); + wxSize FromDIP(const wxSize& sz) const wxOVERRIDE + { + return sz; + } + + void SetFont(const wxFont& font) wxOVERRIDE + { + wxFont scaledFont = font; + if (scaledFont.IsOk()) + scaledFont.WXAdjustToPPI(wxDisplay::GetStdPPI()); + wxMSWDCImpl::SetFont(scaledFont); + } + // obtain a pointer to the new metafile (caller should delete it) wxEnhMetaFile *Close(); diff --git a/src/msw/graphics.cpp b/src/msw/graphics.cpp index c03fca4a1a..0d67bcf7a6 100644 --- a/src/msw/graphics.cpp +++ b/src/msw/graphics.cpp @@ -2544,12 +2544,10 @@ wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer* renderer void wxGDIPlusPrintingContext::GetDPI(wxDouble* dpiX, wxDouble* dpiY) const { - // override to use same scaling as wxWindowsPrintPreview::DetermineScaling - const wxSize dpi = wxGetDPIofHDC(ScreenHDC()); if ( dpiX ) - *dpiX = dpi.x; + *dpiX = 96.0; if ( dpiY ) - *dpiY = dpi.y; + *dpiY = 96.0; } //----------------------------------------------------------------------------- diff --git a/src/msw/printwin.cpp b/src/msw/printwin.cpp index 051b1f93a1..cf644d3a8f 100644 --- a/src/msw/printwin.cpp +++ b/src/msw/printwin.cpp @@ -46,6 +46,7 @@ #include "wx/msw/private.h" #include "wx/msw/dcprint.h" #include "wx/msw/enhmeta.h" +#include "wx/display.h" #include @@ -310,7 +311,7 @@ bool wxWindowsPrintPreview::Print(bool interactive) void wxWindowsPrintPreview::DetermineScaling() { - const wxSize logPPIScreen = wxGetDPIofHDC(ScreenHDC()); + const wxSize logPPIScreen = wxDisplay::GetStdPPI(); m_previewPrintout->SetPPIScreen(logPPIScreen); // Get a device context for the currently selected printer diff --git a/src/osx/core/printmac.cpp b/src/osx/core/printmac.cpp index 235bea2b57..c69ad4d0cc 100644 --- a/src/osx/core/printmac.cpp +++ b/src/osx/core/printmac.cpp @@ -30,6 +30,7 @@ #include "wx/printdlg.h" #include "wx/paper.h" +#include "wx/display.h" #include "wx/osx/printdlg.h" #include @@ -746,10 +747,7 @@ bool wxMacPrintPreview::Print(bool interactive) void wxMacPrintPreview::DetermineScaling() { - int screenWidth , screenHeight ; - wxDisplaySize( &screenWidth , &screenHeight ) ; - - wxSize ppiScreen = wxGetDisplayPPI(); + wxSize ppiScreen = wxDisplay::GetStdPPI(); wxSize ppiPrinter( 72 , 72 ) ; m_previewPrintout->SetPPIScreen( ppiScreen.x , ppiScreen.y ) ; From 5b5275c3881264ffb627c6ba609974d0723f836d Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 20:25:01 +0200 Subject: [PATCH 13/29] Make the printing sample DPI aware --- samples/printing/printing.cpp | 102 +++++++++++++++++----------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/samples/printing/printing.cpp b/samples/printing/printing.cpp index 59a546c0f4..d6c7bfa221 100644 --- a/samples/printing/printing.cpp +++ b/samples/printing/printing.cpp @@ -111,8 +111,8 @@ bool MyApp::OnInit(void) // ---------------------------- MyFrame* frame = new MyFrame((wxFrame *) NULL, "wxWidgets Printing Demo", - wxPoint(0, 0), wxSize(400, 400)); - + wxDefaultPosition, wxDefaultSize); + frame->SetSize(frame->FromDIP(wxSize(400, 400))); frame->Centre(wxBOTH); frame->Show(); @@ -142,79 +142,79 @@ void MyApp::Draw(wxDC&dc) dc.SetPen(*wxBLACK_PEN); dc.SetBrush(*wxLIGHT_GREY_BRUSH); - dc.DrawRectangle(0, 0, 230, 350); - dc.DrawLine(0, 0, 229, 349); - dc.DrawLine(229, 0, 0, 349); + dc.DrawRectangle(dc.FromDIP(0), dc.FromDIP(0), dc.FromDIP(230), dc.FromDIP(350)); + dc.DrawLine(dc.FromDIP(0), dc.FromDIP(0), dc.FromDIP(229), dc.FromDIP(349)); + dc.DrawLine(dc.FromDIP(229), dc.FromDIP(0), dc.FromDIP(0), dc.FromDIP(349)); dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.SetBrush(*wxCYAN_BRUSH); dc.SetPen(*wxRED_PEN); - dc.DrawRoundedRectangle(0, 20, 200, 80, 20); + dc.DrawRoundedRectangle(dc.FromDIP(0), dc.FromDIP(20), dc.FromDIP(200), dc.FromDIP(80), 20); - dc.DrawText( "Rectangle 200 by 80", 40, 40); + dc.DrawText( "Rectangle 200 by 80", dc.FromDIP(40), dc.FromDIP(40)); dc.SetPen( wxPen(*wxBLACK, 0, wxPENSTYLE_DOT_DASH) ); - dc.DrawEllipse(50, 140, 100, 50); + dc.DrawEllipse(dc.FromDIP(50), dc.FromDIP(140), dc.FromDIP(100), dc.FromDIP(50)); dc.SetPen(*wxRED_PEN); - dc.DrawText( "Test message: this is in 10 point text", 10, 180); + dc.DrawText( "Test message: this is in 10 point text", dc.FromDIP(10), dc.FromDIP(180)); - dc.DrawRotatedText( "This\nis\na multi-line\ntext", 170, 100, -m_angle/1.5); + dc.DrawRotatedText( "This\nis\na multi-line\ntext", dc.FromDIP(170), dc.FromDIP(100), -m_angle/1.5); #if wxUSE_UNICODE const char *test = "Hebrew שלום -- Japanese (日本語)"; wxString tmp = wxConvUTF8.cMB2WC( test ); - dc.DrawText( tmp, 10, 200 ); + dc.DrawText( tmp, dc.FromDIP(10), dc.FromDIP(200) ); #endif wxPoint points[5]; - points[0].x = 0; - points[0].y = 0; - points[1].x = 20; - points[1].y = 0; - points[2].x = 20; - points[2].y = 20; - points[3].x = 10; - points[3].y = 20; - points[4].x = 10; - points[4].y = -20; - dc.DrawPolygon( 5, points, 20, 250, wxODDEVEN_RULE ); - dc.DrawPolygon( 5, points, 50, 250, wxWINDING_RULE ); + points[0].x = dc.FromDIP(0); + points[0].y = dc.FromDIP(0); + points[1].x = dc.FromDIP(20); + points[1].y = dc.FromDIP(0); + points[2].x = dc.FromDIP(20); + points[2].y = dc.FromDIP(20); + points[3].x = dc.FromDIP(10); + points[3].y = dc.FromDIP(20); + points[4].x = dc.FromDIP(10); + points[4].y = dc.FromDIP(-20); + dc.DrawPolygon( 5, points, dc.FromDIP(20), dc.FromDIP(250), wxODDEVEN_RULE ); + dc.DrawPolygon( 5, points, dc.FromDIP(50), dc.FromDIP(250), wxWINDING_RULE ); - dc.DrawArc( 20, 330, 40, 300, 20, 300 ); + dc.DrawArc( dc.FromDIP(20), dc.FromDIP(330), dc.FromDIP(40), dc.FromDIP(300), dc.FromDIP(20), dc.FromDIP(300) ); { wxDCBrushChanger changeBrush(dc, *wxTRANSPARENT_BRUSH); - dc.DrawArc( 60, 330, 80, 300, 60, 300 ); + dc.DrawArc( dc.FromDIP(60), dc.FromDIP(330), dc.FromDIP(80), dc.FromDIP(300), dc.FromDIP(60), dc.FromDIP(300) ); } - dc.DrawEllipticArc( 80, 250, 60, 30, 0.0, 270.0 ); + dc.DrawEllipticArc( dc.FromDIP(80), dc.FromDIP(250), dc.FromDIP(60), dc.FromDIP(30), 0.0, 270.0 ); - points[0].x = 150; - points[0].y = 250; - points[1].x = 180; - points[1].y = 250; - points[2].x = 180; - points[2].y = 220; - points[3].x = 200; - points[3].y = 220; + points[0].x = dc.FromDIP(150); + points[0].y = dc.FromDIP(250); + points[1].x = dc.FromDIP(180); + points[1].y = dc.FromDIP(250); + points[2].x = dc.FromDIP(180); + points[2].y = dc.FromDIP(220); + points[3].x = dc.FromDIP(200); + points[3].y = dc.FromDIP(220); dc.DrawSpline( 4, points ); wxString str; int i = 0; str.Printf( "---- Text at angle %d ----", i ); - dc.DrawRotatedText( str, 100, 300, i ); + dc.DrawRotatedText( str, dc.FromDIP(100), dc.FromDIP(300), i ); i = m_angle; str.Printf( "---- Text at angle %d ----", i ); - dc.DrawRotatedText( str, 100, 300, i ); + dc.DrawRotatedText( str, dc.FromDIP(100), dc.FromDIP(300), i ); wxIcon my_icon = wxICON(sample); - dc.DrawIcon( my_icon, 100, 100); + dc.DrawIcon( my_icon, dc.FromDIP(100), dc.FromDIP(100)); if (m_bitmap.IsOk()) - dc.DrawBitmap( m_bitmap, 10, 10 ); + dc.DrawBitmap( m_bitmap, dc.FromDIP(10), dc.FromDIP(10) ); #if wxUSE_GRAPHICS_CONTEXT wxScopedPtr gc(wxGraphicsContext::CreateFromUnknownDC(dc)); @@ -225,26 +225,26 @@ void MyApp::Draw(wxDC&dc) gc->SetPen( *wxRED_PEN ); wxGraphicsPath path = gc->CreatePath(); - path.AddCircle( 50.0, 50.0, 50.0 ); - path.MoveToPoint(0.0, 50.0); - path.AddLineToPoint(100.0, 50.0); - path.MoveToPoint(50.0, 0.0); - path.AddLineToPoint(50.0, 100.0 ); + path.AddCircle( gc->FromDIP(50.0), gc->FromDIP(50.0), gc->FromDIP(50.0) ); + path.MoveToPoint(gc->FromDIP(0.0), gc->FromDIP(50.0)); + path.AddLineToPoint(gc->FromDIP(100.0), gc->FromDIP(50.0)); + path.MoveToPoint(gc->FromDIP(50.0), gc->FromDIP(0.0)); + path.AddLineToPoint(gc->FromDIP(50.0), gc->FromDIP(100.0) ); path.CloseSubpath(); - path.AddRectangle(25.0, 25.0, 50.0, 50.0); + path.AddRectangle(gc->FromDIP(25.0), gc->FromDIP(25.0), gc->FromDIP(50.0), gc->FromDIP(50.0)); gc->StrokePath(path); // draw some text wxString text("Text by wxGraphicsContext"); gc->SetFont( m_testFont, *wxBLACK ); - gc->DrawText(text, 25.0, 60.0); + gc->DrawText(text, gc->FromDIP(25.0), gc->FromDIP(60.0)); // draw rectangle around the text double w, h; gc->GetTextExtent(text, &w, &h); gc->SetPen( *wxBLACK_PEN ); - gc->DrawRectangle(25.0, 60.0, w, h); + gc->DrawRectangle(gc->FromDIP(25.0), gc->FromDIP(60.0), w, h); } #endif @@ -343,7 +343,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString&title, const wxPoint&pos, const // create the canvas // ----------------- - m_canvas = new MyCanvas(this, wxPoint(0, 0), wxSize(100, 100), + m_canvas = new MyCanvas(this, wxPoint(0, 0), FromDIP(wxSize(100, 100)), wxRETAINED|wxHSCROLL|wxVSCROLL); // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction @@ -392,7 +392,7 @@ void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event)) } wxPreviewFrame *frame = - new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650)); + new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), FromDIP(wxSize(600, 650))); frame->Centre(wxBOTH); frame->InitializeWithModality(m_previewModality); frame->Show(); @@ -427,7 +427,7 @@ void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event)) wxPrintDialogData printDialogData(* g_printData); wxPrintPreview *preview = new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData); wxPreviewFrame *frame = - new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650)); + new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), FromDIP(wxSize(600, 650))); frame->Centre(wxBOTH); frame->Initialize(); frame->Show(); @@ -560,8 +560,8 @@ void MyPrintout::DrawPageOne() // We know the graphic is 230x350. If we didn't know this, we'd need to // calculate it. - wxCoord maxX = 230; - wxCoord maxY = 350; + wxCoord maxX = GetDC()->FromDIP(230); + wxCoord maxY = GetDC()->FromDIP(350); // This sets the user scale and origin of the DC so that the image fits // within the paper rectangle (but the edges could be cut off by printers From 59443803b47a130573bf655a4eed39c482e16613 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 21:45:30 +0200 Subject: [PATCH 14/29] Only scale MSW wxMemoryDC without window with content scale factor --- include/wx/msw/dcmemory.h | 1 + src/msw/dc.cpp | 2 +- src/msw/dcmemory.cpp | 10 +++++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/wx/msw/dcmemory.h b/include/wx/msw/dcmemory.h index 7449e27885..2fe0fadbce 100644 --- a/include/wx/msw/dcmemory.h +++ b/include/wx/msw/dcmemory.h @@ -22,6 +22,7 @@ public: wxMemoryDCImpl( wxMemoryDC *owner, wxDC *dc ); // Create compatible DC // override some base class virtuals + virtual wxSize GetPPI() const wxOVERRIDE; virtual void SetFont(const wxFont& font) wxOVERRIDE; virtual void DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) wxOVERRIDE; diff --git a/src/msw/dc.cpp b/src/msw/dc.cpp index 6dbca59f88..6b72fe8fe4 100644 --- a/src/msw/dc.cpp +++ b/src/msw/dc.cpp @@ -2655,7 +2655,7 @@ wxSize wxMSWDCImpl::GetPPI() const if ( !ppi.x || !ppi.y ) { - ppi = wxGetDPIofHDC(GetHdc())*GetContentScaleFactor(); + ppi = wxGetDPIofHDC(GetHdc()); } return ppi; diff --git a/src/msw/dcmemory.cpp b/src/msw/dcmemory.cpp index 7218fa34eb..5f6f282655 100644 --- a/src/msw/dcmemory.cpp +++ b/src/msw/dcmemory.cpp @@ -143,13 +143,21 @@ void wxMemoryDCImpl::DoSelect( const wxBitmap& bitmap ) SetFont(GetFont()); } +wxSize wxMemoryDCImpl::GetPPI() const +{ + if ( GetWindow() ) + return GetWindow()->GetDPI(); + else + return wxDisplay::GetStdPPI() * GetContentScaleFactor(); +} + void wxMemoryDCImpl::SetFont(const wxFont& font) { // We need to adjust the font size by the ratio between the scale factor we // use and the default/global scale factor used when creating fonts. wxFont scaledFont = font; if ( scaledFont.IsOk() ) - scaledFont.WXAdjustToPPI(wxDisplay::GetStdPPI()*m_contentScaleFactor); + scaledFont.WXAdjustToPPI(GetPPI()); wxMSWDCImpl::SetFont(scaledFont); } From 7cd4e343a2db73801babd9437b70d664bea60c6f Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 21:58:20 +0200 Subject: [PATCH 15/29] Let wxGCDC inherit wxWindow from wxGraphicsContext --- src/common/dcgraph.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/common/dcgraph.cpp b/src/common/dcgraph.cpp index 13eb330aa1..0255b5de68 100644 --- a/src/common/dcgraph.cpp +++ b/src/common/dcgraph.cpp @@ -160,6 +160,11 @@ void wxGCDCImpl::SetGraphicsContext( wxGraphicsContext* ctx ) if ( DoInitContext(ctx) ) { + if (m_graphicContext->GetWindow()) + { + m_window = m_graphicContext->GetWindow(); + } + // Reapply our attributes to the context. m_graphicContext->SetFont( m_font , m_textForegroundColour ); m_graphicContext->SetPen( m_pen ); From e97926e55551b55d98caa5f9d36d95151c401903 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 22:14:35 +0200 Subject: [PATCH 16/29] Don't override wxUSE_DPI_AWARE_MANIFEST in resource file --- samples/sample.rc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/sample.rc b/samples/sample.rc index 8d8636a72d..68fe34e2f8 100644 --- a/samples/sample.rc +++ b/samples/sample.rc @@ -26,7 +26,9 @@ sample ICON "sample.ico" #define wxUSE_NO_MANIFEST 0 // To get DPI change events, we need to opt in into per monitor DPI support. +#ifndef wxUSE_DPI_AWARE_MANIFEST #define wxUSE_DPI_AWARE_MANIFEST 2 +#endif // this is not always needed but doesn't hurt (except making the executable // very slightly larger): this file contains the standard icons, cursors, ... From 6637811f320f082ff3a5f322d6a897f05b48f0d3 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 20 Apr 2022 22:51:55 +0200 Subject: [PATCH 17/29] Fix saving bitmap with cairo renderer on macOS --- src/generic/graphicc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index b7e96fb471..452cf9c0dd 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -2250,7 +2250,7 @@ wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& #endif #ifdef __WXMAC__ - CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef(); + CGContextRef cgcontext = (CGContextRef)dc.GetSelectedBitmap().GetHBITMAP(); cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height); Init( cairo_create( surface ) ); cairo_surface_destroy( surface ); From e8cf1f989bacaceec96ce664e3eb9665e80698c4 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Thu, 21 Apr 2022 21:37:53 +0200 Subject: [PATCH 18/29] Add ToDIP to wxDC and wxGraphicsContext --- include/wx/dc.h | 16 ++++++++++++++++ include/wx/dcsvg.h | 5 +++++ include/wx/generic/dcpsg.h | 5 +++++ include/wx/graphics.h | 11 +++++++++++ include/wx/msw/dcprint.h | 5 +++++ src/common/dcbase.cpp | 11 +++++++++++ src/common/graphcmn.cpp | 12 ++++++++++++ src/msw/enhmeta.cpp | 5 +++++ 8 files changed, 70 insertions(+) diff --git a/include/wx/dc.h b/include/wx/dc.h index c1f978f2c4..6ca1742f1d 100644 --- a/include/wx/dc.h +++ b/include/wx/dc.h @@ -541,6 +541,8 @@ public: virtual wxSize FromDIP(const wxSize& sz) const; + virtual wxSize ToDIP(const wxSize& sz) const; + #ifdef __WXMSW__ // Native Windows functions using the underlying HDC don't honour GDI+ // transformations which may be applied to it. Using this function we can @@ -839,6 +841,20 @@ public: int FromDIP(int d) const { return FromDIP(wxSize(d, 0)).x; } + wxSize ToDIP(const wxSize & sz) const + { + return m_pimpl->ToDIP(sz); + } + wxPoint ToDIP(const wxPoint & pt) const + { + const wxSize sz = ToDIP(wxSize(pt.x, pt.y)); + return wxPoint(sz.x, sz.y); + } + int ToDIP(int d) const + { + return ToDIP(wxSize(d, 0)).x; + } + // Right-To-Left (RTL) modes void SetLayoutDirection(wxLayoutDirection dir) diff --git a/include/wx/dcsvg.h b/include/wx/dcsvg.h index 91e04c4449..778e247446 100644 --- a/include/wx/dcsvg.h +++ b/include/wx/dcsvg.h @@ -253,6 +253,11 @@ private: return sz; } + virtual wxSize ToDIP(const wxSize& sz) const wxOVERRIDE + { + return sz; + } + void Init(const wxString& filename, int width, int height, double dpi, const wxString& title); diff --git a/include/wx/generic/dcpsg.h b/include/wx/generic/dcpsg.h index 45628064ba..17b58c6285 100644 --- a/include/wx/generic/dcpsg.h +++ b/include/wx/generic/dcpsg.h @@ -81,6 +81,11 @@ public: return sz; } + virtual wxSize ToDIP(const wxSize& sz) const wxOVERRIDE + { + return sz; + } + virtual void ComputeScaleAndOrigin() wxOVERRIDE; void SetBackgroundMode(int WXUNUSED(mode)) wxOVERRIDE { } diff --git a/include/wx/graphics.h b/include/wx/graphics.h index 935f06ac53..e6b3b02196 100644 --- a/include/wx/graphics.h +++ b/include/wx/graphics.h @@ -750,6 +750,17 @@ public: return FromDIP(wxSize(d, 0)).x; } + wxSize ToDIP(const wxSize& sz) const; + wxPoint ToDIP(const wxPoint& pt) const + { + const wxSize sz = ToDIP(wxSize(pt.x, pt.y)); + return wxPoint(sz.x, sz.y); + } + int ToDIP(int d) const + { + return ToDIP(wxSize(d, 0)).x; + } + #if 0 // sets the current alpha on this context virtual void SetAlpha( wxDouble alpha ); diff --git a/include/wx/msw/dcprint.h b/include/wx/msw/dcprint.h index 0b9a9af7fa..fac813719d 100644 --- a/include/wx/msw/dcprint.h +++ b/include/wx/msw/dcprint.h @@ -41,6 +41,11 @@ public: return sz; } + virtual wxSize ToDIP(const wxSize& sz) const wxOVERRIDE + { + return sz; + } + void SetFont(const wxFont& font) wxOVERRIDE; protected: diff --git a/src/common/dcbase.cpp b/src/common/dcbase.cpp index f718a1163a..be5c069e63 100644 --- a/src/common/dcbase.cpp +++ b/src/common/dcbase.cpp @@ -617,6 +617,17 @@ wxSize wxDCImpl::FromDIP(const wxSize& sz) const #endif // wxHAS_DPI_INDEPENDENT_PIXELS } +wxSize wxDCImpl::ToDIP(const wxSize& sz) const +{ +#ifdef wxHAS_DPI_INDEPENDENT_PIXELS + return sz; +#else + const wxSize dpi = GetPPI(); + const wxSize baseline = wxDisplay::GetStdPPI(); + return wxRescaleCoord(sz).From(dpi).To(baseline); +#endif // wxHAS_DPI_INDEPENDENT_PIXELS +} + bool wxDCImpl::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const { wxTextMeasure tm(GetOwner(), &m_font); diff --git a/src/common/graphcmn.cpp b/src/common/graphcmn.cpp index 1143fe4a7b..f03d1f7dec 100644 --- a/src/common/graphcmn.cpp +++ b/src/common/graphcmn.cpp @@ -647,6 +647,18 @@ wxSize wxGraphicsContext::FromDIP(const wxSize& sz) const #endif // wxHAS_DPI_INDEPENDENT_PIXELS } +wxSize wxGraphicsContext::ToDIP(const wxSize& sz) const +{ +#ifdef wxHAS_DPI_INDEPENDENT_PIXELS + return sz; +#else + wxRealPoint dpi; + GetDPI(&dpi.x, &dpi.y); + const wxSize baseline = wxDisplay::GetStdPPI(); + return wxRescaleCoord(sz).From(wxSize((int)dpi.x, (int)dpi.y)).To(baseline); +#endif // wxHAS_DPI_INDEPENDENT_PIXELS +} + // sets the pen void wxGraphicsContext::SetPen( const wxGraphicsPen& pen ) { diff --git a/src/msw/enhmeta.cpp b/src/msw/enhmeta.cpp index 62e9105e35..c1f2e2f7ef 100644 --- a/src/msw/enhmeta.cpp +++ b/src/msw/enhmeta.cpp @@ -229,6 +229,11 @@ public: return sz; } + virtual wxSize ToDIP(const wxSize& sz) const wxOVERRIDE + { + return sz; + } + void SetFont(const wxFont& font) wxOVERRIDE { wxFont scaledFont = font; From b50633f440c0596991ec5f20474bdc7a6966fda4 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Thu, 21 Apr 2022 21:41:42 +0200 Subject: [PATCH 19/29] Fix bitmap size when saving image in drawing sample wxBitmap::CreateWithDIPSize expects an DPI independent size. And when saving as SVG, it also expects a DPI independent size. Also show DPI independent coordinates in the status bar. --- samples/drawing/drawing.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/drawing/drawing.cpp b/samples/drawing/drawing.cpp index d9836109c0..366c408e1a 100644 --- a/samples/drawing/drawing.cpp +++ b/samples/drawing/drawing.cpp @@ -2000,8 +2000,8 @@ void MyCanvas::Draw(wxDC& pdc) { wxCoord x0, y0; dc.GetDeviceOrigin(&x0, &y0); - m_sizeX = dc.LogicalToDeviceX(dc.MaxX()) - x0 + 1; - m_sizeY = dc.LogicalToDeviceY(dc.MaxY()) - y0 + 1; + m_sizeX = dc.ToDIP(dc.LogicalToDeviceX(dc.MaxX()) - x0) + 1; + m_sizeY = dc.ToDIP(dc.LogicalToDeviceY(dc.MaxY()) - y0) + 1; } } @@ -2013,7 +2013,7 @@ void MyCanvas::OnMouseMove(wxMouseEvent &event) PrepareDC(dc); m_owner->PrepareDC(dc); - wxPoint pos = dc.DeviceToLogical(event.GetPosition()); + wxPoint pos = dc.ToDIP(dc.DeviceToLogical(event.GetPosition())); wxString str; str.Printf( "Current mouse position: %d,%d", pos.x, pos.y ); m_owner->SetStatusText( str ); From deaa43e32db0d9eb8e6edc7eacbfad4ef504314c Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sun, 24 Apr 2022 18:35:24 +0200 Subject: [PATCH 20/29] CMake: Remove workaround for removing png headers from cairo CAIRO_INCLUDE_DIRS does not include them with the current FindCairo.cmake. --- build/cmake/init.cmake | 3 --- 1 file changed, 3 deletions(-) diff --git a/build/cmake/init.cmake b/build/cmake/init.cmake index 12644fb883..3ff14ab91f 100644 --- a/build/cmake/init.cmake +++ b/build/cmake/init.cmake @@ -580,9 +580,6 @@ if(wxUSE_GUI) if(NOT CAIRO_FOUND) message(WARNING "Cairo not found, Cairo renderer won't be available") wx_option_force_value(wxUSE_CAIRO OFF) - else() - #TODO only if build-in png? - list(FILTER CAIRO_INCLUDE_DIRS EXCLUDE REGEX ".*libpng.*") endif() endif() endif() From bf4e45fc67fc953814debd34f37792eeeabd4534 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sat, 23 Apr 2022 16:21:56 +0200 Subject: [PATCH 21/29] Fix Cairo DPI when no wxWindow is associated Always multiply with the content scale factor, also on macOS. --- src/generic/graphicc.cpp | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index 452cf9c0dd..2460ad932e 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -2764,27 +2764,13 @@ void wxCairoContext::Flush() void wxCairoContext::GetDPI(wxDouble* dpiX, wxDouble* dpiY) const { - if ( GetWindow() ) - { - const wxSize dpi = GetWindow()->GetDPI(); + const wxSize dpi = GetWindow() ? GetWindow()->GetDPI() : + (wxDisplay::GetStdPPI() * GetContentScaleFactor()); - if ( dpiX ) - *dpiX = dpi.x; - if ( dpiY ) - *dpiY = dpi.y; - } - else - { - double dpi = (double)wxDisplay::GetStdPPIValue(); -#ifndef __WXMAC__ - dpi *= GetContentScaleFactor(); -#endif - - if ( dpiX ) - *dpiX = dpi; - if ( dpiY ) - *dpiY = dpi; - } + if (dpiX ) + *dpiX = dpi.x; + if ( dpiY ) + *dpiY = dpi.y; } void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) From 77a61569a8362cbef0247a2801c427641b447af3 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sun, 24 Apr 2022 18:40:02 +0200 Subject: [PATCH 22/29] Restructure Cairo font initialization Add some comments explaining why things are done. --- src/generic/graphicc.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index 2460ad932e..5e05177112 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -1138,19 +1138,24 @@ wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &fo { InitColour(col); -#ifndef __WXMAC__ +#ifdef __WXMSW__ + // Font is 72 DPI, screen is 96-based, and font needs a correction + // for screens with higher DPI (similar to gdi+ and d2d renderers). m_size = !dpi.y - ? double(font.GetPixelSize().GetHeight()) - : double(font.GetFractionalPointSize() * dpi.y / 72); + ? double(font.GetPixelSize().GetHeight()) + : (font.GetFractionalPointSize() * dpi.y / 72); #else + // On macOS, font and screen both are 72 DPI, and macOS font size does + // not need a correction for retina displays. + // GTK does not use m_size, but initialize it anyway and mark dpi as unused + // so we don't get any compiler warnings. wxUnusedVar(dpi); - m_size = font.GetPointSize(); + m_size = font.GetFractionalPointSize() * wxDisplay::GetStdPPIValue() / 72; +#ifdef __WXMAC__ + m_font = cairo_quartz_font_face_create_for_cgfont(font.OSXGetCGFont()); +#endif #endif -#ifdef __WXMAC__ - m_font = cairo_quartz_font_face_create_for_cgfont( font.OSXGetCGFont() ); -#elif defined(__WXGTK__) -#else InitFontComponents ( font.GetFaceName(), @@ -1159,7 +1164,6 @@ wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &fo font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD : CAIRO_FONT_WEIGHT_NORMAL ); -#endif } wxCairoFontData::wxCairoFontData(wxGraphicsRenderer* renderer, From bb91534cb31c619f1906af7945340f9e30e3311c Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sun, 24 Apr 2022 18:41:33 +0200 Subject: [PATCH 23/29] Use different method to get CGContextRef on Cairo renderer --- src/generic/graphicc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index 5e05177112..c43735010c 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -2254,7 +2254,7 @@ wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& #endif #ifdef __WXMAC__ - CGContextRef cgcontext = (CGContextRef)dc.GetSelectedBitmap().GetHBITMAP(); + CGContextRef cgcontext = (CGContextRef)dc.GetGraphicsContext()->GetNativeContext(); cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height); Init( cairo_create( surface ) ); cairo_surface_destroy( surface ); From 8665960c5567d33d18528653def60887b8c6848e Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sat, 23 Apr 2022 18:09:50 +0200 Subject: [PATCH 24/29] Simplify printing and drawing sample constructors Don't use FromDIP for 0. Include DIP in drawing sample variable and function names to make it clear these sizes are in DIP. Show both logical and DIP coordinates in statusbar. --- samples/drawing/drawing.cpp | 58 ++++++++++++++++++----------------- samples/printing/printing.cpp | 37 +++++++++++----------- samples/printing/printing.h | 4 +-- 3 files changed, 50 insertions(+), 49 deletions(-) diff --git a/samples/drawing/drawing.cpp b/samples/drawing/drawing.cpp index 366c408e1a..b190c1e6c8 100644 --- a/samples/drawing/drawing.cpp +++ b/samples/drawing/drawing.cpp @@ -125,7 +125,7 @@ public: #endif // wxUSE_GRAPHICS_CONTEXT void UseBuffer(bool use) { m_useBuffer = use; Refresh(); } void ShowBoundingBox(bool show) { m_showBBox = show; Refresh(); } - void GetDrawingSize(int* width, int* height) const; + wxSize GetDIPDrawingSize() const; void Draw(wxDC& dc); @@ -175,8 +175,7 @@ private: #endif bool m_useBuffer; bool m_showBBox; - wxCoord m_sizeX; - wxCoord m_sizeY; + wxSize m_sizeDIP; wxDECLARE_EVENT_TABLE(); }; @@ -187,7 +186,7 @@ class MyFrame : public wxFrame { public: // ctor(s) - MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); + MyFrame(const wxString& title); // event handlers (these functions should _not_ be virtual) void OnQuit(wxCommandEvent& event); @@ -461,8 +460,7 @@ bool MyApp::OnInit() #endif // Create the main application window - MyFrame *frame = new MyFrame("Drawing sample", - wxDefaultPosition, wxSize(550, 840)); + MyFrame *frame = new MyFrame("Drawing sample"); // Show it frame->Show(true); @@ -522,8 +520,7 @@ MyCanvas::MyCanvas(MyFrame *parent) #endif m_useBuffer = false; m_showBBox = false; - m_sizeX = 0; - m_sizeY = 0; + m_sizeDIP = wxSize(0, 0); } void MyCanvas::DrawTestBrushes(wxDC& dc) @@ -587,7 +584,7 @@ void MyCanvas::DrawTestPoly(wxDC& dc) "one entirely hatched", dc.FromDIP(10), dc.FromDIP(30)); dc.DrawText("The third star only has a hatched outline", dc.FromDIP(10), dc.FromDIP(50)); - dc.DrawPolygon(WXSIZEOF(star), star, dc.FromDIP(0), dc.FromDIP(30)); + dc.DrawPolygon(WXSIZEOF(star), star, 0, dc.FromDIP(30)); dc.DrawPolygon(WXSIZEOF(star), star, dc.FromDIP(160), dc.FromDIP(30), wxWINDING_RULE); wxBrush brushHatchGreen(*wxGREEN, wxBRUSHSTYLE_FDIAGONAL_HATCH); @@ -1808,13 +1805,9 @@ void MyCanvas::DrawRegionsHelper(wxDC& dc, wxCoord x, bool firstTime) } } -void MyCanvas::GetDrawingSize(int* width, int* height) const +wxSize MyCanvas::GetDIPDrawingSize() const { - if ( width ) - *width = m_sizeX; - - if ( height ) - *height = m_sizeY; + return m_sizeDIP; } void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event)) @@ -1895,8 +1888,8 @@ void MyCanvas::Draw(wxDC& pdc) if ( m_clip ) { - int clipSize = dc.FromDIP(100); - dc.SetClippingRegion(clipSize, clipSize, clipSize, clipSize); + dc.SetClippingRegion(wxPoint(dc.FromDIP(100), dc.FromDIP(100)), + wxSize(dc.FromDIP(100), dc.FromDIP(100))); } dc.Clear(); @@ -2000,8 +1993,8 @@ void MyCanvas::Draw(wxDC& pdc) { wxCoord x0, y0; dc.GetDeviceOrigin(&x0, &y0); - m_sizeX = dc.ToDIP(dc.LogicalToDeviceX(dc.MaxX()) - x0) + 1; - m_sizeY = dc.ToDIP(dc.LogicalToDeviceY(dc.MaxY()) - y0) + 1; + m_sizeDIP.x = dc.ToDIP(dc.LogicalToDeviceX(dc.MaxX()) - x0) + 1; + m_sizeDIP.y = dc.ToDIP(dc.LogicalToDeviceY(dc.MaxY()) - y0) + 1; } } @@ -2013,9 +2006,12 @@ void MyCanvas::OnMouseMove(wxMouseEvent &event) PrepareDC(dc); m_owner->PrepareDC(dc); - wxPoint pos = dc.ToDIP(dc.DeviceToLogical(event.GetPosition())); + wxPoint pos = dc.DeviceToLogical(event.GetPosition()); + wxPoint dipPos = dc.ToDIP(pos); wxString str; - str.Printf( "Current mouse position: %d,%d", pos.x, pos.y ); + str.Printf( "Mouse position: %d,%d", pos.x, pos.y ); + if ( pos != dipPos ) + str += wxString::Format("; DIP position: %d,%d", dipPos.x, dipPos.y); m_owner->SetStatusText( str ); } @@ -2231,8 +2227,8 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) wxEND_EVENT_TABLE() // frame constructor -MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) - : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) +MyFrame::MyFrame(const wxString& title) + : wxFrame(NULL, wxID_ANY, title) { // set the frame icon SetIcon(wxICON(sample)); @@ -2403,6 +2399,9 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) m_canvas = new MyCanvas( this ); m_canvas->SetScrollbars( 10, 10, 100, 240 ); + + SetSize(FromDIP(wxSize(800, 700))); + Center(wxBOTH); } // event handlers @@ -2468,8 +2467,7 @@ void MyFrame::OnSave(wxCommandEvent& WXUNUSED(event)) wxFD_SAVE | wxFD_OVERWRITE_PROMPT); if (dlg.ShowModal() == wxID_OK) { - int width, height; - m_canvas->GetDrawingSize(&width, &height); + wxSize canvasSize = m_canvas->GetDIPDrawingSize(); wxFileName fn(dlg.GetPath()); wxString ext = fn.GetExt().Lower(); #if wxUSE_SVG @@ -2484,7 +2482,11 @@ void MyFrame::OnSave(wxCommandEvent& WXUNUSED(event)) wxGraphicsRenderer* tempRenderer = m_canvas->GetRenderer(); m_canvas->UseGraphicRenderer(NULL); #endif - wxSVGFileDC svgdc(dlg.GetPath(), width, height, 72, "Drawing sample"); + wxSVGFileDC svgdc(dlg.GetPath(), + canvasSize.GetWidth(), + canvasSize.GetHeight(), + 72, + "Drawing sample"); svgdc.SetBitmapHandler(new wxSVGBitmapEmbedHandler()); m_canvas->Draw(svgdc); #if wxUSE_GRAPHICS_CONTEXT @@ -2518,7 +2520,7 @@ void MyFrame::OnSave(wxCommandEvent& WXUNUSED(event)) // Change the scale temporarily to fit the drawing into the page. int w, h; psdc.GetSize(&w, &h); - double sc = wxMin((double)w / width, (double)h / height); + double sc = wxMin((double)w / canvasSize.GetWidth(), (double)h / canvasSize.GetHeight()); m_xUserScale *= sc; m_yUserScale *= sc; psdc.StartDoc("Drawing sample"); @@ -2539,7 +2541,7 @@ void MyFrame::OnSave(wxCommandEvent& WXUNUSED(event)) #endif // wxUSE_POSTSCRIPT { wxBitmap bmp; - bmp.CreateWithDIPSize(wxSize(width, height), GetDPIScaleFactor()); + bmp.CreateWithDIPSize(canvasSize, GetDPIScaleFactor()); wxMemoryDC mdc(bmp); mdc.SetBackground(*wxWHITE_BRUSH); mdc.Clear(); diff --git a/samples/printing/printing.cpp b/samples/printing/printing.cpp index d6c7bfa221..8d8b4a361d 100644 --- a/samples/printing/printing.cpp +++ b/samples/printing/printing.cpp @@ -110,10 +110,7 @@ bool MyApp::OnInit(void) // Create the main frame window // ---------------------------- - MyFrame* frame = new MyFrame((wxFrame *) NULL, "wxWidgets Printing Demo", - wxDefaultPosition, wxDefaultSize); - frame->SetSize(frame->FromDIP(wxSize(400, 400))); - frame->Centre(wxBOTH); + MyFrame* frame = new MyFrame("wxWidgets Printing Demo"); frame->Show(); return true; @@ -142,15 +139,15 @@ void MyApp::Draw(wxDC&dc) dc.SetPen(*wxBLACK_PEN); dc.SetBrush(*wxLIGHT_GREY_BRUSH); - dc.DrawRectangle(dc.FromDIP(0), dc.FromDIP(0), dc.FromDIP(230), dc.FromDIP(350)); - dc.DrawLine(dc.FromDIP(0), dc.FromDIP(0), dc.FromDIP(229), dc.FromDIP(349)); - dc.DrawLine(dc.FromDIP(229), dc.FromDIP(0), dc.FromDIP(0), dc.FromDIP(349)); + dc.DrawRectangle(0, 0, dc.FromDIP(230), dc.FromDIP(350)); + dc.DrawLine(0, 0, dc.FromDIP(229), dc.FromDIP(349)); + dc.DrawLine(dc.FromDIP(229), 0, 0, dc.FromDIP(349)); dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.SetBrush(*wxCYAN_BRUSH); dc.SetPen(*wxRED_PEN); - dc.DrawRoundedRectangle(dc.FromDIP(0), dc.FromDIP(20), dc.FromDIP(200), dc.FromDIP(80), 20); + dc.DrawRoundedRectangle(0, dc.FromDIP(20), dc.FromDIP(200), dc.FromDIP(80), 20); dc.DrawText( "Rectangle 200 by 80", dc.FromDIP(40), dc.FromDIP(40)); @@ -169,10 +166,10 @@ void MyApp::Draw(wxDC&dc) #endif wxPoint points[5]; - points[0].x = dc.FromDIP(0); - points[0].y = dc.FromDIP(0); + points[0].x = 0; + points[0].y = 0; points[1].x = dc.FromDIP(20); - points[1].y = dc.FromDIP(0); + points[1].y = 0; points[2].x = dc.FromDIP(20); points[2].y = dc.FromDIP(20); points[3].x = dc.FromDIP(10); @@ -277,8 +274,8 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) MyFrame::OnPreviewFrameModalityKind) wxEND_EVENT_TABLE() -MyFrame::MyFrame(wxFrame *frame, const wxString&title, const wxPoint&pos, const wxSize&size) - : wxFrame(frame, wxID_ANY, title, pos, size) +MyFrame::MyFrame(const wxString& title) + : wxFrame(NULL, wxID_ANY, title) { m_canvas = NULL; m_previewModality = wxPreviewFrame_AppModal; @@ -343,11 +340,13 @@ MyFrame::MyFrame(wxFrame *frame, const wxString&title, const wxPoint&pos, const // create the canvas // ----------------- - m_canvas = new MyCanvas(this, wxPoint(0, 0), FromDIP(wxSize(100, 100)), - wxRETAINED|wxHSCROLL|wxVSCROLL); + m_canvas = new MyCanvas(this, wxRETAINED | wxHSCROLL | wxVSCROLL); // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction m_canvas->SetScrollbars(20, 20, 50, 50); + + SetSize(FromDIP(wxSize(400, 400))); + Centre(wxBOTH); } void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) @@ -392,7 +391,7 @@ void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event)) } wxPreviewFrame *frame = - new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), FromDIP(wxSize(600, 650))); + new wxPreviewFrame(preview, this, "Demo Print Preview", wxDefaultPosition, FromDIP(wxSize(600, 700))); frame->Centre(wxBOTH); frame->InitializeWithModality(m_previewModality); frame->Show(); @@ -427,7 +426,7 @@ void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event)) wxPrintDialogData printDialogData(* g_printData); wxPrintPreview *preview = new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData); wxPreviewFrame *frame = - new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), FromDIP(wxSize(600, 650))); + new wxPreviewFrame(preview, this, "Demo Print Preview", wxDefaultPosition, FromDIP(wxSize(600, 700))); frame->Centre(wxBOTH); frame->Initialize(); frame->Show(); @@ -491,8 +490,8 @@ wxBEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) // EVT_PAINT(MyCanvas::OnPaint) wxEND_EVENT_TABLE() -MyCanvas::MyCanvas(wxFrame *frame, const wxPoint&pos, const wxSize&size, long style) - : wxScrolledWindow(frame, wxID_ANY, pos, size, style) +MyCanvas::MyCanvas(wxFrame *frame, long style) + : wxScrolledWindow(frame, wxID_ANY, wxDefaultPosition, wxDefaultSize, style) { SetBackgroundColour(*wxWHITE); } diff --git a/samples/printing/printing.h b/samples/printing/printing.h index 7d04618673..46747cc2bc 100644 --- a/samples/printing/printing.h +++ b/samples/printing/printing.h @@ -40,7 +40,7 @@ class MyCanvas; class MyFrame: public wxFrame { public: - MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size); + MyFrame(const wxString& title); void OnAngleUp(wxCommandEvent& event); void OnAngleDown(wxCommandEvent& event); @@ -73,7 +73,7 @@ private: class MyCanvas: public wxScrolledWindow { public: - MyCanvas(wxFrame *frame, const wxPoint& pos, const wxSize& size, long style = wxRETAINED); + MyCanvas(wxFrame *frame, long style); //void OnPaint(wxPaintEvent& evt); virtual void OnDraw(wxDC& dc) wxOVERRIDE; From 2bab3e8d1c9502a106ee70f5eb61cf1ca933be93 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sun, 24 Apr 2022 10:48:38 +0200 Subject: [PATCH 25/29] Remove wxWindow DPI from wxMemoryDC wxMemoryDC is normally not associated with a wxWindow. --- src/msw/dcmemory.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/msw/dcmemory.cpp b/src/msw/dcmemory.cpp index 5f6f282655..b648779c34 100644 --- a/src/msw/dcmemory.cpp +++ b/src/msw/dcmemory.cpp @@ -145,10 +145,7 @@ void wxMemoryDCImpl::DoSelect( const wxBitmap& bitmap ) wxSize wxMemoryDCImpl::GetPPI() const { - if ( GetWindow() ) - return GetWindow()->GetDPI(); - else - return wxDisplay::GetStdPPI() * GetContentScaleFactor(); + return wxDisplay::GetStdPPI() * GetContentScaleFactor(); } void wxMemoryDCImpl::SetFont(const wxFont& font) From d2366b2eb0c6570293059eb68173e6a75242b656 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sun, 24 Apr 2022 14:23:03 +0200 Subject: [PATCH 26/29] Fix printing at high DPI on GTK3 and when saving as postscript --- src/common/dcbase.cpp | 3 +-- src/generic/printps.cpp | 3 ++- src/gtk/print.cpp | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/common/dcbase.cpp b/src/common/dcbase.cpp index be5c069e63..2090be4818 100644 --- a/src/common/dcbase.cpp +++ b/src/common/dcbase.cpp @@ -1471,8 +1471,7 @@ float wxDCImpl::GetFontPointSizeAdjustment(float dpi) // are ~6 times smaller when printing. Unfortunately, this bug is so severe // that *all* printing code has to account for it and consequently, other // ports need to emulate this bug too: - const wxSize screenPPI = wxGetDisplayPPI(); - return float(screenPPI.y) / dpi; + return float(wxDisplay::GetStdPPIValue()) / dpi; } double wxDCImpl::GetMMToPXx() const diff --git a/src/generic/printps.cpp b/src/generic/printps.cpp index c49d24af2a..e9a230e90d 100644 --- a/src/generic/printps.cpp +++ b/src/generic/printps.cpp @@ -38,6 +38,7 @@ #include "wx/generic/prntdlgg.h" #include "wx/progdlg.h" #include "wx/paper.h" +#include "wx/display.h" #include @@ -312,7 +313,7 @@ void wxPostScriptPrintPreview::DetermineScaling() { int resolution = 600; // TODO, this is correct, but get this from wxPSDC somehow - const wxSize screenPPI = wxGetDisplayPPI(); + const wxSize screenPPI = wxDisplay::GetStdPPI(); int logPPIScreenX = screenPPI.GetWidth(); int logPPIScreenY = screenPPI.GetHeight(); int logPPIPrinterX = resolution; diff --git a/src/gtk/print.cpp b/src/gtk/print.cpp index e263b23561..97dea834b1 100644 --- a/src/gtk/print.cpp +++ b/src/gtk/print.cpp @@ -29,6 +29,7 @@ #include "wx/dynlib.h" #include "wx/paper.h" #include "wx/modalhook.h" +#include "wx/display.h" #include "wx/gtk/private/wrapgtk.h" @@ -2476,7 +2477,7 @@ void wxGtkPrintPreview::DetermineScaling() if (paper) { - const wxSize screenPPI = wxGetDisplayPPI(); + const wxSize screenPPI = wxDisplay::GetStdPPI(); int logPPIScreenX = screenPPI.GetWidth(); int logPPIScreenY = screenPPI.GetHeight(); int logPPIPrinterX = m_resolution; From fed52eb7163f6766e1c05d125f0b77df5d35c6c4 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Tue, 26 Apr 2022 20:50:49 +0200 Subject: [PATCH 27/29] CMake: Mark Cairo and other module variables as advanced --- build/cmake/modules/FindCairo.cmake | 2 ++ build/cmake/modules/FindGNOMEVFS2.cmake | 3 +-- build/cmake/modules/FindGTK3.cmake | 2 ++ build/cmake/modules/FindGTK4.cmake | 2 ++ build/cmake/modules/FindLIBSECRET.cmake | 2 ++ build/cmake/modules/FindWAYLANDEGL.cmake | 2 ++ 6 files changed, 11 insertions(+), 2 deletions(-) diff --git a/build/cmake/modules/FindCairo.cmake b/build/cmake/modules/FindCairo.cmake index 1ee147bd8d..ddb3129bc7 100644 --- a/build/cmake/modules/FindCairo.cmake +++ b/build/cmake/modules/FindCairo.cmake @@ -79,3 +79,5 @@ ENDIF () INCLUDE(FindPackageHandleStandardArgs) FIND_PACKAGE_HANDLE_STANDARD_ARGS(Cairo DEFAULT_MSG CAIRO_INCLUDE_DIRS CAIRO_LIBRARIES VERSION_OK) + +mark_as_advanced(CAIRO_INCLUDE_DIRS CAIRO_LIBRARIES) diff --git a/build/cmake/modules/FindGNOMEVFS2.cmake b/build/cmake/modules/FindGNOMEVFS2.cmake index 1313975ea1..0d1b85bf8b 100644 --- a/build/cmake/modules/FindGNOMEVFS2.cmake +++ b/build/cmake/modules/FindGNOMEVFS2.cmake @@ -84,7 +84,6 @@ else (GNOMEVFS2_LIBRARIES AND GNOMEVFS2_INCLUDE_DIRS) endif (GNOMEVFS2_FOUND) # show the GNOMEVFS2_INCLUDE_DIRS and GNOMEVFS2_LIBRARIES variables only in the advanced view - mark_as_advanced(GNOMEVFS2_INCLUDE_DIRS GNOMEVFS2_LIBRARIES) + mark_as_advanced(GNOMEVFS2_INCLUDE_DIRS GNOMEVFS2_LIBRARIES GNOMEVFS2_INCLUDE_DIR GNOMEVFS-2_LIBRARY) endif (GNOMEVFS2_LIBRARIES AND GNOMEVFS2_INCLUDE_DIRS) - diff --git a/build/cmake/modules/FindGTK3.cmake b/build/cmake/modules/FindGTK3.cmake index 8fee6f9f41..d2939a1df5 100644 --- a/build/cmake/modules/FindGTK3.cmake +++ b/build/cmake/modules/FindGTK3.cmake @@ -49,3 +49,5 @@ check_symbol_exists(GDK_WINDOWING_WAYLAND "gdk/gdk.h" wxHAVE_GDK_WAYLAND) check_symbol_exists(GDK_WINDOWING_X11 "gdk/gdk.h" wxHAVE_GDK_X11) include(FindPackageHandleStandardArgs) FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK3 DEFAULT_MSG GTK3_INCLUDE_DIRS GTK3_LIBRARIES VERSION_OK) + +mark_as_advanced(GTK3_INCLUDE_DIRS GTK3_LIBRARIES) diff --git a/build/cmake/modules/FindGTK4.cmake b/build/cmake/modules/FindGTK4.cmake index ea8f07e6c0..7dc1ed09f3 100644 --- a/build/cmake/modules/FindGTK4.cmake +++ b/build/cmake/modules/FindGTK4.cmake @@ -44,3 +44,5 @@ endif () endif () include(FindPackageHandleStandardArgs) FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK4 DEFAULT_MSG GTK4_INCLUDE_DIRS GTK4_LIBRARIES VERSION_OK) + +mark_as_advanced(GTK4_INCLUDE_DIRS GTK4_LIBRARIES) diff --git a/build/cmake/modules/FindLIBSECRET.cmake b/build/cmake/modules/FindLIBSECRET.cmake index af1e059fcb..6f064cb837 100644 --- a/build/cmake/modules/FindLIBSECRET.cmake +++ b/build/cmake/modules/FindLIBSECRET.cmake @@ -47,3 +47,5 @@ endif () include(FindPackageHandleStandardArgs) FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBSECRET DEFAULT_MSG LIBSECRET_INCLUDE_DIRS LIBSECRET_LIBRARIES VERSION_OK) + +mark_as_advanced(LIBSECRET_INCLUDE_DIRS LIBSECRET_LIBRARIES) diff --git a/build/cmake/modules/FindWAYLANDEGL.cmake b/build/cmake/modules/FindWAYLANDEGL.cmake index 6ca89ef560..ad23bf94c2 100644 --- a/build/cmake/modules/FindWAYLANDEGL.cmake +++ b/build/cmake/modules/FindWAYLANDEGL.cmake @@ -16,3 +16,5 @@ endif () include(FindPackageHandleStandardArgs) FIND_PACKAGE_HANDLE_STANDARD_ARGS(WAYLANDEGL DEFAULT_MSG WAYLANDEGL_LIBRARIES VERSION_OK) + +mark_as_advanced(WAYLANDEGL_LIBRARIES) From 6072d3aeee4ca82bd10a9616335e096538752537 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Tue, 26 Apr 2022 21:35:05 +0200 Subject: [PATCH 28/29] Add documentation for wxDC and wxGraphicsContext FromDIP and ToDIP Remove documentation of removed function GetDPIScaleFactor. --- interface/wx/dc.h | 51 ++++++++++++++++++++++++++++++++++------- interface/wx/graphics.h | 50 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 89 insertions(+), 12 deletions(-) diff --git a/interface/wx/dc.h b/interface/wx/dc.h index 6d1b214575..6235811e42 100644 --- a/interface/wx/dc.h +++ b/interface/wx/dc.h @@ -1562,17 +1562,52 @@ public: wxSize GetPPI() const; /** - Returns the DPI scale factor. + Convert DPI-independent pixel values to the value in pixels appropriate + for the DC. - Can be used to scale coordinates and sizes appropriate to the DPI of the - associated wxWindow, if any. + See wxWindow::FromDIP(const wxSize& sz) for more info about converting + device independent pixel values. - See wxWindow::GetDPIScaleFactor() for more details about the DPI scale - factor and its difference with GetContentScaleFactor(). + @since 3.1.7 + */ + wxSize FromDIP(const wxSize& sz) const; - @since 3.1.7 - */ - double GetDPIScaleFactor() const; + /// @overload + wxPoint FromDIP(const wxPoint& pt) const; + + /** + Convert DPI-independent value in pixels to the value in pixels + appropriate for the DC. + + This is the same as FromDIP(const wxSize& sz) overload, but assumes + that the resolution is the same in horizontal and vertical directions. + + @since 3.1.7 + */ + int FromDIP(int d) const; + + /** + Convert pixel values of the current DC to DPI-independent pixel values. + + See wxWindow::ToDIP(const wxSize& sz) for more info about converting + device independent pixel values. + + @since 3.1.7 + */ + wxSize ToDIP(const wxSize& sz) const; + + /// @overload + wxPoint ToDIP(const wxPoint& pt) const; + + /** + Convert pixel values of the current DC to DPI-independent pixel values. + + This is the same as ToDIP(const wxSize& sz) overload, but assumes + that the resolution is the same in horizontal and vertical directions. + + @since 3.1.7 + */ + int ToDIP(int d) const; /** Gets the horizontal and vertical extent of this device context in @e device units. diff --git a/interface/wx/graphics.h b/interface/wx/graphics.h index 11809fa4f1..adb56efa86 100644 --- a/interface/wx/graphics.h +++ b/interface/wx/graphics.h @@ -1157,12 +1157,54 @@ public: bool OffsetEnabled() const; /** - Get the DPI scale factor. Can be used to scale coordinates and sizes - appropriate to the DPI of the associated wxWindow or wxDC. + Convert DPI-independent pixel values to the value in pixels appropriate + for the graphics context. + + See wxWindow::FromDIP(const wxSize& sz) and wxDC::FromDIP(const wxSize& sz) + for more info about converting device independent pixel values. @since 3.1.7 - */ - double GetDPIScaleFactor() const; + */ + wxSize FromDIP(const wxSize& sz) const; + + /// @overload + wxPoint FromDIP(const wxPoint& pt) const; + + /** + Convert DPI-independent value in pixels to the value in pixels + appropriate for the graphics context. + + This is the same as FromDIP(const wxSize& sz) overload, but assumes + that the resolution is the same in horizontal and vertical directions. + + @since 3.1.7 + */ + int FromDIP(int d) const; + + /** + Convert pixel values of the current graphics context to DPI-independent + pixel values. + + See wxWindow::ToDIP(const wxSize& sz) and wxDC::ToDIP(const wxSize& sz) + for more info about converting device independent pixel values. + + @since 3.1.7 + */ + wxSize ToDIP(const wxSize& sz) const; + + /// @overload + wxPoint ToDIP(const wxPoint& pt) const; + + /** + Convert pixel values of the current graphics context to DPI-independent + pixel values. + + This is the same as ToDIP(const wxSize& sz) overload, but assumes + that the resolution is the same in horizontal and vertical directions. + + @since 3.1.7 + */ + int ToDIP(int d) const; /** @} */ From dc5fe63cb2b58ce62698a6fd8f2908d4d6689443 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Fri, 29 Apr 2022 00:38:46 +0200 Subject: [PATCH 29/29] Don't implement FromDIP and ToDIP in headers --- include/wx/dcsvg.h | 10 ++-------- include/wx/generic/dcpsg.h | 10 ++-------- include/wx/msw/dcprint.h | 10 ++-------- src/common/dcsvg.cpp | 10 ++++++++++ src/generic/dcpsg.cpp | 9 +++++++++ src/msw/dcprint.cpp | 10 ++++++++++ 6 files changed, 35 insertions(+), 24 deletions(-) diff --git a/include/wx/dcsvg.h b/include/wx/dcsvg.h index 778e247446..fcdecc036c 100644 --- a/include/wx/dcsvg.h +++ b/include/wx/dcsvg.h @@ -248,15 +248,9 @@ private: virtual wxSize GetPPI() const wxOVERRIDE; - virtual wxSize FromDIP(const wxSize& sz) const wxOVERRIDE - { - return sz; - } + virtual wxSize FromDIP(const wxSize& sz) const wxOVERRIDE; - virtual wxSize ToDIP(const wxSize& sz) const wxOVERRIDE - { - return sz; - } + virtual wxSize ToDIP(const wxSize& sz) const wxOVERRIDE; void Init(const wxString& filename, int width, int height, double dpi, const wxString& title); diff --git a/include/wx/generic/dcpsg.h b/include/wx/generic/dcpsg.h index 17b58c6285..cf3e1356ff 100644 --- a/include/wx/generic/dcpsg.h +++ b/include/wx/generic/dcpsg.h @@ -76,15 +76,9 @@ public: // Resolution in pixels per logical inch wxSize GetPPI() const wxOVERRIDE; - virtual wxSize FromDIP(const wxSize& sz) const wxOVERRIDE - { - return sz; - } + virtual wxSize FromDIP(const wxSize& sz) const wxOVERRIDE; - virtual wxSize ToDIP(const wxSize& sz) const wxOVERRIDE - { - return sz; - } + virtual wxSize ToDIP(const wxSize& sz) const wxOVERRIDE; virtual void ComputeScaleAndOrigin() wxOVERRIDE; diff --git a/include/wx/msw/dcprint.h b/include/wx/msw/dcprint.h index fac813719d..845c0d64ab 100644 --- a/include/wx/msw/dcprint.h +++ b/include/wx/msw/dcprint.h @@ -36,15 +36,9 @@ public: virtual wxRect GetPaperRect() const wxOVERRIDE; - virtual wxSize FromDIP(const wxSize& sz) const wxOVERRIDE - { - return sz; - } + virtual wxSize FromDIP(const wxSize& sz) const wxOVERRIDE; - virtual wxSize ToDIP(const wxSize& sz) const wxOVERRIDE - { - return sz; - } + virtual wxSize ToDIP(const wxSize& sz) const wxOVERRIDE; void SetFont(const wxFont& font) wxOVERRIDE; diff --git a/src/common/dcsvg.cpp b/src/common/dcsvg.cpp index 6104c349d3..5e5b20c81a 100644 --- a/src/common/dcsvg.cpp +++ b/src/common/dcsvg.cpp @@ -580,6 +580,16 @@ wxSize wxSVGFileDCImpl::GetPPI() const return wxSize(wxRound(m_dpi), wxRound(m_dpi)); } +wxSize wxSVGFileDCImpl::FromDIP(const wxSize& sz) const +{ + return sz; +} + +wxSize wxSVGFileDCImpl::ToDIP(const wxSize& sz) const +{ + return sz; +} + void wxSVGFileDCImpl::Clear() { { diff --git a/src/generic/dcpsg.cpp b/src/generic/dcpsg.cpp index a089c8a8d1..62d8d43b91 100644 --- a/src/generic/dcpsg.cpp +++ b/src/generic/dcpsg.cpp @@ -1614,6 +1614,15 @@ wxSize wxPostScriptDCImpl::GetPPI() const return wxSize( DPI, DPI ); } +wxSize wxPostScriptDCImpl::FromDIP(const wxSize& sz) const +{ + return sz; +} + +wxSize wxPostScriptDCImpl::ToDIP(const wxSize& sz) const +{ + return sz; +} bool wxPostScriptDCImpl::StartDoc( const wxString& WXUNUSED(message) ) { diff --git a/src/msw/dcprint.cpp b/src/msw/dcprint.cpp index 294e7ebe84..b12aa4c73c 100644 --- a/src/msw/dcprint.cpp +++ b/src/msw/dcprint.cpp @@ -231,6 +231,16 @@ wxRect wxPrinterDCImpl::GetPaperRect() const return wxRect(x, y, w, h); } +wxSize wxPrinterDCImpl::FromDIP(const wxSize& sz) const +{ + return sz; +} + +wxSize wxPrinterDCImpl::ToDIP(const wxSize& sz) const +{ + return sz; +} + void wxPrinterDCImpl::SetFont(const wxFont& font) { wxFont scaledFont = font;