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