Skip to content

Commit

Permalink
Fix Preferred Editor dialog (lp: 1558353)
Browse files Browse the repository at this point in the history
The functions involved in the problem were PGM_BASE::GetEditorName and
EDA_BASE_FRAME::OnSelectPreferredEditor:

1) OnSelectPreferredEditor showed a dialog to allow the user selecting the
editor, but before that called GetEditorName to get the name of the current
editor (to show as a default in the choose file dialog).

2) The problem was when there was no editor, GetEditorName showed its own
dialog.

3) So the user was seeing first the dialog from (2) and then the dialog from
(1).

4) As GetEditorName is used in many other places the solution I did was to add
to it an optional parameter that tells it what to do if no editor is set. To
avoid modifying other code that relies on the current behaviour, this parameter
has a default value that causes to show the dialog. But now when
OnSelectPreferredEditor calls it, it passes the parameter that causes it to
return an empty string if no editor was set.

5) Also, I found a second bug while doing it which allowed in the first dialog
to select an unexistent file (the dialog was missing the wxFD_FILE_MUST_EXIST
flag).

6) Lastly, to avoid having duplicated code (the one that showed the same dialog
and that configured the wildcard was in two methods) I created a single
function that now both functions call: PGM_BASE::AskUserForPreferredEditor.
This way we also will have consistency in the behaviour of both dialogs and
there is a single place where it needs to be modified.
  • Loading branch information
dwilches authored and Chris Pavlina committed Mar 17, 2016
1 parent aa5a1d1 commit 1c19699
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 38 deletions.
32 changes: 12 additions & 20 deletions common/basicframe.cpp
Expand Up @@ -441,26 +441,18 @@ void EDA_BASE_FRAME::GetKicadHelp( wxCommandEvent& event )

void EDA_BASE_FRAME::OnSelectPreferredEditor( wxCommandEvent& event )
{
wxFileName fn = Pgm().GetEditorName();
wxString wildcard( wxT( "*" ) );

#ifdef __WINDOWS__
wildcard += wxT( ".exe" );
#endif

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 );
// Ask for the current editor and instruct GetEditorName() to not show
// unless we pass false as argument.
wxString editorname = Pgm().GetEditorName( false );

// Ask the user to select a new editor, but suggest the current one as the default.
editorname = Pgm().AskUserForPreferredEditor( editorname );

// 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 );
}


Expand Down
50 changes: 33 additions & 17 deletions common/pgm_base.cpp
Expand Up @@ -315,14 +315,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__
Expand All @@ -333,30 +332,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()
{
Expand Down Expand Up @@ -603,7 +619,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() );
}
Expand Down
17 changes: 16 additions & 1 deletion include/pgm_base.h
Expand Up @@ -133,8 +133,23 @@ class PGM_BASE

/**
* 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(); }

Expand Down

0 comments on commit 1c19699

Please sign in to comment.