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#pragma once
25
26#include <bitset>
27#include <memory>
28#include <utility>
29#include <vector>
30
31#include <wx/event.h>
32#include <wx/grid.h>
33#include <wx/version.h>
34
35#include <kicommon.h>
37#include <units_provider.h>
38
39class wxTextEntryBase;
41
42
51
52
53class KICOMMON_API WX_GRID_TABLE_BASE : public wxGridTableBase
54{
55public:
57 {
58 for( const auto& [col, attr] : m_colAttrs )
59 wxSafeDecRef( attr );
60 }
61
62 void SetColAttr( wxGridCellAttr* aAttr, int aCol ) override
63 {
64 wxSafeDecRef( m_colAttrs[aCol] );
65 m_colAttrs[aCol] = aAttr;
66 }
67
68 wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind ) override
69 {
70 if( m_colAttrs[aCol] )
71 {
72 m_colAttrs[aCol]->IncRef();
73 return enhanceAttr( m_colAttrs[aCol], aRow, aCol, aKind );
74 }
75
76 return enhanceAttr( nullptr, aRow, aCol, aKind );
77 }
78
79 virtual bool IsExpanderColumn( int aCol ) const { return false; }
80 virtual GROUP_TYPE GetGroupType( int aRow ) const { return GROUP_SINGLETON; }
81
82 void Clear() override
83 {
84 if( GetNumberRows() )
85 DeleteRows( 0, GetNumberRows() );
86 }
87
88protected:
89 wxGridCellAttr* enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
90 wxGridCellAttr::wxAttrKind aKind );
91
92protected:
93 std::map<int, wxGridCellAttr*> m_colAttrs;
94};
95
96
97class KICOMMON_API WX_GRID : public wxGrid
98{
99public:
100 // Constructor has to be wxFormBuilder-compatible
101 WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition,
102 const wxSize& size = wxDefaultSize, long style = wxWANTS_CHARS,
103 const wxString& name = wxGridNameStr );
104
105 ~WX_GRID() override;
106
113 void SetColLabelSize( int aHeight ); // Yes, we're hiding a non-virtual method
114
119 void SetLabelFont( const wxFont& aFont ); // Yes, we're hiding a non-virtual method
120
127 void EnableAlternateRowColors( bool aEnable = true );
128
134 wxString GetShownColumnsAsString();
135 std::bitset<64> GetShownColumns();
136
140 void ShowHideColumns( const wxString& shownColumns );
141
146 void SetTable( wxGridTableBase* table, bool aTakeOwnership = false );
147
152 void DestroyTable( wxGridTableBase* aTable );
153
160 bool CommitPendingChanges( bool aQuietMode = false );
162
163 void OnAddRow( const std::function<std::pair<int, int>()>& aAdder );
164
169 void OnDeleteRows( const std::function<void( int row )>& aDeleter );
170
171 void OnDeleteRows( const std::function<bool( int row )>& aFilter,
172 const std::function<void( int row )>& aDeleter );
173
177 void SwapRows( int aRowA, int aRowB );
178 void OnMoveRowUp( const std::function<void( int row )>& aMover );
179 void OnMoveRowDown( const std::function<void( int row )>& aMover );
180 void OnMoveRowUp( const std::function<bool( int row )>& aFilter,
181 const std::function<void( int row )>& aMover );
182 void OnMoveRowDown( const std::function<bool( int row )>& aFilter,
183 const std::function<void( int row )>& aMover );
184
190 void SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol = 0 );
191
192 void SetAutoEvalCols( const std::vector<int>& aCols ) { m_autoEvalCols = aCols; }
193
197 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType );
198
202 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit );
203
212 int GetUnitValue( int aRow, int aCol );
213
221 std::optional<int> GetOptionalUnitValue( int aRow, int aCol );
222
226 void SetUnitValue( int aRow, int aCol, int aValue );
227
231 void SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue );
232
242 int GetVisibleWidth( int aCol, bool aHeader = true, bool aContents = true, bool aKeep = false );
243
248 void EnsureColLabelsVisible();
249
258 void ShowEditorOnMouseUp() { m_waitForSlowClick = true; }
259 void CancelShowEditorOnMouseUp() { m_waitForSlowClick = false; }
260
266 void ClearRows( bool aUpdateLabels = true )
267 {
268 if( GetNumberRows() > 0 )
269 DeleteRows( 0, GetNumberRows(), aUpdateLabels );
270 }
271
275 static void CellEditorSetMargins( wxTextEntryBase* aEntry );
276
280 static void CellEditorTransformSizeRect( wxRect& aRect );
281
288 void OverrideMinSize( double aXPct, double aYPct )
289 {
290 wxSize size = DoGetBestSize();
291 m_minSizeOverride = wxSize( KiROUND( size.x * aXPct ), KiROUND( size.y * aYPct ) );
292 }
293
294 wxSize DoGetBestSize() const override
295 {
297 return m_minSizeOverride.value();
298 else
299 return wxGrid::DoGetBestSize();
300 }
301
306 void SetupColumnAutosizer( int aFlexibleCol );
307
309
311
312protected:
317 void DrawColLabel( wxDC& dc, int col ) override;
318
322 void DrawRowLabel( wxDC& dc, int row ) override;
323
327 void DrawCornerLabel( wxDC& dc ) override;
328
329 void onGridColMove( wxGridEvent& aEvent );
330 void onGridCellSelect( wxGridEvent& aEvent );
331 void onCellEditorShown( wxGridEvent& aEvent );
332 void onCellEditorHidden( wxGridEvent& aEvent );
333
334 void onDPIChanged(wxDPIChangedEvent& event);
335
337 {
338 if( m_unitsProviders.contains( aCol ) )
339 return m_unitsProviders.at( aCol );
340 else
341 return m_unitsProviders.begin()->second;
342 }
343
347 std::pair<EDA_UNITS, EDA_DATA_TYPE> getColumnUnits( int aCol ) const;
348
349private:
350 void recomputeGridWidths();
351
352 void onSizeEvent( wxSizeEvent& aEvent );
353
354protected:
356
357 std::map<int, UNITS_PROVIDER*> m_unitsProviders;
358 std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
359 std::vector<int> m_autoEvalCols;
360 std::unordered_map<int, std::pair<EDA_UNITS, EDA_DATA_TYPE>> m_autoEvalColsUnits;
361 std::map< std::pair<int, int>, std::pair<wxString, wxString> > m_evalBeforeAfter;
362
363 std::optional<wxSize> m_minSizeOverride;
364
365 std::map<int, int> m_autosizedCols; // map of col : min_width
367
368 bool m_gridWidthsDirty = true;
369 int m_gridWidth = 0;
370
372};
const char * name
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
Icon provider for the "standard" row indicators, for example in layer selection lists.
~WX_GRID_TABLE_BASE() override
Definition wx_grid.h:56
virtual GROUP_TYPE GetGroupType(int aRow) const
Definition wx_grid.h:80
virtual bool IsExpanderColumn(int aCol) const
Definition wx_grid.h:79
wxGridCellAttr * enhanceAttr(wxGridCellAttr *aInputAttr, int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind)
Definition wx_grid.cpp:46
std::map< int, wxGridCellAttr * > m_colAttrs
Definition wx_grid.h:93
void Clear() override
Definition wx_grid.h:82
wxGridCellAttr * GetAttr(int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
Definition wx_grid.h:68
void SetColAttr(wxGridCellAttr *aAttr, int aCol) override
Definition wx_grid.h:62
int m_flexibleCol
Definition wx_grid.h:366
std::map< int, int > m_autosizedCols
Definition wx_grid.h:365
bool m_gridWidthsDirty
Definition wx_grid.h:368
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:266
bool m_weOwnTable
Definition wx_grid.h:355
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition wx_grid.cpp:493
void OnMoveRowUp(const std::function< void(int row)> &aMover)
Definition wx_grid.cpp:762
std::map< int, UNITS_PROVIDER * > m_unitsProviders
Definition wx_grid.h:357
std::optional< wxSize > m_minSizeOverride
Definition wx_grid.h:363
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:272
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:449
std::unordered_map< int, std::pair< EDA_UNITS, EDA_DATA_TYPE > > m_autoEvalColsUnits
Definition wx_grid.h:360
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:288
bool CancelPendingChanges()
Definition wx_grid.cpp:600
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:251
void SwapRows(int aRowA, int aRowB)
These aren't that tricky, but might as well share code.
Definition wx_grid.cpp:751
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:205
void SetGridWidthsDirty()
Definition wx_grid.h:308
std::vector< int > m_autoEvalCols
Definition wx_grid.h:359
UNITS_PROVIDER * getUnitsProvider(int aCol) const
Definition wx_grid.h:336
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
Definition wx_grid.h:361
void CancelShowEditorOnMouseUp()
Definition wx_grid.h:259
void ClearRows(bool aUpdateLabels=true)
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition wx_grid.h:266
ROW_ICON_PROVIDER * m_rowIconProvider
Definition wx_grid.h:371
void OnMoveRowDown(const std::function< void(int row)> &aMover)
Definition wx_grid.cpp:795
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:258
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition wx_grid.cpp:700
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition wx_grid.cpp:463
void OnAddRow(const std::function< std::pair< int, int >()> &aAdder)
Definition wx_grid.cpp:680
int m_gridWidth
Definition wx_grid.h:369
std::bitset< 64 > GetShownColumns()
Definition wx_grid.cpp:482
void SetAutoEvalCols(const std::vector< int > &aCols)
Definition wx_grid.h:192
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:307
ROW_ICON_PROVIDER * GetRowIconProvider() const
Definition wx_grid.h:310
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:828
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition wx_grid.cpp:628
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
Definition wx_grid.h:358
wxSize DoGetBestSize() const override
Definition wx_grid.h:294
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:38
EDA_UNITS
Definition eda_units.h:48
#define KICOMMON_API
Definition kicommon.h:28
enum KICOMMON_API GROUP_TYPE
Definition wx_grid.h:44
GROUP_COLLAPSED_DURING_SORT
Definition wx_grid.h:47
GROUP_COLLAPSED
Definition wx_grid.h:46
GROUP_EXPANDED
Definition wx_grid.h:48
GROUP_SINGLETON
Definition wx_grid.h:45