]>
Commit | Line | Data |
---|---|---|
0cf3e587 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/gtk/filectrl.cpp | |
3 | // Purpose: wxGtkFileCtrl Implementation | |
4 | // Author: Diaa M. Sami | |
5 | // Created: 2007-08-10 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Diaa M. Sami | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
17 | #include "wx/filectrl.h" | |
18 | ||
a82b0593 | 19 | #if wxUSE_FILECTRL && !defined(__WXUNIVERSAL__) |
cc681534 | 20 | |
0cf3e587 VZ |
21 | #ifndef WX_PRECOMP |
22 | # include "wx/sizer.h" | |
23 | # include "wx/debug.h" | |
24 | #endif | |
25 | ||
26 | #include "wx/gtk/private.h" | |
27 | #include "wx/filedlg.h" | |
28 | #include "wx/filename.h" | |
29 | #include "wx/tokenzr.h" | |
30 | ||
31 | //----------------------------------------------------------------------------- | |
32 | // wxGtkFileChooser implementation | |
33 | //----------------------------------------------------------------------------- | |
34 | ||
35 | void wxGtkFileChooser::SetWidget(GtkFileChooser *w) | |
36 | { | |
37 | // check arguments | |
38 | wxASSERT( w ); | |
39 | wxASSERT( GTK_FILE_CHOOSER( w ) ); | |
40 | ||
41 | this->m_widget = w; | |
42 | } | |
43 | ||
44 | wxString wxGtkFileChooser::GetPath() const | |
45 | { | |
46 | wxGtkString str( gtk_file_chooser_get_filename( m_widget ) ); | |
47 | ||
260020e3 PC |
48 | wxString string; |
49 | if (str.c_str() != NULL) | |
b74d7c85 | 50 | string = wxString::FromUTF8(str); |
260020e3 | 51 | return string; |
0cf3e587 VZ |
52 | } |
53 | ||
54 | void wxGtkFileChooser::GetFilenames( wxArrayString& files ) const | |
55 | { | |
56 | GetPaths( files ); | |
57 | for ( size_t n = 0; n < files.GetCount(); ++n ) | |
58 | { | |
59 | const wxFileName file( files[n] ); | |
60 | files[n] = file.GetFullName(); | |
61 | } | |
62 | } | |
63 | ||
64 | void wxGtkFileChooser::GetPaths( wxArrayString& paths ) const | |
65 | { | |
66 | paths.Empty(); | |
67 | if ( gtk_file_chooser_get_select_multiple( m_widget ) ) | |
68 | { | |
69 | GSList *gpathsi = gtk_file_chooser_get_filenames( m_widget ); | |
70 | GSList *gpaths = gpathsi; | |
71 | while ( gpathsi ) | |
72 | { | |
b74d7c85 | 73 | wxString file(wxString::FromUTF8(static_cast<gchar *>(gpathsi->data))); |
0cf3e587 VZ |
74 | paths.Add( file ); |
75 | g_free( gpathsi->data ); | |
76 | gpathsi = gpathsi->next; | |
77 | } | |
78 | ||
79 | g_slist_free( gpaths ); | |
80 | } | |
81 | else | |
82 | paths.Add( GetPath() ); | |
83 | } | |
84 | ||
85 | bool wxGtkFileChooser::SetPath( const wxString& path ) | |
86 | { | |
b74d7c85 VZ |
87 | if ( path.empty() ) |
88 | return true; | |
0cf3e587 | 89 | |
b74d7c85 | 90 | return gtk_file_chooser_set_filename( m_widget, path.utf8_str() ); |
0cf3e587 VZ |
91 | } |
92 | ||
93 | bool wxGtkFileChooser::SetDirectory( const wxString& dir ) | |
94 | { | |
b74d7c85 | 95 | return gtk_file_chooser_set_current_folder( m_widget, dir.utf8_str() ) != 0; |
0cf3e587 VZ |
96 | } |
97 | ||
98 | wxString wxGtkFileChooser::GetDirectory() const | |
99 | { | |
100 | const wxGtkString str( gtk_file_chooser_get_current_folder( m_widget ) ); | |
b74d7c85 | 101 | return wxString::FromUTF8(str); |
0cf3e587 VZ |
102 | } |
103 | ||
104 | wxString wxGtkFileChooser::GetFilename() const | |
105 | { | |
106 | return wxFileName( GetPath() ).GetFullName(); | |
107 | } | |
108 | ||
109 | void wxGtkFileChooser::SetWildcard( const wxString& wildCard ) | |
110 | { | |
20380343 RR |
111 | m_wildcards.Empty(); |
112 | ||
0cf3e587 VZ |
113 | // parse filters |
114 | wxArrayString wildDescriptions, wildFilters; | |
115 | ||
116 | if ( !wxParseCommonDialogsFilter( wildCard, wildDescriptions, wildFilters ) ) | |
117 | { | |
118 | wxFAIL_MSG( wxT( "wxGtkFileChooser::SetWildcard - bad wildcard string" ) ); | |
119 | } | |
120 | else | |
121 | { | |
122 | // Parsing went fine. Set m_wildCard to be returned by wxGtkFileChooserBase::GetWildcard | |
123 | GtkFileChooser* chooser = m_widget; | |
124 | ||
125 | // empty current filter list: | |
126 | GSList* ifilters = gtk_file_chooser_list_filters( chooser ); | |
127 | GSList* filters = ifilters; | |
128 | ||
129 | while ( ifilters ) | |
130 | { | |
131 | gtk_file_chooser_remove_filter( chooser, GTK_FILE_FILTER( ifilters->data ) ); | |
132 | ifilters = ifilters->next; | |
133 | } | |
134 | g_slist_free( filters ); | |
135 | ||
136 | if (!wildCard.empty()) | |
137 | { | |
138 | // add parsed to GtkChooser | |
139 | for ( size_t n = 0; n < wildFilters.GetCount(); ++n ) | |
140 | { | |
141 | GtkFileFilter* filter = gtk_file_filter_new(); | |
142 | ||
143 | gtk_file_filter_set_name( filter, wxGTK_CONV_SYS( wildDescriptions[n] ) ); | |
144 | ||
145 | wxStringTokenizer exttok( wildFilters[n], wxT( ";" ) ); | |
03647350 | 146 | |
81d6a507 | 147 | int n1 = 1; |
0cf3e587 VZ |
148 | while ( exttok.HasMoreTokens() ) |
149 | { | |
150 | wxString token = exttok.GetNextToken(); | |
151 | gtk_file_filter_add_pattern( filter, wxGTK_CONV_SYS( token ) ); | |
03647350 | 152 | |
81d6a507 | 153 | if (n1 == 1) |
20380343 | 154 | m_wildcards.Add( token ); // Only add first pattern to list, used later when saving |
81d6a507 | 155 | n1++; |
0cf3e587 VZ |
156 | } |
157 | ||
158 | gtk_file_chooser_add_filter( chooser, filter ); | |
159 | } | |
160 | ||
161 | // Reset the filter index | |
162 | SetFilterIndex( 0 ); | |
163 | } | |
164 | } | |
165 | } | |
166 | ||
167 | void wxGtkFileChooser::SetFilterIndex( int filterIndex ) | |
168 | { | |
169 | gpointer filter; | |
170 | GtkFileChooser *chooser = m_widget; | |
171 | GSList *filters = gtk_file_chooser_list_filters( chooser ); | |
172 | ||
173 | filter = g_slist_nth_data( filters, filterIndex ); | |
174 | ||
175 | if ( filter != NULL ) | |
176 | { | |
177 | gtk_file_chooser_set_filter( chooser, GTK_FILE_FILTER( filter ) ); | |
178 | } | |
179 | else | |
180 | { | |
181 | wxFAIL_MSG( wxT( "wxGtkFileChooser::SetFilterIndex - bad filter index" ) ); | |
182 | } | |
183 | ||
184 | g_slist_free( filters ); | |
185 | } | |
186 | ||
187 | int wxGtkFileChooser::GetFilterIndex() const | |
188 | { | |
189 | GtkFileChooser *chooser = m_widget; | |
190 | GtkFileFilter *filter = gtk_file_chooser_get_filter( chooser ); | |
191 | GSList *filters = gtk_file_chooser_list_filters( chooser ); | |
192 | const gint index = g_slist_index( filters, filter ); | |
193 | g_slist_free( filters ); | |
194 | ||
195 | if ( index == -1 ) | |
196 | { | |
197 | wxFAIL_MSG( wxT( "wxGtkFileChooser::GetFilterIndex - bad filter index returned by gtk+" ) ); | |
198 | return 0; | |
199 | } | |
200 | else | |
201 | return index; | |
74ab5f5b | 202 | } |
0cf3e587 VZ |
203 | |
204 | //----------------------------------------------------------------------------- | |
205 | // end wxGtkFileChooser Implementation | |
206 | //----------------------------------------------------------------------------- | |
207 | ||
208 | #if wxUSE_FILECTRL | |
209 | ||
210 | // gtk signal handlers | |
211 | ||
212 | extern "C" | |
213 | { | |
214 | static void | |
215 | gtkfilechooserwidget_file_activated_callback( GtkWidget *WXUNUSED( widget ), wxGtkFileCtrl *fileCtrl ) | |
216 | { | |
217 | GenerateFileActivatedEvent( fileCtrl, fileCtrl ); | |
218 | } | |
219 | } | |
220 | ||
221 | extern "C" | |
222 | { | |
223 | static void | |
224 | gtkfilechooserwidget_selection_changed_callback( GtkWidget *WXUNUSED( widget ), wxGtkFileCtrl *fileCtrl ) | |
225 | { | |
226 | // check next selection event and ignore it if it has 0 files | |
227 | // because such events are redundantly generated by gtk. | |
228 | if ( fileCtrl->m_checkNextSelEvent ) | |
229 | { | |
230 | wxArrayString filenames; | |
231 | fileCtrl->GetFilenames( filenames ); | |
232 | ||
233 | if ( filenames.Count() != 0 ) | |
234 | fileCtrl->m_checkNextSelEvent = false; | |
235 | } | |
236 | ||
237 | if ( !fileCtrl->m_checkNextSelEvent ) | |
238 | GenerateSelectionChangedEvent( fileCtrl, fileCtrl ); | |
239 | } | |
240 | } | |
241 | ||
242 | extern "C" | |
243 | { | |
244 | static void | |
245 | gtkfilechooserwidget_folder_changed_callback( GtkWidget *WXUNUSED( widget ), wxGtkFileCtrl *fileCtrl ) | |
246 | { | |
247 | if ( fileCtrl->m_ignoreNextFolderChangeEvent ) | |
248 | { | |
249 | fileCtrl->m_ignoreNextFolderChangeEvent = false; | |
250 | } | |
251 | else | |
252 | { | |
253 | GenerateFolderChangedEvent( fileCtrl, fileCtrl ); | |
254 | } | |
255 | ||
256 | fileCtrl->m_checkNextSelEvent = true; | |
257 | } | |
258 | } | |
259 | ||
260 | // wxGtkFileCtrl implementation | |
261 | ||
262 | IMPLEMENT_DYNAMIC_CLASS( wxGtkFileCtrl, wxControl ) | |
263 | ||
264 | void wxGtkFileCtrl::Init() | |
265 | { | |
0cf3e587 VZ |
266 | m_checkNextSelEvent = false; |
267 | ||
268 | // ignore the first folder change event which is fired upon startup. | |
269 | m_ignoreNextFolderChangeEvent = true; | |
270 | } | |
271 | ||
272 | bool wxGtkFileCtrl::Create( wxWindow *parent, | |
273 | wxWindowID id, | |
274 | const wxString& defaultDirectory, | |
275 | const wxString& defaultFileName, | |
276 | const wxString& wildCard, | |
277 | long style, | |
278 | const wxPoint& pos, | |
279 | const wxSize& size, | |
280 | const wxString& name ) | |
281 | { | |
282 | if ( !PreCreation( parent, pos, size ) || | |
283 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ) ) | |
284 | { | |
285 | wxFAIL_MSG( wxT( "wxGtkFileCtrl creation failed" ) ); | |
286 | return false; | |
287 | } | |
288 | ||
260020e3 | 289 | GtkFileChooserAction gtkAction = GTK_FILE_CHOOSER_ACTION_OPEN; |
0cf3e587 VZ |
290 | |
291 | if ( style & wxFC_SAVE ) | |
0cf3e587 | 292 | gtkAction = GTK_FILE_CHOOSER_ACTION_SAVE; |
0cf3e587 VZ |
293 | |
294 | m_widget = gtk_alignment_new ( 0, 0, 1, 1 ); | |
9ff9d30c | 295 | g_object_ref(m_widget); |
260020e3 | 296 | m_fcWidget = GTK_FILE_CHOOSER( gtk_file_chooser_widget_new(gtkAction) ); |
0cf3e587 VZ |
297 | gtk_widget_show ( GTK_WIDGET( m_fcWidget ) ); |
298 | gtk_container_add ( GTK_CONTAINER ( m_widget ), GTK_WIDGET( m_fcWidget ) ); | |
299 | ||
300 | m_focusWidget = GTK_WIDGET( m_fcWidget ); | |
301 | ||
302 | g_signal_connect ( m_fcWidget, "file-activated", | |
303 | G_CALLBACK ( gtkfilechooserwidget_file_activated_callback ), | |
304 | this ); | |
305 | ||
306 | g_signal_connect ( m_fcWidget, "current-folder-changed", | |
307 | G_CALLBACK ( gtkfilechooserwidget_folder_changed_callback ), | |
308 | this ); | |
309 | ||
310 | g_signal_connect ( m_fcWidget, "selection-changed", | |
311 | G_CALLBACK ( gtkfilechooserwidget_selection_changed_callback ), | |
312 | this ); | |
313 | ||
314 | m_fc.SetWidget( m_fcWidget ); | |
315 | ||
316 | if ( style & wxFC_MULTIPLE ) | |
317 | gtk_file_chooser_set_select_multiple( m_fcWidget, true ); | |
318 | ||
319 | SetWildcard( wildCard ); | |
320 | ||
321 | // if defaultDir is specified it should contain the directory and | |
322 | // defaultFileName should contain the default name of the file, however if | |
323 | // directory is not given, defaultFileName contains both | |
324 | wxFileName fn; | |
325 | if ( defaultDirectory.empty() ) | |
326 | fn.Assign( defaultFileName ); | |
327 | else if ( !defaultFileName.empty() ) | |
328 | fn.Assign( defaultDirectory, defaultFileName ); | |
329 | else | |
330 | fn.AssignDir( defaultDirectory ); | |
331 | ||
332 | // set the initial file name and/or directory | |
333 | const wxString dir = fn.GetPath(); | |
334 | if ( !dir.empty() ) | |
335 | { | |
336 | gtk_file_chooser_set_current_folder( m_fcWidget, | |
337 | dir.fn_str() ); | |
338 | } | |
339 | ||
340 | const wxString fname = fn.GetFullName(); | |
341 | if ( style & wxFC_SAVE ) | |
342 | { | |
343 | if ( !fname.empty() ) | |
344 | { | |
345 | gtk_file_chooser_set_current_name( m_fcWidget, | |
346 | fname.fn_str() ); | |
347 | } | |
348 | } | |
349 | else // wxFC_OPEN | |
350 | { | |
351 | if ( !fname.empty() ) | |
352 | { | |
353 | gtk_file_chooser_set_filename( m_fcWidget, | |
354 | fn.GetFullPath().fn_str() ); | |
355 | } | |
356 | } | |
357 | ||
358 | m_parent->DoAddChild( this ); | |
359 | ||
360 | PostCreation( size ); | |
361 | ||
362 | return TRUE; | |
363 | } | |
364 | ||
365 | bool wxGtkFileCtrl::SetPath( const wxString& path ) | |
366 | { | |
367 | return m_fc.SetPath( path ); | |
368 | } | |
369 | ||
370 | bool wxGtkFileCtrl::SetDirectory( const wxString& dir ) | |
371 | { | |
372 | return m_fc.SetDirectory( dir ); | |
373 | } | |
374 | ||
375 | bool wxGtkFileCtrl::SetFilename( const wxString& name ) | |
376 | { | |
700d08c1 | 377 | if ( HasFlag( wxFC_SAVE ) ) |
0cf3e587 | 378 | { |
700d08c1 RR |
379 | gtk_file_chooser_set_current_name( m_fcWidget, wxGTK_CONV( name ) ); |
380 | return true; | |
0cf3e587 | 381 | } |
700d08c1 RR |
382 | else |
383 | return SetPath( wxFileName( GetDirectory(), name ).GetFullPath() ); | |
0cf3e587 VZ |
384 | } |
385 | ||
386 | void wxGtkFileCtrl::SetWildcard( const wxString& wildCard ) | |
387 | { | |
388 | m_wildCard = wildCard; | |
389 | ||
390 | m_fc.SetWildcard( wildCard ); | |
391 | } | |
392 | ||
393 | void wxGtkFileCtrl::SetFilterIndex( int filterIndex ) | |
394 | { | |
395 | m_fc.SetFilterIndex( filterIndex ); | |
396 | } | |
397 | ||
398 | wxString wxGtkFileCtrl::GetPath() const | |
399 | { | |
400 | return m_fc.GetPath(); | |
401 | } | |
402 | ||
403 | void wxGtkFileCtrl::GetPaths( wxArrayString& paths ) const | |
404 | { | |
405 | m_fc.GetPaths( paths ); | |
406 | } | |
407 | ||
408 | wxString wxGtkFileCtrl::GetDirectory() const | |
409 | { | |
410 | return m_fc.GetDirectory(); | |
411 | } | |
412 | ||
413 | wxString wxGtkFileCtrl::GetFilename() const | |
414 | { | |
415 | return m_fc.GetFilename(); | |
416 | } | |
417 | ||
418 | void wxGtkFileCtrl::GetFilenames( wxArrayString& files ) const | |
419 | { | |
420 | m_fc.GetFilenames( files ); | |
421 | } | |
422 | ||
260020e3 | 423 | void wxGtkFileCtrl::ShowHidden(bool show) |
0cf3e587 | 424 | { |
700d08c1 | 425 | // gtk_file_chooser_set_show_hidden() is new in 2.6 |
33a56604 | 426 | g_object_set (G_OBJECT (m_fcWidget), "show-hidden", show, NULL); |
0cf3e587 VZ |
427 | } |
428 | ||
ff654490 | 429 | #endif // wxUSE_FILECTRL |
cc681534 | 430 | |
a82b0593 | 431 | #endif // wxUSE_FILECTRL && !defined(__WXUNIVERSAL__) |