1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/filedlg.cpp
3 // Purpose: native implementation of wxFileDialog
4 // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/filedlg.h"
20 #include "wx/gtk/private.h"
22 #include <unistd.h> // chdir
25 #include "wx/filename.h" // wxFilename
26 #include "wx/tokenzr.h" // wxStringTokenizer
27 #include "wx/filefn.h" // ::wxGetCwd
28 #include "wx/msgdlg.h" // wxMessageDialog
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern void wxapp_install_idle_handler();
36 //-----------------------------------------------------------------------------
37 // "clicked" for OK-button
38 //-----------------------------------------------------------------------------
41 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
43 int style
= dialog
->GetStyle();
44 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
46 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
47 #if GTK_CHECK_VERSION(2,7,3)
48 if(gtk_check_version(2,7,3) != NULL
)
50 if ((style
& wxSAVE
) && (style
& wxOVERWRITE_PROMPT
))
52 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
57 _("File '%s' already exists, do you really want to overwrite it?"),
58 wxString(wxConvFileName
->cMB2WX(filename
)).c_str());
60 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
61 wxYES_NO
| wxICON_QUESTION
);
62 if (dlg
.ShowModal() != wxID_YES
)
67 // change to the directory where the user went if asked
68 if (style
& wxCHANGE_DIR
)
70 // Use chdir to not care about filename encodings
71 gchar
* folder
= g_path_get_dirname(filename
);
78 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
79 event
.SetEventObject(dialog
);
80 dialog
->GetEventHandler()->ProcessEvent(event
);
84 //-----------------------------------------------------------------------------
85 // "clicked" for Cancel-button
86 //-----------------------------------------------------------------------------
89 static void gtk_filedialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
92 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
93 event
.SetEventObject(dialog
);
94 dialog
->GetEventHandler()->ProcessEvent(event
);
99 static void gtk_filedialog_response_callback(GtkWidget
*w
,
101 wxFileDialog
*dialog
)
103 wxapp_install_idle_handler();
105 if (response
== GTK_RESPONSE_ACCEPT
)
106 gtk_filedialog_ok_callback(w
, dialog
);
107 else if (response
== GTK_RESPONSE_CANCEL
)
108 gtk_filedialog_cancel_callback(w
, dialog
);
111 gtk_filedialog_cancel_callback(w
, dialog
);
112 dialog
->m_destroyed_by_delete
= true;
117 #endif // __WXGTK24__
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
123 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
125 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
126 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
129 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
130 const wxString
& defaultDir
,
131 const wxString
& defaultFileName
,
132 const wxString
& wildCard
,
133 long style
, const wxPoint
& pos
)
134 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
135 wildCard
, style
, pos
, true )
138 if (!gtk_check_version(2,4,0))
140 wxASSERT_MSG( !( (style
& wxSAVE
) && (style
& wxMULTIPLE
) ), wxT("wxFileDialog - wxMULTIPLE used on a save dialog" ) );
141 m_needParent
= false;
142 m_destroyed_by_delete
= false;
144 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
145 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
146 wxDefaultValidator
, wxT("filedialog")))
148 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
152 GtkFileChooserAction gtk_action
;
153 GtkWindow
* gtk_parent
= NULL
;
155 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
157 const gchar
* ok_btn_stock
;
158 if ( style
& wxSAVE
)
160 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
161 ok_btn_stock
= GTK_STOCK_SAVE
;
165 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
166 ok_btn_stock
= GTK_STOCK_OPEN
;
169 m_widget
= gtk_file_chooser_dialog_new(
170 wxGTK_CONV(m_message
),
173 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
174 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
177 if ( style
& wxMULTIPLE
)
178 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
180 // local-only property could be set to false to allow non-local files to be loaded.
181 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
182 // and the GtkFileChooserDialog should probably also be created with a backend,
183 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
184 // Currently local-only is kept as the default - true:
185 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
187 g_signal_connect(G_OBJECT(m_widget
), "response",
188 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback
), (gpointer
)this);
190 SetWildcard(wildCard
);
192 if ( style
& wxSAVE
)
194 if ( !defaultDir
.empty() )
195 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
196 wxConvFileName
->cWX2MB(defaultDir
));
198 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
199 wxConvFileName
->cWX2MB(defaultFileName
));
201 #if GTK_CHECK_VERSION(2,7,3)
202 if (!gtk_check_version(2,7,3))
203 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget
), TRUE
);
208 if ( !defaultFileName
.empty() )
211 if ( defaultDir
.empty() )
216 gtk_file_chooser_set_filename(
217 GTK_FILE_CHOOSER(m_widget
),
218 wxConvFileName
->cWX2MB( wxFileName(dir
, defaultFileName
).GetFullPath() ) );
220 else if ( !defaultDir
.empty() )
221 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
222 wxConvFileName
->cWX2MB(defaultDir
) );
227 wxGenericFileDialog::Create( parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
);
230 wxFileDialog::~wxFileDialog()
233 if (!gtk_check_version(2,4,0))
235 if (m_destroyed_by_delete
)
241 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
244 if (!gtk_check_version(2,4,0))
245 wxDialog::OnOK( event
);
248 wxGenericFileDialog::OnListOk( event
);
251 int wxFileDialog::ShowModal()
254 if (!gtk_check_version(2,4,0))
255 return wxDialog::ShowModal();
258 return wxGenericFileDialog::ShowModal();
261 bool wxFileDialog::Show( bool show
)
264 if (!gtk_check_version(2,4,0))
265 return wxDialog::Show( show
);
268 return wxGenericFileDialog::Show( show
);
271 void wxFileDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
276 wxGenericFileDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
279 wxString
wxFileDialog::GetPath() const
282 if (!gtk_check_version(2,4,0))
283 return wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
286 return wxGenericFileDialog::GetPath();
289 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
292 if (!gtk_check_version(2,4,0))
295 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
297 wxFileName
file(files
[n
]);
298 files
[n
] = file
.GetFullName();
303 wxGenericFileDialog::GetFilenames( files
);
306 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
309 if (!gtk_check_version(2,4,0))
312 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
314 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
315 GSList
*gpaths
= gpathsi
;
318 wxString
file(wxConvFileName
->cMB2WX((gchar
*) gpathsi
->data
));
320 g_free(gpathsi
->data
);
321 gpathsi
= gpathsi
->next
;
324 g_slist_free(gpaths
);
327 paths
.Add(GetPath());
331 wxGenericFileDialog::GetPaths( paths
);
334 void wxFileDialog::SetMessage(const wxString
& message
)
337 if (!gtk_check_version(2,4,0))
344 wxGenericFileDialog::SetMessage( message
);
347 void wxFileDialog::SetPath(const wxString
& path
)
350 if (!gtk_check_version(2,4,0))
352 if (path
.empty()) return;
354 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(path
));
358 wxGenericFileDialog::SetPath( path
);
361 void wxFileDialog::SetDirectory(const wxString
& dir
)
364 if (!gtk_check_version(2,4,0))
366 if (wxDirExists(dir
))
368 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
373 wxGenericFileDialog::SetDirectory( dir
);
376 wxString
wxFileDialog::GetDirectory() const
379 if (!gtk_check_version(2,4,0))
380 return wxConvFileName
->cMB2WX(
381 gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget
) ) );
384 return wxGenericFileDialog::GetDirectory();
387 void wxFileDialog::SetFilename(const wxString
& name
)
390 if (!gtk_check_version(2,4,0))
392 if (GetStyle() & wxSAVE
)
393 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(name
));
395 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
399 wxGenericFileDialog::SetFilename( name
);
402 wxString
wxFileDialog::GetFilename() const
405 if (!gtk_check_version(2,4,0))
407 wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
))) ).GetFullName();
410 return wxGenericFileDialog::GetFilename();
413 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
416 if (!gtk_check_version(2,4,0))
419 wxArrayString wildDescriptions
, wildFilters
;
420 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
422 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
426 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
427 m_wildCard
= wildCard
;
429 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
431 // empty current filter list:
432 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
433 GSList
* filters
= ifilters
;
437 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
438 ifilters
= ifilters
->next
;
440 g_slist_free(filters
);
442 // add parsed to GtkChooser
443 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
445 GtkFileFilter
* filter
= gtk_file_filter_new();
446 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
448 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
449 while (exttok
.HasMoreTokens())
451 wxString token
= exttok
.GetNextToken();
452 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
455 gtk_file_chooser_add_filter(chooser
, filter
);
458 // Reset the filter index
464 wxGenericFileDialog::SetWildcard( wildCard
);
467 void wxFileDialog::SetFilterIndex(int filterIndex
)
470 if (!gtk_check_version(2,4,0))
473 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
474 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
476 filter
= g_slist_nth_data(filters
, filterIndex
);
480 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
484 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
487 g_slist_free(filters
);
491 wxGenericFileDialog::SetFilterIndex( filterIndex
);
494 int wxFileDialog::GetFilterIndex() const
497 if (!gtk_check_version(2,4,0))
499 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
500 GtkFileFilter
*filter
= gtk_file_chooser_get_filter(chooser
);
501 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
502 gint index
= g_slist_index(filters
, filter
);
503 g_slist_free(filters
);
507 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
515 return wxGenericFileDialog::GetFilterIndex();
518 #endif // wxUSE_FILEDLG