25#include <wx/tokenzr.h>
27#include <wx/settings.h>
29#include <wx/textentry.h>
43 wxGridCellAttr::wxAttrKind aKind )
45 wxGridCellAttr* attr = aInputAttr;
47 if( wxGridCellAttrProvider* provider = GetAttrProvider() )
49 wxGridCellAttr* providerAttr = provider->GetAttr( aRow, aCol, aKind );
53 attr =
new wxGridCellAttr;
54 attr->SetKind( wxGridCellAttr::Merged );
58 attr->MergeWith( aInputAttr );
62 attr->MergeWith( providerAttr );
63 providerAttr->DecRef();
71#define MIN_GRIDCELL_MARGIN FromDIP( 2 )
77 aEntry->SetMargins( 0, 0 );
83#if defined( __WXMSW__ ) || defined( __WXGTK__ )
91 KIGFX::COLOR4D bg = wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK );
92 KIGFX::COLOR4D fg = wxSystemSettings::GetColour( wxSYS_COLOUR_ACTIVEBORDER );
103 wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
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 );
120 wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
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 );
137 wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
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 );
173 wxGridCellAttr::wxAttrKind kind )
const override
175 wxGridCellAttrPtr cellAttr( wxGridCellAttrProvider::GetAttr( row, col, kind ) );
179 return cellAttr.release();
187 if( !cellAttr->HasBackgroundColour() )
189 cellAttr = cellAttr->Clone();
190 cellAttr->SetBackgroundColour(
m_attrEven->GetBackgroundColour() );
194 return cellAttr.release();
203 long style,
const wxString&
name ) :
204 wxGrid( parent, id, pos, size, style,
name ),
205 m_weOwnTable( false )
207 SetDefaultCellOverflow(
false );
235 wxGrid::SetColLabelSize( wxGRID_AUTOSIZE );
248 if( aHeight == 0 || aHeight == wxGRID_AUTOSIZE )
250 wxGrid::SetColLabelSize( aHeight );
257 wxGrid::SetColLabelSize( std::max( aHeight, minHeight ) );
271 int numberCols = GetNumberCols();
272 int* formBuilderColWidths =
new int[numberCols];
274 for(
int i = 0; i < numberCols; ++i )
275 formBuilderColWidths[ i ] = GetColSize( i );
277 wxGrid::SetTable( aTable );
281 numberCols = std::min( numberCols, GetNumberCols() );
283 for(
int i = 0; i < numberCols; ++i )
288 SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
291 delete[] formBuilderColWidths;
304 wxGridTableBase* table = wxGrid::GetTable();
306 wxCHECK_MSG( table, ,
307 "Tried to enable alternate row colors without a table assigned to the grid" );
311 wxColor
color = wxGrid::GetDefaultCellBackgroundColour();
316 table->SetAttrProvider(
nullptr );
326 int row = aEvent.GetRow();
327 int col = aEvent.GetCol();
329 if( row >= 0 && row < GetNumberRows() && col >= 0 && col < GetNumberCols() )
331 if( GetSelectionMode() == wxGrid::wxGridSelectCells )
333 SelectBlock( row, col, row, col,
false );
335 else if( GetSelectionMode() == wxGrid::wxGridSelectRows
336 || GetSelectionMode() == wxGrid::wxGridSelectRowsOrColumns )
338 SelectBlock( row, 0, row, GetNumberCols() - 1,
false );
340 else if( GetSelectionMode() == wxGrid::wxGridSelectColumns )
342 SelectBlock( 0, col, GetNumberRows() - 1, col,
false );
352 int row = aEvent.GetRow();
353 int col = aEvent.GetCol();
355 const std::pair<wxString, wxString>& beforeAfter =
m_evalBeforeAfter[ { row, col } ];
357 if( GetCellValue( row, col ) == beforeAfter.second )
358 SetCellValue( row, col, beforeAfter.first );
374 int row = aEvent.GetRow();
375 int col = aEvent.GetCol();
378 [
this, row, col, unitsProvider]()
380 wxString stringValue = GetCellValue( row, col );
382 if(
m_eval->Process( stringValue ) )
387 if( stringValue != evalValue )
389 SetCellValue( row, col, evalValue );
409 wxGrid::SetTable(
nullptr );
416 wxString shownColumns;
418 for(
int i = 0; i < GetNumberCols(); ++i )
420 if( IsColShown( i ) )
422 if( shownColumns.Length() )
423 shownColumns << wxT(
" " );
435 std::bitset<64> shownColumns;
437 for(
int ii = 0; ii < GetNumberCols(); ++ii )
438 shownColumns[ii] = IsColShown( ii );
446 for(
int i = 0; i < GetNumberCols(); ++i )
449 wxStringTokenizer shownTokens( shownColumns );
451 while( shownTokens.HasMoreTokens() )
454 shownTokens.GetNextToken().ToLong( &colNumber );
456 if( colNumber >= 0 && colNumber < GetNumberCols() )
457 ShowCol( (
int) colNumber );
464 for(
int ii = 0; ii < GetNumberCols(); ++ ii )
466 if( aShownColumns[ii] )
476 if( m_nativeColumnLabels )
477 wxGrid::DrawCornerLabel( dc );
479 wxRect rect( wxSize( m_rowLabelWidth, m_colLabelHeight ) );
486 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
487 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
488 dc.DrawRectangle( rect.Inflate( 1 ) );
497 if( m_nativeColumnLabels )
498 wxGrid::DrawColLabel( dc, col );
500 if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
503 wxRect rect( GetColLeft( col ), 0, GetColWidth( col ), m_colLabelHeight );
510 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
511 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
512 dc.DrawRectangle( rect.Inflate( 1 ) );
518 dc.SetFont( GetLabelFont() );
521 GetColLabelAlignment( &hAlign, &vAlign );
522 const int orient = GetColLabelTextOrientation();
525 hAlign = wxALIGN_LEFT;
527 if( hAlign == wxALIGN_LEFT )
530 rend.DrawLabel( *
this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
536 if ( GetRowHeight( row ) <= 0 || m_rowLabelWidth <= 0 )
539 wxRect rect( 0, GetRowTop( row ), m_rowLabelWidth, GetRowHeight( row ) );
546 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
547 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
548 dc.DrawRectangle( rect.Inflate( 1 ) );
554 dc.SetFont( GetLabelFont() );
557 GetRowLabelAlignment(&hAlign, &vAlign);
559 if( hAlign == wxALIGN_LEFT )
562 rend.DrawLabel( *
this, dc, GetRowLabelValue( row ), rect, hAlign, vAlign, wxHORIZONTAL );
568 if( !IsCellEditControlEnabled() )
571 HideCellEditControl();
574 m_cellEditCtrlEnabled =
false;
576 int row = m_currentCellCoords.GetRow();
577 int col = m_currentCellCoords.GetCol();
579 wxString oldval = GetCellValue( row, col );
582 wxGridCellAttr* attr = GetCellAttr( row, col );
583 wxGridCellEditor*
editor = attr->GetEditor(
this, row, col );
585 editor->EndEdit( row, col,
this, oldval, &newval );
596 if( !IsCellEditControlEnabled() )
599 if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
602 HideCellEditControl();
605 m_cellEditCtrlEnabled =
false;
607 int row = m_currentCellCoords.GetRow();
608 int col = m_currentCellCoords.GetCol();
610 wxString oldval = GetCellValue( row, col );
613 wxGridCellAttr* attr = GetCellAttr( row, col );
614 wxGridCellEditor*
editor = attr->GetEditor(
this, row, col );
616 bool changed =
editor->EndEdit( row, col,
this, oldval, &newval );
623 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
626 editor->ApplyEdit( row, col,
this );
631 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
634 SetCellValue( row, col, oldval );
659 wxString stringValue = GetCellValue( aRow, aCol );
665 if(
m_eval->Process( stringValue ) )
666 stringValue =
m_eval->Result();
680 SetCellValue( aRow, aCol, unitsProvider->
StringFromValue( aValue,
true ) );
698 size = GetRowLabelSize();
700 for(
int row = 0; aContents && row < GetNumberRows(); row++ )
701 size = std::max( size,
int( GetTextExtent( GetRowLabelValue( row ) + wxS(
"M" ) ).x ) );
706 size = GetColSize( aCol );
714 size = std::max( size,
int( GetTextExtent( GetColLabelValue( aCol ) + wxS(
"M" ) ).x ) );
717 for(
int row = 0; aContents && row < GetNumberRows(); row++ )
720 if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
721 size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + wxS(
"M" ) ).x );
723 size = std::max( size, GetTextExtent(
"MM" ).x );
733 int line_height = int( GetTextExtent(
"Mj" ).y ) + 3;
734 int row_height = GetColLabelSize();
735 int initial_row_height = row_height;
740 for(
int col = 0; col < GetNumberCols(); col++ )
742 int nl_count = GetColLabelValue( col ).Freq(
'\n' );
747 if( row_height < line_height * ( nl_count+1 ) )
748 row_height += line_height * nl_count;
754 if( initial_row_height != row_height )
A color representation with 4 components: red, green, blue, alpha.
wxColour ToColour() const
COLOR4D Mix(const COLOR4D &aColor, double aFactor) const
Return a color that is mixed with the input by a factor.
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...
WX_GRID_ALT_ROW_COLOR_PROVIDER(const wxColor &aBaseColor)
wxGridCellAttr * GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) const override
wxGridCellAttrPtr m_attrEven
void UpdateColors(const wxColor &aBaseColor)
void DrawBorder(const wxGrid &grid, wxDC &dc, wxRect &rect) const override
wxGridCellAttr * enhanceAttr(wxGridCellAttr *aInputAttr, int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind)
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.
void onGridCellSelect(wxGridEvent &aEvent)
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...
void onDPIChanged(wxDPIChangedEvent &event)
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
std::map< int, UNITS_PROVIDER * > m_unitsProviders
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...
void DestroyTable(wxGridTableBase *aTable)
Work-around for a bug in wxGrid which crashes when deleting the table if the cell edit control was no...
bool CancelPendingChanges()
void SetColLabelSize(int aHeight)
Hide wxGrid's SetColLabelSize() method with one which makes sure the size is tall enough for the syst...
void SetUnitValue(int aRow, int aCol, int aValue)
Set a unitized cell's value.
WX_GRID(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxWANTS_CHARS, const wxString &name=wxGridNameStr)
int GetUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
std::vector< int > m_autoEvalCols
std::map< std::pair< int, int >, std::pair< wxString, wxString > > m_evalBeforeAfter
void DrawCornerLabel(wxDC &dc) override
A re-implementation of wxGrid::DrawCornerLabel which draws flat borders.
void onCellEditorHidden(wxGridEvent &aEvent)
void onGridColMove(wxGridEvent &aEvent)
void onCellEditorShown(wxGridEvent &aEvent)
void EnsureColLabelsVisible()
Ensure the height of the row displaying the column labels is enough, even if labels are multiline tex...
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
void DrawRowLabel(wxDC &dc, int row) override
A re-implementation of wxGrid::DrawRowLabel which draws flat borders.
std::bitset< 64 > GetShownColumns()
static void CellEditorSetMargins(wxTextEntryBase *aEntry)
A helper function to set OS-specific margins for text-based cell editors.
void EnableAlternateRowColors(bool aEnable=true)
Enable alternate row highlighting, where every odd row has a different background color than the even...
void SetUnitsProvider(UNITS_PROVIDER *aProvider, int aCol=0)
Set a UNITS_PROVIDER to enable use of unit- and eval-based Getters.
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
void DrawColLabel(wxDC &dc, int col) override
A re-implementation of wxGrid::DrawColLabel which left-aligns the first column and draws flat borders...
static void CellEditorTransformSizeRect(wxRect &aRect)
A helper function to tweak sizes of text-based cell editors depending on OS.
KICOMMON_API wxFont GetControlFont(wxWindow *aWindow)
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
PGM_BASE & Pgm()
The global Program "get" accessor.
Functions to provide common constants and other functions to assist in making a consistent UI.
wxColour getBorderColour()
#define MIN_GRIDCELL_MARGIN