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