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 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
158 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
159 wxDefaultValidator
, wxT("filedialog")))
161 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
165 GtkFileChooserAction gtk_action
;
166 GtkWindow
* gtk_parent
= NULL
;
168 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
170 const gchar
* ok_btn_stock
;
171 if ( style
& wxFD_SAVE
)
173 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
174 ok_btn_stock
= GTK_STOCK_SAVE
;
178 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
179 ok_btn_stock
= GTK_STOCK_OPEN
;
182 m_widget
= gtk_file_chooser_dialog_new(
183 wxGTK_CONV(m_message
),
186 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
187 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
190 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
192 if ( style
& wxFD_MULTIPLE
)
193 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
195 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
196 // destroys the dialog when the user press ESC on the dialog: in that case
197 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
199 g_signal_connect (G_OBJECT(m_widget
),
201 G_CALLBACK (gtk_widget_hide_on_delete
),
204 // local-only property could be set to false to allow non-local files to be
205 // loaded. In that case get/set_uri(s) should be used instead of
206 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
207 // probably also be created with a backend, e.g "gnome-vfs", "default", ...
208 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
209 // as the default - true:
210 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
212 g_signal_connect (m_widget
, "response",
213 G_CALLBACK (gtk_filedialog_response_callback
), this);
215 SetWildcard(wildCard
);
217 // if defaultDir is specified it should contain the directory and
218 // defaultFileName should contain the default name of the file, however if
219 // directory is not given, defaultFileName contains both
221 if ( defaultDir
.empty() )
222 fn
.Assign(defaultFileName
);
223 else if ( !defaultFileName
.empty() )
224 fn
.Assign(defaultDir
, defaultFileName
);
226 fn
.AssignDir(defaultDir
);
228 // set the initial file name and/or directory
229 const wxString dir
= fn
.GetPath();
232 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
236 const wxString fname
= fn
.GetFullName();
237 if ( style
& wxFD_SAVE
)
239 if ( !fname
.empty() )
241 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
245 #if GTK_CHECK_VERSION(2,7,3)
246 if ((style
& wxFD_OVERWRITE_PROMPT
) && !gtk_check_version(2,7,3))
247 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget
), TRUE
);
252 if ( !fname
.empty() )
254 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
255 fn
.GetFullPath().fn_str());
259 #if GTK_CHECK_VERSION(2,4,0)
260 if ( style
& wxFD_PREVIEW
)
262 GtkWidget
*previewImage
= gtk_image_new();
264 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget
),
266 g_signal_connect(m_widget
, "update-preview",
267 G_CALLBACK(gtk_filedialog_update_preview_callback
),
273 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
275 if (!gtk_check_version(2,4,0))
278 wxGenericFileDialog::OnListOk( event
);
281 int wxFileDialog::ShowModal()
283 if (!gtk_check_version(2,4,0))
284 return wxDialog::ShowModal();
286 return wxGenericFileDialog::ShowModal();
289 bool wxFileDialog::Show( bool show
)
291 if (!gtk_check_version(2,4,0))
292 return wxDialog::Show( show
);
294 return wxGenericFileDialog::Show( show
);
297 void wxFileDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
302 wxGenericFileDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
305 wxString
wxFileDialog::GetPath() const
307 if (!gtk_check_version(2,4,0))
309 wxGtkString
str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
310 return wxConvFileName
->cMB2WX(str
);
313 return wxGenericFileDialog::GetPath();
316 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
318 if (!gtk_check_version(2,4,0))
321 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
323 wxFileName
file(files
[n
]);
324 files
[n
] = file
.GetFullName();
328 wxGenericFileDialog::GetFilenames( files
);
331 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
333 if (!gtk_check_version(2,4,0))
336 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
338 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
339 GSList
*gpaths
= gpathsi
;
342 wxString
file(wxConvFileName
->cMB2WX((gchar
*) gpathsi
->data
));
344 g_free(gpathsi
->data
);
345 gpathsi
= gpathsi
->next
;
348 g_slist_free(gpaths
);
351 paths
.Add(GetPath());
354 wxGenericFileDialog::GetPaths( paths
);
357 void wxFileDialog::SetMessage(const wxString
& message
)
359 if (!gtk_check_version(2,4,0))
365 wxGenericFileDialog::SetMessage( message
);
368 void wxFileDialog::SetPath(const wxString
& path
)
370 if (!gtk_check_version(2,4,0))
372 if (path
.empty()) return;
374 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(path
));
377 wxGenericFileDialog::SetPath( path
);
380 void wxFileDialog::SetDirectory(const wxString
& dir
)
382 if (!gtk_check_version(2,4,0))
384 if (wxDirExists(dir
))
386 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
390 wxGenericFileDialog::SetDirectory( dir
);
393 wxString
wxFileDialog::GetDirectory() const
395 if (!gtk_check_version(2,4,0))
397 wxGtkString
str(gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(m_widget
)));
398 return wxConvFileName
->cMB2WX(str
);
401 return wxGenericFileDialog::GetDirectory();
404 void wxFileDialog::SetFilename(const wxString
& name
)
406 if (!gtk_check_version(2,4,0))
408 if (HasFdFlag(wxFD_SAVE
))
409 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
411 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
414 wxGenericFileDialog::SetFilename( name
);
417 wxString
wxFileDialog::GetFilename() const
419 if (!gtk_check_version(2,4,0))
420 return wxFileName(GetPath()).GetFullName();
422 return wxGenericFileDialog::GetFilename();
425 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
427 if (!gtk_check_version(2,4,0))
430 wxArrayString wildDescriptions
, wildFilters
;
431 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
433 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
437 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
438 m_wildCard
= wildCard
;
440 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
442 // empty current filter list:
443 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
444 GSList
* filters
= ifilters
;
448 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
449 ifilters
= ifilters
->next
;
451 g_slist_free(filters
);
453 // add parsed to GtkChooser
454 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
456 GtkFileFilter
* filter
= gtk_file_filter_new();
457 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
459 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
460 while (exttok
.HasMoreTokens())
462 wxString token
= exttok
.GetNextToken();
463 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
466 gtk_file_chooser_add_filter(chooser
, filter
);
469 // Reset the filter index
474 wxGenericFileDialog::SetWildcard( wildCard
);
477 void wxFileDialog::SetFilterIndex(int filterIndex
)
480 if (!gtk_check_version(2,4,0))
483 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
484 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
486 filter
= g_slist_nth_data(filters
, filterIndex
);
490 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
494 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
497 g_slist_free(filters
);
500 wxGenericFileDialog::SetFilterIndex( filterIndex
);
503 int wxFileDialog::GetFilterIndex() const
505 if (!gtk_check_version(2,4,0))
507 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
508 GtkFileFilter
*filter
= gtk_file_chooser_get_filter(chooser
);
509 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
510 gint index
= g_slist_index(filters
, filter
);
511 g_slist_free(filters
);
515 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
522 return wxGenericFileDialog::GetFilterIndex();
525 #endif // wxUSE_FILEDLG && __WXGTK24__