1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/filedlg.cpp
3 // Purpose: native implementation of wxFileDialog
4 // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
5 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/filedlg.h"
18 #include "wx/msgdlg.h"
22 #include "wx/gtk/private.h"
25 #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/modalhook.h"
33 //-----------------------------------------------------------------------------
34 // "clicked" for OK-button
35 //-----------------------------------------------------------------------------
38 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
40 int style
= dialog
->GetWindowStyle();
41 wxGtkString
filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
)));
43 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
45 #if GTK_CHECK_VERSION(2,7,3)
46 if (gtk_check_version(2, 7, 3) != NULL
)
49 if ((style
& wxFD_SAVE
) && (style
& wxFD_OVERWRITE_PROMPT
))
51 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
56 _("File '%s' already exists, do you really want to overwrite it?"),
57 wxString::FromUTF8(filename
));
59 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
60 wxYES_NO
| wxICON_QUESTION
);
61 if (dlg
.ShowModal() != wxID_YES
)
68 if (style
& wxFD_FILE_MUST_EXIST
)
70 if ( !g_file_test(filename
, G_FILE_TEST_EXISTS
) )
72 wxMessageDialog
dlg( dialog
, _("Please choose an existing file."),
73 _("Error"), wxOK
| wxICON_ERROR
);
79 // change to the directory where the user went if asked
80 if (style
& wxFD_CHANGE_DIR
)
82 // Use chdir to not care about filename encodings
83 wxGtkString
folder(g_path_get_dirname(filename
));
87 wxCommandEvent
event(wxEVT_BUTTON
, wxID_OK
);
88 event
.SetEventObject(dialog
);
89 dialog
->HandleWindowEvent(event
);
93 //-----------------------------------------------------------------------------
94 // "clicked" for Cancel-button
95 //-----------------------------------------------------------------------------
101 gtk_filedialog_cancel_callback(GtkWidget
* WXUNUSED(w
), wxFileDialog
*dialog
)
103 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
104 event
.SetEventObject(dialog
);
105 dialog
->HandleWindowEvent(event
);
108 static void gtk_filedialog_response_callback(GtkWidget
*w
,
110 wxFileDialog
*dialog
)
112 if (response
== GTK_RESPONSE_ACCEPT
)
113 gtk_filedialog_ok_callback(w
, dialog
);
114 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
115 gtk_filedialog_cancel_callback(w
, dialog
);
118 static void gtk_filedialog_selchanged_callback(GtkFileChooser
*chooser
,
119 wxFileDialog
*dialog
)
121 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
123 dialog
->GTKSelectionChanged(wxString::FromUTF8(filename
));
127 static void gtk_filedialog_update_preview_callback(GtkFileChooser
*chooser
,
130 GtkWidget
*preview
= GTK_WIDGET(user_data
);
132 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
137 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
138 gboolean have_preview
= pixbuf
!= NULL
;
140 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
142 g_object_unref (pixbuf
);
144 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
149 void wxFileDialog::AddChildGTK(wxWindowGTK
* child
)
151 // allow dialog to be resized smaller horizontally
152 gtk_widget_set_size_request(
153 child
->m_widget
, child
->GetMinWidth(), child
->m_height
);
155 gtk_file_chooser_set_extra_widget(
156 GTK_FILE_CHOOSER(m_widget
), child
->m_widget
);
159 //-----------------------------------------------------------------------------
161 //-----------------------------------------------------------------------------
163 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxFileDialogBase
)
165 BEGIN_EVENT_TABLE(wxFileDialog
,wxFileDialogBase
)
166 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
167 EVT_SIZE(wxFileDialog::OnSize
)
170 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
171 const wxString
& defaultDir
,
172 const wxString
& defaultFileName
,
173 const wxString
& wildCard
,
174 long style
, const wxPoint
& pos
,
176 const wxString
& name
)
179 Create(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
);
182 bool wxFileDialog::Create(wxWindow
*parent
, const wxString
& message
,
183 const wxString
& defaultDir
,
184 const wxString
& defaultFileName
,
185 const wxString
& wildCard
,
186 long style
, const wxPoint
& pos
,
188 const wxString
& name
)
190 parent
= GetParentForModalDialog(parent
, style
);
192 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFileName
,
193 wildCard
, style
, pos
, sz
, name
))
198 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
199 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
200 wxDefaultValidator
, wxT("filedialog")))
202 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
206 GtkFileChooserAction gtk_action
;
207 GtkWindow
* gtk_parent
= NULL
;
209 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
211 const gchar
* ok_btn_stock
;
212 if ( style
& wxFD_SAVE
)
214 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
215 ok_btn_stock
= GTK_STOCK_SAVE
;
219 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
220 ok_btn_stock
= GTK_STOCK_OPEN
;
223 m_widget
= gtk_file_chooser_dialog_new(
224 wxGTK_CONV(m_message
),
227 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
228 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
230 g_object_ref(m_widget
);
231 GtkFileChooser
* file_chooser
= GTK_FILE_CHOOSER(m_widget
);
233 m_fc
.SetWidget(file_chooser
);
235 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
237 if ( style
& wxFD_MULTIPLE
)
238 gtk_file_chooser_set_select_multiple(file_chooser
, true);
240 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
241 // destroys the dialog when the user press ESC on the dialog: in that case
242 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
244 g_signal_connect(m_widget
,
246 G_CALLBACK (gtk_widget_hide_on_delete
),
249 // local-only property could be set to false to allow non-local files to be
250 // loaded. In that case get/set_uri(s) should be used instead of
251 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
252 // probably also be created with a backend, e.g. "gnome-vfs", "default", ...
253 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
254 // as the default - true:
255 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
257 g_signal_connect (m_widget
, "response",
258 G_CALLBACK (gtk_filedialog_response_callback
), this);
260 g_signal_connect (m_widget
, "selection-changed",
261 G_CALLBACK (gtk_filedialog_selchanged_callback
), this);
263 // deal with extensions/filters
264 SetWildcard(wildCard
);
266 wxString defaultFileNameWithExt
= defaultFileName
;
267 if ( !wildCard
.empty() && !defaultFileName
.empty() &&
268 !wxFileName(defaultFileName
).HasExt() )
270 // append the default extension, if any, to the initial file name: GTK
271 // won't do it for us by default (unlike e.g. MSW)
272 const wxFileName
fnWC(m_fc
.GetCurrentWildCard());
275 // Notice that we shouldn't append the extension if it's a wildcard
276 // because this is not useful: the user would need to change it to use
277 // some fixed extension anyhow.
278 const wxString
& ext
= fnWC
.GetExt();
279 if ( ext
.find_first_of("?*") == wxString::npos
)
280 defaultFileNameWithExt
<< "." << ext
;
285 // if defaultDir is specified it should contain the directory and
286 // defaultFileName should contain the default name of the file, however if
287 // directory is not given, defaultFileName contains both
289 if ( defaultDir
.empty() )
290 fn
.Assign(defaultFileNameWithExt
);
291 else if ( !defaultFileNameWithExt
.empty() )
292 fn
.Assign(defaultDir
, defaultFileNameWithExt
);
294 fn
.AssignDir(defaultDir
);
296 // set the initial file name and/or directory
297 fn
.MakeAbsolute(); // GTK+ needs absolute path
298 const wxString dir
= fn
.GetPath();
301 gtk_file_chooser_set_current_folder(file_chooser
, wxGTK_CONV_FN(dir
));
304 const wxString fname
= fn
.GetFullName();
305 if ( style
& wxFD_SAVE
)
307 if ( !fname
.empty() )
309 gtk_file_chooser_set_current_name(file_chooser
, wxGTK_CONV_FN(fname
));
312 #if GTK_CHECK_VERSION(2,7,3)
313 if ((style
& wxFD_OVERWRITE_PROMPT
)
315 && gtk_check_version(2,7,3) == NULL
319 gtk_file_chooser_set_do_overwrite_confirmation(file_chooser
, true);
325 if ( !fname
.empty() )
327 gtk_file_chooser_set_filename(file_chooser
,
328 wxGTK_CONV_FN(fn
.GetFullPath()));
332 if ( style
& wxFD_PREVIEW
)
334 GtkWidget
*previewImage
= gtk_image_new();
336 gtk_file_chooser_set_preview_widget(file_chooser
, previewImage
);
337 g_signal_connect(m_widget
, "update-preview",
338 G_CALLBACK(gtk_filedialog_update_preview_callback
),
345 wxFileDialog::~wxFileDialog()
349 // get chooser to drop its reference right now, allowing wxWindow dtor
350 // to verify that ref count drops to zero
351 gtk_file_chooser_set_extra_widget(
352 GTK_FILE_CHOOSER(m_widget
), NULL
);
356 void wxFileDialog::OnFakeOk(wxCommandEvent
& WXUNUSED(event
))
358 // Update the current directory from here, accessing it later may not work
359 // due to the strange way GtkFileChooser works.
361 str(gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(m_widget
)));
362 m_dir
= wxString::FromUTF8(str
);
367 int wxFileDialog::ShowModal()
369 WX_HOOK_MODAL_DIALOG();
371 CreateExtraControl();
373 return wxDialog::ShowModal();
376 void wxFileDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
377 int WXUNUSED(width
), int WXUNUSED(height
),
378 int WXUNUSED(sizeFlags
))
382 void wxFileDialog::OnSize(wxSizeEvent
&)
384 // avoid calling DoLayout(), which will set the (wrong) size of
385 // m_extraControl, its size is managed by GtkFileChooser
388 wxString
wxFileDialog::GetPath() const
390 return m_fc
.GetPath();
393 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
395 m_fc
.GetFilenames( files
);
398 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
400 m_fc
.GetPaths( paths
);
403 void wxFileDialog::SetMessage(const wxString
& message
)
409 void wxFileDialog::SetPath(const wxString
& path
)
411 wxFileDialogBase::SetPath(path
);
413 // Don't do anything if no path is specified, in particular don't set the
414 // path to m_dir below as this would result in opening the dialog in the
415 // parent directory of this one instead of m_dir itself.
419 // we need an absolute path for GTK native chooser so ensure that we have
420 // it: use the initial directory if it was set or just CWD otherwise (this
421 // is the default behaviour if m_dir is empty)
423 fn
.MakeAbsolute(m_dir
);
424 m_fc
.SetPath(fn
.GetFullPath());
427 void wxFileDialog::SetDirectory(const wxString
& dir
)
429 wxFileDialogBase::SetDirectory(dir
);
431 m_fc
.SetDirectory(dir
);
434 void wxFileDialog::SetFilename(const wxString
& name
)
436 wxFileDialogBase::SetFilename(name
);
438 if (HasFdFlag(wxFD_SAVE
))
440 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
445 wxString
path( GetDirectory() );
448 // SetPath() fires an assert if fed other than filepaths
451 SetPath(wxFileName(path
, name
).GetFullPath());
455 wxString
wxFileDialog::GetFilename() const
457 wxString
currentFilename( m_fc
.GetFilename() );
458 if (currentFilename
.empty())
460 // m_fc.GetFilename() will return empty until the dialog has been shown
461 // in which case use any previously provided value
462 currentFilename
= m_fileName
;
464 return currentFilename
;
467 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
469 wxFileDialogBase::SetWildcard(wildCard
);
470 m_fc
.SetWildcard( GetWildcard() );
473 void wxFileDialog::SetFilterIndex(int filterIndex
)
475 m_fc
.SetFilterIndex( filterIndex
);
478 int wxFileDialog::GetFilterIndex() const
480 return m_fc
.GetFilterIndex();
483 void wxFileDialog::GTKSelectionChanged(const wxString
& filename
)
485 m_currentlySelectedFilename
= filename
;
488 m_extraControl
->UpdateWindowUI(wxUPDATE_UI_RECURSE
);
491 #endif // wxUSE_FILEDLG