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