]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/filectrl.cpp
Fix return value of wxCountingOutputStream::LastWrite().
[wxWidgets.git] / src / gtk / filectrl.cpp
index d2b6e8c501c02c8660ec7a07c5e9c354fc7cad89..b6c642fcab45307400667e47c95178ed15331f9a 100644 (file)
 #pragma hdrstop
 #endif
 
-#include "wx/filectrl.h"
-
-#if !defined(__WXUNIVERSAL__)
+#if wxUSE_FILECTRL && !defined(__WXUNIVERSAL__)
 
-#ifndef WX_PRECOMP
-#    include "wx/sizer.h"
-#    include "wx/debug.h"
-#endif
+#include "wx/filectrl.h"
 
 #include "wx/gtk/private.h"
-#include "wx/filedlg.h"
 #include "wx/filename.h"
+#include "wx/scopeguard.h"
 #include "wx/tokenzr.h"
 
 //-----------------------------------------------------------------------------
@@ -46,8 +41,8 @@ wxString wxGtkFileChooser::GetPath() const
     wxGtkString str( gtk_file_chooser_get_filename( m_widget ) );
 
     wxString string;
-    if (str.c_str() != NULL)
-        string = wxConvFileName->cMB2WX(str);
+    if (str)
+        string = wxString::FromUTF8(str);
     return string;
 }
 
@@ -70,7 +65,7 @@ void wxGtkFileChooser::GetPaths( wxArrayString& paths ) const
         GSList *gpaths = gpathsi;
         while ( gpathsi )
         {
-            wxString file( wxConvFileName->cMB2WX( ( gchar* ) gpathsi->data ) );
+            wxString file(wxString::FromUTF8(static_cast<gchar *>(gpathsi->data)));
             paths.Add( file );
             g_free( gpathsi->data );
             gpathsi = gpathsi->next;
@@ -84,24 +79,46 @@ void wxGtkFileChooser::GetPaths( wxArrayString& paths ) const
 
 bool wxGtkFileChooser::SetPath( const wxString& path )
 {
-    if ( path.empty() ) return true;
+    if ( path.empty() )
+        return true;
+
+    switch ( gtk_file_chooser_get_action( m_widget ) )
+    {
+        case GTK_FILE_CHOOSER_ACTION_SAVE:
+            {
+                wxFileName fn(path);
+
+                const wxString fname = fn.GetFullName();
+                gtk_file_chooser_set_current_name( m_widget, fname.utf8_str() );
 
-    return gtk_file_chooser_set_filename( m_widget,
-                                          wxConvFileName->cWX2MB( path.c_str() ) );
+                // set the initial file name and/or directory
+                const wxString dir = fn.GetPath();
+                return gtk_file_chooser_set_current_folder( m_widget,
+                                                            dir.utf8_str() ) != 0;
+            }
+
+        case GTK_FILE_CHOOSER_ACTION_OPEN:
+            return gtk_file_chooser_set_filename( m_widget, path.utf8_str() ) != 0;
+
+        case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
+        case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
+            break;
+    }
+
+    wxFAIL_MSG( "Unexpected file chooser type" );
+
+    return false;
 }
 
 bool wxGtkFileChooser::SetDirectory( const wxString& dir )
 {
-    const gboolean b =
-        gtk_file_chooser_set_current_folder( m_widget,
-                                             wxConvFileName->cWX2MB( dir.c_str() ) );
-    return b != 0;
+    return gtk_file_chooser_set_current_folder( m_widget, dir.utf8_str() ) != 0;
 }
 
 wxString wxGtkFileChooser::GetDirectory() const
 {
     const wxGtkString str( gtk_file_chooser_get_current_folder( m_widget ) );
-    return wxString( str, *wxConvFileName );
+    return wxString::FromUTF8(str);
 }
 
 wxString wxGtkFileChooser::GetFilename() const
@@ -111,6 +128,8 @@ wxString wxGtkFileChooser::GetFilename() const
 
 void wxGtkFileChooser::SetWildcard( const wxString& wildCard )
 {
+    m_wildcards.Empty();
+
     // parse filters
     wxArrayString wildDescriptions, wildFilters;
 
@@ -127,6 +146,9 @@ void wxGtkFileChooser::SetWildcard( const wxString& wildCard )
         GSList* ifilters = gtk_file_chooser_list_filters( chooser );
         GSList* filters = ifilters;
 
+        m_ignoreNextFilterEvent = true;
+        wxON_BLOCK_EXIT_SET(m_ignoreNextFilterEvent, false);
+
         while ( ifilters )
         {
             gtk_file_chooser_remove_filter( chooser, GTK_FILE_FILTER( ifilters->data ) );
@@ -144,10 +166,16 @@ void wxGtkFileChooser::SetWildcard( const wxString& wildCard )
                 gtk_file_filter_set_name( filter, wxGTK_CONV_SYS( wildDescriptions[n] ) );
 
                 wxStringTokenizer exttok( wildFilters[n], wxT( ";" ) );
+
+                int n1 = 1;
                 while ( exttok.HasMoreTokens() )
                 {
                     wxString token = exttok.GetNextToken();
                     gtk_file_filter_add_pattern( filter, wxGTK_CONV_SYS( token ) );
+
+                    if (n1 == 1)
+                        m_wildcards.Add( token ); // Only add first pattern to list, used later when saving
+                    n1++;
                 }
 
                 gtk_file_chooser_add_filter( chooser, filter );
@@ -196,6 +224,11 @@ int wxGtkFileChooser::GetFilterIndex() const
         return index;
 }
 
+bool wxGtkFileChooser::HasFilterChoice() const
+{
+    return gtk_file_chooser_get_filter( m_widget ) != NULL;
+}
+
 //-----------------------------------------------------------------------------
 // end wxGtkFileChooser Implementation
 //-----------------------------------------------------------------------------
@@ -252,13 +285,33 @@ extern "C"
     }
 }
 
+extern "C"
+{
+    static void
+    gtkfilechooserwidget_notify_callback( GObject *WXUNUSED( gobject ), GParamSpec *arg1, wxGtkFileCtrl *fileCtrl )
+    {
+        const char *name = g_param_spec_get_name (arg1);
+        if ( strcmp( name, "filter" ) == 0 &&
+             fileCtrl->HasFilterChoice() &&
+             !fileCtrl->GTKShouldIgnoreNextFilterEvent() )
+        {
+            GenerateFilterChangedEvent( fileCtrl, fileCtrl );
+        }
+    }
+}
+
 // wxGtkFileCtrl implementation
 
 IMPLEMENT_DYNAMIC_CLASS( wxGtkFileCtrl, wxControl )
 
+wxGtkFileCtrl::~wxGtkFileCtrl()
+{
+    if (m_fcWidget)
+        GTKDisconnect(m_fcWidget);
+}
+
 void wxGtkFileCtrl::Init()
 {
-    m_hasFocus = false;
     m_checkNextSelEvent = false;
 
     // ignore the first folder change event which is fired upon startup.
@@ -288,6 +341,7 @@ bool wxGtkFileCtrl::Create( wxWindow *parent,
         gtkAction = GTK_FILE_CHOOSER_ACTION_SAVE;
 
     m_widget =  gtk_alignment_new ( 0, 0, 1, 1 );
+    g_object_ref(m_widget);
     m_fcWidget = GTK_FILE_CHOOSER( gtk_file_chooser_widget_new(gtkAction) );
     gtk_widget_show ( GTK_WIDGET( m_fcWidget ) );
     gtk_container_add ( GTK_CONTAINER ( m_widget ), GTK_WIDGET( m_fcWidget ) );
@@ -306,6 +360,10 @@ bool wxGtkFileCtrl::Create( wxWindow *parent,
                        G_CALLBACK ( gtkfilechooserwidget_selection_changed_callback ),
                        this );
 
+    g_signal_connect ( m_fcWidget, "notify",
+                       G_CALLBACK ( gtkfilechooserwidget_notify_callback ),
+                       this );
+
     m_fc.SetWidget( m_fcWidget );
 
     if ( style & wxFC_MULTIPLE )
@@ -329,7 +387,7 @@ bool wxGtkFileCtrl::Create( wxWindow *parent,
     if ( !dir.empty() )
     {
         gtk_file_chooser_set_current_folder( m_fcWidget,
-                                             dir.fn_str() );
+                                             wxGTK_CONV_FN(dir) );
     }
 
     const wxString fname = fn.GetFullName();
@@ -338,7 +396,7 @@ bool wxGtkFileCtrl::Create( wxWindow *parent,
         if ( !fname.empty() )
         {
             gtk_file_chooser_set_current_name( m_fcWidget,
-                                               fname.fn_str() );
+                                               wxGTK_CONV_FN(fname) );
         }
     }
     else // wxFC_OPEN
@@ -346,7 +404,7 @@ bool wxGtkFileCtrl::Create( wxWindow *parent,
         if ( !fname.empty() )
         {
             gtk_file_chooser_set_filename( m_fcWidget,
-                                           fn.GetFullPath().fn_str() );
+                                           wxGTK_CONV_FN(fn.GetFullPath()) );
         }
     }
 
@@ -417,10 +475,9 @@ void wxGtkFileCtrl::GetFilenames( wxArrayString& files ) const
 
 void wxGtkFileCtrl::ShowHidden(bool show)
 {
-    // gtk_file_chooser_set_show_hidden() is new in 2.6
-    g_object_set (G_OBJECT (m_fcWidget), "show-hidden", show, NULL);
+    gtk_file_chooser_set_show_hidden(m_fcWidget, show);
 }
 
 #endif // wxUSE_FILECTRL
 
-#endif // !defined(__WXUNIVERSAL__)
+#endif // wxUSE_FILECTRL && !defined(__WXUNIVERSAL__)