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_grid.h"
27 
28 
29 #define MIN_GRIDCELL_MARGIN 3
30 
31 
32 WX_GRID::WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
33  long style, const wxString& name ) :
34  wxGrid( parent, id, pos, size, style, name ),
35  m_weOwnTable( false )
36 {}
37 
38 
40 {
41  if( m_weOwnTable )
42  DestroyTable( GetTable() );
43 }
44 
45 
46 void WX_GRID::SetTable( wxGridTableBase* aTable, bool aTakeOwnership )
47 {
48  // wxGrid::SetTable() messes up the column widths from wxFormBuilder so we have to save
49  // and restore them.
50  int numberCols = GetNumberCols();
51  int* formBuilderColWidths = new int[numberCols];
52 
53  for( int i = 0; i < numberCols; ++i )
54  formBuilderColWidths[ i ] = GetColSize( i );
55 
56  wxGrid::SetTable( aTable );
57  // wxGrid::SetTable() may change the number of columns,
58  // so prevent out-of-bounds access to formBuildColWidths
59  numberCols = std::min( numberCols, GetNumberCols() );
60 
61  for( int i = 0; i < numberCols; ++i )
62  {
63  // correct wxFormBuilder width for large fonts and/or long translations
64  int headingWidth = GetTextExtent( GetColLabelValue( i ) ).x + 2 * MIN_GRIDCELL_MARGIN;
65 
66  SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
67  }
68 
69  delete[] formBuilderColWidths;
70 
71  Connect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), NULL, this );
72 
73  m_weOwnTable = aTakeOwnership;
74 }
75 
76 
77 void WX_GRID::DestroyTable( wxGridTableBase* aTable )
78 {
79  // wxGrid's destructor will crash trying to look up the cell attr if the edit control
80  // is left open. Normally it's closed in Validate(), but not if the user hit Cancel.
81  CommitPendingChanges( true /* quiet mode */ );
82 
83  Disconnect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), NULL, this );
84 
85  wxGrid::SetTable( nullptr );
86  delete aTable;
87 }
88 
89 
91 {
92  wxString shownColumns;
93 
94  for( int i = 0; i < GetNumberCols(); ++i )
95  {
96  if( IsColShown( i ) )
97  {
98  if( shownColumns.Length() )
99  shownColumns << wxT( " " );
100  shownColumns << i;
101  }
102  }
103 
104  return shownColumns;
105 }
106 
107 
108 void WX_GRID::ShowHideColumns( const wxString& shownColumns )
109 {
110  for( int i = 0; i < GetNumberCols(); ++i )
111  HideCol( i );
112 
113  wxStringTokenizer shownTokens( shownColumns );
114 
115  while( shownTokens.HasMoreTokens() )
116  {
117  long colNumber;
118  shownTokens.GetNextToken().ToLong( &colNumber );
119 
120  if( colNumber >= 0 && colNumber < GetNumberCols() )
121  ShowCol( colNumber );
122  }
123 }
124 
125 
126 // An re-implementation of wxGrid::DrawColLabel which left-aligns the first column when
127 // there are no row labels.
128 void WX_GRID::DrawColLabel( wxDC& dc, int col )
129 {
130  if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
131  return;
132 
133  int colLeft = GetColLeft( col );
134 
135  wxRect rect( colLeft, 0, GetColWidth( col ), m_colLabelHeight );
136  static wxGridColumnHeaderRendererDefault rend;
137 
138  // It is reported that we need to erase the background to avoid display
139  // artefacts, see #12055.
140  wxDCBrushChanger setBrush( dc, m_colWindow->GetBackgroundColour() );
141  dc.DrawRectangle(rect);
142 
143  rend.DrawBorder( *this, dc, rect );
144 
145  int hAlign, vAlign;
146  GetColLabelAlignment( &hAlign, &vAlign );
147  const int orient = GetColLabelTextOrientation();
148 
149  if( col == 0 && GetRowLabelSize() == 0 )
150  hAlign = wxALIGN_LEFT;
151 
152  rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
153 }
154 
155 
156 bool WX_GRID::CommitPendingChanges( bool aQuietMode )
157 {
158  if( !IsCellEditControlEnabled() )
159  return true;
160 
161  if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
162  return false;
163 
164  HideCellEditControl();
165 
166  // do it after HideCellEditControl()
167  m_cellEditCtrlEnabled = false;
168 
169  int row = m_currentCellCoords.GetRow();
170  int col = m_currentCellCoords.GetCol();
171 
172  wxString oldval = GetCellValue( row, col );
173  wxString newval;
174 
175  wxGridCellAttr* attr = GetCellAttr( row, col );
176  wxGridCellEditor* editor = attr->GetEditor( this, row, col );
177 
178  bool changed = editor->EndEdit( row, col, this, oldval, &newval );
179 
180  editor->DecRef();
181  attr->DecRef();
182 
183  if( changed )
184  {
185  if( !aQuietMode && SendEvent(wxEVT_GRID_CELL_CHANGING, newval) == -1 )
186  return false;
187 
188  editor->ApplyEdit(row, col, this);
189 
190  // for compatibility reasons dating back to wx 2.8 when this event
191  // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
192  // didn't exist we allow vetoing this one too
193  if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
194  {
195  // Event has been vetoed, set the data back.
196  SetCellValue( row, col, oldval );
197  return false;
198  }
199  }
200 
201  return true;
202 }
203 
204 
205 void WX_GRID::onGridColMove( wxGridEvent& aEvent )
206 {
207  // wxWidgets won't move an open editor, so better just to close it
208  CommitPendingChanges( true );
209 }
bool m_weOwnTable
Definition: wx_grid.h:75
void DrawColLabel(wxDC &dc, int col) override
Definition: wx_grid.cpp:128
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:32
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition: wx_grid.cpp:108
void SetTable(wxGridTableBase *table, bool aTakeOwnership=false)
Hide wxGrid&#39;s SetTable() method with one which doesn&#39;t mess up the grid column widths when setting th...
Definition: wx_grid.cpp:46
#define MIN_GRIDCELL_MARGIN
Definition: wx_grid.cpp:29
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:77
void onGridColMove(wxGridEvent &aEvent)
Definition: wx_grid.cpp:205
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:156
~WX_GRID() override
Definition: wx_grid.cpp:39
wxString GetShownColumns()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:90
const char * name
Definition: DXF_plotter.cpp:61
#define max(a, b)
Definition: auxiliary.h:86
size_t i
Definition: json11.cpp:597
#define min(a, b)
Definition: auxiliary.h:85