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