Interpret default file name relatively to initial dir in wxGTK wxFileDialog.
[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 #include <gtk/gtk.h>
23 #include "wx/gtk/private.h"
24
25 #ifdef __UNIX__
26 #include <unistd.h> // chdir
27 #endif
28
29 #include "wx/filename.h" // wxFilename
30 #include "wx/tokenzr.h" // wxStringTokenizer
31 #include "wx/filefn.h" // ::wxGetCwd
32
33 //-----------------------------------------------------------------------------
34 // "clicked" for OK-button
35 //-----------------------------------------------------------------------------
36
37 extern "C" {
38 static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
39 {
40 int style = dialog->GetWindowStyle();
41 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
42
43 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
44 #if GTK_CHECK_VERSION(2,7,3)
45 if (gtk_check_version(2, 7, 3) != NULL)
46 #endif
47 {
48 if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
49 {
50 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
51 {
52 wxString msg;
53
54 msg.Printf(
55 _("File '%s' already exists, do you really want to overwrite it?"),
56 wxString::FromUTF8(filename));
57
58 wxMessageDialog dlg(dialog, msg, _("Confirm"),
59 wxYES_NO | wxICON_QUESTION);
60 if (dlg.ShowModal() != wxID_YES)
61 return;
62 }
63 }
64 }
65
66 if (style & wxFD_FILE_MUST_EXIST)
67 {
68 if ( !g_file_test(filename, G_FILE_TEST_EXISTS) )
69 {
70 wxMessageDialog dlg( dialog, _("Please choose an existing file."),
71 _("Error"), wxOK| wxICON_ERROR);
72 dlg.ShowModal();
73 return;
74 }
75 }
76
77 // change to the directory where the user went if asked
78 if (style & wxFD_CHANGE_DIR)
79 {
80 // Use chdir to not care about filename encodings
81 wxGtkString folder(g_path_get_dirname(filename));
82 chdir(folder);
83 }
84
85 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
86 event.SetEventObject(dialog);
87 dialog->HandleWindowEvent(event);
88 }
89 }
90
91 //-----------------------------------------------------------------------------
92 // "clicked" for Cancel-button
93 //-----------------------------------------------------------------------------
94
95 extern "C"
96 {
97
98 static void
99 gtk_filedialog_cancel_callback(GtkWidget * WXUNUSED(w), wxFileDialog *dialog)
100 {
101 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
102 event.SetEventObject(dialog);
103 dialog->HandleWindowEvent(event);
104 }
105
106 static void gtk_filedialog_response_callback(GtkWidget *w,
107 gint response,
108 wxFileDialog *dialog)
109 {
110 if (response == GTK_RESPONSE_ACCEPT)
111 gtk_filedialog_ok_callback(w, dialog);
112 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
113 gtk_filedialog_cancel_callback(w, dialog);
114 }
115
116 static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser,
117 gpointer user_data)
118 {
119 GtkWidget *preview = GTK_WIDGET(user_data);
120
121 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
122
123 if ( !filename )
124 return;
125
126 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL);
127 gboolean have_preview = pixbuf != NULL;
128
129 gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf);
130 if ( pixbuf )
131 g_object_unref (pixbuf);
132
133 gtk_file_chooser_set_preview_widget_active(chooser, have_preview);
134 }
135
136 } // extern "C"
137
138 //-----------------------------------------------------------------------------
139 // "size_request" from m_extraControl
140 //-----------------------------------------------------------------------------
141
142 extern "C" {
143 static void extra_widget_size_request(GtkWidget*, GtkRequisition* req, wxWindow* win)
144 {
145 // allow dialog to be resized smaller horizontally
146 req->width = win->GetMinWidth();
147 }
148 }
149
150 void wxFileDialog::AddChildGTK(wxWindowGTK* child)
151 {
152 g_signal_connect_after(child->m_widget, "size_request",
153 G_CALLBACK(extra_widget_size_request), child);
154 gtk_file_chooser_set_extra_widget(
155 GTK_FILE_CHOOSER(m_widget), child->m_widget);
156 }
157
158 //-----------------------------------------------------------------------------
159 // wxFileDialog
160 //-----------------------------------------------------------------------------
161
162 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
163
164 BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase)
165 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
166 EVT_SIZE(wxFileDialog::OnSize)
167 END_EVENT_TABLE()
168
169 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
170 const wxString& defaultDir,
171 const wxString& defaultFileName,
172 const wxString& wildCard,
173 long style, const wxPoint& pos,
174 const wxSize& sz,
175 const wxString& name)
176 : wxFileDialogBase()
177 {
178 parent = GetParentForModalDialog(parent, style);
179
180 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName,
181 wildCard, style, pos, sz, name))
182 {
183 return;
184 }
185
186 if (!PreCreation(parent, pos, wxDefaultSize) ||
187 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
188 wxDefaultValidator, wxT("filedialog")))
189 {
190 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
191 return;
192 }
193
194 GtkFileChooserAction gtk_action;
195 GtkWindow* gtk_parent = NULL;
196 if (parent)
197 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
198
199 const gchar* ok_btn_stock;
200 if ( style & wxFD_SAVE )
201 {
202 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
203 ok_btn_stock = GTK_STOCK_SAVE;
204 }
205 else
206 {
207 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
208 ok_btn_stock = GTK_STOCK_OPEN;
209 }
210
211 m_widget = gtk_file_chooser_dialog_new(
212 wxGTK_CONV(m_message),
213 gtk_parent,
214 gtk_action,
215 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
216 ok_btn_stock, GTK_RESPONSE_ACCEPT,
217 NULL);
218 g_object_ref(m_widget);
219 GtkFileChooser* file_chooser = GTK_FILE_CHOOSER(m_widget);
220
221 m_fc.SetWidget(file_chooser);
222
223 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
224
225 if ( style & wxFD_MULTIPLE )
226 gtk_file_chooser_set_select_multiple(file_chooser, true);
227
228 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
229 // destroys the dialog when the user press ESC on the dialog: in that case
230 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
231 // errors...
232 g_signal_connect(m_widget,
233 "delete_event",
234 G_CALLBACK (gtk_widget_hide_on_delete),
235 this);
236
237 // local-only property could be set to false to allow non-local files to be
238 // loaded. In that case get/set_uri(s) should be used instead of
239 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
240 // probably also be created with a backend, e.g. "gnome-vfs", "default", ...
241 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
242 // as the default - true:
243 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
244
245 g_signal_connect (m_widget, "response",
246 G_CALLBACK (gtk_filedialog_response_callback), this);
247
248
249 // deal with extensions/filters
250 SetWildcard(wildCard);
251
252 wxString defaultFileNameWithExt = defaultFileName;
253 if ( !wildCard.empty() && !defaultFileName.empty() &&
254 !wxFileName(defaultFileName).HasExt() )
255 {
256 // append the default extension to the initial file name: GTK won't do
257 // it for us by default (unlike e.g. MSW)
258 const wxString defaultExt = m_fc.GetCurrentWildCard().AfterFirst('.');
259 if ( defaultExt.find_first_of("?*") == wxString::npos )
260 defaultFileNameWithExt += "." + defaultExt;
261 }
262
263
264 // if defaultDir is specified it should contain the directory and
265 // defaultFileName should contain the default name of the file, however if
266 // directory is not given, defaultFileName contains both
267 wxFileName fn;
268 if ( defaultDir.empty() )
269 fn.Assign(defaultFileNameWithExt);
270 else if ( !defaultFileNameWithExt.empty() )
271 fn.Assign(defaultDir, defaultFileNameWithExt);
272 else
273 fn.AssignDir(defaultDir);
274
275 // set the initial file name and/or directory
276 fn.MakeAbsolute(); // GTK+ needs absolute path
277 const wxString dir = fn.GetPath();
278 if ( !dir.empty() )
279 {
280 gtk_file_chooser_set_current_folder(file_chooser, dir.fn_str());
281 }
282
283 const wxString fname = fn.GetFullName();
284 if ( style & wxFD_SAVE )
285 {
286 if ( !fname.empty() )
287 {
288 gtk_file_chooser_set_current_name(file_chooser, fname.fn_str());
289 }
290
291 #if GTK_CHECK_VERSION(2,7,3)
292 if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3))
293 gtk_file_chooser_set_do_overwrite_confirmation(file_chooser, true);
294 #endif
295 }
296 else // wxFD_OPEN
297 {
298 if ( !fname.empty() )
299 {
300 gtk_file_chooser_set_filename(file_chooser,
301 fn.GetFullPath().fn_str());
302 }
303 }
304
305 if ( style & wxFD_PREVIEW )
306 {
307 GtkWidget *previewImage = gtk_image_new();
308
309 gtk_file_chooser_set_preview_widget(file_chooser, previewImage);
310 g_signal_connect(m_widget, "update-preview",
311 G_CALLBACK(gtk_filedialog_update_preview_callback),
312 previewImage);
313 }
314 }
315
316 wxFileDialog::~wxFileDialog()
317 {
318 if (m_extraControl)
319 {
320 // get chooser to drop its reference right now, allowing wxWindow dtor
321 // to verify that ref count drops to zero
322 gtk_file_chooser_set_extra_widget(
323 GTK_FILE_CHOOSER(m_widget), NULL);
324 }
325 }
326
327 void wxFileDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event))
328 {
329 EndDialog(wxID_OK);
330 }
331
332 int wxFileDialog::ShowModal()
333 {
334 CreateExtraControl();
335
336 return wxDialog::ShowModal();
337 }
338
339 void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
340 int WXUNUSED(width), int WXUNUSED(height),
341 int WXUNUSED(sizeFlags))
342 {
343 }
344
345 void wxFileDialog::OnSize(wxSizeEvent&)
346 {
347 // avoid calling DoLayout(), which will set the (wrong) size of
348 // m_extraControl, its size is managed by GtkFileChooser
349 }
350
351 wxString wxFileDialog::GetPath() const
352 {
353 return m_fc.GetPath();
354 }
355
356 void wxFileDialog::GetFilenames(wxArrayString& files) const
357 {
358 m_fc.GetFilenames( files );
359 }
360
361 void wxFileDialog::GetPaths(wxArrayString& paths) const
362 {
363 m_fc.GetPaths( paths );
364 }
365
366 void wxFileDialog::SetMessage(const wxString& message)
367 {
368 m_message = message;
369 SetTitle(message);
370 }
371
372 void wxFileDialog::SetPath(const wxString& path)
373 {
374 // we need an absolute path for GTK native chooser so ensure that we have
375 // it: use the initial directory if it was set or just CWD otherwise (this
376 // is the default behaviour if m_dir is empty)
377 wxFileName fn(path);
378 fn.MakeAbsolute(m_dir);
379 m_fc.SetPath(fn.GetFullPath());
380 }
381
382 void wxFileDialog::SetDirectory(const wxString& dir)
383 {
384 if (m_fc.SetDirectory( dir ))
385 {
386 // Cache the dir, as gtk_file_chooser_get_current_folder()
387 // doesn't return anything until the dialog has been shown
388 m_dir = dir;
389 }
390 }
391
392 wxString wxFileDialog::GetDirectory() const
393 {
394 wxString currentDir( m_fc.GetDirectory() );
395 if (currentDir.empty())
396 {
397 // m_fc.GetDirectory() will return empty until the dialog has been shown
398 // in which case use any previously provided value
399 currentDir = m_dir;
400 }
401 return currentDir;
402 }
403
404 void wxFileDialog::SetFilename(const wxString& name)
405 {
406 if (HasFdFlag(wxFD_SAVE))
407 {
408 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
409 m_fileName = name;
410 }
411
412 else
413 {
414 wxString path( GetDirectory() );
415 if (path.empty())
416 {
417 // SetPath() fires an assert if fed other than filepaths
418 return;
419 }
420 SetPath(wxFileName(path, name).GetFullPath());
421 m_fileName = name;
422 }
423 }
424
425 wxString wxFileDialog::GetFilename() const
426 {
427 wxString currentFilename( m_fc.GetFilename() );
428 if (currentFilename.empty())
429 {
430 // m_fc.GetFilename() will return empty until the dialog has been shown
431 // in which case use any previously provided value
432 currentFilename = m_fileName;
433 }
434 return currentFilename;
435 }
436
437 void wxFileDialog::SetWildcard(const wxString& wildCard)
438 {
439 wxFileDialogBase::SetWildcard(wildCard);
440 m_fc.SetWildcard( GetWildcard() );
441 }
442
443 void wxFileDialog::SetFilterIndex(int filterIndex)
444 {
445 m_fc.SetFilterIndex( filterIndex);
446 }
447
448 int wxFileDialog::GetFilterIndex() const
449 {
450 return m_fc.GetFilterIndex();
451 }
452
453 #endif // wxUSE_FILEDLG