diff --git a/cvpcb/class_components_listbox.cpp b/cvpcb/class_components_listbox.cpp index 3b79ad8a6c..3a46ebab37 100644 --- a/cvpcb/class_components_listbox.cpp +++ b/cvpcb/class_components_listbox.cpp @@ -47,7 +47,6 @@ COMPONENTS_LISTBOX::~COMPONENTS_LISTBOX() BEGIN_EVENT_TABLE( COMPONENTS_LISTBOX, ITEMS_LISTBOX_BASE ) - EVT_SIZE( ITEMS_LISTBOX_BASE::OnSize ) EVT_CHAR( COMPONENTS_LISTBOX::OnChar ) EVT_LIST_ITEM_SELECTED( ID_CVPCB_COMPONENT_LIST, COMPONENTS_LISTBOX::OnSelectComponent ) END_EVENT_TABLE() @@ -72,14 +71,19 @@ void COMPONENTS_LISTBOX::SetString( unsigned linecount, const wxString& text ) linecount = m_ComponentList.Count() - 1; if( m_ComponentList.Count() > 0 ) + { m_ComponentList[linecount] = text; + UpdateWidth( linecount ); + } } void COMPONENTS_LISTBOX::AppendLine( const wxString& text ) { m_ComponentList.Add( text ); - SetItemCount( m_ComponentList.Count() ); + int lines = m_ComponentList.Count(); + SetItemCount( lines ); + UpdateWidth( lines - 1 ); } diff --git a/cvpcb/class_footprints_listbox.cpp b/cvpcb/class_footprints_listbox.cpp index 1ba1669a3a..f2ef7b1008 100644 --- a/cvpcb/class_footprints_listbox.cpp +++ b/cvpcb/class_footprints_listbox.cpp @@ -67,6 +67,7 @@ void FOOTPRINTS_LISTBOX::SetString( unsigned linecount, const wxString& text ) linecount = count - 1; m_footprintList[linecount] = text; } + UpdateWidth( linecount ); } @@ -90,7 +91,9 @@ wxString FOOTPRINTS_LISTBOX::GetSelectedFootprint() void FOOTPRINTS_LISTBOX::AppendLine( const wxString& text ) { m_footprintList.Add( text ); - SetItemCount( m_footprintList.Count() ); + int lines = m_footprintList.Count(); + SetItemCount( lines ); + UpdateWidth( lines - 1 ); } @@ -179,24 +182,12 @@ void FOOTPRINTS_LISTBOX::SetFootprints( FOOTPRINT_LIST& aList, const wxString& a SetItemCount( m_footprintList.GetCount() ); SetSelection( selection, true ); RefreshItems( 0L, m_footprintList.GetCount()-1 ); - -#if defined (__WXGTK__ ) //&& wxMINOR_VERSION == 8 - // @bug On GTK and wxWidgets 2.8.x, this will assert in debug builds because the - - // column parameter is -1. This was the only way to prevent GTK3 from - // ellipsizing long strings down to a few characters. It still doesn't set - // the scroll bars correctly (too short) but it's better than any of the - // other alternatives. If someone knows how to fix this, please do. - SetColumnWidth( -1, wxLIST_AUTOSIZE ); -#else - SetColumnWidth( 0, wxLIST_AUTOSIZE ); -#endif + UpdateWidth(); } } BEGIN_EVENT_TABLE( FOOTPRINTS_LISTBOX, ITEMS_LISTBOX_BASE ) - EVT_SIZE( ITEMS_LISTBOX_BASE::OnSize ) EVT_CHAR( FOOTPRINTS_LISTBOX::OnChar ) EVT_LIST_ITEM_SELECTED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftClick ) EVT_LIST_ITEM_ACTIVATED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftDClick ) diff --git a/cvpcb/class_library_listbox.cpp b/cvpcb/class_library_listbox.cpp index 366c208478..259c2b15b1 100644 --- a/cvpcb/class_library_listbox.cpp +++ b/cvpcb/class_library_listbox.cpp @@ -66,6 +66,7 @@ void LIBRARY_LISTBOX::SetString( unsigned linecount, const wxString& text ) if( linecount >= count ) linecount = count - 1; m_libraryList[linecount] = text; + UpdateWidth( linecount ); } } @@ -87,7 +88,9 @@ wxString LIBRARY_LISTBOX::GetSelectedLibrary() void LIBRARY_LISTBOX::AppendLine( const wxString& text ) { m_libraryList.Add( text ); - SetItemCount( m_libraryList.Count() ); + int lines = m_libraryList.Count(); + SetItemCount( lines ); + UpdateWidth( lines - 1 ); } @@ -129,23 +132,12 @@ void LIBRARY_LISTBOX::SetLibraryList( const wxArrayString& aList ) if( m_libraryList.Count() ) { RefreshItems( 0L, m_libraryList.Count()-1 ); - -#if defined (__WXGTK__ ) && wxMINOR_VERSION == 8 - // @bug On GTK and wxWidgets 2.8.x, this will assert in debug builds because the - // column parameter is -1. This was the only way to prevent GTK3 from - // ellipsizing long strings down to a few characters. It still doesn't set - // the scroll bars correctly (too short) but it's better than any of the - // other alternatives. If someone knows how to fix this, please do. - SetColumnWidth( -1, wxLIST_AUTOSIZE ); -#else - SetColumnWidth( 0, wxLIST_AUTOSIZE ); -#endif + UpdateWidth(); } } BEGIN_EVENT_TABLE( LIBRARY_LISTBOX, ITEMS_LISTBOX_BASE ) - EVT_SIZE( ITEMS_LISTBOX_BASE::OnSize ) EVT_CHAR( LIBRARY_LISTBOX::OnChar ) EVT_LIST_ITEM_SELECTED( ID_CVPCB_LIBRARY_LIST, LIBRARY_LISTBOX::OnSelectLibrary ) END_EVENT_TABLE() diff --git a/cvpcb/cvframe.cpp b/cvpcb/cvframe.cpp index 2c8e13c17d..3fde5b30ac 100644 --- a/cvpcb/cvframe.cpp +++ b/cvpcb/cvframe.cpp @@ -445,6 +445,10 @@ bool CVPCB_MAINFRAME::OpenProjectFiles( const std::vector& aFileSet, i UpdateTitle(); + // Resize the components list box. This is needed in case the + // contents have shrunk compared to the previous netlist. + m_compListBox->UpdateWidth(); + // OSX need it since some objects are "rebuild" just make aware AUI // Fixes #1258081 m_auimgr.Update(); @@ -949,17 +953,7 @@ void CVPCB_MAINFRAME::BuildCmpListBox() m_compListBox->SetItemCount( m_compListBox->m_ComponentList.Count() ); m_compListBox->SetSelection( 0, true ); m_compListBox->RefreshItems( 0L, m_compListBox->m_ComponentList.Count()-1 ); - -#if defined (__WXGTK__ ) - // @bug On GTK and wxWidgets 2.8.x, this will assert in debug builds because the - // column parameter is -1. This was the only way to prevent GTK3 from - // ellipsizing long strings down to a few characters. It still doesn't set - // the scroll bars correctly (too short) but it's better than any of the - // other alternatives. If someone knows how to fix this, please do. - m_compListBox->SetColumnWidth( -1, wxLIST_AUTOSIZE ); -#else - m_compListBox->SetColumnWidth( 0, wxLIST_AUTOSIZE ); -#endif + m_compListBox->UpdateWidth(); } } diff --git a/cvpcb/cvstruct.h b/cvpcb/cvstruct.h index fcf9cf419d..7623658ac8 100644 --- a/cvpcb/cvstruct.h +++ b/cvpcb/cvstruct.h @@ -50,9 +50,23 @@ public: ~ITEMS_LISTBOX_BASE(); int GetSelection(); - void OnSize( wxSizeEvent& event ); virtual CVPCB_MAINFRAME* GetParent() const; + + /* Function UpdateWidth + * + * Update the width of the column based on its contents. + * + * @param aLine is the line to calculate the width from. If positive, the + * width will only be increased if needed. If negative, we start from + * scratch and all lines are considered, i.e., the column may be shrunk. + */ + void UpdateWidth( int aLine = -1 ); + +private: + void UpdateLineWidth( unsigned aLine ); + + int columnWidth; }; diff --git a/cvpcb/listboxes.cpp b/cvpcb/listboxes.cpp index bc7f364658..da2d6cf938 100644 --- a/cvpcb/listboxes.cpp +++ b/cvpcb/listboxes.cpp @@ -44,10 +44,9 @@ ITEMS_LISTBOX_BASE::ITEMS_LISTBOX_BASE( CVPCB_MAINFRAME* aParent, wxWindowID aId, const wxPoint& aLocation, const wxSize& aSize, long aStyle) : - wxListView( aParent, aId, aLocation, aSize, LISTB_STYLE | aStyle ) + wxListView( aParent, aId, aLocation, aSize, LISTB_STYLE | aStyle ), columnWidth(0) { InsertColumn( 0, wxEmptyString ); - SetColumnWidth( 0, wxLIST_AUTOSIZE ); } @@ -56,17 +55,47 @@ ITEMS_LISTBOX_BASE::~ITEMS_LISTBOX_BASE() } -/* - * Adjust the column width to the entire available window width - */ -void ITEMS_LISTBOX_BASE::OnSize( wxSizeEvent& event ) +void ITEMS_LISTBOX_BASE::UpdateWidth( int aLine ) { - wxSize size = GetClientSize(); - int width = 0; + // Less than zero: recalculate width of all items. + if( aLine < 0 ) + { + columnWidth = 0; + for( int ii = 0; ii < GetItemCount(); ii++ ) + { + UpdateLineWidth( (unsigned)ii ); + } + } - SetColumnWidth( 0, std::max( width, size.x ) ); + // Zero or above: update from a single line. + else + { + if( aLine < GetItemCount() ) + UpdateLineWidth( (unsigned)aLine ); + } +} - event.Skip(); + +/* + * Calculate the width of the given line, and increase the column width + * if needed. This is effectively the wxListCtrl code for autosizing. + * NB. it relies on the caller checking the given line number is valid. + */ +void ITEMS_LISTBOX_BASE::UpdateLineWidth( unsigned aLine ) +{ + wxClientDC dc( this ); + wxCoord w; + int newWidth = 10; // Value of AUTOSIZE_COL_MARGIN from wxWidgets source. + + dc.SetFont( GetFont() ); + dc.GetTextExtent( GetItemText( aLine, 0 ) + " ", &w, NULL ); + newWidth += w; + + if( newWidth > columnWidth ) + { + columnWidth = newWidth; + SetColumnWidth( 0, columnWidth ); + } }