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