]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: gtk/filedlg.cpp | |
3 | // Purpose: native implementation of wxFileDialog | |
4 | // Author: Robert Roebling, Zbigniew Zagorski | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
11 | #pragma implementation "filedlggtk.h" | |
12 | #endif | |
13 | ||
14 | // For compilers that support precompilation, includes "wx.h". | |
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #if wxUSE_FILEDLG | |
18 | ||
19 | #include "wx/filedlg.h" | |
20 | #include "wx/utils.h" | |
21 | #include "wx/intl.h" | |
22 | #include "wx/filename.h" | |
23 | #include "wx/msgdlg.h" | |
24 | #include "wx/log.h" | |
25 | ||
26 | #include <gtk/gtk.h> | |
27 | ||
28 | #ifdef __WXGTK24__ | |
29 | #include "wx/gtk/private.h" | |
30 | ||
31 | //----------------------------------------------------------------------------- | |
32 | // idle system | |
33 | //----------------------------------------------------------------------------- | |
34 | ||
35 | extern void wxapp_install_idle_handler(); | |
36 | extern bool g_isIdle; | |
37 | ||
38 | //----------------------------------------------------------------------------- | |
39 | // "clicked" for OK-button | |
40 | //----------------------------------------------------------------------------- | |
41 | ||
42 | extern "C" { | |
43 | static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog) | |
44 | { | |
45 | int style = dialog->GetStyle(); | |
46 | gchar* text = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)); | |
47 | wxString filename(wxGTK_CONV_BACK(text)); | |
48 | if ( filename.empty() ) | |
49 | { | |
50 | // this is totally lame but better than silent error | |
51 | wxLogWarning(_("This filename can't be used by wxWidgets because it contains invalid UTF-8 characters, please rename the file.")); | |
52 | return; | |
53 | } | |
54 | ||
55 | if ((style & wxSAVE) && (style & wxOVERWRITE_PROMPT)) | |
56 | { | |
57 | if (wxFileExists(filename)) | |
58 | { | |
59 | wxString msg; | |
60 | msg.Printf( | |
61 | _("File '%s' already exists, do you really want to overwrite it?"), | |
62 | filename.c_str()); | |
63 | ||
64 | wxMessageDialog dlg(dialog, msg, _("Confirm"), | |
65 | wxYES_NO | wxICON_QUESTION); | |
66 | if (dlg.ShowModal() != wxID_YES) | |
67 | return; | |
68 | } | |
69 | } | |
70 | else if ((style & wxOPEN) && ( style & wxFILE_MUST_EXIST)) | |
71 | { | |
72 | if (!wxFileExists( filename )) | |
73 | { | |
74 | wxMessageDialog dlg(dialog, | |
75 | _("Please choose an existing file."), | |
76 | _("Error"), wxOK | wxICON_ERROR); | |
77 | dlg.ShowModal(); | |
78 | ||
79 | return; | |
80 | } | |
81 | } | |
82 | ||
83 | // change to the directory where the user went if asked | |
84 | if (style & wxCHANGE_DIR) | |
85 | { | |
86 | wxString cwd; | |
87 | wxSplitPath(filename, &cwd, NULL, NULL); | |
88 | ||
89 | if (cwd != wxGetCwd()) | |
90 | { | |
91 | wxSetWorkingDirectory(cwd); | |
92 | } | |
93 | } | |
94 | ||
95 | dialog->SetPath(filename); | |
96 | dialog->UpdateFromDialog(); | |
97 | ||
98 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK); | |
99 | event.SetEventObject(dialog); | |
100 | dialog->GetEventHandler()->ProcessEvent(event); | |
101 | } | |
102 | } | |
103 | ||
104 | //----------------------------------------------------------------------------- | |
105 | // "clicked" for Cancel-button | |
106 | //----------------------------------------------------------------------------- | |
107 | ||
108 | extern "C" { | |
109 | static void gtk_filedialog_cancel_callback(GtkWidget *WXUNUSED(w), | |
110 | wxFileDialog *dialog) | |
111 | { | |
112 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
113 | event.SetEventObject(dialog); | |
114 | dialog->GetEventHandler()->ProcessEvent(event); | |
115 | } | |
116 | } | |
117 | ||
118 | extern "C" { | |
119 | static void gtk_filedialog_response_callback(GtkWidget *w, | |
120 | int response, | |
121 | wxFileDialog *dialog) | |
122 | { | |
123 | wxapp_install_idle_handler(); | |
124 | ||
125 | if (response == GTK_RESPONSE_ACCEPT) | |
126 | gtk_filedialog_ok_callback(w, dialog); | |
127 | else if (response == GTK_RESPONSE_CANCEL) | |
128 | gtk_filedialog_cancel_callback(w, dialog); | |
129 | else // "delete" | |
130 | { | |
131 | gtk_filedialog_cancel_callback(w, dialog); | |
132 | dialog->m_destroyed_by_delete = true; | |
133 | } | |
134 | } | |
135 | } | |
136 | ||
137 | #endif // __WXGTK24__ | |
138 | ||
139 | //----------------------------------------------------------------------------- | |
140 | // wxFileDialog | |
141 | //----------------------------------------------------------------------------- | |
142 | ||
143 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog) | |
144 | ||
145 | BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog) | |
146 | EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk) | |
147 | END_EVENT_TABLE() | |
148 | ||
149 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, | |
150 | const wxString& defaultDir, | |
151 | const wxString& defaultFileName, | |
152 | const wxString& wildCard, | |
153 | long style, const wxPoint& pos) | |
154 | : wxGenericFileDialog(parent, message, defaultDir, defaultFileName, | |
155 | wildCard, style, pos, true ) | |
156 | { | |
157 | #ifdef __WXGTK24__ | |
158 | if (!gtk_check_version(2,4,0)) | |
159 | { | |
160 | m_needParent = false; | |
161 | m_destroyed_by_delete = false; | |
162 | ||
163 | if (!PreCreation(parent, pos, wxDefaultSize) || | |
164 | !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style, | |
165 | wxDefaultValidator, wxT("filedialog"))) | |
166 | { | |
167 | wxFAIL_MSG( wxT("wxFileDialog creation failed") ); | |
168 | return; | |
169 | } | |
170 | ||
171 | bool multiple = (style & wxMULTIPLE) == wxMULTIPLE; | |
172 | GtkFileChooserAction gtk_action; | |
173 | GtkWindow* gtk_parent = NULL; | |
174 | if (parent) | |
175 | gtk_parent = GTK_WINDOW(parent->m_widget); | |
176 | ||
177 | gchar* ok_btn_stock; | |
178 | if ((style & wxSAVE) == wxSAVE) | |
179 | { | |
180 | gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE; | |
181 | ok_btn_stock = GTK_STOCK_SAVE; | |
182 | } | |
183 | else | |
184 | { | |
185 | gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN; | |
186 | ok_btn_stock = GTK_STOCK_OPEN; | |
187 | } | |
188 | m_widget = gtk_file_chooser_dialog_new( | |
189 | wxGTK_CONV(m_message), | |
190 | gtk_parent, | |
191 | gtk_action, | |
192 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | |
193 | ok_btn_stock, GTK_RESPONSE_ACCEPT, | |
194 | NULL); | |
195 | ||
196 | gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), multiple); | |
197 | ||
198 | gtk_signal_connect(GTK_OBJECT(m_widget), | |
199 | "response", | |
200 | GTK_SIGNAL_FUNC(gtk_filedialog_response_callback), | |
201 | (gpointer*)this); | |
202 | ||
203 | m_path = m_dir; | |
204 | if (!m_path.empty() && m_path.Last() != wxT('/')) | |
205 | m_path += wxT('/'); | |
206 | m_path += m_fileName; | |
207 | SetPath(m_path); | |
208 | ||
209 | SetWildcard(wildCard); | |
210 | SetFilterIndex(0); | |
211 | } | |
212 | else | |
213 | #endif | |
214 | wxGenericFileDialog::Create( parent, message, defaultDir, defaultFileName, wildCard, style, pos ); | |
215 | } | |
216 | ||
217 | wxFileDialog::~wxFileDialog() | |
218 | { | |
219 | #ifdef __WXGTK24__ | |
220 | if (!gtk_check_version(2,4,0)) | |
221 | { | |
222 | if (m_destroyed_by_delete) | |
223 | m_widget = NULL; | |
224 | } | |
225 | #endif | |
226 | } | |
227 | ||
228 | void wxFileDialog::OnFakeOk( wxCommandEvent &event ) | |
229 | { | |
230 | #ifdef __WXGTK24__ | |
231 | if (!gtk_check_version(2,4,0)) | |
232 | wxDialog::OnOK( event ); | |
233 | else | |
234 | #endif | |
235 | wxGenericFileDialog::OnListOk( event ); | |
236 | } | |
237 | ||
238 | int wxFileDialog::ShowModal() | |
239 | { | |
240 | #ifdef __WXGTK24__ | |
241 | if (!gtk_check_version(2,4,0)) | |
242 | return wxDialog::ShowModal(); | |
243 | else | |
244 | #endif | |
245 | return wxGenericFileDialog::ShowModal(); | |
246 | } | |
247 | ||
248 | bool wxFileDialog::Show( bool show ) | |
249 | { | |
250 | #ifdef __WXGTK24__ | |
251 | if (!gtk_check_version(2,4,0)) | |
252 | return wxDialog::Show( show ); | |
253 | else | |
254 | #endif | |
255 | return wxGenericFileDialog::Show( show ); | |
256 | } | |
257 | ||
258 | void wxFileDialog::GetFilenames(wxArrayString& files) const | |
259 | { | |
260 | #ifdef __WXGTK24__ | |
261 | if (!gtk_check_version(2,4,0)) | |
262 | { | |
263 | GetPaths(files); | |
264 | for (size_t n = 0; n < files.GetCount(); n++ ) | |
265 | { | |
266 | wxString name,ext; | |
267 | wxSplitPath(files[n], NULL, &name, &ext); | |
268 | if (!ext.empty()) | |
269 | { | |
270 | name += wxT("."); | |
271 | name += ext; | |
272 | } | |
273 | files[n] = name; | |
274 | } | |
275 | } | |
276 | else | |
277 | #endif | |
278 | wxGenericFileDialog::GetFilenames( files ); | |
279 | } | |
280 | ||
281 | void wxFileDialog::GetPaths(wxArrayString& paths) const | |
282 | { | |
283 | #ifdef __WXGTK24__ | |
284 | if (!gtk_check_version(2,4,0)) | |
285 | { | |
286 | paths.Empty(); | |
287 | if (GetWindowStyle() & wxMULTIPLE) | |
288 | { | |
289 | GSList *gpathsi = | |
290 | gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget)); | |
291 | GSList *gpaths = gpathsi; | |
292 | while (gpathsi) | |
293 | { | |
294 | wxString file = wxGTK_CONV_BACK((gchar*) gpathsi->data); | |
295 | paths.Add(file); | |
296 | g_free(gpathsi->data); | |
297 | gpathsi = gpathsi->next; | |
298 | } | |
299 | if (gpaths) | |
300 | g_slist_free(gpaths); | |
301 | } | |
302 | ||
303 | if ( paths.IsEmpty() ) | |
304 | { | |
305 | paths.Add(m_fileName); | |
306 | } | |
307 | } | |
308 | else | |
309 | #endif | |
310 | wxGenericFileDialog::GetPaths( paths ); | |
311 | } | |
312 | ||
313 | void wxFileDialog::SetMessage(const wxString& message) | |
314 | { | |
315 | #ifdef __WXGTK24__ | |
316 | if (!gtk_check_version(2,4,0)) | |
317 | { | |
318 | m_message = message; | |
319 | SetTitle(message); | |
320 | } | |
321 | else | |
322 | #endif | |
323 | wxGenericFileDialog::SetMessage( message ); | |
324 | } | |
325 | ||
326 | void wxFileDialog::SetPath(const wxString& path) | |
327 | { | |
328 | #ifdef __WXGTK24__ | |
329 | if (!gtk_check_version(2,4,0)) | |
330 | { | |
331 | if (path.empty()) return; | |
332 | ||
333 | wxFileName fn(path); | |
334 | m_path = fn.GetFullPath(); | |
335 | m_dir = fn.GetPath(); | |
336 | m_fileName = fn.GetFullName(); | |
337 | UpdateDialog(); | |
338 | } | |
339 | else | |
340 | #endif | |
341 | wxGenericFileDialog::SetPath( path ); | |
342 | } | |
343 | ||
344 | void wxFileDialog::SetDirectory(const wxString& dir) | |
345 | { | |
346 | #ifdef __WXGTK24__ | |
347 | if (!gtk_check_version(2,4,0)) | |
348 | { | |
349 | if (wxPathExists(dir)) | |
350 | { | |
351 | m_dir = dir; | |
352 | m_path = wxFileName(m_dir, m_fileName).GetFullPath(); | |
353 | UpdateDialog(); | |
354 | } | |
355 | } | |
356 | else | |
357 | #endif | |
358 | wxGenericFileDialog::SetDirectory( dir ); | |
359 | } | |
360 | ||
361 | void wxFileDialog::SetFilename(const wxString& name) | |
362 | { | |
363 | #ifdef __WXGTK24__ | |
364 | if (!gtk_check_version(2,4,0)) | |
365 | { | |
366 | m_fileName = name; | |
367 | m_path = wxFileName(m_dir, m_fileName).GetFullPath(); | |
368 | UpdateDialog(); | |
369 | } | |
370 | else | |
371 | #endif | |
372 | wxGenericFileDialog::SetFilename( name ); | |
373 | } | |
374 | ||
375 | void wxFileDialog::SetWildcard(const wxString& wildCard) | |
376 | { | |
377 | #ifdef __WXGTK24__ | |
378 | if (!gtk_check_version(2,4,0)) | |
379 | { | |
380 | m_wildCard = wildCard; | |
381 | GtkFileChooser* chooser = GTK_FILE_CHOOSER(m_widget); | |
382 | ||
383 | // empty current filter list: | |
384 | GSList* ifilters = gtk_file_chooser_list_filters(chooser); | |
385 | GSList* filters = ifilters; | |
386 | while (ifilters) | |
387 | { | |
388 | gtk_file_chooser_remove_filter(chooser,GTK_FILE_FILTER(ifilters->data)); | |
389 | ifilters = ifilters->next; | |
390 | } | |
391 | g_slist_free(filters); | |
392 | ||
393 | // parse filters | |
394 | wxArrayString wildDescriptions, wildFilters; | |
395 | if (!wxParseCommonDialogsFilter(m_wildCard, wildDescriptions, wildFilters)) | |
396 | { | |
397 | wxFAIL_MSG( wxT("Wrong file type description") ); | |
398 | } | |
399 | else | |
400 | { | |
401 | // add parsed to GtkChooser | |
402 | for (size_t n = 0; n < wildFilters.GetCount(); n++) | |
403 | { | |
404 | GtkFileFilter* filter = gtk_file_filter_new(); | |
405 | gtk_file_filter_set_name(filter,wxGTK_CONV(wildDescriptions[n])); | |
406 | wxString after = wildFilters[n]; | |
407 | do | |
408 | { | |
409 | wxString ext = after.BeforeFirst(wxT(';')); | |
410 | gtk_file_filter_add_pattern(filter,wxGTK_CONV(ext)); | |
411 | if (after.Find(wxT(';')) == wxNOT_FOUND) | |
412 | break; | |
413 | after = after.AfterLast(wxT(';')); | |
414 | } | |
415 | while (!after.empty()); | |
416 | ||
417 | gtk_file_chooser_add_filter(chooser, filter); | |
418 | } | |
419 | } | |
420 | } | |
421 | else | |
422 | #endif | |
423 | wxGenericFileDialog::SetWildcard( wildCard ); | |
424 | } | |
425 | ||
426 | void wxFileDialog::SetFilterIndex(int filterIndex) | |
427 | { | |
428 | #ifdef __WXGTK24__ | |
429 | if (!gtk_check_version(2,4,0)) | |
430 | { | |
431 | m_filterIndex = filterIndex; | |
432 | ||
433 | GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget); | |
434 | GSList *fnode = gtk_file_chooser_list_filters(chooser); | |
435 | GSList *filters = fnode; | |
436 | int i = 0; | |
437 | while (fnode) | |
438 | { | |
439 | if (i == filterIndex) | |
440 | { | |
441 | gtk_file_chooser_set_filter(chooser, GTK_FILE_FILTER(fnode->data)); | |
442 | m_filterIndex = i; | |
443 | break; | |
444 | } | |
445 | i++; | |
446 | fnode = fnode->next; | |
447 | } | |
448 | g_slist_free(filters); | |
449 | } | |
450 | else | |
451 | #endif | |
452 | wxGenericFileDialog::SetFilterIndex( filterIndex ); | |
453 | } | |
454 | ||
455 | void wxFileDialog::UpdateDialog() | |
456 | { | |
457 | #ifdef __WXGTK24__ | |
458 | // set currently selected directory to match the path: | |
459 | if (!m_dir.empty() && wxPathExists(m_dir)) | |
460 | { | |
461 | // NB: This is important -- if we set directory only and not the path, | |
462 | // then dialog will still remember old path set using previous | |
463 | // call to gtk_chooser_set_filename. If the previous directory | |
464 | // was a subdirectory of the directory we want to select now, | |
465 | // the dialog would still contain directory selector controls | |
466 | // for the subdirectory (with the parent directory selected), | |
467 | // instead of showing only the parent directory as expected. | |
468 | // This way, we force GtkFileChooser to really change the | |
469 | // directory. Finally, it doesn't have to be done if filename | |
470 | // is not empty because of the code that sets the filename below. | |
471 | if (m_fileName.empty()) | |
472 | gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), | |
473 | wxGTK_CONV(m_dir)); | |
474 | ||
475 | gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), | |
476 | wxGTK_CONV(m_dir)); | |
477 | } | |
478 | ||
479 | // if the user set only the directory (e.g. by calling SetDirectory) | |
480 | // and not the default filename, then we don't want to set the filename: | |
481 | if (!m_fileName.empty()) | |
482 | { | |
483 | gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), | |
484 | wxGTK_CONV(m_path)); | |
485 | ||
486 | // pre-fill the filename when saving, too (there's no text entry | |
487 | // control when opening a file, so it doesn't make sense to | |
488 | // do this when opening files): | |
489 | if (GetWindowStyle() & wxSAVE) | |
490 | { | |
491 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), | |
492 | wxGTK_CONV(m_fileName)); | |
493 | } | |
494 | } | |
495 | #endif | |
496 | } | |
497 | ||
498 | void wxFileDialog::UpdateFromDialog() | |
499 | { | |
500 | #ifdef __WXGTK24__ | |
501 | // update filterIndex | |
502 | GSList *fnode = gtk_file_chooser_list_filters(GTK_FILE_CHOOSER(m_widget)); | |
503 | GSList *filters = fnode; | |
504 | GtkFileFilter *current = | |
505 | gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(m_widget)); | |
506 | ||
507 | int i = 0; | |
508 | m_filterIndex = 0; | |
509 | while (fnode) | |
510 | { | |
511 | if (fnode->data == (gpointer)current) | |
512 | { | |
513 | m_filterIndex = i; | |
514 | break; | |
515 | } | |
516 | i++; | |
517 | fnode = fnode->next; | |
518 | } | |
519 | g_slist_free(filters); | |
520 | #endif | |
521 | } | |
522 | ||
523 | #endif // wxUSE_FILEDLG |