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