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