1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/filedlg.cpp
3 // Purpose: native implementation of wxFileDialog
4 // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/filedlg.h"
24 #include "wx/gtk/private.h"
26 #include <unistd.h> // chdir
28 #include "wx/filename.h" // wxFilename
29 #include "wx/tokenzr.h" // wxStringTokenizer
30 #include "wx/filefn.h" // ::wxGetCwd
31 #include "wx/msgdlg.h" // wxMessageDialog
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern void wxapp_install_idle_handler();
39 //-----------------------------------------------------------------------------
40 // "clicked" for OK-button
41 //-----------------------------------------------------------------------------
44 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
46 int style
= dialog
->GetStyle();
47 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
49 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
50 #if GTK_CHECK_VERSION(2,7,3)
51 if(gtk_check_version(2,7,3) != NULL
)
53 if ((style
& wxSAVE
) && (style
& wxOVERWRITE_PROMPT
))
55 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
60 _("File '%s' already exists, do you really want to overwrite it?"),
61 wxString(wxConvFileName
->cMB2WX(filename
)).c_str());
63 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
64 wxYES_NO
| wxICON_QUESTION
);
65 if (dlg
.ShowModal() != wxID_YES
)
70 // change to the directory where the user went if asked
71 if (style
& wxCHANGE_DIR
)
73 // Use chdir to not care about filename encodings
74 gchar
* folder
= g_path_get_dirname(filename
);
81 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
82 event
.SetEventObject(dialog
);
83 dialog
->GetEventHandler()->ProcessEvent(event
);
87 //-----------------------------------------------------------------------------
88 // "clicked" for Cancel-button
89 //-----------------------------------------------------------------------------
92 static void gtk_filedialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
95 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
96 event
.SetEventObject(dialog
);
97 dialog
->GetEventHandler()->ProcessEvent(event
);
102 static void gtk_filedialog_response_callback(GtkWidget
*w
,
104 wxFileDialog
*dialog
)
106 wxapp_install_idle_handler();
108 if (response
== GTK_RESPONSE_ACCEPT
)
109 gtk_filedialog_ok_callback(w
, dialog
);
110 else if (response
== GTK_RESPONSE_CANCEL
)
111 gtk_filedialog_cancel_callback(w
, dialog
);
114 gtk_filedialog_cancel_callback(w
, dialog
);
115 dialog
->m_destroyed_by_delete
= true;
120 #endif // __WXGTK24__
122 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
126 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
128 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
129 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
132 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
133 const wxString
& defaultDir
,
134 const wxString
& defaultFileName
,
135 const wxString
& wildCard
,
136 long style
, const wxPoint
& pos
)
137 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
138 wildCard
, style
, pos
, true )
141 if (!gtk_check_version(2,4,0))
143 wxASSERT_MSG( !( (style
& wxSAVE
) && (style
& wxMULTIPLE
) ), wxT("wxFileDialog - wxMULTIPLE used on a save dialog" ) );
144 m_needParent
= false;
145 m_destroyed_by_delete
= false;
147 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
148 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
149 wxDefaultValidator
, wxT("filedialog")))
151 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
155 GtkFileChooserAction gtk_action
;
156 GtkWindow
* gtk_parent
= NULL
;
158 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
160 const gchar
* ok_btn_stock
;
161 if ( style
& wxSAVE
)
163 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
164 ok_btn_stock
= GTK_STOCK_SAVE
;
168 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
169 ok_btn_stock
= GTK_STOCK_OPEN
;
172 m_widget
= gtk_file_chooser_dialog_new(
173 wxGTK_CONV(m_message
),
176 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
177 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
180 if ( style
& wxMULTIPLE
)
181 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
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);
190 g_signal_connect(G_OBJECT(m_widget
), "response",
191 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback
), (gpointer
)this);
193 SetWildcard(wildCard
);
195 if ( style
& wxSAVE
)
197 if ( !defaultDir
.empty() )
198 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
199 wxConvFileName
->cWX2MB(defaultDir
));
201 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
202 wxConvFileName
->cWX2MB(defaultFileName
));
204 #if GTK_CHECK_VERSION(2,7,3)
205 if (!gtk_check_version(2,7,3))
206 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget
), TRUE
);
211 if ( !defaultFileName
.empty() )
214 if ( defaultDir
.empty() )
219 gtk_file_chooser_set_filename(
220 GTK_FILE_CHOOSER(m_widget
),
221 wxConvFileName
->cWX2MB( wxFileName(dir
, defaultFileName
).GetFullPath() ) );
223 else if ( !defaultDir
.empty() )
224 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
225 wxConvFileName
->cWX2MB(defaultDir
) );
230 wxGenericFileDialog::Create( parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
);
233 wxFileDialog::~wxFileDialog()
236 if (!gtk_check_version(2,4,0))
238 if (m_destroyed_by_delete
)
244 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
247 if (!gtk_check_version(2,4,0))
248 wxDialog::OnOK( event
);
251 wxGenericFileDialog::OnListOk( event
);
254 int wxFileDialog::ShowModal()
257 if (!gtk_check_version(2,4,0))
258 return wxDialog::ShowModal();
261 return wxGenericFileDialog::ShowModal();
264 bool wxFileDialog::Show( bool show
)
267 if (!gtk_check_version(2,4,0))
268 return wxDialog::Show( show
);
271 return wxGenericFileDialog::Show( show
);
274 void wxFileDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
279 wxGenericFileDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
282 wxString
wxFileDialog::GetPath() const
285 if (!gtk_check_version(2,4,0))
286 return wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
289 return wxGenericFileDialog::GetPath();
292 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
295 if (!gtk_check_version(2,4,0))
298 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
300 wxFileName
file(files
[n
]);
301 files
[n
] = file
.GetFullName();
306 wxGenericFileDialog::GetFilenames( files
);
309 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
312 if (!gtk_check_version(2,4,0))
315 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
317 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
318 GSList
*gpaths
= gpathsi
;
321 wxString
file(wxConvFileName
->cMB2WX((gchar
*) gpathsi
->data
));
323 g_free(gpathsi
->data
);
324 gpathsi
= gpathsi
->next
;
327 g_slist_free(gpaths
);
330 paths
.Add(GetPath());
334 wxGenericFileDialog::GetPaths( paths
);
337 void wxFileDialog::SetMessage(const wxString
& message
)
340 if (!gtk_check_version(2,4,0))
347 wxGenericFileDialog::SetMessage( message
);
350 void wxFileDialog::SetPath(const wxString
& path
)
353 if (!gtk_check_version(2,4,0))
355 if (path
.empty()) return;
357 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(path
));
361 wxGenericFileDialog::SetPath( path
);
364 void wxFileDialog::SetDirectory(const wxString
& dir
)
367 if (!gtk_check_version(2,4,0))
369 if (wxDirExists(dir
))
371 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
376 wxGenericFileDialog::SetDirectory( dir
);
379 wxString
wxFileDialog::GetDirectory() const
382 if (!gtk_check_version(2,4,0))
383 return wxConvFileName
->cMB2WX(
384 gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget
) ) );
387 return wxGenericFileDialog::GetDirectory();
390 void wxFileDialog::SetFilename(const wxString
& name
)
393 if (!gtk_check_version(2,4,0))
395 if (GetStyle() & wxSAVE
)
396 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(name
));
398 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
402 wxGenericFileDialog::SetFilename( name
);
405 wxString
wxFileDialog::GetFilename() const
408 if (!gtk_check_version(2,4,0))
410 wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
))) ).GetFullName();
413 return wxGenericFileDialog::GetFilename();
416 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
419 if (!gtk_check_version(2,4,0))
422 wxArrayString wildDescriptions
, wildFilters
;
423 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
425 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
429 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
430 m_wildCard
= wildCard
;
432 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
434 // empty current filter list:
435 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
436 GSList
* filters
= ifilters
;
440 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
441 ifilters
= ifilters
->next
;
443 g_slist_free(filters
);
445 // add parsed to GtkChooser
446 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
448 GtkFileFilter
* filter
= gtk_file_filter_new();
449 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
451 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
452 while (exttok
.HasMoreTokens())
454 wxString token
= exttok
.GetNextToken();
455 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
458 gtk_file_chooser_add_filter(chooser
, filter
);
461 // Reset the filter index
467 wxGenericFileDialog::SetWildcard( wildCard
);
470 void wxFileDialog::SetFilterIndex(int filterIndex
)
473 if (!gtk_check_version(2,4,0))
476 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
477 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
479 filter
= g_slist_nth_data(filters
, filterIndex
);
483 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
487 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
490 g_slist_free(filters
);
494 wxGenericFileDialog::SetFilterIndex( filterIndex
);
497 int wxFileDialog::GetFilterIndex() const
500 if (!gtk_check_version(2,4,0))
502 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
503 GtkFileFilter
*filter
= gtk_file_chooser_get_filter(chooser
);
504 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
505 gint index
= g_slist_index(filters
, filter
);
506 g_slist_free(filters
);
510 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
518 return wxGenericFileDialog::GetFilterIndex();
521 #endif // wxUSE_FILEDLG