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/scopeguard.h"
30 #include "wx/tokenzr.h"
32 //-----------------------------------------------------------------------------
33 // wxGtkFileChooser implementation
34 //-----------------------------------------------------------------------------
36 void wxGtkFileChooser::SetWidget(GtkFileChooser
*w
)
40 wxASSERT( GTK_FILE_CHOOSER( w
) );
45 wxString
wxGtkFileChooser::GetPath() const
47 wxGtkString
str( gtk_file_chooser_get_filename( m_widget
) );
50 if (str
.c_str() != NULL
)
51 string
= wxString::FromUTF8(str
);
55 void wxGtkFileChooser::GetFilenames( wxArrayString
& files
) const
58 for ( size_t n
= 0; n
< files
.GetCount(); ++n
)
60 const wxFileName
file( files
[n
] );
61 files
[n
] = file
.GetFullName();
65 void wxGtkFileChooser::GetPaths( wxArrayString
& paths
) const
68 if ( gtk_file_chooser_get_select_multiple( m_widget
) )
70 GSList
*gpathsi
= gtk_file_chooser_get_filenames( m_widget
);
71 GSList
*gpaths
= gpathsi
;
74 wxString
file(wxString::FromUTF8(static_cast<gchar
*>(gpathsi
->data
)));
76 g_free( gpathsi
->data
);
77 gpathsi
= gpathsi
->next
;
80 g_slist_free( gpaths
);
83 paths
.Add( GetPath() );
86 bool wxGtkFileChooser::SetPath( const wxString
& path
)
91 return gtk_file_chooser_set_filename( m_widget
, path
.utf8_str() );
94 bool wxGtkFileChooser::SetDirectory( const wxString
& dir
)
96 return gtk_file_chooser_set_current_folder( m_widget
, dir
.utf8_str() ) != 0;
99 wxString
wxGtkFileChooser::GetDirectory() const
101 const wxGtkString
str( gtk_file_chooser_get_current_folder( m_widget
) );
102 return wxString::FromUTF8(str
);
105 wxString
wxGtkFileChooser::GetFilename() const
107 return wxFileName( GetPath() ).GetFullName();
110 void wxGtkFileChooser::SetWildcard( const wxString
& wildCard
)
115 wxArrayString wildDescriptions
, wildFilters
;
117 if ( !wxParseCommonDialogsFilter( wildCard
, wildDescriptions
, wildFilters
) )
119 wxFAIL_MSG( wxT( "wxGtkFileChooser::SetWildcard - bad wildcard string" ) );
123 // Parsing went fine. Set m_wildCard to be returned by wxGtkFileChooserBase::GetWildcard
124 GtkFileChooser
* chooser
= m_widget
;
126 // empty current filter list:
127 GSList
* ifilters
= gtk_file_chooser_list_filters( chooser
);
128 GSList
* filters
= ifilters
;
130 m_ignoreNextFilterEvent
= true;
131 wxON_BLOCK_EXIT_SET(m_ignoreNextFilterEvent
, false);
135 gtk_file_chooser_remove_filter( chooser
, GTK_FILE_FILTER( ifilters
->data
) );
136 ifilters
= ifilters
->next
;
138 g_slist_free( filters
);
140 if (!wildCard
.empty())
142 // add parsed to GtkChooser
143 for ( size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
145 GtkFileFilter
* filter
= gtk_file_filter_new();
147 gtk_file_filter_set_name( filter
, wxGTK_CONV_SYS( wildDescriptions
[n
] ) );
149 wxStringTokenizer
exttok( wildFilters
[n
], wxT( ";" ) );
152 while ( exttok
.HasMoreTokens() )
154 wxString token
= exttok
.GetNextToken();
155 gtk_file_filter_add_pattern( filter
, wxGTK_CONV_SYS( token
) );
158 m_wildcards
.Add( token
); // Only add first pattern to list, used later when saving
162 gtk_file_chooser_add_filter( chooser
, filter
);
165 // Reset the filter index
171 void wxGtkFileChooser::SetFilterIndex( int filterIndex
)
174 GtkFileChooser
*chooser
= m_widget
;
175 GSList
*filters
= gtk_file_chooser_list_filters( chooser
);
177 filter
= g_slist_nth_data( filters
, filterIndex
);
179 if ( filter
!= NULL
)
181 gtk_file_chooser_set_filter( chooser
, GTK_FILE_FILTER( filter
) );
185 wxFAIL_MSG( wxT( "wxGtkFileChooser::SetFilterIndex - bad filter index" ) );
188 g_slist_free( filters
);
191 int wxGtkFileChooser::GetFilterIndex() const
193 GtkFileChooser
*chooser
= m_widget
;
194 GtkFileFilter
*filter
= gtk_file_chooser_get_filter( chooser
);
195 GSList
*filters
= gtk_file_chooser_list_filters( chooser
);
196 const gint index
= g_slist_index( filters
, filter
);
197 g_slist_free( filters
);
201 wxFAIL_MSG( wxT( "wxGtkFileChooser::GetFilterIndex - bad filter index returned by gtk+" ) );
208 bool wxGtkFileChooser::HasFilterChoice() const
210 return gtk_file_chooser_get_filter( m_widget
) != NULL
;
213 //-----------------------------------------------------------------------------
214 // end wxGtkFileChooser Implementation
215 //-----------------------------------------------------------------------------
219 // gtk signal handlers
224 gtkfilechooserwidget_file_activated_callback( GtkWidget
*WXUNUSED( widget
), wxGtkFileCtrl
*fileCtrl
)
226 GenerateFileActivatedEvent( fileCtrl
, fileCtrl
);
233 gtkfilechooserwidget_selection_changed_callback( GtkWidget
*WXUNUSED( widget
), wxGtkFileCtrl
*fileCtrl
)
235 // check next selection event and ignore it if it has 0 files
236 // because such events are redundantly generated by gtk.
237 if ( fileCtrl
->m_checkNextSelEvent
)
239 wxArrayString filenames
;
240 fileCtrl
->GetFilenames( filenames
);
242 if ( filenames
.Count() != 0 )
243 fileCtrl
->m_checkNextSelEvent
= false;
246 if ( !fileCtrl
->m_checkNextSelEvent
)
247 GenerateSelectionChangedEvent( fileCtrl
, fileCtrl
);
254 gtkfilechooserwidget_folder_changed_callback( GtkWidget
*WXUNUSED( widget
), wxGtkFileCtrl
*fileCtrl
)
256 if ( fileCtrl
->m_ignoreNextFolderChangeEvent
)
258 fileCtrl
->m_ignoreNextFolderChangeEvent
= false;
262 GenerateFolderChangedEvent( fileCtrl
, fileCtrl
);
265 fileCtrl
->m_checkNextSelEvent
= true;
272 gtkfilechooserwidget_notify_callback( GObject
*WXUNUSED( gobject
), GParamSpec
*arg1
, wxGtkFileCtrl
*fileCtrl
)
274 const char *name
= g_param_spec_get_name (arg1
);
275 if ( strcmp( name
, "filter" ) == 0 &&
276 fileCtrl
->HasFilterChoice() &&
277 !fileCtrl
->GTKShouldIgnoreNextFilterEvent() )
279 GenerateFilterChangedEvent( fileCtrl
, fileCtrl
);
284 // wxGtkFileCtrl implementation
286 IMPLEMENT_DYNAMIC_CLASS( wxGtkFileCtrl
, wxControl
)
288 void wxGtkFileCtrl::Init()
290 m_checkNextSelEvent
= false;
292 // ignore the first folder change event which is fired upon startup.
293 m_ignoreNextFolderChangeEvent
= true;
296 bool wxGtkFileCtrl::Create( wxWindow
*parent
,
298 const wxString
& defaultDirectory
,
299 const wxString
& defaultFileName
,
300 const wxString
& wildCard
,
304 const wxString
& name
)
306 if ( !PreCreation( parent
, pos
, size
) ||
307 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
309 wxFAIL_MSG( wxT( "wxGtkFileCtrl creation failed" ) );
313 GtkFileChooserAction gtkAction
= GTK_FILE_CHOOSER_ACTION_OPEN
;
315 if ( style
& wxFC_SAVE
)
316 gtkAction
= GTK_FILE_CHOOSER_ACTION_SAVE
;
318 m_widget
= gtk_alignment_new ( 0, 0, 1, 1 );
319 g_object_ref(m_widget
);
320 m_fcWidget
= GTK_FILE_CHOOSER( gtk_file_chooser_widget_new(gtkAction
) );
321 gtk_widget_show ( GTK_WIDGET( m_fcWidget
) );
322 gtk_container_add ( GTK_CONTAINER ( m_widget
), GTK_WIDGET( m_fcWidget
) );
324 m_focusWidget
= GTK_WIDGET( m_fcWidget
);
326 g_signal_connect ( m_fcWidget
, "file-activated",
327 G_CALLBACK ( gtkfilechooserwidget_file_activated_callback
),
330 g_signal_connect ( m_fcWidget
, "current-folder-changed",
331 G_CALLBACK ( gtkfilechooserwidget_folder_changed_callback
),
334 g_signal_connect ( m_fcWidget
, "selection-changed",
335 G_CALLBACK ( gtkfilechooserwidget_selection_changed_callback
),
338 g_signal_connect ( m_fcWidget
, "notify",
339 G_CALLBACK ( gtkfilechooserwidget_notify_callback
),
342 m_fc
.SetWidget( m_fcWidget
);
344 if ( style
& wxFC_MULTIPLE
)
345 gtk_file_chooser_set_select_multiple( m_fcWidget
, true );
347 SetWildcard( wildCard
);
349 // if defaultDir is specified it should contain the directory and
350 // defaultFileName should contain the default name of the file, however if
351 // directory is not given, defaultFileName contains both
353 if ( defaultDirectory
.empty() )
354 fn
.Assign( defaultFileName
);
355 else if ( !defaultFileName
.empty() )
356 fn
.Assign( defaultDirectory
, defaultFileName
);
358 fn
.AssignDir( defaultDirectory
);
360 // set the initial file name and/or directory
361 const wxString dir
= fn
.GetPath();
364 gtk_file_chooser_set_current_folder( m_fcWidget
,
365 wxGTK_CONV_FN(dir
) );
368 const wxString fname
= fn
.GetFullName();
369 if ( style
& wxFC_SAVE
)
371 if ( !fname
.empty() )
373 gtk_file_chooser_set_current_name( m_fcWidget
,
374 wxGTK_CONV_FN(fname
) );
379 if ( !fname
.empty() )
381 gtk_file_chooser_set_filename( m_fcWidget
,
382 wxGTK_CONV_FN(fn
.GetFullPath()) );
386 m_parent
->DoAddChild( this );
388 PostCreation( size
);
393 bool wxGtkFileCtrl::SetPath( const wxString
& path
)
395 return m_fc
.SetPath( path
);
398 bool wxGtkFileCtrl::SetDirectory( const wxString
& dir
)
400 return m_fc
.SetDirectory( dir
);
403 bool wxGtkFileCtrl::SetFilename( const wxString
& name
)
405 if ( HasFlag( wxFC_SAVE
) )
407 gtk_file_chooser_set_current_name( m_fcWidget
, wxGTK_CONV( name
) );
411 return SetPath( wxFileName( GetDirectory(), name
).GetFullPath() );
414 void wxGtkFileCtrl::SetWildcard( const wxString
& wildCard
)
416 m_wildCard
= wildCard
;
418 m_fc
.SetWildcard( wildCard
);
421 void wxGtkFileCtrl::SetFilterIndex( int filterIndex
)
423 m_fc
.SetFilterIndex( filterIndex
);
426 wxString
wxGtkFileCtrl::GetPath() const
428 return m_fc
.GetPath();
431 void wxGtkFileCtrl::GetPaths( wxArrayString
& paths
) const
433 m_fc
.GetPaths( paths
);
436 wxString
wxGtkFileCtrl::GetDirectory() const
438 return m_fc
.GetDirectory();
441 wxString
wxGtkFileCtrl::GetFilename() const
443 return m_fc
.GetFilename();
446 void wxGtkFileCtrl::GetFilenames( wxArrayString
& files
) const
448 m_fc
.GetFilenames( files
);
451 void wxGtkFileCtrl::ShowHidden(bool show
)
453 // gtk_file_chooser_set_show_hidden() is new in 2.6
454 g_object_set (G_OBJECT (m_fcWidget
), "show-hidden", show
, NULL
);
457 #endif // wxUSE_FILECTRL
459 #endif // wxUSE_FILECTRL && !defined(__WXUNIVERSAL__)