KiCad PCB EDA Suite
Loading...
Searching...
No Matches
wx_grid.cpp
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#include <wx/colour.h>
21#include <wx/tokenzr.h>
22#include <wx/dc.h>
23#include <wx/settings.h>
24#include <wx/event.h> // Needed for textentry.h on MSW
25#include <wx/textentry.h>
26
29#include <widgets/wx_grid.h>
30#include <widgets/ui_common.h>
31#include <algorithm>
32#include <vector>
33#include <core/kicad_algo.h>
34#include <gal/color4d.h>
35#include <kiplatform/ui.h>
36#include <utility>
37
38#include <pgm_base.h>
40#include <dialog_shim.h>
41
42
43wxGridCellAttr* WX_GRID_TABLE_BASE::enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
44 wxGridCellAttr::wxAttrKind aKind )
45{
46 wxGridCellAttr* attr = aInputAttr;
47
48 if( wxGridCellAttrProvider* provider = GetAttrProvider() )
49 {
50 wxGridCellAttr* providerAttr = provider->GetAttr( aRow, aCol, aKind );
51
52 if( providerAttr )
53 {
54 attr = new wxGridCellAttr;
55 attr->SetKind( wxGridCellAttr::Merged );
56
57 if( aInputAttr )
58 {
59 attr->MergeWith( aInputAttr );
60 aInputAttr->DecRef();
61 }
62
63 attr->MergeWith( providerAttr );
64 providerAttr->DecRef();
65 }
66 }
67
68 return attr;
69}
70
71
72#define MIN_GRIDCELL_MARGIN FromDIP( 2 )
73
74
75void WX_GRID::CellEditorSetMargins( wxTextEntryBase* aEntry )
76{
77 // This is consistent with wxGridCellTextEditor. But works differently across platforms of course.
78 aEntry->SetMargins( 0, 0 );
79}
80
81
83{
84#if defined( __WXMSW__ ) || defined( __WXGTK__ )
85 aRect.Deflate( 2 );
86#endif
87}
88
89
91{
92 KIGFX::COLOR4D bg = wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK );
93 KIGFX::COLOR4D fg = wxSystemSettings::GetColour( wxSYS_COLOUR_ACTIVEBORDER );
94 KIGFX::COLOR4D border = fg.Mix( bg, 0.50 );
95 return border.ToColour();
96}
97
98
99class WX_GRID_CORNER_HEADER_RENDERER : public wxGridCornerHeaderRendererDefault
100{
101public:
102 void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
103 {
104 wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
105 wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
106
107 rect.SetTop( rect.GetTop() + 1 );
108 rect.SetLeft( rect.GetLeft() + 1 );
109 rect.SetBottom( rect.GetBottom() - 1 );
110 rect.SetRight( rect.GetRight() - 1 );
111 dc.DrawRectangle( rect );
112 }
113};
114
115
116class WX_GRID_COLUMN_HEADER_RENDERER : public wxGridColumnHeaderRendererDefault
117{
118public:
119 void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
120 {
121 wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
122 wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
123
124 rect.SetTop( rect.GetTop() + 1 );
125 rect.SetLeft( rect.GetLeft() );
126 rect.SetBottom( rect.GetBottom() - 1 );
127 rect.SetRight( rect.GetRight() - 1 );
128 dc.DrawRectangle( rect );
129 }
130};
131
132
133class WX_GRID_ROW_HEADER_RENDERER : public wxGridRowHeaderRendererDefault
134{
135public:
136 void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
137 {
138 wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
139 wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
140
141 rect.SetTop( rect.GetTop() + 1 );
142 rect.SetLeft( rect.GetLeft() + 1 );
143 rect.SetBottom( rect.GetBottom() - 1 );
144 rect.SetRight( rect.GetRight() );
145 dc.DrawRectangle( rect );
146 }
147};
148
149
154class WX_GRID_ALT_ROW_COLOR_PROVIDER : public wxGridCellAttrProvider
155{
156public:
157 WX_GRID_ALT_ROW_COLOR_PROVIDER( const wxColor& aBaseColor ) :
158 wxGridCellAttrProvider(),
159 m_attrEven( new wxGridCellAttr() )
160 {
161 UpdateColors( aBaseColor );
162 }
163
164 void UpdateColors( const wxColor& aBaseColor )
165 {
166 // Choose the default color, taking into account if the dark mode theme is enabled
167 wxColor rowColor = aBaseColor.ChangeLightness( KIPLATFORM::UI::IsDarkTheme() ? 105 : 95 );
168
169 m_attrEven->SetBackgroundColour( rowColor );
170 }
171
172 wxGridCellAttr* GetAttr( int row, int col, wxGridCellAttr::wxAttrKind kind ) const override
173 {
174 wxGridCellAttrPtr cellAttr( wxGridCellAttrProvider::GetAttr( row, col, kind ) );
175
176 // Just pass through the cell attribute on odd rows (start normal to allow for the
177 // header row)
178 if( !( row % 2 ) )
179 return cellAttr.release();
180
181 if( !cellAttr )
182 {
183 cellAttr = m_attrEven;
184 }
185 else
186 {
187 if( !cellAttr->HasBackgroundColour() )
188 {
189 cellAttr = cellAttr->Clone();
190 cellAttr->SetBackgroundColour( m_attrEven->GetBackgroundColour() );
191 }
192 }
193
194 return cellAttr.release();
195 }
196
197private:
198 wxGridCellAttrPtr m_attrEven;
199};
200
201
202WX_GRID::WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style,
203 const wxString& name ) :
204 wxGrid( parent, id, pos, size, style, name ),
205 m_weOwnTable( false )
206{
207 // Grids with comboboxes need a bit of extra height; other grids look better if they're consistent.
208 SetDefaultRowSize( GetDefaultRowSize() + FromDIP( 4 ) );
209
210 SetDefaultCellOverflow( false );
211
212 // Make sure the GUI font scales properly
213 SetDefaultCellFont( KIUI::GetControlFont( this ) );
215
217
218 Connect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
219 Connect( wxEVT_GRID_EDITOR_SHOWN, wxGridEventHandler( WX_GRID::onCellEditorShown ), nullptr, this );
220 Connect( wxEVT_GRID_EDITOR_HIDDEN, wxGridEventHandler( WX_GRID::onCellEditorHidden ), nullptr, this );
221}
222
223
225{
226 if( m_weOwnTable )
227 DestroyTable( GetTable() );
228
229 delete m_rowIconProvider;
230
231 Disconnect( wxEVT_GRID_EDITOR_SHOWN, wxGridEventHandler( WX_GRID::onCellEditorShown ), nullptr, this );
232 Disconnect( wxEVT_GRID_EDITOR_HIDDEN, wxGridEventHandler( WX_GRID::onCellEditorHidden ), nullptr, this );
233 Disconnect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
234}
235
236
237void WX_GRID::onDPIChanged(wxDPIChangedEvent& aEvt)
238{
239 // Workaround for wxWidgets bug where hidden column widths (stored as negative values)
240 // are not scaled during DPI changes, corrupting the internal m_colRights array.
241 // https://github.com/wxWidgets/wxWidgets/issues/26079
242 // Fix is to re-hide all hidden columns after the DPI change completes, which forces
243 // wxGrid to recalculate the column geometry correctly.
244
245 std::vector<int> hiddenCols;
246
247 for( int col = 0; col < GetNumberCols(); ++col )
248 {
249 if( !IsColShown( col ) )
250 hiddenCols.push_back( col );
251 }
252
253 aEvt.Skip();
254
255 if( !hiddenCols.empty() )
256 {
257 CallAfter(
258 [this, hiddenCols]()
259 {
260 for( int col : hiddenCols )
261 {
262 ShowCol( col );
263 HideCol( col );
264 }
265
266 ForceRefresh();
267 } );
268 }
269
270 CallAfter(
271 [this]()
272 {
273 wxGrid::SetColLabelSize( wxGRID_AUTOSIZE );
274 } );
275}
276
277
278void WX_GRID::SetColLabelSize( int aHeight )
279{
280 if( aHeight == 0 || aHeight == wxGRID_AUTOSIZE )
281 {
282 wxGrid::SetColLabelSize( aHeight );
283 return;
284 }
285
286 // Correct wxFormBuilder height for large fonts
287 int minHeight = GetCharHeight() + 2 * MIN_GRIDCELL_MARGIN;
288
289 wxGrid::SetColLabelSize( std::max( aHeight, minHeight ) );
290}
291
292
293void WX_GRID::SetLabelFont( const wxFont& aFont )
294{
295 wxGrid::SetLabelFont( KIUI::GetControlFont( this ) );
296}
297
298
299void WX_GRID::SetTable( wxGridTableBase* aTable, bool aTakeOwnership )
300{
301 // wxGrid::SetTable() messes up the column widths from wxFormBuilder so we have to save
302 // and restore them.
303 int numberCols = GetNumberCols();
304 int* formBuilderColWidths = new int[numberCols];
305
306 for( int i = 0; i < numberCols; ++i )
307 formBuilderColWidths[ i ] = GetColSize( i );
308
309 wxGrid::SetTable( aTable );
310
311 // wxGrid::SetTable() may change the number of columns, so prevent out-of-bounds access
312 // to formBuilderColWidths
313 numberCols = std::min( numberCols, GetNumberCols() );
314
315 for( int i = 0; i < numberCols; ++i )
316 {
317 // correct wxFormBuilder width for large fonts and/or long translations
318 int headingWidth = GetTextExtent( GetColLabelValue( i ) ).x + 2 * MIN_GRIDCELL_MARGIN;
319
320 SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
321 }
322
323 delete[] formBuilderColWidths;
324
325 EnableAlternateRowColors( Pgm().GetCommonSettings() && Pgm().GetCommonSettings()->m_Appearance.grid_striping );
326
327 Connect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
328 Connect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
329
330 m_weOwnTable = aTakeOwnership;
331}
332
333
335{
336 wxGridTableBase* table = wxGrid::GetTable();
337
338 wxCHECK_MSG( table, /* void */, "Tried to enable alternate row colors without a table assigned to the grid" );
339
340 if( aEnable )
341 {
342 wxColor color = wxGrid::GetDefaultCellBackgroundColour();
343 table->SetAttrProvider( new WX_GRID_ALT_ROW_COLOR_PROVIDER( color ) );
344 }
345 else
346 {
347 table->SetAttrProvider( nullptr );
348 }
349}
350
351
352void WX_GRID::onGridCellSelect( wxGridEvent& aEvent )
353{
354 // Highlight the selected cell.
355 // Calling SelectBlock() allows a visual effect when cells are selected by tab or arrow keys.
356 // Otherwise, one cannot really know what actual cell is selected.
357 int row = aEvent.GetRow();
358 int col = aEvent.GetCol();
359
360 if( row >= 0 && row < GetNumberRows() && col >= 0 && col < GetNumberCols() )
361 {
362 if( GetSelectionMode() == wxGrid::wxGridSelectCells )
363 {
364 SelectBlock( row, col, row, col, false );
365 }
366 else if( GetSelectionMode() == wxGrid::wxGridSelectRows
367 || GetSelectionMode() == wxGrid::wxGridSelectRowsOrColumns )
368 {
369 SelectBlock( row, 0, row, GetNumberCols() - 1, false );
370 }
371 else if( GetSelectionMode() == wxGrid::wxGridSelectColumns )
372 {
373 SelectBlock( 0, col, GetNumberRows() - 1, col, false );
374 }
375
376#ifdef __WXMSW__
377 // On Windows with wxWidgets 3.3+, the selection highlight can be drawn incorrectly
378 // on the first selection if the grid hasn't been fully laid out yet. Force a single
379 // deferred refresh after the first selection to ensure correct rendering.
380 if( !m_firstSelectionRefreshDone )
381 {
382 m_firstSelectionRefreshDone = true;
383 CallAfter( [this]() { ForceRefresh(); } );
384 }
385#endif
386 }
387}
388
389
390void WX_GRID::onCellEditorShown( wxGridEvent& aEvent )
391{
392 if( alg::contains( m_autoEvalCols, aEvent.GetCol() ) )
393 {
394 int row = aEvent.GetRow();
395 int col = aEvent.GetCol();
396
397 const std::pair<wxString, wxString>& beforeAfter = m_evalBeforeAfter[ { row, col } ];
398
399 if( GetCellValue( row, col ) == beforeAfter.second )
400 SetCellValue( row, col, beforeAfter.first );
401 }
402}
403
404
405void WX_GRID::onCellEditorHidden( wxGridEvent& aEvent )
406{
407 const int col = aEvent.GetCol();
408
409 if( alg::contains( m_autoEvalCols, col ) )
410 {
411 UNITS_PROVIDER* unitsProvider = getUnitsProvider( col );
412
413 auto cellUnitsData = getColumnUnits( col );
414 EDA_UNITS cellUnits = cellUnitsData.first;
415 EDA_DATA_TYPE cellDataType = cellUnitsData.second;
416
417 m_eval->SetDefaultUnits( cellUnits );
418
419 const int row = aEvent.GetRow();
420
421 // Determine if this cell is marked as holding nullable values
422 bool isNullable = false;
423 wxGridCellEditor* cellEditor = GetCellEditor( row, col );
424
425 if( cellEditor )
426 {
427 if( const GRID_CELL_NULLABLE_INTERFACE* nullable =
428 dynamic_cast<GRID_CELL_NULLABLE_INTERFACE*>( cellEditor ) )
429 isNullable = nullable->IsNullable();
430
431 cellEditor->DecRef();
432 }
433
434 CallAfter(
435 [this, row, col, isNullable, unitsProvider, cellDataType]()
436 {
437 // Careful; if called from CommitPendingChange() in a delete operation, the cell may
438 // no longer exist.
439 if( row >= GetNumberRows() || col >= GetNumberCols() )
440 return;
441
442 wxString stringValue = GetCellValue( row, col );
443 bool processedOk = true;
444
445 if( stringValue != UNITS_PROVIDER::NullUiString )
446 processedOk = m_eval->Process( stringValue );
447
448 if( processedOk )
449 {
450 wxString evalValue;
451
452 if( isNullable )
453 {
454 std::optional<int> val;
455
456 if( stringValue == UNITS_PROVIDER::NullUiString )
457 {
459 cellDataType );
460 }
461 else
462 {
463 val = unitsProvider->OptionalValueFromString( m_eval->Result(), cellDataType );
464 }
465
466 evalValue = unitsProvider->StringFromOptionalValue( val, true, cellDataType );
467 }
468 else
469 {
470 int val = unitsProvider->ValueFromString( m_eval->Result(), cellDataType );
471 evalValue = unitsProvider->StringFromValue( val, true, cellDataType );
472 }
473
474 if( stringValue != evalValue )
475 {
476 SetCellValue( row, col, evalValue );
477 m_evalBeforeAfter[{ row, col }] = { stringValue, evalValue };
478 }
479 }
480 } );
481 }
482
483 aEvent.Skip();
484}
485
486
487void WX_GRID::DestroyTable( wxGridTableBase* aTable )
488{
489 // wxGrid's destructor will crash trying to look up the cell attr if the edit control
490 // is left open. Normally it's closed in Validate(), but not if the user hit Cancel.
491 CommitPendingChanges( true /* quiet mode */ );
492
493 Disconnect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
494 Disconnect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
495
496 wxGrid::SetTable( nullptr );
497 delete aTable;
498}
499
500
502{
503 wxString shownColumns;
504
505 for( int i = 0; i < GetNumberCols(); ++i )
506 {
507 if( IsColShown( i ) )
508 {
509 if( shownColumns.Length() )
510 shownColumns << wxT( " " );
511
512 shownColumns << i;
513 }
514 }
515
516 return shownColumns;
517}
518
519
521{
522 std::bitset<64> shownColumns;
523
524 for( int ii = 0; ii < GetNumberCols(); ++ii )
525 shownColumns[ii] = IsColShown( ii );
526
527 return shownColumns;
528}
529
530
531void WX_GRID::ShowHideColumns( const wxString& shownColumns )
532{
533 for( int i = 0; i < GetNumberCols(); ++i )
534 HideCol( i );
535
536 wxStringTokenizer shownTokens( shownColumns, " \t\r\n", wxTOKEN_STRTOK );
537
538 while( shownTokens.HasMoreTokens() )
539 {
540 long colNumber;
541 shownTokens.GetNextToken().ToLong( &colNumber );
542
543 if( colNumber >= 0 && colNumber < GetNumberCols() )
544 ShowCol( (int) colNumber );
545 }
546}
547
548
550{
551 if( m_nativeColumnLabels )
552 wxGrid::DrawCornerLabel( dc );
553
554 wxRect rect( wxSize( m_rowLabelWidth, m_colLabelHeight ) );
555
557
558 // It is reported that we need to erase the background to avoid display artifacts; see #12055.
559 {
560 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
561 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
562 dc.DrawRectangle( rect.Inflate( 1 ) );
563 }
564
565 rend.DrawBorder( *this, dc, rect );
566}
567
568
569void WX_GRID::DrawColLabel( wxDC& dc, int col )
570{
571 if( m_nativeColumnLabels )
572 wxGrid::DrawColLabel( dc, col );
573
574 if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
575 return;
576
577 wxRect rect( GetColLeft( col ), 0, GetColWidth( col ), m_colLabelHeight );
578
580
581 // It is reported that we need to erase the background to avoid display artifacts; see #12055.
582 {
583 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
584 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
585 dc.DrawRectangle( rect.Inflate( 1 ) );
586 }
587
588 rend.DrawBorder( *this, dc, rect );
589
590 // Make sure fonts get scaled correctly on GTK HiDPI monitors
591 dc.SetFont( GetLabelFont() );
592
593 int hAlign, vAlign;
594 GetColLabelAlignment( &hAlign, &vAlign );
595 const int orient = GetColLabelTextOrientation();
596
597 if( col == 0 )
598 hAlign = wxALIGN_LEFT;
599
600 if( hAlign == wxALIGN_LEFT )
601 rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
602
603 rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
604}
605
606
607void WX_GRID::DrawRowLabel( wxDC& dc, int row )
608{
609 if( GetRowHeight( row ) <= 0 || m_rowLabelWidth <= 0 )
610 return;
611
612 wxRect rect( 0, GetRowTop( row ), m_rowLabelWidth, GetRowHeight( row ) );
613
614 static WX_GRID_ROW_HEADER_RENDERER rend;
615
616 // It is reported that we need to erase the background to avoid display artifacts; see #12055.
617 {
618 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
619 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
620 dc.DrawRectangle( rect.Inflate( 1 ) );
621 }
622
623 rend.DrawBorder( *this, dc, rect );
624
625 // Make sure fonts get scaled correctly on GTK HiDPI monitors
626 dc.SetFont( GetLabelFont() );
627
628 int hAlign, vAlign;
629 GetRowLabelAlignment(&hAlign, &vAlign);
630
631 if( hAlign == wxALIGN_LEFT )
632 rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
633
634 rend.DrawLabel( *this, dc, GetRowLabelValue( row ), rect, hAlign, vAlign, wxHORIZONTAL );
635}
636
637
639{
640 if( !IsCellEditControlEnabled() )
641 return true;
642
643 HideCellEditControl();
644
645 // do it after HideCellEditControl()
646 m_cellEditCtrlEnabled = false;
647
648 int row = m_currentCellCoords.GetRow();
649 int col = m_currentCellCoords.GetCol();
650
651 wxString oldval = GetCellValue( row, col );
652 wxString newval;
653
654 wxGridCellAttr* attr = GetCellAttr( row, col );
655 wxGridCellEditor* editor = attr->GetEditor( this, row, col );
656
657 editor->EndEdit( row, col, this, oldval, &newval );
658
659 editor->DecRef();
660 attr->DecRef();
661
662 return true;
663}
664
665
666bool WX_GRID::CommitPendingChanges( bool aQuietMode )
667{
668 if( !IsCellEditControlEnabled() )
669 return true;
670
671 if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
672 return false;
673
674 HideCellEditControl();
675
676 // do it after HideCellEditControl()
677 m_cellEditCtrlEnabled = false;
678
679 int row = m_currentCellCoords.GetRow();
680 int col = m_currentCellCoords.GetCol();
681
682 wxString oldval = GetCellValue( row, col );
683 wxString newval;
684
685 wxGridCellAttr* attr = GetCellAttr( row, col );
686 wxGridCellEditor* editor = attr->GetEditor( this, row, col );
687
688 bool changed = editor->EndEdit( row, col, this, oldval, &newval );
689
690 editor->DecRef();
691 attr->DecRef();
692
693 if( changed )
694 {
695 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
696 return false;
697
698 editor->ApplyEdit( row, col, this );
699
700 // for compatibility reasons dating back to wx 2.8 when this event
701 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
702 // didn't exist we allow vetoing this one too
703 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
704 {
705 // Event has been vetoed, set the data back.
706 SetCellValue( row, col, oldval );
707 return false;
708 }
709
710 if( DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
711 dlg->OnModify();
712 }
713
714 return true;
715}
716
717
718void WX_GRID::OnAddRow( const std::function<std::pair<int, int>()>& aAdder )
719{
720 if( !CommitPendingChanges() )
721 return;
722
723 auto [row, editCol] = aAdder();
724
725 // wx documentation is wrong, SetGridCursor does not make visible.
726 SetFocus();
727 MakeCellVisible( row, std::max( editCol, 0 ) );
728 SetGridCursor( row, std::max( editCol, 0 ) );
729
730 if( editCol >= 0 )
731 {
732 EnableCellEditControl( true );
733 ShowCellEditControl();
734 }
735}
736
737
738void WX_GRID::OnDeleteRows( const std::function<void( int row )>& aDeleter )
739{
741 []( int row )
742 {
743 return true;
744 },
745 aDeleter );
746}
747
748
749void WX_GRID::OnDeleteRows( const std::function<bool( int row )>& aFilter,
750 const std::function<void( int row )>& aDeleter )
751{
752 wxArrayInt selectedRows = GetSelectedRows();
753
754 auto addSelectedRow = [&]( int row )
755 {
756 for( size_t i = 0; i < selectedRows.size(); ++i )
757 {
758 if( selectedRows[i] == row )
759 return;
760 }
761
762 selectedRows.push_back( row );
763 };
764
765 wxGridCellCoordsArray topLeft = GetSelectionBlockTopLeft();
766 wxGridCellCoordsArray botRight = GetSelectionBlockBottomRight();
767
768 for( size_t i = 0; i < std::min( topLeft.Count(), botRight.Count() ); ++i )
769 {
770 for( int row = topLeft[i].GetRow(); row <= botRight[i].GetRow(); ++row )
771 addSelectedRow( row );
772 }
773
774 wxGridCellCoordsArray cells = GetSelectedCells();
775
776 for( size_t i = 0; i < cells.Count(); ++i )
777 addSelectedRow( cells[i].GetRow() );
778
779 if( selectedRows.empty() && GetGridCursorRow() >= 0 )
780 selectedRows.push_back( GetGridCursorRow() );
781
782 if( selectedRows.empty() )
783 return;
784
785 for( int row : selectedRows )
786 {
787 if( !aFilter( row ) )
788 return;
789 }
790
791 if( !CommitPendingChanges() )
792 return;
793
794 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
795 selectedRows.Sort(
796 []( int* first, int* second )
797 {
798 return *second - *first;
799 } );
800
801 int nextSelRow = selectedRows.back() - 1;
802
803 if( nextSelRow >= 0 )
804 {
805 GoToCell( nextSelRow, GetGridCursorCol() );
806 SetGridCursor( nextSelRow, GetGridCursorCol() );
807 }
808
809 for( int row : selectedRows )
810 aDeleter( row );
811}
812
813
814void WX_GRID::SwapRows( int aRowA, int aRowB )
815{
816 for( int col = 0; col < GetNumberCols(); ++col )
817 {
818 wxString temp = GetCellValue( aRowA, col );
819 SetCellValue( aRowA, col, GetCellValue( aRowB, col ) );
820 SetCellValue( aRowB, col, temp );
821 }
822}
823
824
825void WX_GRID::OnMoveRowUp( const std::function<void( int row )>& aMover )
826{
828 []( int row )
829 {
830 return true;
831 },
832 aMover );
833}
834
835
836void WX_GRID::OnMoveRowUp( const std::function<bool( int row )>& aFilter,
837 const std::function<void( int row )>& aMover )
838{
839 if( !CommitPendingChanges() )
840 return;
841
842 int i = GetGridCursorRow();
843
844 if( i > 0 && aFilter( i ) )
845 {
846 aMover( i );
847
848 SetGridCursor( i - 1, GetGridCursorCol() );
849 MakeCellVisible( GetGridCursorRow(), GetGridCursorCol() );
850 }
851 else
852 {
853 wxBell();
854 }
855}
856
857
858void WX_GRID::OnMoveRowDown( const std::function<void( int row )>& aMover )
859{
861 []( int row )
862 {
863 return true;
864 },
865 aMover );
866}
867
868
869void WX_GRID::OnMoveRowDown( const std::function<bool( int row )>& aFilter,
870 const std::function<void( int row )>& aMover )
871{
872 if( !CommitPendingChanges() )
873 return;
874
875 int i = GetGridCursorRow();
876
877 if( i + 1 < GetNumberRows() && aFilter( i ) )
878 {
879 aMover( i );
880
881 SetGridCursor( i + 1, GetGridCursorCol() );
882 MakeCellVisible( GetGridCursorRow(), GetGridCursorCol() );
883 }
884 else
885 {
886 wxBell();
887 }
888}
889
890
891void WX_GRID::SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol )
892{
893 m_unitsProviders[ aCol ] = aProvider;
894
895 if( !m_eval )
896 m_eval = std::make_unique<NUMERIC_EVALUATOR>( aProvider->GetUserUnits() );
897}
898
899
900void WX_GRID::SetAutoEvalColUnits( const int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType )
901{
902 m_autoEvalColsUnits[col] = std::make_pair( aUnit, aUnitType );
903}
904
905
906void WX_GRID::SetAutoEvalColUnits( const int col, EDA_UNITS aUnit )
907{
909 SetAutoEvalColUnits( col, aUnit, type );
910}
911
912
913int WX_GRID::GetUnitValue( int aRow, int aCol )
914{
915 wxString stringValue = GetCellValue( aRow, aCol );
916
917 auto [cellUnits, cellDataType] = getColumnUnits( aCol );
918
919 if( alg::contains( m_autoEvalCols, aCol ) )
920 {
921 m_eval->SetDefaultUnits( cellUnits );
922
923 if( m_eval->Process( stringValue ) )
924 stringValue = m_eval->Result();
925 }
926
927 return getUnitsProvider( aCol )->ValueFromString( stringValue, cellDataType );
928}
929
930
931std::optional<int> WX_GRID::GetOptionalUnitValue( int aRow, int aCol )
932{
933 wxString stringValue = GetCellValue( aRow, aCol );
934
935 auto [cellUnits, cellDataType] = getColumnUnits( aCol );
936
937 if( alg::contains( m_autoEvalCols, aCol ) )
938 {
939 m_eval->SetDefaultUnits( cellUnits );
940
941 if( stringValue != UNITS_PROVIDER::NullUiString && m_eval->Process( stringValue ) )
942 stringValue = m_eval->Result();
943 }
944
945 return getUnitsProvider( aCol )->OptionalValueFromString( stringValue, cellDataType );
946}
947
948
949void WX_GRID::SetUnitValue( int aRow, int aCol, int aValue )
950{
951 EDA_DATA_TYPE cellDataType;
952
953 if( m_autoEvalColsUnits.contains( aCol ) )
954 cellDataType = m_autoEvalColsUnits[aCol].second;
955 else
956 cellDataType = EDA_DATA_TYPE::DISTANCE;
957
958 SetCellValue( aRow, aCol, getUnitsProvider( aCol )->StringFromValue( aValue, true, cellDataType ) );
959}
960
961
962void WX_GRID::SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue )
963{
964 EDA_DATA_TYPE cellDataType;
965
966 if( m_autoEvalColsUnits.contains( aCol ) )
967 cellDataType = m_autoEvalColsUnits[aCol].second;
968 else
969 cellDataType = EDA_DATA_TYPE::DISTANCE;
970
971 SetCellValue( aRow, aCol, getUnitsProvider( aCol )->StringFromOptionalValue( aValue, true, cellDataType ) );
972}
973
974
975void WX_GRID::onGridColMove( wxGridEvent& aEvent )
976{
977 // wxWidgets won't move an open editor, so better just to close it
978 CommitPendingChanges( true );
979}
980
981
982int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep )
983{
984 int size = 0;
985
986 if( aCol < 0 )
987 {
988 if( aKeep )
989 size = GetRowLabelSize();
990
991 for( int row = 0; aContents && row < GetNumberRows(); row++ )
992 size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + wxS( "M" ) ).x ) );
993 }
994 else
995 {
996 if( aKeep )
997 size = GetColSize( aCol );
998
999 // 'M' is generally the widest character, so we buffer the column width by default to
1000 // ensure we don't write a continuous line of text at the column header
1001 if( aHeader )
1002 {
1004
1005 size = std::max( size, int( GetTextExtent( GetColLabelValue( aCol ) + wxS( "M" ) ).x ) );
1006 }
1007
1008 for( int row = 0; aContents && row < GetNumberRows(); row++ )
1009 {
1010 // If we have text, get the size. Otherwise, use a placeholder for the checkbox
1011 if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
1012 size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + wxS( "M" ) ).x );
1013 else
1014 size = std::max( size, GetTextExtent( "MM" ).x );
1015 }
1016 }
1017
1018 return size;
1019}
1020
1021
1023{
1024 int line_height = int( GetTextExtent( "Mj" ).y ) + 3;
1025 int row_height = GetColLabelSize();
1026 int initial_row_height = row_height;
1027
1028 // Headers can be multiline. Fix the Column Label Height to show the full header
1029 // However GetTextExtent does not work on multiline strings,
1030 // and do not return the full text height (only the height of one line)
1031 for( int col = 0; col < GetNumberCols(); col++ )
1032 {
1033 int nl_count = GetColLabelValue( col ).Freq( '\n' );
1034
1035 if( nl_count )
1036 {
1037 // Col Label height must be able to show nl_count+1 lines
1038 if( row_height < line_height * ( nl_count+1 ) )
1039 row_height += line_height * nl_count;
1040 }
1041 }
1042
1043 // Update the column label size, but only if needed, to avoid generating useless
1044 // and perhaps annoying UI events when the size does not change
1045 if( initial_row_height != row_height )
1046 SetColLabelSize( row_height );
1047}
1048
1049
1050std::pair<EDA_UNITS, EDA_DATA_TYPE> WX_GRID::getColumnUnits( const int aCol ) const
1051{
1052 if( m_autoEvalColsUnits.contains( aCol ) )
1053 return { m_autoEvalColsUnits.at( aCol ).first, m_autoEvalColsUnits.at( aCol ).second };
1054
1055 // Legacy - default always DISTANCE
1057}
1058
1059
1060void WX_GRID::SetupColumnAutosizer( int aFlexibleCol )
1061{
1062 const int colCount = GetNumberCols();
1063
1064 for( int ii = 0; ii < GetNumberCols(); ++ii )
1065 m_autosizedCols[ii] = GetColSize( ii );
1066
1067 m_flexibleCol = aFlexibleCol;
1068
1069 wxASSERT_MSG( m_flexibleCol < colCount, "Flexible column index does not exist in grid" );
1070
1071 Bind( wxEVT_UPDATE_UI,
1072 [this]( wxUpdateUIEvent& aEvent )
1073 {
1075 aEvent.Skip();
1076 } );
1077
1078 Bind( wxEVT_SIZE,
1079 [this]( wxSizeEvent& aEvent )
1080 {
1081 onSizeEvent( aEvent );
1082 aEvent.Skip();
1083 } );
1084
1085 // Handles the case when the user changes the cell content to be longer than the current column size
1086 Bind( wxEVT_GRID_CELL_CHANGED,
1087 [this]( wxGridEvent& aEvent )
1088 {
1089 m_gridWidthsDirty = true;
1090 aEvent.Skip();
1091 } );
1092}
1093
1094
1096{
1097 if( m_gridWidthsDirty )
1098 {
1099 const int width = GetSize().GetX() - wxSystemSettings::GetMetric( wxSYS_VSCROLL_X );
1100
1101 std::optional<int> flexibleMinWidth;
1102
1103 for( const auto& [colIndex, minWidth] : m_autosizedCols )
1104 {
1105 if( GetColSize( colIndex ) != 0 )
1106 {
1107 AutoSizeColumn( colIndex );
1108 const int colSize = GetColSize( colIndex );
1109
1110 int minWidthScaled = FromDIP( minWidth );
1111 SetColSize( colIndex, std::max( minWidthScaled, colSize ) );
1112
1113 if( colIndex == m_flexibleCol )
1114 flexibleMinWidth = minWidthScaled;
1115 }
1116 }
1117
1118 // Gather all the widths except the flexi one
1119 int nonFlexibleWidth = 0;
1120
1121 for( int i = 0; i < GetNumberCols(); ++i )
1122 {
1123 if( i != m_flexibleCol )
1124 nonFlexibleWidth += GetColSize( i );
1125 }
1126
1127 if( GetColSize( m_flexibleCol ) != 0 )
1128 SetColSize( m_flexibleCol, std::max( flexibleMinWidth.value_or( 0 ), width - nonFlexibleWidth ) );
1129
1130 // Store the state for next time
1131 m_gridWidth = GetSize().GetX();
1132 m_gridWidthsDirty = false;
1133 }
1134}
1135
1136
1137void WX_GRID::onSizeEvent( wxSizeEvent& aEvent )
1138{
1139 const int width = aEvent.GetSize().GetX();
1140
1141 if( width != m_gridWidth )
1142 m_gridWidthsDirty = true;
1143}
const char * name
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition dialog_shim.h:65
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
wxColour ToColour() const
Definition color4d.cpp:221
COLOR4D Mix(const COLOR4D &aColor, double aFactor) const
Return a color that is mixed with the input by a factor.
Definition color4d.h:292
Icon provider for the "standard" row indicators, for example in layer selection lists.
static const wxString NullUiString
The string that is used in the UI to represent a null value.
std::optional< int > OptionalValueFromString(const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aTextValue in aUnits to internal units used by the frame.
wxString StringFromOptionalValue(std::optional< int > aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts an optional aValue in internal units into a united string.
EDA_UNITS GetUserUnits() const
static EDA_DATA_TYPE GetTypeFromUnits(const EDA_UNITS aUnits)
Gets the inferred type from the given units.
wxString StringFromValue(double aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aValue in internal units into a united string.
int ValueFromString(const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aTextValue in aUnits to internal units used by the frame.
Attribute provider that provides attributes (or modifies the existing attribute) to alternate a row c...
Definition wx_grid.cpp:155
WX_GRID_ALT_ROW_COLOR_PROVIDER(const wxColor &aBaseColor)
Definition wx_grid.cpp:157
wxGridCellAttr * GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) const override
Definition wx_grid.cpp:172
wxGridCellAttrPtr m_attrEven
Definition wx_grid.cpp:198
void UpdateColors(const wxColor &aBaseColor)
Definition wx_grid.cpp:164
void DrawBorder(const wxGrid &grid, wxDC &dc, wxRect &rect) const override
Definition wx_grid.cpp:119
void DrawBorder(const wxGrid &grid, wxDC &dc, wxRect &rect) const override
Definition wx_grid.cpp:102
void DrawBorder(const wxGrid &grid, wxDC &dc, wxRect &rect) const override
Definition wx_grid.cpp:136
wxGridCellAttr * enhanceAttr(wxGridCellAttr *aInputAttr, int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind)
Definition wx_grid.cpp:43
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:982
int m_flexibleCol
Definition wx_grid.h:373
void onGridCellSelect(wxGridEvent &aEvent)
Definition wx_grid.cpp:352
std::map< int, int > m_autosizedCols
Definition wx_grid.h:372
bool m_gridWidthsDirty
Definition wx_grid.h:375
~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:293
bool m_weOwnTable
Definition wx_grid.h:362
void onDPIChanged(wxDPIChangedEvent &event)
Definition wx_grid.cpp:237
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
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 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:900
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
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:1050
void SetUnitValue(int aRow, int aCol, int aValue)
Set a unitized cell's value.
Definition wx_grid.cpp:949
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
int GetUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition wx_grid.cpp:913
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 DrawCornerLabel(wxDC &dc) override
A re-implementation of wxGrid::DrawCornerLabel which draws flat borders.
Definition wx_grid.cpp:549
ROW_ICON_PROVIDER * m_rowIconProvider
Definition wx_grid.h:382
void onCellEditorHidden(wxGridEvent &aEvent)
Definition wx_grid.cpp:405
void SetupColumnAutosizer(int aFlexibleCol)
Set autosize behaviour using wxFormBuilder column widths as minimums, with a single specified growabl...
Definition wx_grid.cpp:1060
void RecomputeGridWidths()
Definition wx_grid.cpp:1095
void OnMoveRowDown(const std::function< void(int row)> &aMover)
Definition wx_grid.cpp:858
void onGridColMove(wxGridEvent &aEvent)
Definition wx_grid.cpp:975
void SetOptionalUnitValue(int aRow, int aCol, std::optional< int > aValue)
Set a unitized cell's optional value.
Definition wx_grid.cpp:962
void onSizeEvent(wxSizeEvent &aEvent)
Definition wx_grid.cpp:1137
void onCellEditorShown(wxGridEvent &aEvent)
Definition wx_grid.cpp:390
void EnsureColLabelsVisible()
Ensure the height of the row displaying the column labels is enough, even if labels are multiline tex...
Definition wx_grid.cpp:1022
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
void DrawRowLabel(wxDC &dc, int row) override
A re-implementation of wxGrid::DrawRowLabel which draws flat borders.
Definition wx_grid.cpp:607
std::bitset< 64 > GetShownColumns()
Definition wx_grid.cpp:520
static void CellEditorSetMargins(wxTextEntryBase *aEntry)
A helper function to set OS-specific margins for text-based cell editors.
Definition wx_grid.cpp:75
std::optional< int > GetOptionalUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition wx_grid.cpp:931
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
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
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:569
static void CellEditorTransformSizeRect(wxRect &aRect)
A helper function to tweak sizes of text-based cell editors depending on OS.
Definition wx_grid.cpp:82
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:34
EDA_UNITS
Definition eda_units.h:44
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition wxgtk/ui.cpp:50
KICOMMON_API wxFont GetControlFont(wxWindow *aWindow)
const int c_IndicatorSizeDIP
Definition ui_common.h:52
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:96
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
std::vector< std::vector< std::string > > table
Functions to provide common constants and other functions to assist in making a consistent UI.
wxColour getBorderColour()
Definition wx_grid.cpp:90
#define MIN_GRIDCELL_MARGIN
Definition wx_grid.cpp:72