diff --git a/common/widgets/wx_grid.cpp b/common/widgets/wx_grid.cpp index e620e62da6..d1bbc956f9 100644 --- a/common/widgets/wx_grid.cpp +++ b/common/widgets/wx_grid.cpp @@ -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; +} diff --git a/common/widgets/wx_grid.h b/common/widgets/wx_grid.h index c641c3da3c..61c57ed619 100644 --- a/common/widgets/wx_grid.h +++ b/common/widgets/wx_grid.h @@ -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; diff --git a/pcbnew/dialogs/dialog_edit_footprint_for_BoardEditor.cpp b/pcbnew/dialogs/dialog_edit_footprint_for_BoardEditor.cpp index 99711a2154..d1a69521ed 100644 --- a/pcbnew/dialogs/dialog_edit_footprint_for_BoardEditor.cpp +++ b/pcbnew/dialogs/dialog_edit_footprint_for_BoardEditor.cpp @@ -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()); diff --git a/pcbnew/dialogs/dialog_edit_footprint_for_fp_editor.cpp b/pcbnew/dialogs/dialog_edit_footprint_for_fp_editor.cpp index 6999ba7e22..52c58174f7 100644 --- a/pcbnew/dialogs/dialog_edit_footprint_for_fp_editor.cpp +++ b/pcbnew/dialogs/dialog_edit_footprint_for_fp_editor.cpp @@ -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()); diff --git a/pcbnew/dialogs/panel_modedit_defaults.cpp b/pcbnew/dialogs/panel_modedit_defaults.cpp index c80c5031ac..cce359e137 100644 --- a/pcbnew/dialogs/panel_modedit_defaults.cpp +++ b/pcbnew/dialogs/panel_modedit_defaults.cpp @@ -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; } diff --git a/pcbnew/dialogs/panel_pcbnew_action_plugins.cpp b/pcbnew/dialogs/panel_pcbnew_action_plugins.cpp index d9e1d93aa3..8c53383b44 100644 --- a/pcbnew/dialogs/panel_pcbnew_action_plugins.cpp +++ b/pcbnew/dialogs/panel_pcbnew_action_plugins.cpp @@ -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(); diff --git a/pcbnew/dialogs/panel_setup_netclasses.cpp b/pcbnew/dialogs/panel_setup_netclasses.cpp index 33dcf8aab7..18501a1525 100644 --- a/pcbnew/dialogs/panel_setup_netclasses.cpp +++ b/pcbnew/dialogs/panel_setup_netclasses.cpp @@ -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 ] ); } diff --git a/pcbnew/dialogs/panel_setup_text_and_graphics.cpp b/pcbnew/dialogs/panel_setup_text_and_graphics.cpp index f0adcbd45d..8f771d0fd0 100644 --- a/pcbnew/dialogs/panel_setup_text_and_graphics.cpp +++ b/pcbnew/dialogs/panel_setup_text_and_graphics.cpp @@ -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; }