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 const int col = aEvent.GetCol();
375
376 if( alg::contains( m_autoEvalCols, col ) )
377 {
378 UNITS_PROVIDER* unitsProvider = getUnitsProvider( col );
379
380 auto cellUnitsData = getColumnUnits( col );
381 EDA_UNITS cellUnits = cellUnitsData.first;
382 EDA_DATA_TYPE cellDataType = cellUnitsData.second;
383
384 m_eval->SetDefaultUnits( cellUnits );
385
386 const int row = aEvent.GetRow();
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, cellDataType]()
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(
423 UNITS_PROVIDER::NullUiString, cellDataType );
424 }
425 else
426 {
427 val = unitsProvider->OptionalValueFromString( m_eval->Result(),
428 cellDataType );
429 }
430
431 evalValue = unitsProvider->StringFromOptionalValue( val, true,
432 cellDataType );
433 }
434 else
435 {
436 int val = unitsProvider->ValueFromString( m_eval->Result(),
437 cellDataType );
438 evalValue = unitsProvider->StringFromValue( val, true, cellDataType );
439 }
440
441 if( stringValue != evalValue )
442 {
443 SetCellValue( row, col, evalValue );
444 m_evalBeforeAfter[{ row, col }] = { stringValue, evalValue };
445 }
446 }
447 } );
448 }
449
450 aEvent.Skip();
451}
452
453
454void WX_GRID::DestroyTable( wxGridTableBase* aTable )
455{
456 // wxGrid's destructor will crash trying to look up the cell attr if the edit control
457 // is left open. Normally it's closed in Validate(), but not if the user hit Cancel.
458 CommitPendingChanges( true /* quiet mode */ );
459
460 Disconnect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
461 Disconnect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr,
462 this );
463
464 wxGrid::SetTable( nullptr );
465 delete aTable;
466}
467
468
470{
471 wxString shownColumns;
472
473 for( int i = 0; i < GetNumberCols(); ++i )
474 {
475 if( IsColShown( i ) )
476 {
477 if( shownColumns.Length() )
478 shownColumns << wxT( " " );
479
480 shownColumns << i;
481 }
482 }
483
484 return shownColumns;
485}
486
487
489{
490 std::bitset<64> shownColumns;
491
492 for( int ii = 0; ii < GetNumberCols(); ++ii )
493 shownColumns[ii] = IsColShown( ii );
494
495 return shownColumns;
496}
497
498
499void WX_GRID::ShowHideColumns( const wxString& shownColumns )
500{
501 for( int i = 0; i < GetNumberCols(); ++i )
502 HideCol( i );
503
504 wxStringTokenizer shownTokens( shownColumns );
505
506 while( shownTokens.HasMoreTokens() )
507 {
508 long colNumber;
509 shownTokens.GetNextToken().ToLong( &colNumber );
510
511 if( colNumber >= 0 && colNumber < GetNumberCols() )
512 ShowCol( (int) colNumber );
513 }
514}
515
516
517void WX_GRID::ShowHideColumns( const std::bitset<64>& aShownColumns )
518{
519 for( int ii = 0; ii < GetNumberCols(); ++ ii )
520 {
521 if( aShownColumns[ii] )
522 ShowCol( ii );
523 else
524 HideCol( ii );
525 }
526}
527
528
530{
531 if( m_nativeColumnLabels )
532 wxGrid::DrawCornerLabel( dc );
533
534 wxRect rect( wxSize( m_rowLabelWidth, m_colLabelHeight ) );
535
537
538 // It is reported that we need to erase the background to avoid display
539 // artifacts, see #12055.
540 {
541 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
542 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
543 dc.DrawRectangle( rect.Inflate( 1 ) );
544 }
545
546 rend.DrawBorder( *this, dc, rect );
547}
548
549
550void WX_GRID::DrawColLabel( wxDC& dc, int col )
551{
552 if( m_nativeColumnLabels )
553 wxGrid::DrawColLabel( dc, col );
554
555 if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
556 return;
557
558 wxRect rect( GetColLeft( col ), 0, GetColWidth( col ), m_colLabelHeight );
559
561
562 // It is reported that we need to erase the background to avoid display
563 // artifacts, see #12055.
564 {
565 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
566 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
567 dc.DrawRectangle( rect.Inflate( 1 ) );
568 }
569
570 rend.DrawBorder( *this, dc, rect );
571
572 // Make sure fonts get scaled correctly on GTK HiDPI monitors
573 dc.SetFont( GetLabelFont() );
574
575 int hAlign, vAlign;
576 GetColLabelAlignment( &hAlign, &vAlign );
577 const int orient = GetColLabelTextOrientation();
578
579 if( col == 0 )
580 hAlign = wxALIGN_LEFT;
581
582 if( hAlign == wxALIGN_LEFT )
583 rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
584
585 rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
586}
587
588
589void WX_GRID::DrawRowLabel( wxDC& dc, int row )
590{
591 if( GetRowHeight( row ) <= 0 || m_rowLabelWidth <= 0 )
592 return;
593
594 wxRect rect( 0, GetRowTop( row ), m_rowLabelWidth, GetRowHeight( row ) );
595
596 static WX_GRID_ROW_HEADER_RENDERER rend;
597
598 // It is reported that we need to erase the background to avoid display
599 // artifacts, see #12055.
600 {
601 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
602 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
603 dc.DrawRectangle( rect.Inflate( 1 ) );
604 }
605
606 rend.DrawBorder( *this, dc, rect );
607
608 // Make sure fonts get scaled correctly on GTK HiDPI monitors
609 dc.SetFont( GetLabelFont() );
610
611 int hAlign, vAlign;
612 GetRowLabelAlignment(&hAlign, &vAlign);
613
614 if( hAlign == wxALIGN_LEFT )
615 rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
616
617 rend.DrawLabel( *this, dc, GetRowLabelValue( row ), rect, hAlign, vAlign, wxHORIZONTAL );
618}
619
620
622{
623 if( !IsCellEditControlEnabled() )
624 return true;
625
626 HideCellEditControl();
627
628 // do it after HideCellEditControl()
629 m_cellEditCtrlEnabled = false;
630
631 int row = m_currentCellCoords.GetRow();
632 int col = m_currentCellCoords.GetCol();
633
634 wxString oldval = GetCellValue( row, col );
635 wxString newval;
636
637 wxGridCellAttr* attr = GetCellAttr( row, col );
638 wxGridCellEditor* editor = attr->GetEditor( this, row, col );
639
640 editor->EndEdit( row, col, this, oldval, &newval );
641
642 editor->DecRef();
643 attr->DecRef();
644
645 return true;
646}
647
648
649bool WX_GRID::CommitPendingChanges( bool aQuietMode )
650{
651 if( !IsCellEditControlEnabled() )
652 return true;
653
654 if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
655 return false;
656
657 HideCellEditControl();
658
659 // do it after HideCellEditControl()
660 m_cellEditCtrlEnabled = false;
661
662 int row = m_currentCellCoords.GetRow();
663 int col = m_currentCellCoords.GetCol();
664
665 wxString oldval = GetCellValue( row, col );
666 wxString newval;
667
668 wxGridCellAttr* attr = GetCellAttr( row, col );
669 wxGridCellEditor* editor = attr->GetEditor( this, row, col );
670
671 bool changed = editor->EndEdit( row, col, this, oldval, &newval );
672
673 editor->DecRef();
674 attr->DecRef();
675
676 if( changed )
677 {
678 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
679 return false;
680
681 editor->ApplyEdit( row, col, this );
682
683 // for compatibility reasons dating back to wx 2.8 when this event
684 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
685 // didn't exist we allow vetoing this one too
686 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
687 {
688 // Event has been vetoed, set the data back.
689 SetCellValue( row, col, oldval );
690 return false;
691 }
692
693 if( DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
694 dlg->OnModify();
695 }
696
697 return true;
698}
699
700
701void WX_GRID::SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol )
702{
703 m_unitsProviders[ aCol ] = aProvider;
704
705 if( !m_eval )
706 m_eval = std::make_unique<NUMERIC_EVALUATOR>( aProvider->GetUserUnits() );
707}
708
709
710void WX_GRID::SetAutoEvalColUnits( const int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType )
711{
712 m_autoEvalColsUnits[col] = std::make_pair( aUnit, aUnitType );
713}
714
715
716void WX_GRID::SetAutoEvalColUnits( const int col, EDA_UNITS aUnit )
717{
719 SetAutoEvalColUnits( col, aUnit, type );
720}
721
722
723int WX_GRID::GetUnitValue( int aRow, int aCol )
724{
725 wxString stringValue = GetCellValue( aRow, aCol );
726
727 auto [cellUnits, cellDataType] = getColumnUnits( aCol );
728
729 if( alg::contains( m_autoEvalCols, aCol ) )
730 {
731 m_eval->SetDefaultUnits( cellUnits );
732
733 if( m_eval->Process( stringValue ) )
734 stringValue = m_eval->Result();
735 }
736
737 return getUnitsProvider( aCol )->ValueFromString( stringValue, cellDataType );
738}
739
740
741std::optional<int> WX_GRID::GetOptionalUnitValue( int aRow, int aCol )
742{
743 wxString stringValue = GetCellValue( aRow, aCol );
744
745 auto [cellUnits, cellDataType] = getColumnUnits( aCol );
746
747 if( alg::contains( m_autoEvalCols, aCol ) )
748 {
749 m_eval->SetDefaultUnits( cellUnits );
750
751 if( stringValue != UNITS_PROVIDER::NullUiString && m_eval->Process( stringValue ) )
752 stringValue = m_eval->Result();
753 }
754
755 return getUnitsProvider( aCol )->OptionalValueFromString( stringValue, cellDataType );
756}
757
758
759void WX_GRID::SetUnitValue( int aRow, int aCol, int aValue )
760{
761 EDA_DATA_TYPE cellDataType;
762
763 if( m_autoEvalColsUnits.contains( aCol ) )
764 cellDataType = m_autoEvalColsUnits[aCol].second;
765 else
766 cellDataType = EDA_DATA_TYPE::DISTANCE;
767
768 SetCellValue( aRow, aCol, getUnitsProvider( aCol )->StringFromValue( aValue, true, cellDataType ) );
769}
770
771
772void WX_GRID::SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue )
773{
774 SetCellValue( aRow, aCol, getUnitsProvider( aCol )->StringFromOptionalValue( aValue, true ) );
775}
776
777
778void WX_GRID::onGridColMove( wxGridEvent& aEvent )
779{
780 // wxWidgets won't move an open editor, so better just to close it
781 CommitPendingChanges( true );
782}
783
784
785int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep )
786{
787 int size = 0;
788
789 if( aCol < 0 )
790 {
791 if( aKeep )
792 size = GetRowLabelSize();
793
794 for( int row = 0; aContents && row < GetNumberRows(); row++ )
795 size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + wxS( "M" ) ).x ) );
796 }
797 else
798 {
799 if( aKeep )
800 size = GetColSize( aCol );
801
802 // 'M' is generally the widest character, so we buffer the column width by default to
803 // ensure we don't write a continuous line of text at the column header
804 if( aHeader )
805 {
807
808 size = std::max( size,
809 int( GetTextExtent( GetColLabelValue( aCol ) + wxS( "M" ) ).x ) );
810 }
811
812 for( int row = 0; aContents && row < GetNumberRows(); row++ )
813 {
814 // If we have text, get the size. Otherwise, use a placeholder for the checkbox
815 if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
816 size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + wxS( "M" ) ).x );
817 else
818 size = std::max( size, GetTextExtent( "MM" ).x );
819 }
820 }
821
822 return size;
823}
824
825
827{
828 int line_height = int( GetTextExtent( "Mj" ).y ) + 3;
829 int row_height = GetColLabelSize();
830 int initial_row_height = row_height;
831
832 // Headers can be multiline. Fix the Column Label Height to show the full header
833 // However GetTextExtent does not work on multiline strings,
834 // and do not return the full text height (only the height of one line)
835 for( int col = 0; col < GetNumberCols(); col++ )
836 {
837 int nl_count = GetColLabelValue( col ).Freq( '\n' );
838
839 if( nl_count )
840 {
841 // Col Label height must be able to show nl_count+1 lines
842 if( row_height < line_height * ( nl_count+1 ) )
843 row_height += line_height * nl_count;
844 }
845 }
846
847 // Update the column label size, but only if needed, to avoid generating useless
848 // and perhaps annoying UI events when the size does not change
849 if( initial_row_height != row_height )
850 SetColLabelSize( row_height );
851}
852
853
854std::pair<EDA_UNITS, EDA_DATA_TYPE> WX_GRID::getColumnUnits( const int aCol ) const
855{
856 if( m_autoEvalColsUnits.contains( aCol ) )
857 return { m_autoEvalColsUnits.at( aCol ).first, m_autoEvalColsUnits.at( aCol ).second };
858
859 // Legacy - default always DISTANCE
860 return { getUnitsProvider( aCol )->GetUserUnits(), EDA_DATA_TYPE::DISTANCE };
861}
int color
Definition: DXF_plotter.cpp:63
const char * name
Definition: DXF_plotter.cpp:62
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:52
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
wxColour ToColour() const
Definition: color4d.cpp:220
COLOR4D Mix(const COLOR4D &aColor, double aFactor) const
Return a color that is mixed with the input by a factor.
Definition: color4d.h:295
static const wxString NullUiString
The string that is used in the UI to represent a null value.
std::optional< int > OptionalValueFromString(const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aTextValue in aUnits to internal units used by the frame.
wxString StringFromOptionalValue(std::optional< int > aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts an optional aValue in internal units into a united string.
EDA_UNITS GetUserUnits() const
static EDA_DATA_TYPE GetTypeFromUnits(const EDA_UNITS aUnits)
Gets the inferred type from the given units.
wxString StringFromValue(double aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aValue in internal units into a united string.
int ValueFromString(const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aTextValue in aUnits to internal units used by the frame.
Attribute provider that provides attributes (or modifies the existing attribute) to alternate a row c...
Definition: wx_grid.cpp:158
WX_GRID_ALT_ROW_COLOR_PROVIDER(const wxColor &aBaseColor)
Definition: wx_grid.cpp:160
wxGridCellAttr * GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) const override
Definition: wx_grid.cpp:175
wxGridCellAttrPtr m_attrEven
Definition: wx_grid.cpp:201
void UpdateColors(const wxColor &aBaseColor)
Definition: wx_grid.cpp:167
void DrawBorder(const wxGrid &grid, wxDC &dc, wxRect &rect) const override
Definition: wx_grid.cpp:122
void DrawBorder(const wxGrid &grid, wxDC &dc, wxRect &rect) const override
Definition: wx_grid.cpp:105
void DrawBorder(const wxGrid &grid, wxDC &dc, wxRect &rect) const override
Definition: wx_grid.cpp:139
wxGridCellAttr * enhanceAttr(wxGridCellAttr *aInputAttr, int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind)
Definition: wx_grid.cpp:45
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=true, bool aKeep=false)
Calculate the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:785
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:277
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:499
std::map< int, UNITS_PROVIDER * > m_unitsProviders
Definition: wx_grid.h:279
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:454
std::unordered_map< int, std::pair< EDA_UNITS, EDA_DATA_TYPE > > m_autoEvalColsUnits
Definition: wx_grid.h:282
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:710
bool CancelPendingChanges()
Definition: wx_grid.cpp:621
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
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:854
void SetUnitValue(int aRow, int aCol, int aValue)
Set a unitized cell's value.
Definition: wx_grid.cpp:759
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:723
std::vector< int > m_autoEvalCols
Definition: wx_grid.h:281
UNITS_PROVIDER * getUnitsProvider(int aCol) const
Definition: wx_grid.h:263
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
Definition: wx_grid.h:284
void DrawCornerLabel(wxDC &dc) override
A re-implementation of wxGrid::DrawCornerLabel which draws flat borders.
Definition: wx_grid.cpp:529
void onCellEditorHidden(wxGridEvent &aEvent)
Definition: wx_grid.cpp:372
void onGridColMove(wxGridEvent &aEvent)
Definition: wx_grid.cpp:778
void SetOptionalUnitValue(int aRow, int aCol, std::optional< int > aValue)
Set a unitized cell's optional value.
Definition: wx_grid.cpp:772
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:826
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:469
void DrawRowLabel(wxDC &dc, int row) override
A re-implementation of wxGrid::DrawRowLabel which draws flat borders.
Definition: wx_grid.cpp:589
std::bitset< 64 > GetShownColumns()
Definition: wx_grid.cpp:488
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:741
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:701
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:649
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
Definition: wx_grid.h:280
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:550
static void CellEditorTransformSizeRect(wxRect &aRect)
A helper function to tweak sizes of text-based cell editors depending on OS.
Definition: wx_grid.cpp:85
EDA_DATA_TYPE
The type of unit.
Definition: eda_units.h:38
EDA_UNITS
Definition: eda_units.h:48
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: wxgtk/ui.cpp:48
KICOMMON_API wxFont GetControlFont(wxWindow *aWindow)
Definition: ui_common.cpp:168
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:893
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