]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/filedlg.cpp
Patch from Hartwig for wxMac implementation
[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
ff3e84ff 13#if wxUSE_FILEDLG && defined(__WXGTK24__)
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
f8bc53eb
JS
25#include <unistd.h> // chdir
26
f8bc53eb
JS
27#include "wx/filename.h" // wxFilename
28#include "wx/tokenzr.h" // wxStringTokenizer
29#include "wx/filefn.h" // ::wxGetCwd
f8bc53eb 30
291a8f20
RR
31//-----------------------------------------------------------------------------
32// "clicked" for OK-button
c801d85f
KB
33//-----------------------------------------------------------------------------
34
865bb325 35extern "C" {
9755e73b 36static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
c801d85f 37{
ff3e84ff 38 int style = dialog->GetWindowStyle();
e808cf8a 39 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
035b704a 40
b5ab6d19
MR
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)
44#endif
ff3e84ff 45 if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
bfc6fde4 46 {
f8bc53eb 47 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
0e1399b3
VZ
48 {
49 wxString msg;
f8bc53eb 50
9755e73b 51 msg.Printf(
f8bc53eb 52 _("File '%s' already exists, do you really want to overwrite it?"),
86501081 53 wxString(filename, *wxConvFileName));
0e1399b3 54
9755e73b
VS
55 wxMessageDialog dlg(dialog, msg, _("Confirm"),
56 wxYES_NO | wxICON_QUESTION);
57 if (dlg.ShowModal() != wxID_YES)
0e1399b3
VZ
58 return;
59 }
83624f79 60 }
035b704a 61
4c84a0dc
RR
62 if (style & wxFD_FILE_MUST_EXIST)
63 {
64 if ( !g_file_test(filename, G_FILE_TEST_EXISTS) )
65 {
66 wxMessageDialog dlg( dialog, _("Please choose an existing file."),
67 _("Error"), wxOK| wxICON_ERROR);
68 dlg.ShowModal();
69 return;
70 }
71 }
72
3f6638b8 73 // change to the directory where the user went if asked
ff3e84ff 74 if (style & wxFD_CHANGE_DIR)
3f6638b8 75 {
f8bc53eb 76 // Use chdir to not care about filename encodings
e808cf8a 77 wxGtkString folder(g_path_get_dirname(filename));
f8bc53eb 78 chdir(folder);
3f6638b8
VZ
79 }
80
bfc6fde4 81 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
9755e73b
VS
82 event.SetEventObject(dialog);
83 dialog->GetEventHandler()->ProcessEvent(event);
ff7b1510 84}
865bb325 85}
c801d85f 86
291a8f20
RR
87//-----------------------------------------------------------------------------
88// "clicked" for Cancel-button
89//-----------------------------------------------------------------------------
90
9f057af5
VZ
91extern "C"
92{
93
a552d120 94static void gtk_filedialog_cancel_callback(GtkWidget *w, wxFileDialog *dialog)
c801d85f 95{
bfc6fde4 96 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
9755e73b
VS
97 event.SetEventObject(dialog);
98 dialog->GetEventHandler()->ProcessEvent(event);
99}
100
101static void gtk_filedialog_response_callback(GtkWidget *w,
f8bc53eb 102 gint response,
9755e73b
VS
103 wxFileDialog *dialog)
104{
a7334928 105 if (response == GTK_RESPONSE_ACCEPT)
9755e73b 106 gtk_filedialog_ok_callback(w, dialog);
a552d120 107 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
c2740a5a 108 gtk_filedialog_cancel_callback(w, dialog);
ff7b1510 109}
9f057af5
VZ
110
111static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser,
112 gpointer user_data)
113{
114#if GTK_CHECK_VERSION(2,4,0)
115 GtkWidget *preview = GTK_WIDGET(user_data);
3ab296d9 116
e808cf8a 117 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
3ab296d9 118
9f057af5
VZ
119 if ( !filename )
120 return;
121
122 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL);
123 gboolean have_preview = pixbuf != NULL;
124
125 gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf);
126 if ( pixbuf )
127 g_object_unref (pixbuf);
128
129 gtk_file_chooser_set_preview_widget_active(chooser, have_preview);
130#else
131 wxUnusedVar(chooser);
132 wxUnusedVar(user_data);
133#endif // GTK+ 2.4+
865bb325
VZ
134}
135
9f057af5
VZ
136} // extern "C"
137
c801d85f 138
291a8f20
RR
139//-----------------------------------------------------------------------------
140// wxFileDialog
141//-----------------------------------------------------------------------------
142
4e1901b7
RR
143IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog)
144
145BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog)
f8bc53eb 146 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
4e1901b7 147END_EVENT_TABLE()
c801d85f 148
9755e73b
VS
149wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
150 const wxString& defaultDir,
151 const wxString& defaultFileName,
152 const wxString& wildCard,
ff3e84ff
VZ
153 long style, const wxPoint& pos,
154 const wxSize& sz,
155 const wxString& name)
4e1901b7 156 : wxGenericFileDialog(parent, message, defaultDir, defaultFileName,
ff3e84ff 157 wildCard, style, pos, sz, name, true )
c801d85f 158{
9f057af5 159 if (gtk_check_version(2,4,0))
77f70672 160 {
9f057af5
VZ
161 wxGenericFileDialog::Create( parent, message, defaultDir,
162 defaultFileName, wildCard, style, pos );
163 return;
164 }
83624f79 165
2229243b
VZ
166 parent = GetParentForModalDialog(parent);
167
9f057af5
VZ
168 if (!PreCreation(parent, pos, wxDefaultSize) ||
169 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
170 wxDefaultValidator, wxT("filedialog")))
171 {
172 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
173 return;
174 }
3f6638b8 175
9f057af5
VZ
176 GtkFileChooserAction gtk_action;
177 GtkWindow* gtk_parent = NULL;
178 if (parent)
179 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
27b2dd53 180
9f057af5
VZ
181 const gchar* ok_btn_stock;
182 if ( style & wxFD_SAVE )
183 {
184 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
185 ok_btn_stock = GTK_STOCK_SAVE;
186 }
187 else
188 {
189 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
190 ok_btn_stock = GTK_STOCK_OPEN;
191 }
f8bc53eb 192
9f057af5
VZ
193 m_widget = gtk_file_chooser_dialog_new(
194 wxGTK_CONV(m_message),
195 gtk_parent,
196 gtk_action,
197 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
198 ok_btn_stock, GTK_RESPONSE_ACCEPT,
199 NULL);
9755e73b 200
9f057af5 201 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
c1dfe97c 202
9f057af5
VZ
203 if ( style & wxFD_MULTIPLE )
204 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true);
27b2dd53 205
bf345206
VZ
206 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
207 // destroys the dialog when the user press ESC on the dialog: in that case
208 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
209 // errors...
9f057af5
VZ
210 g_signal_connect (G_OBJECT(m_widget),
211 "delete_event",
212 G_CALLBACK (gtk_widget_hide_on_delete),
213 (gpointer)this);
a552d120 214
bf345206
VZ
215 // local-only property could be set to false to allow non-local files to be
216 // loaded. In that case get/set_uri(s) should be used instead of
217 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
218 // probably also be created with a backend, e.g "gnome-vfs", "default", ...
219 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
220 // as the default - true:
9f057af5 221 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
27b2dd53 222
9f057af5
VZ
223 g_signal_connect (m_widget, "response",
224 G_CALLBACK (gtk_filedialog_response_callback), this);
27b2dd53 225
9f057af5 226 SetWildcard(wildCard);
f8bc53eb 227
bf345206
VZ
228 // if defaultDir is specified it should contain the directory and
229 // defaultFileName should contain the default name of the file, however if
230 // directory is not given, defaultFileName contains both
231 wxFileName fn;
232 if ( defaultDir.empty() )
233 fn.Assign(defaultFileName);
234 else if ( !defaultFileName.empty() )
235 fn.Assign(defaultDir, defaultFileName);
f241631e
RD
236 else
237 fn.AssignDir(defaultDir);
bf345206
VZ
238
239 // set the initial file name and/or directory
78355ffc 240 fn.MakeAbsolute(); // GTK+ needs absolute path
3a41827a
VZ
241 const wxString dir = fn.GetPath();
242 if ( !dir.empty() )
9f057af5 243 {
3a41827a
VZ
244 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
245 dir.fn_str());
bf345206 246 }
f8bc53eb 247
3a41827a 248 const wxString fname = fn.GetFullName();
bf345206
VZ
249 if ( style & wxFD_SAVE )
250 {
251 if ( !fname.empty() )
252 {
253 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
254 fname.fn_str());
255 }
b5ab6d19
MR
256
257#if GTK_CHECK_VERSION(2,7,3)
9f057af5
VZ
258 if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3))
259 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget), TRUE);
b5ab6d19 260#endif
9f057af5
VZ
261 }
262 else // wxFD_OPEN
263 {
bf345206 264 if ( !fname.empty() )
f8bc53eb 265 {
bf345206
VZ
266 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
267 fn.GetFullPath().fn_str());
f8bc53eb 268 }
77f70672 269 }
9f057af5
VZ
270
271#if GTK_CHECK_VERSION(2,4,0)
272 if ( style & wxFD_PREVIEW )
273 {
274 GtkWidget *previewImage = gtk_image_new();
275
276 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget),
277 previewImage);
278 g_signal_connect(m_widget, "update-preview",
279 G_CALLBACK(gtk_filedialog_update_preview_callback),
280 previewImage);
281 }
282#endif // GTK+ 2.4+
9755e73b 283}
0e1399b3 284
4e1901b7
RR
285void wxFileDialog::OnFakeOk( wxCommandEvent &event )
286{
77f70672 287 if (!gtk_check_version(2,4,0))
86508681 288 EndDialog(wxID_OK);
77f70672 289 else
77f70672 290 wxGenericFileDialog::OnListOk( event );
4e1901b7
RR
291}
292
293int wxFileDialog::ShowModal()
294{
77f70672
RR
295 if (!gtk_check_version(2,4,0))
296 return wxDialog::ShowModal();
297 else
77f70672 298 return wxGenericFileDialog::ShowModal();
4e1901b7
RR
299}
300
301bool wxFileDialog::Show( bool show )
302{
77f70672
RR
303 if (!gtk_check_version(2,4,0))
304 return wxDialog::Show( show );
305 else
77f70672 306 return wxGenericFileDialog::Show( show );
9755e73b 307}
0e1399b3 308
5b2e23bf
RR
309void wxFileDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags )
310{
311 if (!m_wxwindow)
312 return;
313 else
314 wxGenericFileDialog::DoSetSize( x, y, width, height, sizeFlags );
315}
316
f8bc53eb
JS
317wxString wxFileDialog::GetPath() const
318{
f8bc53eb 319 if (!gtk_check_version(2,4,0))
3ab296d9 320 {
e808cf8a 321 wxGtkString str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget)));
86501081 322 return wxString(str, *wxConvFileName);
3ab296d9 323 }
e808cf8a
PC
324
325 return wxGenericFileDialog::GetPath();
f8bc53eb
JS
326}
327
27b2dd53 328void wxFileDialog::GetFilenames(wxArrayString& files) const
9755e73b 329{
77f70672 330 if (!gtk_check_version(2,4,0))
9755e73b 331 {
77f70672 332 GetPaths(files);
f8bc53eb 333 for (size_t n = 0; n < files.GetCount(); ++n )
9755e73b 334 {
f8bc53eb
JS
335 wxFileName file(files[n]);
336 files[n] = file.GetFullName();
9755e73b 337 }
9755e73b 338 }
77f70672 339 else
77f70672 340 wxGenericFileDialog::GetFilenames( files );
9755e73b 341}
76840ed0 342
27b2dd53 343void wxFileDialog::GetPaths(wxArrayString& paths) const
9755e73b 344{
77f70672 345 if (!gtk_check_version(2,4,0))
9755e73b 346 {
27b2dd53 347 paths.Empty();
f8bc53eb 348 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget)))
9755e73b 349 {
f8bc53eb 350 GSList *gpathsi = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget));
77f70672
RR
351 GSList *gpaths = gpathsi;
352 while (gpathsi)
353 {
86501081 354 wxString file((gchar*) gpathsi->data, *wxConvFileName);
f8bc53eb 355 paths.Add(file);
77f70672
RR
356 g_free(gpathsi->data);
357 gpathsi = gpathsi->next;
358 }
0832aaaf 359
f8bc53eb 360 g_slist_free(gpaths);
9755e73b 361 }
f8bc53eb
JS
362 else
363 paths.Add(GetPath());
9755e73b
VS
364 }
365 else
77f70672 366 wxGenericFileDialog::GetPaths( paths );
9755e73b 367}
035b704a 368
9755e73b
VS
369void wxFileDialog::SetMessage(const wxString& message)
370{
77f70672
RR
371 if (!gtk_check_version(2,4,0))
372 {
373 m_message = message;
374 SetTitle(message);
375 }
376 else
77f70672 377 wxGenericFileDialog::SetMessage( message );
9755e73b
VS
378}
379
76840ed0
RR
380void wxFileDialog::SetPath(const wxString& path)
381{
77f70672
RR
382 if (!gtk_check_version(2,4,0))
383 {
384 if (path.empty()) return;
385
86501081 386 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), path.fn_str());
77f70672
RR
387 }
388 else
77f70672 389 wxGenericFileDialog::SetPath( path );
76840ed0
RR
390}
391
9755e73b
VS
392void wxFileDialog::SetDirectory(const wxString& dir)
393{
77f70672 394 if (!gtk_check_version(2,4,0))
76840ed0 395 {
da865fdd 396 if (wxDirExists(dir))
77f70672 397 {
86501081 398 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), dir.fn_str());
77f70672 399 }
76840ed0 400 }
77f70672 401 else
77f70672 402 wxGenericFileDialog::SetDirectory( dir );
9755e73b
VS
403}
404
f8bc53eb
JS
405wxString wxFileDialog::GetDirectory() const
406{
f8bc53eb 407 if (!gtk_check_version(2,4,0))
3ab296d9 408 {
e808cf8a 409 wxGtkString str(gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(m_widget)));
86501081 410 return wxString(str, *wxConvFileName);
3ab296d9 411 }
e808cf8a
PC
412
413 return wxGenericFileDialog::GetDirectory();
f8bc53eb
JS
414}
415
9755e73b
VS
416void wxFileDialog::SetFilename(const wxString& name)
417{
77f70672
RR
418 if (!gtk_check_version(2,4,0))
419 {
b014db05 420 if (HasFdFlag(wxFD_SAVE))
f0f1afb8 421 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
f8bc53eb
JS
422 else
423 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
77f70672
RR
424 }
425 else
77f70672 426 wxGenericFileDialog::SetFilename( name );
9755e73b 427}
035b704a 428
f8bc53eb
JS
429wxString wxFileDialog::GetFilename() const
430{
f8bc53eb 431 if (!gtk_check_version(2,4,0))
3ab296d9 432 return wxFileName(GetPath()).GetFullName();
f8bc53eb 433 else
f8bc53eb
JS
434 return wxGenericFileDialog::GetFilename();
435}
436
9755e73b
VS
437void wxFileDialog::SetWildcard(const wxString& wildCard)
438{
77f70672 439 if (!gtk_check_version(2,4,0))
9755e73b 440 {
77f70672
RR
441 // parse filters
442 wxArrayString wildDescriptions, wildFilters;
f8bc53eb 443 if (!wxParseCommonDialogsFilter(wildCard, wildDescriptions, wildFilters))
77f70672 444 {
f8bc53eb 445 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
77f70672
RR
446 }
447 else
9755e73b 448 {
f8bc53eb
JS
449 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
450 m_wildCard = wildCard;
451
452 GtkFileChooser* chooser = GTK_FILE_CHOOSER(m_widget);
453
454 // empty current filter list:
455 GSList* ifilters = gtk_file_chooser_list_filters(chooser);
456 GSList* filters = ifilters;
457
458 while (ifilters)
459 {
460 gtk_file_chooser_remove_filter(chooser,GTK_FILE_FILTER(ifilters->data));
461 ifilters = ifilters->next;
462 }
463 g_slist_free(filters);
464
77f70672 465 // add parsed to GtkChooser
f8bc53eb 466 for (size_t n = 0; n < wildFilters.GetCount(); ++n)
9755e73b 467 {
77f70672 468 GtkFileFilter* filter = gtk_file_filter_new();
f8bc53eb
JS
469 gtk_file_filter_set_name(filter, wxGTK_CONV(wildDescriptions[n]));
470
471 wxStringTokenizer exttok(wildFilters[n], wxT(";"));
472 while (exttok.HasMoreTokens())
77f70672 473 {
f8bc53eb
JS
474 wxString token = exttok.GetNextToken();
475 gtk_file_filter_add_pattern(filter, wxGTK_CONV(token));
77f70672 476 }
27b2dd53 477
77f70672
RR
478 gtk_file_chooser_add_filter(chooser, filter);
479 }
f8bc53eb
JS
480
481 // Reset the filter index
482 SetFilterIndex(0);
9755e73b
VS
483 }
484 }
77f70672 485 else
77f70672 486 wxGenericFileDialog::SetWildcard( wildCard );
9755e73b 487}
a3622daa 488
9755e73b
VS
489void wxFileDialog::SetFilterIndex(int filterIndex)
490{
ff3e84ff 491
77f70672 492 if (!gtk_check_version(2,4,0))
9755e73b 493 {
f8bc53eb 494 gpointer filter;
77f70672 495 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
f8bc53eb
JS
496 GSList *filters = gtk_file_chooser_list_filters(chooser);
497
498 filter = g_slist_nth_data(filters, filterIndex);
499
500 if (filter != NULL)
9755e73b 501 {
f8bc53eb
JS
502 gtk_file_chooser_set_filter(chooser, GTK_FILE_FILTER(filter));
503 }
504 else
505 {
506 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
9755e73b 507 }
f8bc53eb 508
77f70672 509 g_slist_free(filters);
9755e73b 510 }
77f70672 511 else
77f70672 512 wxGenericFileDialog::SetFilterIndex( filterIndex );
4e1901b7
RR
513}
514
f8bc53eb 515int wxFileDialog::GetFilterIndex() const
4e1901b7 516{
f8bc53eb 517 if (!gtk_check_version(2,4,0))
4e1901b7 518 {
f8bc53eb
JS
519 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
520 GtkFileFilter *filter = gtk_file_chooser_get_filter(chooser);
521 GSList *filters = gtk_file_chooser_list_filters(chooser);
522 gint index = g_slist_index(filters, filter);
523 g_slist_free(filters);
3502e687 524
f8bc53eb 525 if (index == -1)
9755e73b 526 {
f8bc53eb
JS
527 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
528 return 0;
9755e73b 529 }
f8bc53eb
JS
530 else
531 return index;
9755e73b 532 }
f8bc53eb 533 else
f8bc53eb 534 return wxGenericFileDialog::GetFilterIndex();
9755e73b 535}
3f6638b8 536
ff3e84ff 537#endif // wxUSE_FILEDLG && __WXGTK24__