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