KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dpi_scaling_common.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2019 KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <dpi_scaling_common.h>
25
26#include <optional>
27
28#include <env_vars.h>
30#include <kiplatform/ui.h>
31
32#include <wx/log.h>
33#include <wx/window.h>
34
35
43const wxChar* const traceHiDpi = wxT( "KICAD_TRACE_HIGH_DPI" );
44
45
51static std::optional<double> getKiCadConfiguredScale( const COMMON_SETTINGS& aConfig )
52{
53 std::optional<double> scale;
54 double canvas_scale = aConfig.m_Appearance.canvas_scale;
55
56 if( canvas_scale > 0.0 )
57 {
58 scale = canvas_scale;
59 }
60
61 return scale;
62}
63
64
71static std::optional<double> getEnvironmentScale()
72{
73 const wxPortId port_id = wxPlatformInfo::Get().GetPortId();
74 std::optional<double> scale;
75
76 if( port_id == wxPORT_GTK )
77 {
78 // Under GTK, the user can use GDK_SCALE to force the scaling
79 scale = ENV_VAR::GetEnvVar<double>( wxS( "GDK_SCALE" ) );
80 }
81
82 return scale;
83}
84
85
86DPI_SCALING_COMMON::DPI_SCALING_COMMON( COMMON_SETTINGS* aConfig, const wxWindow* aWindow ) :
87 m_config( aConfig ),
88 m_window( aWindow )
89{
90}
91
92
94{
95 std::optional<double> val;
96 wxString src;
97
98 if( m_config )
99 {
101 src = wxS( "config" );
102 }
103
104 if( !val )
105 {
106 val = getEnvironmentScale();
107 src = wxS( "env" );
108 }
109
110 if( !val && m_window )
111 {
112 // Use the native WX reporting.
113 // On Linux, this will not work until WX 3.2 and GTK >= 3.10
114 // Otherwise it returns 1.0
116 src = wxS( "platform" );
117 }
118
119 if( !val )
120 {
121 // Nothing else we can do, give it a default value
122 val = GetDefaultScaleFactor();
123 src = wxS( "default" );
124 }
125
126 wxLogTrace( traceHiDpi, wxS( "Scale factor (%s): %f" ), src, *val );
127
128 return *val;
129}
130
131
133{
134 std::optional<double> val;
135 wxString src;
136
137 if( m_config )
138 {
140 src = wxS( "config" );
141 }
142
143 if( !val )
144 {
145 val = getEnvironmentScale();
146 src = wxS( "env" );
147 }
148
149 if( !val && m_window )
150 {
151 // Use the native WX reporting.
152 // On Linux, this will not work until WX 3.2 and GTK >= 3.10
153 // Otherwise it returns 1.0
155 src = wxS( "platform" );
156 }
157
158 if( !val )
159 {
160 // Nothing else we can do, give it a default value
161 val = GetDefaultScaleFactor();
162 src = wxS( "default" );
163 }
164
165 wxLogTrace( traceHiDpi, wxS( "Content scale factor (%s): %f" ), src, *val );
166
167 return *val;
168}
169
170
172{
173 if( m_config == nullptr )
174 {
175 // No configuration given, so has to be automatic scaling
176 return true;
177 }
178
179 const bool automatic = getKiCadConfiguredScale( *m_config ) == std::nullopt;
180 wxLogTrace( traceHiDpi, wxS( "Scale is automatic: %d" ), automatic );
181 return automatic;
182}
183
184
185void DPI_SCALING_COMMON::SetDpiConfig( bool aAuto, double aValue )
186{
187 wxCHECK_RET( m_config != nullptr, wxS( "Setting DPI config without a config store." ) );
188
189 const double value = aAuto ? 0.0 : aValue;
190
192}
APPEARANCE m_Appearance
const wxWindow * m_window
The WX window to use for WX's automatic DPI checking.
bool GetCanvasIsAutoScaled() const override
Is the current value auto scaled, or is it user-set in the config.
double GetContentScaleFactor() const override
Get the content scale factor, which may be different from the scale factor on some platforms.
double GetScaleFactor() const override
Get the DPI scale from all known sources in order:
void SetDpiConfig(bool aAuto, double aValue) override
Set the common DPI config in a given config object.
DPI_SCALING_COMMON(COMMON_SETTINGS *aConfig, const wxWindow *aWindow)
Construct a DPI scale provider.
COMMON_SETTINGS * m_config
The configuration object to use to get/set user setting.
static double GetDefaultScaleFactor()
Get the "default" scaling factor to use if not other config is available.
Definition: dpi_scaling.cpp:42
static std::optional< double > getKiCadConfiguredScale(const COMMON_SETTINGS &aConfig)
Get a user-configured scale factor from KiCad config file.
static std::optional< double > getEnvironmentScale()
Get the toolkit scale factor from a user-set environment variable (for example GDK_SCALE on GTK).
Functions related to environment variables, including help functions.
const wxChar *const traceHiDpi
Flag to enable trace for HiDPI scaling factors.
double GetPixelScaleFactor(const wxWindow *aWindow)
Tries to determine the pixel scaling factor currently in use for the window.
Definition: wxgtk/ui.cpp:175
double GetContentScaleFactor(const wxWindow *aWindow)
Tries to determine the content scaling factor currently in use for the window.
Definition: wxgtk/ui.cpp:188
const int scale