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/modalhook.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_BUTTON
, 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_selchanged_callback(GtkFileChooser
*chooser
,
120 wxFileDialog
*dialog
)
122 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
124 dialog
->GTKSelectionChanged(wxString::FromUTF8(filename
));
128 static void gtk_filedialog_update_preview_callback(GtkFileChooser
*chooser
,
131 GtkWidget
*preview
= GTK_WIDGET(user_data
);
133 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
138 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
139 gboolean have_preview
= pixbuf
!= NULL
;
141 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
143 g_object_unref (pixbuf
);
145 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
150 void wxFileDialog::AddChildGTK(wxWindowGTK
* child
)
152 // allow dialog to be resized smaller horizontally
153 gtk_widget_set_size_request(
154 child
->m_widget
, child
->GetMinWidth(), child
->m_height
);
156 gtk_file_chooser_set_extra_widget(
157 GTK_FILE_CHOOSER(m_widget
), child
->m_widget
);
160 //-----------------------------------------------------------------------------
162 //-----------------------------------------------------------------------------
164 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxFileDialogBase
)
166 BEGIN_EVENT_TABLE(wxFileDialog
,wxFileDialogBase
)
167 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
168 EVT_SIZE(wxFileDialog::OnSize
)
171 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
172 const wxString
& defaultDir
,
173 const wxString
& defaultFileName
,
174 const wxString
& wildCard
,
175 long style
, const wxPoint
& pos
,
177 const wxString
& name
)
180 Create(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
);
183 bool wxFileDialog::Create(wxWindow
*parent
, const wxString
& message
,
184 const wxString
& defaultDir
,
185 const wxString
& defaultFileName
,
186 const wxString
& wildCard
,
187 long style
, const wxPoint
& pos
,
189 const wxString
& name
)
191 parent
= GetParentForModalDialog(parent
, style
);
193 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFileName
,
194 wildCard
, style
, pos
, sz
, name
))
199 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
200 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
201 wxDefaultValidator
, wxT("filedialog")))
203 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
207 GtkFileChooserAction gtk_action
;
208 GtkWindow
* gtk_parent
= NULL
;
210 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
212 const gchar
* ok_btn_stock
;
213 if ( style
& wxFD_SAVE
)
215 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
216 ok_btn_stock
= GTK_STOCK_SAVE
;
220 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
221 ok_btn_stock
= GTK_STOCK_OPEN
;
224 m_widget
= gtk_file_chooser_dialog_new(
225 wxGTK_CONV(m_message
),
228 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
229 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
231 g_object_ref(m_widget
);
232 GtkFileChooser
* file_chooser
= GTK_FILE_CHOOSER(m_widget
);
234 m_fc
.SetWidget(file_chooser
);
236 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
238 if ( style
& wxFD_MULTIPLE
)
239 gtk_file_chooser_set_select_multiple(file_chooser
, true);
241 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
242 // destroys the dialog when the user press ESC on the dialog: in that case
243 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
245 g_signal_connect(m_widget
,
247 G_CALLBACK (gtk_widget_hide_on_delete
),
250 // local-only property could be set to false to allow non-local files to be
251 // loaded. In that case get/set_uri(s) should be used instead of
252 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
253 // probably also be created with a backend, e.g. "gnome-vfs", "default", ...
254 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
255 // as the default - true:
256 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
258 g_signal_connect (m_widget
, "response",
259 G_CALLBACK (gtk_filedialog_response_callback
), this);
261 g_signal_connect (m_widget
, "selection-changed",
262 G_CALLBACK (gtk_filedialog_selchanged_callback
), this);
264 // deal with extensions/filters
265 SetWildcard(wildCard
);
267 wxString defaultFileNameWithExt
= defaultFileName
;
268 if ( !wildCard
.empty() && !defaultFileName
.empty() &&
269 !wxFileName(defaultFileName
).HasExt() )
271 // append the default extension to the initial file name: GTK won't do
272 // it for us by default (unlike e.g. MSW)
273 const wxString defaultExt
= m_fc
.GetCurrentWildCard().AfterFirst('.');
274 if ( defaultExt
.find_first_of("?*") == wxString::npos
)
275 defaultFileNameWithExt
+= "." + defaultExt
;
279 // if defaultDir is specified it should contain the directory and
280 // defaultFileName should contain the default name of the file, however if
281 // directory is not given, defaultFileName contains both
283 if ( defaultDir
.empty() )
284 fn
.Assign(defaultFileNameWithExt
);
285 else if ( !defaultFileNameWithExt
.empty() )
286 fn
.Assign(defaultDir
, defaultFileNameWithExt
);
288 fn
.AssignDir(defaultDir
);
290 // set the initial file name and/or directory
291 fn
.MakeAbsolute(); // GTK+ needs absolute path
292 const wxString dir
= fn
.GetPath();
295 gtk_file_chooser_set_current_folder(file_chooser
, wxGTK_CONV_FN(dir
));
298 const wxString fname
= fn
.GetFullName();
299 if ( style
& wxFD_SAVE
)
301 if ( !fname
.empty() )
303 gtk_file_chooser_set_current_name(file_chooser
, wxGTK_CONV_FN(fname
));
306 #if GTK_CHECK_VERSION(2,7,3)
307 if ((style
& wxFD_OVERWRITE_PROMPT
)
309 && gtk_check_version(2,7,3) == NULL
313 gtk_file_chooser_set_do_overwrite_confirmation(file_chooser
, true);
319 if ( !fname
.empty() )
321 gtk_file_chooser_set_filename(file_chooser
,
322 wxGTK_CONV_FN(fn
.GetFullPath()));
326 if ( style
& wxFD_PREVIEW
)
328 GtkWidget
*previewImage
= gtk_image_new();
330 gtk_file_chooser_set_preview_widget(file_chooser
, previewImage
);
331 g_signal_connect(m_widget
, "update-preview",
332 G_CALLBACK(gtk_filedialog_update_preview_callback
),
339 wxFileDialog::~wxFileDialog()
343 // get chooser to drop its reference right now, allowing wxWindow dtor
344 // to verify that ref count drops to zero
345 gtk_file_chooser_set_extra_widget(
346 GTK_FILE_CHOOSER(m_widget
), NULL
);
350 void wxFileDialog::OnFakeOk(wxCommandEvent
& WXUNUSED(event
))
352 // Update the current directory from here, accessing it later may not work
353 // due to the strange way GtkFileChooser works.
355 str(gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(m_widget
)));
356 m_dir
= wxString::FromUTF8(str
);
361 int wxFileDialog::ShowModal()
363 WX_HOOK_MODAL_DIALOG();
365 CreateExtraControl();
367 return wxDialog::ShowModal();
370 void wxFileDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
371 int WXUNUSED(width
), int WXUNUSED(height
),
372 int WXUNUSED(sizeFlags
))
376 void wxFileDialog::OnSize(wxSizeEvent
&)
378 // avoid calling DoLayout(), which will set the (wrong) size of
379 // m_extraControl, its size is managed by GtkFileChooser
382 wxString
wxFileDialog::GetPath() const
384 return m_fc
.GetPath();
387 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
389 m_fc
.GetFilenames( files
);
392 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
394 m_fc
.GetPaths( paths
);
397 void wxFileDialog::SetMessage(const wxString
& message
)
403 void wxFileDialog::SetPath(const wxString
& path
)
405 wxFileDialogBase::SetPath(path
);
407 // Don't do anything if no path is specified, in particular don't set the
408 // path to m_dir below as this would result in opening the dialog in the
409 // parent directory of this one instead of m_dir itself.
413 // we need an absolute path for GTK native chooser so ensure that we have
414 // it: use the initial directory if it was set or just CWD otherwise (this
415 // is the default behaviour if m_dir is empty)
417 fn
.MakeAbsolute(m_dir
);
418 m_fc
.SetPath(fn
.GetFullPath());
421 void wxFileDialog::SetDirectory(const wxString
& dir
)
423 wxFileDialogBase::SetDirectory(dir
);
425 m_fc
.SetDirectory(dir
);
428 void wxFileDialog::SetFilename(const wxString
& name
)
430 wxFileDialogBase::SetFilename(name
);
432 if (HasFdFlag(wxFD_SAVE
))
434 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
439 wxString
path( GetDirectory() );
442 // SetPath() fires an assert if fed other than filepaths
445 SetPath(wxFileName(path
, name
).GetFullPath());
449 wxString
wxFileDialog::GetFilename() const
451 wxString
currentFilename( m_fc
.GetFilename() );
452 if (currentFilename
.empty())
454 // m_fc.GetFilename() will return empty until the dialog has been shown
455 // in which case use any previously provided value
456 currentFilename
= m_fileName
;
458 return currentFilename
;
461 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
463 wxFileDialogBase::SetWildcard(wildCard
);
464 m_fc
.SetWildcard( GetWildcard() );
467 void wxFileDialog::SetFilterIndex(int filterIndex
)
469 m_fc
.SetFilterIndex( filterIndex
);
472 int wxFileDialog::GetFilterIndex() const
474 return m_fc
.GetFilterIndex();
477 void wxFileDialog::GTKSelectionChanged(const wxString
& filename
)
479 m_currentlySelectedFilename
= filename
;
482 m_extraControl
->UpdateWindowUI(wxUPDATE_UI_RECURSE
);
485 #endif // wxUSE_FILEDLG