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 ) : wxGridCellAttrProvider(),
161 m_attrEven( new wxGridCellAttr() )
162 {
163 UpdateColors( aBaseColor );
164 }
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
176 wxGridCellAttr* GetAttr( int row, int col,
177 wxGridCellAttr::wxAttrKind kind ) const override
178 {
179 wxGridCellAttrPtr cellAttr( wxGridCellAttrProvider::GetAttr( row, col, kind ) );
180
181 // Just pass through the cell attribute on odd rows (start normal to allow for the
182 // header row)
183 if( !( row % 2 ) )
184 return cellAttr.release();
185
186 if( !cellAttr )
187 {
188 cellAttr = m_attrEven;
189 }
190 else
191 {
192 if( !cellAttr->HasBackgroundColour() )
193 {
194 cellAttr = cellAttr->Clone();
195 cellAttr->SetBackgroundColour( m_attrEven->GetBackgroundColour() );
196 }
197 }
198
199 return cellAttr.release();
200 }
201
202private:
203 wxGridCellAttrPtr m_attrEven;
204};
205
206
207WX_GRID::WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
208 long style, const wxString& name ) :
209 wxGrid( parent, id, pos, size, style, name ),
210 m_weOwnTable( false )
211{
212 SetDefaultCellOverflow( false );
213
214 // Make sure the GUI font scales properly
215 SetDefaultCellFont( KIUI::GetControlFont( this ) );
217
218 Connect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
219 Connect( wxEVT_GRID_EDITOR_SHOWN, wxGridEventHandler( WX_GRID::onCellEditorShown ), nullptr,
220 this );
221 Connect( wxEVT_GRID_EDITOR_HIDDEN, wxGridEventHandler( WX_GRID::onCellEditorHidden ), nullptr,
222 this );
223}
224
225
227{
228 if( m_weOwnTable )
229 DestroyTable( GetTable() );
230
231 Disconnect( wxEVT_GRID_EDITOR_SHOWN, wxGridEventHandler( WX_GRID::onCellEditorShown ), nullptr,
232 this );
233 Disconnect( wxEVT_GRID_EDITOR_HIDDEN, wxGridEventHandler( WX_GRID::onCellEditorHidden ),
234 nullptr, this );
235 Disconnect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr,
236 this );
237}
238
239
240void WX_GRID::onDPIChanged(wxDPIChangedEvent& aEvt)
241{
242 CallAfter(
243 [&]()
244 {
245 wxGrid::SetColLabelSize( wxGRID_AUTOSIZE );
246 } );
247
250#ifndef __WXMAC__
251 aEvt.Skip();
252#endif
253}
254
255
256void WX_GRID::SetColLabelSize( int aHeight )
257{
258 if( aHeight == 0 || aHeight == wxGRID_AUTOSIZE )
259 {
260 wxGrid::SetColLabelSize( aHeight );
261 return;
262 }
263
264 // Correct wxFormBuilder height for large fonts
265 int minHeight = GetCharHeight() + 2 * MIN_GRIDCELL_MARGIN;
266
267 wxGrid::SetColLabelSize( std::max( aHeight, minHeight ) );
268}
269
270
271void WX_GRID::SetLabelFont( const wxFont& aFont )
272{
273 wxGrid::SetLabelFont( KIUI::GetControlFont( this ) );
274}
275
276
277void WX_GRID::SetTable( wxGridTableBase* aTable, bool aTakeOwnership )
278{
279 // wxGrid::SetTable() messes up the column widths from wxFormBuilder so we have to save
280 // and restore them.
281 int numberCols = GetNumberCols();
282 int* formBuilderColWidths = new int[numberCols];
283
284 for( int i = 0; i < numberCols; ++i )
285 formBuilderColWidths[ i ] = GetColSize( i );
286
287 wxGrid::SetTable( aTable );
288
289 // wxGrid::SetTable() may change the number of columns, so prevent out-of-bounds access
290 // to formBuilderColWidths
291 numberCols = std::min( numberCols, GetNumberCols() );
292
293 for( int i = 0; i < numberCols; ++i )
294 {
295 // correct wxFormBuilder width for large fonts and/or long translations
296 int headingWidth = GetTextExtent( GetColLabelValue( i ) ).x + 2 * MIN_GRIDCELL_MARGIN;
297
298 SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
299 }
300
301 delete[] formBuilderColWidths;
302
303 EnableAlternateRowColors( Pgm().GetCommonSettings()->m_Appearance.grid_striping );
304
305 Connect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
306 Connect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr,
307 this );
308
309 m_weOwnTable = aTakeOwnership;
310}
311
312
314{
315 wxGridTableBase* table = wxGrid::GetTable();
316
317 wxCHECK_MSG( table, /* void */,
318 "Tried to enable alternate row colors without a table assigned to the grid" );
319
320 if( aEnable )
321 {
322 wxColor color = wxGrid::GetDefaultCellBackgroundColour();
323 table->SetAttrProvider( new WX_GRID_ALT_ROW_COLOR_PROVIDER( color ) );
324 }
325 else
326 {
327 table->SetAttrProvider( nullptr );
328 }
329}
330
331
332void WX_GRID::onGridCellSelect( wxGridEvent& aEvent )
333{
334 // Highlight the selected cell.
335 // Calling SelectBlock() allows a visual effect when cells are selected by tab or arrow keys.
336 // Otherwise, one cannot really know what actual cell is selected.
337 int row = aEvent.GetRow();
338 int col = aEvent.GetCol();
339
340 if( row >= 0 && row < GetNumberRows() && col >= 0 && col < GetNumberCols() )
341 {
342 if( GetSelectionMode() == wxGrid::wxGridSelectCells )
343 {
344 SelectBlock( row, col, row, col, false );
345 }
346 else if( GetSelectionMode() == wxGrid::wxGridSelectRows
347 || GetSelectionMode() == wxGrid::wxGridSelectRowsOrColumns )
348 {
349 SelectBlock( row, 0, row, GetNumberCols() - 1, false );
350 }
351 else if( GetSelectionMode() == wxGrid::wxGridSelectColumns )
352 {
353 SelectBlock( 0, col, GetNumberRows() - 1, col, false );
354 }
355 }
356}
357
358
359void WX_GRID::onCellEditorShown( wxGridEvent& aEvent )
360{
361 if( alg::contains( m_autoEvalCols, aEvent.GetCol() ) )
362 {
363 int row = aEvent.GetRow();
364 int col = aEvent.GetCol();
365
366 const std::pair<wxString, wxString>& beforeAfter = m_evalBeforeAfter[ { row, col } ];
367
368 if( GetCellValue( row, col ) == beforeAfter.second )
369 SetCellValue( row, col, beforeAfter.first );
370 }
371}
372
373
374void WX_GRID::onCellEditorHidden( wxGridEvent& aEvent )
375{
376 if( alg::contains( m_autoEvalCols, aEvent.GetCol() ) )
377 {
378 UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aEvent.GetCol() ];
379
380 if( !unitsProvider )
381 unitsProvider = m_unitsProviders.begin()->second;
382
383 m_eval->SetDefaultUnits( unitsProvider->GetUserUnits() );
384
385 int row = aEvent.GetRow();
386 int col = aEvent.GetCol();
387
388 // Determine if this cell is marked as holding nullable values
389 bool isNullable = false;
390 wxGridCellEditor* cellEditor = GetCellEditor( row, col );
391
392 if( cellEditor )
393 {
394 GRID_CELL_MARK_AS_NULLABLE* nullableCell =
395 dynamic_cast<GRID_CELL_MARK_AS_NULLABLE*>( cellEditor );
396
397 if( nullableCell )
398 isNullable = nullableCell->IsNullable();
399
400 cellEditor->DecRef();
401 }
402
403 CallAfter(
404 [this, row, col, isNullable, unitsProvider]()
405 {
406 wxString stringValue = GetCellValue( row, col );
407 bool processedOk = true;
408
409 if( stringValue != UNITS_PROVIDER::NullUiString )
410 processedOk = m_eval->Process( stringValue );
411
412 if( processedOk )
413 {
414 wxString evalValue;
415
416 if( isNullable )
417 {
418 std::optional<int> val;
419
420 if( stringValue == UNITS_PROVIDER::NullUiString )
421 {
422 val = unitsProvider->OptionalValueFromString(
424 }
425 else
426 {
427 val = unitsProvider->OptionalValueFromString( m_eval->Result() );
428 }
429
430 evalValue = unitsProvider->StringFromOptionalValue( val, true );
431 }
432 else
433 {
434 int val = unitsProvider->ValueFromString( m_eval->Result() );
435 evalValue = unitsProvider->StringFromValue( val, true );
436 }
437
438 if( stringValue != evalValue )
439 {
440 SetCellValue( row, col, evalValue );
441 m_evalBeforeAfter[{ row, col }] = { stringValue, evalValue };
442 }
443 }
444 } );
445 }
446
447 aEvent.Skip();
448}
449
450
451void WX_GRID::DestroyTable( wxGridTableBase* aTable )
452{
453 // wxGrid's destructor will crash trying to look up the cell attr if the edit control
454 // is left open. Normally it's closed in Validate(), but not if the user hit Cancel.
455 CommitPendingChanges( true /* quiet mode */ );
456
457 Disconnect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
458 Disconnect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr,
459 this );
460
461 wxGrid::SetTable( nullptr );
462 delete aTable;
463}
464
465
467{
468 wxString shownColumns;
469
470 for( int i = 0; i < GetNumberCols(); ++i )
471 {
472 if( IsColShown( i ) )
473 {
474 if( shownColumns.Length() )
475 shownColumns << wxT( " " );
476
477 shownColumns << i;
478 }
479 }
480
481 return shownColumns;
482}
483
484
486{
487 std::bitset<64> shownColumns;
488
489 for( int ii = 0; ii < GetNumberCols(); ++ii )
490 shownColumns[ii] = IsColShown( ii );
491
492 return shownColumns;
493}
494
495
496void WX_GRID::ShowHideColumns( const wxString& shownColumns )
497{
498 for( int i = 0; i < GetNumberCols(); ++i )
499 HideCol( i );
500
501 wxStringTokenizer shownTokens( shownColumns );
502
503 while( shownTokens.HasMoreTokens() )
504 {
505 long colNumber;
506 shownTokens.GetNextToken().ToLong( &colNumber );
507
508 if( colNumber >= 0 && colNumber < GetNumberCols() )
509 ShowCol( (int) colNumber );
510 }
511}
512
513
514void WX_GRID::ShowHideColumns( const std::bitset<64>& aShownColumns )
515{
516 for( int ii = 0; ii < GetNumberCols(); ++ ii )
517 {
518 if( aShownColumns[ii] )
519 ShowCol( ii );
520 else
521 HideCol( ii );
522 }
523}
524
525
527{
528 if( m_nativeColumnLabels )
529 wxGrid::DrawCornerLabel( dc );
530
531 wxRect rect( wxSize( m_rowLabelWidth, m_colLabelHeight ) );
532
534
535 // It is reported that we need to erase the background to avoid display
536 // artifacts, see #12055.
537 {
538 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
539 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
540 dc.DrawRectangle( rect.Inflate( 1 ) );
541 }
542
543 rend.DrawBorder( *this, dc, rect );
544}
545
546
547void WX_GRID::DrawColLabel( wxDC& dc, int col )
548{
549 if( m_nativeColumnLabels )
550 wxGrid::DrawColLabel( dc, col );
551
552 if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
553 return;
554
555 wxRect rect( GetColLeft( col ), 0, GetColWidth( col ), m_colLabelHeight );
556
558
559 // It is reported that we need to erase the background to avoid display
560 // artifacts, see #12055.
561 {
562 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
563 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
564 dc.DrawRectangle( rect.Inflate( 1 ) );
565 }
566
567 rend.DrawBorder( *this, dc, rect );
568
569 // Make sure fonts get scaled correctly on GTK HiDPI monitors
570 dc.SetFont( GetLabelFont() );
571
572 int hAlign, vAlign;
573 GetColLabelAlignment( &hAlign, &vAlign );
574 const int orient = GetColLabelTextOrientation();
575
576 if( col == 0 )
577 hAlign = wxALIGN_LEFT;
578
579 if( hAlign == wxALIGN_LEFT )
580 rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
581
582 rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
583}
584
585
586void WX_GRID::DrawRowLabel( wxDC& dc, int row )
587{
588 if( GetRowHeight( row ) <= 0 || m_rowLabelWidth <= 0 )
589 return;
590
591 wxRect rect( 0, GetRowTop( row ), m_rowLabelWidth, GetRowHeight( row ) );
592
593 static WX_GRID_ROW_HEADER_RENDERER rend;
594
595 // It is reported that we need to erase the background to avoid display
596 // artifacts, see #12055.
597 {
598 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
599 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
600 dc.DrawRectangle( rect.Inflate( 1 ) );
601 }
602
603 rend.DrawBorder( *this, dc, rect );
604
605 // Make sure fonts get scaled correctly on GTK HiDPI monitors
606 dc.SetFont( GetLabelFont() );
607
608 int hAlign, vAlign;
609 GetRowLabelAlignment(&hAlign, &vAlign);
610
611 if( hAlign == wxALIGN_LEFT )
612 rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
613
614 rend.DrawLabel( *this, dc, GetRowLabelValue( row ), rect, hAlign, vAlign, wxHORIZONTAL );
615}
616
617
619{
620 if( !IsCellEditControlEnabled() )
621 return true;
622
623 HideCellEditControl();
624
625 // do it after HideCellEditControl()
626 m_cellEditCtrlEnabled = false;
627
628 int row = m_currentCellCoords.GetRow();
629 int col = m_currentCellCoords.GetCol();
630
631 wxString oldval = GetCellValue( row, col );
632 wxString newval;
633
634 wxGridCellAttr* attr = GetCellAttr( row, col );
635 wxGridCellEditor* editor = attr->GetEditor( this, row, col );
636
637 editor->EndEdit( row, col, this, oldval, &newval );
638
639 editor->DecRef();
640 attr->DecRef();
641
642 return true;
643}
644
645
646bool WX_GRID::CommitPendingChanges( bool aQuietMode )
647{
648 if( !IsCellEditControlEnabled() )
649 return true;
650
651 if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
652 return false;
653
654 HideCellEditControl();
655
656 // do it after HideCellEditControl()
657 m_cellEditCtrlEnabled = false;
658
659 int row = m_currentCellCoords.GetRow();
660 int col = m_currentCellCoords.GetCol();
661
662 wxString oldval = GetCellValue( row, col );
663 wxString newval;
664
665 wxGridCellAttr* attr = GetCellAttr( row, col );
666 wxGridCellEditor* editor = attr->GetEditor( this, row, col );
667
668 bool changed = editor->EndEdit( row, col, this, oldval, &newval );
669
670 editor->DecRef();
671 attr->DecRef();
672
673 if( changed )
674 {
675 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
676 return false;
677
678 editor->ApplyEdit( row, col, this );
679
680 // for compatibility reasons dating back to wx 2.8 when this event
681 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
682 // didn't exist we allow vetoing this one too
683 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
684 {
685 // Event has been vetoed, set the data back.
686 SetCellValue( row, col, oldval );
687 return false;
688 }
689
690 if( DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
691 dlg->OnModify();
692 }
693
694 return true;
695}
696
697
698void WX_GRID::SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol )
699{
700 m_unitsProviders[ aCol ] = aProvider;
701
702 if( !m_eval )
703 m_eval = std::make_unique<NUMERIC_EVALUATOR>( aProvider->GetUserUnits() );
704}
705
706
707int WX_GRID::GetUnitValue( int aRow, int aCol )
708{
709 UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aCol ];
710
711 if( !unitsProvider )
712 unitsProvider = m_unitsProviders.begin()->second;
713
714 wxString stringValue = GetCellValue( aRow, aCol );
715
716 if( alg::contains( m_autoEvalCols, aCol ) )
717 {
718 m_eval->SetDefaultUnits( unitsProvider->GetUserUnits() );
719
720 if( m_eval->Process( stringValue ) )
721 stringValue = m_eval->Result();
722 }
723
724 return unitsProvider->ValueFromString( stringValue );
725}
726
727
728std::optional<int> WX_GRID::GetOptionalUnitValue( int aRow, int aCol )
729{
730 UNITS_PROVIDER* unitsProvider = m_unitsProviders[aCol];
731
732 if( !unitsProvider )
733 unitsProvider = m_unitsProviders.begin()->second;
734
735 wxString stringValue = GetCellValue( aRow, aCol );
736
737 if( alg::contains( m_autoEvalCols, aCol ) )
738 {
739 m_eval->SetDefaultUnits( unitsProvider->GetUserUnits() );
740
741 if( stringValue != UNITS_PROVIDER::NullUiString && m_eval->Process( stringValue ) )
742 stringValue = m_eval->Result();
743 }
744
745 return unitsProvider->OptionalValueFromString( stringValue );
746}
747
748
749void WX_GRID::SetUnitValue( int aRow, int aCol, int aValue )
750{
751 UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aCol ];
752
753 if( !unitsProvider )
754 unitsProvider = m_unitsProviders.begin()->second;
755
756 SetCellValue( aRow, aCol, unitsProvider->StringFromValue( aValue, true ) );
757}
758
759
760void WX_GRID::SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue )
761{
762 UNITS_PROVIDER* unitsProvider = m_unitsProviders[aCol];
763
764 if( !unitsProvider )
765 unitsProvider = m_unitsProviders.begin()->second;
766
767 SetCellValue( aRow, aCol, unitsProvider->StringFromOptionalValue( aValue, true ) );
768}
769
770
771void WX_GRID::onGridColMove( wxGridEvent& aEvent )
772{
773 // wxWidgets won't move an open editor, so better just to close it
774 CommitPendingChanges( true );
775}
776
777
778int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep )
779{
780 int size = 0;
781
782 if( aCol < 0 )
783 {
784 if( aKeep )
785 size = GetRowLabelSize();
786
787 for( int row = 0; aContents && row < GetNumberRows(); row++ )
788 size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + wxS( "M" ) ).x ) );
789 }
790 else
791 {
792 if( aKeep )
793 size = GetColSize( aCol );
794
795 // 'M' is generally the widest character, so we buffer the column width by default to
796 // ensure we don't write a continuous line of text at the column header
797 if( aHeader )
798 {
800
801 size = std::max( size,
802 int( GetTextExtent( GetColLabelValue( aCol ) + wxS( "M" ) ).x ) );
803 }
804
805 for( int row = 0; aContents && row < GetNumberRows(); row++ )
806 {
807 // If we have text, get the size. Otherwise, use a placeholder for the checkbox
808 if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
809 size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + wxS( "M" ) ).x );
810 else
811 size = std::max( size, GetTextExtent( "MM" ).x );
812 }
813 }
814
815 return size;
816}
817
818
820{
821 int line_height = int( GetTextExtent( "Mj" ).y ) + 3;
822 int row_height = GetColLabelSize();
823 int initial_row_height = row_height;
824
825 // Headers can be multiline. Fix the Column Label Height to show the full header
826 // However GetTextExtent does not work on multiline strings,
827 // and do not return the full text height (only the height of one line)
828 for( int col = 0; col < GetNumberCols(); col++ )
829 {
830 int nl_count = GetColLabelValue( col ).Freq( '\n' );
831
832 if( nl_count )
833 {
834 // Col Label height must be able to show nl_count+1 lines
835 if( row_height < line_height * ( nl_count+1 ) )
836 row_height += line_height * nl_count;
837 }
838 }
839
840 // Update the column label size, but only if needed, to avoid generating useless
841 // and perhaps annoying UI events when the size does not change
842 if( initial_row_height != row_height )
843 SetColLabelSize( row_height );
844}
int color
Definition: DXF_plotter.cpp:60
const char * name
Definition: DXF_plotter.cpp:59
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:88
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
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:176
wxGridCellAttrPtr m_attrEven
Definition: wx_grid.cpp:203
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:778
void onGridCellSelect(wxGridEvent &aEvent)
Definition: wx_grid.cpp:332
~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:271
bool m_weOwnTable
Definition: wx_grid.h:254
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:496
std::map< int, UNITS_PROVIDER * > m_unitsProviders
Definition: wx_grid.h:256
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:277
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:451
bool CancelPendingChanges()
Definition: wx_grid.cpp:618
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:256
void SetUnitValue(int aRow, int aCol, int aValue)
Set a unitized cell's value.
Definition: wx_grid.cpp:749
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:207
int GetUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition: wx_grid.cpp:707
std::vector< int > m_autoEvalCols
Definition: wx_grid.h:258
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
Definition: wx_grid.h:260
void DrawCornerLabel(wxDC &dc) override
A re-implementation of wxGrid::DrawCornerLabel which draws flat borders.
Definition: wx_grid.cpp:526
void onCellEditorHidden(wxGridEvent &aEvent)
Definition: wx_grid.cpp:374
void onGridColMove(wxGridEvent &aEvent)
Definition: wx_grid.cpp:771
void SetOptionalUnitValue(int aRow, int aCol, std::optional< int > aValue)
Set a unitized cell's optional value.
Definition: wx_grid.cpp:760
void onCellEditorShown(wxGridEvent &aEvent)
Definition: wx_grid.cpp:359
void EnsureColLabelsVisible()
Ensure the height of the row displaying the column labels is enough, even if labels are multiline tex...
Definition: wx_grid.cpp:819
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:466
void DrawRowLabel(wxDC &dc, int row) override
A re-implementation of wxGrid::DrawRowLabel which draws flat borders.
Definition: wx_grid.cpp:586
std::bitset< 64 > GetShownColumns()
Definition: wx_grid.cpp:485
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:728
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:313
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:698
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:646
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
Definition: wx_grid.h:257
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:547
static void CellEditorTransformSizeRect(wxRect &aRect)
A helper function to tweak sizes of text-based cell editors depending on OS.
Definition: wx_grid.cpp:85
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:161
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:1073
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