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 (C) 2018-2023 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
31#include <widgets/wx_grid.h>
32#include <widgets/ui_common.h>
33#include <algorithm>
34#include <core/kicad_algo.h>
35#include <gal/color4d.h>
36#include <kiplatform/ui.h>
37
38#include <pgm_base.h>
40
41
42wxGridCellAttr* WX_GRID_TABLE_BASE::enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
43 wxGridCellAttr::wxAttrKind aKind )
44{
45 wxGridCellAttr* attr = aInputAttr;
46
47 if( wxGridCellAttrProvider* provider = GetAttrProvider() )
48 {
49 wxGridCellAttr* providerAttr = provider->GetAttr( aRow, aCol, aKind );
50
51 if( providerAttr )
52 {
53 attr = new wxGridCellAttr;
54 attr->SetKind( wxGridCellAttr::Merged );
55
56 if( aInputAttr )
57 {
58 attr->MergeWith( aInputAttr );
59 aInputAttr->DecRef();
60 }
61
62 attr->MergeWith( providerAttr );
63 providerAttr->DecRef();
64 }
65 }
66
67 return attr;
68}
69
70
71#define MIN_GRIDCELL_MARGIN FromDIP( 2 )
72
73
74void WX_GRID::CellEditorSetMargins( wxTextEntryBase* aEntry )
75{
76 // This is consistent with wxGridCellTextEditor. But works differently across platforms or course.
77 aEntry->SetMargins( 0, 0 );
78}
79
80
82{
83#if defined( __WXMSW__ ) || defined( __WXGTK__ )
84 aRect.Deflate( 2 );
85#endif
86}
87
88
90{
91 KIGFX::COLOR4D bg = wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK );
92 KIGFX::COLOR4D fg = wxSystemSettings::GetColour( wxSYS_COLOUR_ACTIVEBORDER );
93 KIGFX::COLOR4D border = fg.Mix( bg, 0.50 );
94 return border.ToColour();
95}
96
97
98class WX_GRID_CORNER_HEADER_RENDERER : public wxGridCornerHeaderRendererDefault
99{
100public:
101 void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
102 {
103 wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
104 wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
105
106 rect.SetTop( rect.GetTop() + 1 );
107 rect.SetLeft( rect.GetLeft() + 1 );
108 rect.SetBottom( rect.GetBottom() - 1 );
109 rect.SetRight( rect.GetRight() - 1 );
110 dc.DrawRectangle( rect );
111 }
112};
113
114
115class WX_GRID_COLUMN_HEADER_RENDERER : public wxGridColumnHeaderRendererDefault
116{
117public:
118 void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
119 {
120 wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
121 wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
122
123 rect.SetTop( rect.GetTop() + 1 );
124 rect.SetLeft( rect.GetLeft() );
125 rect.SetBottom( rect.GetBottom() - 1 );
126 rect.SetRight( rect.GetRight() - 1 );
127 dc.DrawRectangle( rect );
128 }
129};
130
131
132class WX_GRID_ROW_HEADER_RENDERER : public wxGridRowHeaderRendererDefault
133{
134public:
135 void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
136 {
137 wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
138 wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
139
140 rect.SetTop( rect.GetTop() + 1 );
141 rect.SetLeft( rect.GetLeft() + 1 );
142 rect.SetBottom( rect.GetBottom() - 1 );
143 rect.SetRight( rect.GetRight() );
144 dc.DrawRectangle( rect );
145 }
146};
147
148
153class WX_GRID_ALT_ROW_COLOR_PROVIDER : public wxGridCellAttrProvider
154{
155public:
156 WX_GRID_ALT_ROW_COLOR_PROVIDER( const wxColor& aBaseColor ) : wxGridCellAttrProvider(),
157 m_attrEven( new wxGridCellAttr() )
158 {
159 UpdateColors( aBaseColor );
160 }
161
162
163 void UpdateColors( const wxColor& aBaseColor )
164 {
165 // Choose the default color, taking into account if the dark mode theme is enabled
166 wxColor rowColor = aBaseColor.ChangeLightness( KIPLATFORM::UI::IsDarkTheme() ? 105 : 95 );
167
168 m_attrEven->SetBackgroundColour( rowColor );
169 }
170
171
172 wxGridCellAttr* GetAttr( int row, int col,
173 wxGridCellAttr::wxAttrKind kind ) const override
174 {
175 wxGridCellAttrPtr cellAttr( wxGridCellAttrProvider::GetAttr( row, col, kind ) );
176
177 // Just pass through the cell attribute on odd rows (start normal to allow for the header row)
178 if( !( row % 2 ) )
179 return cellAttr.release();
180
181 if( !cellAttr )
182 {
183 cellAttr = m_attrEven;
184 }
185 else
186 {
187 if( !cellAttr->HasBackgroundColour() )
188 {
189 cellAttr = cellAttr->Clone();
190 cellAttr->SetBackgroundColour( m_attrEven->GetBackgroundColour() );
191 }
192 }
193
194 return cellAttr.release();
195 }
196
197private:
198 wxGridCellAttrPtr m_attrEven;
199};
200
201
202WX_GRID::WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
203 long style, const wxString& name ) :
204 wxGrid( parent, id, pos, size, style, name ),
205 m_weOwnTable( false )
206{
207 SetDefaultCellOverflow( false );
208
209 // Make sure the GUI font scales properly
210 SetDefaultCellFont( KIUI::GetControlFont( this ) );
212
213 Connect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
214 Connect( wxEVT_GRID_EDITOR_SHOWN, wxGridEventHandler( WX_GRID::onCellEditorShown ), nullptr, this );
215 Connect( wxEVT_GRID_EDITOR_HIDDEN, wxGridEventHandler( WX_GRID::onCellEditorHidden ), nullptr, this );
216}
217
218
220{
221 if( m_weOwnTable )
222 DestroyTable( GetTable() );
223
224 Disconnect( wxEVT_GRID_EDITOR_SHOWN, wxGridEventHandler( WX_GRID::onCellEditorShown ), nullptr, this );
225 Disconnect( wxEVT_GRID_EDITOR_HIDDEN, wxGridEventHandler( WX_GRID::onCellEditorHidden ), nullptr, this );
226 Disconnect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
227}
228
229
230void WX_GRID::onDPIChanged(wxDPIChangedEvent& aEvt)
231{
232 CallAfter(
233 [&]()
234 {
235 wxGrid::SetColLabelSize( wxGRID_AUTOSIZE );
236 } );
237
240#ifndef __WXMAC__
241 aEvt.Skip();
242#endif
243}
244
245
246void WX_GRID::SetColLabelSize( int aHeight )
247{
248 if( aHeight == 0 || aHeight == wxGRID_AUTOSIZE )
249 {
250 wxGrid::SetColLabelSize( aHeight );
251 return;
252 }
253
254 // Correct wxFormBuilder height for large fonts
255 int minHeight = GetCharHeight() + 2 * MIN_GRIDCELL_MARGIN;
256
257 wxGrid::SetColLabelSize( std::max( aHeight, minHeight ) );
258}
259
260
261void WX_GRID::SetLabelFont( const wxFont& aFont )
262{
263 wxGrid::SetLabelFont( KIUI::GetControlFont( this ) );
264}
265
266
267void WX_GRID::SetTable( wxGridTableBase* aTable, bool aTakeOwnership )
268{
269 // wxGrid::SetTable() messes up the column widths from wxFormBuilder so we have to save
270 // and restore them.
271 int numberCols = GetNumberCols();
272 int* formBuilderColWidths = new int[numberCols];
273
274 for( int i = 0; i < numberCols; ++i )
275 formBuilderColWidths[ i ] = GetColSize( i );
276
277 wxGrid::SetTable( aTable );
278
279 // wxGrid::SetTable() may change the number of columns, so prevent out-of-bounds access
280 // to formBuilderColWidths
281 numberCols = std::min( numberCols, GetNumberCols() );
282
283 for( int i = 0; i < numberCols; ++i )
284 {
285 // correct wxFormBuilder width for large fonts and/or long translations
286 int headingWidth = GetTextExtent( GetColLabelValue( i ) ).x + 2 * MIN_GRIDCELL_MARGIN;
287
288 SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
289 }
290
291 delete[] formBuilderColWidths;
292
293 EnableAlternateRowColors( Pgm().GetCommonSettings()->m_Appearance.grid_striping );
294
295 Connect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
296 Connect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
297
298 m_weOwnTable = aTakeOwnership;
299}
300
301
303{
304 wxGridTableBase* table = wxGrid::GetTable();
305
306 wxCHECK_MSG( table, /* void */,
307 "Tried to enable alternate row colors without a table assigned to the grid" );
308
309 if( aEnable )
310 {
311 wxColor color = wxGrid::GetDefaultCellBackgroundColour();
312 table->SetAttrProvider( new WX_GRID_ALT_ROW_COLOR_PROVIDER( color ) );
313 }
314 else
315 {
316 table->SetAttrProvider( nullptr );
317 }
318}
319
320
321void WX_GRID::onGridCellSelect( wxGridEvent& aEvent )
322{
323 // Highlight the selected cell.
324 // Calling SelectBlock() allows a visual effect when cells are selected by tab or arrow keys.
325 // Otherwise, one cannot really know what actual cell is selected.
326 int row = aEvent.GetRow();
327 int col = aEvent.GetCol();
328
329 if( row >= 0 && row < GetNumberRows() && col >= 0 && col < GetNumberCols() )
330 {
331 if( GetSelectionMode() == wxGrid::wxGridSelectCells )
332 {
333 SelectBlock( row, col, row, col, false );
334 }
335 else if( GetSelectionMode() == wxGrid::wxGridSelectRows
336 || GetSelectionMode() == wxGrid::wxGridSelectRowsOrColumns )
337 {
338 SelectBlock( row, 0, row, GetNumberCols() - 1, false );
339 }
340 else if( GetSelectionMode() == wxGrid::wxGridSelectColumns )
341 {
342 SelectBlock( 0, col, GetNumberRows() - 1, col, false );
343 }
344 }
345}
346
347
348void WX_GRID::onCellEditorShown( wxGridEvent& aEvent )
349{
350 if( alg::contains( m_autoEvalCols, aEvent.GetCol() ) )
351 {
352 int row = aEvent.GetRow();
353 int col = aEvent.GetCol();
354
355 const std::pair<wxString, wxString>& beforeAfter = m_evalBeforeAfter[ { row, col } ];
356
357 if( GetCellValue( row, col ) == beforeAfter.second )
358 SetCellValue( row, col, beforeAfter.first );
359 }
360}
361
362
363void WX_GRID::onCellEditorHidden( wxGridEvent& aEvent )
364{
365 if( alg::contains( m_autoEvalCols, aEvent.GetCol() ) )
366 {
367 UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aEvent.GetCol() ];
368
369 if( !unitsProvider )
370 unitsProvider = m_unitsProviders.begin()->second;
371
372 m_eval->SetDefaultUnits( unitsProvider->GetUserUnits() );
373
374 int row = aEvent.GetRow();
375 int col = aEvent.GetCol();
376
377 CallAfter(
378 [this, row, col, unitsProvider]()
379 {
380 wxString stringValue = GetCellValue( row, col );
381
382 if( m_eval->Process( stringValue ) )
383 {
384 int val = unitsProvider->ValueFromString( m_eval->Result() );
385 wxString evalValue = unitsProvider->StringFromValue( val, true );
386
387 if( stringValue != evalValue )
388 {
389 SetCellValue( row, col, evalValue );
390 m_evalBeforeAfter[ { row, col } ] = { stringValue, evalValue };
391 }
392 }
393 } );
394 }
395
396 aEvent.Skip();
397}
398
399
400void WX_GRID::DestroyTable( wxGridTableBase* aTable )
401{
402 // wxGrid's destructor will crash trying to look up the cell attr if the edit control
403 // is left open. Normally it's closed in Validate(), but not if the user hit Cancel.
404 CommitPendingChanges( true /* quiet mode */ );
405
406 Disconnect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
407 Disconnect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
408
409 wxGrid::SetTable( nullptr );
410 delete aTable;
411}
412
413
415{
416 wxString shownColumns;
417
418 for( int i = 0; i < GetNumberCols(); ++i )
419 {
420 if( IsColShown( i ) )
421 {
422 if( shownColumns.Length() )
423 shownColumns << wxT( " " );
424
425 shownColumns << i;
426 }
427 }
428
429 return shownColumns;
430}
431
432
434{
435 std::bitset<64> shownColumns;
436
437 for( int ii = 0; ii < GetNumberCols(); ++ii )
438 shownColumns[ii] = IsColShown( ii );
439
440 return shownColumns;
441}
442
443
444void WX_GRID::ShowHideColumns( const wxString& shownColumns )
445{
446 for( int i = 0; i < GetNumberCols(); ++i )
447 HideCol( i );
448
449 wxStringTokenizer shownTokens( shownColumns );
450
451 while( shownTokens.HasMoreTokens() )
452 {
453 long colNumber;
454 shownTokens.GetNextToken().ToLong( &colNumber );
455
456 if( colNumber >= 0 && colNumber < GetNumberCols() )
457 ShowCol( (int) colNumber );
458 }
459}
460
461
462void WX_GRID::ShowHideColumns( const std::bitset<64>& aShownColumns )
463{
464 for( int ii = 0; ii < GetNumberCols(); ++ ii )
465 {
466 if( aShownColumns[ii] )
467 ShowCol( ii );
468 else
469 HideCol( ii );
470 }
471}
472
473
475{
476 if( m_nativeColumnLabels )
477 wxGrid::DrawCornerLabel( dc );
478
479 wxRect rect( wxSize( m_rowLabelWidth, m_colLabelHeight ) );
480
482
483 // It is reported that we need to erase the background to avoid display
484 // artifacts, see #12055.
485 {
486 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
487 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
488 dc.DrawRectangle( rect.Inflate( 1 ) );
489 }
490
491 rend.DrawBorder( *this, dc, rect );
492}
493
494
495void WX_GRID::DrawColLabel( wxDC& dc, int col )
496{
497 if( m_nativeColumnLabels )
498 wxGrid::DrawColLabel( dc, col );
499
500 if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
501 return;
502
503 wxRect rect( GetColLeft( col ), 0, GetColWidth( col ), m_colLabelHeight );
504
506
507 // It is reported that we need to erase the background to avoid display
508 // artifacts, see #12055.
509 {
510 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
511 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
512 dc.DrawRectangle( rect.Inflate( 1 ) );
513 }
514
515 rend.DrawBorder( *this, dc, rect );
516
517 // Make sure fonts get scaled correctly on GTK HiDPI monitors
518 dc.SetFont( GetLabelFont() );
519
520 int hAlign, vAlign;
521 GetColLabelAlignment( &hAlign, &vAlign );
522 const int orient = GetColLabelTextOrientation();
523
524 if( col == 0 )
525 hAlign = wxALIGN_LEFT;
526
527 if( hAlign == wxALIGN_LEFT )
528 rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
529
530 rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
531}
532
533
534void WX_GRID::DrawRowLabel( wxDC& dc, int row )
535{
536 if ( GetRowHeight( row ) <= 0 || m_rowLabelWidth <= 0 )
537 return;
538
539 wxRect rect( 0, GetRowTop( row ), m_rowLabelWidth, GetRowHeight( row ) );
540
541 static WX_GRID_ROW_HEADER_RENDERER rend;
542
543 // It is reported that we need to erase the background to avoid display
544 // artifacts, see #12055.
545 {
546 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
547 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
548 dc.DrawRectangle( rect.Inflate( 1 ) );
549 }
550
551 rend.DrawBorder( *this, dc, rect );
552
553 // Make sure fonts get scaled correctly on GTK HiDPI monitors
554 dc.SetFont( GetLabelFont() );
555
556 int hAlign, vAlign;
557 GetRowLabelAlignment(&hAlign, &vAlign);
558
559 if( hAlign == wxALIGN_LEFT )
560 rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
561
562 rend.DrawLabel( *this, dc, GetRowLabelValue( row ), rect, hAlign, vAlign, wxHORIZONTAL );
563}
564
565
567{
568 if( !IsCellEditControlEnabled() )
569 return true;
570
571 HideCellEditControl();
572
573 // do it after HideCellEditControl()
574 m_cellEditCtrlEnabled = false;
575
576 int row = m_currentCellCoords.GetRow();
577 int col = m_currentCellCoords.GetCol();
578
579 wxString oldval = GetCellValue( row, col );
580 wxString newval;
581
582 wxGridCellAttr* attr = GetCellAttr( row, col );
583 wxGridCellEditor* editor = attr->GetEditor( this, row, col );
584
585 editor->EndEdit( row, col, this, oldval, &newval );
586
587 editor->DecRef();
588 attr->DecRef();
589
590 return true;
591}
592
593
594bool WX_GRID::CommitPendingChanges( bool aQuietMode )
595{
596 if( !IsCellEditControlEnabled() )
597 return true;
598
599 if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
600 return false;
601
602 HideCellEditControl();
603
604 // do it after HideCellEditControl()
605 m_cellEditCtrlEnabled = false;
606
607 int row = m_currentCellCoords.GetRow();
608 int col = m_currentCellCoords.GetCol();
609
610 wxString oldval = GetCellValue( row, col );
611 wxString newval;
612
613 wxGridCellAttr* attr = GetCellAttr( row, col );
614 wxGridCellEditor* editor = attr->GetEditor( this, row, col );
615
616 bool changed = editor->EndEdit( row, col, this, oldval, &newval );
617
618 editor->DecRef();
619 attr->DecRef();
620
621 if( changed )
622 {
623 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
624 return false;
625
626 editor->ApplyEdit( row, col, this );
627
628 // for compatibility reasons dating back to wx 2.8 when this event
629 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
630 // didn't exist we allow vetoing this one too
631 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
632 {
633 // Event has been vetoed, set the data back.
634 SetCellValue( row, col, oldval );
635 return false;
636 }
637 }
638
639 return true;
640}
641
642
643void WX_GRID::SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol )
644{
645 m_unitsProviders[ aCol ] = aProvider;
646
647 if( !m_eval )
648 m_eval = std::make_unique<NUMERIC_EVALUATOR>( aProvider->GetUserUnits() );
649}
650
651
652int WX_GRID::GetUnitValue( int aRow, int aCol )
653{
654 UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aCol ];
655
656 if( !unitsProvider )
657 unitsProvider = m_unitsProviders.begin()->second;
658
659 wxString stringValue = GetCellValue( aRow, aCol );
660
661 if( alg::contains( m_autoEvalCols, aCol ) )
662 {
663 m_eval->SetDefaultUnits( unitsProvider->GetUserUnits() );
664
665 if( m_eval->Process( stringValue ) )
666 stringValue = m_eval->Result();
667 }
668
669 return unitsProvider->ValueFromString( stringValue );
670}
671
672
673void WX_GRID::SetUnitValue( int aRow, int aCol, int aValue )
674{
675 UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aCol ];
676
677 if( !unitsProvider )
678 unitsProvider = m_unitsProviders.begin()->second;
679
680 SetCellValue( aRow, aCol, unitsProvider->StringFromValue( aValue, true ) );
681}
682
683
684void WX_GRID::onGridColMove( wxGridEvent& aEvent )
685{
686 // wxWidgets won't move an open editor, so better just to close it
687 CommitPendingChanges( true );
688}
689
690
691int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep )
692{
693 int size = 0;
694
695 if( aCol < 0 )
696 {
697 if( aKeep )
698 size = GetRowLabelSize();
699
700 for( int row = 0; aContents && row < GetNumberRows(); row++ )
701 size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + wxS( "M" ) ).x ) );
702 }
703 else
704 {
705 if( aKeep )
706 size = GetColSize( aCol );
707
708 // 'M' is generally the widest character, so we buffer the column width by default to
709 // ensure we don't write a continuous line of text at the column header
710 if( aHeader )
711 {
713
714 size = std::max( size, int( GetTextExtent( GetColLabelValue( aCol ) + wxS( "M" ) ).x ) );
715 }
716
717 for( int row = 0; aContents && row < GetNumberRows(); row++ )
718 {
719 // If we have text, get the size. Otherwise, use a placeholder for the checkbox
720 if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
721 size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + wxS( "M" ) ).x );
722 else
723 size = std::max( size, GetTextExtent( "MM" ).x );
724 }
725 }
726
727 return size;
728}
729
730
732{
733 int line_height = int( GetTextExtent( "Mj" ).y ) + 3;
734 int row_height = GetColLabelSize();
735 int initial_row_height = row_height;
736
737 // Headers can be multiline. Fix the Column Label Height to show the full header
738 // However GetTextExtent does not work on multiline strings,
739 // and do not return the full text height (only the height of one line)
740 for( int col = 0; col < GetNumberCols(); col++ )
741 {
742 int nl_count = GetColLabelValue( col ).Freq( '\n' );
743
744 if( nl_count )
745 {
746 // Col Label height must be able to show nl_count+1 lines
747 if( row_height < line_height * ( nl_count+1 ) )
748 row_height += line_height * nl_count;
749 }
750 }
751
752 // Update the column label size, but only if needed, to avoid generating useless
753 // and perhaps annoying UI events when the size does not change
754 if( initial_row_height != row_height )
755 SetColLabelSize( row_height );
756}
int color
Definition: DXF_plotter.cpp:58
const char * name
Definition: DXF_plotter.cpp:57
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
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:154
WX_GRID_ALT_ROW_COLOR_PROVIDER(const wxColor &aBaseColor)
Definition: wx_grid.cpp:156
wxGridCellAttr * GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) const override
Definition: wx_grid.cpp:172
wxGridCellAttrPtr m_attrEven
Definition: wx_grid.cpp:198
void UpdateColors(const wxColor &aBaseColor)
Definition: wx_grid.cpp:163
void DrawBorder(const wxGrid &grid, wxDC &dc, wxRect &rect) const override
Definition: wx_grid.cpp:118
void DrawBorder(const wxGrid &grid, wxDC &dc, wxRect &rect) const override
Definition: wx_grid.cpp:101
void DrawBorder(const wxGrid &grid, wxDC &dc, wxRect &rect) const override
Definition: wx_grid.cpp:135
wxGridCellAttr * enhanceAttr(wxGridCellAttr *aInputAttr, int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind)
Definition: wx_grid.cpp:42
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=true, bool aKeep=false)
Calculates the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:691
void onGridCellSelect(wxGridEvent &aEvent)
Definition: wx_grid.cpp:321
~WX_GRID() override
Definition: wx_grid.cpp:219
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:261
bool m_weOwnTable
Definition: wx_grid.h:206
void onDPIChanged(wxDPIChangedEvent &event)
Definition: wx_grid.cpp:230
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition: wx_grid.cpp:444
std::map< int, UNITS_PROVIDER * > m_unitsProviders
Definition: wx_grid.h:208
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:267
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:400
bool CancelPendingChanges()
Definition: wx_grid.cpp:566
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:246
void SetUnitValue(int aRow, int aCol, int aValue)
Set a unitized cell's value.
Definition: wx_grid.cpp:673
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:202
int GetUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition: wx_grid.cpp:652
std::vector< int > m_autoEvalCols
Definition: wx_grid.h:210
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
Definition: wx_grid.h:212
void DrawCornerLabel(wxDC &dc) override
A re-implementation of wxGrid::DrawCornerLabel which draws flat borders.
Definition: wx_grid.cpp:474
void onCellEditorHidden(wxGridEvent &aEvent)
Definition: wx_grid.cpp:363
void onGridColMove(wxGridEvent &aEvent)
Definition: wx_grid.cpp:684
void onCellEditorShown(wxGridEvent &aEvent)
Definition: wx_grid.cpp:348
void EnsureColLabelsVisible()
Ensure the height of the row displaying the column labels is enough, even if labels are multiline tex...
Definition: wx_grid.cpp:731
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:414
void DrawRowLabel(wxDC &dc, int row) override
A re-implementation of wxGrid::DrawRowLabel which draws flat borders.
Definition: wx_grid.cpp:534
std::bitset< 64 > GetShownColumns()
Definition: wx_grid.cpp:433
static void CellEditorSetMargins(wxTextEntryBase *aEntry)
A helper function to set OS-specific margins for text-based cell editors.
Definition: wx_grid.cpp:74
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:302
void SetUnitsProvider(UNITS_PROVIDER *aProvider, int aCol=0)
Set a UNITS_PROVIDER to enable use of unit- and eval-based Getters.
Definition: wx_grid.cpp:643
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:594
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
Definition: wx_grid.h:209
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:495
static void CellEditorTransformSizeRect(wxRect &aRect)
A helper function to tweak sizes of text-based cell editors depending on OS.
Definition: wx_grid.cpp:81
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:160
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:1059
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:89
#define MIN_GRIDCELL_MARGIN
Definition: wx_grid.cpp:71