From fa333077f53d0907364ac39fc55d9de0b838fb5a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 9 Oct 2009 13:05:20 +0000 Subject: [PATCH] Append default extension before showing file save dialog, not after. Appending the extension after the dialog was hidden is a bad idea as it misleads the user by using a different file name from the one shown in the dialog. It is also dangerous as it bypassed wxFD_OVERWRITE_PROMPT check. So just append the default extension to the initial file name if it doesn't have any but don't modify the file name once it was accepted by user. Closes #11256. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62351 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/gtk/filedlg.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/gtk/filedlg.cpp b/src/gtk/filedlg.cpp index b1d189e564..fb0bfa7735 100644 --- a/src/gtk/filedlg.cpp +++ b/src/gtk/filedlg.cpp @@ -243,16 +243,30 @@ wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, g_signal_connect (m_widget, "response", G_CALLBACK (gtk_filedialog_response_callback), this); + + // deal with extensions/filters SetWildcard(wildCard); + wxString defaultFileNameWithExt = defaultFileName; + if ( !wildCard.empty() && !defaultFileName.empty() && + !wxFileName(defaultFileName).HasExt() ) + { + // append the default extension to the initial file name: GTK won't do + // it for us by default (unlike e.g. MSW) + const wxString defaultExt = m_fc.GetCurrentWildCard().AfterFirst('.'); + if ( defaultExt.find_first_of("?*") == wxString::npos ) + defaultFileNameWithExt += "." + defaultExt; + } + + // if defaultDir is specified it should contain the directory and // defaultFileName should contain the default name of the file, however if // directory is not given, defaultFileName contains both wxFileName fn; if ( defaultDir.empty() ) - fn.Assign(defaultFileName); - else if ( !defaultFileName.empty() ) - fn.Assign(defaultDir, defaultFileName); + fn.Assign(defaultFileNameWithExt); + else if ( !defaultFileNameWithExt.empty() ) + fn.Assign(defaultDir, defaultFileNameWithExt); else fn.AssignDir(defaultDir); @@ -334,21 +348,7 @@ void wxFileDialog::OnSize(wxSizeEvent&) wxString wxFileDialog::GetPath() const { - wxFileName fn = m_fc.GetPath(); - - if (HasFdFlag(wxFD_SAVE)) - { - // add extension - if (!fn.HasExt()) - { - wxFileName wildcard( "/dummy", m_fc.GetCurrentWildCard() ); - wxString ext = wildcard.GetExt(); - if (!ext.empty() && (ext.Find('?') == wxNOT_FOUND) && (ext.Find('*') == wxNOT_FOUND)) - fn.SetExt( ext ); - } - } - - return fn.GetFullPath(); + return m_fc.GetPath(); } void wxFileDialog::GetFilenames(wxArrayString& files) const -- 2.45.2