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"
13 #if wxUSE_FILEDLG && defined(__WXGTK24__)
15 #include "wx/filedlg.h"
19 #include "wx/msgdlg.h"
23 #include "wx/gtk/private.h"
25 #include <unistd.h> // chdir
27 #include "wx/filename.h" // wxFilename
28 #include "wx/tokenzr.h" // wxStringTokenizer
29 #include "wx/filefn.h" // ::wxGetCwd
31 //-----------------------------------------------------------------------------
32 // "clicked" for OK-button
33 //-----------------------------------------------------------------------------
36 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
38 int style
= dialog
->GetWindowStyle();
39 wxGtkString
filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
)));
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
)
45 if ((style
& wxFD_SAVE
) && (style
& wxFD_OVERWRITE_PROMPT
))
47 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
52 _("File '%s' already exists, do you really want to overwrite it?"),
53 wxString(wxConvFileName
->cMB2WX(filename
)).c_str());
55 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
56 wxYES_NO
| wxICON_QUESTION
);
57 if (dlg
.ShowModal() != wxID_YES
)
62 // change to the directory where the user went if asked
63 if (style
& wxFD_CHANGE_DIR
)
65 // Use chdir to not care about filename encodings
66 wxGtkString
folder(g_path_get_dirname(filename
));
70 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
71 event
.SetEventObject(dialog
);
72 dialog
->GetEventHandler()->ProcessEvent(event
);
76 //-----------------------------------------------------------------------------
77 // "clicked" for Cancel-button
78 //-----------------------------------------------------------------------------
83 static void gtk_filedialog_cancel_callback(GtkWidget
*w
, wxFileDialog
*dialog
)
85 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
86 event
.SetEventObject(dialog
);
87 dialog
->GetEventHandler()->ProcessEvent(event
);
90 static void gtk_filedialog_response_callback(GtkWidget
*w
,
94 if (response
== GTK_RESPONSE_ACCEPT
)
95 gtk_filedialog_ok_callback(w
, dialog
);
96 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
97 gtk_filedialog_cancel_callback(w
, dialog
);
100 static void gtk_filedialog_update_preview_callback(GtkFileChooser
*chooser
,
103 #if GTK_CHECK_VERSION(2,4,0)
104 GtkWidget
*preview
= GTK_WIDGET(user_data
);
106 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
111 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
112 gboolean have_preview
= pixbuf
!= NULL
;
114 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
116 g_object_unref (pixbuf
);
118 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
120 wxUnusedVar(chooser
);
121 wxUnusedVar(user_data
);
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
132 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
134 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
135 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
138 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
139 const wxString
& defaultDir
,
140 const wxString
& defaultFileName
,
141 const wxString
& wildCard
,
142 long style
, const wxPoint
& pos
,
144 const wxString
& name
)
145 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
146 wildCard
, style
, pos
, sz
, name
, true )
148 if (gtk_check_version(2,4,0))
150 wxGenericFileDialog::Create( parent
, message
, defaultDir
,
151 defaultFileName
, wildCard
, style
, pos
);
155 m_needParent
= false;
157 parent
= GetParentForModalDialog(parent
);
159 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
160 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
161 wxDefaultValidator
, wxT("filedialog")))
163 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
167 GtkFileChooserAction gtk_action
;
168 GtkWindow
* gtk_parent
= NULL
;
170 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
172 const gchar
* ok_btn_stock
;
173 if ( style
& wxFD_SAVE
)
175 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
176 ok_btn_stock
= GTK_STOCK_SAVE
;
180 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
181 ok_btn_stock
= GTK_STOCK_OPEN
;
184 m_widget
= gtk_file_chooser_dialog_new(
185 wxGTK_CONV(m_message
),
188 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
189 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
192 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
194 if ( style
& wxFD_MULTIPLE
)
195 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
197 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
198 // destroys the dialog when the user press ESC on the dialog: in that case
199 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
201 g_signal_connect (G_OBJECT(m_widget
),
203 G_CALLBACK (gtk_widget_hide_on_delete
),
206 // local-only property could be set to false to allow non-local files to be
207 // loaded. In that case get/set_uri(s) should be used instead of
208 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
209 // probably also be created with a backend, e.g "gnome-vfs", "default", ...
210 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
211 // as the default - true:
212 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
214 g_signal_connect (m_widget
, "response",
215 G_CALLBACK (gtk_filedialog_response_callback
), this);
217 SetWildcard(wildCard
);
219 // if defaultDir is specified it should contain the directory and
220 // defaultFileName should contain the default name of the file, however if
221 // directory is not given, defaultFileName contains both
223 if ( defaultDir
.empty() )
224 fn
.Assign(defaultFileName
);
225 else if ( !defaultFileName
.empty() )
226 fn
.Assign(defaultDir
, defaultFileName
);
228 fn
.AssignDir(defaultDir
);
230 // set the initial file name and/or directory
231 const wxString dir
= fn
.GetPath();
234 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
238 const wxString fname
= fn
.GetFullName();
239 if ( style
& wxFD_SAVE
)
241 if ( !fname
.empty() )
243 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
247 #if GTK_CHECK_VERSION(2,7,3)
248 if ((style
& wxFD_OVERWRITE_PROMPT
) && !gtk_check_version(2,7,3))
249 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget
), TRUE
);
254 if ( !fname
.empty() )
256 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
257 fn
.GetFullPath().fn_str());
261 #if GTK_CHECK_VERSION(2,4,0)
262 if ( style
& wxFD_PREVIEW
)
264 GtkWidget
*previewImage
= gtk_image_new();
266 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget
),
268 g_signal_connect(m_widget
, "update-preview",
269 G_CALLBACK(gtk_filedialog_update_preview_callback
),
275 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
277 if (!gtk_check_version(2,4,0))
280 wxGenericFileDialog::OnListOk( event
);
283 int wxFileDialog::ShowModal()
285 if (!gtk_check_version(2,4,0))
286 return wxDialog::ShowModal();
288 return wxGenericFileDialog::ShowModal();
291 bool wxFileDialog::Show( bool show
)
293 if (!gtk_check_version(2,4,0))
294 return wxDialog::Show( show
);
296 return wxGenericFileDialog::Show( show
);
299 void wxFileDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
304 wxGenericFileDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
307 wxString
wxFileDialog::GetPath() const
309 if (!gtk_check_version(2,4,0))
311 wxGtkString
str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
312 return wxConvFileName
->cMB2WX(str
);
315 return wxGenericFileDialog::GetPath();
318 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
320 if (!gtk_check_version(2,4,0))
323 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
325 wxFileName
file(files
[n
]);
326 files
[n
] = file
.GetFullName();
330 wxGenericFileDialog::GetFilenames( files
);
333 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
335 if (!gtk_check_version(2,4,0))
338 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
340 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
341 GSList
*gpaths
= gpathsi
;
344 wxString
file(wxConvFileName
->cMB2WX((gchar
*) gpathsi
->data
));
346 g_free(gpathsi
->data
);
347 gpathsi
= gpathsi
->next
;
350 g_slist_free(gpaths
);
353 paths
.Add(GetPath());
356 wxGenericFileDialog::GetPaths( paths
);
359 void wxFileDialog::SetMessage(const wxString
& message
)
361 if (!gtk_check_version(2,4,0))
367 wxGenericFileDialog::SetMessage( message
);
370 void wxFileDialog::SetPath(const wxString
& path
)
372 if (!gtk_check_version(2,4,0))
374 if (path
.empty()) return;
376 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(path
));
379 wxGenericFileDialog::SetPath( path
);
382 void wxFileDialog::SetDirectory(const wxString
& dir
)
384 if (!gtk_check_version(2,4,0))
386 if (wxDirExists(dir
))
388 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
392 wxGenericFileDialog::SetDirectory( dir
);
395 wxString
wxFileDialog::GetDirectory() const
397 if (!gtk_check_version(2,4,0))
399 wxGtkString
str(gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(m_widget
)));
400 return wxConvFileName
->cMB2WX(str
);
403 return wxGenericFileDialog::GetDirectory();
406 void wxFileDialog::SetFilename(const wxString
& name
)
408 if (!gtk_check_version(2,4,0))
410 if (HasFdFlag(wxFD_SAVE
))
411 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
413 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
416 wxGenericFileDialog::SetFilename( name
);
419 wxString
wxFileDialog::GetFilename() const
421 if (!gtk_check_version(2,4,0))
422 return wxFileName(GetPath()).GetFullName();
424 return wxGenericFileDialog::GetFilename();
427 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
429 if (!gtk_check_version(2,4,0))
432 wxArrayString wildDescriptions
, wildFilters
;
433 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
435 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
439 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
440 m_wildCard
= wildCard
;
442 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
444 // empty current filter list:
445 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
446 GSList
* filters
= ifilters
;
450 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
451 ifilters
= ifilters
->next
;
453 g_slist_free(filters
);
455 // add parsed to GtkChooser
456 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
458 GtkFileFilter
* filter
= gtk_file_filter_new();
459 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
461 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
462 while (exttok
.HasMoreTokens())
464 wxString token
= exttok
.GetNextToken();
465 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
468 gtk_file_chooser_add_filter(chooser
, filter
);
471 // Reset the filter index
476 wxGenericFileDialog::SetWildcard( wildCard
);
479 void wxFileDialog::SetFilterIndex(int filterIndex
)
482 if (!gtk_check_version(2,4,0))
485 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
486 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
488 filter
= g_slist_nth_data(filters
, filterIndex
);
492 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
496 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
499 g_slist_free(filters
);
502 wxGenericFileDialog::SetFilterIndex( filterIndex
);
505 int wxFileDialog::GetFilterIndex() const
507 if (!gtk_check_version(2,4,0))
509 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
510 GtkFileFilter
*filter
= gtk_file_chooser_get_filter(chooser
);
511 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
512 gint index
= g_slist_index(filters
, filter
);
513 g_slist_free(filters
);
517 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
524 return wxGenericFileDialog::GetFilterIndex();
527 #endif // wxUSE_FILEDLG && __WXGTK24__