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
82 int minWidthScaled = m_grid.FromDIP( minWidth );
83 m_grid.SetColSize( colIndex, std::max( minWidthScaled, colSize ) );
84
85 if( colIndex == m_flexibleCol )
86 {
87 flexibleMinWidth = minWidthScaled;
88 }
89 }
90
91 // Gather all the widths except the flexi one
92 int nonFlexibleWidth = 0;
93
94 for( int i = 0; i < m_grid.GetNumberCols(); ++i )
95 {
96 if( i != m_flexibleCol )
97 {
98 nonFlexibleWidth += m_grid.GetColSize( i );
99 }
100 }
101
102 m_grid.SetColSize( m_flexibleCol,
103 std::max( flexibleMinWidth.value_or( 0 ), width - nonFlexibleWidth ) );
104
105 // Store the state for next time
106 m_gridWidth = m_grid.GetSize().GetX();
107 m_gridWidthsDirty = false;
108 }
109}
110
111
112void WX_GRID_AUTOSIZER::onSizeEvent( wxSizeEvent& aEvent )
113{
114 const int width = aEvent.GetSize().GetX();
115
116 if( width != m_gridWidth )
117 {
118 m_gridWidthsDirty = true;
119 }
120
121 aEvent.Skip();
122}
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.