]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/filedlg.cpp
60fc46cdb8bbf9dbd1b00ec1102eec3607b0752d
[wxWidgets.git] / src / gtk / filedlg.cpp
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 && defined(__WXGTK24__)
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 if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
46 {
47 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
48 {
49 wxString msg;
50
51 msg.Printf(
52 _("File '%s' already exists, do you really want to overwrite it?"),
53 wxString(wxConvFileName->cMB2WX(filename)).c_str());
54
55 wxMessageDialog dlg(dialog, msg, _("Confirm"),
56 wxYES_NO | wxICON_QUESTION);
57 if (dlg.ShowModal() != wxID_YES)
58 return;
59 }
60 }
61
62 // change to the directory where the user went if asked
63 if (style & wxFD_CHANGE_DIR)
64 {
65 // Use chdir to not care about filename encodings
66 wxGtkString folder(g_path_get_dirname(filename));
67 chdir(folder);
68 }
69
70 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
71 event.SetEventObject(dialog);
72 dialog->GetEventHandler()->ProcessEvent(event);
73 }
74 }
75
76 //-----------------------------------------------------------------------------
77 // "clicked" for Cancel-button
78 //-----------------------------------------------------------------------------
79
80 extern "C"
81 {
82
83 static void gtk_filedialog_cancel_callback(GtkWidget *w, wxFileDialog *dialog)
84 {
85 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
86 event.SetEventObject(dialog);
87 dialog->GetEventHandler()->ProcessEvent(event);
88 }
89
90 static void gtk_filedialog_response_callback(GtkWidget *w,
91 gint response,
92 wxFileDialog *dialog)
93 {
94 if (response == GTK_RESPONSE_ACCEPT)
95 gtk_filedialog_ok_callback(w, dialog);
96 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
97 gtk_filedialog_cancel_callback(w, dialog);
98 }
99
100 static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser,
101 gpointer user_data)
102 {
103 #if GTK_CHECK_VERSION(2,4,0)
104 GtkWidget *preview = GTK_WIDGET(user_data);
105
106 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
107
108 if ( !filename )
109 return;
110
111 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL);
112 gboolean have_preview = pixbuf != NULL;
113
114 gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf);
115 if ( pixbuf )
116 g_object_unref (pixbuf);
117
118 gtk_file_chooser_set_preview_widget_active(chooser, have_preview);
119 #else
120 wxUnusedVar(chooser);
121 wxUnusedVar(user_data);
122 #endif // GTK+ 2.4+
123 }
124
125 } // extern "C"
126
127
128 //-----------------------------------------------------------------------------
129 // wxFileDialog
130 //-----------------------------------------------------------------------------
131
132 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog)
133
134 BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog)
135 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
136 END_EVENT_TABLE()
137
138 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
139 const wxString& defaultDir,
140 const wxString& defaultFileName,
141 const wxString& wildCard,
142 long style, const wxPoint& pos,
143 const wxSize& sz,
144 const wxString& name)
145 : wxGenericFileDialog(parent, message, defaultDir, defaultFileName,
146 wildCard, style, pos, sz, name, true )
147 {
148 if (gtk_check_version(2,4,0))
149 {
150 wxGenericFileDialog::Create( parent, message, defaultDir,
151 defaultFileName, wildCard, style, pos );
152 return;
153 }
154
155 m_needParent = false;
156
157 parent = GetParentForModalDialog(parent);
158
159 if (!PreCreation(parent, pos, wxDefaultSize) ||
160 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
161 wxDefaultValidator, wxT("filedialog")))
162 {
163 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
164 return;
165 }
166
167 GtkFileChooserAction gtk_action;
168 GtkWindow* gtk_parent = NULL;
169 if (parent)
170 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
171
172 const gchar* ok_btn_stock;
173 if ( style & wxFD_SAVE )
174 {
175 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
176 ok_btn_stock = GTK_STOCK_SAVE;
177 }
178 else
179 {
180 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
181 ok_btn_stock = GTK_STOCK_OPEN;
182 }
183
184 m_widget = gtk_file_chooser_dialog_new(
185 wxGTK_CONV(m_message),
186 gtk_parent,
187 gtk_action,
188 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
189 ok_btn_stock, GTK_RESPONSE_ACCEPT,
190 NULL);
191
192 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
193
194 if ( style & wxFD_MULTIPLE )
195 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true);
196
197 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
198 // destroys the dialog when the user press ESC on the dialog: in that case
199 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
200 // errors...
201 g_signal_connect (G_OBJECT(m_widget),
202 "delete_event",
203 G_CALLBACK (gtk_widget_hide_on_delete),
204 (gpointer)this);
205
206 // local-only property could be set to false to allow non-local files to be
207 // loaded. In that case get/set_uri(s) should be used instead of
208 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
209 // probably also be created with a backend, e.g "gnome-vfs", "default", ...
210 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
211 // as the default - true:
212 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
213
214 g_signal_connect (m_widget, "response",
215 G_CALLBACK (gtk_filedialog_response_callback), this);
216
217 SetWildcard(wildCard);
218
219 // if defaultDir is specified it should contain the directory and
220 // defaultFileName should contain the default name of the file, however if
221 // directory is not given, defaultFileName contains both
222 wxFileName fn;
223 if ( defaultDir.empty() )
224 fn.Assign(defaultFileName);
225 else if ( !defaultFileName.empty() )
226 fn.Assign(defaultDir, defaultFileName);
227 else
228 fn.AssignDir(defaultDir);
229
230 // set the initial file name and/or directory
231 const wxString dir = fn.GetPath();
232 if ( !dir.empty() )
233 {
234 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
235 dir.fn_str());
236 }
237
238 const wxString fname = fn.GetFullName();
239 if ( style & wxFD_SAVE )
240 {
241 if ( !fname.empty() )
242 {
243 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
244 fname.fn_str());
245 }
246
247 #if GTK_CHECK_VERSION(2,7,3)
248 if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3))
249 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget), TRUE);
250 #endif
251 }
252 else // wxFD_OPEN
253 {
254 if ( !fname.empty() )
255 {
256 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
257 fn.GetFullPath().fn_str());
258 }
259 }
260
261 #if GTK_CHECK_VERSION(2,4,0)
262 if ( style & wxFD_PREVIEW )
263 {
264 GtkWidget *previewImage = gtk_image_new();
265
266 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget),
267 previewImage);
268 g_signal_connect(m_widget, "update-preview",
269 G_CALLBACK(gtk_filedialog_update_preview_callback),
270 previewImage);
271 }
272 #endif // GTK+ 2.4+
273 }
274
275 void wxFileDialog::OnFakeOk( wxCommandEvent &event )
276 {
277 if (!gtk_check_version(2,4,0))
278 EndDialog(wxID_OK);
279 else
280 wxGenericFileDialog::OnListOk( event );
281 }
282
283 int wxFileDialog::ShowModal()
284 {
285 if (!gtk_check_version(2,4,0))
286 return wxDialog::ShowModal();
287 else
288 return wxGenericFileDialog::ShowModal();
289 }
290
291 bool wxFileDialog::Show( bool show )
292 {
293 if (!gtk_check_version(2,4,0))
294 return wxDialog::Show( show );
295 else
296 return wxGenericFileDialog::Show( show );
297 }
298
299 void wxFileDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags )
300 {
301 if (!m_wxwindow)
302 return;
303 else
304 wxGenericFileDialog::DoSetSize( x, y, width, height, sizeFlags );
305 }
306
307 wxString wxFileDialog::GetPath() const
308 {
309 if (!gtk_check_version(2,4,0))
310 {
311 wxGtkString str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget)));
312 return wxConvFileName->cMB2WX(str);
313 }
314
315 return wxGenericFileDialog::GetPath();
316 }
317
318 void wxFileDialog::GetFilenames(wxArrayString& files) const
319 {
320 if (!gtk_check_version(2,4,0))
321 {
322 GetPaths(files);
323 for (size_t n = 0; n < files.GetCount(); ++n )
324 {
325 wxFileName file(files[n]);
326 files[n] = file.GetFullName();
327 }
328 }
329 else
330 wxGenericFileDialog::GetFilenames( files );
331 }
332
333 void wxFileDialog::GetPaths(wxArrayString& paths) const
334 {
335 if (!gtk_check_version(2,4,0))
336 {
337 paths.Empty();
338 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget)))
339 {
340 GSList *gpathsi = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget));
341 GSList *gpaths = gpathsi;
342 while (gpathsi)
343 {
344 wxString file(wxConvFileName->cMB2WX((gchar*) gpathsi->data));
345 paths.Add(file);
346 g_free(gpathsi->data);
347 gpathsi = gpathsi->next;
348 }
349
350 g_slist_free(gpaths);
351 }
352 else
353 paths.Add(GetPath());
354 }
355 else
356 wxGenericFileDialog::GetPaths( paths );
357 }
358
359 void wxFileDialog::SetMessage(const wxString& message)
360 {
361 if (!gtk_check_version(2,4,0))
362 {
363 m_message = message;
364 SetTitle(message);
365 }
366 else
367 wxGenericFileDialog::SetMessage( message );
368 }
369
370 void wxFileDialog::SetPath(const wxString& path)
371 {
372 if (!gtk_check_version(2,4,0))
373 {
374 if (path.empty()) return;
375
376 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(path));
377 }
378 else
379 wxGenericFileDialog::SetPath( path );
380 }
381
382 void wxFileDialog::SetDirectory(const wxString& dir)
383 {
384 if (!gtk_check_version(2,4,0))
385 {
386 if (wxDirExists(dir))
387 {
388 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir));
389 }
390 }
391 else
392 wxGenericFileDialog::SetDirectory( dir );
393 }
394
395 wxString wxFileDialog::GetDirectory() const
396 {
397 if (!gtk_check_version(2,4,0))
398 {
399 wxGtkString str(gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(m_widget)));
400 return wxConvFileName->cMB2WX(str);
401 }
402
403 return wxGenericFileDialog::GetDirectory();
404 }
405
406 void wxFileDialog::SetFilename(const wxString& name)
407 {
408 if (!gtk_check_version(2,4,0))
409 {
410 if (HasFdFlag(wxFD_SAVE))
411 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
412 else
413 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
414 }
415 else
416 wxGenericFileDialog::SetFilename( name );
417 }
418
419 wxString wxFileDialog::GetFilename() const
420 {
421 if (!gtk_check_version(2,4,0))
422 return wxFileName(GetPath()).GetFullName();
423 else
424 return wxGenericFileDialog::GetFilename();
425 }
426
427 void wxFileDialog::SetWildcard(const wxString& wildCard)
428 {
429 if (!gtk_check_version(2,4,0))
430 {
431 // parse filters
432 wxArrayString wildDescriptions, wildFilters;
433 if (!wxParseCommonDialogsFilter(wildCard, wildDescriptions, wildFilters))
434 {
435 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
436 }
437 else
438 {
439 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
440 m_wildCard = wildCard;
441
442 GtkFileChooser* chooser = GTK_FILE_CHOOSER(m_widget);
443
444 // empty current filter list:
445 GSList* ifilters = gtk_file_chooser_list_filters(chooser);
446 GSList* filters = ifilters;
447
448 while (ifilters)
449 {
450 gtk_file_chooser_remove_filter(chooser,GTK_FILE_FILTER(ifilters->data));
451 ifilters = ifilters->next;
452 }
453 g_slist_free(filters);
454
455 // add parsed to GtkChooser
456 for (size_t n = 0; n < wildFilters.GetCount(); ++n)
457 {
458 GtkFileFilter* filter = gtk_file_filter_new();
459 gtk_file_filter_set_name(filter, wxGTK_CONV(wildDescriptions[n]));
460
461 wxStringTokenizer exttok(wildFilters[n], wxT(";"));
462 while (exttok.HasMoreTokens())
463 {
464 wxString token = exttok.GetNextToken();
465 gtk_file_filter_add_pattern(filter, wxGTK_CONV(token));
466 }
467
468 gtk_file_chooser_add_filter(chooser, filter);
469 }
470
471 // Reset the filter index
472 SetFilterIndex(0);
473 }
474 }
475 else
476 wxGenericFileDialog::SetWildcard( wildCard );
477 }
478
479 void wxFileDialog::SetFilterIndex(int filterIndex)
480 {
481
482 if (!gtk_check_version(2,4,0))
483 {
484 gpointer filter;
485 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
486 GSList *filters = gtk_file_chooser_list_filters(chooser);
487
488 filter = g_slist_nth_data(filters, filterIndex);
489
490 if (filter != NULL)
491 {
492 gtk_file_chooser_set_filter(chooser, GTK_FILE_FILTER(filter));
493 }
494 else
495 {
496 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
497 }
498
499 g_slist_free(filters);
500 }
501 else
502 wxGenericFileDialog::SetFilterIndex( filterIndex );
503 }
504
505 int wxFileDialog::GetFilterIndex() const
506 {
507 if (!gtk_check_version(2,4,0))
508 {
509 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
510 GtkFileFilter *filter = gtk_file_chooser_get_filter(chooser);
511 GSList *filters = gtk_file_chooser_list_filters(chooser);
512 gint index = g_slist_index(filters, filter);
513 g_slist_free(filters);
514
515 if (index == -1)
516 {
517 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
518 return 0;
519 }
520 else
521 return index;
522 }
523 else
524 return wxGenericFileDialog::GetFilterIndex();
525 }
526
527 #endif // wxUSE_FILEDLG && __WXGTK24__