]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/filedlg.cpp
Fix broken and missing DataView interface items for Phoenix
[wxWidgets.git] / src / gtk / filedlg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
9b5f1895 2// Name: src/gtk/filedlg.cpp
9755e73b 3// Purpose: native implementation of wxFileDialog
f8bc53eb 4// Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
a81258be 5// Id: $Id$
f8bc53eb 6// Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
03647350 13#if wxUSE_FILEDLG
9755e73b 14
c801d85f 15#include "wx/filedlg.h"
4e1901b7 16
88a7a4e1
WS
17#ifndef WX_PRECOMP
18 #include "wx/intl.h"
246c5004 19 #include "wx/msgdlg.h"
88a7a4e1
WS
20#endif
21
f8bc53eb 22#include <gtk/gtk.h>
9755e73b 23#include "wx/gtk/private.h"
83624f79 24
f04f570f 25#ifdef __UNIX__
f8bc53eb 26#include <unistd.h> // chdir
f04f570f 27#endif
f8bc53eb 28
f8bc53eb
JS
29#include "wx/filename.h" // wxFilename
30#include "wx/tokenzr.h" // wxStringTokenizer
31#include "wx/filefn.h" // ::wxGetCwd
643e9cf9 32#include "wx/testing.h"
f8bc53eb 33
291a8f20
RR
34//-----------------------------------------------------------------------------
35// "clicked" for OK-button
c801d85f
KB
36//-----------------------------------------------------------------------------
37
865bb325 38extern "C" {
9755e73b 39static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
c801d85f 40{
ff3e84ff 41 int style = dialog->GetWindowStyle();
e808cf8a 42 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
035b704a 43
b5ab6d19 44 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
9dc44eff 45#ifndef __WXGTK3__
b5ab6d19 46#if GTK_CHECK_VERSION(2,7,3)
f2448f21 47 if (gtk_check_version(2, 7, 3) != NULL)
b5ab6d19 48#endif
bfc6fde4 49 {
f2448f21 50 if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
0e1399b3 51 {
f2448f21
PC
52 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
53 {
54 wxString msg;
55
56 msg.Printf(
57 _("File '%s' already exists, do you really want to overwrite it?"),
b74d7c85 58 wxString::FromUTF8(filename));
f2448f21
PC
59
60 wxMessageDialog dlg(dialog, msg, _("Confirm"),
61 wxYES_NO | wxICON_QUESTION);
62 if (dlg.ShowModal() != wxID_YES)
63 return;
64 }
0e1399b3 65 }
83624f79 66 }
9dc44eff 67#endif
035b704a 68
4c84a0dc
RR
69 if (style & wxFD_FILE_MUST_EXIST)
70 {
71 if ( !g_file_test(filename, G_FILE_TEST_EXISTS) )
72 {
e4161a2a 73 wxMessageDialog dlg( dialog, _("Please choose an existing file."),
4c84a0dc
RR
74 _("Error"), wxOK| wxICON_ERROR);
75 dlg.ShowModal();
76 return;
77 }
78 }
e4161a2a 79
3f6638b8 80 // change to the directory where the user went if asked
ff3e84ff 81 if (style & wxFD_CHANGE_DIR)
3f6638b8 82 {
f8bc53eb 83 // Use chdir to not care about filename encodings
e808cf8a 84 wxGtkString folder(g_path_get_dirname(filename));
f8bc53eb 85 chdir(folder);
3f6638b8
VZ
86 }
87
bfc6fde4 88 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
9755e73b 89 event.SetEventObject(dialog);
937013e0 90 dialog->HandleWindowEvent(event);
ff7b1510 91}
865bb325 92}
c801d85f 93
291a8f20
RR
94//-----------------------------------------------------------------------------
95// "clicked" for Cancel-button
96//-----------------------------------------------------------------------------
97
9f057af5
VZ
98extern "C"
99{
100
e4161a2a
VZ
101static void
102gtk_filedialog_cancel_callback(GtkWidget * WXUNUSED(w), wxFileDialog *dialog)
c801d85f 103{
bfc6fde4 104 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
9755e73b 105 event.SetEventObject(dialog);
937013e0 106 dialog->HandleWindowEvent(event);
9755e73b
VS
107}
108
109static void gtk_filedialog_response_callback(GtkWidget *w,
f8bc53eb 110 gint response,
9755e73b
VS
111 wxFileDialog *dialog)
112{
a7334928 113 if (response == GTK_RESPONSE_ACCEPT)
9755e73b 114 gtk_filedialog_ok_callback(w, dialog);
a552d120 115 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
c2740a5a 116 gtk_filedialog_cancel_callback(w, dialog);
ff7b1510 117}
9f057af5
VZ
118
119static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser,
120 gpointer user_data)
121{
9f057af5 122 GtkWidget *preview = GTK_WIDGET(user_data);
3ab296d9 123
e808cf8a 124 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
3ab296d9 125
9f057af5
VZ
126 if ( !filename )
127 return;
128
129 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL);
130 gboolean have_preview = pixbuf != NULL;
131
132 gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf);
133 if ( pixbuf )
134 g_object_unref (pixbuf);
135
136 gtk_file_chooser_set_preview_widget_active(chooser, have_preview);
865bb325
VZ
137}
138
9f057af5
VZ
139} // extern "C"
140
3b7067a0 141void wxFileDialog::AddChildGTK(wxWindowGTK* child)
38a36cf5
PC
142{
143 // allow dialog to be resized smaller horizontally
3b7067a0
PC
144 gtk_widget_set_size_request(
145 child->m_widget, child->GetMinWidth(), child->m_height);
38a36cf5 146
3abfbb43 147 gtk_file_chooser_set_extra_widget(
48200154 148 GTK_FILE_CHOOSER(m_widget), child->m_widget);
8ce68f7f
VZ
149}
150
291a8f20
RR
151//-----------------------------------------------------------------------------
152// wxFileDialog
153//-----------------------------------------------------------------------------
154
700d08c1 155IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
4e1901b7 156
700d08c1 157BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase)
f8bc53eb 158 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
f1d5aa4e 159 EVT_SIZE(wxFileDialog::OnSize)
4e1901b7 160END_EVENT_TABLE()
c801d85f 161
9755e73b
VS
162wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
163 const wxString& defaultDir,
164 const wxString& defaultFileName,
165 const wxString& wildCard,
ff3e84ff
VZ
166 long style, const wxPoint& pos,
167 const wxSize& sz,
168 const wxString& name)
700d08c1 169 : wxFileDialogBase()
e3f54c8f
VZ
170{
171 Create(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name);
172}
173
174bool wxFileDialog::Create(wxWindow *parent, const wxString& message,
175 const wxString& defaultDir,
176 const wxString& defaultFileName,
177 const wxString& wildCard,
178 long style, const wxPoint& pos,
179 const wxSize& sz,
180 const wxString& name)
c801d85f 181{
cdc48273 182 parent = GetParentForModalDialog(parent, style);
f2448f21 183
700d08c1
RR
184 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName,
185 wildCard, style, pos, sz, name))
77f70672 186 {
e3f54c8f 187 return false;
9f057af5 188 }
83624f79 189
9f057af5
VZ
190 if (!PreCreation(parent, pos, wxDefaultSize) ||
191 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
192 wxDefaultValidator, wxT("filedialog")))
193 {
194 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
e3f54c8f 195 return false;
9f057af5 196 }
3f6638b8 197
9f057af5
VZ
198 GtkFileChooserAction gtk_action;
199 GtkWindow* gtk_parent = NULL;
200 if (parent)
201 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
27b2dd53 202
9f057af5
VZ
203 const gchar* ok_btn_stock;
204 if ( style & wxFD_SAVE )
205 {
206 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
207 ok_btn_stock = GTK_STOCK_SAVE;
208 }
209 else
210 {
211 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
212 ok_btn_stock = GTK_STOCK_OPEN;
213 }
f8bc53eb 214
9f057af5
VZ
215 m_widget = gtk_file_chooser_dialog_new(
216 wxGTK_CONV(m_message),
217 gtk_parent,
218 gtk_action,
219 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
220 ok_btn_stock, GTK_RESPONSE_ACCEPT,
221 NULL);
9ff9d30c 222 g_object_ref(m_widget);
f2448f21 223 GtkFileChooser* file_chooser = GTK_FILE_CHOOSER(m_widget);
9755e73b 224
f2448f21 225 m_fc.SetWidget(file_chooser);
0cf3e587 226
9f057af5 227 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
c1dfe97c 228
9f057af5 229 if ( style & wxFD_MULTIPLE )
f2448f21 230 gtk_file_chooser_set_select_multiple(file_chooser, true);
27b2dd53 231
bf345206
VZ
232 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
233 // destroys the dialog when the user press ESC on the dialog: in that case
234 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
235 // errors...
f2448f21 236 g_signal_connect(m_widget,
9f057af5
VZ
237 "delete_event",
238 G_CALLBACK (gtk_widget_hide_on_delete),
f2448f21 239 this);
a552d120 240
bf345206
VZ
241 // local-only property could be set to false to allow non-local files to be
242 // loaded. In that case get/set_uri(s) should be used instead of
243 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
d13b34d3 244 // probably also be created with a backend, e.g. "gnome-vfs", "default", ...
bf345206
VZ
245 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
246 // as the default - true:
9f057af5 247 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
27b2dd53 248
9f057af5
VZ
249 g_signal_connect (m_widget, "response",
250 G_CALLBACK (gtk_filedialog_response_callback), this);
27b2dd53 251
fa333077
VZ
252
253 // deal with extensions/filters
9f057af5 254 SetWildcard(wildCard);
f8bc53eb 255
fa333077
VZ
256 wxString defaultFileNameWithExt = defaultFileName;
257 if ( !wildCard.empty() && !defaultFileName.empty() &&
258 !wxFileName(defaultFileName).HasExt() )
259 {
260 // append the default extension to the initial file name: GTK won't do
261 // it for us by default (unlike e.g. MSW)
262 const wxString defaultExt = m_fc.GetCurrentWildCard().AfterFirst('.');
263 if ( defaultExt.find_first_of("?*") == wxString::npos )
264 defaultFileNameWithExt += "." + defaultExt;
265 }
266
267
bf345206
VZ
268 // if defaultDir is specified it should contain the directory and
269 // defaultFileName should contain the default name of the file, however if
270 // directory is not given, defaultFileName contains both
271 wxFileName fn;
272 if ( defaultDir.empty() )
fa333077
VZ
273 fn.Assign(defaultFileNameWithExt);
274 else if ( !defaultFileNameWithExt.empty() )
275 fn.Assign(defaultDir, defaultFileNameWithExt);
f241631e
RD
276 else
277 fn.AssignDir(defaultDir);
bf345206
VZ
278
279 // set the initial file name and/or directory
78355ffc 280 fn.MakeAbsolute(); // GTK+ needs absolute path
3a41827a
VZ
281 const wxString dir = fn.GetPath();
282 if ( !dir.empty() )
9f057af5 283 {
c282e47d 284 gtk_file_chooser_set_current_folder(file_chooser, wxGTK_CONV_FN(dir));
bf345206 285 }
f8bc53eb 286
3a41827a 287 const wxString fname = fn.GetFullName();
bf345206
VZ
288 if ( style & wxFD_SAVE )
289 {
290 if ( !fname.empty() )
291 {
c282e47d 292 gtk_file_chooser_set_current_name(file_chooser, wxGTK_CONV_FN(fname));
bf345206 293 }
b5ab6d19
MR
294
295#if GTK_CHECK_VERSION(2,7,3)
9dc44eff
PC
296 if ((style & wxFD_OVERWRITE_PROMPT)
297#ifndef __WXGTK3__
298 && gtk_check_version(2,7,3) == NULL
299#endif
300 )
301 {
f2448f21 302 gtk_file_chooser_set_do_overwrite_confirmation(file_chooser, true);
9dc44eff 303 }
b5ab6d19 304#endif
9f057af5
VZ
305 }
306 else // wxFD_OPEN
307 {
bf345206 308 if ( !fname.empty() )
f8bc53eb 309 {
f2448f21 310 gtk_file_chooser_set_filename(file_chooser,
c282e47d 311 wxGTK_CONV_FN(fn.GetFullPath()));
f8bc53eb 312 }
77f70672 313 }
9f057af5 314
9f057af5
VZ
315 if ( style & wxFD_PREVIEW )
316 {
317 GtkWidget *previewImage = gtk_image_new();
318
f2448f21 319 gtk_file_chooser_set_preview_widget(file_chooser, previewImage);
9f057af5
VZ
320 g_signal_connect(m_widget, "update-preview",
321 G_CALLBACK(gtk_filedialog_update_preview_callback),
322 previewImage);
323 }
e3f54c8f
VZ
324
325 return true;
9755e73b 326}
0e1399b3 327
9ff9d30c
PC
328wxFileDialog::~wxFileDialog()
329{
330 if (m_extraControl)
331 {
332 // get chooser to drop its reference right now, allowing wxWindow dtor
333 // to verify that ref count drops to zero
334 gtk_file_chooser_set_extra_widget(
335 GTK_FILE_CHOOSER(m_widget), NULL);
336 }
337}
338
ff654490 339void wxFileDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event))
4e1901b7 340{
700d08c1 341 EndDialog(wxID_OK);
4e1901b7
RR
342}
343
344int wxFileDialog::ShowModal()
345{
643e9cf9
VS
346 WX_TESTING_SHOW_MODAL_HOOK();
347
3abfbb43 348 CreateExtraControl();
8ce68f7f
VZ
349
350 return wxDialog::ShowModal();
9755e73b 351}
0e1399b3 352
03647350
VZ
353void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
354 int WXUNUSED(width), int WXUNUSED(height),
700d08c1 355 int WXUNUSED(sizeFlags))
5b2e23bf 356{
5b2e23bf
RR
357}
358
f1d5aa4e
PC
359void wxFileDialog::OnSize(wxSizeEvent&)
360{
361 // avoid calling DoLayout(), which will set the (wrong) size of
362 // m_extraControl, its size is managed by GtkFileChooser
363}
364
f8bc53eb
JS
365wxString wxFileDialog::GetPath() const
366{
fa333077 367 return m_fc.GetPath();
f8bc53eb
JS
368}
369
27b2dd53 370void wxFileDialog::GetFilenames(wxArrayString& files) const
9755e73b 371{
700d08c1 372 m_fc.GetFilenames( files );
9755e73b 373}
76840ed0 374
27b2dd53 375void wxFileDialog::GetPaths(wxArrayString& paths) const
9755e73b 376{
700d08c1 377 m_fc.GetPaths( paths );
9755e73b 378}
035b704a 379
9755e73b
VS
380void wxFileDialog::SetMessage(const wxString& message)
381{
700d08c1
RR
382 m_message = message;
383 SetTitle(message);
9755e73b
VS
384}
385
76840ed0
RR
386void wxFileDialog::SetPath(const wxString& path)
387{
ab49aec9
VS
388 wxFileDialogBase::SetPath(path);
389
dea43e6e
VZ
390 // Don't do anything if no path is specified, in particular don't set the
391 // path to m_dir below as this would result in opening the dialog in the
392 // parent directory of this one instead of m_dir itself.
393 if ( path.empty() )
394 return;
395
8f79ece3 396 // we need an absolute path for GTK native chooser so ensure that we have
257d35ae
VZ
397 // it: use the initial directory if it was set or just CWD otherwise (this
398 // is the default behaviour if m_dir is empty)
8f79ece3 399 wxFileName fn(path);
257d35ae 400 fn.MakeAbsolute(m_dir);
8f79ece3 401 m_fc.SetPath(fn.GetFullPath());
76840ed0
RR
402}
403
9755e73b
VS
404void wxFileDialog::SetDirectory(const wxString& dir)
405{
ab49aec9
VS
406 wxFileDialogBase::SetDirectory(dir);
407
408 m_fc.SetDirectory(dir);
9755e73b
VS
409}
410
f8bc53eb
JS
411wxString wxFileDialog::GetDirectory() const
412{
02a40b8a
JS
413 wxString currentDir( m_fc.GetDirectory() );
414 if (currentDir.empty())
415 {
416 // m_fc.GetDirectory() will return empty until the dialog has been shown
417 // in which case use any previously provided value
418 currentDir = m_dir;
419 }
420 return currentDir;
f8bc53eb
JS
421}
422
9755e73b
VS
423void wxFileDialog::SetFilename(const wxString& name)
424{
ab49aec9
VS
425 wxFileDialogBase::SetFilename(name);
426
700d08c1 427 if (HasFdFlag(wxFD_SAVE))
02a40b8a 428 {
700d08c1 429 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
02a40b8a
JS
430 }
431
77f70672 432 else
02a40b8a
JS
433 {
434 wxString path( GetDirectory() );
435 if (path.empty())
436 {
437 // SetPath() fires an assert if fed other than filepaths
438 return;
439 }
440 SetPath(wxFileName(path, name).GetFullPath());
02a40b8a 441 }
9755e73b 442}
035b704a 443
f8bc53eb
JS
444wxString wxFileDialog::GetFilename() const
445{
02a40b8a
JS
446 wxString currentFilename( m_fc.GetFilename() );
447 if (currentFilename.empty())
448 {
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;
452 }
453 return currentFilename;
f8bc53eb
JS
454}
455
9755e73b
VS
456void wxFileDialog::SetWildcard(const wxString& wildCard)
457{
b534aeb2
VZ
458 wxFileDialogBase::SetWildcard(wildCard);
459 m_fc.SetWildcard( GetWildcard() );
9755e73b 460}
a3622daa 461
9755e73b
VS
462void wxFileDialog::SetFilterIndex(int filterIndex)
463{
700d08c1 464 m_fc.SetFilterIndex( filterIndex);
4e1901b7
RR
465}
466
f8bc53eb 467int wxFileDialog::GetFilterIndex() const
4e1901b7 468{
700d08c1 469 return m_fc.GetFilterIndex();
9755e73b 470}
3f6638b8 471
03647350 472#endif // wxUSE_FILEDLG