Deprecate wxPathExists, make wxDirExists used everywhere, minor source cleaning.
[wxWidgets.git] / src / gtk / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/filedlg.cpp
3 // Purpose: native implementation of wxFileDialog
4 // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "filedlggtk.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 // Include setup.h to get wxUSE flags for compilers that do not support precompilation of headers
18 #include "wx/setup.h"
19
20 #if wxUSE_FILEDLG
21
22 #include "wx/filedlg.h"
23
24 #ifdef __WXGTK24__
25
26 #include <gtk/gtk.h>
27 #include "wx/gtk/private.h"
28
29 #include <unistd.h> // chdir
30
31 #include "wx/intl.h"
32 #include "wx/filename.h" // wxFilename
33 #include "wx/tokenzr.h" // wxStringTokenizer
34 #include "wx/filefn.h" // ::wxGetCwd
35 #include "wx/msgdlg.h" // wxMessageDialog
36
37 //-----------------------------------------------------------------------------
38 // idle system
39 //-----------------------------------------------------------------------------
40
41 extern void wxapp_install_idle_handler();
42 extern bool g_isIdle;
43
44 //-----------------------------------------------------------------------------
45 // "clicked" for OK-button
46 //-----------------------------------------------------------------------------
47
48 extern "C" {
49 static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
50 {
51 int style = dialog->GetStyle();
52 gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
53
54 if ((style & wxSAVE) && (style & wxOVERWRITE_PROMPT))
55 {
56 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
57 {
58 wxString msg;
59
60 msg.Printf(
61 _("File '%s' already exists, do you really want to overwrite it?"),
62 wxString(wxConvFileName->cMB2WX(filename)).c_str());
63
64 wxMessageDialog dlg(dialog, msg, _("Confirm"),
65 wxYES_NO | wxICON_QUESTION);
66 if (dlg.ShowModal() != wxID_YES)
67 return;
68 }
69 }
70
71 // change to the directory where the user went if asked
72 if (style & wxCHANGE_DIR)
73 {
74 // Use chdir to not care about filename encodings
75 gchar* folder = g_path_get_dirname(filename);
76 chdir(folder);
77 g_free(folder);
78 }
79
80 g_free(filename);
81
82 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
83 event.SetEventObject(dialog);
84 dialog->GetEventHandler()->ProcessEvent(event);
85 }
86 }
87
88 //-----------------------------------------------------------------------------
89 // "clicked" for Cancel-button
90 //-----------------------------------------------------------------------------
91
92 extern "C" {
93 static void gtk_filedialog_cancel_callback(GtkWidget *WXUNUSED(w),
94 wxFileDialog *dialog)
95 {
96 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
97 event.SetEventObject(dialog);
98 dialog->GetEventHandler()->ProcessEvent(event);
99 }
100 }
101
102 extern "C" {
103 static void gtk_filedialog_response_callback(GtkWidget *w,
104 gint response,
105 wxFileDialog *dialog)
106 {
107 wxapp_install_idle_handler();
108
109 if (response == GTK_RESPONSE_ACCEPT)
110 gtk_filedialog_ok_callback(w, dialog);
111 else if (response == GTK_RESPONSE_CANCEL)
112 gtk_filedialog_cancel_callback(w, dialog);
113 else // "delete"
114 {
115 gtk_filedialog_cancel_callback(w, dialog);
116 dialog->m_destroyed_by_delete = true;
117 }
118 }
119 }
120
121 #endif // __WXGTK24__
122
123 //-----------------------------------------------------------------------------
124 // wxFileDialog
125 //-----------------------------------------------------------------------------
126
127 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog)
128
129 BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog)
130 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
131 END_EVENT_TABLE()
132
133 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
134 const wxString& defaultDir,
135 const wxString& defaultFileName,
136 const wxString& wildCard,
137 long style, const wxPoint& pos)
138 : wxGenericFileDialog(parent, message, defaultDir, defaultFileName,
139 wildCard, style, pos, true )
140 {
141 #ifdef __WXGTK24__
142 if (!gtk_check_version(2,4,0))
143 {
144 wxASSERT_MSG( !( (style & wxSAVE) && (style & wxMULTIPLE) ), wxT("wxFileDialog - wxMULTIPLE used on a save dialog" ) );
145 m_needParent = false;
146 m_destroyed_by_delete = false;
147
148 if (!PreCreation(parent, pos, wxDefaultSize) ||
149 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
150 wxDefaultValidator, wxT("filedialog")))
151 {
152 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
153 return;
154 }
155
156 GtkFileChooserAction gtk_action;
157 GtkWindow* gtk_parent = NULL;
158 if (parent)
159 gtk_parent = GTK_WINDOW(parent->m_widget);
160
161 gchar* ok_btn_stock;
162 if ( style & wxSAVE )
163 {
164 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
165 ok_btn_stock = GTK_STOCK_SAVE;
166 }
167 else
168 {
169 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
170 ok_btn_stock = GTK_STOCK_OPEN;
171 }
172
173 m_widget = gtk_file_chooser_dialog_new(
174 wxGTK_CONV(m_message),
175 gtk_parent,
176 gtk_action,
177 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
178 ok_btn_stock, GTK_RESPONSE_ACCEPT,
179 NULL);
180
181 if ( style & wxMULTIPLE )
182 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true);
183
184 // local-only property could be set to false to allow non-local files to be loaded.
185 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
186 // and the GtkFileChooserDialog should probably also be created with a backend,
187 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
188 // Currently local-only is kept as the default - true:
189 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
190
191 g_signal_connect(G_OBJECT(m_widget), "response",
192 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback), (gpointer)this);
193
194 SetWildcard(wildCard);
195
196 if ( style & wxSAVE )
197 {
198 if ( !defaultDir.empty() )
199 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
200 wxConvFileName->cWX2MB(defaultDir));
201
202 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
203 wxConvFileName->cWX2MB(defaultFileName));
204 }
205 else
206 {
207 if ( !defaultFileName.empty() )
208 {
209 wxString dir;
210 if ( defaultDir.empty() )
211 dir = ::wxGetCwd();
212 else
213 dir = defaultDir;
214
215 gtk_file_chooser_set_filename(
216 GTK_FILE_CHOOSER(m_widget),
217 wxConvFileName->cWX2MB( wxFileName(dir, defaultFileName).GetFullPath() ) );
218 }
219 else if ( !defaultDir.empty() )
220 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget),
221 wxConvFileName->cWX2MB(defaultDir) );
222 }
223 }
224 else
225 #endif
226 wxGenericFileDialog::Create( parent, message, defaultDir, defaultFileName, wildCard, style, pos );
227 }
228
229 wxFileDialog::~wxFileDialog()
230 {
231 #ifdef __WXGTK24__
232 if (!gtk_check_version(2,4,0))
233 {
234 if (m_destroyed_by_delete)
235 m_widget = NULL;
236 }
237 #endif
238 }
239
240 void wxFileDialog::OnFakeOk( wxCommandEvent &event )
241 {
242 #ifdef __WXGTK24__
243 if (!gtk_check_version(2,4,0))
244 wxDialog::OnOK( event );
245 else
246 #endif
247 wxGenericFileDialog::OnListOk( event );
248 }
249
250 int wxFileDialog::ShowModal()
251 {
252 #ifdef __WXGTK24__
253 if (!gtk_check_version(2,4,0))
254 return wxDialog::ShowModal();
255 else
256 #endif
257 return wxGenericFileDialog::ShowModal();
258 }
259
260 bool wxFileDialog::Show( bool show )
261 {
262 #ifdef __WXGTK24__
263 if (!gtk_check_version(2,4,0))
264 return wxDialog::Show( show );
265 else
266 #endif
267 return wxGenericFileDialog::Show( show );
268 }
269
270 wxString wxFileDialog::GetPath() const
271 {
272 #ifdef __WXGTK24__
273 if (!gtk_check_version(2,4,0))
274 return wxConvFileName->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget)));
275 else
276 #endif
277 return wxGenericFileDialog::GetPath();
278 }
279
280 void wxFileDialog::GetFilenames(wxArrayString& files) const
281 {
282 #ifdef __WXGTK24__
283 if (!gtk_check_version(2,4,0))
284 {
285 GetPaths(files);
286 for (size_t n = 0; n < files.GetCount(); ++n )
287 {
288 wxFileName file(files[n]);
289 files[n] = file.GetFullName();
290 }
291 }
292 else
293 #endif
294 wxGenericFileDialog::GetFilenames( files );
295 }
296
297 void wxFileDialog::GetPaths(wxArrayString& paths) const
298 {
299 #ifdef __WXGTK24__
300 if (!gtk_check_version(2,4,0))
301 {
302 paths.Empty();
303 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget)))
304 {
305 GSList *gpathsi = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget));
306 GSList *gpaths = gpathsi;
307 while (gpathsi)
308 {
309 wxString file(wxConvFileName->cMB2WX((gchar*) gpathsi->data));
310 paths.Add(file);
311 g_free(gpathsi->data);
312 gpathsi = gpathsi->next;
313 }
314
315 g_slist_free(gpaths);
316 }
317 else
318 paths.Add(GetPath());
319 }
320 else
321 #endif
322 wxGenericFileDialog::GetPaths( paths );
323 }
324
325 void wxFileDialog::SetMessage(const wxString& message)
326 {
327 #ifdef __WXGTK24__
328 if (!gtk_check_version(2,4,0))
329 {
330 m_message = message;
331 SetTitle(message);
332 }
333 else
334 #endif
335 wxGenericFileDialog::SetMessage( message );
336 }
337
338 void wxFileDialog::SetPath(const wxString& path)
339 {
340 #ifdef __WXGTK24__
341 if (!gtk_check_version(2,4,0))
342 {
343 if (path.empty()) return;
344
345 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(path));
346 }
347 else
348 #endif
349 wxGenericFileDialog::SetPath( path );
350 }
351
352 void wxFileDialog::SetDirectory(const wxString& dir)
353 {
354 #ifdef __WXGTK24__
355 if (!gtk_check_version(2,4,0))
356 {
357 if (wxDirExists(dir))
358 {
359 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir));
360 }
361 }
362 else
363 #endif
364 wxGenericFileDialog::SetDirectory( dir );
365 }
366
367 wxString wxFileDialog::GetDirectory() const
368 {
369 #ifdef __WXGTK24__
370 if (!gtk_check_version(2,4,0))
371 return wxConvFileName->cMB2WX(
372 gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget) ) );
373 else
374 #endif
375 return wxGenericFileDialog::GetDirectory();
376 }
377
378 void wxFileDialog::SetFilename(const wxString& name)
379 {
380 #ifdef __WXGTK24__
381 if (!gtk_check_version(2,4,0))
382 {
383 if (GetStyle() & wxSAVE)
384 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(name));
385 else
386 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
387 }
388 else
389 #endif
390 wxGenericFileDialog::SetFilename( name );
391 }
392
393 wxString wxFileDialog::GetFilename() const
394 {
395 #ifdef __WXGTK24__
396 if (!gtk_check_version(2,4,0))
397 return wxFileName(
398 wxConvFileName->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget))) ).GetFullName();
399 else
400 #endif
401 return wxGenericFileDialog::GetFilename();
402 }
403
404 void wxFileDialog::SetWildcard(const wxString& wildCard)
405 {
406 #ifdef __WXGTK24__
407 if (!gtk_check_version(2,4,0))
408 {
409 // parse filters
410 wxArrayString wildDescriptions, wildFilters;
411 if (!wxParseCommonDialogsFilter(wildCard, wildDescriptions, wildFilters))
412 {
413 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
414 }
415 else
416 {
417 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
418 m_wildCard = wildCard;
419
420 GtkFileChooser* chooser = GTK_FILE_CHOOSER(m_widget);
421
422 // empty current filter list:
423 GSList* ifilters = gtk_file_chooser_list_filters(chooser);
424 GSList* filters = ifilters;
425
426 while (ifilters)
427 {
428 gtk_file_chooser_remove_filter(chooser,GTK_FILE_FILTER(ifilters->data));
429 ifilters = ifilters->next;
430 }
431 g_slist_free(filters);
432
433 // add parsed to GtkChooser
434 for (size_t n = 0; n < wildFilters.GetCount(); ++n)
435 {
436 GtkFileFilter* filter = gtk_file_filter_new();
437 gtk_file_filter_set_name(filter, wxGTK_CONV(wildDescriptions[n]));
438
439 wxStringTokenizer exttok(wildFilters[n], wxT(";"));
440 while (exttok.HasMoreTokens())
441 {
442 wxString token = exttok.GetNextToken();
443 gtk_file_filter_add_pattern(filter, wxGTK_CONV(token));
444 }
445
446 gtk_file_chooser_add_filter(chooser, filter);
447 }
448
449 // Reset the filter index
450 SetFilterIndex(0);
451 }
452 }
453 else
454 #endif
455 wxGenericFileDialog::SetWildcard( wildCard );
456 }
457
458 void wxFileDialog::SetFilterIndex(int filterIndex)
459 {
460 #ifdef __WXGTK24__
461 if (!gtk_check_version(2,4,0))
462 {
463 gpointer filter;
464 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
465 GSList *filters = gtk_file_chooser_list_filters(chooser);
466
467 filter = g_slist_nth_data(filters, filterIndex);
468
469 if (filter != NULL)
470 {
471 gtk_file_chooser_set_filter(chooser, GTK_FILE_FILTER(filter));
472 }
473 else
474 {
475 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
476 }
477
478 g_slist_free(filters);
479 }
480 else
481 #endif
482 wxGenericFileDialog::SetFilterIndex( filterIndex );
483 }
484
485 int wxFileDialog::GetFilterIndex() const
486 {
487 #ifdef __WXGTK24__
488 if (!gtk_check_version(2,4,0))
489 {
490 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
491 GtkFileFilter *filter = gtk_file_chooser_get_filter(chooser);
492 GSList *filters = gtk_file_chooser_list_filters(chooser);
493 gint index = g_slist_index(filters, filter);
494 g_slist_free(filters);
495
496 if (index == -1)
497 {
498 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
499 return 0;
500 }
501 else
502 return index;
503 }
504 else
505 #endif
506 return wxGenericFileDialog::GetFilterIndex();
507 }
508
509 #endif // wxUSE_FILEDLG