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