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
)
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
;
132 gtk_file_chooser_remove_filter( chooser
, GTK_FILE_FILTER( ifilters
->data
) );
133 ifilters
= ifilters
->next
;
135 g_slist_free( filters
);
137 if (!wildCard
.empty())
139 // add parsed to GtkChooser
140 for ( size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
142 GtkFileFilter
* filter
= gtk_file_filter_new();
144 gtk_file_filter_set_name( filter
, wxGTK_CONV_SYS( wildDescriptions
[n
] ) );
146 wxStringTokenizer
exttok( wildFilters
[n
], wxT( ";" ) );
147 while ( exttok
.HasMoreTokens() )
149 wxString token
= exttok
.GetNextToken();
150 gtk_file_filter_add_pattern( filter
, wxGTK_CONV_SYS( token
) );
153 gtk_file_chooser_add_filter( chooser
, filter
);
156 // Reset the filter index
162 void wxGtkFileChooser::SetFilterIndex( int filterIndex
)
165 GtkFileChooser
*chooser
= m_widget
;
166 GSList
*filters
= gtk_file_chooser_list_filters( chooser
);
168 filter
= g_slist_nth_data( filters
, filterIndex
);
170 if ( filter
!= NULL
)
172 gtk_file_chooser_set_filter( chooser
, GTK_FILE_FILTER( filter
) );
176 wxFAIL_MSG( wxT( "wxGtkFileChooser::SetFilterIndex - bad filter index" ) );
179 g_slist_free( filters
);
182 int wxGtkFileChooser::GetFilterIndex() const
184 GtkFileChooser
*chooser
= m_widget
;
185 GtkFileFilter
*filter
= gtk_file_chooser_get_filter( chooser
);
186 GSList
*filters
= gtk_file_chooser_list_filters( chooser
);
187 const gint index
= g_slist_index( filters
, filter
);
188 g_slist_free( filters
);
192 wxFAIL_MSG( wxT( "wxGtkFileChooser::GetFilterIndex - bad filter index returned by gtk+" ) );
199 //-----------------------------------------------------------------------------
200 // end wxGtkFileChooser Implementation
201 //-----------------------------------------------------------------------------
205 // gtk signal handlers
210 gtkfilechooserwidget_file_activated_callback( GtkWidget
*WXUNUSED( widget
), wxGtkFileCtrl
*fileCtrl
)
212 GenerateFileActivatedEvent( fileCtrl
, fileCtrl
);
219 gtkfilechooserwidget_selection_changed_callback( GtkWidget
*WXUNUSED( widget
), wxGtkFileCtrl
*fileCtrl
)
221 // check next selection event and ignore it if it has 0 files
222 // because such events are redundantly generated by gtk.
223 if ( fileCtrl
->m_checkNextSelEvent
)
225 wxArrayString filenames
;
226 fileCtrl
->GetFilenames( filenames
);
228 if ( filenames
.Count() != 0 )
229 fileCtrl
->m_checkNextSelEvent
= false;
232 if ( !fileCtrl
->m_checkNextSelEvent
)
233 GenerateSelectionChangedEvent( fileCtrl
, fileCtrl
);
240 gtkfilechooserwidget_folder_changed_callback( GtkWidget
*WXUNUSED( widget
), wxGtkFileCtrl
*fileCtrl
)
242 if ( fileCtrl
->m_ignoreNextFolderChangeEvent
)
244 fileCtrl
->m_ignoreNextFolderChangeEvent
= false;
248 GenerateFolderChangedEvent( fileCtrl
, fileCtrl
);
251 fileCtrl
->m_checkNextSelEvent
= true;
255 // wxGtkFileCtrl implementation
257 IMPLEMENT_DYNAMIC_CLASS( wxGtkFileCtrl
, wxControl
)
259 void wxGtkFileCtrl::Init()
262 m_checkNextSelEvent
= false;
264 // ignore the first folder change event which is fired upon startup.
265 m_ignoreNextFolderChangeEvent
= true;
268 bool wxGtkFileCtrl::Create( wxWindow
*parent
,
270 const wxString
& defaultDirectory
,
271 const wxString
& defaultFileName
,
272 const wxString
& wildCard
,
276 const wxString
& name
)
278 if ( !PreCreation( parent
, pos
, size
) ||
279 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
281 wxFAIL_MSG( wxT( "wxGtkFileCtrl creation failed" ) );
285 GtkFileChooserAction gtkAction
= GTK_FILE_CHOOSER_ACTION_OPEN
;
287 if ( style
& wxFC_SAVE
)
288 gtkAction
= GTK_FILE_CHOOSER_ACTION_SAVE
;
290 m_widget
= gtk_alignment_new ( 0, 0, 1, 1 );
291 m_fcWidget
= GTK_FILE_CHOOSER( gtk_file_chooser_widget_new(gtkAction
) );
292 gtk_widget_show ( GTK_WIDGET( m_fcWidget
) );
293 gtk_container_add ( GTK_CONTAINER ( m_widget
), GTK_WIDGET( m_fcWidget
) );
295 m_focusWidget
= GTK_WIDGET( m_fcWidget
);
297 g_signal_connect ( m_fcWidget
, "file-activated",
298 G_CALLBACK ( gtkfilechooserwidget_file_activated_callback
),
301 g_signal_connect ( m_fcWidget
, "current-folder-changed",
302 G_CALLBACK ( gtkfilechooserwidget_folder_changed_callback
),
305 g_signal_connect ( m_fcWidget
, "selection-changed",
306 G_CALLBACK ( gtkfilechooserwidget_selection_changed_callback
),
309 m_fc
.SetWidget( m_fcWidget
);
311 if ( style
& wxFC_MULTIPLE
)
312 gtk_file_chooser_set_select_multiple( m_fcWidget
, true );
314 SetWildcard( wildCard
);
316 // if defaultDir is specified it should contain the directory and
317 // defaultFileName should contain the default name of the file, however if
318 // directory is not given, defaultFileName contains both
320 if ( defaultDirectory
.empty() )
321 fn
.Assign( defaultFileName
);
322 else if ( !defaultFileName
.empty() )
323 fn
.Assign( defaultDirectory
, defaultFileName
);
325 fn
.AssignDir( defaultDirectory
);
327 // set the initial file name and/or directory
328 const wxString dir
= fn
.GetPath();
331 gtk_file_chooser_set_current_folder( m_fcWidget
,
335 const wxString fname
= fn
.GetFullName();
336 if ( style
& wxFC_SAVE
)
338 if ( !fname
.empty() )
340 gtk_file_chooser_set_current_name( m_fcWidget
,
346 if ( !fname
.empty() )
348 gtk_file_chooser_set_filename( m_fcWidget
,
349 fn
.GetFullPath().fn_str() );
353 m_parent
->DoAddChild( this );
355 PostCreation( size
);
360 bool wxGtkFileCtrl::SetPath( const wxString
& path
)
362 return m_fc
.SetPath( path
);
365 bool wxGtkFileCtrl::SetDirectory( const wxString
& dir
)
367 return m_fc
.SetDirectory( dir
);
370 bool wxGtkFileCtrl::SetFilename( const wxString
& name
)
372 if ( HasFlag( wxFC_SAVE
) )
374 gtk_file_chooser_set_current_name( m_fcWidget
, wxGTK_CONV( name
) );
378 return SetPath( wxFileName( GetDirectory(), name
).GetFullPath() );
381 void wxGtkFileCtrl::SetWildcard( const wxString
& wildCard
)
383 m_wildCard
= wildCard
;
385 m_fc
.SetWildcard( wildCard
);
388 void wxGtkFileCtrl::SetFilterIndex( int filterIndex
)
390 m_fc
.SetFilterIndex( filterIndex
);
393 wxString
wxGtkFileCtrl::GetPath() const
395 return m_fc
.GetPath();
398 void wxGtkFileCtrl::GetPaths( wxArrayString
& paths
) const
400 m_fc
.GetPaths( paths
);
403 wxString
wxGtkFileCtrl::GetDirectory() const
405 return m_fc
.GetDirectory();
408 wxString
wxGtkFileCtrl::GetFilename() const
410 return m_fc
.GetFilename();
413 void wxGtkFileCtrl::GetFilenames( wxArrayString
& files
) const
415 m_fc
.GetFilenames( files
);
418 void wxGtkFileCtrl::ShowHidden(bool show
)
420 // gtk_file_chooser_set_show_hidden() is new in 2.6
421 g_object_set (G_OBJECT (m_fcWidget
), "show-hidden", show
, NULL
);
424 #endif // wxUSE_FILECTRL
426 #endif // wxUSE_FILECTRL && !defined(__WXUNIVERSAL__)