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