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, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <bitset>
23#include <memory>
24#include <utility>
25#include <vector>
26
27#include <wx/event.h>
28#include <wx/grid.h>
29#include <wx/version.h>
30
31#include <kicommon.h>
33#include <units_provider.h>
34
35class wxTextEntryBase;
37
38
47
48
49class KICOMMON_API WX_GRID_TABLE_BASE : public wxGridTableBase
50{
51public:
53 {
54 for( const auto& [col, attr] : m_colAttrs )
55 wxSafeDecRef( attr );
56 }
57
58 void SetColAttr( wxGridCellAttr* aAttr, int aCol ) override
59 {
60 wxSafeDecRef( m_colAttrs[aCol] );
61 m_colAttrs[aCol] = aAttr;
62 }
63
64 wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind ) override
65 {
66 if( m_colAttrs[aCol] )
67 {
68 m_colAttrs[aCol]->IncRef();
69 return enhanceAttr( m_colAttrs[aCol], aRow, aCol, aKind );
70 }
71
72 return enhanceAttr( nullptr, aRow, aCol, aKind );
73 }
74
75 virtual bool IsExpanderColumn( int aCol ) const { return false; }
76 virtual GROUP_TYPE GetGroupType( int aRow ) const { return GROUP_SINGLETON; }
77
85 virtual bool HasUndoStateSerialization() const { return false; }
86 virtual wxString SerializeUndoState() const { return wxEmptyString; }
87 virtual void RestoreUndoState( const wxString& aState ) {}
88
89 void Clear() override
90 {
91 if( GetNumberRows() )
92 DeleteRows( 0, GetNumberRows() );
93 }
94
95protected:
96 wxGridCellAttr* enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
97 wxGridCellAttr::wxAttrKind aKind );
98
99protected:
100 std::map<int, wxGridCellAttr*> m_colAttrs;
101};
102
103
104class KICOMMON_API WX_GRID : public wxGrid
105{
106public:
107 // Constructor has to be wxFormBuilder-compatible
108 WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition,
109 const wxSize& size = wxDefaultSize, long style = wxWANTS_CHARS,
110 const wxString& name = wxGridNameStr );
111
112 ~WX_GRID() override;
113
120 void SetColLabelSize( int aHeight ); // Yes, we're hiding a non-virtual method
121
126 void SetLabelFont( const wxFont& aFont ); // Yes, we're hiding a non-virtual method
127
134 void EnableAlternateRowColors( bool aEnable = true );
135
141 wxString GetShownColumnsAsString();
142 std::bitset<64> GetShownColumns();
143
147 void ShowHideColumns( const wxString& shownColumns );
148
153 void SetTable( wxGridTableBase* table, bool aTakeOwnership = false );
154
159 void DestroyTable( wxGridTableBase* aTable );
160
167 bool CommitPendingChanges( bool aQuietMode = false );
169
170 void OnAddRow( const std::function<std::pair<int, int>()>& aAdder );
171
176 void OnDeleteRows( const std::function<void( int row )>& aDeleter );
177
178 void OnDeleteRows( const std::function<bool( int row )>& aFilter,
179 const std::function<void( int row )>& aDeleter );
180
184 void SwapRows( int aRowA, int aRowB );
185 void OnMoveRowUp( const std::function<void( int row )>& aMover );
186 void OnMoveRowDown( const std::function<void( int row )>& aMover );
187 void OnMoveRowUp( const std::function<bool( int row )>& aFilter,
188 const std::function<void( int row )>& aMover );
189 void OnMoveRowDown( const std::function<bool( int row )>& aFilter,
190 const std::function<void( int row )>& aMover );
191
197 void SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol = 0 );
198
199 void SetAutoEvalCols( const std::vector<int>& aCols ) { m_autoEvalCols = aCols; }
200
204 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType );
205
209 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit );
210
219 int GetUnitValue( int aRow, int aCol );
220
228 std::optional<int> GetOptionalUnitValue( int aRow, int aCol );
229
233 void SetUnitValue( int aRow, int aCol, int aValue );
234
238 void SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue );
239
249 int GetVisibleWidth( int aCol, bool aHeader = true, bool aContents = true, bool aKeep = false );
250
255 void EnsureColLabelsVisible();
256
265 void ShowEditorOnMouseUp() { m_waitForSlowClick = true; }
266 void CancelShowEditorOnMouseUp() { m_waitForSlowClick = false; }
267
273 void ClearRows( bool aUpdateLabels = true )
274 {
275 if( GetNumberRows() > 0 )
276 DeleteRows( 0, GetNumberRows(), aUpdateLabels );
277 }
278
282 static void CellEditorSetMargins( wxTextEntryBase* aEntry );
283
287 static void CellEditorTransformSizeRect( wxRect& aRect );
288
295 void OverrideMinSize( double aXPct, double aYPct )
296 {
297 wxSize size = DoGetBestSize();
298 m_minSizeOverride = wxSize( KiROUND( size.x * aXPct ), KiROUND( size.y * aYPct ) );
299 }
300
301 wxSize DoGetBestSize() const override
302 {
304 return m_minSizeOverride.value();
305 else
306 return wxGrid::DoGetBestSize();
307 }
308
313 void SetupColumnAutosizer( int aFlexibleCol );
314
316
318
319 void RecomputeGridWidths();
320
321protected:
326 void DrawColLabel( wxDC& dc, int col ) override;
327
331 void DrawRowLabel( wxDC& dc, int row ) override;
332
336 void DrawCornerLabel( wxDC& dc ) override;
337
338 void onGridColMove( wxGridEvent& aEvent );
339 void onGridCellSelect( wxGridEvent& aEvent );
340 void onCellEditorShown( wxGridEvent& aEvent );
341 void onCellEditorHidden( wxGridEvent& aEvent );
342
343 void onDPIChanged(wxDPIChangedEvent& event);
344
346 {
347 if( m_unitsProviders.contains( aCol ) )
348 return m_unitsProviders.at( aCol );
349 else
350 return m_unitsProviders.begin()->second;
351 }
352
356 std::pair<EDA_UNITS, EDA_DATA_TYPE> getColumnUnits( int aCol ) const;
357
358private:
359 void onSizeEvent( wxSizeEvent& aEvent );
360
361protected:
363
364 std::map<int, UNITS_PROVIDER*> m_unitsProviders;
365 std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
366 std::vector<int> m_autoEvalCols;
367 std::unordered_map<int, std::pair<EDA_UNITS, EDA_DATA_TYPE>> m_autoEvalColsUnits;
368 std::map< std::pair<int, int>, std::pair<wxString, wxString> > m_evalBeforeAfter;
369
370 std::optional<wxSize> m_minSizeOverride;
371
372 std::map<int, int> m_autosizedCols; // map of col : min_width
374
375 bool m_gridWidthsDirty = true;
376 int m_gridWidth = 0;
377
378#ifdef __WXMSW__
379 bool m_firstSelectionRefreshDone = false;
380#endif
381
383};
const char * name
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
Icon provider for the "standard" row indicators, for example in layer selection lists.
~WX_GRID_TABLE_BASE() override
Definition wx_grid.h:52
virtual void RestoreUndoState(const wxString &aState)
Definition wx_grid.h:87
virtual GROUP_TYPE GetGroupType(int aRow) const
Definition wx_grid.h:76
virtual bool IsExpanderColumn(int aCol) const
Definition wx_grid.h:75
wxGridCellAttr * enhanceAttr(wxGridCellAttr *aInputAttr, int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind)
Definition wx_grid.cpp:43
std::map< int, wxGridCellAttr * > m_colAttrs
Definition wx_grid.h:100
void Clear() override
Definition wx_grid.h:89
wxGridCellAttr * GetAttr(int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
Definition wx_grid.h:64
void SetColAttr(wxGridCellAttr *aAttr, int aCol) override
Definition wx_grid.h:58
virtual wxString SerializeUndoState() const
Definition wx_grid.h:86
virtual bool HasUndoStateSerialization() const
Optional identity-based serialization for the host dialog's Ctrl+Z/Ctrl+Y.
Definition wx_grid.h:85
int m_flexibleCol
Definition wx_grid.h:373
std::map< int, int > m_autosizedCols
Definition wx_grid.h:372
bool m_gridWidthsDirty
Definition wx_grid.h:375
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:293
bool m_weOwnTable
Definition wx_grid.h:362
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition wx_grid.cpp:531
void OnMoveRowUp(const std::function< void(int row)> &aMover)
Definition wx_grid.cpp:825
std::map< int, UNITS_PROVIDER * > m_unitsProviders
Definition wx_grid.h:364
std::optional< wxSize > m_minSizeOverride
Definition wx_grid.h:370
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:299
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:487
std::unordered_map< int, std::pair< EDA_UNITS, EDA_DATA_TYPE > > m_autoEvalColsUnits
Definition wx_grid.h:367
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:295
bool CancelPendingChanges()
Definition wx_grid.cpp:638
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:278
void SwapRows(int aRowA, int aRowB)
These aren't that tricky, but might as well share code.
Definition wx_grid.cpp:814
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:202
void SetGridWidthsDirty()
Definition wx_grid.h:315
std::vector< int > m_autoEvalCols
Definition wx_grid.h:366
UNITS_PROVIDER * getUnitsProvider(int aCol) const
Definition wx_grid.h:345
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
Definition wx_grid.h:368
void CancelShowEditorOnMouseUp()
Definition wx_grid.h:266
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:273
ROW_ICON_PROVIDER * m_rowIconProvider
Definition wx_grid.h:382
void OnMoveRowDown(const std::function< void(int row)> &aMover)
Definition wx_grid.cpp:858
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:265
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition wx_grid.cpp:738
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition wx_grid.cpp:501
void OnAddRow(const std::function< std::pair< int, int >()> &aAdder)
Definition wx_grid.cpp:718
int m_gridWidth
Definition wx_grid.h:376
std::bitset< 64 > GetShownColumns()
Definition wx_grid.cpp:520
void SetAutoEvalCols(const std::vector< int > &aCols)
Definition wx_grid.h:199
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:334
ROW_ICON_PROVIDER * GetRowIconProvider() const
Definition wx_grid.h:317
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:891
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition wx_grid.cpp:666
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
Definition wx_grid.h:365
wxSize DoGetBestSize() const override
Definition wx_grid.h:301
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:34
EDA_UNITS
Definition eda_units.h:44
#define KICOMMON_API
Definition kicommon.h:27
std::vector< std::vector< std::string > > table
enum KICOMMON_API GROUP_TYPE
Definition wx_grid.h:40
GROUP_COLLAPSED_DURING_SORT
Definition wx_grid.h:43
GROUP_COLLAPSED
Definition wx_grid.h:42
GROUP_EXPANDED
Definition wx_grid.h:44
GROUP_SINGLETON
Definition wx_grid.h:41