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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <dpi_scaling_common.h>
21
22#include <optional>
23
24#include <env_vars.h>
26#include <kiplatform/ui.h>
27
28#include <wx/log.h>
29#include <wx/window.h>
30
31
39const wxChar* const traceHiDpi = wxT( "KICAD_TRACE_HIGH_DPI" );
40
41
47static std::optional<double> getKiCadConfiguredScale( const COMMON_SETTINGS& aConfig )
48{
49 std::optional<double> scale;
50 double canvas_scale = aConfig.m_Appearance.canvas_scale;
51
52 if( canvas_scale > 0.0 )
53 {
54 scale = canvas_scale;
55 }
56
57 return scale;
58}
59
60
67static std::optional<double> getEnvironmentScale()
68{
69 const wxPortId port_id = wxPlatformInfo::Get().GetPortId();
70 std::optional<double> scale;
71
72 if( port_id == wxPORT_GTK )
73 {
74 // Under GTK, the user can use GDK_SCALE to force the scaling
75 scale = ENV_VAR::GetEnvVar<double>( wxS( "GDK_SCALE" ) );
76 }
77
78 return scale;
79}
80
81
82DPI_SCALING_COMMON::DPI_SCALING_COMMON( COMMON_SETTINGS* aConfig, const wxWindow* aWindow ) :
83 m_config( aConfig ),
84 m_window( aWindow )
85{
86}
87
88
90{
91 std::optional<double> val;
92 wxString src;
93
94 if( m_config )
95 {
97 src = wxS( "config" );
98 }
99
100 if( !val )
101 {
102 val = getEnvironmentScale();
103 src = wxS( "env" );
104 }
105
106 if( !val && m_window )
107 {
108 // Use the native WX reporting.
109 // On Linux, this will not work until WX 3.2 and GTK >= 3.10
110 // Otherwise it returns 1.0
112 src = wxS( "platform" );
113 }
114
115 if( !val )
116 {
117 // Nothing else we can do, give it a default value
118 val = GetDefaultScaleFactor();
119 src = wxS( "default" );
120 }
121
122 wxLogTrace( traceHiDpi, wxS( "Scale factor (%s): %f" ), src, *val );
123
124 return *val;
125}
126
127
129{
130 std::optional<double> val;
131 wxString src;
132
133 if( m_config )
134 {
136 src = wxS( "config" );
137 }
138
139 if( !val )
140 {
141 val = getEnvironmentScale();
142 src = wxS( "env" );
143 }
144
145 if( !val && m_window )
146 {
147 // Use the native WX reporting.
148 // On Linux, this will not work until WX 3.2 and GTK >= 3.10
149 // Otherwise it returns 1.0
151 src = wxS( "platform" );
152 }
153
154 if( !val )
155 {
156 // Nothing else we can do, give it a default value
157 val = GetDefaultScaleFactor();
158 src = wxS( "default" );
159 }
160
161 wxLogTrace( traceHiDpi, wxS( "Content scale factor (%s): %f" ), src, *val );
162
163 return *val;
164}
165
166
168{
169 if( m_config == nullptr )
170 {
171 // No configuration given, so has to be automatic scaling
172 return true;
173 }
174
175 const bool automatic = getKiCadConfiguredScale( *m_config ) == std::nullopt;
176 wxLogTrace( traceHiDpi, wxS( "Scale is automatic: %d" ), automatic );
177 return automatic;
178}
179
180
181void DPI_SCALING_COMMON::SetDpiConfig( bool aAuto, double aValue )
182{
183 wxCHECK_RET( m_config != nullptr, wxS( "Setting DPI config without a config store." ) );
184
185 const double value = aAuto ? 0.0 : aValue;
186
187 m_config->m_Appearance.canvas_scale = value;
188}
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.
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.
KICOMMON_API std::optional< VAL_TYPE > GetEnvVar(const wxString &aEnvVarName)
Get an environment variable as a specific type, if set correctly.
double GetPixelScaleFactor(const wxWindow *aWindow)
Tries to determine the pixel scaling factor currently in use for the window.
Definition wxgtk/ui.cpp:274
double GetContentScaleFactor(const wxWindow *aWindow)
Tries to determine the content scaling factor currently in use for the window.
Definition wxgtk/ui.cpp:287
const int scale