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