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