KiCad PCB EDA Suite
Loading...
Searching...
No Matches
wx_grid_autosizer.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) 2024 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
25
26#include <optional>
27
28#include <wx/settings.h>
29
30
32 unsigned aFlexibleCol ) :
33 m_grid( aGrid ), m_autosizedCols( std::move( aAutosizedCols ) ),
34 m_flexibleCol( aFlexibleCol )
35{
36 const int colCount = m_grid.GetNumberCols();
37 for( const auto& [colIndex, width] : m_autosizedCols )
38 {
39 wxASSERT_MSG( colIndex < colCount, "Autosized column does not exist in grid" );
40 }
41 wxASSERT_MSG( m_flexibleCol < colCount, "Flexible column index does not exist in grid" );
42
43 m_grid.Bind( wxEVT_UPDATE_UI,
44 [this]( wxUpdateUIEvent& aEvent )
45 {
47 } );
48
49 m_grid.Bind( wxEVT_SIZE,
50 [this]( wxSizeEvent& aEvent )
51 {
52 onSizeEvent( aEvent );
53 } );
54
55 // Handles the case when the user changes the cell content to be longer than the
56 // current column size
57 m_grid.Bind( wxEVT_GRID_CELL_CHANGED,
58 [this]( wxGridEvent& aEvent )
59 {
60 m_gridWidthsDirty = true;
61 aEvent.Skip();
62 } );
63}
64
66{
68 {
69 const int width =
70 m_grid.GetClientRect().GetWidth() - wxSystemSettings::GetMetric( wxSYS_VSCROLL_X );
71
72 std::optional<int> flexibleMinWidth;
73 for( const auto& [colIndex, minWidth] : m_autosizedCols )
74 {
75 m_grid.AutoSizeColumn( colIndex );
76 const int colSize = m_grid.GetColSize( colIndex );
77 m_grid.SetColSize( colIndex, std::max( minWidth, colSize ) );
78
79 if( colIndex == m_flexibleCol )
80 {
81 flexibleMinWidth = minWidth;
82 }
83 }
84
85 // Gather all the widths except the flexi one
86 int nonFlexibleWidth = 0;
87
88 for( int i = 0; i < m_grid.GetNumberCols(); ++i )
89 {
90 if( i != m_flexibleCol )
91 {
92 nonFlexibleWidth += m_grid.GetColSize( i );
93 }
94 }
95
96 m_grid.SetColSize( m_flexibleCol,
97 std::max( flexibleMinWidth.value_or( 0 ), width - nonFlexibleWidth ) );
98
99 // Store the state for next time
100 m_gridWidth = m_grid.GetSize().GetX();
101 m_gridWidthsDirty = false;
102 }
103}
104
105void WX_GRID_AUTOSIZER::onSizeEvent( wxSizeEvent& aEvent )
106{
107 const int width = aEvent.GetSize().GetX();
108 if( width != m_gridWidth )
109 {
110 m_gridWidthsDirty = true;
111 }
112 aEvent.Skip();
113}
WX_GRID_AUTOSIZER(wxGrid &aGrid, COL_MIN_WIDTHS aAutosizedCols, unsigned aFlexibleCol)
std::map< int, int > COL_MIN_WIDTHS
Map of column indices to minimum widths.
COL_MIN_WIDTHS m_autosizedCols
void onSizeEvent(wxSizeEvent &aEvent)
STL namespace.