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