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