1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "filedlggtk.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 // Include setup.h to get wxUSE flags for compilers that do not support precompilation of headers
22 #include "wx/filedlg.h"
27 #include "wx/gtk/private.h"
29 #include <unistd.h> // chdir
32 #include "wx/filename.h" // wxFilename
33 #include "wx/tokenzr.h" // wxStringTokenizer
34 #include "wx/filefn.h" // ::wxGetCwd
35 #include "wx/msgdlg.h" // wxMessageDialog
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern void wxapp_install_idle_handler();
44 //-----------------------------------------------------------------------------
45 // "clicked" for OK-button
46 //-----------------------------------------------------------------------------
49 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
51 int style
= dialog
->GetStyle();
52 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
54 if ((style
& wxSAVE
) && (style
& wxOVERWRITE_PROMPT
))
56 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
61 _("File '%s' already exists, do you really want to overwrite it?"),
62 wxString(wxConvFileName
->cMB2WX(filename
)).c_str());
64 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
65 wxYES_NO
| wxICON_QUESTION
);
66 if (dlg
.ShowModal() != wxID_YES
)
71 // change to the directory where the user went if asked
72 if (style
& wxCHANGE_DIR
)
74 // Use chdir to not care about filename encodings
75 gchar
* folder
= g_path_get_dirname(filename
);
82 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
83 event
.SetEventObject(dialog
);
84 dialog
->GetEventHandler()->ProcessEvent(event
);
88 //-----------------------------------------------------------------------------
89 // "clicked" for Cancel-button
90 //-----------------------------------------------------------------------------
93 static void gtk_filedialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
96 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
97 event
.SetEventObject(dialog
);
98 dialog
->GetEventHandler()->ProcessEvent(event
);
103 static void gtk_filedialog_response_callback(GtkWidget
*w
,
105 wxFileDialog
*dialog
)
107 wxapp_install_idle_handler();
109 if (response
== GTK_RESPONSE_ACCEPT
)
110 gtk_filedialog_ok_callback(w
, dialog
);
111 else if (response
== GTK_RESPONSE_CANCEL
)
112 gtk_filedialog_cancel_callback(w
, dialog
);
115 gtk_filedialog_cancel_callback(w
, dialog
);
116 dialog
->m_destroyed_by_delete
= true;
121 #endif // __WXGTK24__
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
127 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
129 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
130 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
133 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
134 const wxString
& defaultDir
,
135 const wxString
& defaultFileName
,
136 const wxString
& wildCard
,
137 long style
, const wxPoint
& pos
)
138 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
139 wildCard
, style
, pos
, true )
142 if (!gtk_check_version(2,4,0))
144 wxASSERT_MSG( !( (style
& wxSAVE
) && (style
& wxMULTIPLE
) ), wxT("wxFileDialog - wxMULTIPLE used on a save dialog" ) );
145 m_needParent
= false;
146 m_destroyed_by_delete
= false;
148 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
149 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
150 wxDefaultValidator
, wxT("filedialog")))
152 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
156 GtkFileChooserAction gtk_action
;
157 GtkWindow
* gtk_parent
= NULL
;
159 gtk_parent
= GTK_WINDOW(parent
->m_widget
);
162 if ( style
& wxSAVE
)
164 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
165 ok_btn_stock
= GTK_STOCK_SAVE
;
169 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
170 ok_btn_stock
= GTK_STOCK_OPEN
;
173 m_widget
= gtk_file_chooser_dialog_new(
174 wxGTK_CONV(m_message
),
177 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
178 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
181 if ( style
& wxMULTIPLE
)
182 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
184 // local-only property could be set to false to allow non-local files to be loaded.
185 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
186 // and the GtkFileChooserDialog should probably also be created with a backend,
187 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
188 // Currently local-only is kept as the default - true:
189 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
191 g_signal_connect(G_OBJECT(m_widget
), "response",
192 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback
), (gpointer
)this);
194 SetWildcard(wildCard
);
196 if ( style
& wxSAVE
)
198 if ( !defaultDir
.empty() )
199 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
200 wxConvFileName
->cWX2MB(defaultDir
));
202 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
203 wxConvFileName
->cWX2MB(defaultFileName
));
207 if ( !defaultFileName
.empty() )
210 if ( defaultDir
.empty() )
215 gtk_file_chooser_set_filename(
216 GTK_FILE_CHOOSER(m_widget
),
217 wxConvFileName
->cWX2MB( wxFileName(dir
, defaultFileName
).GetFullPath() ) );
219 else if ( !defaultDir
.empty() )
220 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
221 wxConvFileName
->cWX2MB(defaultDir
) );
226 wxGenericFileDialog::Create( parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
);
229 wxFileDialog::~wxFileDialog()
232 if (!gtk_check_version(2,4,0))
234 if (m_destroyed_by_delete
)
240 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
243 if (!gtk_check_version(2,4,0))
244 wxDialog::OnOK( event
);
247 wxGenericFileDialog::OnListOk( event
);
250 int wxFileDialog::ShowModal()
253 if (!gtk_check_version(2,4,0))
254 return wxDialog::ShowModal();
257 return wxGenericFileDialog::ShowModal();
260 bool wxFileDialog::Show( bool show
)
263 if (!gtk_check_version(2,4,0))
264 return wxDialog::Show( show
);
267 return wxGenericFileDialog::Show( show
);
270 wxString
wxFileDialog::GetPath() const
273 if (!gtk_check_version(2,4,0))
274 return wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
277 return wxGenericFileDialog::GetPath();
280 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
283 if (!gtk_check_version(2,4,0))
286 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
288 wxFileName
file(files
[n
]);
289 files
[n
] = file
.GetFullName();
294 wxGenericFileDialog::GetFilenames( files
);
297 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
300 if (!gtk_check_version(2,4,0))
303 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
305 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
306 GSList
*gpaths
= gpathsi
;
309 wxString
file(wxConvFileName
->cMB2WX((gchar
*) gpathsi
->data
));
311 g_free(gpathsi
->data
);
312 gpathsi
= gpathsi
->next
;
315 g_slist_free(gpaths
);
318 paths
.Add(GetPath());
322 wxGenericFileDialog::GetPaths( paths
);
325 void wxFileDialog::SetMessage(const wxString
& message
)
328 if (!gtk_check_version(2,4,0))
335 wxGenericFileDialog::SetMessage( message
);
338 void wxFileDialog::SetPath(const wxString
& path
)
341 if (!gtk_check_version(2,4,0))
343 if (path
.empty()) return;
345 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(path
));
349 wxGenericFileDialog::SetPath( path
);
352 void wxFileDialog::SetDirectory(const wxString
& dir
)
355 if (!gtk_check_version(2,4,0))
357 if (wxDirExists(dir
))
359 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
364 wxGenericFileDialog::SetDirectory( dir
);
367 wxString
wxFileDialog::GetDirectory() const
370 if (!gtk_check_version(2,4,0))
371 return wxConvFileName
->cMB2WX(
372 gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget
) ) );
375 return wxGenericFileDialog::GetDirectory();
378 void wxFileDialog::SetFilename(const wxString
& name
)
381 if (!gtk_check_version(2,4,0))
383 if (GetStyle() & wxSAVE
)
384 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(name
));
386 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
390 wxGenericFileDialog::SetFilename( name
);
393 wxString
wxFileDialog::GetFilename() const
396 if (!gtk_check_version(2,4,0))
398 wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
))) ).GetFullName();
401 return wxGenericFileDialog::GetFilename();
404 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
407 if (!gtk_check_version(2,4,0))
410 wxArrayString wildDescriptions
, wildFilters
;
411 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
413 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
417 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
418 m_wildCard
= wildCard
;
420 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
422 // empty current filter list:
423 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
424 GSList
* filters
= ifilters
;
428 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
429 ifilters
= ifilters
->next
;
431 g_slist_free(filters
);
433 // add parsed to GtkChooser
434 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
436 GtkFileFilter
* filter
= gtk_file_filter_new();
437 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
439 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
440 while (exttok
.HasMoreTokens())
442 wxString token
= exttok
.GetNextToken();
443 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
446 gtk_file_chooser_add_filter(chooser
, filter
);
449 // Reset the filter index
455 wxGenericFileDialog::SetWildcard( wildCard
);
458 void wxFileDialog::SetFilterIndex(int filterIndex
)
461 if (!gtk_check_version(2,4,0))
464 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
465 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
467 filter
= g_slist_nth_data(filters
, filterIndex
);
471 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
475 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
478 g_slist_free(filters
);
482 wxGenericFileDialog::SetFilterIndex( filterIndex
);
485 int wxFileDialog::GetFilterIndex() const
488 if (!gtk_check_version(2,4,0))
490 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
491 GtkFileFilter
*filter
= gtk_file_chooser_get_filter(chooser
);
492 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
493 gint index
= g_slist_index(filters
, filter
);
494 g_slist_free(filters
);
498 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
506 return wxGenericFileDialog::GetFilterIndex();
509 #endif // wxUSE_FILEDLG