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 wxGtkString
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 wxGtkString
folder(g_path_get_dirname(filename
));
76 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
77 event
.SetEventObject(dialog
);
78 dialog
->GetEventHandler()->ProcessEvent(event
);
82 //-----------------------------------------------------------------------------
83 // "clicked" for Cancel-button
84 //-----------------------------------------------------------------------------
89 static void gtk_filedialog_cancel_callback(GtkWidget
*w
, wxFileDialog
*dialog
)
91 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
92 event
.SetEventObject(dialog
);
93 dialog
->GetEventHandler()->ProcessEvent(event
);
96 static void gtk_filedialog_response_callback(GtkWidget
*w
,
100 wxapp_install_idle_handler();
102 if (response
== GTK_RESPONSE_ACCEPT
)
103 gtk_filedialog_ok_callback(w
, dialog
);
104 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
105 gtk_filedialog_cancel_callback(w
, dialog
);
108 static void gtk_filedialog_update_preview_callback(GtkFileChooser
*chooser
,
111 #if GTK_CHECK_VERSION(2,4,0)
112 GtkWidget
*preview
= GTK_WIDGET(user_data
);
114 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
119 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
120 gboolean have_preview
= pixbuf
!= NULL
;
122 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
124 g_object_unref (pixbuf
);
126 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
128 wxUnusedVar(chooser
);
129 wxUnusedVar(user_data
);
136 //-----------------------------------------------------------------------------
138 //-----------------------------------------------------------------------------
140 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
142 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
143 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
146 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
147 const wxString
& defaultDir
,
148 const wxString
& defaultFileName
,
149 const wxString
& wildCard
,
150 long style
, const wxPoint
& pos
,
152 const wxString
& name
)
153 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
154 wildCard
, style
, pos
, sz
, name
, true )
156 if (gtk_check_version(2,4,0))
158 wxGenericFileDialog::Create( parent
, message
, defaultDir
,
159 defaultFileName
, wildCard
, style
, pos
);
163 m_needParent
= false;
165 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
166 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
167 wxDefaultValidator
, wxT("filedialog")))
169 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
173 GtkFileChooserAction gtk_action
;
174 GtkWindow
* gtk_parent
= NULL
;
176 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
178 const gchar
* ok_btn_stock
;
179 if ( style
& wxFD_SAVE
)
181 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
182 ok_btn_stock
= GTK_STOCK_SAVE
;
186 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
187 ok_btn_stock
= GTK_STOCK_OPEN
;
190 m_widget
= gtk_file_chooser_dialog_new(
191 wxGTK_CONV(m_message
),
194 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
195 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
198 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
200 if ( style
& wxFD_MULTIPLE
)
201 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
203 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
204 // the dialog when the user press ESC on the dialog: in that case a second call to
205 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
206 g_signal_connect (G_OBJECT(m_widget
),
208 G_CALLBACK (gtk_widget_hide_on_delete
),
211 // local-only property could be set to false to allow non-local files to be loaded.
212 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
213 // and the GtkFileChooserDialog should probably also be created with a backend,
214 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
215 // Currently local-only is kept as the default - true:
216 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
218 g_signal_connect (m_widget
, "response",
219 G_CALLBACK (gtk_filedialog_response_callback
), this);
221 SetWildcard(wildCard
);
223 if ( style
& wxFD_SAVE
)
225 if ( !defaultDir
.empty() )
226 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
227 wxConvFileName
->cWX2MB(defaultDir
));
229 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
230 wxGTK_CONV(defaultFileName
));
232 #if GTK_CHECK_VERSION(2,7,3)
233 if ((style
& wxFD_OVERWRITE_PROMPT
) && !gtk_check_version(2,7,3))
234 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget
), TRUE
);
239 if ( !defaultFileName
.empty() )
242 if ( defaultDir
.empty() )
247 gtk_file_chooser_set_filename(
248 GTK_FILE_CHOOSER(m_widget
),
249 wxConvFileName
->cWX2MB( wxFileName(dir
, defaultFileName
).GetFullPath() ) );
251 else if ( !defaultDir
.empty() )
253 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
254 wxConvFileName
->cWX2MB(defaultDir
) );
258 #if GTK_CHECK_VERSION(2,4,0)
259 if ( style
& wxFD_PREVIEW
)
261 GtkWidget
*previewImage
= gtk_image_new();
263 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget
),
265 g_signal_connect(m_widget
, "update-preview",
266 G_CALLBACK(gtk_filedialog_update_preview_callback
),
272 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
274 if (!gtk_check_version(2,4,0))
277 wxGenericFileDialog::OnListOk( event
);
280 int wxFileDialog::ShowModal()
282 if (!gtk_check_version(2,4,0))
283 return wxDialog::ShowModal();
285 return wxGenericFileDialog::ShowModal();
288 bool wxFileDialog::Show( bool show
)
290 if (!gtk_check_version(2,4,0))
291 return wxDialog::Show( show
);
293 return wxGenericFileDialog::Show( show
);
296 void wxFileDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
301 wxGenericFileDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
304 wxString
wxFileDialog::GetPath() const
306 if (!gtk_check_version(2,4,0))
308 wxGtkString
str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
309 return wxConvFileName
->cMB2WX(str
);
312 return wxGenericFileDialog::GetPath();
315 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
317 if (!gtk_check_version(2,4,0))
320 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
322 wxFileName
file(files
[n
]);
323 files
[n
] = file
.GetFullName();
327 wxGenericFileDialog::GetFilenames( files
);
330 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
332 if (!gtk_check_version(2,4,0))
335 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
337 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
338 GSList
*gpaths
= gpathsi
;
341 wxString
file(wxConvFileName
->cMB2WX((gchar
*) gpathsi
->data
));
343 g_free(gpathsi
->data
);
344 gpathsi
= gpathsi
->next
;
347 g_slist_free(gpaths
);
350 paths
.Add(GetPath());
353 wxGenericFileDialog::GetPaths( paths
);
356 void wxFileDialog::SetMessage(const wxString
& message
)
358 if (!gtk_check_version(2,4,0))
364 wxGenericFileDialog::SetMessage( message
);
367 void wxFileDialog::SetPath(const wxString
& path
)
369 if (!gtk_check_version(2,4,0))
371 if (path
.empty()) return;
373 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(path
));
376 wxGenericFileDialog::SetPath( path
);
379 void wxFileDialog::SetDirectory(const wxString
& dir
)
381 if (!gtk_check_version(2,4,0))
383 if (wxDirExists(dir
))
385 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
389 wxGenericFileDialog::SetDirectory( dir
);
392 wxString
wxFileDialog::GetDirectory() const
394 if (!gtk_check_version(2,4,0))
396 wxGtkString
str(gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(m_widget
)));
397 return wxConvFileName
->cMB2WX(str
);
400 return wxGenericFileDialog::GetDirectory();
403 void wxFileDialog::SetFilename(const wxString
& name
)
405 if (!gtk_check_version(2,4,0))
407 if (HasFdFlag(wxFD_SAVE
))
408 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
410 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
413 wxGenericFileDialog::SetFilename( name
);
416 wxString
wxFileDialog::GetFilename() const
418 if (!gtk_check_version(2,4,0))
419 return wxFileName(GetPath()).GetFullName();
421 return wxGenericFileDialog::GetFilename();
424 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
426 if (!gtk_check_version(2,4,0))
429 wxArrayString wildDescriptions
, wildFilters
;
430 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
432 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
436 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
437 m_wildCard
= wildCard
;
439 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
441 // empty current filter list:
442 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
443 GSList
* filters
= ifilters
;
447 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
448 ifilters
= ifilters
->next
;
450 g_slist_free(filters
);
452 // add parsed to GtkChooser
453 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
455 GtkFileFilter
* filter
= gtk_file_filter_new();
456 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
458 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
459 while (exttok
.HasMoreTokens())
461 wxString token
= exttok
.GetNextToken();
462 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
465 gtk_file_chooser_add_filter(chooser
, filter
);
468 // Reset the filter index
473 wxGenericFileDialog::SetWildcard( wildCard
);
476 void wxFileDialog::SetFilterIndex(int filterIndex
)
479 if (!gtk_check_version(2,4,0))
482 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
483 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
485 filter
= g_slist_nth_data(filters
, filterIndex
);
489 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
493 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
496 g_slist_free(filters
);
499 wxGenericFileDialog::SetFilterIndex( filterIndex
);
502 int wxFileDialog::GetFilterIndex() const
504 if (!gtk_check_version(2,4,0))
506 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
507 GtkFileFilter
*filter
= gtk_file_chooser_get_filter(chooser
);
508 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
509 gint index
= g_slist_index(filters
, filter
);
510 g_slist_free(filters
);
514 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
521 return wxGenericFileDialog::GetFilterIndex();
524 #endif // wxUSE_FILEDLG && __WXGTK24__