From c65e33ac131664b79b7e9b49eabdce7f3c8ef9b4 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Thu, 23 Mar 2023 19:45:29 -0400 Subject: [PATCH] Prevent crash in color settings due to dropdown weirdness Fixes sentry reported crash KICAD-R6 --- common/dialogs/panel_color_settings.cpp | 21 +++++++++++++++------ include/dialogs/panel_color_settings.h | 5 +++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/common/dialogs/panel_color_settings.cpp b/common/dialogs/panel_color_settings.cpp index 974dbd1af2..be13fd3148 100644 --- a/common/dialogs/panel_color_settings.cpp +++ b/common/dialogs/panel_color_settings.cpp @@ -117,8 +117,7 @@ void PANEL_COLOR_SETTINGS::OnThemeChanged( wxCommandEvent& event ) if( idx == static_cast( m_cbTheme->GetCount() ) - 2 ) { - // separator; re-select active theme - m_cbTheme->SetStringSelection( m_currentSettings->GetName() ); + m_cbTheme->SetStringSelection( GetSettingsDropdownName( m_currentSettings ) ); return; } @@ -217,10 +216,7 @@ void PANEL_COLOR_SETTINGS::createThemeList( const wxString& aCurrent ) for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() ) { - wxString name = settings->GetName(); - - if( settings->IsReadOnly() ) - name += wxS( " " ) + _( "(read-only)" ); + wxString name = GetSettingsDropdownName( settings ); int pos = m_cbTheme->Append( name, static_cast( settings ) ); @@ -286,6 +282,8 @@ void PANEL_COLOR_SETTINGS::ShowColorContextMenu( wxMouseEvent& aEvent, int aLaye auto selected = static_cast( m_cbTheme->GetClientData( m_cbTheme->GetSelection() ) ); + wxCHECK_RET( selected, wxT( "Invalid color theme selected" ) ); + COLOR4D current = m_currentSettings->GetColor( aLayer ); COLOR4D saved = selected->GetColor( aLayer ); bool readOnly = m_currentSettings->IsReadOnly(); @@ -379,3 +377,14 @@ bool PANEL_COLOR_SETTINGS::saveCurrentTheme( bool aValidate ) return true; } + + +wxString PANEL_COLOR_SETTINGS::GetSettingsDropdownName(COLOR_SETTINGS* aSettings) +{ + wxString name = aSettings->GetName(); + + if( aSettings->IsReadOnly() ) + name += wxS( " " ) + _( "(read-only)" ); + + return name; +} \ No newline at end of file diff --git a/include/dialogs/panel_color_settings.h b/include/dialogs/panel_color_settings.h index 36bf236670..0d41e638ad 100644 --- a/include/dialogs/panel_color_settings.h +++ b/include/dialogs/panel_color_settings.h @@ -97,6 +97,11 @@ protected: */ virtual void onColorChanged() {} + /** + * Retrieves the drop down name to be displayed for a color setting + */ + wxString GetSettingsDropdownName( COLOR_SETTINGS* aSettings ); + COLOR_SETTINGS* m_currentSettings; std::map m_labels;