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