KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dpi_scaling.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 <gal/dpi_scaling.h>
25
26#include <optional>
27
28#include <env_vars.h>
30#include <kiplatform/ui.h>
31
32#include <wx/log.h>
33
34
42const wxChar* const traceHiDpi = wxT( "KICAD_TRACE_HIGH_DPI" );
43
44
50static std::optional<double> getKiCadConfiguredScale( const COMMON_SETTINGS& aConfig )
51{
52 std::optional<double> scale;
53 double canvas_scale = aConfig.m_Appearance.canvas_scale;
54
55 if( canvas_scale > 0.0 )
56 {
57 scale = canvas_scale;
58 }
59
60 return scale;
61}
62
63
70static std::optional<double> getEnvironmentScale()
71{
72 const wxPortId port_id = wxPlatformInfo::Get().GetPortId();
73 std::optional<double> scale;
74
75 if( port_id == wxPORT_GTK )
76 {
77 // Under GTK, the user can use GDK_SCALE to force the scaling
78 scale = ENV_VAR::GetEnvVar<double>( wxS( "GDK_SCALE" ) );
79 }
80
81 return scale;
82}
83
84
85DPI_SCALING::DPI_SCALING( COMMON_SETTINGS* aConfig, const wxWindow* aWindow )
86 : m_config( aConfig ), m_window( aWindow )
87{
88}
89
90
92{
93 std::optional<double> val;
94 wxString src;
95
96 if( m_config )
97 {
99 src = wxS( "config" );
100 }
101
102 if( !val )
103 {
104 val = getEnvironmentScale();
105 src = wxS( "env" );
106 }
107
108 if( !val && m_window )
109 {
110 // Use the native WX reporting.
111 // On Linux, this will not work until WX 3.2 and GTK >= 3.10
112 // Otherwise it returns 1.0
114 src = wxS( "platform" );
115 }
116
117 if( !val )
118 {
119 // Nothing else we can do, give it a default value
120 val = GetDefaultScaleFactor();
121 src = wxS( "default" );
122 }
123
124 wxLogTrace( traceHiDpi, wxS( "Scale factor (%s): %f" ), src, *val );
125
126 return *val;
127}
128
129
131{
132 std::optional<double> val;
133 wxString src;
134
135 if( m_config )
136 {
138 src = wxS( "config" );
139 }
140
141 if( !val )
142 {
143 val = getEnvironmentScale();
144 src = wxS( "env" );
145 }
146
147 if( !val && m_window )
148 {
149 // Use the native WX reporting.
150 // On Linux, this will not work until WX 3.2 and GTK >= 3.10
151 // Otherwise it returns 1.0
153 src = wxS( "platform" );
154 }
155
156 if( !val )
157 {
158 // Nothing else we can do, give it a default value
159 val = GetDefaultScaleFactor();
160 src = wxS( "default" );
161 }
162
163 wxLogTrace( traceHiDpi, wxS( "Content scale factor (%s): %f" ), src, *val );
164
165 return *val;
166}
167
168
170{
171 if( m_config == nullptr )
172 {
173 // No configuration given, so has to be automatic scaling
174 return true;
175 }
176
177 const bool automatic = getKiCadConfiguredScale( *m_config ) == std::nullopt;
178 wxLogTrace( traceHiDpi, wxS( "Scale is automatic: %d" ), automatic );
179 return automatic;
180}
181
182
183void DPI_SCALING::SetDpiConfig( bool aAuto, double aValue )
184{
185 wxCHECK_RET( m_config != nullptr, wxS( "Setting DPI config without a config store." ) );
186
187 const double value = aAuto ? 0.0 : aValue;
188
190}
191
192
194{
195 // displays with higher than 4.0 DPI are not really going to be useful
196 // for KiCad (even an 8k display would be effectively only ~1080p at 4x)
197 return 6.0;
198}
199
200
202{
203 // scales under 1.0 don't make sense from a HiDPI perspective
204 return 1.0;
205}
206
208{
209 // no scaling => 1.0
210 return 1.0;
211}
APPEARANCE m_Appearance
double GetScaleFactor() const
Get the DPI scale from all known sources in order:
Definition: dpi_scaling.cpp:91
void SetDpiConfig(bool aAuto, double aValue)
Set the common DPI config in a given config object.
double GetContentScaleFactor() const
Get the content scale factor, which may be different from the scale factor on some platforms.
DPI_SCALING(COMMON_SETTINGS *aConfig, const wxWindow *aWindow)
Construct a DPI scale provider.
Definition: dpi_scaling.cpp:85
COMMON_SETTINGS * m_config
The configuration object to use to get/set user setting.
Definition: dpi_scaling.h:103
static double GetDefaultScaleFactor()
Get the "default" scaling factor to use if not other config is available.
const wxWindow * m_window
The WX window to use for WX's automatic DPI checking.
Definition: dpi_scaling.h:108
static double GetMaxScaleFactor()
bool GetCanvasIsAutoScaled() const
Is the current value auto scaled, or is it user-set in the config.
static double GetMinScaleFactor()
static std::optional< double > getKiCadConfiguredScale(const COMMON_SETTINGS &aConfig)
Get a user-configured scale factor from KiCad config file.
Definition: dpi_scaling.cpp:50
static std::optional< double > getEnvironmentScale()
Get the toolkit scale factor from a user-set environment variable (for example GDK_SCALE on GTK).
Definition: dpi_scaling.cpp:70
Functions related to environment variables, including help functions.
const wxChar *const traceHiDpi
Flag to enable trace for HiDPI scaling factors.
Definition: dpi_scaling.cpp:42
double GetPixelScaleFactor(const wxWindow *aWindow)
Tries to determine the pixel scaling factor currently in use for the window.
Definition: gtk/ui.cpp:152
double GetContentScaleFactor(const wxWindow *aWindow)
Tries to determine the content scaling factor currently in use for the window.
Definition: gtk/ui.cpp:165
const int scale