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
36#include <units_provider.h>
37
38class wxTextEntryBase;
40
41
50
51
52class WX_GRID_TABLE_BASE : public wxGridTableBase
53{
54public:
56 {
57 for( const auto& [col, attr] : m_colAttrs )
58 wxSafeDecRef( attr );
59 }
60
61 void SetColAttr( wxGridCellAttr* aAttr, int aCol ) override
62 {
63 wxSafeDecRef( m_colAttrs[aCol] );
64 m_colAttrs[aCol] = aAttr;
65 }
66
67 wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind ) override
68 {
69 if( m_colAttrs[aCol] )
70 {
71 m_colAttrs[aCol]->IncRef();
72 return enhanceAttr( m_colAttrs[aCol], aRow, aCol, aKind );
73 }
74
75 return nullptr;
76 }
77
78 virtual bool IsExpanderColumn( int aCol ) const { return false; }
79 virtual GROUP_TYPE GetGroupType( int aRow ) const { return GROUP_SINGLETON; }
80
81protected:
82 wxGridCellAttr* enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
83 wxGridCellAttr::wxAttrKind aKind );
84
85protected:
86 std::map<int, wxGridCellAttr*> m_colAttrs;
87};
88
89
90class WX_GRID : public wxGrid
91{
92public:
93 // Constructor has to be wxFormBuilder-compatible
94 WX_GRID( wxWindow *parent, wxWindowID id,
95 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
96 long style = wxWANTS_CHARS, const wxString& name = wxGridNameStr );
97
98 ~WX_GRID() override;
99
106 void SetColLabelSize( int aHeight ); // Yes, we're hiding a non-virtual method
107
112 void SetLabelFont( const wxFont& aFont ); // Yes, we're hiding a non-virtual method
113
120 void EnableAlternateRowColors( bool aEnable = true );
121
127 wxString GetShownColumnsAsString();
128 std::bitset<64> GetShownColumns();
129
133 void ShowHideColumns( const wxString& shownColumns );
134
139 void SetTable( wxGridTableBase* table, bool aTakeOwnership = false );
140
145 void DestroyTable( wxGridTableBase* aTable );
146
153 bool CommitPendingChanges( bool aQuietMode = false );
155
156 void OnAddRow( const std::function<std::pair<int, int>()>& aAdder );
157
162 void OnDeleteRows( const std::function<void( int row )>& aDeleter );
163
164 void OnDeleteRows( const std::function<bool( int row )>& aFilter,
165 const std::function<void( int row )>& aDeleter );
166
170 void SwapRows( int aRowA, int aRowB );
171 void OnMoveRowUp( const std::function<void( int row )>& aMover );
172 void OnMoveRowDown( const std::function<void( int row )>& aMover );
173 void OnMoveRowUp( const std::function<bool( int row )>& aFilter,
174 const std::function<void( int row )>& aMover );
175 void OnMoveRowDown( const std::function<bool( int row )>& aFilter,
176 const std::function<void( int row )>& aMover );
177
183 void SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol = 0 );
184
185 void SetAutoEvalCols( const std::vector<int>& aCols ) { m_autoEvalCols = aCols; }
186
190 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType );
191
195 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit );
196
205 int GetUnitValue( int aRow, int aCol );
206
207
215 std::optional<int> GetOptionalUnitValue( int aRow, int aCol );
216
220 void SetUnitValue( int aRow, int aCol, int aValue );
221
225 void SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue );
226
236 int GetVisibleWidth( int aCol, bool aHeader = true, bool aContents = true, bool aKeep = false );
237
243
252 void ShowEditorOnMouseUp() { m_waitForSlowClick = true; }
253 void CancelShowEditorOnMouseUp() { m_waitForSlowClick = false; }
254
260 void ClearRows( bool aUpdateLabels = true )
261 {
262 if( GetNumberRows() > 0 )
263 DeleteRows( 0, GetNumberRows(), aUpdateLabels );
264 }
265
269 static void CellEditorSetMargins( wxTextEntryBase* aEntry );
270
274 static void CellEditorTransformSizeRect( wxRect& aRect );
275
282 void OverrideMinSize( double aXPct, double aYPct )
283 {
284 wxSize size = DoGetBestSize();
285 m_minSizeOverride = wxSize( KiROUND( size.x * aXPct ), KiROUND( size.y * aYPct ) );
286 }
287
288 wxSize DoGetBestSize() const override
289 {
291 return m_minSizeOverride.value();
292 else
293 return wxGrid::DoGetBestSize();
294 }
295
300 void SetupColumnAutosizer( int aFlexibleCol );
301
303
305
306protected:
311 void DrawColLabel( wxDC& dc, int col ) override;
312
316 void DrawRowLabel( wxDC& dc, int row ) override;
317
321 void DrawCornerLabel( wxDC& dc ) override;
322
323 void onGridColMove( wxGridEvent& aEvent );
324 void onGridCellSelect( wxGridEvent& aEvent );
325 void onCellEditorShown( wxGridEvent& aEvent );
326 void onCellEditorHidden( wxGridEvent& aEvent );
327
328 void onDPIChanged(wxDPIChangedEvent& event);
329
331 {
332 if( m_unitsProviders.contains( aCol ) )
333 return m_unitsProviders.at( aCol );
334 else
335 return m_unitsProviders.begin()->second;
336 }
337
341 std::pair<EDA_UNITS, EDA_DATA_TYPE> getColumnUnits( int aCol ) const;
342
343private:
344 void recomputeGridWidths();
345
346 void onSizeEvent( wxSizeEvent& aEvent );
347
348protected:
350
351 std::map<int, UNITS_PROVIDER*> m_unitsProviders;
352 std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
353 std::vector<int> m_autoEvalCols;
354 std::unordered_map<int, std::pair<EDA_UNITS, EDA_DATA_TYPE>> m_autoEvalColsUnits;
355 std::map< std::pair<int, int>, std::pair<wxString, wxString> > m_evalBeforeAfter;
356
357 std::optional<wxSize> m_minSizeOverride;
358
359 std::map<int, int> m_autosizedCols; // map of col : min_width
361
362 bool m_gridWidthsDirty = true;
363 int m_gridWidth = 0;
364
366};
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:55
virtual GROUP_TYPE GetGroupType(int aRow) const
Definition wx_grid.h:79
virtual bool IsExpanderColumn(int aCol) const
Definition wx_grid.h:78
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:86
wxGridCellAttr * GetAttr(int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
Definition wx_grid.h:67
void SetColAttr(wxGridCellAttr *aAttr, int aCol) override
Definition wx_grid.h:61
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:911
int m_flexibleCol
Definition wx_grid.h:360
void onGridCellSelect(wxGridEvent &aEvent)
Definition wx_grid.cpp:325
std::map< int, int > m_autosizedCols
Definition wx_grid.h:359
bool m_gridWidthsDirty
Definition wx_grid.h:362
~WX_GRID() override
Definition wx_grid.cpp:227
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:349
void onDPIChanged(wxDPIChangedEvent &event)
Definition wx_grid.cpp:240
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition wx_grid.cpp:492
void OnMoveRowUp(const std::function< void(int row)> &aMover)
Definition wx_grid.cpp:761
std::map< int, UNITS_PROVIDER * > m_unitsProviders
Definition wx_grid.h:351
std::optional< wxSize > m_minSizeOverride
Definition wx_grid.h:357
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:448
std::unordered_map< int, std::pair< EDA_UNITS, EDA_DATA_TYPE > > m_autoEvalColsUnits
Definition wx_grid.h:354
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:282
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:836
bool CancelPendingChanges()
Definition wx_grid.cpp:599
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:750
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:979
void SetUnitValue(int aRow, int aCol, int aValue)
Set a unitized cell's value.
Definition wx_grid.cpp:885
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:302
int GetUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition wx_grid.cpp:849
std::vector< int > m_autoEvalCols
Definition wx_grid.h:353
UNITS_PROVIDER * getUnitsProvider(int aCol) const
Definition wx_grid.h:330
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
Definition wx_grid.h:355
void CancelShowEditorOnMouseUp()
Definition wx_grid.h:253
void DrawCornerLabel(wxDC &dc) override
A re-implementation of wxGrid::DrawCornerLabel which draws flat borders.
Definition wx_grid.cpp:510
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:260
ROW_ICON_PROVIDER * m_rowIconProvider
Definition wx_grid.h:365
void onCellEditorHidden(wxGridEvent &aEvent)
Definition wx_grid.cpp:367
void SetupColumnAutosizer(int aFlexibleCol)
Set autosize behaviour using wxFormBuilder column widths as minimums, with a single specified growabl...
Definition wx_grid.cpp:989
void OnMoveRowDown(const std::function< void(int row)> &aMover)
Definition wx_grid.cpp:794
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:252
void onGridColMove(wxGridEvent &aEvent)
Definition wx_grid.cpp:904
void SetOptionalUnitValue(int aRow, int aCol, std::optional< int > aValue)
Set a unitized cell's optional value.
Definition wx_grid.cpp:898
void onSizeEvent(wxSizeEvent &aEvent)
Definition wx_grid.cpp:1066
void onCellEditorShown(wxGridEvent &aEvent)
Definition wx_grid.cpp:352
void recomputeGridWidths()
Definition wx_grid.cpp:1024
void EnsureColLabelsVisible()
Ensure the height of the row displaying the column labels is enough, even if labels are multiline tex...
Definition wx_grid.cpp:951
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition wx_grid.cpp:699
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition wx_grid.cpp:462
void OnAddRow(const std::function< std::pair< int, int >()> &aAdder)
Definition wx_grid.cpp:679
int m_gridWidth
Definition wx_grid.h:363
void DrawRowLabel(wxDC &dc, int row) override
A re-implementation of wxGrid::DrawRowLabel which draws flat borders.
Definition wx_grid.cpp:568
std::bitset< 64 > GetShownColumns()
Definition wx_grid.cpp:481
static void CellEditorSetMargins(wxTextEntryBase *aEntry)
A helper function to set OS-specific margins for text-based cell editors.
Definition wx_grid.cpp:78
std::optional< int > GetOptionalUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition wx_grid.cpp:867
void SetAutoEvalCols(const std::vector< int > &aCols)
Definition wx_grid.h:185
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:304
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:827
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition wx_grid.cpp:627
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
Definition wx_grid.h:352
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:530
wxSize DoGetBestSize() const override
Definition wx_grid.h:288
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
GROUP_TYPE
Definition wx_grid.h:43
@ GROUP_COLLAPSED_DURING_SORT
Definition wx_grid.h:46
@ GROUP_EXPANDED
Definition wx_grid.h:47
@ GROUP_COLLAPSED
Definition wx_grid.h:45
@ GROUP_SINGLETON
Definition wx_grid.h:44
@ CHILD_ITEM
Definition wx_grid.h:48