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)
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_COMMAND_BUTTON_CLICKED
, 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_update_preview_callback(GtkFileChooser
*chooser
,
121 GtkWidget
*preview
= GTK_WIDGET(user_data
);
123 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
128 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
129 gboolean have_preview
= pixbuf
!= NULL
;
131 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
133 g_object_unref (pixbuf
);
135 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
140 void wxFileDialog::AddChildGTK(wxWindowGTK
* child
)
142 // allow dialog to be resized smaller horizontally
143 gtk_widget_set_size_request(
144 child
->m_widget
, child
->GetMinWidth(), child
->m_height
);
146 gtk_file_chooser_set_extra_widget(
147 GTK_FILE_CHOOSER(m_widget
), child
->m_widget
);
150 //-----------------------------------------------------------------------------
152 //-----------------------------------------------------------------------------
154 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxFileDialogBase
)
156 BEGIN_EVENT_TABLE(wxFileDialog
,wxFileDialogBase
)
157 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
158 EVT_SIZE(wxFileDialog::OnSize
)
161 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
162 const wxString
& defaultDir
,
163 const wxString
& defaultFileName
,
164 const wxString
& wildCard
,
165 long style
, const wxPoint
& pos
,
167 const wxString
& name
)
170 Create(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
, sz
, name
);
173 bool wxFileDialog::Create(wxWindow
*parent
, const wxString
& message
,
174 const wxString
& defaultDir
,
175 const wxString
& defaultFileName
,
176 const wxString
& wildCard
,
177 long style
, const wxPoint
& pos
,
179 const wxString
& name
)
181 parent
= GetParentForModalDialog(parent
, style
);
183 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFileName
,
184 wildCard
, style
, pos
, sz
, name
))
189 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
190 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
191 wxDefaultValidator
, wxT("filedialog")))
193 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
197 GtkFileChooserAction gtk_action
;
198 GtkWindow
* gtk_parent
= NULL
;
200 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
202 const gchar
* ok_btn_stock
;
203 if ( style
& wxFD_SAVE
)
205 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
206 ok_btn_stock
= GTK_STOCK_SAVE
;
210 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
211 ok_btn_stock
= GTK_STOCK_OPEN
;
214 m_widget
= gtk_file_chooser_dialog_new(
215 wxGTK_CONV(m_message
),
218 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
219 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
221 g_object_ref(m_widget
);
222 GtkFileChooser
* file_chooser
= GTK_FILE_CHOOSER(m_widget
);
224 m_fc
.SetWidget(file_chooser
);
226 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
228 if ( style
& wxFD_MULTIPLE
)
229 gtk_file_chooser_set_select_multiple(file_chooser
, true);
231 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
232 // destroys the dialog when the user press ESC on the dialog: in that case
233 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
235 g_signal_connect(m_widget
,
237 G_CALLBACK (gtk_widget_hide_on_delete
),
240 // local-only property could be set to false to allow non-local files to be
241 // loaded. In that case get/set_uri(s) should be used instead of
242 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
243 // probably also be created with a backend, e.g. "gnome-vfs", "default", ...
244 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
245 // as the default - true:
246 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
248 g_signal_connect (m_widget
, "response",
249 G_CALLBACK (gtk_filedialog_response_callback
), this);
252 // deal with extensions/filters
253 SetWildcard(wildCard
);
255 wxString defaultFileNameWithExt
= defaultFileName
;
256 if ( !wildCard
.empty() && !defaultFileName
.empty() &&
257 !wxFileName(defaultFileName
).HasExt() )
259 // append the default extension to the initial file name: GTK won't do
260 // it for us by default (unlike e.g. MSW)
261 const wxString defaultExt
= m_fc
.GetCurrentWildCard().AfterFirst('.');
262 if ( defaultExt
.find_first_of("?*") == wxString::npos
)
263 defaultFileNameWithExt
+= "." + defaultExt
;
267 // if defaultDir is specified it should contain the directory and
268 // defaultFileName should contain the default name of the file, however if
269 // directory is not given, defaultFileName contains both
271 if ( defaultDir
.empty() )
272 fn
.Assign(defaultFileNameWithExt
);
273 else if ( !defaultFileNameWithExt
.empty() )
274 fn
.Assign(defaultDir
, defaultFileNameWithExt
);
276 fn
.AssignDir(defaultDir
);
278 // set the initial file name and/or directory
279 fn
.MakeAbsolute(); // GTK+ needs absolute path
280 const wxString dir
= fn
.GetPath();
283 gtk_file_chooser_set_current_folder(file_chooser
, wxGTK_CONV_FN(dir
));
286 const wxString fname
= fn
.GetFullName();
287 if ( style
& wxFD_SAVE
)
289 if ( !fname
.empty() )
291 gtk_file_chooser_set_current_name(file_chooser
, wxGTK_CONV_FN(fname
));
294 #if GTK_CHECK_VERSION(2,7,3)
295 if ((style
& wxFD_OVERWRITE_PROMPT
)
297 && gtk_check_version(2,7,3) == NULL
301 gtk_file_chooser_set_do_overwrite_confirmation(file_chooser
, true);
307 if ( !fname
.empty() )
309 gtk_file_chooser_set_filename(file_chooser
,
310 wxGTK_CONV_FN(fn
.GetFullPath()));
314 if ( style
& wxFD_PREVIEW
)
316 GtkWidget
*previewImage
= gtk_image_new();
318 gtk_file_chooser_set_preview_widget(file_chooser
, previewImage
);
319 g_signal_connect(m_widget
, "update-preview",
320 G_CALLBACK(gtk_filedialog_update_preview_callback
),
327 wxFileDialog::~wxFileDialog()
331 // get chooser to drop its reference right now, allowing wxWindow dtor
332 // to verify that ref count drops to zero
333 gtk_file_chooser_set_extra_widget(
334 GTK_FILE_CHOOSER(m_widget
), NULL
);
338 void wxFileDialog::OnFakeOk(wxCommandEvent
& WXUNUSED(event
))
343 int wxFileDialog::ShowModal()
345 CreateExtraControl();
347 return wxDialog::ShowModal();
350 void wxFileDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
351 int WXUNUSED(width
), int WXUNUSED(height
),
352 int WXUNUSED(sizeFlags
))
356 void wxFileDialog::OnSize(wxSizeEvent
&)
358 // avoid calling DoLayout(), which will set the (wrong) size of
359 // m_extraControl, its size is managed by GtkFileChooser
362 wxString
wxFileDialog::GetPath() const
364 return m_fc
.GetPath();
367 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
369 m_fc
.GetFilenames( files
);
372 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
374 m_fc
.GetPaths( paths
);
377 void wxFileDialog::SetMessage(const wxString
& message
)
383 void wxFileDialog::SetPath(const wxString
& path
)
385 // Don't do anything if no path is specified, in particular don't set the
386 // path to m_dir below as this would result in opening the dialog in the
387 // parent directory of this one instead of m_dir itself.
391 // we need an absolute path for GTK native chooser so ensure that we have
392 // it: use the initial directory if it was set or just CWD otherwise (this
393 // is the default behaviour if m_dir is empty)
395 fn
.MakeAbsolute(m_dir
);
396 m_fc
.SetPath(fn
.GetFullPath());
399 void wxFileDialog::SetDirectory(const wxString
& dir
)
401 if (m_fc
.SetDirectory( dir
))
403 // Cache the dir, as gtk_file_chooser_get_current_folder()
404 // doesn't return anything until the dialog has been shown
409 wxString
wxFileDialog::GetDirectory() const
411 wxString
currentDir( m_fc
.GetDirectory() );
412 if (currentDir
.empty())
414 // m_fc.GetDirectory() will return empty until the dialog has been shown
415 // in which case use any previously provided value
421 void wxFileDialog::SetFilename(const wxString
& name
)
423 if (HasFdFlag(wxFD_SAVE
))
425 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
431 wxString
path( GetDirectory() );
434 // SetPath() fires an assert if fed other than filepaths
437 SetPath(wxFileName(path
, name
).GetFullPath());
442 wxString
wxFileDialog::GetFilename() const
444 wxString
currentFilename( m_fc
.GetFilename() );
445 if (currentFilename
.empty())
447 // m_fc.GetFilename() will return empty until the dialog has been shown
448 // in which case use any previously provided value
449 currentFilename
= m_fileName
;
451 return currentFilename
;
454 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
456 wxFileDialogBase::SetWildcard(wildCard
);
457 m_fc
.SetWildcard( GetWildcard() );
460 void wxFileDialog::SetFilterIndex(int filterIndex
)
462 m_fc
.SetFilterIndex( filterIndex
);
465 int wxFileDialog::GetFilterIndex() const
467 return m_fc
.GetFilterIndex();
470 #endif // wxUSE_FILEDLG