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 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
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
38 for( const auto& [colIndex, width] : m_autosizedCols )
39 {
40 wxASSERT_MSG( colIndex < colCount, "Autosized column does not exist in grid" );
41 }
42
43 wxASSERT_MSG( m_flexibleCol < colCount, "Flexible column index does not exist in grid" );
44
45 m_grid.Bind( wxEVT_UPDATE_UI,
46 [this]( wxUpdateUIEvent& aEvent )
47 {
49 } );
50
51 m_grid.Bind( wxEVT_SIZE,
52 [this]( wxSizeEvent& aEvent )
53 {
54 onSizeEvent( aEvent );
55 } );
56
57 // Handles the case when the user changes the cell content to be longer than the
58 // current column size
59 m_grid.Bind( wxEVT_GRID_CELL_CHANGED,
60 [this]( wxGridEvent& aEvent )
61 {
62 m_gridWidthsDirty = true;
63 aEvent.Skip();
64 } );
65}
66
67
69{
71 {
72 const int width =
73 m_grid.GetClientRect().GetWidth() - wxSystemSettings::GetMetric( wxSYS_VSCROLL_X );
74
75 std::optional<int> flexibleMinWidth;
76
77 for( const auto& [colIndex, minWidth] : m_autosizedCols )
78 {
79 m_grid.AutoSizeColumn( colIndex );
80 const int colSize = m_grid.GetColSize( colIndex );
81 m_grid.SetColSize( colIndex, std::max( minWidth, colSize ) );
82
83 if( colIndex == m_flexibleCol )
84 {
85 flexibleMinWidth = minWidth;
86 }
87 }
88
89 // Gather all the widths except the flexi one
90 int nonFlexibleWidth = 0;
91
92 for( int i = 0; i < m_grid.GetNumberCols(); ++i )
93 {
94 if( i != m_flexibleCol )
95 {
96 nonFlexibleWidth += m_grid.GetColSize( i );
97 }
98 }
99
100 m_grid.SetColSize( m_flexibleCol,
101 std::max( flexibleMinWidth.value_or( 0 ), width - nonFlexibleWidth ) );
102
103 // Store the state for next time
104 m_gridWidth = m_grid.GetSize().GetX();
105 m_gridWidthsDirty = false;
106 }
107}
108
109
110void WX_GRID_AUTOSIZER::onSizeEvent( wxSizeEvent& aEvent )
111{
112 const int width = aEvent.GetSize().GetX();
113
114 if( width != m_gridWidth )
115 {
116 m_gridWidthsDirty = true;
117 }
118
119 aEvent.Skip();
120}
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.