1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/filectrl.cpp
3 // Purpose: wxGtkFileCtrl Implementation
4 // Author: Diaa M. Sami
7 // Copyright: (c) Diaa M. Sami
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
17 #include "wx/filectrl.h"
19 #if wxUSE_FILECTRL && !defined(__WXUNIVERSAL__)
22 # include "wx/sizer.h"
23 # include "wx/debug.h"
26 #include "wx/gtk/private.h"
27 #include "wx/filedlg.h"
28 #include "wx/filename.h"
29 #include "wx/tokenzr.h"
31 //-----------------------------------------------------------------------------
32 // wxGtkFileChooser implementation
33 //-----------------------------------------------------------------------------
35 void wxGtkFileChooser::SetWidget(GtkFileChooser *w)
39 wxASSERT( GTK_FILE_CHOOSER( w ) );
44 wxString wxGtkFileChooser::GetPath() const
46 wxGtkString str( gtk_file_chooser_get_filename( m_widget ) );
49 if (str.c_str() != NULL)
50 string = wxConvFileName->cMB2WX(str);
54 void wxGtkFileChooser::GetFilenames( wxArrayString& files ) const
57 for ( size_t n = 0; n < files.GetCount(); ++n )
59 const wxFileName file( files[n] );
60 files[n] = file.GetFullName();
64 void wxGtkFileChooser::GetPaths( wxArrayString& paths ) const
67 if ( gtk_file_chooser_get_select_multiple( m_widget ) )
69 GSList *gpathsi = gtk_file_chooser_get_filenames( m_widget );
70 GSList *gpaths = gpathsi;
73 wxString file( wxConvFileName->cMB2WX( ( gchar* ) gpathsi->data ) );
75 g_free( gpathsi->data );
76 gpathsi = gpathsi->next;
79 g_slist_free( gpaths );
82 paths.Add( GetPath() );
85 bool wxGtkFileChooser::SetPath( const wxString& path )
87 if ( path.empty() ) return true;
89 return gtk_file_chooser_set_filename( m_widget,
90 wxConvFileName->cWX2MB( path.c_str() ) );
93 bool wxGtkFileChooser::SetDirectory( const wxString& dir )
96 gtk_file_chooser_set_current_folder( m_widget,
97 wxConvFileName->cWX2MB( dir.c_str() ) );
101 wxString wxGtkFileChooser::GetDirectory() const
103 const wxGtkString str( gtk_file_chooser_get_current_folder( m_widget ) );
104 return wxString( str, *wxConvFileName );
107 wxString wxGtkFileChooser::GetFilename() const
109 return wxFileName( GetPath() ).GetFullName();
112 void wxGtkFileChooser::SetWildcard( const wxString& wildCard )
117 wxArrayString wildDescriptions, wildFilters;
119 if ( !wxParseCommonDialogsFilter( wildCard, wildDescriptions, wildFilters ) )
121 wxFAIL_MSG( wxT( "wxGtkFileChooser::SetWildcard - bad wildcard string" ) );
125 // Parsing went fine. Set m_wildCard to be returned by wxGtkFileChooserBase::GetWildcard
126 GtkFileChooser* chooser = m_widget;
128 // empty current filter list:
129 GSList* ifilters = gtk_file_chooser_list_filters( chooser );
130 GSList* filters = ifilters;
134 gtk_file_chooser_remove_filter( chooser, GTK_FILE_FILTER( ifilters->data ) );
135 ifilters = ifilters->next;
137 g_slist_free( filters );
139 if (!wildCard.empty())
141 // add parsed to GtkChooser
142 for ( size_t n = 0; n < wildFilters.GetCount(); ++n )
144 GtkFileFilter* filter = gtk_file_filter_new();
146 gtk_file_filter_set_name( filter, wxGTK_CONV_SYS( wildDescriptions[n] ) );
148 wxStringTokenizer exttok( wildFilters[n], wxT( ";" ) );
151 while ( exttok.HasMoreTokens() )
153 wxString token = exttok.GetNextToken();
154 gtk_file_filter_add_pattern( filter, wxGTK_CONV_SYS( token ) );
157 m_wildcards.Add( token ); // Only add first pattern to list, used later when saving
161 gtk_file_chooser_add_filter( chooser, filter );
164 // Reset the filter index
170 void wxGtkFileChooser::SetFilterIndex( int filterIndex )
173 GtkFileChooser *chooser = m_widget;
174 GSList *filters = gtk_file_chooser_list_filters( chooser );
176 filter = g_slist_nth_data( filters, filterIndex );
178 if ( filter != NULL )
180 gtk_file_chooser_set_filter( chooser, GTK_FILE_FILTER( filter ) );
184 wxFAIL_MSG( wxT( "wxGtkFileChooser::SetFilterIndex - bad filter index" ) );
187 g_slist_free( filters );
190 int wxGtkFileChooser::GetFilterIndex() const
192 GtkFileChooser *chooser = m_widget;
193 GtkFileFilter *filter = gtk_file_chooser_get_filter( chooser );
194 GSList *filters = gtk_file_chooser_list_filters( chooser );
195 const gint index = g_slist_index( filters, filter );
196 g_slist_free( filters );
200 wxFAIL_MSG( wxT( "wxGtkFileChooser::GetFilterIndex - bad filter index returned by gtk+" ) );
207 //-----------------------------------------------------------------------------
208 // end wxGtkFileChooser Implementation
209 //-----------------------------------------------------------------------------
213 // gtk signal handlers
218 gtkfilechooserwidget_file_activated_callback( GtkWidget *WXUNUSED( widget ), wxGtkFileCtrl *fileCtrl )
220 GenerateFileActivatedEvent( fileCtrl, fileCtrl );
227 gtkfilechooserwidget_selection_changed_callback( GtkWidget *WXUNUSED( widget ), wxGtkFileCtrl *fileCtrl )
229 // check next selection event and ignore it if it has 0 files
230 // because such events are redundantly generated by gtk.
231 if ( fileCtrl->m_checkNextSelEvent )
233 wxArrayString filenames;
234 fileCtrl->GetFilenames( filenames );
236 if ( filenames.Count() != 0 )
237 fileCtrl->m_checkNextSelEvent = false;
240 if ( !fileCtrl->m_checkNextSelEvent )
241 GenerateSelectionChangedEvent( fileCtrl, fileCtrl );
248 gtkfilechooserwidget_folder_changed_callback( GtkWidget *WXUNUSED( widget ), wxGtkFileCtrl *fileCtrl )
250 if ( fileCtrl->m_ignoreNextFolderChangeEvent )
252 fileCtrl->m_ignoreNextFolderChangeEvent = false;
256 GenerateFolderChangedEvent( fileCtrl, fileCtrl );
259 fileCtrl->m_checkNextSelEvent = true;
263 // wxGtkFileCtrl implementation
265 IMPLEMENT_DYNAMIC_CLASS( wxGtkFileCtrl, wxControl )
267 void wxGtkFileCtrl::Init()
269 m_checkNextSelEvent = false;
271 // ignore the first folder change event which is fired upon startup.
272 m_ignoreNextFolderChangeEvent = true;
275 bool wxGtkFileCtrl::Create( wxWindow *parent,
277 const wxString& defaultDirectory,
278 const wxString& defaultFileName,
279 const wxString& wildCard,
283 const wxString& name )
285 if ( !PreCreation( parent, pos, size ) ||
286 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ) )
288 wxFAIL_MSG( wxT( "wxGtkFileCtrl creation failed" ) );
292 GtkFileChooserAction gtkAction = GTK_FILE_CHOOSER_ACTION_OPEN;
294 if ( style & wxFC_SAVE )
295 gtkAction = GTK_FILE_CHOOSER_ACTION_SAVE;
297 m_widget = gtk_alignment_new ( 0, 0, 1, 1 );
298 g_object_ref(m_widget);
299 m_fcWidget = GTK_FILE_CHOOSER( gtk_file_chooser_widget_new(gtkAction) );
300 gtk_widget_show ( GTK_WIDGET( m_fcWidget ) );
301 gtk_container_add ( GTK_CONTAINER ( m_widget ), GTK_WIDGET( m_fcWidget ) );
303 m_focusWidget = GTK_WIDGET( m_fcWidget );
305 g_signal_connect ( m_fcWidget, "file-activated",
306 G_CALLBACK ( gtkfilechooserwidget_file_activated_callback ),
309 g_signal_connect ( m_fcWidget, "current-folder-changed",
310 G_CALLBACK ( gtkfilechooserwidget_folder_changed_callback ),
313 g_signal_connect ( m_fcWidget, "selection-changed",
314 G_CALLBACK ( gtkfilechooserwidget_selection_changed_callback ),
317 m_fc.SetWidget( m_fcWidget );
319 if ( style & wxFC_MULTIPLE )
320 gtk_file_chooser_set_select_multiple( m_fcWidget, true );
322 SetWildcard( wildCard );
324 // if defaultDir is specified it should contain the directory and
325 // defaultFileName should contain the default name of the file, however if
326 // directory is not given, defaultFileName contains both
328 if ( defaultDirectory.empty() )
329 fn.Assign( defaultFileName );
330 else if ( !defaultFileName.empty() )
331 fn.Assign( defaultDirectory, defaultFileName );
333 fn.AssignDir( defaultDirectory );
335 // set the initial file name and/or directory
336 const wxString dir = fn.GetPath();
339 gtk_file_chooser_set_current_folder( m_fcWidget,
343 const wxString fname = fn.GetFullName();
344 if ( style & wxFC_SAVE )
346 if ( !fname.empty() )
348 gtk_file_chooser_set_current_name( m_fcWidget,
354 if ( !fname.empty() )
356 gtk_file_chooser_set_filename( m_fcWidget,
357 fn.GetFullPath().fn_str() );
361 m_parent->DoAddChild( this );
363 PostCreation( size );
368 bool wxGtkFileCtrl::SetPath( const wxString& path )
370 return m_fc.SetPath( path );
373 bool wxGtkFileCtrl::SetDirectory( const wxString& dir )
375 return m_fc.SetDirectory( dir );
378 bool wxGtkFileCtrl::SetFilename( const wxString& name )
380 if ( HasFlag( wxFC_SAVE ) )
382 gtk_file_chooser_set_current_name( m_fcWidget, wxGTK_CONV( name ) );
386 return SetPath( wxFileName( GetDirectory(), name ).GetFullPath() );
389 void wxGtkFileCtrl::SetWildcard( const wxString& wildCard )
391 m_wildCard = wildCard;
393 m_fc.SetWildcard( wildCard );
396 void wxGtkFileCtrl::SetFilterIndex( int filterIndex )
398 m_fc.SetFilterIndex( filterIndex );
401 wxString wxGtkFileCtrl::GetPath() const
403 return m_fc.GetPath();
406 void wxGtkFileCtrl::GetPaths( wxArrayString& paths ) const
408 m_fc.GetPaths( paths );
411 wxString wxGtkFileCtrl::GetDirectory() const
413 return m_fc.GetDirectory();
416 wxString wxGtkFileCtrl::GetFilename() const
418 return m_fc.GetFilename();
421 void wxGtkFileCtrl::GetFilenames( wxArrayString& files ) const
423 m_fc.GetFilenames( files );
426 void wxGtkFileCtrl::ShowHidden(bool show)
428 // gtk_file_chooser_set_show_hidden() is new in 2.6
429 g_object_set (G_OBJECT (m_fcWidget), "show-hidden", show, NULL);
432 #endif // wxUSE_FILECTRL
434 #endif // wxUSE_FILECTRL && !defined(__WXUNIVERSAL__)