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 CHANGELOG.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.h>
25 
26 #include <core/optional.h>
27 
28 #include <env_vars.h>
29 #include <pgm_base.h>
30 
31 #include <wx/config.h>
32 #include <wx/log.h>
33 
34 
42 const wxChar* const traceHiDpi = wxT( "KICAD_TRACE_HIGH_DPI" );
43 
44 
50 static OPT<double> getKiCadConfiguredScale( const wxConfigBase& aConfig )
51 {
53  double canvas_scale = 0.0;
54  aConfig.Read( CANVAS_SCALE_KEY, &canvas_scale, 0.0 );
55 
56  if( canvas_scale > 0.0 )
57  {
58  scale = canvas_scale;
59  }
60 
61  if( scale )
62  {
63  wxLogTrace( traceHiDpi, "Scale factor (configured): %f", *scale );
64  }
65 
66  return scale;
67 }
68 
69 
77 {
78  const wxPortId port_id = wxPlatformInfo::Get().GetPortId();
80 
81  if( port_id == wxPORT_GTK )
82  {
83  // Under GTK, the user can use GDK_SCALE to force the scaling
84  scale = GetEnvVar<double>( "GDK_SCALE" );
85  }
86 
87  if( scale )
88  {
89  wxLogTrace( traceHiDpi, "Scale factor (environment): %f", *scale );
90  }
91 
92  return scale;
93 }
94 
95 
96 DPI_SCALING::DPI_SCALING( wxConfigBase* aConfig, const wxWindow* aWindow )
97  : m_config( aConfig ), m_window( aWindow )
98 {
99 }
100 
101 
103 {
104  OPT<double> val;
105 
106  if( m_config )
107  {
109  }
110 
111  if( !val )
112  {
113  val = getEnviromentScale();
114  }
115 
116  if( !val && m_window )
117  {
118  // Use the native WX reporting.
119  // On Linux, this will not work until WX 3.2 and GTK >= 3.10
120  // Otherwise it returns 1.0
121  val = m_window->GetContentScaleFactor();
122  wxLogTrace( traceHiDpi, "Scale factor (WX): %f", *val );
123  }
124 
125  if( !val )
126  {
127  // Nothing else we can do, give it a default value
128  val = GetDefaultScaleFactor();
129  wxLogTrace( traceHiDpi, "Scale factor (default): %f", *val );
130  }
131 
132  return *val;
133 }
134 
135 
137 {
138  if( m_config == nullptr )
139  {
140  // No configuration given, so has to be automatic scaling
141  return true;
142  }
143 
144  const bool automatic = getKiCadConfiguredScale( *m_config ) == boost::none;
145  wxLogTrace( traceHiDpi, "Scale is automatic: %d", automatic );
146  return automatic;
147 }
148 
149 
150 void DPI_SCALING::SetDpiConfig( bool aAuto, double aValue )
151 {
152  wxCHECK_RET( m_config != nullptr, "Setting DPI config without a config store." );
153 
154  const double value = aAuto ? 0.0 : aValue;
155 
156  m_config->Write( CANVAS_SCALE_KEY, value );
157 }
158 
159 
161 {
162  // displays with higher than 4.0 DPI are not really going to be useful
163  // for KiCad (even an 8k display would be effectively only ~1080p at 4x)
164  return 6.0;
165 }
166 
167 
169 {
170  // scales under 1.0 don't make sense from a HiDPI perspective
171  return 1.0;
172 }
173 
175 {
176  // no scaling => 1.0
177  return 1.0;
178 }
static double GetDefaultScaleFactor()
Get the "default" scaling factor to use if not other config is available.
double GetScaleFactor() const
Get the DPI scale from all known sources in order:
static OPT< double > getEnviromentScale()
Get the toolkit scale factor from a user-set environment variable (for example GDK_SCALE on GTK).
Definition: dpi_scaling.cpp:76
void SetDpiConfig(bool aAuto, double aValue)
Set the common DPI config in a given config object.
static double GetMaxScaleFactor()
static OPT< double > getKiCadConfiguredScale(const wxConfigBase &aConfig)
Get a user-configured scale factor from KiCad config file.
Definition: dpi_scaling.cpp:50
DPI_SCALING(wxConfigBase *aConfig, const wxWindow *aWindow)
Construct a DPI scale provider.
Definition: dpi_scaling.cpp:96
static double GetMinScaleFactor()
const wxChar *const traceHiDpi
Flag to enable trace for HiDPI scaling factors.
Definition: dpi_scaling.cpp:42
const int scale
see class PGM_BASE
boost::optional< T > OPT
Definition: optional.h:7
#define CANVAS_SCALE_KEY
Definition: pgm_base.h:48
Functions related to environment variables, including help functions.
const wxWindow * m_window
The WX window to use for WX's automatic DPI checking.
Definition: dpi_scaling.h:99
wxConfigBase * m_config
The configuration object to use to get/set user setting.
Definition: dpi_scaling.h:94
bool GetCanvasIsAutoScaled() const
Is the current value auto scaled, or is it user-set in the config.