From 178e9c9335d9e664c8c2752fa036e52db5945b94 Mon Sep 17 00:00:00 2001 From: "Kevin B. McCarty" Date: Sat, 9 May 2015 19:05:05 +0200 Subject: [PATCH] Improve wxFileCtrl::SetFilename() and SetPath() consistency. Don't allow specifying the directory in the former and do check for the directory existence in the latter. Also update the file shown in the dialog in SetFilename(). Closes #16685. --- docs/changes.txt | 1 + interface/wx/filectrl.h | 6 +++--- src/generic/filectrlg.cpp | 34 ++++++++++++++++++++++------------ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 39532b35a5..6024c25123 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -114,6 +114,7 @@ All (GUI): - Harmonize wxMenuEvent handling between all major ports. - Fix wxPGChoices copy ctor (Snoits). - Show how to handle files on command line in docview sample (Neil Mayhew). +- Improve wxFileCtrl::SetFilename() and SetPath() (Kevin B. McCarty). wxGTK: diff --git a/interface/wx/filectrl.h b/interface/wx/filectrl.h index a56f8023a5..364fbf7117 100644 --- a/interface/wx/filectrl.h +++ b/interface/wx/filectrl.h @@ -180,9 +180,9 @@ public: /** Changes to a certain directory and selects a certain file. - - In case the filename specified isn't found/couldn't be shown with - currently selected filter, false is returned. + + If @a path includes the directory part, it must exist, otherwise @false + is returned and nothing else is done. @return Returns @true on success, @false otherwise */ diff --git a/src/generic/filectrlg.cpp b/src/generic/filectrlg.cpp index ed61862e37..3fbb2c93d4 100644 --- a/src/generic/filectrlg.cpp +++ b/src/generic/filectrlg.cpp @@ -1145,15 +1145,16 @@ bool wxGenericFileCtrl::SetDirectory( const wxString& dir ) bool wxGenericFileCtrl::SetFilename( const wxString& name ) { - const long item = m_list->FindItem( -1, name ); - - if ( item == -1 ) // file not found either because it doesn't exist or the - // current filter doesn't show it. - return false; + wxString dir, fn, ext; + wxFileName::SplitPath(name, &dir, &fn, &ext); + wxCHECK_MSG( dir.empty(), false, + wxS( "can't specify directory component to SetFilename" ) ); m_noSelChgEvent = true; - // Deselect selected items + m_text->ChangeValue( name ); + + // Deselect previously selected items { const int numSelectedItems = m_list->GetSelectedItemCount(); @@ -1172,8 +1173,14 @@ bool wxGenericFileCtrl::SetFilename( const wxString& name ) } } - m_list->SetItemState( item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); - m_list->EnsureVisible( item ); + // Select new filename if it's in the list + long item = m_list->FindItem(wxNOT_FOUND, name); + + if ( item != wxNOT_FOUND ) + { + m_list->SetItemState( item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); + m_list->EnsureVisible( item ); + } m_noSelChgEvent = false; @@ -1436,12 +1443,15 @@ void wxGenericFileCtrl::HandleAction( const wxString &fn ) bool wxGenericFileCtrl::SetPath( const wxString& path ) { - if ( !wxFileName::FileExists( ( path ) ) ) + wxString dir, fn, ext; + wxFileName::SplitPath(path, &dir, &fn, &ext); + + if ( !dir.empty() && !wxFileName::DirExists(dir) ) return false; - wxString ext; - wxFileName::SplitPath( path, &m_dir, &m_fileName, &ext ); - if ( !ext.empty() ) + m_dir = dir; + m_fileName = fn; + if ( !ext.empty() || path.Last() == '.' ) { m_fileName += wxT( "." ); m_fileName += ext;