diff --git a/include/wx/filedlg.h b/include/wx/filedlg.h index 37643f0c0e..678e47eba3 100644 --- a/include/wx/filedlg.h +++ b/include/wx/filedlg.h @@ -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; diff --git a/src/common/fldlgcmn.cpp b/src/common/fldlgcmn.cpp index 6b02055206..33f691b084 100644 --- a/src/common/fldlgcmn.cpp +++ b/src/common/fldlgcmn.cpp @@ -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= 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 0) - fileDialog.SetFilterIndex(filterIndex); - } + fileDialog.SetFilterIndexFromExt(defaultExtension); wxString filename; if ( fileDialog.ShowModal() == wxID_OK )