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 nullptr;
77 }
78
79 virtual bool IsExpanderColumn( int aCol ) const { return false; }
80 virtual GROUP_TYPE GetGroupType( int aRow ) const { return GROUP_SINGLETON; }
81
82protected:
83 wxGridCellAttr* enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
84 wxGridCellAttr::wxAttrKind aKind );
85
86protected:
87 std::map<int, wxGridCellAttr*> m_colAttrs;
88};
89
90
91class KICOMMON_API WX_GRID : public wxGrid
92{
93public:
94 // Constructor has to be wxFormBuilder-compatible
95 WX_GRID( wxWindow *parent, wxWindowID id,
96 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
97 long style = wxWANTS_CHARS, const wxString& name = wxGridNameStr );
98
99 ~WX_GRID() override;
100
107 void SetColLabelSize( int aHeight ); // Yes, we're hiding a non-virtual method
108
113 void SetLabelFont( const wxFont& aFont ); // Yes, we're hiding a non-virtual method
114
121 void EnableAlternateRowColors( bool aEnable = true );
122
128 wxString GetShownColumnsAsString();
129 std::bitset<64> GetShownColumns();
130
134 void ShowHideColumns( const wxString& shownColumns );
135
140 void SetTable( wxGridTableBase* table, bool aTakeOwnership = false );
141
146 void DestroyTable( wxGridTableBase* aTable );
147
154 bool CommitPendingChanges( bool aQuietMode = false );
156
157 void OnAddRow( const std::function<std::pair<int, int>()>& aAdder );
158
163 void OnDeleteRows( const std::function<void( int row )>& aDeleter );
164
165 void OnDeleteRows( const std::function<bool( int row )>& aFilter,
166 const std::function<void( int row )>& aDeleter );
167
171 void SwapRows( int aRowA, int aRowB );
172 void OnMoveRowUp( const std::function<void( int row )>& aMover );
173 void OnMoveRowDown( const std::function<void( int row )>& aMover );
174 void OnMoveRowUp( const std::function<bool( int row )>& aFilter,
175 const std::function<void( int row )>& aMover );
176 void OnMoveRowDown( const std::function<bool( int row )>& aFilter,
177 const std::function<void( int row )>& aMover );
178
184 void SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol = 0 );
185
186 void SetAutoEvalCols( const std::vector<int>& aCols ) { m_autoEvalCols = aCols; }
187
191 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType );
192
196 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit );
197
206 int GetUnitValue( int aRow, int aCol );
207
208
216 std::optional<int> GetOptionalUnitValue( int aRow, int aCol );
217
221 void SetUnitValue( int aRow, int aCol, int aValue );
222
226 void SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue );
227
237 int GetVisibleWidth( int aCol, bool aHeader = true, bool aContents = true, bool aKeep = false );
238
243 void EnsureColLabelsVisible();
244
253 void ShowEditorOnMouseUp() { m_waitForSlowClick = true; }
254 void CancelShowEditorOnMouseUp() { m_waitForSlowClick = false; }
255
261 void ClearRows( bool aUpdateLabels = true )
262 {
263 if( GetNumberRows() > 0 )
264 DeleteRows( 0, GetNumberRows(), aUpdateLabels );
265 }
266
270 static void CellEditorSetMargins( wxTextEntryBase* aEntry );
271
275 static void CellEditorTransformSizeRect( wxRect& aRect );
276
283 void OverrideMinSize( double aXPct, double aYPct )
284 {
285 wxSize size = DoGetBestSize();
286 m_minSizeOverride = wxSize( KiROUND( size.x * aXPct ), KiROUND( size.y * aYPct ) );
287 }
288
289 wxSize DoGetBestSize() const override
290 {
292 return m_minSizeOverride.value();
293 else
294 return wxGrid::DoGetBestSize();
295 }
296
301 void SetupColumnAutosizer( int aFlexibleCol );
302
304
306
307protected:
312 void DrawColLabel( wxDC& dc, int col ) override;
313
317 void DrawRowLabel( wxDC& dc, int row ) override;
318
322 void DrawCornerLabel( wxDC& dc ) override;
323
324 void onGridColMove( wxGridEvent& aEvent );
325 void onGridCellSelect( wxGridEvent& aEvent );
326 void onCellEditorShown( wxGridEvent& aEvent );
327 void onCellEditorHidden( wxGridEvent& aEvent );
328
329 void onDPIChanged(wxDPIChangedEvent& event);
330
332 {
333 if( m_unitsProviders.contains( aCol ) )
334 return m_unitsProviders.at( aCol );
335 else
336 return m_unitsProviders.begin()->second;
337 }
338
342 std::pair<EDA_UNITS, EDA_DATA_TYPE> getColumnUnits( int aCol ) const;
343
344private:
345 void recomputeGridWidths();
346
347 void onSizeEvent( wxSizeEvent& aEvent );
348
349protected:
351
352 std::map<int, UNITS_PROVIDER*> m_unitsProviders;
353 std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
354 std::vector<int> m_autoEvalCols;
355 std::unordered_map<int, std::pair<EDA_UNITS, EDA_DATA_TYPE>> m_autoEvalColsUnits;
356 std::map< std::pair<int, int>, std::pair<wxString, wxString> > m_evalBeforeAfter;
357
358 std::optional<wxSize> m_minSizeOverride;
359
360 std::map<int, int> m_autosizedCols; // map of col : min_width
362
363 bool m_gridWidthsDirty = true;
364 int m_gridWidth = 0;
365
367};
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:87
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:361
std::map< int, int > m_autosizedCols
Definition wx_grid.h:360
bool m_gridWidthsDirty
Definition wx_grid.h:363
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:350
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:352
std::optional< wxSize > m_minSizeOverride
Definition wx_grid.h:358
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:355
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:283
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:303
std::vector< int > m_autoEvalCols
Definition wx_grid.h:354
UNITS_PROVIDER * getUnitsProvider(int aCol) const
Definition wx_grid.h:331
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
Definition wx_grid.h:356
void CancelShowEditorOnMouseUp()
Definition wx_grid.h:254
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:261
ROW_ICON_PROVIDER * m_rowIconProvider
Definition wx_grid.h:366
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:253
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:364
std::bitset< 64 > GetShownColumns()
Definition wx_grid.cpp:482
void SetAutoEvalCols(const std::vector< int > &aCols)
Definition wx_grid.h:186
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:305
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:353
wxSize DoGetBestSize() const override
Definition wx_grid.h:289
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