]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/filedlg.cpp
Route data from wxDataViewModel in a wxVariant
[wxWidgets.git] / src / gtk / filedlg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
9b5f1895 2// Name: src/gtk/filedlg.cpp
9755e73b 3// Purpose: native implementation of wxFileDialog
f8bc53eb 4// Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
a81258be 5// Id: $Id$
f8bc53eb 6// Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
4e1901b7 13#if wxUSE_FILEDLG
9755e73b 14
c801d85f 15#include "wx/filedlg.h"
4e1901b7
RR
16
17#ifdef __WXGTK24__
f8bc53eb
JS
18
19#include <gtk/gtk.h>
9755e73b 20#include "wx/gtk/private.h"
83624f79 21
f8bc53eb
JS
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
acfd422a
RR
30//-----------------------------------------------------------------------------
31// idle system
32//-----------------------------------------------------------------------------
33
34extern void wxapp_install_idle_handler();
acfd422a 35
291a8f20
RR
36//-----------------------------------------------------------------------------
37// "clicked" for OK-button
c801d85f
KB
38//-----------------------------------------------------------------------------
39
865bb325 40extern "C" {
9755e73b 41static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
c801d85f 42{
2748d251 43 int style = dialog->GetStyle();
f8bc53eb 44 gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
035b704a 45
b5ab6d19
MR
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
9755e73b 50 if ((style & wxSAVE) && (style & wxOVERWRITE_PROMPT))
bfc6fde4 51 {
f8bc53eb 52 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
0e1399b3
VZ
53 {
54 wxString msg;
f8bc53eb 55
9755e73b 56 msg.Printf(
f8bc53eb
JS
57 _("File '%s' already exists, do you really want to overwrite it?"),
58 wxString(wxConvFileName->cMB2WX(filename)).c_str());
0e1399b3 59
9755e73b
VS
60 wxMessageDialog dlg(dialog, msg, _("Confirm"),
61 wxYES_NO | wxICON_QUESTION);
62 if (dlg.ShowModal() != wxID_YES)
0e1399b3
VZ
63 return;
64 }
83624f79 65 }
035b704a 66
3f6638b8 67 // change to the directory where the user went if asked
9755e73b 68 if (style & wxCHANGE_DIR)
3f6638b8 69 {
f8bc53eb
JS
70 // Use chdir to not care about filename encodings
71 gchar* folder = g_path_get_dirname(filename);
72 chdir(folder);
73 g_free(folder);
3f6638b8
VZ
74 }
75
f8bc53eb 76 g_free(filename);
27b2dd53 77
bfc6fde4 78 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
9755e73b
VS
79 event.SetEventObject(dialog);
80 dialog->GetEventHandler()->ProcessEvent(event);
ff7b1510 81}
865bb325 82}
c801d85f 83
291a8f20
RR
84//-----------------------------------------------------------------------------
85// "clicked" for Cancel-button
86//-----------------------------------------------------------------------------
87
865bb325 88extern "C" {
9755e73b
VS
89static void gtk_filedialog_cancel_callback(GtkWidget *WXUNUSED(w),
90 wxFileDialog *dialog)
c801d85f 91{
bfc6fde4 92 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
9755e73b
VS
93 event.SetEventObject(dialog);
94 dialog->GetEventHandler()->ProcessEvent(event);
95}
865bb325 96}
9755e73b 97
865bb325 98extern "C" {
9755e73b 99static void gtk_filedialog_response_callback(GtkWidget *w,
f8bc53eb 100 gint response,
9755e73b
VS
101 wxFileDialog *dialog)
102{
103 wxapp_install_idle_handler();
27b2dd53 104
a7334928 105 if (response == GTK_RESPONSE_ACCEPT)
9755e73b 106 gtk_filedialog_ok_callback(w, dialog);
c2740a5a
RR
107 else if (response == GTK_RESPONSE_CANCEL)
108 gtk_filedialog_cancel_callback(w, dialog);
109 else // "delete"
110 {
a7334928 111 gtk_filedialog_cancel_callback(w, dialog);
27b2dd53 112 dialog->m_destroyed_by_delete = true;
c2740a5a 113 }
ff7b1510 114}
865bb325
VZ
115}
116
117#endif // __WXGTK24__
c801d85f 118
291a8f20
RR
119//-----------------------------------------------------------------------------
120// wxFileDialog
121//-----------------------------------------------------------------------------
122
4e1901b7
RR
123IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog)
124
125BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog)
f8bc53eb 126 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
4e1901b7 127END_EVENT_TABLE()
c801d85f 128
9755e73b
VS
129wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
130 const wxString& defaultDir,
131 const wxString& defaultFileName,
132 const wxString& wildCard,
133 long style, const wxPoint& pos)
4e1901b7
RR
134 : wxGenericFileDialog(parent, message, defaultDir, defaultFileName,
135 wildCard, style, pos, true )
c801d85f 136{
4e1901b7 137#ifdef __WXGTK24__
77f70672
RR
138 if (!gtk_check_version(2,4,0))
139 {
f8bc53eb 140 wxASSERT_MSG( !( (style & wxSAVE) && (style & wxMULTIPLE) ), wxT("wxFileDialog - wxMULTIPLE used on a save dialog" ) );
27b2dd53
WS
141 m_needParent = false;
142 m_destroyed_by_delete = false;
83624f79 143
77f70672
RR
144 if (!PreCreation(parent, pos, wxDefaultSize) ||
145 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
9755e73b 146 wxDefaultValidator, wxT("filedialog")))
77f70672
RR
147 {
148 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
149 return;
150 }
3f6638b8 151
77f70672
RR
152 GtkFileChooserAction gtk_action;
153 GtkWindow* gtk_parent = NULL;
154 if (parent)
29a7bba6 155 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
27b2dd53 156
fbfb8bcc 157 const gchar* ok_btn_stock;
f8bc53eb 158 if ( style & wxSAVE )
77f70672
RR
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 }
f8bc53eb 168
77f70672 169 m_widget = gtk_file_chooser_dialog_new(
9755e73b
VS
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
f8bc53eb
JS
177 if ( style & wxMULTIPLE )
178 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true);
27b2dd53 179
f8bc53eb
JS
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);
27b2dd53 186
f8bc53eb
JS
187 g_signal_connect(G_OBJECT(m_widget), "response",
188 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback), (gpointer)this);
27b2dd53 189
77f70672 190 SetWildcard(wildCard);
f8bc53eb
JS
191
192 if ( style & wxSAVE )
193 {
da865fdd 194 if ( !defaultDir.empty() )
f8bc53eb
JS
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));
b5ab6d19
MR
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
f8bc53eb
JS
205 }
206 else
207 {
da865fdd 208 if ( !defaultFileName.empty() )
f8bc53eb
JS
209 {
210 wxString dir;
da865fdd 211 if ( defaultDir.empty() )
f8bc53eb
JS
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 }
da865fdd 220 else if ( !defaultDir.empty() )
f8bc53eb
JS
221 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget),
222 wxConvFileName->cWX2MB(defaultDir) );
223 }
77f70672
RR
224 }
225 else
4e1901b7 226#endif
77f70672 227 wxGenericFileDialog::Create( parent, message, defaultDir, defaultFileName, wildCard, style, pos );
9755e73b 228}
0e1399b3 229
76840ed0 230wxFileDialog::~wxFileDialog()
9755e73b 231{
4e1901b7 232#ifdef __WXGTK24__
77f70672
RR
233 if (!gtk_check_version(2,4,0))
234 {
235 if (m_destroyed_by_delete)
236 m_widget = NULL;
237 }
4e1901b7
RR
238#endif
239}
240
241void wxFileDialog::OnFakeOk( wxCommandEvent &event )
242{
243#ifdef __WXGTK24__
77f70672
RR
244 if (!gtk_check_version(2,4,0))
245 wxDialog::OnOK( event );
246 else
4e1901b7 247#endif
77f70672 248 wxGenericFileDialog::OnListOk( event );
4e1901b7
RR
249}
250
251int wxFileDialog::ShowModal()
252{
253#ifdef __WXGTK24__
77f70672
RR
254 if (!gtk_check_version(2,4,0))
255 return wxDialog::ShowModal();
256 else
4e1901b7 257#endif
77f70672 258 return wxGenericFileDialog::ShowModal();
4e1901b7
RR
259}
260
261bool wxFileDialog::Show( bool show )
262{
263#ifdef __WXGTK24__
77f70672
RR
264 if (!gtk_check_version(2,4,0))
265 return wxDialog::Show( show );
266 else
4e1901b7 267#endif
77f70672 268 return wxGenericFileDialog::Show( show );
9755e73b 269}
0e1399b3 270
5b2e23bf
RR
271void 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
f8bc53eb
JS
279wxString 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
27b2dd53 289void wxFileDialog::GetFilenames(wxArrayString& files) const
9755e73b 290{
4e1901b7 291#ifdef __WXGTK24__
77f70672 292 if (!gtk_check_version(2,4,0))
9755e73b 293 {
77f70672 294 GetPaths(files);
f8bc53eb 295 for (size_t n = 0; n < files.GetCount(); ++n )
9755e73b 296 {
f8bc53eb
JS
297 wxFileName file(files[n]);
298 files[n] = file.GetFullName();
9755e73b 299 }
9755e73b 300 }
77f70672 301 else
4e1901b7 302#endif
77f70672 303 wxGenericFileDialog::GetFilenames( files );
9755e73b 304}
76840ed0 305
27b2dd53 306void wxFileDialog::GetPaths(wxArrayString& paths) const
9755e73b 307{
4e1901b7 308#ifdef __WXGTK24__
77f70672 309 if (!gtk_check_version(2,4,0))
9755e73b 310 {
27b2dd53 311 paths.Empty();
f8bc53eb 312 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget)))
9755e73b 313 {
f8bc53eb 314 GSList *gpathsi = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget));
77f70672
RR
315 GSList *gpaths = gpathsi;
316 while (gpathsi)
317 {
f8bc53eb
JS
318 wxString file(wxConvFileName->cMB2WX((gchar*) gpathsi->data));
319 paths.Add(file);
77f70672
RR
320 g_free(gpathsi->data);
321 gpathsi = gpathsi->next;
322 }
0832aaaf 323
f8bc53eb 324 g_slist_free(gpaths);
9755e73b 325 }
f8bc53eb
JS
326 else
327 paths.Add(GetPath());
9755e73b
VS
328 }
329 else
4e1901b7 330#endif
77f70672 331 wxGenericFileDialog::GetPaths( paths );
9755e73b 332}
035b704a 333
9755e73b
VS
334void wxFileDialog::SetMessage(const wxString& message)
335{
4e1901b7 336#ifdef __WXGTK24__
77f70672
RR
337 if (!gtk_check_version(2,4,0))
338 {
339 m_message = message;
340 SetTitle(message);
341 }
342 else
27b2dd53 343#endif
77f70672 344 wxGenericFileDialog::SetMessage( message );
9755e73b
VS
345}
346
76840ed0
RR
347void wxFileDialog::SetPath(const wxString& path)
348{
4e1901b7 349#ifdef __WXGTK24__
77f70672
RR
350 if (!gtk_check_version(2,4,0))
351 {
352 if (path.empty()) return;
353
f8bc53eb 354 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(path));
77f70672
RR
355 }
356 else
27b2dd53 357#endif
77f70672 358 wxGenericFileDialog::SetPath( path );
76840ed0
RR
359}
360
9755e73b
VS
361void wxFileDialog::SetDirectory(const wxString& dir)
362{
4e1901b7 363#ifdef __WXGTK24__
77f70672 364 if (!gtk_check_version(2,4,0))
76840ed0 365 {
da865fdd 366 if (wxDirExists(dir))
77f70672 367 {
f8bc53eb 368 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir));
77f70672 369 }
76840ed0 370 }
77f70672 371 else
27b2dd53 372#endif
77f70672 373 wxGenericFileDialog::SetDirectory( dir );
9755e73b
VS
374}
375
f8bc53eb
JS
376wxString 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
9755e73b
VS
387void wxFileDialog::SetFilename(const wxString& name)
388{
4e1901b7 389#ifdef __WXGTK24__
77f70672
RR
390 if (!gtk_check_version(2,4,0))
391 {
f8bc53eb
JS
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());
77f70672
RR
396 }
397 else
4e1901b7 398#endif
77f70672 399 wxGenericFileDialog::SetFilename( name );
9755e73b 400}
035b704a 401
f8bc53eb
JS
402wxString 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
9755e73b
VS
413void wxFileDialog::SetWildcard(const wxString& wildCard)
414{
4e1901b7 415#ifdef __WXGTK24__
77f70672 416 if (!gtk_check_version(2,4,0))
9755e73b 417 {
77f70672
RR
418 // parse filters
419 wxArrayString wildDescriptions, wildFilters;
f8bc53eb 420 if (!wxParseCommonDialogsFilter(wildCard, wildDescriptions, wildFilters))
77f70672 421 {
f8bc53eb 422 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
77f70672
RR
423 }
424 else
9755e73b 425 {
f8bc53eb
JS
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
77f70672 442 // add parsed to GtkChooser
f8bc53eb 443 for (size_t n = 0; n < wildFilters.GetCount(); ++n)
9755e73b 444 {
77f70672 445 GtkFileFilter* filter = gtk_file_filter_new();
f8bc53eb
JS
446 gtk_file_filter_set_name(filter, wxGTK_CONV(wildDescriptions[n]));
447
448 wxStringTokenizer exttok(wildFilters[n], wxT(";"));
449 while (exttok.HasMoreTokens())
77f70672 450 {
f8bc53eb
JS
451 wxString token = exttok.GetNextToken();
452 gtk_file_filter_add_pattern(filter, wxGTK_CONV(token));
77f70672 453 }
27b2dd53 454
77f70672
RR
455 gtk_file_chooser_add_filter(chooser, filter);
456 }
f8bc53eb
JS
457
458 // Reset the filter index
459 SetFilterIndex(0);
9755e73b
VS
460 }
461 }
77f70672 462 else
4e1901b7 463#endif
77f70672 464 wxGenericFileDialog::SetWildcard( wildCard );
9755e73b 465}
a3622daa 466
9755e73b
VS
467void wxFileDialog::SetFilterIndex(int filterIndex)
468{
4e1901b7 469#ifdef __WXGTK24__
77f70672 470 if (!gtk_check_version(2,4,0))
9755e73b 471 {
f8bc53eb 472 gpointer filter;
77f70672 473 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
f8bc53eb
JS
474 GSList *filters = gtk_file_chooser_list_filters(chooser);
475
476 filter = g_slist_nth_data(filters, filterIndex);
477
478 if (filter != NULL)
9755e73b 479 {
f8bc53eb
JS
480 gtk_file_chooser_set_filter(chooser, GTK_FILE_FILTER(filter));
481 }
482 else
483 {
484 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
9755e73b 485 }
f8bc53eb 486
77f70672 487 g_slist_free(filters);
9755e73b 488 }
77f70672 489 else
4e1901b7 490#endif
77f70672 491 wxGenericFileDialog::SetFilterIndex( filterIndex );
4e1901b7
RR
492}
493
f8bc53eb 494int wxFileDialog::GetFilterIndex() const
4e1901b7
RR
495{
496#ifdef __WXGTK24__
f8bc53eb 497 if (!gtk_check_version(2,4,0))
4e1901b7 498 {
f8bc53eb
JS
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);
3502e687 504
f8bc53eb 505 if (index == -1)
9755e73b 506 {
f8bc53eb
JS
507 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
508 return 0;
9755e73b 509 }
f8bc53eb
JS
510 else
511 return index;
9755e73b 512 }
f8bc53eb 513 else
4e1901b7 514#endif
f8bc53eb 515 return wxGenericFileDialog::GetFilterIndex();
9755e73b 516}
3f6638b8 517
4e1901b7 518#endif // wxUSE_FILEDLG