KiCad PCB EDA Suite
Loading...
Searching...
No Matches
wx_grid.h
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 The 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#ifndef KICAD_WX_GRID_H
25#define KICAD_WX_GRID_H
26
27#include <bitset>
28#include <memory>
29#include <utility>
30#include <vector>
31
32#include <wx/event.h>
33#include <wx/grid.h>
34#include <wx/version.h>
35
37#include <units_provider.h>
38
39class wxTextEntryBase;
40
41
42class WX_GRID_TABLE_BASE : public wxGridTableBase
43{
44protected:
45 wxGridCellAttr* enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
46 wxGridCellAttr::wxAttrKind aKind );
47};
48
49
50class WX_GRID : public wxGrid
51{
52public:
53 // Constructor has to be wxFormBuilder-compatible
54 WX_GRID( wxWindow *parent, wxWindowID id,
55 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
56 long style = wxWANTS_CHARS, const wxString& name = wxGridNameStr );
57
58 ~WX_GRID() override;
59
66 void SetColLabelSize( int aHeight ); // Yes, we're hiding a non-virtual method
67
72 void SetLabelFont( const wxFont& aFont ); // Yes, we're hiding a non-virtual method
73
80 void EnableAlternateRowColors( bool aEnable = true );
81
87 wxString GetShownColumnsAsString();
88 std::bitset<64> GetShownColumns();
89
93 void ShowHideColumns( const wxString& shownColumns );
94
99 void SetTable( wxGridTableBase* table, bool aTakeOwnership = false );
100
105 void DestroyTable( wxGridTableBase* aTable );
106
113 bool CommitPendingChanges( bool aQuietMode = false );
115
116 void OnAddRow( const std::function<std::pair<int, int>()>& aAdder );
117
122 void OnDeleteRows( const std::function<void( int row )>& aDeleter );
123
124 void OnDeleteRows( const std::function<bool( int row )>& aFilter,
125 const std::function<void( int row )>& aDeleter );
126
130 void SwapRows( int aRowA, int aRowB );
131 void OnMoveRowUp( const std::function<void( int row )>& aMover );
132 void OnMoveRowDown( const std::function<void( int row )>& aMover );
133 void OnMoveRowUp( const std::function<bool( int row )>& aFilter,
134 const std::function<void( int row )>& aMover );
135 void OnMoveRowDown( const std::function<bool( int row )>& aFilter,
136 const std::function<void( int row )>& aMover );
137
143 void SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol = 0 );
144
145 void SetAutoEvalCols( const std::vector<int>& aCols ) { m_autoEvalCols = aCols; }
146
150 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType );
151
155 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit );
156
165 int GetUnitValue( int aRow, int aCol );
166
167
175 std::optional<int> GetOptionalUnitValue( int aRow, int aCol );
176
180 void SetUnitValue( int aRow, int aCol, int aValue );
181
185 void SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue );
186
196 int GetVisibleWidth( int aCol, bool aHeader = true, bool aContents = true, bool aKeep = false );
197
203
212 void ShowEditorOnMouseUp() { m_waitForSlowClick = true; }
213 void CancelShowEditorOnMouseUp() { m_waitForSlowClick = false; }
214
221 {
222 if( GetNumberRows() )
223 DeleteRows( 0, GetNumberRows() );
224 }
225
229 static void CellEditorSetMargins( wxTextEntryBase* aEntry );
230
234 static void CellEditorTransformSizeRect( wxRect& aRect );
235
242 void OverrideMinSize( double aXPct, double aYPct )
243 {
244 wxSize size = DoGetBestSize();
245 m_minSizeOverride = wxSize( KiROUND( size.x * aXPct ), KiROUND( size.y * aYPct ) );
246 }
247
248 wxSize DoGetBestSize() const override
249 {
251 return m_minSizeOverride.value();
252 else
253 return wxGrid::DoGetBestSize();
254 }
255
256protected:
261 void DrawColLabel( wxDC& dc, int col ) override;
262
266 void DrawRowLabel( wxDC& dc, int row ) override;
267
271 void DrawCornerLabel( wxDC& dc ) override;
272
273 void onGridColMove( wxGridEvent& aEvent );
274 void onGridCellSelect( wxGridEvent& aEvent );
275 void onCellEditorShown( wxGridEvent& aEvent );
276 void onCellEditorHidden( wxGridEvent& aEvent );
277
278 void onDPIChanged(wxDPIChangedEvent& event);
279
281 {
282 if( m_unitsProviders.contains( aCol ) )
283 return m_unitsProviders.at( aCol );
284 else
285 return m_unitsProviders.begin()->second;
286 }
287
291 std::pair<EDA_UNITS, EDA_DATA_TYPE> getColumnUnits( int aCol ) const;
292
293protected:
295
296 std::map<int, UNITS_PROVIDER*> m_unitsProviders;
297 std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
298 std::vector<int> m_autoEvalCols;
299 std::unordered_map<int, std::pair<EDA_UNITS, EDA_DATA_TYPE>> m_autoEvalColsUnits;
300
301 std::map< std::pair<int, int>, std::pair<wxString, wxString> > m_evalBeforeAfter;
302
303 std::optional<wxSize> m_minSizeOverride;
304};
305
306#endif //KICAD_WX_GRID_H
const char * name
Definition: DXF_plotter.cpp:62
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
wxGridCellAttr * enhanceAttr(wxGridCellAttr *aInputAttr, int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind)
Definition: wx_grid.cpp:45
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=true, bool aKeep=false)
Calculate the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:916
void onGridCellSelect(wxGridEvent &aEvent)
Definition: wx_grid.cpp:326
~WX_GRID() override
Definition: wx_grid.cpp:226
void SetLabelFont(const wxFont &aFont)
Hide wxGrid's SetLabelFont() because for some reason on MSW it's a one-shot and subsequent calls to i...
Definition: wx_grid.cpp:267
bool m_weOwnTable
Definition: wx_grid.h:294
void onDPIChanged(wxDPIChangedEvent &event)
Definition: wx_grid.cpp:237
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition: wx_grid.cpp:494
void OnMoveRowUp(const std::function< void(int row)> &aMover)
Definition: wx_grid.cpp:766
std::map< int, UNITS_PROVIDER * > m_unitsProviders
Definition: wx_grid.h:296
std::optional< wxSize > m_minSizeOverride
Definition: wx_grid.h:303
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:273
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:450
std::unordered_map< int, std::pair< EDA_UNITS, EDA_DATA_TYPE > > m_autoEvalColsUnits
Definition: wx_grid.h:299
void OverrideMinSize(double aXPct, double aYPct)
Grids that have column sizes automatically set to fill the available width don't want to shrink after...
Definition: wx_grid.h:242
void SetAutoEvalColUnits(int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType)
Set the unit and unit data type to use for a given column.
Definition: wx_grid.cpp:841
bool CancelPendingChanges()
Definition: wx_grid.cpp:604
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:252
void SwapRows(int aRowA, int aRowB)
These aren't that tricky, but might as well share code.
Definition: wx_grid.cpp:755
std::pair< EDA_UNITS, EDA_DATA_TYPE > getColumnUnits(int aCol) const
Returns the units and data type associated with a given column.
Definition: wx_grid.cpp:984
void SetUnitValue(int aRow, int aCol, int aValue)
Set a unitized cell's value.
Definition: wx_grid.cpp:890
int GetUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition: wx_grid.cpp:854
std::vector< int > m_autoEvalCols
Definition: wx_grid.h:298
UNITS_PROVIDER * getUnitsProvider(int aCol) const
Definition: wx_grid.h:280
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
Definition: wx_grid.h:301
void CancelShowEditorOnMouseUp()
Definition: wx_grid.h:213
void DrawCornerLabel(wxDC &dc) override
A re-implementation of wxGrid::DrawCornerLabel which draws flat borders.
Definition: wx_grid.cpp:512
void onCellEditorHidden(wxGridEvent &aEvent)
Definition: wx_grid.cpp:368
void OnMoveRowDown(const std::function< void(int row)> &aMover)
Definition: wx_grid.cpp:799
void ShowEditorOnMouseUp()
WxWidgets has a bunch of bugs in its handling of wxGrid mouse events which close cell editors right a...
Definition: wx_grid.h:212
void onGridColMove(wxGridEvent &aEvent)
Definition: wx_grid.cpp:909
void SetOptionalUnitValue(int aRow, int aCol, std::optional< int > aValue)
Set a unitized cell's optional value.
Definition: wx_grid.cpp:903
void onCellEditorShown(wxGridEvent &aEvent)
Definition: wx_grid.cpp:353
void EnsureColLabelsVisible()
Ensure the height of the row displaying the column labels is enough, even if labels are multiline tex...
Definition: wx_grid.cpp:956
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition: wx_grid.cpp:704
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:464
void OnAddRow(const std::function< std::pair< int, int >()> &aAdder)
Definition: wx_grid.cpp:684
void DrawRowLabel(wxDC &dc, int row) override
A re-implementation of wxGrid::DrawRowLabel which draws flat borders.
Definition: wx_grid.cpp:572
std::bitset< 64 > GetShownColumns()
Definition: wx_grid.cpp:483
static void CellEditorSetMargins(wxTextEntryBase *aEntry)
A helper function to set OS-specific margins for text-based cell editors.
Definition: wx_grid.cpp:77
std::optional< int > GetOptionalUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition: wx_grid.cpp:872
void SetAutoEvalCols(const std::vector< int > &aCols)
Definition: wx_grid.h:145
void EnableAlternateRowColors(bool aEnable=true)
Enable alternate row highlighting, where every odd row has a different background color than the even...
Definition: wx_grid.cpp:308
void SetUnitsProvider(UNITS_PROVIDER *aProvider, int aCol=0)
Set a EUNITS_PROVIDER to enable use of unit- and eval-based Getters.
Definition: wx_grid.cpp:832
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:220
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:632
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
Definition: wx_grid.h:297
void DrawColLabel(wxDC &dc, int col) override
A re-implementation of wxGrid::DrawColLabel which left-aligns the first column and draws flat borders...
Definition: wx_grid.cpp:533
wxSize DoGetBestSize() const override
Definition: wx_grid.h:248
static void CellEditorTransformSizeRect(wxRect &aRect)
A helper function to tweak sizes of text-based cell editors depending on OS.
Definition: wx_grid.cpp:85
EDA_DATA_TYPE
The type of unit.
Definition: eda_units.h:38
EDA_UNITS
Definition: eda_units.h:48