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
32#include <widgets/wx_grid.h>
33#include <widgets/ui_common.h>
34#include <algorithm>
35#include <core/kicad_algo.h>
36#include <gal/color4d.h>
37#include <kiplatform/ui.h>
38#include <utility>
39
40#include <pgm_base.h>
42#include <dialog_shim.h>
43
44
45wxGridCellAttr* WX_GRID_TABLE_BASE::enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
46 wxGridCellAttr::wxAttrKind aKind )
47{
48 wxGridCellAttr* attr = aInputAttr;
49
50 if( wxGridCellAttrProvider* provider = GetAttrProvider() )
51 {
52 wxGridCellAttr* providerAttr = provider->GetAttr( aRow, aCol, aKind );
53
54 if( providerAttr )
55 {
56 attr = new wxGridCellAttr;
57 attr->SetKind( wxGridCellAttr::Merged );
58
59 if( aInputAttr )
60 {
61 attr->MergeWith( aInputAttr );
62 aInputAttr->DecRef();
63 }
64
65 attr->MergeWith( providerAttr );
66 providerAttr->DecRef();
67 }
68 }
69
70 return attr;
71}
72
73
74#define MIN_GRIDCELL_MARGIN FromDIP( 2 )
75
76
77void WX_GRID::CellEditorSetMargins( wxTextEntryBase* aEntry )
78{
79 // This is consistent with wxGridCellTextEditor. But works differently across platforms or
80 // 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,
206 long style, 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
211 // consistent.
212 SetDefaultRowSize( GetDefaultRowSize() + FromDIP( 4 ) );
213
214 SetDefaultCellOverflow( false );
215
216 // Make sure the GUI font scales properly
217 SetDefaultCellFont( KIUI::GetControlFont( this ) );
219
220 Connect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
221 Connect( wxEVT_GRID_EDITOR_SHOWN, wxGridEventHandler( WX_GRID::onCellEditorShown ), nullptr, this );
222 Connect( wxEVT_GRID_EDITOR_HIDDEN, wxGridEventHandler( WX_GRID::onCellEditorHidden ), nullptr, this );
223}
224
225
227{
228 if( m_weOwnTable )
229 DestroyTable( GetTable() );
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 CallAfter( [&]()
240 {
241 wxGrid::SetColLabelSize( wxGRID_AUTOSIZE );
242 } );
243
246#ifndef __WXMAC__
247 aEvt.Skip();
248#endif
249}
250
251
252void WX_GRID::SetColLabelSize( int aHeight )
253{
254 if( aHeight == 0 || aHeight == wxGRID_AUTOSIZE )
255 {
256 wxGrid::SetColLabelSize( aHeight );
257 return;
258 }
259
260 // Correct wxFormBuilder height for large fonts
261 int minHeight = GetCharHeight() + 2 * MIN_GRIDCELL_MARGIN;
262
263 wxGrid::SetColLabelSize( std::max( aHeight, minHeight ) );
264}
265
266
267void WX_GRID::SetLabelFont( const wxFont& aFont )
268{
269 wxGrid::SetLabelFont( KIUI::GetControlFont( this ) );
270}
271
272
273void WX_GRID::SetTable( wxGridTableBase* aTable, bool aTakeOwnership )
274{
275 // wxGrid::SetTable() messes up the column widths from wxFormBuilder so we have to save
276 // and restore them.
277 int numberCols = GetNumberCols();
278 int* formBuilderColWidths = new int[numberCols];
279
280 for( int i = 0; i < numberCols; ++i )
281 formBuilderColWidths[ i ] = GetColSize( i );
282
283 wxGrid::SetTable( aTable );
284
285 // wxGrid::SetTable() may change the number of columns, so prevent out-of-bounds access
286 // to formBuilderColWidths
287 numberCols = std::min( numberCols, GetNumberCols() );
288
289 for( int i = 0; i < numberCols; ++i )
290 {
291 // correct wxFormBuilder width for large fonts and/or long translations
292 int headingWidth = GetTextExtent( GetColLabelValue( i ) ).x + 2 * MIN_GRIDCELL_MARGIN;
293
294 SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
295 }
296
297 delete[] formBuilderColWidths;
298
299 EnableAlternateRowColors( Pgm().GetCommonSettings()->m_Appearance.grid_striping );
300
301 Connect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
302 Connect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
303
304 m_weOwnTable = aTakeOwnership;
305}
306
307
309{
310 wxGridTableBase* table = wxGrid::GetTable();
311
312 wxCHECK_MSG( table, /* void */, "Tried to enable alternate row colors without a table assigned to the grid" );
313
314 if( aEnable )
315 {
316 wxColor color = wxGrid::GetDefaultCellBackgroundColour();
317 table->SetAttrProvider( new WX_GRID_ALT_ROW_COLOR_PROVIDER( color ) );
318 }
319 else
320 {
321 table->SetAttrProvider( nullptr );
322 }
323}
324
325
326void WX_GRID::onGridCellSelect( wxGridEvent& aEvent )
327{
328 // Highlight the selected cell.
329 // Calling SelectBlock() allows a visual effect when cells are selected by tab or arrow keys.
330 // Otherwise, one cannot really know what actual cell is selected.
331 int row = aEvent.GetRow();
332 int col = aEvent.GetCol();
333
334 if( row >= 0 && row < GetNumberRows() && col >= 0 && col < GetNumberCols() )
335 {
336 if( GetSelectionMode() == wxGrid::wxGridSelectCells )
337 {
338 SelectBlock( row, col, row, col, false );
339 }
340 else if( GetSelectionMode() == wxGrid::wxGridSelectRows
341 || GetSelectionMode() == wxGrid::wxGridSelectRowsOrColumns )
342 {
343 SelectBlock( row, 0, row, GetNumberCols() - 1, false );
344 }
345 else if( GetSelectionMode() == wxGrid::wxGridSelectColumns )
346 {
347 SelectBlock( 0, col, GetNumberRows() - 1, col, false );
348 }
349 }
350}
351
352
353void WX_GRID::onCellEditorShown( wxGridEvent& aEvent )
354{
355 if( alg::contains( m_autoEvalCols, aEvent.GetCol() ) )
356 {
357 int row = aEvent.GetRow();
358 int col = aEvent.GetCol();
359
360 const std::pair<wxString, wxString>& beforeAfter = m_evalBeforeAfter[ { row, col } ];
361
362 if( GetCellValue( row, col ) == beforeAfter.second )
363 SetCellValue( row, col, beforeAfter.first );
364 }
365}
366
367
368void WX_GRID::onCellEditorHidden( wxGridEvent& aEvent )
369{
370 const int col = aEvent.GetCol();
371
372 if( alg::contains( m_autoEvalCols, col ) )
373 {
374 UNITS_PROVIDER* unitsProvider = getUnitsProvider( col );
375
376 auto cellUnitsData = getColumnUnits( col );
377 EDA_UNITS cellUnits = cellUnitsData.first;
378 EDA_DATA_TYPE cellDataType = cellUnitsData.second;
379
380 m_eval->SetDefaultUnits( cellUnits );
381
382 const int row = aEvent.GetRow();
383
384 // Determine if this cell is marked as holding nullable values
385 bool isNullable = false;
386 wxGridCellEditor* cellEditor = GetCellEditor( row, col );
387
388 if( cellEditor )
389 {
390 if( GRID_CELL_MARK_AS_NULLABLE* nullable = dynamic_cast<GRID_CELL_MARK_AS_NULLABLE*>( cellEditor ) )
391 isNullable = nullable->IsNullable();
392
393 cellEditor->DecRef();
394 }
395
396 CallAfter(
397 [this, row, col, isNullable, unitsProvider, cellDataType]()
398 {
399 // Careful; if called from CommitPendingChange() in a delete operation, the cell may
400 // no longer exist.
401 if( row >= GetNumberRows() || col >= GetNumberCols() )
402 return;
403
404 wxString stringValue = GetCellValue( row, col );
405 bool processedOk = true;
406
407 if( stringValue != UNITS_PROVIDER::NullUiString )
408 processedOk = m_eval->Process( stringValue );
409
410 if( processedOk )
411 {
412 wxString evalValue;
413
414 if( isNullable )
415 {
416 std::optional<int> val;
417
418 if( stringValue == UNITS_PROVIDER::NullUiString )
419 {
421 cellDataType );
422 }
423 else
424 {
425 val = unitsProvider->OptionalValueFromString( m_eval->Result(),
426 cellDataType );
427 }
428
429 evalValue = unitsProvider->StringFromOptionalValue( val, true, cellDataType );
430 }
431 else
432 {
433 int val = unitsProvider->ValueFromString( m_eval->Result(), cellDataType );
434 evalValue = unitsProvider->StringFromValue( val, true, cellDataType );
435 }
436
437 if( stringValue != evalValue )
438 {
439 SetCellValue( row, col, evalValue );
440 m_evalBeforeAfter[{ row, col }] = { stringValue, evalValue };
441 }
442 }
443 } );
444 }
445
446 aEvent.Skip();
447}
448
449
450void WX_GRID::DestroyTable( wxGridTableBase* aTable )
451{
452 // wxGrid's destructor will crash trying to look up the cell attr if the edit control
453 // is left open. Normally it's closed in Validate(), but not if the user hit Cancel.
454 CommitPendingChanges( true /* quiet mode */ );
455
456 Disconnect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
457 Disconnect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
458
459 wxGrid::SetTable( nullptr );
460 delete aTable;
461}
462
463
465{
466 wxString shownColumns;
467
468 for( int i = 0; i < GetNumberCols(); ++i )
469 {
470 if( IsColShown( i ) )
471 {
472 if( shownColumns.Length() )
473 shownColumns << wxT( " " );
474
475 shownColumns << i;
476 }
477 }
478
479 return shownColumns;
480}
481
482
484{
485 std::bitset<64> shownColumns;
486
487 for( int ii = 0; ii < GetNumberCols(); ++ii )
488 shownColumns[ii] = IsColShown( ii );
489
490 return shownColumns;
491}
492
493
494void WX_GRID::ShowHideColumns( const wxString& shownColumns )
495{
496 for( int i = 0; i < GetNumberCols(); ++i )
497 HideCol( i );
498
499 wxStringTokenizer shownTokens( shownColumns );
500
501 while( shownTokens.HasMoreTokens() )
502 {
503 long colNumber;
504 shownTokens.GetNextToken().ToLong( &colNumber );
505
506 if( colNumber >= 0 && colNumber < GetNumberCols() )
507 ShowCol( (int) colNumber );
508 }
509}
510
511
513{
514 if( m_nativeColumnLabels )
515 wxGrid::DrawCornerLabel( dc );
516
517 wxRect rect( wxSize( m_rowLabelWidth, m_colLabelHeight ) );
518
520
521 // It is reported that we need to erase the background to avoid display
522 // artifacts, see #12055.
523 {
524 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
525 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
526 dc.DrawRectangle( rect.Inflate( 1 ) );
527 }
528
529 rend.DrawBorder( *this, dc, rect );
530}
531
532
533void WX_GRID::DrawColLabel( wxDC& dc, int col )
534{
535 if( m_nativeColumnLabels )
536 wxGrid::DrawColLabel( dc, col );
537
538 if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
539 return;
540
541 wxRect rect( GetColLeft( col ), 0, GetColWidth( col ), m_colLabelHeight );
542
544
545 // It is reported that we need to erase the background to avoid display
546 // artifacts, see #12055.
547 {
548 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
549 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
550 dc.DrawRectangle( rect.Inflate( 1 ) );
551 }
552
553 rend.DrawBorder( *this, dc, rect );
554
555 // Make sure fonts get scaled correctly on GTK HiDPI monitors
556 dc.SetFont( GetLabelFont() );
557
558 int hAlign, vAlign;
559 GetColLabelAlignment( &hAlign, &vAlign );
560 const int orient = GetColLabelTextOrientation();
561
562 if( col == 0 )
563 hAlign = wxALIGN_LEFT;
564
565 if( hAlign == wxALIGN_LEFT )
566 rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
567
568 rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
569}
570
571
572void WX_GRID::DrawRowLabel( wxDC& dc, int row )
573{
574 if( GetRowHeight( row ) <= 0 || m_rowLabelWidth <= 0 )
575 return;
576
577 wxRect rect( 0, GetRowTop( row ), m_rowLabelWidth, GetRowHeight( row ) );
578
579 static WX_GRID_ROW_HEADER_RENDERER rend;
580
581 // It is reported that we need to erase the background to avoid display
582 // artifacts, see #12055.
583 {
584 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
585 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
586 dc.DrawRectangle( rect.Inflate( 1 ) );
587 }
588
589 rend.DrawBorder( *this, dc, rect );
590
591 // Make sure fonts get scaled correctly on GTK HiDPI monitors
592 dc.SetFont( GetLabelFont() );
593
594 int hAlign, vAlign;
595 GetRowLabelAlignment(&hAlign, &vAlign);
596
597 if( hAlign == wxALIGN_LEFT )
598 rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
599
600 rend.DrawLabel( *this, dc, GetRowLabelValue( row ), rect, hAlign, vAlign, wxHORIZONTAL );
601}
602
603
605{
606 if( !IsCellEditControlEnabled() )
607 return true;
608
609 HideCellEditControl();
610
611 // do it after HideCellEditControl()
612 m_cellEditCtrlEnabled = false;
613
614 int row = m_currentCellCoords.GetRow();
615 int col = m_currentCellCoords.GetCol();
616
617 wxString oldval = GetCellValue( row, col );
618 wxString newval;
619
620 wxGridCellAttr* attr = GetCellAttr( row, col );
621 wxGridCellEditor* editor = attr->GetEditor( this, row, col );
622
623 editor->EndEdit( row, col, this, oldval, &newval );
624
625 editor->DecRef();
626 attr->DecRef();
627
628 return true;
629}
630
631
632bool WX_GRID::CommitPendingChanges( bool aQuietMode )
633{
634 if( !IsCellEditControlEnabled() )
635 return true;
636
637 if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
638 return false;
639
640 HideCellEditControl();
641
642 // do it after HideCellEditControl()
643 m_cellEditCtrlEnabled = false;
644
645 int row = m_currentCellCoords.GetRow();
646 int col = m_currentCellCoords.GetCol();
647
648 wxString oldval = GetCellValue( row, col );
649 wxString newval;
650
651 wxGridCellAttr* attr = GetCellAttr( row, col );
652 wxGridCellEditor* editor = attr->GetEditor( this, row, col );
653
654 bool changed = editor->EndEdit( row, col, this, oldval, &newval );
655
656 editor->DecRef();
657 attr->DecRef();
658
659 if( changed )
660 {
661 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
662 return false;
663
664 editor->ApplyEdit( row, col, this );
665
666 // for compatibility reasons dating back to wx 2.8 when this event
667 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
668 // didn't exist we allow vetoing this one too
669 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
670 {
671 // Event has been vetoed, set the data back.
672 SetCellValue( row, col, oldval );
673 return false;
674 }
675
676 if( DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
677 dlg->OnModify();
678 }
679
680 return true;
681}
682
683
684void WX_GRID::OnAddRow( const std::function<std::pair<int, int>()>& aAdder )
685{
686 if( !CommitPendingChanges() )
687 return;
688
689 auto [row, editCol] = aAdder();
690
691 // wx documentation is wrong, SetGridCursor does not make visible.
692 SetFocus();
693 MakeCellVisible( row, std::max( editCol, 0 ) );
694 SetGridCursor( row, std::max( editCol, 0 ) );
695
696 if( editCol >= 0 )
697 {
698 EnableCellEditControl( true );
699 ShowCellEditControl();
700 }
701}
702
703
704void WX_GRID::OnDeleteRows( const std::function<void( int row )>& aDeleter )
705{
707 []( int row )
708 {
709 return true;
710 },
711 aDeleter );
712}
713
714
715void WX_GRID::OnDeleteRows( const std::function<bool( int row )>& aFilter,
716 const std::function<void( int row )>& aDeleter )
717{
718 wxArrayInt selectedRows = GetSelectedRows();
719
720 if( selectedRows.empty() && GetGridCursorRow() >= 0 )
721 selectedRows.push_back( GetGridCursorRow() );
722
723 if( selectedRows.empty() )
724 return;
725
726 for( int row : selectedRows )
727 {
728 if( !aFilter( row ) )
729 return;
730 }
731
732 if( !CommitPendingChanges() )
733 return;
734
735 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
736 selectedRows.Sort(
737 []( int* first, int* second )
738 {
739 return *second - *first;
740 } );
741
742 int nextSelRow = selectedRows.back() - 1;
743
744 if( nextSelRow >= 0 )
745 {
746 GoToCell( nextSelRow, GetGridCursorCol() );
747 SetGridCursor( nextSelRow, GetGridCursorCol() );
748 }
749
750 for( int row : selectedRows )
751 aDeleter( row );
752}
753
754
755void WX_GRID::SwapRows( int aRowA, int aRowB )
756{
757 for( int col = 0; col < GetNumberCols(); ++col )
758 {
759 wxString temp = GetCellValue( aRowA, col );
760 SetCellValue( aRowA, col, GetCellValue( aRowB, col ) );
761 SetCellValue( aRowB, col, temp );
762 }
763}
764
765
766void WX_GRID::OnMoveRowUp( const std::function<void( int row )>& aMover )
767{
769 []( int row )
770 {
771 return true;
772 },
773 aMover );
774}
775
776
777void WX_GRID::OnMoveRowUp( const std::function<bool( int row )>& aFilter,
778 const std::function<void( int row )>& aMover )
779{
780 if( !CommitPendingChanges() )
781 return;
782
783 int i = GetGridCursorRow();
784
785 if( i > 0 && aFilter( i ) )
786 {
787 aMover( i );
788
789 SetGridCursor( i - 1, GetGridCursorCol() );
790 MakeCellVisible( GetGridCursorRow(), GetGridCursorCol() );
791 }
792 else
793 {
794 wxBell();
795 }
796}
797
798
799void WX_GRID::OnMoveRowDown( const std::function<void( int row )>& aMover )
800{
802 []( int row )
803 {
804 return true;
805 },
806 aMover );
807}
808
809
810void WX_GRID::OnMoveRowDown( const std::function<bool( int row )>& aFilter,
811 const std::function<void( int row )>& aMover )
812{
813 if( !CommitPendingChanges() )
814 return;
815
816 int i = GetGridCursorRow();
817
818 if( i + 1 < GetNumberRows() && aFilter( i ) )
819 {
820 aMover( i );
821
822 SetGridCursor( i + 1, GetGridCursorCol() );
823 MakeCellVisible( GetGridCursorRow(), GetGridCursorCol() );
824 }
825 else
826 {
827 wxBell();
828 }
829}
830
831
832void WX_GRID::SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol )
833{
834 m_unitsProviders[ aCol ] = aProvider;
835
836 if( !m_eval )
837 m_eval = std::make_unique<NUMERIC_EVALUATOR>( aProvider->GetUserUnits() );
838}
839
840
841void WX_GRID::SetAutoEvalColUnits( const int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType )
842{
843 m_autoEvalColsUnits[col] = std::make_pair( aUnit, aUnitType );
844}
845
846
847void WX_GRID::SetAutoEvalColUnits( const int col, EDA_UNITS aUnit )
848{
850 SetAutoEvalColUnits( col, aUnit, type );
851}
852
853
854int WX_GRID::GetUnitValue( int aRow, int aCol )
855{
856 wxString stringValue = GetCellValue( aRow, aCol );
857
858 auto [cellUnits, cellDataType] = getColumnUnits( aCol );
859
860 if( alg::contains( m_autoEvalCols, aCol ) )
861 {
862 m_eval->SetDefaultUnits( cellUnits );
863
864 if( m_eval->Process( stringValue ) )
865 stringValue = m_eval->Result();
866 }
867
868 return getUnitsProvider( aCol )->ValueFromString( stringValue, cellDataType );
869}
870
871
872std::optional<int> WX_GRID::GetOptionalUnitValue( int aRow, int aCol )
873{
874 wxString stringValue = GetCellValue( aRow, aCol );
875
876 auto [cellUnits, cellDataType] = getColumnUnits( aCol );
877
878 if( alg::contains( m_autoEvalCols, aCol ) )
879 {
880 m_eval->SetDefaultUnits( cellUnits );
881
882 if( stringValue != UNITS_PROVIDER::NullUiString && m_eval->Process( stringValue ) )
883 stringValue = m_eval->Result();
884 }
885
886 return getUnitsProvider( aCol )->OptionalValueFromString( stringValue, cellDataType );
887}
888
889
890void WX_GRID::SetUnitValue( int aRow, int aCol, int aValue )
891{
892 EDA_DATA_TYPE cellDataType;
893
894 if( m_autoEvalColsUnits.contains( aCol ) )
895 cellDataType = m_autoEvalColsUnits[aCol].second;
896 else
897 cellDataType = EDA_DATA_TYPE::DISTANCE;
898
899 SetCellValue( aRow, aCol, getUnitsProvider( aCol )->StringFromValue( aValue, true, cellDataType ) );
900}
901
902
903void WX_GRID::SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue )
904{
905 SetCellValue( aRow, aCol, getUnitsProvider( aCol )->StringFromOptionalValue( aValue, true ) );
906}
907
908
909void WX_GRID::onGridColMove( wxGridEvent& aEvent )
910{
911 // wxWidgets won't move an open editor, so better just to close it
912 CommitPendingChanges( true );
913}
914
915
916int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep )
917{
918 int size = 0;
919
920 if( aCol < 0 )
921 {
922 if( aKeep )
923 size = GetRowLabelSize();
924
925 for( int row = 0; aContents && row < GetNumberRows(); row++ )
926 size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + wxS( "M" ) ).x ) );
927 }
928 else
929 {
930 if( aKeep )
931 size = GetColSize( aCol );
932
933 // 'M' is generally the widest character, so we buffer the column width by default to
934 // ensure we don't write a continuous line of text at the column header
935 if( aHeader )
936 {
938
939 size = std::max( size, int( GetTextExtent( GetColLabelValue( aCol ) + wxS( "M" ) ).x ) );
940 }
941
942 for( int row = 0; aContents && row < GetNumberRows(); row++ )
943 {
944 // If we have text, get the size. Otherwise, use a placeholder for the checkbox
945 if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
946 size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + wxS( "M" ) ).x );
947 else
948 size = std::max( size, GetTextExtent( "MM" ).x );
949 }
950 }
951
952 return size;
953}
954
955
957{
958 int line_height = int( GetTextExtent( "Mj" ).y ) + 3;
959 int row_height = GetColLabelSize();
960 int initial_row_height = row_height;
961
962 // Headers can be multiline. Fix the Column Label Height to show the full header
963 // However GetTextExtent does not work on multiline strings,
964 // and do not return the full text height (only the height of one line)
965 for( int col = 0; col < GetNumberCols(); col++ )
966 {
967 int nl_count = GetColLabelValue( col ).Freq( '\n' );
968
969 if( nl_count )
970 {
971 // Col Label height must be able to show nl_count+1 lines
972 if( row_height < line_height * ( nl_count+1 ) )
973 row_height += line_height * nl_count;
974 }
975 }
976
977 // Update the column label size, but only if needed, to avoid generating useless
978 // and perhaps annoying UI events when the size does not change
979 if( initial_row_height != row_height )
980 SetColLabelSize( row_height );
981}
982
983
984std::pair<EDA_UNITS, EDA_DATA_TYPE> WX_GRID::getColumnUnits( const int aCol ) const
985{
986 if( m_autoEvalColsUnits.contains( aCol ) )
987 return { m_autoEvalColsUnits.at( aCol ).first, m_autoEvalColsUnits.at( aCol ).second };
988
989 // Legacy - default always DISTANCE
990 return { getUnitsProvider( aCol )->GetUserUnits(), EDA_DATA_TYPE::DISTANCE };
991}
int color
Definition: DXF_plotter.cpp:63
const char * name
Definition: DXF_plotter.cpp:62
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:61
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
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:45
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:916
void onGridCellSelect(wxGridEvent &aEvent)
Definition: wx_grid.cpp:326
~WX_GRID() override
Definition: wx_grid.cpp:226
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:267
bool m_weOwnTable
Definition: wx_grid.h:294
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:494
void OnMoveRowUp(const std::function< void(int row)> &aMover)
Definition: wx_grid.cpp:766
std::map< int, UNITS_PROVIDER * > m_unitsProviders
Definition: wx_grid.h:296
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:273
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:450
std::unordered_map< int, std::pair< EDA_UNITS, EDA_DATA_TYPE > > m_autoEvalColsUnits
Definition: wx_grid.h:299
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:841
bool CancelPendingChanges()
Definition: wx_grid.cpp:604
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:252
void SwapRows(int aRowA, int aRowB)
These aren't that tricky, but might as well share code.
Definition: wx_grid.cpp:755
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:984
void SetUnitValue(int aRow, int aCol, int aValue)
Set a unitized cell's value.
Definition: wx_grid.cpp:890
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:854
std::vector< int > m_autoEvalCols
Definition: wx_grid.h:298
UNITS_PROVIDER * getUnitsProvider(int aCol) const
Definition: wx_grid.h:280
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
Definition: wx_grid.h:301
void DrawCornerLabel(wxDC &dc) override
A re-implementation of wxGrid::DrawCornerLabel which draws flat borders.
Definition: wx_grid.cpp:512
void onCellEditorHidden(wxGridEvent &aEvent)
Definition: wx_grid.cpp:368
void OnMoveRowDown(const std::function< void(int row)> &aMover)
Definition: wx_grid.cpp:799
void onGridColMove(wxGridEvent &aEvent)
Definition: wx_grid.cpp:909
void SetOptionalUnitValue(int aRow, int aCol, std::optional< int > aValue)
Set a unitized cell's optional value.
Definition: wx_grid.cpp:903
void onCellEditorShown(wxGridEvent &aEvent)
Definition: wx_grid.cpp:353
void EnsureColLabelsVisible()
Ensure the height of the row displaying the column labels is enough, even if labels are multiline tex...
Definition: wx_grid.cpp:956
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition: wx_grid.cpp:704
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:464
void OnAddRow(const std::function< std::pair< int, int >()> &aAdder)
Definition: wx_grid.cpp:684
void DrawRowLabel(wxDC &dc, int row) override
A re-implementation of wxGrid::DrawRowLabel which draws flat borders.
Definition: wx_grid.cpp:572
std::bitset< 64 > GetShownColumns()
Definition: wx_grid.cpp:483
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:872
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:308
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:832
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:632
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
Definition: wx_grid.h:297
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:533
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)
Definition: ui_common.cpp:168
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:902
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:74