KiCad PCB EDA Suite
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 if( scale )
61 {
62 wxLogTrace( traceHiDpi, "Scale factor (configured): %f", *scale );
63 }
64
65 return scale;
66}
67
68
75static std::optional<double> getEnvironmentScale()
76{
77 const wxPortId port_id = wxPlatformInfo::Get().GetPortId();
78 std::optional<double> scale;
79
80 if( port_id == wxPORT_GTK )
81 {
82 // Under GTK, the user can use GDK_SCALE to force the scaling
83 scale = ENV_VAR::GetEnvVar<double>( "GDK_SCALE" );
84 }
85
86 if( scale )
87 {
88 wxLogTrace( traceHiDpi, "Scale factor (environment): %f", *scale );
89 }
90
91 return scale;
92}
93
94
95DPI_SCALING::DPI_SCALING( COMMON_SETTINGS* aConfig, const wxWindow* aWindow )
96 : m_config( aConfig ), m_window( aWindow )
97{
98}
99
100
102{
103 std::optional<double> val;
104
105 if( m_config )
106 {
108 }
109
110 if( !val )
111 {
112 val = getEnvironmentScale();
113 }
114
115 if( !val && m_window )
116 {
117 // Use the native WX reporting.
118 // On Linux, this will not work until WX 3.2 and GTK >= 3.10
119 // Otherwise it returns 1.0
121 wxLogTrace( traceHiDpi, "Scale factor (WX): %f", *val );
122 }
123
124 if( !val )
125 {
126 // Nothing else we can do, give it a default value
127 val = GetDefaultScaleFactor();
128 wxLogTrace( traceHiDpi, "Scale factor (default): %f", *val );
129 }
130
131 return *val;
132}
133
134
136{
137 if( m_config == nullptr )
138 {
139 // No configuration given, so has to be automatic scaling
140 return true;
141 }
142
143 const bool automatic = getKiCadConfiguredScale( *m_config ) == std::nullopt;
144 wxLogTrace( traceHiDpi, "Scale is automatic: %d", automatic );
145 return automatic;
146}
147
148
149void DPI_SCALING::SetDpiConfig( bool aAuto, double aValue )
150{
151 wxCHECK_RET( m_config != nullptr, "Setting DPI config without a config store." );
152
153 const double value = aAuto ? 0.0 : aValue;
154
156}
157
158
160{
161 // displays with higher than 4.0 DPI are not really going to be useful
162 // for KiCad (even an 8k display would be effectively only ~1080p at 4x)
163 return 6.0;
164}
165
166
168{
169 // scales under 1.0 don't make sense from a HiDPI perspective
170 return 1.0;
171}
172
174{
175 // no scaling => 1.0
176 return 1.0;
177}
APPEARANCE m_Appearance
double GetScaleFactor() const
Get the DPI scale from all known sources in order:
void SetDpiConfig(bool aAuto, double aValue)
Set the common DPI config in a given config object.
DPI_SCALING(COMMON_SETTINGS *aConfig, const wxWindow *aWindow)
Construct a DPI scale provider.
Definition: dpi_scaling.cpp:95
COMMON_SETTINGS * m_config
The configuration object to use to get/set user setting.
Definition: dpi_scaling.h:95
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:100
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:75
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 GetSystemScaleFactor(const wxWindow *aWindow)
Tries to determine the system scaling factor currently in use for the window.
Definition: gtk/ui.cpp:113
const int scale