]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk1/filedlg.cpp
Crash fix under VC++
[wxWidgets.git] / src / gtk1 / filedlg.cpp
index a9f92c274f4e58a731db49a88df1727031cf5863..adb8f61207321d7791d2a68b1db9999a25af7c53 100644 (file)
@@ -20,6 +20,7 @@
 #include "wx/utils.h"
 #include "wx/intl.h"
 #include "wx/filename.h"
+#include "wx/msgdlg.h"
 
 #include <gtk/gtk.h>
 #include "wx/gtk/private.h"
@@ -81,7 +82,7 @@ static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
         }
     }
 
-    dialog->DoSetPath(filename);
+    dialog->SetPath(filename);
     dialog->UpdateFromDialog();
     
     wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
@@ -107,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;
+    }
 }
 
 //-----------------------------------------------------------------------------
@@ -128,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,
@@ -179,10 +186,10 @@ wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
     SetFilterIndex(0);
 }
 
-void wxFileDialog::SetPath(const wxString& path)
+wxFileDialog::~wxFileDialog()
 {
-    DoSetPath(path);
-    UpdateDialog();
+    if (m_destroyed_by_delete)
+        m_widget = NULL;
 }
 
 void wxFileDialog::GetFilenames(wxArrayString& files) const 
@@ -200,6 +207,7 @@ void wxFileDialog::GetFilenames(wxArrayString& files) const
         files[n] = name;
     }
 }
+
 void wxFileDialog::GetPaths(wxArrayString& paths) const 
 {
     paths.Empty(); 
@@ -230,17 +238,73 @@ void wxFileDialog::SetMessage(const wxString& message)
     SetTitle(message);
 }
 
+void wxFileDialog::SetPath(const wxString& path)
+{
+    if (path.empty()) return;
+
+    wxFileName fn(path);
+    m_path = fn.GetFullPath();
+    m_dir = fn.GetPath();
+    m_fileName = fn.GetFullName();
+    UpdateDialog();
+}
+
 void wxFileDialog::SetDirectory(const wxString& dir)
 {
-    wxFileName fn(dir,m_fileName);
-    SetPath(fn.GetFullPath());
+    if (wxDirExists(dir))
+    {
+        m_dir = dir;
+        m_path = wxFileName(m_dir, m_fileName).GetFullPath();
+        UpdateDialog();
+    }
 }
 
 void wxFileDialog::SetFilename(const wxString& name)
 {
     m_fileName = name;
-    wxFileName fn(m_dir,name);
-    SetPath(fn.GetFullPath());
+    m_path = wxFileName(m_dir, m_fileName).GetFullPath();
+    UpdateDialog();
+}
+
+void wxFileDialog::UpdateDialog()
+{
+    // set currently selected directory to match the path:
+    if (!m_dir.empty() && wxDirExists(m_dir))
+    {
+        // 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));
+        }
+    }
 }
 
 void wxFileDialog::SetWildcard(const wxString& wildCard)
@@ -333,48 +397,4 @@ void wxFileDialog::UpdateFromDialog()
     g_slist_free(filters);
 }
 
-void wxFileDialog::UpdateDialog()
-{
-    
-    if (wxDirExists(m_path))
-    {
-        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
-                                            wxGTK_CONV(m_path));
-    }
-    else
-    {
-        gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
-                                      wxGTK_CONV(m_path));
-
-        // pre-fill the filename, too:
-        if (GetWindowStyle() & wxSAVE)
-        {
-            gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
-                                              wxGTK_CONV(m_fileName));
-        }
-    }
-}
-
-void wxFileDialog::DoSetPath(const wxString& path)
-{
-    if (!path.empty())
-    {
-        wxFileName fn(path);
-        fn.MakeAbsolute();
-        m_path = fn.GetFullPath();
-        
-        wxString ext;
-        wxSplitPath(path, &m_dir, &m_fileName, &ext);
-        if (!ext.empty())
-        {
-            m_fileName += wxT(".");
-            m_fileName += ext;
-        }
-    }
-    else
-    {
-        m_path = path;
-    }
-}
-
 #endif // wxUSE_FILEDLG && defined(__WXGTK24__)