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