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