2018-05-30 11:52:19 +01:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2025-01-01 13:30:11 -08:00
|
|
|
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
|
2018-05-30 11:52:19 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2024-02-13 18:59:13 +00:00
|
|
|
#include <wx/colour.h>
|
2018-05-30 11:52:19 +01:00
|
|
|
#include <wx/tokenzr.h>
|
|
|
|
#include <wx/dc.h>
|
2023-01-16 02:46:09 +00:00
|
|
|
#include <wx/settings.h>
|
2024-04-13 23:25:57 +03:00
|
|
|
#include <wx/event.h> // Needed for textentry.h on MSW
|
|
|
|
#include <wx/textentry.h>
|
2023-01-16 02:46:09 +00:00
|
|
|
|
2024-07-26 20:49:29 +00:00
|
|
|
#include <widgets/grid_icon_text_helpers.h>
|
2020-12-17 08:12:18 -05:00
|
|
|
#include <widgets/wx_grid.h>
|
2021-09-10 15:59:27 +01:00
|
|
|
#include <widgets/ui_common.h>
|
2020-05-08 16:34:28 +02:00
|
|
|
#include <algorithm>
|
2022-10-11 18:01:47 +01:00
|
|
|
#include <core/kicad_algo.h>
|
2023-01-16 02:46:09 +00:00
|
|
|
#include <gal/color4d.h>
|
2024-02-13 18:59:13 +00:00
|
|
|
#include <kiplatform/ui.h>
|
2024-07-26 20:49:29 +00:00
|
|
|
#include <utility>
|
2024-02-13 18:59:13 +00:00
|
|
|
|
|
|
|
#include <pgm_base.h>
|
|
|
|
#include <settings/common_settings.h>
|
2024-08-08 10:24:17 +01:00
|
|
|
#include <dialog_shim.h>
|
2018-05-30 11:52:19 +01:00
|
|
|
|
2024-05-16 15:50:57 +01:00
|
|
|
|
|
|
|
wxGridCellAttr* WX_GRID_TABLE_BASE::enhanceAttr( wxGridCellAttr* aInputAttr, int aRow, int aCol,
|
|
|
|
wxGridCellAttr::wxAttrKind aKind )
|
|
|
|
{
|
|
|
|
wxGridCellAttr* attr = aInputAttr;
|
|
|
|
|
|
|
|
if( wxGridCellAttrProvider* provider = GetAttrProvider() )
|
|
|
|
{
|
|
|
|
wxGridCellAttr* providerAttr = provider->GetAttr( aRow, aCol, aKind );
|
|
|
|
|
|
|
|
if( providerAttr )
|
|
|
|
{
|
|
|
|
attr = new wxGridCellAttr;
|
|
|
|
attr->SetKind( wxGridCellAttr::Merged );
|
|
|
|
|
|
|
|
if( aInputAttr )
|
|
|
|
{
|
|
|
|
attr->MergeWith( aInputAttr );
|
|
|
|
aInputAttr->DecRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
attr->MergeWith( providerAttr );
|
|
|
|
providerAttr->DecRef();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-26 07:54:59 +03:00
|
|
|
#define MIN_GRIDCELL_MARGIN FromDIP( 2 )
|
2018-08-23 11:59:37 +01:00
|
|
|
|
|
|
|
|
2024-04-13 23:25:57 +03:00
|
|
|
void WX_GRID::CellEditorSetMargins( wxTextEntryBase* aEntry )
|
|
|
|
{
|
2025-01-16 07:37:05 -05:00
|
|
|
// This is consistent with wxGridCellTextEditor. But works differently across platforms or
|
|
|
|
// course.
|
2024-04-13 23:25:57 +03:00
|
|
|
aEntry->SetMargins( 0, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WX_GRID::CellEditorTransformSizeRect( wxRect& aRect )
|
|
|
|
{
|
|
|
|
#if defined( __WXMSW__ ) || defined( __WXGTK__ )
|
|
|
|
aRect.Deflate( 2 );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-16 02:46:09 +00:00
|
|
|
wxColour getBorderColour()
|
|
|
|
{
|
|
|
|
KIGFX::COLOR4D bg = wxSystemSettings::GetColour( wxSYS_COLOUR_FRAMEBK );
|
|
|
|
KIGFX::COLOR4D fg = wxSystemSettings::GetColour( wxSYS_COLOUR_ACTIVEBORDER );
|
|
|
|
KIGFX::COLOR4D border = fg.Mix( bg, 0.50 );
|
|
|
|
return border.ToColour();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class WX_GRID_CORNER_HEADER_RENDERER : public wxGridCornerHeaderRendererDefault
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
|
|
|
|
{
|
|
|
|
wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
|
|
|
|
wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
|
|
|
|
|
|
|
|
rect.SetTop( rect.GetTop() + 1 );
|
|
|
|
rect.SetLeft( rect.GetLeft() + 1 );
|
|
|
|
rect.SetBottom( rect.GetBottom() - 1 );
|
|
|
|
rect.SetRight( rect.GetRight() - 1 );
|
|
|
|
dc.DrawRectangle( rect );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class WX_GRID_COLUMN_HEADER_RENDERER : public wxGridColumnHeaderRendererDefault
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
|
|
|
|
{
|
|
|
|
wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
|
|
|
|
wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
|
|
|
|
|
|
|
|
rect.SetTop( rect.GetTop() + 1 );
|
|
|
|
rect.SetLeft( rect.GetLeft() );
|
|
|
|
rect.SetBottom( rect.GetBottom() - 1 );
|
|
|
|
rect.SetRight( rect.GetRight() - 1 );
|
|
|
|
dc.DrawRectangle( rect );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class WX_GRID_ROW_HEADER_RENDERER : public wxGridRowHeaderRendererDefault
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void DrawBorder( const wxGrid& grid, wxDC& dc, wxRect& rect ) const override
|
|
|
|
{
|
|
|
|
wxDCBrushChanger SetBrush( dc, *wxTRANSPARENT_BRUSH );
|
|
|
|
wxDCPenChanger SetPen( dc, wxPen( getBorderColour(), 1 ) );
|
|
|
|
|
|
|
|
rect.SetTop( rect.GetTop() + 1 );
|
|
|
|
rect.SetLeft( rect.GetLeft() + 1 );
|
|
|
|
rect.SetBottom( rect.GetBottom() - 1 );
|
|
|
|
rect.SetRight( rect.GetRight() );
|
|
|
|
dc.DrawRectangle( rect );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-02-13 18:59:13 +00:00
|
|
|
/**
|
2025-01-16 07:37:05 -05:00
|
|
|
* Attribute provider that provides attributes (or modifies the existing attribute) to alternate
|
|
|
|
* a row color between the odd and even rows.
|
2024-02-13 18:59:13 +00:00
|
|
|
*/
|
|
|
|
class WX_GRID_ALT_ROW_COLOR_PROVIDER : public wxGridCellAttrProvider
|
|
|
|
{
|
|
|
|
public:
|
2025-02-03 20:05:43 +00:00
|
|
|
WX_GRID_ALT_ROW_COLOR_PROVIDER( const wxColor& aBaseColor ) :
|
|
|
|
wxGridCellAttrProvider(),
|
|
|
|
m_attrEven( new wxGridCellAttr() )
|
2024-02-13 18:59:13 +00:00
|
|
|
{
|
|
|
|
UpdateColors( aBaseColor );
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateColors( const wxColor& aBaseColor )
|
|
|
|
{
|
|
|
|
// Choose the default color, taking into account if the dark mode theme is enabled
|
|
|
|
wxColor rowColor = aBaseColor.ChangeLightness( KIPLATFORM::UI::IsDarkTheme() ? 105 : 95 );
|
|
|
|
|
2024-05-16 09:15:40 -07:00
|
|
|
m_attrEven->SetBackgroundColour( rowColor );
|
2024-02-13 18:59:13 +00:00
|
|
|
}
|
|
|
|
|
2025-02-03 20:05:43 +00:00
|
|
|
wxGridCellAttr* GetAttr( int row, int col, wxGridCellAttr::wxAttrKind kind ) const override
|
2024-02-13 18:59:13 +00:00
|
|
|
{
|
|
|
|
wxGridCellAttrPtr cellAttr( wxGridCellAttrProvider::GetAttr( row, col, kind ) );
|
|
|
|
|
2025-01-16 07:37:05 -05:00
|
|
|
// Just pass through the cell attribute on odd rows (start normal to allow for the
|
|
|
|
// header row)
|
2024-05-16 09:15:40 -07:00
|
|
|
if( !( row % 2 ) )
|
2024-02-13 18:59:13 +00:00
|
|
|
return cellAttr.release();
|
|
|
|
|
|
|
|
if( !cellAttr )
|
|
|
|
{
|
2024-05-16 09:15:40 -07:00
|
|
|
cellAttr = m_attrEven;
|
2024-02-13 18:59:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( !cellAttr->HasBackgroundColour() )
|
|
|
|
{
|
|
|
|
cellAttr = cellAttr->Clone();
|
2024-05-16 09:15:40 -07:00
|
|
|
cellAttr->SetBackgroundColour( m_attrEven->GetBackgroundColour() );
|
2024-02-13 18:59:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cellAttr.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-05-16 09:15:40 -07:00
|
|
|
wxGridCellAttrPtr m_attrEven;
|
2024-02-13 18:59:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-08-19 17:10:14 +01:00
|
|
|
WX_GRID::WX_GRID( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
|
|
|
|
long style, const wxString& name ) :
|
|
|
|
wxGrid( parent, id, pos, size, style, name ),
|
2022-11-04 12:20:53 +00:00
|
|
|
m_weOwnTable( false )
|
2021-08-01 11:57:15 +01:00
|
|
|
{
|
2025-06-19 14:11:04 +01:00
|
|
|
// Grids with comboboxes need a bit of extra height; other grids look better if they're
|
|
|
|
// consistent.
|
|
|
|
SetDefaultRowSize( GetDefaultRowSize() + FromDIP( 4 ) );
|
|
|
|
|
2021-08-01 11:57:15 +01:00
|
|
|
SetDefaultCellOverflow( false );
|
2021-09-08 22:30:21 +01:00
|
|
|
|
2023-01-16 02:46:09 +00:00
|
|
|
// Make sure the GUI font scales properly
|
2021-09-11 20:07:33 +01:00
|
|
|
SetDefaultCellFont( KIUI::GetControlFont( this ) );
|
2023-01-17 13:24:49 +00:00
|
|
|
SetLabelFont( KIUI::GetControlFont( this ) );
|
2022-03-21 16:44:28 -07:00
|
|
|
|
|
|
|
Connect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
|
2025-06-19 14:11:04 +01:00
|
|
|
Connect( wxEVT_GRID_EDITOR_SHOWN, wxGridEventHandler( WX_GRID::onCellEditorShown ), nullptr, this );
|
|
|
|
Connect( wxEVT_GRID_EDITOR_HIDDEN, wxGridEventHandler( WX_GRID::onCellEditorHidden ), nullptr, this );
|
2021-08-01 11:57:15 +01:00
|
|
|
}
|
2018-08-19 17:10:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
WX_GRID::~WX_GRID()
|
|
|
|
{
|
|
|
|
if( m_weOwnTable )
|
|
|
|
DestroyTable( GetTable() );
|
2022-03-21 16:44:28 -07:00
|
|
|
|
2025-06-19 14:11:04 +01:00
|
|
|
Disconnect( wxEVT_GRID_EDITOR_SHOWN, wxGridEventHandler( WX_GRID::onCellEditorShown ), nullptr, this );
|
|
|
|
Disconnect( wxEVT_GRID_EDITOR_HIDDEN, wxGridEventHandler( WX_GRID::onCellEditorHidden ), nullptr, this );
|
|
|
|
Disconnect( wxEVT_DPI_CHANGED, wxDPIChangedEventHandler( WX_GRID::onDPIChanged ), nullptr, this );
|
2018-08-19 17:10:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-21 16:44:28 -07:00
|
|
|
void WX_GRID::onDPIChanged(wxDPIChangedEvent& aEvt)
|
|
|
|
{
|
2025-06-19 14:11:04 +01:00
|
|
|
CallAfter( [&]()
|
|
|
|
{
|
|
|
|
wxGrid::SetColLabelSize( wxGRID_AUTOSIZE );
|
|
|
|
} );
|
2024-05-26 07:54:59 +03:00
|
|
|
|
2025-01-16 07:37:05 -05:00
|
|
|
/// This terrible hack is a way to avoid the incredibly disruptive resizing of grids that
|
|
|
|
/// happens on Macs when moving a window between monitors of different DPIs.
|
2022-03-21 16:44:28 -07:00
|
|
|
#ifndef __WXMAC__
|
|
|
|
aEvt.Skip();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:50:47 +00:00
|
|
|
|
2019-02-07 21:48:41 +00:00
|
|
|
void WX_GRID::SetColLabelSize( int aHeight )
|
|
|
|
{
|
2024-05-26 07:54:59 +03:00
|
|
|
if( aHeight == 0 || aHeight == wxGRID_AUTOSIZE )
|
2020-09-04 18:42:04 +01:00
|
|
|
{
|
2024-05-26 07:54:59 +03:00
|
|
|
wxGrid::SetColLabelSize( aHeight );
|
2020-09-04 18:42:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-10 18:35:45 +01:00
|
|
|
// Correct wxFormBuilder height for large fonts
|
2024-05-26 07:54:59 +03:00
|
|
|
int minHeight = GetCharHeight() + 2 * MIN_GRIDCELL_MARGIN;
|
|
|
|
|
2019-02-07 21:48:41 +00:00
|
|
|
wxGrid::SetColLabelSize( std::max( aHeight, minHeight ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-18 11:50:47 +00:00
|
|
|
void WX_GRID::SetLabelFont( const wxFont& aFont )
|
|
|
|
{
|
|
|
|
wxGrid::SetLabelFont( KIUI::GetControlFont( this ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-19 17:10:14 +01:00
|
|
|
void WX_GRID::SetTable( wxGridTableBase* aTable, bool aTakeOwnership )
|
2018-05-30 11:52:19 +01:00
|
|
|
{
|
|
|
|
// wxGrid::SetTable() messes up the column widths from wxFormBuilder so we have to save
|
|
|
|
// and restore them.
|
2018-09-12 10:53:16 +02:00
|
|
|
int numberCols = GetNumberCols();
|
|
|
|
int* formBuilderColWidths = new int[numberCols];
|
2018-05-30 11:52:19 +01:00
|
|
|
|
2018-09-12 10:53:16 +02:00
|
|
|
for( int i = 0; i < numberCols; ++i )
|
2018-05-30 11:52:19 +01:00
|
|
|
formBuilderColWidths[ i ] = GetColSize( i );
|
|
|
|
|
|
|
|
wxGrid::SetTable( aTable );
|
2020-12-17 08:12:18 -05:00
|
|
|
|
2019-02-07 21:48:41 +00:00
|
|
|
// wxGrid::SetTable() may change the number of columns, so prevent out-of-bounds access
|
|
|
|
// to formBuilderColWidths
|
2018-09-12 10:53:16 +02:00
|
|
|
numberCols = std::min( numberCols, GetNumberCols() );
|
2018-05-30 11:52:19 +01:00
|
|
|
|
2018-09-12 10:53:16 +02:00
|
|
|
for( int i = 0; i < numberCols; ++i )
|
2018-08-23 11:59:37 +01:00
|
|
|
{
|
|
|
|
// correct wxFormBuilder width for large fonts and/or long translations
|
|
|
|
int headingWidth = GetTextExtent( GetColLabelValue( i ) ).x + 2 * MIN_GRIDCELL_MARGIN;
|
|
|
|
|
|
|
|
SetColSize( i, std::max( formBuilderColWidths[ i ], headingWidth ) );
|
|
|
|
}
|
2018-07-17 07:53:24 +01:00
|
|
|
|
|
|
|
delete[] formBuilderColWidths;
|
2018-08-19 17:10:14 +01:00
|
|
|
|
2024-02-13 18:59:13 +00:00
|
|
|
EnableAlternateRowColors( Pgm().GetCommonSettings()->m_Appearance.grid_striping );
|
|
|
|
|
2021-07-15 15:26:35 -04:00
|
|
|
Connect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
|
2025-06-19 14:11:04 +01:00
|
|
|
Connect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
|
2018-10-09 21:27:15 +01:00
|
|
|
|
2018-08-19 17:10:14 +01:00
|
|
|
m_weOwnTable = aTakeOwnership;
|
2018-05-30 11:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-13 18:59:13 +00:00
|
|
|
void WX_GRID::EnableAlternateRowColors( bool aEnable )
|
|
|
|
{
|
|
|
|
wxGridTableBase* table = wxGrid::GetTable();
|
|
|
|
|
|
|
|
wxCHECK_MSG( table, /* void */,
|
|
|
|
"Tried to enable alternate row colors without a table assigned to the grid" );
|
|
|
|
|
|
|
|
if( aEnable )
|
|
|
|
{
|
|
|
|
wxColor color = wxGrid::GetDefaultCellBackgroundColour();
|
|
|
|
table->SetAttrProvider( new WX_GRID_ALT_ROW_COLOR_PROVIDER( color ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
table->SetAttrProvider( nullptr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-04 12:36:27 +01:00
|
|
|
void WX_GRID::onGridCellSelect( wxGridEvent& aEvent )
|
|
|
|
{
|
|
|
|
// Highlight the selected cell.
|
2022-10-11 18:01:47 +01:00
|
|
|
// Calling SelectBlock() allows a visual effect when cells are selected by tab or arrow keys.
|
|
|
|
// Otherwise, one cannot really know what actual cell is selected.
|
2022-02-04 12:36:27 +01:00
|
|
|
int row = aEvent.GetRow();
|
|
|
|
int col = aEvent.GetCol();
|
|
|
|
|
2024-01-24 16:39:52 +00:00
|
|
|
if( row >= 0 && row < GetNumberRows() && col >= 0 && col < GetNumberCols() )
|
|
|
|
{
|
|
|
|
if( GetSelectionMode() == wxGrid::wxGridSelectCells )
|
|
|
|
{
|
|
|
|
SelectBlock( row, col, row, col, false );
|
|
|
|
}
|
|
|
|
else if( GetSelectionMode() == wxGrid::wxGridSelectRows
|
|
|
|
|| GetSelectionMode() == wxGrid::wxGridSelectRowsOrColumns )
|
|
|
|
{
|
|
|
|
SelectBlock( row, 0, row, GetNumberCols() - 1, false );
|
|
|
|
}
|
|
|
|
else if( GetSelectionMode() == wxGrid::wxGridSelectColumns )
|
|
|
|
{
|
|
|
|
SelectBlock( 0, col, GetNumberRows() - 1, col, false );
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 18:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WX_GRID::onCellEditorShown( wxGridEvent& aEvent )
|
|
|
|
{
|
|
|
|
if( alg::contains( m_autoEvalCols, aEvent.GetCol() ) )
|
|
|
|
{
|
|
|
|
int row = aEvent.GetRow();
|
|
|
|
int col = aEvent.GetCol();
|
|
|
|
|
|
|
|
const std::pair<wxString, wxString>& beforeAfter = m_evalBeforeAfter[ { row, col } ];
|
|
|
|
|
|
|
|
if( GetCellValue( row, col ) == beforeAfter.second )
|
|
|
|
SetCellValue( row, col, beforeAfter.first );
|
|
|
|
}
|
2022-02-04 12:36:27 +01:00
|
|
|
}
|
|
|
|
|
2022-10-11 18:01:47 +01:00
|
|
|
|
|
|
|
void WX_GRID::onCellEditorHidden( wxGridEvent& aEvent )
|
|
|
|
{
|
2025-02-10 20:57:51 +00:00
|
|
|
const int col = aEvent.GetCol();
|
|
|
|
|
|
|
|
if( alg::contains( m_autoEvalCols, col ) )
|
2022-10-11 18:01:47 +01:00
|
|
|
{
|
2025-04-18 18:41:43 +01:00
|
|
|
UNITS_PROVIDER* unitsProvider = getUnitsProvider( col );
|
2022-11-04 12:20:53 +00:00
|
|
|
|
2025-04-27 16:37:19 -07:00
|
|
|
auto cellUnitsData = getColumnUnits( col );
|
|
|
|
EDA_UNITS cellUnits = cellUnitsData.first;
|
|
|
|
EDA_DATA_TYPE cellDataType = cellUnitsData.second;
|
2022-10-11 18:01:47 +01:00
|
|
|
|
2025-02-10 20:57:51 +00:00
|
|
|
m_eval->SetDefaultUnits( cellUnits );
|
|
|
|
|
|
|
|
const int row = aEvent.GetRow();
|
2022-10-11 18:01:47 +01:00
|
|
|
|
2024-07-26 20:49:29 +00:00
|
|
|
// Determine if this cell is marked as holding nullable values
|
|
|
|
bool isNullable = false;
|
|
|
|
wxGridCellEditor* cellEditor = GetCellEditor( row, col );
|
|
|
|
|
|
|
|
if( cellEditor )
|
|
|
|
{
|
2025-06-19 14:11:04 +01:00
|
|
|
if( GRID_CELL_MARK_AS_NULLABLE* nullable = dynamic_cast<GRID_CELL_MARK_AS_NULLABLE*>( cellEditor ) )
|
|
|
|
isNullable = nullable->IsNullable();
|
2024-07-26 20:49:29 +00:00
|
|
|
|
|
|
|
cellEditor->DecRef();
|
|
|
|
}
|
|
|
|
|
2022-10-11 18:01:47 +01:00
|
|
|
CallAfter(
|
2025-02-10 20:57:51 +00:00
|
|
|
[this, row, col, isNullable, unitsProvider, cellDataType]()
|
2024-07-26 20:49:29 +00:00
|
|
|
{
|
2025-07-12 23:08:25 +01:00
|
|
|
// Careful; if called from CommitPendingChange() in a delete operation, the cell may
|
|
|
|
// no longer exist.
|
|
|
|
if( row >= GetNumberRows() || col >= GetNumberCols() )
|
|
|
|
return;
|
|
|
|
|
2024-07-26 20:49:29 +00:00
|
|
|
wxString stringValue = GetCellValue( row, col );
|
|
|
|
bool processedOk = true;
|
|
|
|
|
|
|
|
if( stringValue != UNITS_PROVIDER::NullUiString )
|
|
|
|
processedOk = m_eval->Process( stringValue );
|
|
|
|
|
|
|
|
if( processedOk )
|
|
|
|
{
|
|
|
|
wxString evalValue;
|
|
|
|
|
|
|
|
if( isNullable )
|
|
|
|
{
|
|
|
|
std::optional<int> val;
|
|
|
|
|
|
|
|
if( stringValue == UNITS_PROVIDER::NullUiString )
|
|
|
|
{
|
2025-06-19 14:11:04 +01:00
|
|
|
val = unitsProvider->OptionalValueFromString( UNITS_PROVIDER::NullUiString,
|
|
|
|
cellDataType );
|
2024-07-26 20:49:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2025-02-10 20:57:51 +00:00
|
|
|
val = unitsProvider->OptionalValueFromString( m_eval->Result(),
|
|
|
|
cellDataType );
|
2024-07-26 20:49:29 +00:00
|
|
|
}
|
|
|
|
|
2025-06-19 14:11:04 +01:00
|
|
|
evalValue = unitsProvider->StringFromOptionalValue( val, true, cellDataType );
|
2024-07-26 20:49:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2025-06-19 14:11:04 +01:00
|
|
|
int val = unitsProvider->ValueFromString( m_eval->Result(), cellDataType );
|
2025-02-10 20:57:51 +00:00
|
|
|
evalValue = unitsProvider->StringFromValue( val, true, cellDataType );
|
2024-07-26 20:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( stringValue != evalValue )
|
|
|
|
{
|
|
|
|
SetCellValue( row, col, evalValue );
|
|
|
|
m_evalBeforeAfter[{ row, col }] = { stringValue, evalValue };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
2022-10-11 18:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
aEvent.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-30 11:52:19 +01:00
|
|
|
void WX_GRID::DestroyTable( wxGridTableBase* aTable )
|
|
|
|
{
|
|
|
|
// wxGrid's destructor will crash trying to look up the cell attr if the edit control
|
|
|
|
// is left open. Normally it's closed in Validate(), but not if the user hit Cancel.
|
2018-08-19 17:10:14 +01:00
|
|
|
CommitPendingChanges( true /* quiet mode */ );
|
2018-05-30 11:52:19 +01:00
|
|
|
|
2021-07-15 15:26:35 -04:00
|
|
|
Disconnect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this );
|
2025-06-19 14:11:04 +01:00
|
|
|
Disconnect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this );
|
2018-10-09 21:27:15 +01:00
|
|
|
|
2018-05-30 11:52:19 +01:00
|
|
|
wxGrid::SetTable( nullptr );
|
|
|
|
delete aTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-13 13:45:22 +01:00
|
|
|
wxString WX_GRID::GetShownColumnsAsString()
|
2018-05-30 11:52:19 +01:00
|
|
|
{
|
|
|
|
wxString shownColumns;
|
|
|
|
|
|
|
|
for( int i = 0; i < GetNumberCols(); ++i )
|
|
|
|
{
|
|
|
|
if( IsColShown( i ) )
|
|
|
|
{
|
|
|
|
if( shownColumns.Length() )
|
|
|
|
shownColumns << wxT( " " );
|
2020-12-17 08:12:18 -05:00
|
|
|
|
2018-05-30 11:52:19 +01:00
|
|
|
shownColumns << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return shownColumns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-13 13:45:22 +01:00
|
|
|
std::bitset<64> WX_GRID::GetShownColumns()
|
|
|
|
{
|
|
|
|
std::bitset<64> shownColumns;
|
|
|
|
|
|
|
|
for( int ii = 0; ii < GetNumberCols(); ++ii )
|
|
|
|
shownColumns[ii] = IsColShown( ii );
|
|
|
|
|
|
|
|
return shownColumns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-30 11:52:19 +01:00
|
|
|
void WX_GRID::ShowHideColumns( const wxString& shownColumns )
|
|
|
|
{
|
|
|
|
for( int i = 0; i < GetNumberCols(); ++i )
|
|
|
|
HideCol( i );
|
|
|
|
|
|
|
|
wxStringTokenizer shownTokens( shownColumns );
|
|
|
|
|
|
|
|
while( shownTokens.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
long colNumber;
|
|
|
|
shownTokens.GetNextToken().ToLong( &colNumber );
|
|
|
|
|
|
|
|
if( colNumber >= 0 && colNumber < GetNumberCols() )
|
2023-01-16 02:46:09 +00:00
|
|
|
ShowCol( (int) colNumber );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-13 13:45:22 +01:00
|
|
|
void WX_GRID::ShowHideColumns( const std::bitset<64>& aShownColumns )
|
|
|
|
{
|
|
|
|
for( int ii = 0; ii < GetNumberCols(); ++ ii )
|
|
|
|
{
|
|
|
|
if( aShownColumns[ii] )
|
|
|
|
ShowCol( ii );
|
|
|
|
else
|
|
|
|
HideCol( ii );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-16 02:46:09 +00:00
|
|
|
void WX_GRID::DrawCornerLabel( wxDC& dc )
|
|
|
|
{
|
|
|
|
if( m_nativeColumnLabels )
|
|
|
|
wxGrid::DrawCornerLabel( dc );
|
|
|
|
|
|
|
|
wxRect rect( wxSize( m_rowLabelWidth, m_colLabelHeight ) );
|
|
|
|
|
|
|
|
static WX_GRID_CORNER_HEADER_RENDERER rend;
|
|
|
|
|
|
|
|
// It is reported that we need to erase the background to avoid display
|
|
|
|
// artifacts, see #12055.
|
|
|
|
{
|
|
|
|
wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
|
|
|
|
wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
|
|
|
|
dc.DrawRectangle( rect.Inflate( 1 ) );
|
2018-05-30 11:52:19 +01:00
|
|
|
}
|
2023-01-16 02:46:09 +00:00
|
|
|
|
|
|
|
rend.DrawBorder( *this, dc, rect );
|
2018-05-30 11:52:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WX_GRID::DrawColLabel( wxDC& dc, int col )
|
|
|
|
{
|
2023-01-16 02:46:09 +00:00
|
|
|
if( m_nativeColumnLabels )
|
|
|
|
wxGrid::DrawColLabel( dc, col );
|
|
|
|
|
2018-05-30 11:52:19 +01:00
|
|
|
if( GetColWidth( col ) <= 0 || m_colLabelHeight <= 0 )
|
|
|
|
return;
|
|
|
|
|
2023-01-16 02:46:09 +00:00
|
|
|
wxRect rect( GetColLeft( col ), 0, GetColWidth( col ), m_colLabelHeight );
|
2018-05-30 11:52:19 +01:00
|
|
|
|
2023-01-16 02:46:09 +00:00
|
|
|
static WX_GRID_COLUMN_HEADER_RENDERER rend;
|
2018-05-30 11:52:19 +01:00
|
|
|
|
|
|
|
// It is reported that we need to erase the background to avoid display
|
2021-07-15 15:26:35 -04:00
|
|
|
// artifacts, see #12055.
|
2023-01-16 02:46:09 +00:00
|
|
|
{
|
|
|
|
wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
|
|
|
|
wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
|
|
|
|
dc.DrawRectangle( rect.Inflate( 1 ) );
|
|
|
|
}
|
2018-05-30 11:52:19 +01:00
|
|
|
|
|
|
|
rend.DrawBorder( *this, dc, rect );
|
|
|
|
|
2021-09-12 12:17:22 +01:00
|
|
|
// Make sure fonts get scaled correctly on GTK HiDPI monitors
|
|
|
|
dc.SetFont( GetLabelFont() );
|
|
|
|
|
2018-05-30 11:52:19 +01:00
|
|
|
int hAlign, vAlign;
|
|
|
|
GetColLabelAlignment( &hAlign, &vAlign );
|
|
|
|
const int orient = GetColLabelTextOrientation();
|
|
|
|
|
2023-01-16 02:46:09 +00:00
|
|
|
if( col == 0 )
|
2018-05-30 11:52:19 +01:00
|
|
|
hAlign = wxALIGN_LEFT;
|
|
|
|
|
2023-01-16 02:46:09 +00:00
|
|
|
if( hAlign == wxALIGN_LEFT )
|
|
|
|
rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
|
|
|
|
|
2018-05-30 11:52:19 +01:00
|
|
|
rend.DrawLabel( *this, dc, GetColLabelValue( col ), rect, hAlign, vAlign, orient );
|
|
|
|
}
|
2018-08-13 18:00:08 +01:00
|
|
|
|
|
|
|
|
2023-01-16 02:46:09 +00:00
|
|
|
void WX_GRID::DrawRowLabel( wxDC& dc, int row )
|
|
|
|
{
|
2025-01-16 07:37:05 -05:00
|
|
|
if( GetRowHeight( row ) <= 0 || m_rowLabelWidth <= 0 )
|
2023-01-16 02:46:09 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
wxRect rect( 0, GetRowTop( row ), m_rowLabelWidth, GetRowHeight( row ) );
|
|
|
|
|
|
|
|
static WX_GRID_ROW_HEADER_RENDERER rend;
|
|
|
|
|
|
|
|
// It is reported that we need to erase the background to avoid display
|
|
|
|
// artifacts, see #12055.
|
|
|
|
{
|
|
|
|
wxDCBrushChanger setBrush( dc, m_colLabelWin->GetBackgroundColour() );
|
|
|
|
wxDCPenChanger setPen( dc, m_colLabelWin->GetBackgroundColour() );
|
|
|
|
dc.DrawRectangle( rect.Inflate( 1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
rend.DrawBorder( *this, dc, rect );
|
|
|
|
|
|
|
|
// Make sure fonts get scaled correctly on GTK HiDPI monitors
|
|
|
|
dc.SetFont( GetLabelFont() );
|
|
|
|
|
|
|
|
int hAlign, vAlign;
|
|
|
|
GetRowLabelAlignment(&hAlign, &vAlign);
|
|
|
|
|
|
|
|
if( hAlign == wxALIGN_LEFT )
|
|
|
|
rect.SetLeft( rect.GetLeft() + MIN_GRIDCELL_MARGIN );
|
|
|
|
|
|
|
|
rend.DrawLabel( *this, dc, GetRowLabelValue( row ), rect, hAlign, vAlign, wxHORIZONTAL );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-10 15:12:35 +01:00
|
|
|
bool WX_GRID::CancelPendingChanges()
|
|
|
|
{
|
|
|
|
if( !IsCellEditControlEnabled() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
HideCellEditControl();
|
|
|
|
|
|
|
|
// do it after HideCellEditControl()
|
|
|
|
m_cellEditCtrlEnabled = false;
|
|
|
|
|
|
|
|
int row = m_currentCellCoords.GetRow();
|
|
|
|
int col = m_currentCellCoords.GetCol();
|
|
|
|
|
|
|
|
wxString oldval = GetCellValue( row, col );
|
|
|
|
wxString newval;
|
|
|
|
|
|
|
|
wxGridCellAttr* attr = GetCellAttr( row, col );
|
|
|
|
wxGridCellEditor* editor = attr->GetEditor( this, row, col );
|
|
|
|
|
2023-04-11 01:53:35 -07:00
|
|
|
editor->EndEdit( row, col, this, oldval, &newval );
|
2023-04-10 15:12:35 +01:00
|
|
|
|
|
|
|
editor->DecRef();
|
|
|
|
attr->DecRef();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-13 18:00:08 +01:00
|
|
|
bool WX_GRID::CommitPendingChanges( bool aQuietMode )
|
|
|
|
{
|
|
|
|
if( !IsCellEditControlEnabled() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( !aQuietMode && SendEvent( wxEVT_GRID_EDITOR_HIDDEN ) == -1 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
HideCellEditControl();
|
|
|
|
|
|
|
|
// do it after HideCellEditControl()
|
|
|
|
m_cellEditCtrlEnabled = false;
|
|
|
|
|
|
|
|
int row = m_currentCellCoords.GetRow();
|
|
|
|
int col = m_currentCellCoords.GetCol();
|
|
|
|
|
|
|
|
wxString oldval = GetCellValue( row, col );
|
|
|
|
wxString newval;
|
|
|
|
|
|
|
|
wxGridCellAttr* attr = GetCellAttr( row, col );
|
|
|
|
wxGridCellEditor* editor = attr->GetEditor( this, row, col );
|
|
|
|
|
|
|
|
bool changed = editor->EndEdit( row, col, this, oldval, &newval );
|
|
|
|
|
|
|
|
editor->DecRef();
|
|
|
|
attr->DecRef();
|
|
|
|
|
|
|
|
if( changed )
|
|
|
|
{
|
2021-07-15 15:26:35 -04:00
|
|
|
if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGING, newval ) == -1 )
|
2018-08-13 18:00:08 +01:00
|
|
|
return false;
|
|
|
|
|
2021-07-15 15:26:35 -04:00
|
|
|
editor->ApplyEdit( row, col, this );
|
2018-08-13 18:00:08 +01:00
|
|
|
|
|
|
|
// for compatibility reasons dating back to wx 2.8 when this event
|
|
|
|
// was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
|
|
|
|
// didn't exist we allow vetoing this one too
|
|
|
|
if( !aQuietMode && SendEvent( wxEVT_GRID_CELL_CHANGED, oldval ) == -1 )
|
|
|
|
{
|
|
|
|
// Event has been vetoed, set the data back.
|
|
|
|
SetCellValue( row, col, oldval );
|
|
|
|
return false;
|
|
|
|
}
|
2024-08-08 10:24:17 +01:00
|
|
|
|
|
|
|
if( DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
|
|
|
|
dlg->OnModify();
|
2018-08-13 18:00:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-09-12 10:53:16 +02:00
|
|
|
}
|
2018-10-09 21:27:15 +01:00
|
|
|
|
|
|
|
|
2025-07-24 17:55:00 +01:00
|
|
|
void WX_GRID::OnDeleteRows( const std::function<void( int row )>& aDeleter )
|
|
|
|
{
|
|
|
|
OnDeleteRows(
|
|
|
|
[]( int row )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
aDeleter );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WX_GRID::OnDeleteRows( const std::function<bool( int row )>& aFilter,
|
|
|
|
const std::function<void( int row )>& aDeleter )
|
|
|
|
{
|
|
|
|
wxArrayInt selectedRows = GetSelectedRows();
|
|
|
|
|
|
|
|
if( selectedRows.empty() && GetGridCursorRow() >= 0 )
|
|
|
|
selectedRows.push_back( GetGridCursorRow() );
|
|
|
|
|
|
|
|
if( selectedRows.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for( int row : selectedRows )
|
|
|
|
{
|
|
|
|
if( !aFilter( row ) )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !CommitPendingChanges() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Reverse sort so deleting a row doesn't change the indexes of the other rows.
|
|
|
|
selectedRows.Sort(
|
|
|
|
[]( int* first, int* second )
|
|
|
|
{
|
|
|
|
return *second - *first;
|
|
|
|
} );
|
|
|
|
|
|
|
|
int nextSelRow = selectedRows.back() - 1;
|
|
|
|
|
|
|
|
if( nextSelRow >= 0 )
|
|
|
|
{
|
|
|
|
GoToCell( nextSelRow, GetGridCursorCol() );
|
|
|
|
SetGridCursor( nextSelRow, GetGridCursorCol() );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( int row : selectedRows )
|
|
|
|
aDeleter( row );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-04 12:20:53 +00:00
|
|
|
void WX_GRID::SetUnitsProvider( UNITS_PROVIDER* aProvider, int aCol )
|
2022-10-11 18:01:47 +01:00
|
|
|
{
|
2022-11-04 12:20:53 +00:00
|
|
|
m_unitsProviders[ aCol ] = aProvider;
|
|
|
|
|
|
|
|
if( !m_eval )
|
|
|
|
m_eval = std::make_unique<NUMERIC_EVALUATOR>( aProvider->GetUserUnits() );
|
2022-10-11 18:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-10 20:57:51 +00:00
|
|
|
void WX_GRID::SetAutoEvalColUnits( const int col, EDA_UNITS aUnit, EDA_DATA_TYPE aUnitType )
|
|
|
|
{
|
|
|
|
m_autoEvalColsUnits[col] = std::make_pair( aUnit, aUnitType );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WX_GRID::SetAutoEvalColUnits( const int col, EDA_UNITS aUnit )
|
|
|
|
{
|
|
|
|
const EDA_DATA_TYPE type = UNITS_PROVIDER::GetTypeFromUnits( aUnit );
|
|
|
|
SetAutoEvalColUnits( col, aUnit, type );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-11 18:01:47 +01:00
|
|
|
int WX_GRID::GetUnitValue( int aRow, int aCol )
|
|
|
|
{
|
|
|
|
wxString stringValue = GetCellValue( aRow, aCol );
|
|
|
|
|
2025-02-10 20:57:51 +00:00
|
|
|
auto [cellUnits, cellDataType] = getColumnUnits( aCol );
|
|
|
|
|
2022-10-11 18:01:47 +01:00
|
|
|
if( alg::contains( m_autoEvalCols, aCol ) )
|
|
|
|
{
|
2025-02-10 20:57:51 +00:00
|
|
|
m_eval->SetDefaultUnits( cellUnits );
|
2022-10-11 18:01:47 +01:00
|
|
|
|
|
|
|
if( m_eval->Process( stringValue ) )
|
|
|
|
stringValue = m_eval->Result();
|
|
|
|
}
|
|
|
|
|
2025-04-18 18:41:43 +01:00
|
|
|
return getUnitsProvider( aCol )->ValueFromString( stringValue, cellDataType );
|
2022-10-11 18:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-26 20:49:29 +00:00
|
|
|
std::optional<int> WX_GRID::GetOptionalUnitValue( int aRow, int aCol )
|
|
|
|
{
|
|
|
|
wxString stringValue = GetCellValue( aRow, aCol );
|
|
|
|
|
2025-02-10 20:57:51 +00:00
|
|
|
auto [cellUnits, cellDataType] = getColumnUnits( aCol );
|
|
|
|
|
2024-07-26 20:49:29 +00:00
|
|
|
if( alg::contains( m_autoEvalCols, aCol ) )
|
|
|
|
{
|
2025-02-10 20:57:51 +00:00
|
|
|
m_eval->SetDefaultUnits( cellUnits );
|
2024-07-26 20:49:29 +00:00
|
|
|
|
|
|
|
if( stringValue != UNITS_PROVIDER::NullUiString && m_eval->Process( stringValue ) )
|
|
|
|
stringValue = m_eval->Result();
|
|
|
|
}
|
|
|
|
|
2025-04-18 18:41:43 +01:00
|
|
|
return getUnitsProvider( aCol )->OptionalValueFromString( stringValue, cellDataType );
|
2024-07-26 20:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-11 18:01:47 +01:00
|
|
|
void WX_GRID::SetUnitValue( int aRow, int aCol, int aValue )
|
|
|
|
{
|
2025-02-10 20:57:51 +00:00
|
|
|
EDA_DATA_TYPE cellDataType;
|
|
|
|
|
|
|
|
if( m_autoEvalColsUnits.contains( aCol ) )
|
|
|
|
cellDataType = m_autoEvalColsUnits[aCol].second;
|
|
|
|
else
|
|
|
|
cellDataType = EDA_DATA_TYPE::DISTANCE;
|
|
|
|
|
2025-04-18 18:41:43 +01:00
|
|
|
SetCellValue( aRow, aCol, getUnitsProvider( aCol )->StringFromValue( aValue, true, cellDataType ) );
|
2022-10-11 18:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-26 20:49:29 +00:00
|
|
|
void WX_GRID::SetOptionalUnitValue( int aRow, int aCol, std::optional<int> aValue )
|
|
|
|
{
|
2025-04-18 18:41:43 +01:00
|
|
|
SetCellValue( aRow, aCol, getUnitsProvider( aCol )->StringFromOptionalValue( aValue, true ) );
|
2024-07-26 20:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-09 21:27:15 +01:00
|
|
|
void WX_GRID::onGridColMove( wxGridEvent& aEvent )
|
|
|
|
{
|
|
|
|
// wxWidgets won't move an open editor, so better just to close it
|
|
|
|
CommitPendingChanges( true );
|
|
|
|
}
|
2019-02-09 15:31:31 -08:00
|
|
|
|
|
|
|
|
|
|
|
int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep )
|
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
if( aCol < 0 )
|
|
|
|
{
|
|
|
|
if( aKeep )
|
|
|
|
size = GetRowLabelSize();
|
|
|
|
|
2019-02-10 07:59:27 +01:00
|
|
|
for( int row = 0; aContents && row < GetNumberRows(); row++ )
|
2023-01-16 23:14:38 -05:00
|
|
|
size = std::max( size, int( GetTextExtent( GetRowLabelValue( row ) + wxS( "M" ) ).x ) );
|
2019-02-09 15:31:31 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( aKeep )
|
|
|
|
size = GetColSize( aCol );
|
|
|
|
|
2020-12-17 08:12:18 -05:00
|
|
|
// 'M' is generally the widest character, so we buffer the column width by default to
|
|
|
|
// ensure we don't write a continuous line of text at the column header
|
2019-02-09 15:31:31 -08:00
|
|
|
if( aHeader )
|
2019-02-10 09:49:42 +01:00
|
|
|
{
|
2019-02-18 20:34:44 +01:00
|
|
|
EnsureColLabelsVisible();
|
2020-12-17 08:12:18 -05:00
|
|
|
|
2025-06-19 14:11:04 +01:00
|
|
|
size = std::max( size, int( GetTextExtent( GetColLabelValue( aCol ) + wxS( "M" ) ).x ) );
|
2019-02-10 09:49:42 +01:00
|
|
|
}
|
2019-02-09 15:31:31 -08:00
|
|
|
|
2019-02-10 07:59:27 +01:00
|
|
|
for( int row = 0; aContents && row < GetNumberRows(); row++ )
|
2019-02-14 22:02:34 -08:00
|
|
|
{
|
|
|
|
// If we have text, get the size. Otherwise, use a placeholder for the checkbox
|
|
|
|
if( GetTable()->CanGetValueAs( row, aCol, wxGRID_VALUE_STRING ) )
|
2023-01-16 23:14:38 -05:00
|
|
|
size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + wxS( "M" ) ).x );
|
2019-02-14 22:02:34 -08:00
|
|
|
else
|
|
|
|
size = std::max( size, GetTextExtent( "MM" ).x );
|
|
|
|
}
|
2019-02-09 15:31:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2019-02-18 20:34:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
void WX_GRID::EnsureColLabelsVisible()
|
|
|
|
{
|
2023-01-16 02:46:09 +00:00
|
|
|
int line_height = int( GetTextExtent( "Mj" ).y ) + 3;
|
2020-03-23 17:00:49 +00:00
|
|
|
int row_height = GetColLabelSize();
|
2021-09-14 20:08:48 +02:00
|
|
|
int initial_row_height = row_height;
|
2020-03-23 17:00:49 +00:00
|
|
|
|
2019-02-18 20:34:44 +01:00
|
|
|
// Headers can be multiline. Fix the Column Label Height to show the full header
|
|
|
|
// However GetTextExtent does not work on multiline strings,
|
|
|
|
// and do not return the full text height (only the height of one line)
|
|
|
|
for( int col = 0; col < GetNumberCols(); col++ )
|
|
|
|
{
|
2020-03-23 17:00:49 +00:00
|
|
|
int nl_count = GetColLabelValue( col ).Freq( '\n' );
|
2019-02-18 20:34:44 +01:00
|
|
|
|
|
|
|
if( nl_count )
|
|
|
|
{
|
|
|
|
// Col Label height must be able to show nl_count+1 lines
|
2020-03-23 17:00:49 +00:00
|
|
|
if( row_height < line_height * ( nl_count+1 ) )
|
|
|
|
row_height += line_height * nl_count;
|
2019-02-18 20:34:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 20:08:48 +02:00
|
|
|
// Update the column label size, but only if needed, to avoid generating useless
|
|
|
|
// and perhaps annoying UI events when the size does not change
|
|
|
|
if( initial_row_height != row_height )
|
|
|
|
SetColLabelSize( row_height );
|
2019-02-18 20:34:44 +01:00
|
|
|
}
|
2025-02-10 20:57:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
std::pair<EDA_UNITS, EDA_DATA_TYPE> WX_GRID::getColumnUnits( const int aCol ) const
|
|
|
|
{
|
|
|
|
if( m_autoEvalColsUnits.contains( aCol ) )
|
|
|
|
return { m_autoEvalColsUnits.at( aCol ).first, m_autoEvalColsUnits.at( aCol ).second };
|
|
|
|
|
|
|
|
// Legacy - default always DISTANCE
|
2025-04-18 18:41:43 +01:00
|
|
|
return { getUnitsProvider( aCol )->GetUserUnits(), EDA_DATA_TYPE::DISTANCE };
|
2025-02-10 20:57:51 +00:00
|
|
|
}
|