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 ), m_window( aWindow )
88{
89}
90
91
93{
94 std::optional<double> val;
95 wxString src;
96
97 if( m_config )
98 {
100 src = wxS( "config" );
101 }
102
103 if( !val )
104 {
105 val = getEnvironmentScale();
106 src = wxS( "env" );
107 }
108
109 if( !val && m_window )
110 {
111 // Use the native WX reporting.
112 // On Linux, this will not work until WX 3.2 and GTK >= 3.10
113 // Otherwise it returns 1.0
115 src = wxS( "platform" );
116 }
117
118 if( !val )
119 {
120 // Nothing else we can do, give it a default value
121 val = GetDefaultScaleFactor();
122 src = wxS( "default" );
123 }
124
125 wxLogTrace( traceHiDpi, wxS( "Scale factor (%s): %f" ), src, *val );
126
127 return *val;
128}
129
130
132{
133 std::optional<double> val;
134 wxString src;
135
136 if( m_config )
137 {
139 src = wxS( "config" );
140 }
141
142 if( !val )
143 {
144 val = getEnvironmentScale();
145 src = wxS( "env" );
146 }
147
148 if( !val && m_window )
149 {
150 // Use the native WX reporting.
151 // On Linux, this will not work until WX 3.2 and GTK >= 3.10
152 // Otherwise it returns 1.0
154 src = wxS( "platform" );
155 }
156
157 if( !val )
158 {
159 // Nothing else we can do, give it a default value
160 val = GetDefaultScaleFactor();
161 src = wxS( "default" );
162 }
163
164 wxLogTrace( traceHiDpi, wxS( "Content scale factor (%s): %f" ), src, *val );
165
166 return *val;
167}
168
169
171{
172 if( m_config == nullptr )
173 {
174 // No configuration given, so has to be automatic scaling
175 return true;
176 }
177
178 const bool automatic = getKiCadConfiguredScale( *m_config ) == std::nullopt;
179 wxLogTrace( traceHiDpi, wxS( "Scale is automatic: %d" ), automatic );
180 return automatic;
181}
182
183
184void DPI_SCALING_COMMON::SetDpiConfig( bool aAuto, double aValue )
185{
186 wxCHECK_RET( m_config != nullptr, wxS( "Setting DPI config without a config store." ) );
187
188 const double value = aAuto ? 0.0 : aValue;
189
191}
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: gtk/ui.cpp:175
double GetContentScaleFactor(const wxWindow *aWindow)
Tries to determine the content scaling factor currently in use for the window.
Definition: gtk/ui.cpp:188
const int scale