diff --git a/common/basicframe.cpp b/common/basicframe.cpp index 05af429e5d..3d74e9bf1b 100644 --- a/common/basicframe.cpp +++ b/common/basicframe.cpp @@ -455,26 +455,18 @@ void EDA_BASE_FRAME::GetKicadHelp( wxCommandEvent& event ) void EDA_BASE_FRAME::OnSelectPreferredEditor( wxCommandEvent& event ) { - wxFileName fn = Pgm().GetEditorName(); - wxString wildcard( wxT( "*" ) ); + // Ask for the current editor and instruct GetEditorName() to not show + // unless we pass false as argument. + wxString editorname = Pgm().GetEditorName( false ); -#ifdef __WINDOWS__ - wildcard += wxT( ".exe" ); -#endif + // Ask the user to select a new editor, but suggest the current one as the default. + editorname = Pgm().AskUserForPreferredEditor( editorname ); - wildcard.Printf( _( "Executable file (%s)|%s" ), - GetChars( wildcard ), GetChars( wildcard ) ); - - wxFileDialog dlg( this, _( "Select Preferred Editor" ), fn.GetPath(), - fn.GetFullName(), wildcard, - wxFD_OPEN | wxFD_FILE_MUST_EXIST ); - - if( dlg.ShowModal() == wxID_CANCEL ) - return; - - wxString editor = dlg.GetPath(); - - Pgm().SetEditorName( editor ); + // If we have a new editor name request it to be copied to m_editor_name and saved + // to the preferences file. If the user cancelled the dialog then the previous + // value will be retained. + if( !editorname.IsEmpty() ) + Pgm().SetEditorName( editorname ); } diff --git a/common/pgm_base.cpp b/common/pgm_base.cpp index d7b06ada7a..3000e8f6c5 100644 --- a/common/pgm_base.cpp +++ b/common/pgm_base.cpp @@ -308,14 +308,13 @@ void PGM_BASE::SetEditorName( const wxString& aFileName ) } -const wxString& PGM_BASE::GetEditorName() +const wxString& PGM_BASE::GetEditorName( bool aCanShowFileChooser ) { wxString editorname = m_editor_name; if( !editorname ) { - // Get the preferred editor name from environment variable first. - if(!wxGetEnv( wxT( "EDITOR" ), &editorname )) + if( !wxGetEnv( wxT( "EDITOR" ), &editorname ) ) { // If there is no EDITOR variable set, try the desktop default #ifdef __WXMAC__ @@ -326,30 +325,47 @@ const wxString& PGM_BASE::GetEditorName() } } - if( !editorname ) // We must get a preferred editor name + // If we still don't have an editor name show a dialog asking the user to select one + if( !editorname && aCanShowFileChooser ) { DisplayInfoMessage( NULL, _( "No default editor found, you must choose it" ) ); - wxString mask( wxT( "*" ) ); - -#ifdef __WINDOWS__ - mask += wxT( ".exe" ); -#endif - editorname = EDA_FILE_SELECTOR( _( "Preferred Editor:" ), wxEmptyString, - wxEmptyString, wxEmptyString, mask, - NULL, wxFD_OPEN, true ); + editorname = AskUserForPreferredEditor(); } + // If we finally have a new editor name request it to be copied to m_editor_name and + // saved to the preferences file. if( !editorname.IsEmpty() ) - { - m_editor_name = editorname; - m_common_settings->Write( wxT( "Editor" ), m_editor_name ); - } + SetEditorName( editorname ); + // m_editor_name already has the same value that editorname, or empty if no editor was + // found/chosen. return m_editor_name; } +const wxString PGM_BASE::AskUserForPreferredEditor( const wxString& aDefaultEditor ) +{ + // Create a mask representing the executable files in the current platform +#ifdef __WINDOWS__ + wxString mask( _( "Executable file (*.exe)|*.exe" ) ); +#else + wxString mask( _( "Executable file (*)|*" ) ); +#endif + + // Extract the path, name and extension from the default editor (even if the editor's + // name was empty, this method will succeed and return empty strings). + wxString path, name, ext; + wxFileName::SplitPath( aDefaultEditor, &path, &name, &ext ); + + // Show the modal editor and return the file chosen (may be empty if the user cancels + // the dialog). + return EDA_FILE_SELECTOR( _( "Select Preferred Editor" ), path, + name, ext, mask, + NULL, wxFD_OPEN | wxFD_FILE_MUST_EXIST, + true ); +} + bool PGM_BASE::initPgm() { @@ -596,7 +612,7 @@ void PGM_BASE::saveCommonSettings() for( ENV_VAR_MAP_ITER it = m_local_env_vars.begin(); it != m_local_env_vars.end(); ++it ) { - wxLogTrace( traceEnvVars, wxT( "Saving environment varaiable config entry %s as %s" ), + wxLogTrace( traceEnvVars, wxT( "Saving environment variable config entry %s as %s" ), GetChars( it->first ), GetChars( it->second.GetValue() ) ); m_common_settings->Write( it->first, it->second.GetValue() ); } diff --git a/include/pgm_base.h b/include/pgm_base.h index e8cc98de6a..740e5275be 100644 --- a/include/pgm_base.h +++ b/include/pgm_base.h @@ -133,8 +133,23 @@ public: /** * Return the preferred editor name. + * @param aCanShowFileChooser If no editor is currently set and this argument is + * 'true' then this method will show a file chooser dialog asking for the + * editor's executable. + * @return Returns the full path of the editor, or an empty string if no editor has + * been set. */ - VTBL_ENTRY const wxString& GetEditorName(); + VTBL_ENTRY const wxString& GetEditorName( bool aCanShowFileChooser = true ); + + /** + * Shows a dialog that instructs the user to select a new preferred editor. + * @param aDefaultEditor Default full path for the default editor this dialog should + * show by default. + * @return Returns the full path of the editor, or an empty string if no editor was + * chosen. + */ + VTBL_ENTRY const wxString AskUserForPreferredEditor( + const wxString& aDefaultEditor = wxEmptyString ); VTBL_ENTRY bool IsKicadEnvVariableDefined() const { return !m_kicad_env.IsEmpty(); }