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"
36 static void gtk_filedialog_response_callback(GtkWidget
* WXUNUSED(w
),
40 if (response
== GTK_RESPONSE_ACCEPT
)
41 dialog
->GTKOnAccept();
42 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
43 dialog
->GTKOnCancel();
46 static void gtk_filedialog_update_preview_callback(GtkFileChooser
*chooser
,
49 GtkWidget
*preview
= GTK_WIDGET(user_data
);
51 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
56 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
57 gboolean have_preview
= pixbuf
!= NULL
;
59 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
61 g_object_unref (pixbuf
);
63 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
68 void wxFileDialog::AddChildGTK(wxWindowGTK
* child
)
70 // allow dialog to be resized smaller horizontally
71 gtk_widget_set_size_request(
72 child
->m_widget
, child
->GetMinWidth(), child
->m_height
);
74 gtk_file_chooser_set_extra_widget(
75 GTK_FILE_CHOOSER(m_widget
), child
->m_widget
);
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
82 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxFileDialogBase
)
84 BEGIN_EVENT_TABLE(wxFileDialog
,wxFileDialogBase
)
85 EVT_SIZE(wxFileDialog::OnSize
)
88 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
89 const wxString
& defaultDir
,
90 const wxString
& defaultFileName
,
91 const wxString
& wildCard
,
92 long style
, const wxPoint
& pos
,
97 Create(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
);
100 bool wxFileDialog::Create(wxWindow
*parent
, const wxString
& message
,
101 const wxString
& defaultDir
,
102 const wxString
& defaultFileName
,
103 const wxString
& wildCard
,
104 long style
, const wxPoint
& pos
,
106 const wxString
& name
)
108 parent
= GetParentForModalDialog(parent
, style
);
110 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFileName
,
111 wildCard
, style
, pos
, sz
, name
))
116 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
117 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
118 wxDefaultValidator
, wxT("filedialog")))
120 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
124 GtkFileChooserAction gtk_action
;
125 GtkWindow
* gtk_parent
= NULL
;
127 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
129 const gchar
* ok_btn_stock
;
130 if ( style
& wxFD_SAVE
)
132 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
133 ok_btn_stock
= GTK_STOCK_SAVE
;
137 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
138 ok_btn_stock
= GTK_STOCK_OPEN
;
141 m_widget
= gtk_file_chooser_dialog_new(
142 wxGTK_CONV(m_message
),
145 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
146 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
148 g_object_ref(m_widget
);
149 GtkFileChooser
* file_chooser
= GTK_FILE_CHOOSER(m_widget
);
151 m_fc
.SetWidget(file_chooser
);
153 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
155 if ( style
& wxFD_MULTIPLE
)
156 gtk_file_chooser_set_select_multiple(file_chooser
, true);
158 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
159 // destroys the dialog when the user press ESC on the dialog: in that case
160 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
162 g_signal_connect(m_widget
,
164 G_CALLBACK (gtk_widget_hide_on_delete
),
167 // local-only property could be set to false to allow non-local files to be
168 // loaded. In that case get/set_uri(s) should be used instead of
169 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
170 // probably also be created with a backend, e.g. "gnome-vfs", "default", ...
171 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
172 // as the default - true:
173 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
175 g_signal_connect (m_widget
, "response",
176 G_CALLBACK (gtk_filedialog_response_callback
), this);
179 // deal with extensions/filters
180 SetWildcard(wildCard
);
182 wxString defaultFileNameWithExt
= defaultFileName
;
183 if ( !wildCard
.empty() && !defaultFileName
.empty() &&
184 !wxFileName(defaultFileName
).HasExt() )
186 // append the default extension to the initial file name: GTK won't do
187 // it for us by default (unlike e.g. MSW)
188 const wxString defaultExt
= m_fc
.GetCurrentWildCard().AfterFirst('.');
189 if ( defaultExt
.find_first_of("?*") == wxString::npos
)
190 defaultFileNameWithExt
+= "." + defaultExt
;
194 // if defaultDir is specified it should contain the directory and
195 // defaultFileName should contain the default name of the file, however if
196 // directory is not given, defaultFileName contains both
198 if ( defaultDir
.empty() )
199 fn
.Assign(defaultFileNameWithExt
);
200 else if ( !defaultFileNameWithExt
.empty() )
201 fn
.Assign(defaultDir
, defaultFileNameWithExt
);
203 fn
.AssignDir(defaultDir
);
205 // set the initial file name and/or directory
206 fn
.MakeAbsolute(); // GTK+ needs absolute path
207 const wxString dir
= fn
.GetPath();
210 gtk_file_chooser_set_current_folder(file_chooser
, wxGTK_CONV_FN(dir
));
213 const wxString fname
= fn
.GetFullName();
214 if ( style
& wxFD_SAVE
)
216 if ( !fname
.empty() )
218 gtk_file_chooser_set_current_name(file_chooser
, wxGTK_CONV_FN(fname
));
221 #if GTK_CHECK_VERSION(2,7,3)
222 if ((style
& wxFD_OVERWRITE_PROMPT
)
224 && gtk_check_version(2,7,3) == NULL
228 gtk_file_chooser_set_do_overwrite_confirmation(file_chooser
, true);
234 if ( !fname
.empty() )
236 gtk_file_chooser_set_filename(file_chooser
,
237 wxGTK_CONV_FN(fn
.GetFullPath()));
241 if ( style
& wxFD_PREVIEW
)
243 GtkWidget
*previewImage
= gtk_image_new();
245 gtk_file_chooser_set_preview_widget(file_chooser
, previewImage
);
246 g_signal_connect(m_widget
, "update-preview",
247 G_CALLBACK(gtk_filedialog_update_preview_callback
),
254 wxFileDialog::~wxFileDialog()
258 // get chooser to drop its reference right now, allowing wxWindow dtor
259 // to verify that ref count drops to zero
260 gtk_file_chooser_set_extra_widget(
261 GTK_FILE_CHOOSER(m_widget
), NULL
);
264 void wxFileDialog::GTKOnAccept()
266 int style
= GetWindowStyle();
267 wxString filename
= m_fc
.GetPath();
268 m_selectedDirectory
= m_fc
.GetDirectory();
270 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
272 #if GTK_CHECK_VERSION(2,7,3)
273 if (gtk_check_version(2, 7, 3) != NULL
)
276 if ((style
& wxFD_SAVE
) && (style
& wxFD_OVERWRITE_PROMPT
))
278 if ( g_file_test(filename
.utf8_str(), G_FILE_TEST_EXISTS
) )
283 _("File '%s' already exists, do you really want to overwrite it?"),
284 wxString::FromUTF8(filename
.utf8_str()));
286 wxMessageDialog
dlg(this, msg
, _("Confirm"),
287 wxYES_NO
| wxICON_QUESTION
);
288 if (dlg
.ShowModal() != wxID_YES
)
295 if (style
& wxFD_FILE_MUST_EXIST
)
297 if ( !g_file_test(filename
.utf8_str(), G_FILE_TEST_EXISTS
) )
299 wxMessageDialog
dlg( this, _("Please choose an existing file."),
300 _("Error"), wxOK
| wxICON_ERROR
);
306 // change to the directory where the user went if asked
307 if (style
& wxFD_CHANGE_DIR
)
309 // Use chdir to not care about filename encodings
310 wxGtkString
folder(g_path_get_dirname(filename
.utf8_str()));
316 void wxFileDialog::GTKOnCancel()
318 EndDialog(wxID_CANCEL
);
321 int wxFileDialog::ShowModal()
323 WX_TESTING_SHOW_MODAL_HOOK();
325 CreateExtraControl();
327 return wxDialog::ShowModal();
330 void wxFileDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
331 int WXUNUSED(width
), int WXUNUSED(height
),
332 int WXUNUSED(sizeFlags
))
336 void wxFileDialog::OnSize(wxSizeEvent
&)
338 // avoid calling DoLayout(), which will set the (wrong) size of
339 // m_extraControl, its size is managed by GtkFileChooser
342 wxString
wxFileDialog::GetPath() const
344 return m_fc
.GetPath();
347 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
349 m_fc
.GetFilenames( files
);
352 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
354 m_fc
.GetPaths( paths
);
357 void wxFileDialog::SetMessage(const wxString
& message
)
363 void wxFileDialog::SetPath(const wxString
& path
)
365 wxFileDialogBase::SetPath(path
);
367 // Don't do anything if no path is specified, in particular don't set the
368 // path to m_dir below as this would result in opening the dialog in the
369 // parent directory of this one instead of m_dir itself.
373 // we need an absolute path for GTK native chooser so ensure that we have
374 // it: use the initial directory if it was set or just CWD otherwise (this
375 // is the default behaviour if m_dir is empty)
377 fn
.MakeAbsolute(m_dir
);
378 m_fc
.SetPath(fn
.GetFullPath());
381 void wxFileDialog::SetDirectory(const wxString
& dir
)
383 wxFileDialogBase::SetDirectory(dir
);
385 m_fc
.SetDirectory(dir
);
386 m_selectedDirectory
= dir
;
389 wxString
wxFileDialog::GetDirectory() const
391 return m_selectedDirectory
;
394 void wxFileDialog::SetFilename(const wxString
& name
)
396 wxFileDialogBase::SetFilename(name
);
398 if (HasFdFlag(wxFD_SAVE
))
400 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
405 wxString
path( GetDirectory() );
408 // SetPath() fires an assert if fed other than filepaths
411 SetPath(wxFileName(path
, name
).GetFullPath());
415 wxString
wxFileDialog::GetFilename() const
417 wxString
currentFilename( m_fc
.GetFilename() );
418 if (currentFilename
.empty())
420 // m_fc.GetFilename() will return empty until the dialog has been shown
421 // in which case use any previously provided value
422 currentFilename
= m_fileName
;
424 return currentFilename
;
427 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
429 wxFileDialogBase::SetWildcard(wildCard
);
430 m_fc
.SetWildcard( GetWildcard() );
433 void wxFileDialog::SetFilterIndex(int filterIndex
)
435 m_fc
.SetFilterIndex( filterIndex
);
438 int wxFileDialog::GetFilterIndex() const
440 return m_fc
.GetFilterIndex();
443 #endif // wxUSE_FILEDLG