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{
44public:
46 {
47 for( const auto& [col, attr] : m_colAttrs )
48 wxSafeDecRef( attr );
49 }
50
51 void SetColAttr( wxGridCellAttr* aAttr, int aCol ) override
52 {
53 wxSafeDecRef( m_colAttrs[aCol] );
54 m_colAttrs[aCol] = aAttr;
55 }
56
57 wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind ) override
58 {
59 if( m_colAttrs[aCol] )
60 {
61 m_colAttrs[aCol]->IncRef();
62 return enhanceAttr( m_colAttrs[aCol], aRow, aCol, aKind );
63 }
64
65 return nullptr;
66 }
67
68protected:
69 wxGridCellAttr* enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
70 wxGridCellAttr::wxAttrKind aKind );
71
72protected:
73 std::map<int, wxGridCellAttr*> m_colAttrs;
74};
75
76
77class WX_GRID : public wxGrid
78{
79public:
80 // Constructor has to be wxFormBuilder-compatible
81 WX_GRID( wxWindow *parent, wxWindowID id,
82 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
83 long style = wxWANTS_CHARS, const wxString& name = wxGridNameStr );
84
85 ~WX_GRID() override;
86
93 void SetColLabelSize( int aHeight ); // Yes, we're hiding a non-virtual method
94
99 void SetLabelFont( const wxFont& aFont ); // Yes, we're hiding a non-virtual method
100
107 void EnableAlternateRowColors( bool aEnable = true );
108
114 wxString GetShownColumnsAsString();
115 std::bitset<64> GetShownColumns();
116
120 void ShowHideColumns( const wxString& shownColumns );
121
126 void SetTable( wxGridTableBase* table, bool aTakeOwnership = false );
127
132 void DestroyTable( wxGridTableBase* aTable );
133
140 bool CommitPendingChanges( bool aQuietMode = false );
142
143 void OnAddRow( const std::function<std::pair<int, int>()>& aAdder );
144
149 void OnDeleteRows( const std::function<void( int row )>& aDeleter );
150
151 void OnDeleteRows( const std::function<bool( int row )>& aFilter,
152 const std::function<void( int row )>& aDeleter );
153
157 void SwapRows( int aRowA, int aRowB );
158 void OnMoveRowUp( const std::function<void( int row )>& aMover );
159 void OnMoveRowDown( const std::function<void( int row )>& aMover );
160 void OnMoveRowUp( const std::function<bool( int row )>& aFilter,
161 const std::function<void( int row )>& aMover );
162 void OnMoveRowDown( const std::function<bool( int row )>& aFilter,
163 const std::function<void( int row )>& aMover );
164
170 void SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol = 0 );
171
172 void SetAutoEvalCols( const std::vector<int>& aCols ) { m_autoEvalCols = aCols; }
173
177 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType );
178
182 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit );
183
192 int GetUnitValue( int aRow, int aCol );
193
194
202 std::optional<int> GetOptionalUnitValue( int aRow, int aCol );
203
207 void SetUnitValue( int aRow, int aCol, int aValue );
208
212 void SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue );
213
223 int GetVisibleWidth( int aCol, bool aHeader = true, bool aContents = true, bool aKeep = false );
224
230
239 void ShowEditorOnMouseUp() { m_waitForSlowClick = true; }
240 void CancelShowEditorOnMouseUp() { m_waitForSlowClick = false; }
241
247 void ClearRows( bool aUpdateLabels = true )
248 {
249 if( GetNumberRows() > 0 )
250 DeleteRows( 0, GetNumberRows(), aUpdateLabels );
251 }
252
256 static void CellEditorSetMargins( wxTextEntryBase* aEntry );
257
261 static void CellEditorTransformSizeRect( wxRect& aRect );
262
269 void OverrideMinSize( double aXPct, double aYPct )
270 {
271 wxSize size = DoGetBestSize();
272 m_minSizeOverride = wxSize( KiROUND( size.x * aXPct ), KiROUND( size.y * aYPct ) );
273 }
274
275 wxSize DoGetBestSize() const override
276 {
278 return m_minSizeOverride.value();
279 else
280 return wxGrid::DoGetBestSize();
281 }
282
283protected:
288 void DrawColLabel( wxDC& dc, int col ) override;
289
293 void DrawRowLabel( wxDC& dc, int row ) override;
294
298 void DrawCornerLabel( wxDC& dc ) override;
299
300 void onGridColMove( wxGridEvent& aEvent );
301 void onGridCellSelect( wxGridEvent& aEvent );
302 void onCellEditorShown( wxGridEvent& aEvent );
303 void onCellEditorHidden( wxGridEvent& aEvent );
304
305 void onDPIChanged(wxDPIChangedEvent& event);
306
308 {
309 if( m_unitsProviders.contains( aCol ) )
310 return m_unitsProviders.at( aCol );
311 else
312 return m_unitsProviders.begin()->second;
313 }
314
318 std::pair<EDA_UNITS, EDA_DATA_TYPE> getColumnUnits( int aCol ) const;
319
320protected:
322
323 std::map<int, UNITS_PROVIDER*> m_unitsProviders;
324 std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
325 std::vector<int> m_autoEvalCols;
326 std::unordered_map<int, std::pair<EDA_UNITS, EDA_DATA_TYPE>> m_autoEvalColsUnits;
327 std::map< std::pair<int, int>, std::pair<wxString, wxString> > m_evalBeforeAfter;
328
329 std::optional<wxSize> m_minSizeOverride;
330};
331
332#endif //KICAD_WX_GRID_H
const char * name
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
~WX_GRID_TABLE_BASE() override
Definition wx_grid.h:45
wxGridCellAttr * enhanceAttr(wxGridCellAttr *aInputAttr, int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind)
Definition wx_grid.cpp:45
std::map< int, wxGridCellAttr * > m_colAttrs
Definition wx_grid.h:73
wxGridCellAttr * GetAttr(int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
Definition wx_grid.h:57
void SetColAttr(wxGridCellAttr *aAttr, int aCol) override
Definition wx_grid.h:51
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:906
void onGridCellSelect(wxGridEvent &aEvent)
Definition wx_grid.cpp:320
~WX_GRID() override
Definition wx_grid.cpp:224
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:261
bool m_weOwnTable
Definition wx_grid.h:321
void onDPIChanged(wxDPIChangedEvent &event)
Definition wx_grid.cpp:235
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition wx_grid.cpp:487
void OnMoveRowUp(const std::function< void(int row)> &aMover)
Definition wx_grid.cpp:756
std::map< int, UNITS_PROVIDER * > m_unitsProviders
Definition wx_grid.h:323
std::optional< wxSize > m_minSizeOverride
Definition wx_grid.h:329
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:267
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:443
std::unordered_map< int, std::pair< EDA_UNITS, EDA_DATA_TYPE > > m_autoEvalColsUnits
Definition wx_grid.h:326
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:269
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:831
bool CancelPendingChanges()
Definition wx_grid.cpp:594
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:246
void SwapRows(int aRowA, int aRowB)
These aren't that tricky, but might as well share code.
Definition wx_grid.cpp:745
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:974
void SetUnitValue(int aRow, int aCol, int aValue)
Set a unitized cell's value.
Definition wx_grid.cpp:880
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:204
int GetUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition wx_grid.cpp:844
std::vector< int > m_autoEvalCols
Definition wx_grid.h:325
UNITS_PROVIDER * getUnitsProvider(int aCol) const
Definition wx_grid.h:307
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
Definition wx_grid.h:327
void CancelShowEditorOnMouseUp()
Definition wx_grid.h:240
void DrawCornerLabel(wxDC &dc) override
A re-implementation of wxGrid::DrawCornerLabel which draws flat borders.
Definition wx_grid.cpp:505
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:247
void onCellEditorHidden(wxGridEvent &aEvent)
Definition wx_grid.cpp:362
void OnMoveRowDown(const std::function< void(int row)> &aMover)
Definition wx_grid.cpp:789
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:239
void onGridColMove(wxGridEvent &aEvent)
Definition wx_grid.cpp:899
void SetOptionalUnitValue(int aRow, int aCol, std::optional< int > aValue)
Set a unitized cell's optional value.
Definition wx_grid.cpp:893
void onCellEditorShown(wxGridEvent &aEvent)
Definition wx_grid.cpp:347
void EnsureColLabelsVisible()
Ensure the height of the row displaying the column labels is enough, even if labels are multiline tex...
Definition wx_grid.cpp:946
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition wx_grid.cpp:694
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition wx_grid.cpp:457
void OnAddRow(const std::function< std::pair< int, int >()> &aAdder)
Definition wx_grid.cpp:674
void DrawRowLabel(wxDC &dc, int row) override
A re-implementation of wxGrid::DrawRowLabel which draws flat borders.
Definition wx_grid.cpp:563
std::bitset< 64 > GetShownColumns()
Definition wx_grid.cpp:476
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:862
void SetAutoEvalCols(const std::vector< int > &aCols)
Definition wx_grid.h:172
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:302
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:822
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition wx_grid.cpp:622
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
Definition wx_grid.h:324
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:525
wxSize DoGetBestSize() const override
Definition wx_grid.h:275
static void CellEditorTransformSizeRect(wxRect &aRect)
A helper function to tweak sizes of text-based cell editors depending on OS.
Definition wx_grid.cpp:84
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:38
EDA_UNITS
Definition eda_units.h:48