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