KiCad PCB EDA Suite
wx_grid.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) 2018-2021 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 3
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
24#include <wx/tokenzr.h>
25#include <wx/dc.h>
26#include <widgets/wx_grid.h>
27#include <widgets/ui_common.h>
28#include <algorithm>
29
30
31#define MIN_GRIDCELL_MARGIN 3
32
33
34WX_GRID::WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
35 long style, const wxString& name ) :
36 wxGrid( parent, id, pos, size, style, name ),
37 m_weOwnTable( false )
38{
39 SetDefaultCellOverflow( false );
40
41 // Make sure the GUI font scales properly on GTK
42 SetDefaultCellFont( KIUI::GetControlFont( this ) );
43
44#if wxCHECK_VERSION( 3, 1, 3 )
45 Connect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
46#endif
47
48}
49
50
52{
53 if( m_weOwnTable )
54 DestroyTable( GetTable() );
55
56#if wxCHECK_VERSION( 3, 1, 3 )
57 Disconnect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
58#endif
59
60}
61
62
63#if wxCHECK_VERSION( 3, 1, 3 )
64void WX_GRID::onDPIChanged(wxDPIChangedEvent& aEvt)
65{
68#ifndef __WXMAC__
69 aEvt.Skip();
70#endif
71}
72#endif
73
74void WX_GRID::SetColLabelSize( int aHeight )
75{
76 if( aHeight == 0 )
77 {
78 wxGrid::SetColLabelSize( 0 );
79 return;
80 }
81
82 wxFont headingFont = KIUI::GetControlFont( this ).Bold();
83
84 // Make sure the GUI font scales properly on GTK
85 SetLabelFont( headingFont );
86
87 // Correct wxFormBuilder height for large fonts
88 int minHeight = headingFont.GetPixelSize().y + 2 * MIN_GRIDCELL_MARGIN;
89 wxGrid::SetColLabelSize( std::max( aHeight, minHeight ) );
90}
91
92
93void WX_GRID::SetTable( wxGridTableBase* aTable, bool aTakeOwnership )
94{
95 // wxGrid::SetTable() messes up the column widths from wxFormBuilder so we have to save
96 // and restore them.
97 int numberCols = GetNumberCols();
98 int* formBuilderColWidths = new int[numberCols];
99
100 for( int i = 0; i < numberCols; ++i )
101 formBuilderColWidths[ i ] = GetColSize( i );
102
103 wxGrid::SetTable( aTable );
104
105 // wxGrid::SetTable() may change the number of columns, so prevent out-of-bounds access
106 // to formBuilderColWidths
107 numberCols = std::min( numberCols, GetNumberCols() );
108
109 for( int i = 0; i < numberCols; ++i )
110 {
111 // correct wxFormBuilder width for large fonts and/or long translations
112 int headingWidth = GetTextExtent( GetColLabelValue( i ) ).x + 2 * MIN_GRIDCELL_MARGIN;
113
114 SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
115 }
116
117 delete[] formBuilderColWidths;
118
119 Connect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
120 Connect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
121
122 m_weOwnTable = aTakeOwnership;
123}
124
125
126void WX_GRID::onGridCellSelect( wxGridEvent& aEvent )
127{
128 // Highlight the selected cell.
129 // Calling SelectBlock() allows a visual effect when cells are selected
130 // by tab or arrow keys.
131 // Otherwise, one cannot really know what actual cell is selected
132 int row = aEvent.GetRow();
133 int col = aEvent.GetCol();
134
135 if( row >= 0 && col >= 0 )
136 SelectBlock(row,col,row,col,false);
137}
138
139void WX_GRID::DestroyTable( wxGridTableBase* aTable )
140{
141 // wxGrid's destructor will crash trying to look up the cell attr if the edit control
142 // is left open. Normally it's closed in Validate(), but not if the user hit Cancel.
143 CommitPendingChanges( true /* quiet mode */ );
144
145 Disconnect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
146 Disconnect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
147
148 wxGrid::SetTable( nullptr );
149 delete aTable;
150}
151
152
154{
155 wxString shownColumns;
156
157 for( int i = 0; i < GetNumberCols(); ++i )
158 {
159 if( IsColShown( i ) )
160 {
161 if( shownColumns.Length() )
162 shownColumns << wxT( " " );
163
164 shownColumns << i;
165 }
166 }
167
168 return shownColumns;
169}
170
171
172void WX_GRID::ShowHideColumns( const wxString& shownColumns )
173{
174 for( int i = 0; i < GetNumberCols(); ++i )
175 HideCol( i );
176
177 wxStringTokenizer shownTokens( shownColumns );
178
179 while( shownTokens.HasMoreTokens() )
180 {
181 long colNumber;
182 shownTokens.GetNextToken().ToLong( &colNumber );
183
184 if( colNumber >= 0 && colNumber < GetNumberCols() )
185 ShowCol( colNumber );
186 }
187}
188
189
190void WX_GRID::DrawColLabel( wxDC& dc, int col )
191{
192 if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
193 return;
194
195 int colLeft = GetColLeft( col );
196
197 wxRect rect( colLeft, 0, GetColWidth( col ), m_colLabelHeight );
198 static wxGridColumnHeaderRendererDefault rend;
199
200 // It is reported that we need to erase the background to avoid display
201 // artifacts, see #12055.
202 // wxWidgets renamed this variable between 3.1.2 and 3.1.3 ...
203#if wxCHECK_VERSION( 3, 1, 3 )
204 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
205#else
206 wxDCBrushChanger setBrush( dc, m_colWindow->GetBackgroundColour() );
207#endif
208 dc.DrawRectangle(rect);
209
210 rend.DrawBorder( *this, dc, rect );
211
212 // Make sure fonts get scaled correctly on GTK HiDPI monitors
213 dc.SetFont( GetLabelFont() );
214
215 int hAlign, vAlign;
216 GetColLabelAlignment( &hAlign, &vAlign );
217 const int orient = GetColLabelTextOrientation();
218
219 if( col == 0 && GetRowLabelSize() == 0 )
220 hAlign = wxALIGN_LEFT;
221
222 rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
223}
224
225
226bool WX_GRID::CommitPendingChanges( bool aQuietMode )
227{
228 if( !IsCellEditControlEnabled() )
229 return true;
230
231 if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
232 return false;
233
234 HideCellEditControl();
235
236 // do it after HideCellEditControl()
237 m_cellEditCtrlEnabled = false;
238
239 int row = m_currentCellCoords.GetRow();
240 int col = m_currentCellCoords.GetCol();
241
242 wxString oldval = GetCellValue( row, col );
243 wxString newval;
244
245 wxGridCellAttr* attr = GetCellAttr( row, col );
246 wxGridCellEditor* editor = attr->GetEditor( this, row, col );
247
248 bool changed = editor->EndEdit( row, col, this, oldval, &newval );
249
250 editor->DecRef();
251 attr->DecRef();
252
253 if( changed )
254 {
255 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
256 return false;
257
258 editor->ApplyEdit( row, col, this );
259
260 // for compatibility reasons dating back to wx 2.8 when this event
261 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
262 // didn't exist we allow vetoing this one too
263 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
264 {
265 // Event has been vetoed, set the data back.
266 SetCellValue( row, col, oldval );
267 return false;
268 }
269 }
270
271 return true;
272}
273
274
275void WX_GRID::onGridColMove( wxGridEvent& aEvent )
276{
277 // wxWidgets won't move an open editor, so better just to close it
278 CommitPendingChanges( true );
279}
280
281
282int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep )
283{
284 int size = 0;
285
286 if( aCol < 0 )
287 {
288 if( aKeep )
289 size = GetRowLabelSize();
290
291 // The 1.1 scale factor is due to the fact row labels use a bold font, bigger than
292 // the normal font.
293 // TODO: use a better way to evaluate the text size, for bold font
294 for( int row = 0; aContents && row < GetNumberRows(); row++ )
295 size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + "M" ).x * 1.1 ) );
296 }
297 else
298 {
299 if( aKeep )
300 size = GetColSize( aCol );
301
302 // 'M' is generally the widest character, so we buffer the column width by default to
303 // ensure we don't write a continuous line of text at the column header
304 if( aHeader )
305 {
307
308 // The 1.1 scale factor is due to the fact headers use a bold font, bigger than
309 // the normal font.
310 size = std::max( size, int( GetTextExtent( GetColLabelValue( aCol ) + "M" ).x * 1.1 ) );
311 }
312
313 for( int row = 0; aContents && row < GetNumberRows(); row++ )
314 {
315 // If we have text, get the size. Otherwise, use a placeholder for the checkbox
316 if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
317 size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + "M" ).x );
318 else
319 size = std::max( size, GetTextExtent( "MM" ).x );
320 }
321 }
322
323 return size;
324}
325
326
328{
329 // The 1.1 scale factor is due to the fact row labels use a bold font, bigger than
330 // the normal font
331 // TODO: use a better way to evaluate the text size, for bold font
332 int line_height = int( GetTextExtent( "Mj" ).y * 1.1 ) + 3;
333 int row_height = GetColLabelSize();
334 int initial_row_height = row_height;
335
336 // Headers can be multiline. Fix the Column Label Height to show the full header
337 // However GetTextExtent does not work on multiline strings,
338 // and do not return the full text height (only the height of one line)
339 for( int col = 0; col < GetNumberCols(); col++ )
340 {
341 int nl_count = GetColLabelValue( col ).Freq( '\n' );
342
343 if( nl_count )
344 {
345 // Col Label height must be able to show nl_count+1 lines
346 if( row_height < line_height * ( nl_count+1 ) )
347 row_height += line_height * nl_count;
348 }
349 }
350
351 // Update the column label size, but only if needed, to avoid generating useless
352 // and perhaps annoying UI events when the size does not change
353 if( initial_row_height != row_height )
354 SetColLabelSize( row_height );
355}
const char * name
Definition: DXF_plotter.cpp:56
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=true, bool aKeep=false)
Calculates the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:282
void onGridCellSelect(wxGridEvent &aEvent)
Definition: wx_grid.cpp:126
~WX_GRID() override
Definition: wx_grid.cpp:51
bool m_weOwnTable
Definition: wx_grid.h:129
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition: wx_grid.cpp:172
void SetTable(wxGridTableBase *table, bool aTakeOwnership=false)
Hide wxGrid's SetTable() method with one which doesn't mess up the grid column widths when setting th...
Definition: wx_grid.cpp:93
void DestroyTable(wxGridTableBase *aTable)
Work-around for a bug in wxGrid which crashes when deleting the table if the cell edit control was no...
Definition: wx_grid.cpp:139
void SetColLabelSize(int aHeight)
Hide wxGrid's SetColLabelSize() method with one which makes sure the size is tall enough for the syst...
Definition: wx_grid.cpp:74
WX_GRID(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxWANTS_CHARS, const wxString &name=wxGridNameStr)
Definition: wx_grid.cpp:34
void onGridColMove(wxGridEvent &aEvent)
Definition: wx_grid.cpp:275
void EnsureColLabelsVisible()
Ensure the height of the row displaying the column labels is enough, even if labels are multiline tex...
Definition: wx_grid.cpp:327
wxString GetShownColumns()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:153
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:226
void DrawColLabel(wxDC &dc, int col) override
A re-implementation of wxGrid::DrawColLabel which left-aligns the first column when there are no row ...
Definition: wx_grid.cpp:190
wxFont GetControlFont(wxWindow *aWindow)
Definition: ui_common.cpp:150
Functions to provide common constants and other functions to assist in making a consistent UI.
#define MIN_GRIDCELL_MARGIN
Definition: wx_grid.cpp:31