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.

122 lines
3.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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 "widgets/wx_grid_autosizer.h"
  24. #include <optional>
  25. #include <wx/settings.h>
  26. WX_GRID_AUTOSIZER::WX_GRID_AUTOSIZER( wxGrid& aGrid, COL_MIN_WIDTHS aAutosizedCols,
  27. unsigned aFlexibleCol ) :
  28. m_grid( aGrid ), m_autosizedCols( std::move( aAutosizedCols ) ),
  29. m_flexibleCol( aFlexibleCol )
  30. {
  31. const int colCount = m_grid.GetNumberCols();
  32. for( const auto& [colIndex, width] : m_autosizedCols )
  33. {
  34. wxASSERT_MSG( colIndex < colCount, "Autosized column does not exist in grid" );
  35. }
  36. wxASSERT_MSG( m_flexibleCol < colCount, "Flexible column index does not exist in grid" );
  37. m_grid.Bind( wxEVT_UPDATE_UI,
  38. [this]( wxUpdateUIEvent& aEvent )
  39. {
  40. recomputeGridWidths();
  41. } );
  42. m_grid.Bind( wxEVT_SIZE,
  43. [this]( wxSizeEvent& aEvent )
  44. {
  45. onSizeEvent( aEvent );
  46. } );
  47. // Handles the case when the user changes the cell content to be longer than the
  48. // current column size
  49. m_grid.Bind( wxEVT_GRID_CELL_CHANGED,
  50. [this]( wxGridEvent& aEvent )
  51. {
  52. m_gridWidthsDirty = true;
  53. aEvent.Skip();
  54. } );
  55. }
  56. void WX_GRID_AUTOSIZER::recomputeGridWidths()
  57. {
  58. if( m_gridWidthsDirty )
  59. {
  60. const int width =
  61. m_grid.GetClientRect().GetWidth() - wxSystemSettings::GetMetric( wxSYS_VSCROLL_X );
  62. std::optional<int> flexibleMinWidth;
  63. for( const auto& [colIndex, minWidth] : m_autosizedCols )
  64. {
  65. m_grid.AutoSizeColumn( colIndex );
  66. const int colSize = m_grid.GetColSize( colIndex );
  67. int minWidthScaled = m_grid.FromDIP( minWidth );
  68. m_grid.SetColSize( colIndex, std::max( minWidthScaled, colSize ) );
  69. if( colIndex == m_flexibleCol )
  70. {
  71. flexibleMinWidth = minWidthScaled;
  72. }
  73. }
  74. // Gather all the widths except the flexi one
  75. int nonFlexibleWidth = 0;
  76. for( int i = 0; i < m_grid.GetNumberCols(); ++i )
  77. {
  78. if( i != m_flexibleCol )
  79. {
  80. nonFlexibleWidth += m_grid.GetColSize( i );
  81. }
  82. }
  83. m_grid.SetColSize( m_flexibleCol,
  84. std::max( flexibleMinWidth.value_or( 0 ), width - nonFlexibleWidth ) );
  85. // Store the state for next time
  86. m_gridWidth = m_grid.GetSize().GetX();
  87. m_gridWidthsDirty = false;
  88. }
  89. }
  90. void WX_GRID_AUTOSIZER::onSizeEvent( wxSizeEvent& aEvent )
  91. {
  92. const int width = aEvent.GetSize().GetX();
  93. if( width != m_gridWidth )
  94. {
  95. m_gridWidthsDirty = true;
  96. }
  97. aEvent.Skip();
  98. }