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"
13 #if wxUSE_FILEDLG && defined(__WXGTK24__)
15 #include "wx/filedlg.h"
19 #include "wx/msgdlg.h"
23 #include "wx/gtk/private.h"
25 #include <unistd.h> // chdir
27 #include "wx/filename.h" // wxFilename
28 #include "wx/tokenzr.h" // wxStringTokenizer
29 #include "wx/filefn.h" // ::wxGetCwd
31 //-----------------------------------------------------------------------------
32 // "clicked" for OK-button
33 //-----------------------------------------------------------------------------
36 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
38 int style
= dialog
->GetWindowStyle();
39 wxGtkString
filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
)));
41 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
42 #if GTK_CHECK_VERSION(2,7,3)
43 if(gtk_check_version(2,7,3) != NULL
)
45 if ((style
& wxFD_SAVE
) && (style
& wxFD_OVERWRITE_PROMPT
))
47 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
52 _("File '%s' already exists, do you really want to overwrite it?"),
53 wxString(filename
, *wxConvFileName
));
55 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
56 wxYES_NO
| wxICON_QUESTION
);
57 if (dlg
.ShowModal() != wxID_YES
)
62 if (style
& wxFD_FILE_MUST_EXIST
)
64 if ( !g_file_test(filename
, G_FILE_TEST_EXISTS
) )
66 wxMessageDialog
dlg( dialog
, _("Please choose an existing file."),
67 _("Error"), wxOK
| wxICON_ERROR
);
73 // change to the directory where the user went if asked
74 if (style
& wxFD_CHANGE_DIR
)
76 // Use chdir to not care about filename encodings
77 wxGtkString
folder(g_path_get_dirname(filename
));
81 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
82 event
.SetEventObject(dialog
);
83 dialog
->GetEventHandler()->ProcessEvent(event
);
87 //-----------------------------------------------------------------------------
88 // "clicked" for Cancel-button
89 //-----------------------------------------------------------------------------
95 gtk_filedialog_cancel_callback(GtkWidget
* WXUNUSED(w
), wxFileDialog
*dialog
)
97 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
98 event
.SetEventObject(dialog
);
99 dialog
->GetEventHandler()->ProcessEvent(event
);
102 static void gtk_filedialog_response_callback(GtkWidget
*w
,
104 wxFileDialog
*dialog
)
106 if (response
== GTK_RESPONSE_ACCEPT
)
107 gtk_filedialog_ok_callback(w
, dialog
);
108 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
109 gtk_filedialog_cancel_callback(w
, dialog
);
112 static void gtk_filedialog_update_preview_callback(GtkFileChooser
*chooser
,
115 #if GTK_CHECK_VERSION(2,4,0)
116 GtkWidget
*preview
= GTK_WIDGET(user_data
);
118 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
123 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
124 gboolean have_preview
= pixbuf
!= NULL
;
126 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
128 g_object_unref (pixbuf
);
130 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
132 wxUnusedVar(chooser
);
133 wxUnusedVar(user_data
);
140 //-----------------------------------------------------------------------------
142 //-----------------------------------------------------------------------------
144 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
146 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
147 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
150 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
151 const wxString
& defaultDir
,
152 const wxString
& defaultFileName
,
153 const wxString
& wildCard
,
154 long style
, const wxPoint
& pos
,
156 const wxString
& name
)
157 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
158 wildCard
, style
, pos
, sz
, name
, true )
160 if (gtk_check_version(2,4,0))
162 wxGenericFileDialog::Create( parent
, message
, defaultDir
,
163 defaultFileName
, wildCard
, style
, pos
);
167 parent
= GetParentForModalDialog(parent
);
169 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
170 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
171 wxDefaultValidator
, wxT("filedialog")))
173 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
177 GtkFileChooserAction gtk_action
;
178 GtkWindow
* gtk_parent
= NULL
;
180 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
182 const gchar
* ok_btn_stock
;
183 if ( style
& wxFD_SAVE
)
185 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
186 ok_btn_stock
= GTK_STOCK_SAVE
;
190 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
191 ok_btn_stock
= GTK_STOCK_OPEN
;
194 m_widget
= gtk_file_chooser_dialog_new(
195 wxGTK_CONV(m_message
),
198 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
199 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
202 m_fc
.SetWidget( GTK_FILE_CHOOSER(m_widget
) );
204 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
206 if ( style
& wxFD_MULTIPLE
)
207 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
209 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
210 // destroys the dialog when the user press ESC on the dialog: in that case
211 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
213 g_signal_connect (G_OBJECT(m_widget
),
215 G_CALLBACK (gtk_widget_hide_on_delete
),
218 // local-only property could be set to false to allow non-local files to be
219 // loaded. In that case get/set_uri(s) should be used instead of
220 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
221 // probably also be created with a backend, e.g "gnome-vfs", "default", ...
222 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
223 // as the default - true:
224 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
226 g_signal_connect (m_widget
, "response",
227 G_CALLBACK (gtk_filedialog_response_callback
), this);
229 SetWildcard(wildCard
);
231 // if defaultDir is specified it should contain the directory and
232 // defaultFileName should contain the default name of the file, however if
233 // directory is not given, defaultFileName contains both
235 if ( defaultDir
.empty() )
236 fn
.Assign(defaultFileName
);
237 else if ( !defaultFileName
.empty() )
238 fn
.Assign(defaultDir
, defaultFileName
);
240 fn
.AssignDir(defaultDir
);
242 // set the initial file name and/or directory
243 fn
.MakeAbsolute(); // GTK+ needs absolute path
244 const wxString dir
= fn
.GetPath();
247 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
251 const wxString fname
= fn
.GetFullName();
252 if ( style
& wxFD_SAVE
)
254 if ( !fname
.empty() )
256 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
260 #if GTK_CHECK_VERSION(2,7,3)
261 if ((style
& wxFD_OVERWRITE_PROMPT
) && !gtk_check_version(2,7,3))
262 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget
), TRUE
);
267 if ( !fname
.empty() )
269 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
270 fn
.GetFullPath().fn_str());
274 #if GTK_CHECK_VERSION(2,4,0)
275 if ( style
& wxFD_PREVIEW
)
277 GtkWidget
*previewImage
= gtk_image_new();
279 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget
),
281 g_signal_connect(m_widget
, "update-preview",
282 G_CALLBACK(gtk_filedialog_update_preview_callback
),
288 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
290 if (!gtk_check_version(2,4,0))
293 wxGenericFileDialog::OnOk( event
);
296 int wxFileDialog::ShowModal()
298 if (!gtk_check_version(2,4,0))
299 return wxDialog::ShowModal();
301 return wxGenericFileDialog::ShowModal();
304 bool wxFileDialog::Show( bool show
)
306 if (!gtk_check_version(2,4,0))
307 return wxDialog::Show( show
);
309 return wxGenericFileDialog::Show( show
);
312 void wxFileDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
317 wxGenericFileDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
320 wxString
wxFileDialog::GetPath() const
322 if (!gtk_check_version(2,4,0))
324 return m_fc
.GetPath();
327 return wxGenericFileDialog::GetPath();
330 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
332 if (!gtk_check_version(2,4,0))
334 m_fc
.GetFilenames( files
);
337 wxGenericFileDialog::GetFilenames( files
);
340 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
342 if (!gtk_check_version(2,4,0))
344 m_fc
.GetPaths( paths
);
347 wxGenericFileDialog::GetPaths( paths
);
350 void wxFileDialog::SetMessage(const wxString
& message
)
352 if (!gtk_check_version(2,4,0))
358 wxGenericFileDialog::SetMessage( message
);
361 void wxFileDialog::SetPath(const wxString
& path
)
363 if (!gtk_check_version(2,4,0))
365 m_fc
.SetPath( path
);
368 wxGenericFileDialog::SetPath( path
);
371 void wxFileDialog::SetDirectory(const wxString
& dir
)
373 if (!gtk_check_version(2,4,0))
375 m_fc
.SetDirectory( dir
);
378 wxGenericFileDialog::SetDirectory( dir
);
381 wxString
wxFileDialog::GetDirectory() const
383 if (!gtk_check_version(2,4,0))
388 return wxGenericFileDialog::GetDirectory();
391 void wxFileDialog::SetFilename(const wxString
& name
)
393 if (!gtk_check_version(2,4,0))
395 if (HasFdFlag(wxFD_SAVE
))
396 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
398 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
401 wxGenericFileDialog::SetFilename( name
);
404 wxString
wxFileDialog::GetFilename() const
406 if (!gtk_check_version(2,4,0))
407 return m_fc
.GetFilename();
409 return wxGenericFileDialog::GetFilename();
412 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
414 if (!gtk_check_version(2,4,0))
416 m_fc
.SetWildcard( wildCard
);
419 wxGenericFileDialog::SetWildcard( wildCard
);
422 void wxFileDialog::SetFilterIndex(int filterIndex
)
425 if (!gtk_check_version(2,4,0))
427 m_fc
.SetFilterIndex( filterIndex
);
430 wxGenericFileDialog::SetFilterIndex( filterIndex
);
433 int wxFileDialog::GetFilterIndex() const
435 if (!gtk_check_version(2,4,0))
437 return m_fc
.GetFilterIndex();
440 return wxGenericFileDialog::GetFilterIndex();
443 #endif // wxUSE_FILEDLG && __WXGTK24__