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
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)
44 #if GTK_CHECK_VERSION(2,7,3)
45 if (gtk_check_version(2, 7, 3) != NULL
)
48 if ((style
& wxFD_SAVE
) && (style
& wxFD_OVERWRITE_PROMPT
))
50 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
55 _("File '%s' already exists, do you really want to overwrite it?"),
56 wxString::FromUTF8(filename
));
58 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
59 wxYES_NO
| wxICON_QUESTION
);
60 if (dlg
.ShowModal() != wxID_YES
)
66 if (style
& wxFD_FILE_MUST_EXIST
)
68 if ( !g_file_test(filename
, G_FILE_TEST_EXISTS
) )
70 wxMessageDialog
dlg( dialog
, _("Please choose an existing file."),
71 _("Error"), wxOK
| wxICON_ERROR
);
77 // change to the directory where the user went if asked
78 if (style
& wxFD_CHANGE_DIR
)
80 // Use chdir to not care about filename encodings
81 wxGtkString
folder(g_path_get_dirname(filename
));
85 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
86 event
.SetEventObject(dialog
);
87 dialog
->HandleWindowEvent(event
);
91 //-----------------------------------------------------------------------------
92 // "clicked" for Cancel-button
93 //-----------------------------------------------------------------------------
99 gtk_filedialog_cancel_callback(GtkWidget
* WXUNUSED(w
), wxFileDialog
*dialog
)
101 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
102 event
.SetEventObject(dialog
);
103 dialog
->HandleWindowEvent(event
);
106 static void gtk_filedialog_response_callback(GtkWidget
*w
,
108 wxFileDialog
*dialog
)
110 if (response
== GTK_RESPONSE_ACCEPT
)
111 gtk_filedialog_ok_callback(w
, dialog
);
112 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
113 gtk_filedialog_cancel_callback(w
, dialog
);
116 static void gtk_filedialog_update_preview_callback(GtkFileChooser
*chooser
,
119 GtkWidget
*preview
= GTK_WIDGET(user_data
);
121 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
126 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
127 gboolean have_preview
= pixbuf
!= NULL
;
129 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
131 g_object_unref (pixbuf
);
133 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
138 //-----------------------------------------------------------------------------
139 // "size_request" from m_extraControl
140 //-----------------------------------------------------------------------------
143 static void extra_widget_size_request(GtkWidget
*, GtkRequisition
* req
, wxWindow
* win
)
145 // allow dialog to be resized smaller horizontally
146 req
->width
= win
->GetMinWidth();
150 void wxFileDialog::AddChildGTK(wxWindowGTK
* child
)
152 g_signal_connect_after(child
->m_widget
, "size_request",
153 G_CALLBACK(extra_widget_size_request
), child
);
154 gtk_file_chooser_set_extra_widget(
155 GTK_FILE_CHOOSER(m_widget
), child
->m_widget
);
158 //-----------------------------------------------------------------------------
160 //-----------------------------------------------------------------------------
162 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxFileDialogBase
)
164 BEGIN_EVENT_TABLE(wxFileDialog
,wxFileDialogBase
)
165 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
166 EVT_SIZE(wxFileDialog::OnSize
)
169 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
170 const wxString
& defaultDir
,
171 const wxString
& defaultFileName
,
172 const wxString
& wildCard
,
173 long style
, const wxPoint
& pos
,
175 const wxString
& name
)
178 Create(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
);
181 bool wxFileDialog::Create(wxWindow
*parent
, const wxString
& message
,
182 const wxString
& defaultDir
,
183 const wxString
& defaultFileName
,
184 const wxString
& wildCard
,
185 long style
, const wxPoint
& pos
,
187 const wxString
& name
)
189 parent
= GetParentForModalDialog(parent
, style
);
191 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFileName
,
192 wildCard
, style
, pos
, sz
, name
))
197 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
198 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
199 wxDefaultValidator
, wxT("filedialog")))
201 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
205 GtkFileChooserAction gtk_action
;
206 GtkWindow
* gtk_parent
= NULL
;
208 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
210 const gchar
* ok_btn_stock
;
211 if ( style
& wxFD_SAVE
)
213 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
214 ok_btn_stock
= GTK_STOCK_SAVE
;
218 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
219 ok_btn_stock
= GTK_STOCK_OPEN
;
222 m_widget
= gtk_file_chooser_dialog_new(
223 wxGTK_CONV(m_message
),
226 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
227 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
229 g_object_ref(m_widget
);
230 GtkFileChooser
* file_chooser
= GTK_FILE_CHOOSER(m_widget
);
232 m_fc
.SetWidget(file_chooser
);
234 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
236 if ( style
& wxFD_MULTIPLE
)
237 gtk_file_chooser_set_select_multiple(file_chooser
, true);
239 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
240 // destroys the dialog when the user press ESC on the dialog: in that case
241 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
243 g_signal_connect(m_widget
,
245 G_CALLBACK (gtk_widget_hide_on_delete
),
248 // local-only property could be set to false to allow non-local files to be
249 // loaded. In that case get/set_uri(s) should be used instead of
250 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
251 // probably also be created with a backend, e.g. "gnome-vfs", "default", ...
252 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
253 // as the default - true:
254 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
256 g_signal_connect (m_widget
, "response",
257 G_CALLBACK (gtk_filedialog_response_callback
), this);
260 // deal with extensions/filters
261 SetWildcard(wildCard
);
263 wxString defaultFileNameWithExt
= defaultFileName
;
264 if ( !wildCard
.empty() && !defaultFileName
.empty() &&
265 !wxFileName(defaultFileName
).HasExt() )
267 // append the default extension to the initial file name: GTK won't do
268 // it for us by default (unlike e.g. MSW)
269 const wxString defaultExt
= m_fc
.GetCurrentWildCard().AfterFirst('.');
270 if ( defaultExt
.find_first_of("?*") == wxString::npos
)
271 defaultFileNameWithExt
+= "." + defaultExt
;
275 // if defaultDir is specified it should contain the directory and
276 // defaultFileName should contain the default name of the file, however if
277 // directory is not given, defaultFileName contains both
279 if ( defaultDir
.empty() )
280 fn
.Assign(defaultFileNameWithExt
);
281 else if ( !defaultFileNameWithExt
.empty() )
282 fn
.Assign(defaultDir
, defaultFileNameWithExt
);
284 fn
.AssignDir(defaultDir
);
286 // set the initial file name and/or directory
287 fn
.MakeAbsolute(); // GTK+ needs absolute path
288 const wxString dir
= fn
.GetPath();
291 gtk_file_chooser_set_current_folder(file_chooser
, wxGTK_CONV_FN(dir
));
294 const wxString fname
= fn
.GetFullName();
295 if ( style
& wxFD_SAVE
)
297 if ( !fname
.empty() )
299 gtk_file_chooser_set_current_name(file_chooser
, wxGTK_CONV_FN(fname
));
302 #if GTK_CHECK_VERSION(2,7,3)
303 if ((style
& wxFD_OVERWRITE_PROMPT
) && !gtk_check_version(2,7,3))
304 gtk_file_chooser_set_do_overwrite_confirmation(file_chooser
, true);
309 if ( !fname
.empty() )
311 gtk_file_chooser_set_filename(file_chooser
,
312 wxGTK_CONV_FN(fn
.GetFullPath()));
316 if ( style
& wxFD_PREVIEW
)
318 GtkWidget
*previewImage
= gtk_image_new();
320 gtk_file_chooser_set_preview_widget(file_chooser
, previewImage
);
321 g_signal_connect(m_widget
, "update-preview",
322 G_CALLBACK(gtk_filedialog_update_preview_callback
),
329 wxFileDialog::~wxFileDialog()
333 // get chooser to drop its reference right now, allowing wxWindow dtor
334 // to verify that ref count drops to zero
335 gtk_file_chooser_set_extra_widget(
336 GTK_FILE_CHOOSER(m_widget
), NULL
);
340 void wxFileDialog::OnFakeOk(wxCommandEvent
& WXUNUSED(event
))
345 int wxFileDialog::ShowModal()
347 CreateExtraControl();
349 return wxDialog::ShowModal();
352 void wxFileDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
353 int WXUNUSED(width
), int WXUNUSED(height
),
354 int WXUNUSED(sizeFlags
))
358 void wxFileDialog::OnSize(wxSizeEvent
&)
360 // avoid calling DoLayout(), which will set the (wrong) size of
361 // m_extraControl, its size is managed by GtkFileChooser
364 wxString
wxFileDialog::GetPath() const
366 return m_fc
.GetPath();
369 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
371 m_fc
.GetFilenames( files
);
374 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
376 m_fc
.GetPaths( paths
);
379 void wxFileDialog::SetMessage(const wxString
& message
)
385 void wxFileDialog::SetPath(const wxString
& path
)
387 // Don't do anything if no path is specified, in particular don't set the
388 // path to m_dir below as this would result in opening the dialog in the
389 // parent directory of this one instead of m_dir itself.
393 // we need an absolute path for GTK native chooser so ensure that we have
394 // it: use the initial directory if it was set or just CWD otherwise (this
395 // is the default behaviour if m_dir is empty)
397 fn
.MakeAbsolute(m_dir
);
398 m_fc
.SetPath(fn
.GetFullPath());
401 void wxFileDialog::SetDirectory(const wxString
& dir
)
403 if (m_fc
.SetDirectory( dir
))
405 // Cache the dir, as gtk_file_chooser_get_current_folder()
406 // doesn't return anything until the dialog has been shown
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 if (HasFdFlag(wxFD_SAVE
))
427 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
433 wxString
path( GetDirectory() );
436 // SetPath() fires an assert if fed other than filepaths
439 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