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"
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
)
46 if ((style
& wxFD_SAVE
) && (style
& wxFD_OVERWRITE_PROMPT
))
48 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
53 _("File '%s' already exists, do you really want to overwrite it?"),
54 wxString(filename
, *wxConvFileName
));
56 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
57 wxYES_NO
| wxICON_QUESTION
);
58 if (dlg
.ShowModal() != wxID_YES
)
64 if (style
& wxFD_FILE_MUST_EXIST
)
66 if ( !g_file_test(filename
, G_FILE_TEST_EXISTS
) )
68 wxMessageDialog
dlg( dialog
, _("Please choose an existing file."),
69 _("Error"), wxOK
| wxICON_ERROR
);
75 // change to the directory where the user went if asked
76 if (style
& wxFD_CHANGE_DIR
)
78 // Use chdir to not care about filename encodings
79 wxGtkString
folder(g_path_get_dirname(filename
));
83 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
84 event
.SetEventObject(dialog
);
85 dialog
->HandleWindowEvent(event
);
89 //-----------------------------------------------------------------------------
90 // "clicked" for Cancel-button
91 //-----------------------------------------------------------------------------
97 gtk_filedialog_cancel_callback(GtkWidget
* WXUNUSED(w
), wxFileDialog
*dialog
)
99 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
100 event
.SetEventObject(dialog
);
101 dialog
->HandleWindowEvent(event
);
104 static void gtk_filedialog_response_callback(GtkWidget
*w
,
106 wxFileDialog
*dialog
)
108 if (response
== GTK_RESPONSE_ACCEPT
)
109 gtk_filedialog_ok_callback(w
, dialog
);
110 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
111 gtk_filedialog_cancel_callback(w
, dialog
);
114 static void gtk_filedialog_update_preview_callback(GtkFileChooser
*chooser
,
117 GtkWidget
*preview
= GTK_WIDGET(user_data
);
119 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
124 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
125 gboolean have_preview
= pixbuf
!= NULL
;
127 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
129 g_object_unref (pixbuf
);
131 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
136 //-----------------------------------------------------------------------------
137 // "size_request" from m_extraControl
138 //-----------------------------------------------------------------------------
141 static void extra_widget_size_request(GtkWidget
*, GtkRequisition
* req
, wxWindow
* win
)
143 // allow dialog to be resized smaller horizontally
144 req
->width
= win
->GetMinWidth();
148 static void wxInsertChildInFileDialog(wxWindow
* parent
, wxWindow
* child
)
150 g_signal_connect_after(child
->m_widget
, "size_request",
151 G_CALLBACK(extra_widget_size_request
), child
);
152 gtk_file_chooser_set_extra_widget(
153 GTK_FILE_CHOOSER(parent
->m_widget
), child
->m_widget
);
156 //-----------------------------------------------------------------------------
158 //-----------------------------------------------------------------------------
160 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxFileDialogBase
)
162 BEGIN_EVENT_TABLE(wxFileDialog
,wxFileDialogBase
)
163 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
164 EVT_SIZE(wxFileDialog::OnSize
)
167 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
168 const wxString
& defaultDir
,
169 const wxString
& defaultFileName
,
170 const wxString
& wildCard
,
171 long style
, const wxPoint
& pos
,
173 const wxString
& name
)
176 m_insertCallback
= wxInsertChildInFileDialog
;
177 parent
= GetParentForModalDialog(parent
);
179 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFileName
,
180 wildCard
, style
, pos
, sz
, name
))
185 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
186 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
187 wxDefaultValidator
, wxT("filedialog")))
189 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
193 GtkFileChooserAction gtk_action
;
194 GtkWindow
* gtk_parent
= NULL
;
196 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
198 const gchar
* ok_btn_stock
;
199 if ( style
& wxFD_SAVE
)
201 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
202 ok_btn_stock
= GTK_STOCK_SAVE
;
206 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
207 ok_btn_stock
= GTK_STOCK_OPEN
;
210 m_widget
= gtk_file_chooser_dialog_new(
211 wxGTK_CONV(m_message
),
214 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
215 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
217 GtkFileChooser
* file_chooser
= GTK_FILE_CHOOSER(m_widget
);
219 m_fc
.SetWidget(file_chooser
);
221 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
223 if ( style
& wxFD_MULTIPLE
)
224 gtk_file_chooser_set_select_multiple(file_chooser
, true);
226 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
227 // destroys the dialog when the user press ESC on the dialog: in that case
228 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
230 g_signal_connect(m_widget
,
232 G_CALLBACK (gtk_widget_hide_on_delete
),
235 // local-only property could be set to false to allow non-local files to be
236 // loaded. In that case get/set_uri(s) should be used instead of
237 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
238 // probably also be created with a backend, e.g "gnome-vfs", "default", ...
239 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
240 // as the default - true:
241 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
243 g_signal_connect (m_widget
, "response",
244 G_CALLBACK (gtk_filedialog_response_callback
), this);
246 SetWildcard(wildCard
);
248 // if defaultDir is specified it should contain the directory and
249 // defaultFileName should contain the default name of the file, however if
250 // directory is not given, defaultFileName contains both
252 if ( defaultDir
.empty() )
253 fn
.Assign(defaultFileName
);
254 else if ( !defaultFileName
.empty() )
255 fn
.Assign(defaultDir
, defaultFileName
);
257 fn
.AssignDir(defaultDir
);
259 // set the initial file name and/or directory
260 fn
.MakeAbsolute(); // GTK+ needs absolute path
261 const wxString dir
= fn
.GetPath();
264 gtk_file_chooser_set_current_folder(file_chooser
, dir
.fn_str());
267 const wxString fname
= fn
.GetFullName();
268 if ( style
& wxFD_SAVE
)
270 if ( !fname
.empty() )
272 gtk_file_chooser_set_current_name(file_chooser
, fname
.fn_str());
275 #if GTK_CHECK_VERSION(2,7,3)
276 if ((style
& wxFD_OVERWRITE_PROMPT
) && !gtk_check_version(2,7,3))
277 gtk_file_chooser_set_do_overwrite_confirmation(file_chooser
, true);
282 if ( !fname
.empty() )
284 gtk_file_chooser_set_filename(file_chooser
,
285 fn
.GetFullPath().fn_str());
289 if ( style
& wxFD_PREVIEW
)
291 GtkWidget
*previewImage
= gtk_image_new();
293 gtk_file_chooser_set_preview_widget(file_chooser
, previewImage
);
294 g_signal_connect(m_widget
, "update-preview",
295 G_CALLBACK(gtk_filedialog_update_preview_callback
),
300 void wxFileDialog::OnFakeOk(wxCommandEvent
& WXUNUSED(event
))
305 int wxFileDialog::ShowModal()
307 CreateExtraControl();
309 return wxDialog::ShowModal();
312 void wxFileDialog::DoSetSize(int WXUNUSED(x
), int WXUNUSED(y
),
313 int WXUNUSED(width
), int WXUNUSED(height
),
314 int WXUNUSED(sizeFlags
))
318 void wxFileDialog::OnSize(wxSizeEvent
&)
320 // avoid calling DoLayout(), which will set the (wrong) size of
321 // m_extraControl, its size is managed by GtkFileChooser
324 wxString
wxFileDialog::GetPath() const
326 return m_fc
.GetPath();
329 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
331 m_fc
.GetFilenames( files
);
334 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
336 m_fc
.GetPaths( paths
);
339 void wxFileDialog::SetMessage(const wxString
& message
)
345 void wxFileDialog::SetPath(const wxString
& path
)
347 m_fc
.SetPath( path
);
350 void wxFileDialog::SetDirectory(const wxString
& dir
)
352 m_fc
.SetDirectory( dir
);
355 wxString
wxFileDialog::GetDirectory() const
357 return m_fc
.GetDirectory();
360 void wxFileDialog::SetFilename(const wxString
& name
)
362 if (HasFdFlag(wxFD_SAVE
))
363 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
365 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
368 wxString
wxFileDialog::GetFilename() const
370 return m_fc
.GetFilename();
373 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
375 m_fc
.SetWildcard( wildCard
);
378 void wxFileDialog::SetFilterIndex(int filterIndex
)
381 m_fc
.SetFilterIndex( filterIndex
);
384 int wxFileDialog::GetFilterIndex() const
386 return m_fc
.GetFilterIndex();
389 #endif // wxUSE_FILEDLG