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