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