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