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.
This commit is contained in:
Kevin B. McCarty
2015-05-09 19:05:05 +02:00
committed by Vadim Zeitlin
parent 9a44b109bf
commit 178e9c9335
3 changed files with 26 additions and 15 deletions

View File

@@ -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:

View File

@@ -181,8 +181,8 @@ 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
*/

View File

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