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