diff --git a/kicad/dialogs/dialog_template_selector.cpp b/kicad/dialogs/dialog_template_selector.cpp index 173e3a942c..4de6010d08 100644 --- a/kicad/dialogs/dialog_template_selector.cpp +++ b/kicad/dialogs/dialog_template_selector.cpp @@ -54,6 +54,56 @@ void TEMPLATE_SELECTION_PANEL::AddTemplateWidget( TEMPLATE_WIDGET* aTemplateWidg } +// Sort the widgets alphabetically, leaving Default at the top +void TEMPLATE_SELECTION_PANEL::SortAlphabetically() +{ + std::vector sortedList; + TEMPLATE_WIDGET* default_temp = nullptr; + size_t count = m_SizerChoice->GetItemCount(); + + if( count <= 1 ) + return; + + for( size_t idx = 0; idx < count; idx++ ) + { + wxSizerItem* item = m_SizerChoice->GetItem( idx ); + if( item && item->IsWindow() ) + { + TEMPLATE_WIDGET* temp = static_cast( item->GetWindow() ); + + const wxString title = *temp->GetTemplate()->GetTitle(); + + if( default_temp == nullptr && title.CmpNoCase( "default" ) == 0 ) + default_temp = temp; + else + sortedList.push_back( temp ); + } + } + + std::sort( + sortedList.begin(), sortedList.end(), + []( TEMPLATE_WIDGET* aWidgetA, TEMPLATE_WIDGET* aWidgetB ) -> bool + { + const wxString* a = aWidgetA->GetTemplate()->GetTitle(); + const wxString* b = aWidgetB->GetTemplate()->GetTitle(); + + return ( *a ).CmpNoCase( *b ) < 0; + }); + + m_SizerChoice->Clear( false ); + + if( default_temp != nullptr ) + m_SizerChoice->Add( default_temp ); + + for (TEMPLATE_WIDGET* temp : sortedList) + { + m_SizerChoice->Add( temp ); + } + + Layout(); +} + + TEMPLATE_WIDGET::TEMPLATE_WIDGET( wxWindow* aParent, DIALOG_TEMPLATE_SELECTOR* aDialog ) : TEMPLATE_WIDGET_BASE( aParent ) { @@ -337,6 +387,7 @@ void DIALOG_TEMPLATE_SELECTOR::buildPageContent( const wxString& aPath, int aPag } } + m_panels[aPage]->SortAlphabetically(); Layout(); } diff --git a/kicad/dialogs/dialog_template_selector.h b/kicad/dialogs/dialog_template_selector.h index 4a1e28144c..6229fcfbee 100644 --- a/kicad/dialogs/dialog_template_selector.h +++ b/kicad/dialogs/dialog_template_selector.h @@ -81,6 +81,8 @@ public: void AddTemplateWidget( TEMPLATE_WIDGET* aTemplateWidget ); + void SortAlphabetically(); + protected: wxNotebookPage* m_parent; wxString m_templatesPath; ///< the path to access to the folder