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"
19 #include "wx/msgdlg.h"
23 #include "wx/gtk/private.h"
26 #include <unistd.h> // chdir
29 #include "wx/filename.h" // wxFilename
30 #include "wx/tokenzr.h" // wxStringTokenizer
31 #include "wx/filefn.h" // ::wxGetCwd
32 #include "wx/testing.h"
34 //-----------------------------------------------------------------------------
35 // "clicked" for OK-button
36 //-----------------------------------------------------------------------------
39 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
41 int style
= dialog
->GetWindowStyle();
42 wxGtkString
filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
)));
44 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
46 #if GTK_CHECK_VERSION(2,7,3)
47 if (gtk_check_version(2, 7, 3) != NULL
)
50 if ((style
& wxFD_SAVE
) && (style
& wxFD_OVERWRITE_PROMPT
))
52 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
57 _("File '%s' already exists, do you really want to overwrite it?"),
58 wxString::FromUTF8(filename
));
60 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
61 wxYES_NO
| wxICON_QUESTION
);
62 if (dlg
.ShowModal() != wxID_YES
)
69 if (style
& wxFD_FILE_MUST_EXIST
)
71 if ( !g_file_test(filename
, G_FILE_TEST_EXISTS
) )
73 wxMessageDialog
dlg( dialog
, _("Please choose an existing file."),
74 _("Error"), wxOK
| wxICON_ERROR
);
80 // change to the directory where the user went if asked
81 if (style
& wxFD_CHANGE_DIR
)
83 // Use chdir to not care about filename encodings
84 wxGtkString
folder(g_path_get_dirname(filename
));
88 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
89 event
.SetEventObject(dialog
);
90 dialog
->HandleWindowEvent(event
);
94 //-----------------------------------------------------------------------------
95 // "clicked" for Cancel-button
96 //-----------------------------------------------------------------------------
102 gtk_filedialog_cancel_callback(GtkWidget
* WXUNUSED(w
), wxFileDialog
*dialog
)
104 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
105 event
.SetEventObject(dialog
);
106 dialog
->HandleWindowEvent(event
);
109 static void gtk_filedialog_response_callback(GtkWidget
*w
,
111 wxFileDialog
*dialog
)
113 if (response
== GTK_RESPONSE_ACCEPT
)
114 gtk_filedialog_ok_callback(w
, dialog
);
115 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
116 gtk_filedialog_cancel_callback(w
, dialog
);
119 static void gtk_filedialog_update_preview_callback(GtkFileChooser
*chooser
,
122 GtkWidget
*preview
= GTK_WIDGET(user_data
);
124 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
129 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
130 gboolean have_preview
= pixbuf
!= NULL
;
132 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
134 g_object_unref (pixbuf
);
136 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
141 void wxFileDialog::AddChildGTK(wxWindowGTK
* child
)
143 // allow dialog to be resized smaller horizontally
144 gtk_widget_set_size_request(
145 child
->m_widget
, child
->GetMinWidth(), child
->m_height
);
147 gtk_file_chooser_set_extra_widget(
148 GTK_FILE_CHOOSER(m_widget
), child
->m_widget
);
151 //-----------------------------------------------------------------------------
153 //-----------------------------------------------------------------------------
155 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxFileDialogBase
)
157 BEGIN_EVENT_TABLE(wxFileDialog
,wxFileDialogBase
)
158 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
159 EVT_SIZE(wxFileDialog::OnSize
)
162 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
163 const wxString
& defaultDir
,
164 const wxString
& defaultFileName
,
165 const wxString
& wildCard
,
166 long style
, const wxPoint
& pos
,
168 const wxString
& name
)
171 Create(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
);
174 bool wxFileDialog::Create(wxWindow
*parent
, const wxString
& message
,
175 const wxString
& defaultDir
,
176 const wxString
& defaultFileName
,
177 const wxString
& wildCard
,
178 long style
, const wxPoint
& pos
,
180 const wxString
& name
)
182 parent
= GetParentForModalDialog(parent
, style
);
184 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFileName
,
185 wildCard
, style
, pos
, sz
, name
))
190 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
191 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
192 wxDefaultValidator
, wxT("filedialog")))
194 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
198 GtkFileChooserAction gtk_action
;
199 GtkWindow
* gtk_parent
= NULL
;
201 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
203 const gchar
* ok_btn_stock
;
204 if ( style
& wxFD_SAVE
)
206 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
207 ok_btn_stock
= GTK_STOCK_SAVE
;
211 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
212 ok_btn_stock
= GTK_STOCK_OPEN
;
215 m_widget
= gtk_file_chooser_dialog_new(
216 wxGTK_CONV(m_message
),
219 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
220 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
222 g_object_ref(m_widget
);
223 GtkFileChooser
* file_chooser
= GTK_FILE_CHOOSER(m_widget
);
225 m_fc
.SetWidget(file_chooser
);
227 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
229 if ( style
& wxFD_MULTIPLE
)
230 gtk_file_chooser_set_select_multiple(file_chooser
, true);
232 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
233 // destroys the dialog when the user press ESC on the dialog: in that case
234 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
236 g_signal_connect(m_widget
,
238 G_CALLBACK (gtk_widget_hide_on_delete
),
241 // local-only property could be set to false to allow non-local files to be
242 // loaded. In that case get/set_uri(s) should be used instead of
243 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
244 // probably also be created with a backend, e.g. "gnome-vfs", "default", ...
245 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
246 // as the default - true:
247 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
249 g_signal_connect (m_widget
, "response",
250 G_CALLBACK (gtk_filedialog_response_callback
), this);
253 // deal with extensions/filters
254 SetWildcard(wildCard
);
256 wxString defaultFileNameWithExt
= defaultFileName
;
257 if ( !wildCard
.empty() && !defaultFileName
.empty() &&
258 !wxFileName(defaultFileName
).HasExt() )
260 // append the default extension to the initial file name: GTK won't do
261 // it for us by default (unlike e.g. MSW)
262 const wxString defaultExt
= m_fc
.GetCurrentWildCard().AfterFirst('.');
263 if ( defaultExt
.find_first_of("?*") == wxString::npos
)
264 defaultFileNameWithExt
+= "." + defaultExt
;
268 // if defaultDir is specified it should contain the directory and
269 // defaultFileName should contain the default name of the file, however if
270 // directory is not given, defaultFileName contains both
272 if ( defaultDir
.empty() )
273 fn
.Assign(defaultFileNameWithExt
);
274 else if ( !defaultFileNameWithExt
.empty() )
275 fn
.Assign(defaultDir
, defaultFileNameWithExt
);
277 fn
.AssignDir(defaultDir
);
279 // set the initial file name and/or directory
280 fn
.MakeAbsolute(); // GTK+ needs absolute path
281 const wxString dir
= fn
.GetPath();
284 gtk_file_chooser_set_current_folder(file_chooser
, wxGTK_CONV_FN(dir
));
287 const wxString fname
= fn
.GetFullName();
288 if ( style
& wxFD_SAVE
)
290 if ( !fname
.empty() )
292 gtk_file_chooser_set_current_name(file_chooser
, wxGTK_CONV_FN(fname
));
295 #if GTK_CHECK_VERSION(2,7,3)
296 if ((style
& wxFD_OVERWRITE_PROMPT
)
298 && gtk_check_version(2,7,3) == NULL
302 gtk_file_chooser_set_do_overwrite_confirmation(file_chooser
, true);
308 if ( !fname
.empty() )
310 gtk_file_chooser_set_filename(file_chooser
,
311 wxGTK_CONV_FN(fn
.GetFullPath()));
315 if ( style
& wxFD_PREVIEW
)
317 GtkWidget
*previewImage
= gtk_image_new();
319 gtk_file_chooser_set_preview_widget(file_chooser
, previewImage
);
320 g_signal_connect(m_widget
, "update-preview",
321 G_CALLBACK(gtk_filedialog_update_preview_callback
),
328 wxFileDialog::~wxFileDialog()
332 // get chooser to drop its reference right now, allowing wxWindow dtor
333 // to verify that ref count drops to zero
334 gtk_file_chooser_set_extra_widget(
335 GTK_FILE_CHOOSER(m_widget
), NULL
);
339 void wxFileDialog::OnFakeOk(wxCommandEvent
& WXUNUSED(event
))
344 int wxFileDialog::ShowModal()
346 WX_TESTING_SHOW_MODAL_HOOK();
348 CreateExtraControl();
350 return wxDialog::ShowModal();
353 void wxFileDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
354 int WXUNUSED(width
), int WXUNUSED(height
),
355 int WXUNUSED(sizeFlags
))
359 void wxFileDialog::OnSize(wxSizeEvent
&)
361 // avoid calling DoLayout(), which will set the (wrong) size of
362 // m_extraControl, its size is managed by GtkFileChooser
365 wxString
wxFileDialog::GetPath() const
367 return m_fc
.GetPath();
370 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
372 m_fc
.GetFilenames( files
);
375 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
377 m_fc
.GetPaths( paths
);
380 void wxFileDialog::SetMessage(const wxString
& message
)
386 void wxFileDialog::SetPath(const wxString
& path
)
388 wxFileDialogBase::SetPath(path
);
390 // Don't do anything if no path is specified, in particular don't set the
391 // path to m_dir below as this would result in opening the dialog in the
392 // parent directory of this one instead of m_dir itself.
396 // we need an absolute path for GTK native chooser so ensure that we have
397 // it: use the initial directory if it was set or just CWD otherwise (this
398 // is the default behaviour if m_dir is empty)
400 fn
.MakeAbsolute(m_dir
);
401 m_fc
.SetPath(fn
.GetFullPath());
404 void wxFileDialog::SetDirectory(const wxString
& dir
)
406 wxFileDialogBase::SetDirectory(dir
);
408 m_fc
.SetDirectory(dir
);
411 wxString
wxFileDialog::GetDirectory() const
413 wxString
currentDir( m_fc
.GetDirectory() );
414 if (currentDir
.empty())
416 // m_fc.GetDirectory() will return empty until the dialog has been shown
417 // in which case use any previously provided value
423 void wxFileDialog::SetFilename(const wxString
& name
)
425 wxFileDialogBase::SetFilename(name
);
427 if (HasFdFlag(wxFD_SAVE
))
429 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
434 wxString
path( GetDirectory() );
437 // SetPath() fires an assert if fed other than filepaths
440 SetPath(wxFileName(path
, name
).GetFullPath());
444 wxString
wxFileDialog::GetFilename() const
446 wxString
currentFilename( m_fc
.GetFilename() );
447 if (currentFilename
.empty())
449 // m_fc.GetFilename() will return empty until the dialog has been shown
450 // in which case use any previously provided value
451 currentFilename
= m_fileName
;
453 return currentFilename
;
456 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
458 wxFileDialogBase::SetWildcard(wildCard
);
459 m_fc
.SetWildcard( GetWildcard() );
462 void wxFileDialog::SetFilterIndex(int filterIndex
)
464 m_fc
.SetFilterIndex( filterIndex
);
467 int wxFileDialog::GetFilterIndex() const
469 return m_fc
.GetFilterIndex();
472 #endif // wxUSE_FILEDLG