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"
13 #if wxUSE_FILEDLG && defined(__WXGTK24__)
15 #include "wx/filedlg.h"
19 #include "wx/msgdlg.h"
23 #include "wx/gtk/private.h"
25 #include <unistd.h> // chdir
27 #include "wx/filename.h" // wxFilename
28 #include "wx/tokenzr.h" // wxStringTokenizer
29 #include "wx/filefn.h" // ::wxGetCwd
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern void wxapp_install_idle_handler();
37 //-----------------------------------------------------------------------------
38 // "clicked" for OK-button
39 //-----------------------------------------------------------------------------
42 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
44 int style
= dialog
->GetWindowStyle();
45 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
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
)
51 if ((style
& wxFD_SAVE
) && (style
& wxFD_OVERWRITE_PROMPT
))
53 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
58 _("File '%s' already exists, do you really want to overwrite it?"),
59 wxString(wxConvFileName
->cMB2WX(filename
)).c_str());
61 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
62 wxYES_NO
| wxICON_QUESTION
);
63 if (dlg
.ShowModal() != wxID_YES
)
68 // change to the directory where the user went if asked
69 if (style
& wxFD_CHANGE_DIR
)
71 // Use chdir to not care about filename encodings
72 gchar
* folder
= g_path_get_dirname(filename
);
79 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
80 event
.SetEventObject(dialog
);
81 dialog
->GetEventHandler()->ProcessEvent(event
);
85 //-----------------------------------------------------------------------------
86 // "clicked" for Cancel-button
87 //-----------------------------------------------------------------------------
90 static void gtk_filedialog_cancel_callback(GtkWidget
*w
, wxFileDialog
*dialog
)
92 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
93 event
.SetEventObject(dialog
);
94 dialog
->GetEventHandler()->ProcessEvent(event
);
99 static void gtk_filedialog_response_callback(GtkWidget
*w
,
101 wxFileDialog
*dialog
)
103 wxapp_install_idle_handler();
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
);
113 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
117 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
119 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
120 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
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
,
129 const wxString
& name
)
130 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
131 wildCard
, style
, pos
, sz
, name
, true )
133 if (!gtk_check_version(2,4,0))
135 m_needParent
= false;
137 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
138 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
139 wxDefaultValidator
, wxT("filedialog")))
141 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
145 GtkFileChooserAction gtk_action
;
146 GtkWindow
* gtk_parent
= NULL
;
148 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
150 const gchar
* ok_btn_stock
;
151 if ( style
& wxFD_SAVE
)
153 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
154 ok_btn_stock
= GTK_STOCK_SAVE
;
158 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
159 ok_btn_stock
= GTK_STOCK_OPEN
;
162 m_widget
= gtk_file_chooser_dialog_new(
163 wxGTK_CONV(m_message
),
166 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
167 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
170 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
172 if ( style
& wxFD_MULTIPLE
)
173 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
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
),
180 G_CALLBACK (gtk_widget_hide_on_delete
),
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 (m_widget
, "response",
191 G_CALLBACK (gtk_filedialog_response_callback
), this);
193 SetWildcard(wildCard
);
195 if ( style
& wxFD_SAVE
)
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 ((style
& wxOVERWRITE_PROMPT
) && !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
) );
229 wxGenericFileDialog::Create( parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
);
232 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
234 if (!gtk_check_version(2,4,0))
235 wxDialog::OnOK( event
);
237 wxGenericFileDialog::OnListOk( event
);
240 int wxFileDialog::ShowModal()
242 if (!gtk_check_version(2,4,0))
243 return wxDialog::ShowModal();
245 return wxGenericFileDialog::ShowModal();
248 bool wxFileDialog::Show( bool show
)
250 if (!gtk_check_version(2,4,0))
251 return wxDialog::Show( show
);
253 return wxGenericFileDialog::Show( show
);
256 void wxFileDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
261 wxGenericFileDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
264 wxString
wxFileDialog::GetPath() const
266 if (!gtk_check_version(2,4,0))
267 return wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
269 return wxGenericFileDialog::GetPath();
272 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
274 if (!gtk_check_version(2,4,0))
277 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
279 wxFileName
file(files
[n
]);
280 files
[n
] = file
.GetFullName();
284 wxGenericFileDialog::GetFilenames( files
);
287 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
289 if (!gtk_check_version(2,4,0))
292 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
294 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
295 GSList
*gpaths
= gpathsi
;
298 wxString
file(wxConvFileName
->cMB2WX((gchar
*) gpathsi
->data
));
300 g_free(gpathsi
->data
);
301 gpathsi
= gpathsi
->next
;
304 g_slist_free(gpaths
);
307 paths
.Add(GetPath());
310 wxGenericFileDialog::GetPaths( paths
);
313 void wxFileDialog::SetMessage(const wxString
& message
)
315 if (!gtk_check_version(2,4,0))
321 wxGenericFileDialog::SetMessage( message
);
324 void wxFileDialog::SetPath(const wxString
& path
)
326 if (!gtk_check_version(2,4,0))
328 if (path
.empty()) return;
330 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(path
));
333 wxGenericFileDialog::SetPath( path
);
336 void wxFileDialog::SetDirectory(const wxString
& dir
)
338 if (!gtk_check_version(2,4,0))
340 if (wxDirExists(dir
))
342 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
346 wxGenericFileDialog::SetDirectory( dir
);
349 wxString
wxFileDialog::GetDirectory() const
351 if (!gtk_check_version(2,4,0))
352 return wxConvFileName
->cMB2WX(
353 gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget
) ) );
355 return wxGenericFileDialog::GetDirectory();
358 void wxFileDialog::SetFilename(const wxString
& name
)
360 if (!gtk_check_version(2,4,0))
362 if (HasFlag(wxFD_SAVE
))
363 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(name
));
365 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
368 wxGenericFileDialog::SetFilename( name
);
371 wxString
wxFileDialog::GetFilename() const
373 if (!gtk_check_version(2,4,0))
375 wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
))) ).GetFullName();
377 return wxGenericFileDialog::GetFilename();
380 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
382 if (!gtk_check_version(2,4,0))
385 wxArrayString wildDescriptions
, wildFilters
;
386 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
388 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
392 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
393 m_wildCard
= wildCard
;
395 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
397 // empty current filter list:
398 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
399 GSList
* filters
= ifilters
;
403 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
404 ifilters
= ifilters
->next
;
406 g_slist_free(filters
);
408 // add parsed to GtkChooser
409 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
411 GtkFileFilter
* filter
= gtk_file_filter_new();
412 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
414 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
415 while (exttok
.HasMoreTokens())
417 wxString token
= exttok
.GetNextToken();
418 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
421 gtk_file_chooser_add_filter(chooser
, filter
);
424 // Reset the filter index
429 wxGenericFileDialog::SetWildcard( wildCard
);
432 void wxFileDialog::SetFilterIndex(int filterIndex
)
435 if (!gtk_check_version(2,4,0))
438 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
439 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
441 filter
= g_slist_nth_data(filters
, filterIndex
);
445 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
449 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
452 g_slist_free(filters
);
455 wxGenericFileDialog::SetFilterIndex( filterIndex
);
458 int wxFileDialog::GetFilterIndex() const
460 if (!gtk_check_version(2,4,0))
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
);
470 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
477 return wxGenericFileDialog::GetFilterIndex();
480 #endif // wxUSE_FILEDLG && __WXGTK24__