]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk1/filedlg.cpp
Crash fix under VC++
[wxWidgets.git] / src / gtk1 / filedlg.cpp
index 929c0ccd68a4e6505ee22d8f9b1e92f431295222..adb8f61207321d7791d2a68b1db9999a25af7c53 100644 (file)
@@ -108,10 +108,15 @@ static void gtk_filedialog_response_callback(GtkWidget *w,
 {
     wxapp_install_idle_handler();
     
-    if (response == GTK_RESPONSE_CANCEL)
-        gtk_filedialog_cancel_callback(w, dialog);
-    else
+    if (response == GTK_RESPONSE_ACCEPT)
         gtk_filedialog_ok_callback(w, dialog);
+    else if (response == GTK_RESPONSE_CANCEL)
+        gtk_filedialog_cancel_callback(w, dialog);
+    else // "delete"
+    {
+        gtk_filedialog_cancel_callback(w, dialog);
+        dialog->m_destroyed_by_delete = TRUE;
+    }
 }
 
 //-----------------------------------------------------------------------------
@@ -129,6 +134,7 @@ wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
                        wildCard, style, pos)
 {
     m_needParent = FALSE;
+    m_destroyed_by_delete = FALSE;
 
     if (!PreCreation(parent, pos, wxDefaultSize) ||
         !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
@@ -182,7 +188,8 @@ wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
 
 wxFileDialog::~wxFileDialog()
 {
-    m_widget = NULL;
+    if (m_destroyed_by_delete)
+        m_widget = NULL;
 }
 
 void wxFileDialog::GetFilenames(wxArrayString& files) const 
@@ -237,9 +244,9 @@ void wxFileDialog::SetPath(const wxString& path)
 
     wxFileName fn(path);
     m_path = fn.GetFullPath();
-    
-    SetDirectory(fn.GetPath());
-    SetFilename(fn.GetFullName());
+    m_dir = fn.GetPath();
+    m_fileName = fn.GetFullName();
+    UpdateDialog();
 }
 
 void wxFileDialog::SetDirectory(const wxString& dir)
@@ -247,29 +254,56 @@ void wxFileDialog::SetDirectory(const wxString& dir)
     if (wxDirExists(dir))
     {
         m_dir = dir;
-        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
-                                            wxGTK_CONV(m_dir));
-        wxFileName fn(m_dir,m_fileName);
-        m_path = fn.GetFullPath();
+        m_path = wxFileName(m_dir, m_fileName).GetFullPath();
+        UpdateDialog();
     }
 }
 
 void wxFileDialog::SetFilename(const wxString& name)
 {
     m_fileName = name;
-    wxFileName fn(m_dir,m_fileName);
-    m_path = fn.GetFullPath();
-    
-    gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
-                                      wxGTK_CONV(m_path));
+    m_path = wxFileName(m_dir, m_fileName).GetFullPath();
+    UpdateDialog();
+}
 
-    // pre-fill the filename when saving, too (there's no text entry
-    // control when opening a file, so it doesn't make sense to
-    // do this in when opening files):
-    if (GetWindowStyle() & wxSAVE)
+void wxFileDialog::UpdateDialog()
+{
+    // set currently selected directory to match the path:
+    if (!m_dir.empty() && wxDirExists(m_dir))
     {
-        gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
+        // NB: This is important -- if we set directory only and not the path,
+        //     then dialog will still remember old path set using previous
+        //     call to gtk_chooser_set_filename. If the previous directory
+        //     was a subdirectory of the directory we want to select now,
+        //     the dialog would still contain directory selector controls
+        //     for the subdirectory (with the parent directory selected),
+        //     instead of showing only the parent directory as expected.
+        //     This way, we force GtkFileChooser to really change the
+        //     directory. Finally, it doesn't have to be done if filename
+        //     is not empty because of the code that sets the filename below.
+        if (m_fileName.empty())
+            gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
+                                          wxGTK_CONV(m_dir));
+        
+        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
+                                            wxGTK_CONV(m_dir));
+    }
+    
+    // if the user set only the directory (e.g. by calling SetDirectory)
+    // and not the default filename, then we don't want to set the filename:
+    if (!m_fileName.empty())
+    {
+        gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
+                                      wxGTK_CONV(m_path));
+                                          
+        // pre-fill the filename when saving, too (there's no text entry
+        // control when opening a file, so it doesn't make sense to
+        // do this when opening files):
+        if (GetWindowStyle() & wxSAVE)
+        {
+            gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
                                               wxGTK_CONV(m_fileName));
+        }
     }
 }