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 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern void wxapp_install_idle_handler();
37 //-----------------------------------------------------------------------------
38 // "clicked" for OK-button
39 //-----------------------------------------------------------------------------
42 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
44 int style
= dialog
->GetWindowStyle();
45 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
47 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
48 #if GTK_CHECK_VERSION(2,7,3)
49 if(gtk_check_version(2,7,3) != NULL
)
51 if ((style
& wxFD_SAVE
) && (style
& wxFD_OVERWRITE_PROMPT
))
53 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
58 _("File '%s' already exists, do you really want to overwrite it?"),
59 wxString(wxConvFileName
->cMB2WX(filename
)).c_str());
61 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
62 wxYES_NO
| wxICON_QUESTION
);
63 if (dlg
.ShowModal() != wxID_YES
)
68 // change to the directory where the user went if asked
69 if (style
& wxFD_CHANGE_DIR
)
71 // Use chdir to not care about filename encodings
72 gchar
* folder
= g_path_get_dirname(filename
);
79 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
80 event
.SetEventObject(dialog
);
81 dialog
->GetEventHandler()->ProcessEvent(event
);
85 //-----------------------------------------------------------------------------
86 // "clicked" for Cancel-button
87 //-----------------------------------------------------------------------------
92 static void gtk_filedialog_cancel_callback(GtkWidget
*w
, wxFileDialog
*dialog
)
94 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
95 event
.SetEventObject(dialog
);
96 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 // 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
);
116 wxGtkString
filename(gtk_file_chooser_get_preview_filename(chooser
));
120 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
121 gboolean have_preview
= pixbuf
!= NULL
;
123 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
125 g_object_unref (pixbuf
);
127 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
129 wxUnusedVar(chooser
);
130 wxUnusedVar(user_data
);
137 //-----------------------------------------------------------------------------
139 //-----------------------------------------------------------------------------
141 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
143 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
144 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
147 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
148 const wxString
& defaultDir
,
149 const wxString
& defaultFileName
,
150 const wxString
& wildCard
,
151 long style
, const wxPoint
& pos
,
153 const wxString
& name
)
154 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
155 wildCard
, style
, pos
, sz
, name
, true )
157 if (gtk_check_version(2,4,0))
159 wxGenericFileDialog::Create( parent
, message
, defaultDir
,
160 defaultFileName
, wildCard
, style
, pos
);
164 m_needParent
= false;
166 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
167 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
168 wxDefaultValidator
, wxT("filedialog")))
170 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
174 GtkFileChooserAction gtk_action
;
175 GtkWindow
* gtk_parent
= NULL
;
177 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
179 const gchar
* ok_btn_stock
;
180 if ( style
& wxFD_SAVE
)
182 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
183 ok_btn_stock
= GTK_STOCK_SAVE
;
187 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
188 ok_btn_stock
= GTK_STOCK_OPEN
;
191 m_widget
= gtk_file_chooser_dialog_new(
192 wxGTK_CONV(m_message
),
195 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
196 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
199 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
201 if ( style
& wxFD_MULTIPLE
)
202 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
204 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
205 // the dialog when the user press ESC on the dialog: in that case a second call to
206 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
207 g_signal_connect (G_OBJECT(m_widget
),
209 G_CALLBACK (gtk_widget_hide_on_delete
),
212 // local-only property could be set to false to allow non-local files to be loaded.
213 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
214 // and the GtkFileChooserDialog should probably also be created with a backend,
215 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
216 // Currently local-only is kept as the default - true:
217 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
219 g_signal_connect (m_widget
, "response",
220 G_CALLBACK (gtk_filedialog_response_callback
), this);
222 SetWildcard(wildCard
);
224 if ( style
& wxFD_SAVE
)
226 if ( !defaultDir
.empty() )
227 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
228 wxConvFileName
->cWX2MB(defaultDir
));
230 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
231 wxGTK_CONV(defaultFileName
));
233 #if GTK_CHECK_VERSION(2,7,3)
234 if ((style
& wxFD_OVERWRITE_PROMPT
) && !gtk_check_version(2,7,3))
235 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget
), TRUE
);
240 if ( !defaultFileName
.empty() )
243 if ( defaultDir
.empty() )
248 gtk_file_chooser_set_filename(
249 GTK_FILE_CHOOSER(m_widget
),
250 wxConvFileName
->cWX2MB( wxFileName(dir
, defaultFileName
).GetFullPath() ) );
252 else if ( !defaultDir
.empty() )
254 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
255 wxConvFileName
->cWX2MB(defaultDir
) );
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))
308 return wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
310 return wxGenericFileDialog::GetPath();
313 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
315 if (!gtk_check_version(2,4,0))
318 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
320 wxFileName
file(files
[n
]);
321 files
[n
] = file
.GetFullName();
325 wxGenericFileDialog::GetFilenames( files
);
328 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
330 if (!gtk_check_version(2,4,0))
333 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
335 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
336 GSList
*gpaths
= gpathsi
;
339 wxString
file(wxConvFileName
->cMB2WX((gchar
*) gpathsi
->data
));
341 g_free(gpathsi
->data
);
342 gpathsi
= gpathsi
->next
;
345 g_slist_free(gpaths
);
348 paths
.Add(GetPath());
351 wxGenericFileDialog::GetPaths( paths
);
354 void wxFileDialog::SetMessage(const wxString
& message
)
356 if (!gtk_check_version(2,4,0))
362 wxGenericFileDialog::SetMessage( message
);
365 void wxFileDialog::SetPath(const wxString
& path
)
367 if (!gtk_check_version(2,4,0))
369 if (path
.empty()) return;
371 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(path
));
374 wxGenericFileDialog::SetPath( path
);
377 void wxFileDialog::SetDirectory(const wxString
& dir
)
379 if (!gtk_check_version(2,4,0))
381 if (wxDirExists(dir
))
383 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
387 wxGenericFileDialog::SetDirectory( dir
);
390 wxString
wxFileDialog::GetDirectory() const
392 if (!gtk_check_version(2,4,0))
393 return wxConvFileName
->cMB2WX(
394 gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget
) ) );
396 return wxGenericFileDialog::GetDirectory();
399 void wxFileDialog::SetFilename(const wxString
& name
)
401 if (!gtk_check_version(2,4,0))
403 if (HasFlag(wxFD_SAVE
))
404 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
406 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
409 wxGenericFileDialog::SetFilename( name
);
412 wxString
wxFileDialog::GetFilename() const
414 if (!gtk_check_version(2,4,0))
416 wxConvFileName
->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
))) ).GetFullName();
418 return wxGenericFileDialog::GetFilename();
421 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
423 if (!gtk_check_version(2,4,0))
426 wxArrayString wildDescriptions
, wildFilters
;
427 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
429 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
433 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
434 m_wildCard
= wildCard
;
436 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
438 // empty current filter list:
439 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
440 GSList
* filters
= ifilters
;
444 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
445 ifilters
= ifilters
->next
;
447 g_slist_free(filters
);
449 // add parsed to GtkChooser
450 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
452 GtkFileFilter
* filter
= gtk_file_filter_new();
453 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
455 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
456 while (exttok
.HasMoreTokens())
458 wxString token
= exttok
.GetNextToken();
459 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
462 gtk_file_chooser_add_filter(chooser
, filter
);
465 // Reset the filter index
470 wxGenericFileDialog::SetWildcard( wildCard
);
473 void wxFileDialog::SetFilterIndex(int filterIndex
)
476 if (!gtk_check_version(2,4,0))
479 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
480 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
482 filter
= g_slist_nth_data(filters
, filterIndex
);
486 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
490 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
493 g_slist_free(filters
);
496 wxGenericFileDialog::SetFilterIndex( filterIndex
);
499 int wxFileDialog::GetFilterIndex() const
501 if (!gtk_check_version(2,4,0))
503 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
504 GtkFileFilter
*filter
= gtk_file_chooser_get_filter(chooser
);
505 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
506 gint index
= g_slist_index(filters
, filter
);
507 g_slist_free(filters
);
511 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
518 return wxGenericFileDialog::GetFilterIndex();
521 #endif // wxUSE_FILEDLG && __WXGTK24__