]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/filedlg.cpp
bug fix for using multiline string (i.e. with embedded '\n's) in wxTextCtrl ctor
[wxWidgets.git] / src / gtk / filedlg.cpp
index 42278d6605501ed8e62e296fbe8d94261bf35e76..946ae917c47082900542b2fe9a860f3aa41344f4 100644 (file)
@@ -1,10 +1,9 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        filedlg.cpp
+// Name:        gtk/filedlg.cpp
 // Purpose:
 // Author:      Robert Roebling
-// Created:     01/02/97
-// Id:
-// Copyright:   (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
+// Id:          $Id$
+// Copyright:   (c) 1998 Robert Roebling
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 #include "wx/intl.h"
 #include "wx/generic/msgdlgg.h"
 
+
+#include <gtk/gtk.h>
+
 //-----------------------------------------------------------------------------
-// wxFileDialog
+// idle system
 //-----------------------------------------------------------------------------
 
-void gtk_filedialog_ok_callback( GtkWidget *WXUNUSED(widget), gpointer data )
+extern void wxapp_install_idle_handler();
+extern bool g_isIdle;
+
+//-----------------------------------------------------------------------------
+// "delete_event"
+//-----------------------------------------------------------------------------
+
+static
+bool gtk_filedialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
 {
-  wxFileDialog *dialog = (wxFileDialog*)data;
-  wxCommandEvent event(wxEVT_NULL);
-  int style;
+    if (g_isIdle) wxapp_install_idle_handler();
 
-  style=dialog->GetStyle();
+/*
+    printf( "OnDelete from " );
+    if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
+        printf( win->GetClassInfo()->GetClassName() );
+    printf( ".\n" );
+*/
 
-  if((style&wxSAVE)&&(style&wxOVERWRITE_PROMPT))
-       if(wxFileExists(gtk_file_selection_get_filename(GTK_FILE_SELECTION(dialog->m_widget) ))) {
-         if(wxMessageBox(_("File exists. Overwrite?"),
-                         _("Confirm"), wxYES_NO) != wxYES)
-               return;
-       }
+    win->Close();
 
-  dialog->OnOK( event );
-};
+    return TRUE;
+}
+
+//-----------------------------------------------------------------------------
+// "clicked" for OK-button
+//-----------------------------------------------------------------------------
 
-void gtk_filedialog_cancel_callback( GtkWidget *WXUNUSED(widget), gpointer data )
+static
+void gtk_filedialog_ok_callback( GtkWidget *WXUNUSED(widget), wxFileDialog *dialog )
 {
-  wxFileDialog *dialog = (wxFileDialog*)data;
-  wxCommandEvent event(wxEVT_NULL);
-  dialog->OnCancel( event );
-};
+    if (g_isIdle) wxapp_install_idle_handler();
+
+    int style = dialog->GetStyle();
+
+    GtkFileSelection *filedlg = GTK_FILE_SELECTION(dialog->m_widget);
+    char *filename = gtk_file_selection_get_filename(filedlg);
+
+    if ( (style & wxSAVE) && ( style & wxOVERWRITE_PROMPT ) )
+    {
+        if (wxFileExists( filename ))
+        {
+            wxString msg;
+            msg.Printf( _("File '%s' already exists, do you really want to "
+                         "overwrite it?"), filename);
+
+            if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
+                return;
+        }
+    }
+    else if ( (style & wxOPEN) && ( style & wxFILE_MUST_EXIST) )
+    {
+        if ( !wxFileExists( filename ) )
+        {
+            wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK);
+
+            return;
+        }
+    }
+
+    // change to the directory where the user went if asked
+    if ( style & wxCHANGE_DIR )
+    {
+        wxString cwd;
+        wxSplitPath(filename, &cwd, NULL, NULL);
+
+        if ( cwd != wxGetWorkingDirectory() )
+        {
+            wxSetWorkingDirectory(cwd);
+        }
+    }
+
+    dialog->SetPath( filename );
+
+    wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
+    event.SetEventObject( dialog );
+    dialog->GetEventHandler()->ProcessEvent( event );
+}
 
-IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
+//-----------------------------------------------------------------------------
+// "clicked" for Cancel-button
+//-----------------------------------------------------------------------------
 
-wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
-        const wxString& defaultDir, const wxString& defaultFileName,
-  const wxString& wildCard,
-        long style, const wxPoint& pos )
+static
+void gtk_filedialog_cancel_callback( GtkWidget *WXUNUSED(w), wxFileDialog *dialog )
 {
-  m_needParent = FALSE;
-
-  PreCreation( parent, -1, pos, wxDefaultSize, style | wxDIALOG_MODAL, "filedialog" );
-  m_message = message;
-  m_path = "";
-  m_fileName = defaultFileName;
-  m_dir = defaultDir;
-  m_wildCard = wildCard;
-  m_dialogStyle = style;
-  m_filterIndex = 1;
-
-  m_widget = gtk_file_selection_new(_("File selection"));
-  
-  int x = (gdk_screen_width () - 400) / 2;
-  int y = (gdk_screen_height () - 400) / 2;
-  gtk_widget_set_uposition( m_widget, x, y );
-  
-  GtkFileSelection *sel = GTK_FILE_SELECTION(m_widget);
-
-  m_path.Append(m_dir);
-  if(! m_path.IsEmpty() && m_path.Last()!='/') m_path.Append('/');
-  m_path.Append(m_fileName);
-
-  if(m_path.Length()>1) gtk_file_selection_set_filename(sel,m_path);
-
-  gtk_signal_connect( GTK_OBJECT(sel->ok_button), "clicked",
-    GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback), (gpointer*)this );
-
-  gtk_signal_connect( GTK_OBJECT(sel->cancel_button), "clicked",
-    GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback), (gpointer*)this );
-};
-
-int wxFileDialog::ShowModal(void)
+    if (g_isIdle) wxapp_install_idle_handler();
+
+    wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
+    event.SetEventObject( dialog );
+    dialog->GetEventHandler()->ProcessEvent( event );
+}
+
+//-----------------------------------------------------------------------------
+// wxFileDialog
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
+
+wxFileDialog::wxFileDialog( wxWindow *parent, const wxString& message,
+                            const wxString& defaultDir, const wxString& defaultFileName,
+                            const wxString& wildCard,
+                            long style, const wxPoint& pos )
 {
-  int ret = wxDialog::ShowModal();
+    m_needParent = FALSE;
+
+    if (!PreCreation( parent, pos, wxDefaultSize ) ||
+        !CreateBase( parent, -1, pos, wxDefaultSize, style | wxDIALOG_MODAL, wxDefaultValidator, wxT("filedialog") ))
+    {
+        wxFAIL_MSG( wxT("wxXX creation failed") );
+        return;
+    }
+
+    m_message = message;
+    m_path = wxT("");
+    m_fileName = defaultFileName;
+    m_dir = defaultDir;
+    m_wildCard = wildCard;
+    m_dialogStyle = style;
+    m_filterIndex = 1;
+
+    m_widget = gtk_file_selection_new( m_message.mbc_str() );
+
+    int x = (gdk_screen_width () - 400) / 2;
+    int y = (gdk_screen_height () - 400) / 2;
+    gtk_widget_set_uposition( m_widget, x, y );
+
+    GtkFileSelection *sel = GTK_FILE_SELECTION(m_widget);
+    gtk_file_selection_hide_fileop_buttons( sel ); // they don't work anyway
+
+    m_path.Append(m_dir);
+    if( ! m_path.IsEmpty() && m_path.Last()!=wxT('/') )
+        m_path.Append('/');
+    m_path.Append(m_fileName);
 
-  if (ret == wxID_OK)
-  {
-    m_fileName = gtk_file_selection_get_filename( GTK_FILE_SELECTION(m_widget) );
-    m_path = gtk_file_selection_get_filename( GTK_FILE_SELECTION(m_widget) );
-  };
-  return ret;
-};
+    if(m_path.Length()>1) gtk_file_selection_set_filename(sel,m_path.mbc_str());
 
+    gtk_signal_connect( GTK_OBJECT(sel->ok_button), "clicked",
+      GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback), (gpointer*)this );
 
-char *wxFileSelector(const char *title,
-                     const char *defaultDir, const char *defaultFileName,
-                     const char *defaultExtension, const char *filter, int flags,
-                     wxWindow *parent, int x, int y)
+    // strange way to internationalize
+    gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->ok_button)->child ), wxConvCurrent->cWX2MB(_("OK")) );
+
+    gtk_signal_connect( GTK_OBJECT(sel->cancel_button), "clicked",
+      GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback), (gpointer*)this );
+
+    // strange way to internationalize
+    gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->cancel_button)->child ), wxConvCurrent->cWX2MB(_("Cancel")) );
+
+    gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
+        GTK_SIGNAL_FUNC(gtk_filedialog_delete_callback), (gpointer)this );
+}
+
+void wxFileDialog::SetPath(const wxString& path)
 {
-  wxString filter2("");
-  if ( defaultExtension && !filter )
-    filter2 = wxString("*.") + wxString(defaultExtension) ;
-  else if ( filter )
-    filter2 = filter;
-
-  wxString defaultDirString;
-  if (defaultDir)
-    defaultDirString = defaultDir;
-  else
-    defaultDirString = "";
-
-  wxString defaultFilenameString;
-  if (defaultFileName)
-    defaultFilenameString = defaultFileName;
-  else
-    defaultFilenameString = "";
-
-  wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString,
-    filter2, flags, wxPoint(x, y));
-
-  if ( fileDialog.ShowModal() == wxID_OK )
-  {
-    strcpy(wxBuffer, (const char *)fileDialog.GetPath());
-    return wxBuffer;
-  }
-  else
-    return NULL;
-};
-
-char* wxLoadFileSelector(const char *what, const char *extension, const char *default_name,
-         wxWindow *parent )
+    // not only set the full path but also update filename and dir
+    m_path = path;
+    if ( !!path )
+    {
+        wxString ext;
+        wxSplitPath(path, &m_dir, &m_fileName, &ext);
+        if (!ext.IsEmpty())
+        {
+            m_fileName += wxT(".");
+            m_fileName += ext;
+        }
+    }
+}
+
+// ----------------------------------------------------------------------------
+// global functions
+// ----------------------------------------------------------------------------
+
+wxString
+wxFileSelectorEx(const wxChar *message,
+                 const wxChar *default_path,
+                 const wxChar *default_filename,
+                 int *indexDefaultExtension,
+                 const wxChar *wildcard,
+                 int flags,
+                 wxWindow *parent,
+                 int x, int y)
 {
-  char *ext = (char *)extension;
+    // TODO: implement this somehow
+    return wxFileSelector(message, default_path, default_filename, wxT(""),
+                          wildcard, flags, parent, x, y);
+}
+
+wxString wxFileSelector( const wxChar *title,
+                      const wxChar *defaultDir, const wxChar *defaultFileName,
+                      const wxChar *defaultExtension, const wxChar *filter, int flags,
+                      wxWindow *parent, int x, int y )
+{
+    wxString filter2;
+    if ( defaultExtension && !filter )
+        filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ;
+    else if ( filter )
+        filter2 = filter;
+
+    wxString defaultDirString;
+    if (defaultDir)
+        defaultDirString = defaultDir;
+
+    wxString defaultFilenameString;
+    if (defaultFileName)
+        defaultFilenameString = defaultFileName;
+
+    wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
+
+    if ( fileDialog.ShowModal() == wxID_OK )
+    {
+        return fileDialog.GetPath();
+    }
+    else
+    {
+        return wxEmptyString;
+    }
+}
+
+wxString wxLoadFileSelector( const wxChar *what, const wxChar *extension, const wxChar *default_name, wxWindow *parent )
+{
+    wxChar *ext = (wxChar *)extension;
 
-  char prompt[50];
-  wxString str = _("Load %s file");
-  sprintf(prompt, str, what);
+    wxString prompt = wxString::Format(_("Load %s file"), what);
 
-  if (*ext == '.') ext++;
-  char wild[60];
-  sprintf(wild, "*.%s", ext);
+    if (*ext == wxT('.'))
+        ext++;
 
-  return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
-};
+    wxString wild = wxString::Format(_T("*.%s"), ext);
 
-char* wxSaveFileSelector(const char *what, const char *extension, const char *default_name,
+    return wxFileSelector(prompt, (const wxChar *) NULL, default_name,
+                          ext, wild, 0, parent);
+}
+
+wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name,
          wxWindow *parent )
 {
-  char *ext = (char *)extension;
+    wxChar *ext = (wxChar *)extension;
+
+    wxString prompt = wxString::Format(_("Save %s file"), what);
 
-  char prompt[50];
-  wxString str = _("Save %s file");
-  sprintf(prompt, str, what);
+    if (*ext == wxT('.'))
+        ext++;
 
-  if (*ext == '.') ext++;
-  char wild[60];
-  sprintf(wild, "*.%s", ext);
+    wxString wild = wxString::Format(_T("*.%s"), ext);
 
-  return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
-};
+    return wxFileSelector(prompt, (const wxChar *) NULL, default_name,
+                          ext, wild, 0, parent);
+}