]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
9b5f1895 | 2 | // Name: src/gtk/filedlg.cpp |
9755e73b | 3 | // Purpose: native implementation of wxFileDialog |
f8bc53eb | 4 | // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp |
a81258be | 5 | // Id: $Id$ |
f8bc53eb | 6 | // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
03647350 | 13 | #if wxUSE_FILEDLG |
9755e73b | 14 | |
c801d85f | 15 | #include "wx/filedlg.h" |
4e1901b7 | 16 | |
88a7a4e1 WS |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/intl.h" | |
246c5004 | 19 | #include "wx/msgdlg.h" |
88a7a4e1 WS |
20 | #endif |
21 | ||
f8bc53eb | 22 | #include <gtk/gtk.h> |
9755e73b | 23 | #include "wx/gtk/private.h" |
83624f79 | 24 | |
f04f570f | 25 | #ifdef __UNIX__ |
f8bc53eb | 26 | #include <unistd.h> // chdir |
f04f570f | 27 | #endif |
f8bc53eb | 28 | |
f8bc53eb JS |
29 | #include "wx/filename.h" // wxFilename |
30 | #include "wx/tokenzr.h" // wxStringTokenizer | |
31 | #include "wx/filefn.h" // ::wxGetCwd | |
643e9cf9 | 32 | #include "wx/testing.h" |
f8bc53eb | 33 | |
9f057af5 VZ |
34 | extern "C" |
35 | { | |
05083921 | 36 | static void gtk_filedialog_response_callback(GtkWidget * WXUNUSED(w), |
f8bc53eb | 37 | gint response, |
9755e73b VS |
38 | wxFileDialog *dialog) |
39 | { | |
a7334928 | 40 | if (response == GTK_RESPONSE_ACCEPT) |
05083921 | 41 | dialog->GTKOnAccept(); |
a552d120 | 42 | else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE |
05083921 | 43 | dialog->GTKOnCancel(); |
ff7b1510 | 44 | } |
9f057af5 VZ |
45 | |
46 | static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser, | |
47 | gpointer user_data) | |
48 | { | |
9f057af5 | 49 | GtkWidget *preview = GTK_WIDGET(user_data); |
3ab296d9 | 50 | |
e808cf8a | 51 | wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser)); |
3ab296d9 | 52 | |
9f057af5 VZ |
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); | |
865bb325 VZ |
64 | } |
65 | ||
9f057af5 VZ |
66 | } // extern "C" |
67 | ||
3b7067a0 | 68 | void wxFileDialog::AddChildGTK(wxWindowGTK* child) |
38a36cf5 PC |
69 | { |
70 | // allow dialog to be resized smaller horizontally | |
3b7067a0 PC |
71 | gtk_widget_set_size_request( |
72 | child->m_widget, child->GetMinWidth(), child->m_height); | |
38a36cf5 | 73 | |
3abfbb43 | 74 | gtk_file_chooser_set_extra_widget( |
48200154 | 75 | GTK_FILE_CHOOSER(m_widget), child->m_widget); |
8ce68f7f VZ |
76 | } |
77 | ||
291a8f20 RR |
78 | //----------------------------------------------------------------------------- |
79 | // wxFileDialog | |
80 | //----------------------------------------------------------------------------- | |
81 | ||
700d08c1 | 82 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase) |
4e1901b7 | 83 | |
700d08c1 | 84 | BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase) |
f1d5aa4e | 85 | EVT_SIZE(wxFileDialog::OnSize) |
4e1901b7 | 86 | END_EVENT_TABLE() |
c801d85f | 87 | |
9755e73b VS |
88 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, |
89 | const wxString& defaultDir, | |
90 | const wxString& defaultFileName, | |
91 | const wxString& wildCard, | |
ff3e84ff VZ |
92 | long style, const wxPoint& pos, |
93 | const wxSize& sz, | |
94 | const wxString& name) | |
700d08c1 | 95 | : wxFileDialogBase() |
e3f54c8f VZ |
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) | |
c801d85f | 107 | { |
cdc48273 | 108 | parent = GetParentForModalDialog(parent, style); |
f2448f21 | 109 | |
700d08c1 RR |
110 | if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName, |
111 | wildCard, style, pos, sz, name)) | |
77f70672 | 112 | { |
e3f54c8f | 113 | return false; |
9f057af5 | 114 | } |
83624f79 | 115 | |
9f057af5 VZ |
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") ); | |
e3f54c8f | 121 | return false; |
9f057af5 | 122 | } |
3f6638b8 | 123 | |
9f057af5 VZ |
124 | GtkFileChooserAction gtk_action; |
125 | GtkWindow* gtk_parent = NULL; | |
126 | if (parent) | |
127 | gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) ); | |
27b2dd53 | 128 | |
9f057af5 VZ |
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 | } | |
f8bc53eb | 140 | |
9f057af5 VZ |
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); | |
9ff9d30c | 148 | g_object_ref(m_widget); |
f2448f21 | 149 | GtkFileChooser* file_chooser = GTK_FILE_CHOOSER(m_widget); |
9755e73b | 150 | |
f2448f21 | 151 | m_fc.SetWidget(file_chooser); |
0cf3e587 | 152 | |
9f057af5 | 153 | gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT); |
c1dfe97c | 154 | |
9f057af5 | 155 | if ( style & wxFD_MULTIPLE ) |
f2448f21 | 156 | gtk_file_chooser_set_select_multiple(file_chooser, true); |
27b2dd53 | 157 | |
bf345206 VZ |
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... | |
f2448f21 | 162 | g_signal_connect(m_widget, |
9f057af5 VZ |
163 | "delete_event", |
164 | G_CALLBACK (gtk_widget_hide_on_delete), | |
f2448f21 | 165 | this); |
a552d120 | 166 | |
bf345206 VZ |
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 | |
d13b34d3 | 170 | // probably also be created with a backend, e.g. "gnome-vfs", "default", ... |
bf345206 VZ |
171 | // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept |
172 | // as the default - true: | |
9f057af5 | 173 | // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true); |
27b2dd53 | 174 | |
9f057af5 VZ |
175 | g_signal_connect (m_widget, "response", |
176 | G_CALLBACK (gtk_filedialog_response_callback), this); | |
27b2dd53 | 177 | |
fa333077 VZ |
178 | |
179 | // deal with extensions/filters | |
9f057af5 | 180 | SetWildcard(wildCard); |
f8bc53eb | 181 | |
fa333077 VZ |
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 | ||
bf345206 VZ |
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() ) | |
fa333077 VZ |
199 | fn.Assign(defaultFileNameWithExt); |
200 | else if ( !defaultFileNameWithExt.empty() ) | |
201 | fn.Assign(defaultDir, defaultFileNameWithExt); | |
f241631e RD |
202 | else |
203 | fn.AssignDir(defaultDir); | |
bf345206 VZ |
204 | |
205 | // set the initial file name and/or directory | |
78355ffc | 206 | fn.MakeAbsolute(); // GTK+ needs absolute path |
3a41827a VZ |
207 | const wxString dir = fn.GetPath(); |
208 | if ( !dir.empty() ) | |
9f057af5 | 209 | { |
c282e47d | 210 | gtk_file_chooser_set_current_folder(file_chooser, wxGTK_CONV_FN(dir)); |
bf345206 | 211 | } |
f8bc53eb | 212 | |
3a41827a | 213 | const wxString fname = fn.GetFullName(); |
bf345206 VZ |
214 | if ( style & wxFD_SAVE ) |
215 | { | |
216 | if ( !fname.empty() ) | |
217 | { | |
c282e47d | 218 | gtk_file_chooser_set_current_name(file_chooser, wxGTK_CONV_FN(fname)); |
bf345206 | 219 | } |
b5ab6d19 MR |
220 | |
221 | #if GTK_CHECK_VERSION(2,7,3) | |
9dc44eff PC |
222 | if ((style & wxFD_OVERWRITE_PROMPT) |
223 | #ifndef __WXGTK3__ | |
224 | && gtk_check_version(2,7,3) == NULL | |
225 | #endif | |
226 | ) | |
227 | { | |
f2448f21 | 228 | gtk_file_chooser_set_do_overwrite_confirmation(file_chooser, true); |
9dc44eff | 229 | } |
b5ab6d19 | 230 | #endif |
9f057af5 VZ |
231 | } |
232 | else // wxFD_OPEN | |
233 | { | |
bf345206 | 234 | if ( !fname.empty() ) |
f8bc53eb | 235 | { |
f2448f21 | 236 | gtk_file_chooser_set_filename(file_chooser, |
c282e47d | 237 | wxGTK_CONV_FN(fn.GetFullPath())); |
f8bc53eb | 238 | } |
77f70672 | 239 | } |
9f057af5 | 240 | |
9f057af5 VZ |
241 | if ( style & wxFD_PREVIEW ) |
242 | { | |
243 | GtkWidget *previewImage = gtk_image_new(); | |
244 | ||
f2448f21 | 245 | gtk_file_chooser_set_preview_widget(file_chooser, previewImage); |
9f057af5 VZ |
246 | g_signal_connect(m_widget, "update-preview", |
247 | G_CALLBACK(gtk_filedialog_update_preview_callback), | |
248 | previewImage); | |
249 | } | |
e3f54c8f VZ |
250 | |
251 | return true; | |
9755e73b | 252 | } |
0e1399b3 | 253 | |
9ff9d30c PC |
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 | } | |
05083921 | 264 | void wxFileDialog::GTKOnAccept() |
4e1901b7 | 265 | { |
05083921 VZ |
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 | } | |
700d08c1 | 313 | EndDialog(wxID_OK); |
4e1901b7 RR |
314 | } |
315 | ||
05083921 VZ |
316 | void wxFileDialog::GTKOnCancel() |
317 | { | |
318 | EndDialog(wxID_CANCEL); | |
319 | } | |
320 | ||
4e1901b7 RR |
321 | int wxFileDialog::ShowModal() |
322 | { | |
643e9cf9 VS |
323 | WX_TESTING_SHOW_MODAL_HOOK(); |
324 | ||
3abfbb43 | 325 | CreateExtraControl(); |
8ce68f7f VZ |
326 | |
327 | return wxDialog::ShowModal(); | |
9755e73b | 328 | } |
0e1399b3 | 329 | |
03647350 VZ |
330 | void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y), |
331 | int WXUNUSED(width), int WXUNUSED(height), | |
700d08c1 | 332 | int WXUNUSED(sizeFlags)) |
5b2e23bf | 333 | { |
5b2e23bf RR |
334 | } |
335 | ||
f1d5aa4e PC |
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 | ||
f8bc53eb JS |
342 | wxString wxFileDialog::GetPath() const |
343 | { | |
fa333077 | 344 | return m_fc.GetPath(); |
f8bc53eb JS |
345 | } |
346 | ||
27b2dd53 | 347 | void wxFileDialog::GetFilenames(wxArrayString& files) const |
9755e73b | 348 | { |
700d08c1 | 349 | m_fc.GetFilenames( files ); |
9755e73b | 350 | } |
76840ed0 | 351 | |
27b2dd53 | 352 | void wxFileDialog::GetPaths(wxArrayString& paths) const |
9755e73b | 353 | { |
700d08c1 | 354 | m_fc.GetPaths( paths ); |
9755e73b | 355 | } |
035b704a | 356 | |
9755e73b VS |
357 | void wxFileDialog::SetMessage(const wxString& message) |
358 | { | |
700d08c1 RR |
359 | m_message = message; |
360 | SetTitle(message); | |
9755e73b VS |
361 | } |
362 | ||
76840ed0 RR |
363 | void wxFileDialog::SetPath(const wxString& path) |
364 | { | |
ab49aec9 VS |
365 | wxFileDialogBase::SetPath(path); |
366 | ||
dea43e6e VZ |
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 | ||
8f79ece3 | 373 | // we need an absolute path for GTK native chooser so ensure that we have |
257d35ae VZ |
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) | |
8f79ece3 | 376 | wxFileName fn(path); |
257d35ae | 377 | fn.MakeAbsolute(m_dir); |
8f79ece3 | 378 | m_fc.SetPath(fn.GetFullPath()); |
76840ed0 RR |
379 | } |
380 | ||
9755e73b VS |
381 | void wxFileDialog::SetDirectory(const wxString& dir) |
382 | { | |
ab49aec9 VS |
383 | wxFileDialogBase::SetDirectory(dir); |
384 | ||
385 | m_fc.SetDirectory(dir); | |
05083921 | 386 | m_selectedDirectory = dir; |
9755e73b VS |
387 | } |
388 | ||
f8bc53eb JS |
389 | wxString wxFileDialog::GetDirectory() const |
390 | { | |
05083921 | 391 | return m_selectedDirectory; |
f8bc53eb JS |
392 | } |
393 | ||
9755e73b VS |
394 | void wxFileDialog::SetFilename(const wxString& name) |
395 | { | |
ab49aec9 VS |
396 | wxFileDialogBase::SetFilename(name); |
397 | ||
700d08c1 | 398 | if (HasFdFlag(wxFD_SAVE)) |
02a40b8a | 399 | { |
700d08c1 | 400 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name)); |
02a40b8a JS |
401 | } |
402 | ||
77f70672 | 403 | else |
02a40b8a JS |
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()); | |
02a40b8a | 412 | } |
9755e73b | 413 | } |
035b704a | 414 | |
f8bc53eb JS |
415 | wxString wxFileDialog::GetFilename() const |
416 | { | |
02a40b8a JS |
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; | |
f8bc53eb JS |
425 | } |
426 | ||
9755e73b VS |
427 | void wxFileDialog::SetWildcard(const wxString& wildCard) |
428 | { | |
b534aeb2 VZ |
429 | wxFileDialogBase::SetWildcard(wildCard); |
430 | m_fc.SetWildcard( GetWildcard() ); | |
9755e73b | 431 | } |
a3622daa | 432 | |
9755e73b VS |
433 | void wxFileDialog::SetFilterIndex(int filterIndex) |
434 | { | |
700d08c1 | 435 | m_fc.SetFilterIndex( filterIndex); |
4e1901b7 RR |
436 | } |
437 | ||
f8bc53eb | 438 | int wxFileDialog::GetFilterIndex() const |
4e1901b7 | 439 | { |
700d08c1 | 440 | return m_fc.GetFilterIndex(); |
9755e73b | 441 | } |
3f6638b8 | 442 | |
03647350 | 443 | #endif // wxUSE_FILEDLG |