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( 3 )
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 );
168 m_attrOdd->SetBackgroundColour( rowColor );
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_attrOdd->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 );
213 if( GetColLabelSize() > 0 )
247 wxGrid::SetColLabelSize( 0 );
253 wxGrid::SetColLabelSize( std::max( aHeight, minHeight ) );
267 int numberCols = GetNumberCols();
268 int* formBuilderColWidths =
new int[numberCols];
270 for(
int i = 0; i < numberCols; ++i )
271 formBuilderColWidths[ i ] = GetColSize( i );
273 wxGrid::SetTable( aTable );
277 numberCols = std::min( numberCols, GetNumberCols() );
279 for(
int i = 0; i < numberCols; ++i )
284 SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
287 delete[] formBuilderColWidths;
300 wxGridTableBase* table = wxGrid::GetTable();
302 wxCHECK_MSG( table, ,
303 "Tried to enable alternate row colors without a table assigned to the grid" );
307 wxColor
color = wxGrid::GetDefaultCellBackgroundColour();
312 table->SetAttrProvider(
nullptr );
322 int row = aEvent.GetRow();
323 int col = aEvent.GetCol();
325 if( row >= 0 && row < GetNumberRows() && col >= 0 && col < GetNumberCols() )
327 if( GetSelectionMode() == wxGrid::wxGridSelectCells )
329 SelectBlock( row, col, row, col,
false );
331 else if( GetSelectionMode() == wxGrid::wxGridSelectRows
332 || GetSelectionMode() == wxGrid::wxGridSelectRowsOrColumns )
334 SelectBlock( row, 0, row, GetNumberCols() - 1,
false );
336 else if( GetSelectionMode() == wxGrid::wxGridSelectColumns )
338 SelectBlock( 0, col, GetNumberRows() - 1, col,
false );
348 int row = aEvent.GetRow();
349 int col = aEvent.GetCol();
351 const std::pair<wxString, wxString>& beforeAfter =
m_evalBeforeAfter[ { row, col } ];
353 if( GetCellValue( row, col ) == beforeAfter.second )
354 SetCellValue( row, col, beforeAfter.first );
370 int row = aEvent.GetRow();
371 int col = aEvent.GetCol();
374 [
this, row, col, unitsProvider]()
376 wxString stringValue = GetCellValue( row, col );
378 if(
m_eval->Process( stringValue ) )
383 if( stringValue != evalValue )
385 SetCellValue( row, col, evalValue );
405 wxGrid::SetTable(
nullptr );
412 wxString shownColumns;
414 for(
int i = 0; i < GetNumberCols(); ++i )
416 if( IsColShown( i ) )
418 if( shownColumns.Length() )
419 shownColumns << wxT(
" " );
431 std::bitset<64> shownColumns;
433 for(
int ii = 0; ii < GetNumberCols(); ++ii )
434 shownColumns[ii] = IsColShown( ii );
442 for(
int i = 0; i < GetNumberCols(); ++i )
445 wxStringTokenizer shownTokens( shownColumns );
447 while( shownTokens.HasMoreTokens() )
450 shownTokens.GetNextToken().ToLong( &colNumber );
452 if( colNumber >= 0 && colNumber < GetNumberCols() )
453 ShowCol( (
int) colNumber );
460 for(
int ii = 0; ii < GetNumberCols(); ++ ii )
462 if( aShownColumns[ii] )
472 if( m_nativeColumnLabels )
473 wxGrid::DrawCornerLabel( dc );
475 wxRect rect( wxSize( m_rowLabelWidth, m_colLabelHeight ) );
482 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
483 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
484 dc.DrawRectangle( rect.Inflate( 1 ) );
493 if( m_nativeColumnLabels )
494 wxGrid::DrawColLabel( dc, col );
496 if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
499 wxRect rect( GetColLeft( col ), 0, GetColWidth( col ), m_colLabelHeight );
506 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
507 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
508 dc.DrawRectangle( rect.Inflate( 1 ) );
514 dc.SetFont( GetLabelFont() );
517 GetColLabelAlignment( &hAlign, &vAlign );
518 const int orient = GetColLabelTextOrientation();
521 hAlign = wxALIGN_LEFT;
523 if( hAlign == wxALIGN_LEFT )
526 rend.DrawLabel( *
this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
532 if ( GetRowHeight( row ) <= 0 || m_rowLabelWidth <= 0 )
535 wxRect rect( 0, GetRowTop( row ), m_rowLabelWidth, GetRowHeight( row ) );
542 wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
543 wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
544 dc.DrawRectangle( rect.Inflate( 1 ) );
550 dc.SetFont( GetLabelFont() );
553 GetRowLabelAlignment(&hAlign, &vAlign);
555 if( hAlign == wxALIGN_LEFT )
558 rend.DrawLabel( *
this, dc, GetRowLabelValue( row ), rect, hAlign, vAlign, wxHORIZONTAL );
564 if( !IsCellEditControlEnabled() )
567 HideCellEditControl();
570 m_cellEditCtrlEnabled =
false;
572 int row = m_currentCellCoords.GetRow();
573 int col = m_currentCellCoords.GetCol();
575 wxString oldval = GetCellValue( row, col );
578 wxGridCellAttr* attr = GetCellAttr( row, col );
579 wxGridCellEditor*
editor = attr->GetEditor(
this, row, col );
581 editor->EndEdit( row, col,
this, oldval, &newval );
592 if( !IsCellEditControlEnabled() )
595 if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
598 HideCellEditControl();
601 m_cellEditCtrlEnabled =
false;
603 int row = m_currentCellCoords.GetRow();
604 int col = m_currentCellCoords.GetCol();
606 wxString oldval = GetCellValue( row, col );
609 wxGridCellAttr* attr = GetCellAttr( row, col );
610 wxGridCellEditor*
editor = attr->GetEditor(
this, row, col );
612 bool changed =
editor->EndEdit( row, col,
this, oldval, &newval );
619 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
622 editor->ApplyEdit( row, col,
this );
627 if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
630 SetCellValue( row, col, oldval );
655 wxString stringValue = GetCellValue( aRow, aCol );
661 if(
m_eval->Process( stringValue ) )
662 stringValue =
m_eval->Result();
676 SetCellValue( aRow, aCol, unitsProvider->
StringFromValue( aValue,
true ) );
694 size = GetRowLabelSize();
696 for(
int row = 0; aContents && row < GetNumberRows(); row++ )
697 size = std::max( size,
int( GetTextExtent( GetRowLabelValue( row ) + wxS(
"M" ) ).x ) );
702 size = GetColSize( aCol );
710 size = std::max( size,
int( GetTextExtent( GetColLabelValue( aCol ) + wxS(
"M" ) ).x ) );
713 for(
int row = 0; aContents && row < GetNumberRows(); row++ )
716 if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
717 size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + wxS(
"M" ) ).x );
719 size = std::max( size, GetTextExtent(
"MM" ).x );
729 int line_height = int( GetTextExtent(
"Mj" ).y ) + 3;
730 int row_height = GetColLabelSize();
731 int initial_row_height = row_height;
736 for(
int col = 0; col < GetNumberCols(); col++ )
738 int nl_count = GetColLabelValue( col ).Freq(
'\n' );
743 if( row_height < line_height * ( nl_count+1 ) )
744 row_height += line_height * nl_count;
750 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
void UpdateColors(const wxColor &aBaseColor)
wxGridCellAttrPtr m_attrOdd
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