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.

177 lines
4.3 KiB

  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. #include <dpi_scaling.h>
  24. #include <core/optional.h>
  25. #include <env_vars.h>
  26. #include <pgm_base.h>
  27. #include <wx/config.h>
  28. #include <wx/log.h>
  29. /**
  30. * Flag to enable trace for HiDPI scaling factors
  31. *
  32. * Use "KICAD_TRACE_HIGH_DPI" to enable.
  33. *
  34. * @ingroup trace_env_vars
  35. */
  36. const wxChar* const traceHiDpi = wxT( "KICAD_TRACE_HIGH_DPI" );
  37. /**
  38. * Get a user-configured scale factor from KiCad config file
  39. *
  40. * @return the scale factor, if set
  41. */
  42. static OPT<double> getKiCadConfiguredScale( const wxConfigBase& aConfig )
  43. {
  44. OPT<double> scale;
  45. double canvas_scale = 0.0;
  46. aConfig.Read( CANVAS_SCALE_KEY, &canvas_scale, 0.0 );
  47. if( canvas_scale > 0.0 )
  48. {
  49. scale = canvas_scale;
  50. }
  51. if( scale )
  52. {
  53. wxLogTrace( traceHiDpi, "Scale factor (configured): %f", *scale );
  54. }
  55. return scale;
  56. }
  57. /**
  58. * Get the toolkit scale factor from a user-set environment variable
  59. * (for example GDK_SCALE on GTK).
  60. *
  61. * @return the scale factor, if set
  62. */
  63. static OPT<double> getEnviromentScale()
  64. {
  65. const wxPortId port_id = wxPlatformInfo::Get().GetPortId();
  66. OPT<double> scale;
  67. if( port_id == wxPORT_GTK )
  68. {
  69. // Under GTK, the user can use GDK_SCALE to force the scaling
  70. scale = GetEnvVar<double>( "GDK_SCALE" );
  71. }
  72. if( scale )
  73. {
  74. wxLogTrace( traceHiDpi, "Scale factor (environment): %f", *scale );
  75. }
  76. return scale;
  77. }
  78. DPI_SCALING::DPI_SCALING( wxConfigBase* aConfig, const wxWindow* aWindow )
  79. : m_config( aConfig ), m_window( aWindow )
  80. {
  81. }
  82. double DPI_SCALING::GetScaleFactor() const
  83. {
  84. OPT<double> val;
  85. if( m_config )
  86. {
  87. val = getKiCadConfiguredScale( *m_config );
  88. }
  89. if( !val )
  90. {
  91. val = getEnviromentScale();
  92. }
  93. if( !val && m_window )
  94. {
  95. // Use the native WX reporting.
  96. // On Linux, this will not work until WX 3.2 and GTK >= 3.10
  97. // Otherwise it returns 1.0
  98. val = m_window->GetContentScaleFactor();
  99. wxLogTrace( traceHiDpi, "Scale factor (WX): %f", *val );
  100. }
  101. if( !val )
  102. {
  103. // Nothing else we can do, give it a default value
  104. val = GetDefaultScaleFactor();
  105. wxLogTrace( traceHiDpi, "Scale factor (default): %f", *val );
  106. }
  107. return *val;
  108. }
  109. bool DPI_SCALING::GetCanvasIsAutoScaled() const
  110. {
  111. if( m_config == nullptr )
  112. {
  113. // No configuration given, so has to be automatic scaling
  114. return true;
  115. }
  116. const bool automatic = getKiCadConfiguredScale( *m_config ) == boost::none;
  117. wxLogTrace( traceHiDpi, "Scale is automatic: %d", automatic );
  118. return automatic;
  119. }
  120. void DPI_SCALING::SetDpiConfig( bool aAuto, double aValue )
  121. {
  122. wxCHECK_RET( m_config != nullptr, "Setting DPI config without a config store." );
  123. const double value = aAuto ? 0.0 : aValue;
  124. m_config->Write( CANVAS_SCALE_KEY, value );
  125. }
  126. double DPI_SCALING::GetMaxScaleFactor()
  127. {
  128. // displays with higher than 4.0 DPI are not really going to be useful
  129. // for KiCad (even an 8k display would be effectively only ~1080p at 4x)
  130. return 6.0;
  131. }
  132. double DPI_SCALING::GetMinScaleFactor()
  133. {
  134. // scales under 1.0 don't make sense from a HiDPI perspective
  135. return 1.0;
  136. }
  137. double DPI_SCALING::GetDefaultScaleFactor()
  138. {
  139. // no scaling => 1.0
  140. return 1.0;
  141. }