You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
4.8 KiB

1 year ago
1 year ago
1 year ago
  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. #include <dpi_scaling_common.h>
  24. #include <optional>
  25. #include <env_vars.h>
  26. #include <settings/common_settings.h>
  27. #include <kiplatform/ui.h>
  28. #include <wx/log.h>
  29. #include <wx/window.h>
  30. /**
  31. * Flag to enable trace for HiDPI scaling factors
  32. *
  33. * Use "KICAD_TRACE_HIGH_DPI" to enable.
  34. *
  35. * @ingroup trace_env_vars
  36. */
  37. const wxChar* const traceHiDpi = wxT( "KICAD_TRACE_HIGH_DPI" );
  38. /**
  39. * Get a user-configured scale factor from KiCad config file
  40. *
  41. * @return the scale factor, if set
  42. */
  43. static std::optional<double> getKiCadConfiguredScale( const COMMON_SETTINGS& aConfig )
  44. {
  45. std::optional<double> scale;
  46. double canvas_scale = aConfig.m_Appearance.canvas_scale;
  47. if( canvas_scale > 0.0 )
  48. {
  49. scale = canvas_scale;
  50. }
  51. return scale;
  52. }
  53. /**
  54. * Get the toolkit scale factor from a user-set environment variable
  55. * (for example GDK_SCALE on GTK).
  56. *
  57. * @return the scale factor, if set
  58. */
  59. static std::optional<double> getEnvironmentScale()
  60. {
  61. const wxPortId port_id = wxPlatformInfo::Get().GetPortId();
  62. std::optional<double> scale;
  63. if( port_id == wxPORT_GTK )
  64. {
  65. // Under GTK, the user can use GDK_SCALE to force the scaling
  66. scale = ENV_VAR::GetEnvVar<double>( wxS( "GDK_SCALE" ) );
  67. }
  68. return scale;
  69. }
  70. DPI_SCALING_COMMON::DPI_SCALING_COMMON( COMMON_SETTINGS* aConfig, const wxWindow* aWindow ) :
  71. m_config( aConfig ),
  72. m_window( aWindow )
  73. {
  74. }
  75. double DPI_SCALING_COMMON::GetScaleFactor() const
  76. {
  77. std::optional<double> val;
  78. wxString src;
  79. if( m_config )
  80. {
  81. val = getKiCadConfiguredScale( *m_config );
  82. src = wxS( "config" );
  83. }
  84. if( !val )
  85. {
  86. val = getEnvironmentScale();
  87. src = wxS( "env" );
  88. }
  89. if( !val && m_window )
  90. {
  91. // Use the native WX reporting.
  92. // On Linux, this will not work until WX 3.2 and GTK >= 3.10
  93. // Otherwise it returns 1.0
  94. val = KIPLATFORM::UI::GetPixelScaleFactor( m_window );
  95. src = wxS( "platform" );
  96. }
  97. if( !val )
  98. {
  99. // Nothing else we can do, give it a default value
  100. val = GetDefaultScaleFactor();
  101. src = wxS( "default" );
  102. }
  103. wxLogTrace( traceHiDpi, wxS( "Scale factor (%s): %f" ), src, *val );
  104. return *val;
  105. }
  106. double DPI_SCALING_COMMON::GetContentScaleFactor() const
  107. {
  108. std::optional<double> val;
  109. wxString src;
  110. if( m_config )
  111. {
  112. val = getKiCadConfiguredScale( *m_config );
  113. src = wxS( "config" );
  114. }
  115. if( !val )
  116. {
  117. val = getEnvironmentScale();
  118. src = wxS( "env" );
  119. }
  120. if( !val && m_window )
  121. {
  122. // Use the native WX reporting.
  123. // On Linux, this will not work until WX 3.2 and GTK >= 3.10
  124. // Otherwise it returns 1.0
  125. val = KIPLATFORM::UI::GetContentScaleFactor( m_window );
  126. src = wxS( "platform" );
  127. }
  128. if( !val )
  129. {
  130. // Nothing else we can do, give it a default value
  131. val = GetDefaultScaleFactor();
  132. src = wxS( "default" );
  133. }
  134. wxLogTrace( traceHiDpi, wxS( "Content scale factor (%s): %f" ), src, *val );
  135. return *val;
  136. }
  137. bool DPI_SCALING_COMMON::GetCanvasIsAutoScaled() const
  138. {
  139. if( m_config == nullptr )
  140. {
  141. // No configuration given, so has to be automatic scaling
  142. return true;
  143. }
  144. const bool automatic = getKiCadConfiguredScale( *m_config ) == std::nullopt;
  145. wxLogTrace( traceHiDpi, wxS( "Scale is automatic: %d" ), automatic );
  146. return automatic;
  147. }
  148. void DPI_SCALING_COMMON::SetDpiConfig( bool aAuto, double aValue )
  149. {
  150. wxCHECK_RET( m_config != nullptr, wxS( "Setting DPI config without a config store." ) );
  151. const double value = aAuto ? 0.0 : aValue;
  152. m_config->m_Appearance.canvas_scale = value;
  153. }