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(filename
, *wxConvFileName
));
55 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
56 wxYES_NO
| wxICON_QUESTION
);
57 if (dlg
.ShowModal() != wxID_YES
)
62 if (style
& wxFD_FILE_MUST_EXIST
)
64 if ( !g_file_test(filename
, G_FILE_TEST_EXISTS
) )
66 wxMessageDialog
dlg( dialog
, _("Please choose an existing file."),
67 _("Error"), wxOK
| wxICON_ERROR
);
73 // change to the directory where the user went if asked
74 if (style
& wxFD_CHANGE_DIR
)
76 // Use chdir to not care about filename encodings
77 wxGtkString
folder(g_path_get_dirname(filename
));
81 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
82 event
.SetEventObject(dialog
);
83 dialog
->GetEventHandler()->ProcessEvent(event
);
87 //-----------------------------------------------------------------------------
88 // "clicked" for Cancel-button
89 //-----------------------------------------------------------------------------
94 static void gtk_filedialog_cancel_callback(GtkWidget
*w
, wxFileDialog
*dialog
)
96 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
97 event
.SetEventObject(dialog
);
98 dialog
->GetEventHandler()->ProcessEvent(event
);
101 static void gtk_filedialog_response_callback(GtkWidget
*w
,
103 wxFileDialog
*dialog
)
105 if (response
== GTK_RESPONSE_ACCEPT
)
106 gtk_filedialog_ok_callback(w
, dialog
);
107 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
108 gtk_filedialog_cancel_callback(w
, dialog
);
111 static void gtk_filedialog_update_preview_callback(GtkFileChooser
*chooser
,
114 #if GTK_CHECK_VERSION(2,4,0)
115 GtkWidget
*preview
= GTK_WIDGET(user_data
);
117 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
122 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
123 gboolean have_preview
= pixbuf
!= NULL
;
125 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
127 g_object_unref (pixbuf
);
129 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
131 wxUnusedVar(chooser
);
132 wxUnusedVar(user_data
);
139 //-----------------------------------------------------------------------------
141 //-----------------------------------------------------------------------------
143 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
145 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
146 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
149 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
150 const wxString
& defaultDir
,
151 const wxString
& defaultFileName
,
152 const wxString
& wildCard
,
153 long style
, const wxPoint
& pos
,
155 const wxString
& name
)
156 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
157 wildCard
, style
, pos
, sz
, name
, true )
159 if (gtk_check_version(2,4,0))
161 wxGenericFileDialog::Create( parent
, message
, defaultDir
,
162 defaultFileName
, wildCard
, style
, pos
);
166 parent
= GetParentForModalDialog(parent
);
168 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
169 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
170 wxDefaultValidator
, wxT("filedialog")))
172 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
176 GtkFileChooserAction gtk_action
;
177 GtkWindow
* gtk_parent
= NULL
;
179 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
181 const gchar
* ok_btn_stock
;
182 if ( style
& wxFD_SAVE
)
184 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
185 ok_btn_stock
= GTK_STOCK_SAVE
;
189 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
190 ok_btn_stock
= GTK_STOCK_OPEN
;
193 m_widget
= gtk_file_chooser_dialog_new(
194 wxGTK_CONV(m_message
),
197 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
198 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
201 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
203 if ( style
& wxFD_MULTIPLE
)
204 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
206 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
207 // destroys the dialog when the user press ESC on the dialog: in that case
208 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
210 g_signal_connect (G_OBJECT(m_widget
),
212 G_CALLBACK (gtk_widget_hide_on_delete
),
215 // local-only property could be set to false to allow non-local files to be
216 // loaded. In that case get/set_uri(s) should be used instead of
217 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
218 // probably also be created with a backend, e.g "gnome-vfs", "default", ...
219 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
220 // as the default - true:
221 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
223 g_signal_connect (m_widget
, "response",
224 G_CALLBACK (gtk_filedialog_response_callback
), this);
226 SetWildcard(wildCard
);
228 // if defaultDir is specified it should contain the directory and
229 // defaultFileName should contain the default name of the file, however if
230 // directory is not given, defaultFileName contains both
232 if ( defaultDir
.empty() )
233 fn
.Assign(defaultFileName
);
234 else if ( !defaultFileName
.empty() )
235 fn
.Assign(defaultDir
, defaultFileName
);
237 fn
.AssignDir(defaultDir
);
239 // set the initial file name and/or directory
240 fn
.MakeAbsolute(); // GTK+ needs absolute path
241 const wxString dir
= fn
.GetPath();
244 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
248 const wxString fname
= fn
.GetFullName();
249 if ( style
& wxFD_SAVE
)
251 if ( !fname
.empty() )
253 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
257 #if GTK_CHECK_VERSION(2,7,3)
258 if ((style
& wxFD_OVERWRITE_PROMPT
) && !gtk_check_version(2,7,3))
259 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget
), TRUE
);
264 if ( !fname
.empty() )
266 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
267 fn
.GetFullPath().fn_str());
271 #if GTK_CHECK_VERSION(2,4,0)
272 if ( style
& wxFD_PREVIEW
)
274 GtkWidget
*previewImage
= gtk_image_new();
276 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget
),
278 g_signal_connect(m_widget
, "update-preview",
279 G_CALLBACK(gtk_filedialog_update_preview_callback
),
285 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
287 if (!gtk_check_version(2,4,0))
290 wxGenericFileDialog::OnListOk( event
);
293 int wxFileDialog::ShowModal()
295 if (!gtk_check_version(2,4,0))
296 return wxDialog::ShowModal();
298 return wxGenericFileDialog::ShowModal();
301 bool wxFileDialog::Show( bool show
)
303 if (!gtk_check_version(2,4,0))
304 return wxDialog::Show( show
);
306 return wxGenericFileDialog::Show( show
);
309 void wxFileDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
314 wxGenericFileDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
317 wxString
wxFileDialog::GetPath() const
319 if (!gtk_check_version(2,4,0))
321 wxGtkString
str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
322 return wxString(str
, *wxConvFileName
);
325 return wxGenericFileDialog::GetPath();
328 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
330 if (!gtk_check_version(2,4,0))
333 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
335 wxFileName
file(files
[n
]);
336 files
[n
] = file
.GetFullName();
340 wxGenericFileDialog::GetFilenames( files
);
343 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
345 if (!gtk_check_version(2,4,0))
348 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
350 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
351 GSList
*gpaths
= gpathsi
;
354 wxString
file((gchar
*) gpathsi
->data
, *wxConvFileName
);
356 g_free(gpathsi
->data
);
357 gpathsi
= gpathsi
->next
;
360 g_slist_free(gpaths
);
363 paths
.Add(GetPath());
366 wxGenericFileDialog::GetPaths( paths
);
369 void wxFileDialog::SetMessage(const wxString
& message
)
371 if (!gtk_check_version(2,4,0))
377 wxGenericFileDialog::SetMessage( message
);
380 void wxFileDialog::SetPath(const wxString
& path
)
382 if (!gtk_check_version(2,4,0))
384 if (path
.empty()) return;
386 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), path
.fn_str());
389 wxGenericFileDialog::SetPath( path
);
392 void wxFileDialog::SetDirectory(const wxString
& dir
)
394 if (!gtk_check_version(2,4,0))
396 if (wxDirExists(dir
))
398 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), dir
.fn_str());
402 wxGenericFileDialog::SetDirectory( dir
);
405 wxString
wxFileDialog::GetDirectory() const
407 if (!gtk_check_version(2,4,0))
409 wxGtkString
str(gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(m_widget
)));
410 return wxString(str
, *wxConvFileName
);
413 return wxGenericFileDialog::GetDirectory();
416 void wxFileDialog::SetFilename(const wxString
& name
)
418 if (!gtk_check_version(2,4,0))
420 if (HasFdFlag(wxFD_SAVE
))
421 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
423 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
426 wxGenericFileDialog::SetFilename( name
);
429 wxString
wxFileDialog::GetFilename() const
431 if (!gtk_check_version(2,4,0))
432 return wxFileName(GetPath()).GetFullName();
434 return wxGenericFileDialog::GetFilename();
437 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
439 if (!gtk_check_version(2,4,0))
442 wxArrayString wildDescriptions
, wildFilters
;
443 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
445 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
449 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
450 m_wildCard
= wildCard
;
452 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
454 // empty current filter list:
455 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
456 GSList
* filters
= ifilters
;
460 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
461 ifilters
= ifilters
->next
;
463 g_slist_free(filters
);
465 // add parsed to GtkChooser
466 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
468 GtkFileFilter
* filter
= gtk_file_filter_new();
469 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
471 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
472 while (exttok
.HasMoreTokens())
474 wxString token
= exttok
.GetNextToken();
475 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
478 gtk_file_chooser_add_filter(chooser
, filter
);
481 // Reset the filter index
486 wxGenericFileDialog::SetWildcard( wildCard
);
489 void wxFileDialog::SetFilterIndex(int filterIndex
)
492 if (!gtk_check_version(2,4,0))
495 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
496 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
498 filter
= g_slist_nth_data(filters
, filterIndex
);
502 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
506 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
509 g_slist_free(filters
);
512 wxGenericFileDialog::SetFilterIndex( filterIndex
);
515 int wxFileDialog::GetFilterIndex() const
517 if (!gtk_check_version(2,4,0))
519 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
520 GtkFileFilter
*filter
= gtk_file_chooser_get_filter(chooser
);
521 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
522 gint index
= g_slist_index(filters
, filter
);
523 g_slist_free(filters
);
527 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
534 return wxGenericFileDialog::GetFilterIndex();
537 #endif // wxUSE_FILEDLG && __WXGTK24__