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
44{
45 // This does not mean the row contains only one item. For instance,
46 // a row might contain references to U1A and U1B but symbol fields aren't
47 // independently editable for units of one symbol, so the row DATA_MODEL_ROW
48 // will contain multiple references but not be expandable to multiple child items
53 EXPANDED_CHILD
54};
55
56
57class KICOMMON_API WX_GRID_TABLE_BASE : public wxGridTableBase
58{
59public:
61 {
62 for( const auto& [col, attr] : m_colAttrs )
63 wxSafeDecRef( attr );
64 }
65
66 void SetColAttr( wxGridCellAttr* aAttr, int aCol ) override
67 {
68 wxSafeDecRef( m_colAttrs[aCol] );
69 m_colAttrs[aCol] = aAttr;
70 }
71
72 wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind ) override
73 {
74 if( m_colAttrs[aCol] )
75 {
76 m_colAttrs[aCol]->IncRef();
77 return enhanceAttr( m_colAttrs[aCol], aRow, aCol, aKind );
78 }
79
80 return enhanceAttr( nullptr, aRow, aCol, aKind );
81 }
82
83 virtual bool IsExpanderColumn( int aCol ) const { return false; }
84 virtual ROW_STATE GetRowState( int aRow ) const { return ROW_STATE::NON_EXPANDABLE; }
85
93 virtual bool HasUndoStateSerialization() const { return false; }
94 virtual wxString SerializeUndoState() const { return wxEmptyString; }
95 virtual void RestoreUndoState( const wxString& aState ) {}
96
97 void Clear() override
98 {
99 if( GetNumberRows() )
100 DeleteRows( 0, GetNumberRows() );
101 }
102
103protected:
104 wxGridCellAttr* enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
105 wxGridCellAttr::wxAttrKind aKind );
106
107protected:
108 std::map<int, wxGridCellAttr*> m_colAttrs;
109};
110
111
112class KICOMMON_API WX_GRID : public wxGrid
113{
114public:
115 // Constructor has to be wxFormBuilder-compatible
116 WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition,
117 const wxSize& size = wxDefaultSize, long style = wxWANTS_CHARS,
118 const wxString& name = wxGridNameStr );
119
120 ~WX_GRID() override;
121
128 void SetColLabelSize( int aHeight ); // Yes, we're hiding a non-virtual method
129
134 void SetLabelFont( const wxFont& aFont ); // Yes, we're hiding a non-virtual method
135
142 void EnableAlternateRowColors( bool aEnable = true );
143
147 void EnableCursorRowColumnHighlight( bool aEnable = true ) { m_cursorRowColumnHighlight = aEnable; }
148
150
156 wxString GetShownColumnsAsString();
157 std::bitset<64> GetShownColumns();
158
162 void ShowHideColumns( const wxString& shownColumns );
163
168 void SetTable( wxGridTableBase* table, bool aTakeOwnership = false );
169
174 void DestroyTable( wxGridTableBase* aTable );
175
182 bool CommitPendingChanges( bool aQuietMode = false );
183 bool CancelPendingChanges();
184
185 void OnAddRow( const std::function<std::pair<int, int>()>& aAdder );
186
191 void OnDeleteRows( const std::function<void( int row )>& aDeleter );
192
193 void OnDeleteRows( const std::function<bool( int row )>& aFilter,
194 const std::function<void( int row )>& aDeleter );
195
199 void SwapRows( int aRowA, int aRowB );
200 void OnMoveRowUp( const std::function<void( int row )>& aMover );
201 void OnMoveRowDown( const std::function<void( int row )>& aMover );
202 void OnMoveRowUp( const std::function<bool( int row )>& aFilter,
203 const std::function<void( int row )>& aMover );
204 void OnMoveRowDown( const std::function<bool( int row )>& aFilter,
205 const std::function<void( int row )>& aMover );
206
212 void SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol = 0 );
213
214 void SetAutoEvalCols( const std::vector<int>& aCols ) { m_autoEvalCols = aCols; }
215
219 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType );
220
224 void SetAutoEvalColUnits( int col, EDA_UNITS aUnit );
225
234 int GetUnitValue( int aRow, int aCol );
235
243 std::optional<int> GetOptionalUnitValue( int aRow, int aCol );
244
248 void SetUnitValue( int aRow, int aCol, int aValue );
249
253 void SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue );
254
264 int GetVisibleWidth( int aCol, bool aHeader = true, bool aContents = true, bool aKeep = false );
265
270 void EnsureColLabelsVisible();
271
280 void ShowEditorOnMouseUp() { m_waitForSlowClick = true; }
281 void CancelShowEditorOnMouseUp() { m_waitForSlowClick = false; }
282
288 void ClearRows( bool aUpdateLabels = true )
289 {
290 if( GetNumberRows() > 0 )
291 DeleteRows( 0, GetNumberRows(), aUpdateLabels );
292 }
293
297 static void CellEditorSetMargins( wxTextEntryBase* aEntry );
298
302 static void CellEditorTransformSizeRect( wxRect& aRect );
303
311 void OverrideMinSize( double aXPct, double aYPct )
312 {
313 wxSize size = DoGetBestSize();
314 m_minSizeOverride = wxSize( KiROUND( size.x * aXPct ), KiROUND( size.y * aYPct ) );
315 }
316
324 void SetMinVisibleRows( wxWindow* aDialog, int aMinRows );
325
335 static int CapHeightToVisibleRows( int aMinHeight, int aHeaderHeight,
336 const std::vector<int>& aRowHeights, int aMaxRows );
337
338 wxSize DoGetBestSize() const override
339 {
341 return m_minSizeOverride.value();
342 else
343 return wxGrid::DoGetBestSize();
344 }
345
350 void SetupColumnAutosizer( int aFlexibleCol );
351
353
355
356 void RecomputeGridWidths();
357
358protected:
363 void DrawColLabel( wxDC& dc, int col ) override;
364
368 void DrawRowLabel( wxDC& dc, int row ) override;
369
373 void DrawCornerLabel( wxDC& dc ) override;
374
375 void onGridColMove( wxGridEvent& aEvent );
376 void onGridCellSelect( wxGridEvent& aEvent );
377 void onCellEditorShown( wxGridEvent& aEvent );
378 void onCellEditorHidden( wxGridEvent& aEvent );
379
380#ifdef __WXMSW__
382 void onIdleRefreshHighlight( wxIdleEvent& aEvent );
383#endif
384
385 void onDPIChanged(wxDPIChangedEvent& event);
386
388 {
389 if( m_unitsProviders.contains( aCol ) )
390 return m_unitsProviders.at( aCol );
391 else
392 return m_unitsProviders.begin()->second;
393 }
394
398 std::pair<EDA_UNITS, EDA_DATA_TYPE> getColumnUnits( int aCol ) const;
399
400private:
401 void onSizeEvent( wxSizeEvent& aEvent );
402
403protected:
406
407 std::map<int, UNITS_PROVIDER*> m_unitsProviders;
408 std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
409 std::vector<int> m_autoEvalCols;
410 std::unordered_map<int, std::pair<EDA_UNITS, EDA_DATA_TYPE>> m_autoEvalColsUnits;
411 std::map< std::pair<int, int>, std::pair<wxString, wxString> > m_evalBeforeAfter;
412
413 std::optional<wxSize> m_minSizeOverride;
414
415 std::map<int, int> m_autosizedCols; // map of col : min_width
417
418 bool m_gridWidthsDirty = true;
419 int m_gridWidth = 0;
420
422};
const char * name
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:995
Icon provider for the "standard" row indicators, for example in layer selection lists.
~WX_GRID_TABLE_BASE() override
Definition wx_grid.h:60
virtual void RestoreUndoState(const wxString &aState)
Definition wx_grid.h:95
virtual ROW_STATE GetRowState(int aRow) const
Definition wx_grid.h:84
virtual bool IsExpanderColumn(int aCol) const
Definition wx_grid.h:83
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:108
void Clear() override
Definition wx_grid.h:97
wxGridCellAttr * GetAttr(int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
Definition wx_grid.h:72
void SetColAttr(wxGridCellAttr *aAttr, int aCol) override
Definition wx_grid.h:66
virtual wxString SerializeUndoState() const
Definition wx_grid.h:94
virtual bool HasUndoStateSerialization() const
Optional identity-based serialization for the host dialog's Ctrl+Z/Ctrl+Y.
Definition wx_grid.h:93
int m_flexibleCol
Definition wx_grid.h:416
std::map< int, int > m_autosizedCols
Definition wx_grid.h:415
bool m_gridWidthsDirty
Definition wx_grid.h:418
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:341
bool m_weOwnTable
Definition wx_grid.h:404
std::map< int, UNITS_PROVIDER * > m_unitsProviders
Definition wx_grid.h:407
std::optional< wxSize > m_minSizeOverride
Definition wx_grid.h:413
std::unordered_map< int, std::pair< EDA_UNITS, EDA_DATA_TYPE > > m_autoEvalColsUnits
Definition wx_grid.h:410
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:311
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:326
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:250
void SetGridWidthsDirty()
Definition wx_grid.h:352
void EnableCursorRowColumnHighlight(bool aEnable=true)
Enable repainting for grids whose table uses the cursor row and column when rendering cells.
Definition wx_grid.h:147
std::vector< int > m_autoEvalCols
Definition wx_grid.h:409
UNITS_PROVIDER * getUnitsProvider(int aCol) const
Definition wx_grid.h:387
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
Definition wx_grid.h:411
void CancelShowEditorOnMouseUp()
Definition wx_grid.h:281
bool m_cursorRowColumnHighlight
Definition wx_grid.h:405
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:288
ROW_ICON_PROVIDER * m_rowIconProvider
Definition wx_grid.h:421
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:280
bool IsCursorRowColumnHighlightEnabled() const
Definition wx_grid.h:149
int m_gridWidth
Definition wx_grid.h:419
void SetAutoEvalCols(const std::vector< int > &aCols)
Definition wx_grid.h:214
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:386
ROW_ICON_PROVIDER * GetRowIconProvider() const
Definition wx_grid.h:354
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
Definition wx_grid.h:408
wxSize DoGetBestSize() const override
Definition wx_grid.h:338
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
enum KICOMMON_API ROW_STATE
Used primarily by the fields tables to collapse multiple grouped symbols, etc.
Definition wx_grid.h:44
NON_EXPANDABLE
Definition wx_grid.h:49
COLLAPSED_DURING_SORT
Definition wx_grid.h:51
EXPANDED_PARENT
Definition wx_grid.h:52
COLLAPSED
Definition wx_grid.h:50