wx_grid: abstract visible column width

Cleanup code for checking visible extents of grid text.  Places single
routine to extract the current spacing from the grid in WX_GRID.
This commit is contained in:
Seth Hillbrand 2019-02-09 15:31:31 -08:00
parent 0d2e39e781
commit 8ff764376a
8 changed files with 65 additions and 40 deletions

View File

@ -207,3 +207,33 @@ void WX_GRID::onGridColMove( wxGridEvent& aEvent )
// wxWidgets won't move an open editor, so better just to close it
CommitPendingChanges( true );
}
int WX_GRID::GetVisibleWidth( int aCol, bool aHeader, bool aContents, bool aKeep )
{
int size = 0;
if( aCol < 0 )
{
if( aKeep )
size = GetRowLabelSize();
for( int row = 0; aContents && row < GetRows(); row++ )
size = std::max( size, GetTextExtent( GetRowLabelValue( row ) + "M" ).x );
}
else
{
if( aKeep )
size = GetColSize( aCol );
// '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
if( aHeader )
size = std::max( size, GetTextExtent( GetColLabelValue( aCol ) + "M" ).x );
for( int row = 0; aContents && row < GetRows(); row++ )
size = std::max( size, GetTextExtent( GetCellValue( row, aCol ) + "M" ).x );
}
return size;
}

View File

@ -67,6 +67,17 @@ public:
*/
bool CommitPendingChanges( bool aQuietMode = false );
/**
* Calculates the specified column based on the actual size of the text
* on screen. Will return the maximum value of all calculated widths.
* @param aCol - Integer value of the column to resize. Specify -1 for the row labels.
* @param aHeader - Include the header in the width calculation
* @param aContents - Include the full contents of the
* @param aKeep - Use the current size as a minimum value
* @return The new size of the column
*/
int GetVisibleWidth( int aCol, bool aHeader = true, bool aContents = false, bool aKeep = true );
protected:
void DrawColLabel( wxDC& dc, int col ) override;

View File

@ -374,22 +374,10 @@ bool DIALOG_FOOTPRINT_BOARD_EDITOR::TransferDataToWindow()
// Show the footprint's ID.
m_staticLibraryID->SetLabel( m_footprint->GetFPID().Format() );
// Work around an issue where wxWidgets doesn't calculate the row width on its own
// TODO: Refactor this into a GRID_TRICKS routine or similar
for( int col = 0; col < m_itemsGrid->GetNumberCols(); col++ )
{
// '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
auto size = m_itemsGrid->GetTextExtent( m_itemsGrid->GetColLabelValue( col ) + "M").x;
m_itemsGrid->SetColSize( col, std::max( m_itemsGrid->GetColSize( col ), size ) );
}
m_itemsGrid->SetColSize( col, m_itemsGrid->GetVisibleWidth( col, true, false, false ) );
int size = m_itemsGrid->GetRowLabelSize();
for( int row = 0; row < m_itemsGrid->GetNumberRows(); row++ )
size = std::max( size, m_itemsGrid->GetTextExtent( m_itemsGrid->GetRowLabelValue( row ) + "M" ).x );
m_itemsGrid->SetRowLabelSize( size );
m_itemsGrid->SetRowLabelSize( m_itemsGrid->GetVisibleWidth( -1, false, true, true ) );
Layout();
adjustGridColumns( m_itemsGrid->GetRect().GetWidth());

View File

@ -293,6 +293,11 @@ bool DIALOG_FOOTPRINT_FP_EDITOR::TransferDataToWindow()
select3DModel( 0 ); // will clamp idx within bounds
for( int col = 0; col < m_itemsGrid->GetCols(); col++ )
m_itemsGrid->SetColSize( col, m_itemsGrid->GetVisibleWidth( col, true, false, true ) );
m_itemsGrid->SetRowLabelSize( m_itemsGrid->GetVisibleWidth( -1, true, true, true ) );
Layout();
adjustGridColumns( m_itemsGrid->GetRect().GetWidth());

View File

@ -126,6 +126,11 @@ bool PANEL_MODEDIT_DEFAULTS::TransferDataToWindow()
m_choiceLayerValue->SetSelection( m_brdSettings.m_ValueDefaultlayer == F_SilkS ? 0 : 1 );
m_choiceVisibleValue->SetSelection( m_brdSettings.m_ValueDefaultVisibility ? 0 : 1 );
for( int col = 0; col < m_grid->GetCols(); col++ )
m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true, false ) );
m_grid->SetRowLabelSize( m_grid->GetVisibleWidth( -1, true, true, false ) );
return true;
}

View File

@ -191,7 +191,9 @@ bool PANEL_PCBNEW_ACTION_PLUGINS::TransferDataToWindow()
m_grid->SetCellValue( row, COLUMN_PATH, ap->GetPluginPath() );
}
m_grid->AutoSizeColumns();
for( int col = 0; col < m_grid->GetCols(); col++ )
m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true, false ) );
m_grid->AutoSizeRows();
m_grid->Thaw();

View File

@ -67,11 +67,7 @@ PANEL_SETUP_NETCLASSES::PANEL_SETUP_NETCLASSES( PAGED_DIALOG* aParent, PCB_EDIT_
for( int i = 0; i < m_netclassGrid->GetNumberCols(); ++i )
{
// '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
auto size = m_netclassGrid->GetTextExtent( m_netclassGrid->GetColLabelValue( i ) + "M");
m_originalColWidths[ i ] = std::max( m_netclassGrid->GetColSize( i ), size.x );
m_originalColWidths[ i ] = m_netclassGrid->GetVisibleWidth( i, true, false, true );
m_netclassGrid->SetColSize( i, m_originalColWidths[ i ] );
}

View File

@ -65,26 +65,6 @@ PANEL_SETUP_TEXT_AND_GRAPHICS::PANEL_SETUP_TEXT_AND_GRAPHICS( PAGED_DIALOG* aPar
m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 );
// Work around an issue where wxWidgets doesn't calculate the row width on its own
for( int col = 0; col < m_grid->GetNumberCols(); col++ )
{
// '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
auto size = m_grid->GetTextExtent( m_grid->GetColLabelValue( col ) + "M").x;
for( int row = 0; row < m_grid->GetNumberRows(); row++ )
size = std::max( size, m_grid->GetTextExtent( m_grid->GetCellValue( row, col ) ).x );
m_grid->SetColMinimalWidth( col, size);
}
int size = m_grid->GetRowLabelSize();
for( int row = 0; row < m_grid->GetNumberRows(); row++ )
size = std::max( size, m_grid->GetTextExtent( m_grid->GetRowLabelValue( row ) + "M" ).x );
m_grid->SetRowLabelSize( size );
// Work around a bug in wxWidgets where it fails to recalculate the grid height
// after changing the default row size
m_grid->AppendRows( 1 );
@ -145,6 +125,14 @@ bool PANEL_SETUP_TEXT_AND_GRAPHICS::TransferDataToWindow()
}
}
// Work around an issue where wxWidgets doesn't calculate the row width on its own
for( int col = 0; col < m_grid->GetNumberCols(); col++ )
m_grid->SetColMinimalWidth( col, m_grid->GetVisibleWidth( col, true, true, false ) );
m_grid->SetRowLabelSize( m_grid->GetVisibleWidth( -1, true, true, true ) );
Layout();
return true;
}