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