Update wxFileDialog filter index when setting its path.

This ensures that GetFilename() and GetFilterIndex() always return consistent
results, even if the path was set programmatically, e.g. during unattended
tests using wxExpectModal<wxFileDialog>.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77471 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2014-08-24 15:31:55 +00:00
parent 3bcec846a0
commit 47e13f2be0
2 changed files with 37 additions and 20 deletions

View File

@ -131,6 +131,13 @@ public:
static wxString AppendExtension(const wxString &filePath,
const wxString &extensionList);
// Set the filter index to match the given extension.
//
// This is always valid to call, even if the extension is empty or the
// filter list doesn't contain it, the function will just do nothing in
// these cases.
void SetFilterIndexFromExt(const wxString& ext);
protected:
wxString m_message;
wxString m_dir;

View File

@ -200,7 +200,12 @@ void wxFileDialogBase::SetPath(const wxString& path)
wxString ext;
wxFileName::SplitPath(path, &m_dir, &m_fileName, &ext);
if ( !ext.empty() )
{
SetFilterIndexFromExt(ext);
m_fileName << wxT('.') << ext;
}
m_path = path;
}
@ -216,6 +221,30 @@ void wxFileDialogBase::SetFilename(const wxString& name)
m_path = wxFileName(m_dir, m_fileName).GetFullPath();
}
void wxFileDialogBase::SetFilterIndexFromExt(const wxString& ext)
{
// if filter is of form "All files (*)|*|..." set correct filter index
if ( !ext.empty() && m_wildCard.find(wxT('|')) != wxString::npos )
{
int filterIndex = -1;
wxArrayString descriptions, filters;
// don't care about errors, handled already by wxFileDialog
(void)wxParseCommonDialogsFilter(m_wildCard, descriptions, filters);
for (size_t n=0; n<filters.GetCount(); n++)
{
if (filters[n].Contains(ext))
{
filterIndex = n;
break;
}
}
if (filterIndex >= 0)
SetFilterIndex(filterIndex);
}
}
//----------------------------------------------------------------------------
// wxFileDialog convenience functions
//----------------------------------------------------------------------------
@ -250,26 +279,7 @@ wxString wxFileSelector(const wxString& title,
defaultFileName, filter2,
flags, wxPoint(x, y));
// if filter is of form "All files (*)|*|..." set correct filter index
if ( !defaultExtension.empty() && filter2.find(wxT('|')) != wxString::npos )
{
int filterIndex = 0;
wxArrayString descriptions, filters;
// don't care about errors, handled already by wxFileDialog
(void)wxParseCommonDialogsFilter(filter2, descriptions, filters);
for (size_t n=0; n<filters.GetCount(); n++)
{
if (filters[n].Contains(defaultExtension))
{
filterIndex = n;
break;
}
}
if (filterIndex > 0)
fileDialog.SetFilterIndex(filterIndex);
}
fileDialog.SetFilterIndexFromExt(defaultExtension);
wxString filename;
if ( fileDialog.ShowModal() == wxID_OK )