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
);
117 gchar
*str
= gtk_file_chooser_get_preview_filename(chooser
);
118 wxGtkString
filename(str
);
119 if (str
) g_free(str
);
124 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
125 gboolean have_preview
= pixbuf
!= NULL
;
127 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
129 g_object_unref (pixbuf
);
131 gtk_file_chooser_set_preview_widget_active(chooser
, have_preview
);
133 wxUnusedVar(chooser
);
134 wxUnusedVar(user_data
);
141 //-----------------------------------------------------------------------------
143 //-----------------------------------------------------------------------------
145 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxGenericFileDialog
)
147 BEGIN_EVENT_TABLE(wxFileDialog
,wxGenericFileDialog
)
148 EVT_BUTTON(wxID_OK
, wxFileDialog::OnFakeOk
)
151 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
152 const wxString
& defaultDir
,
153 const wxString
& defaultFileName
,
154 const wxString
& wildCard
,
155 long style
, const wxPoint
& pos
,
157 const wxString
& name
)
158 : wxGenericFileDialog(parent
, message
, defaultDir
, defaultFileName
,
159 wildCard
, style
, pos
, sz
, name
, true )
161 if (gtk_check_version(2,4,0))
163 wxGenericFileDialog::Create( parent
, message
, defaultDir
,
164 defaultFileName
, wildCard
, style
, pos
);
168 m_needParent
= false;
170 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
171 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
172 wxDefaultValidator
, wxT("filedialog")))
174 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
178 GtkFileChooserAction gtk_action
;
179 GtkWindow
* gtk_parent
= NULL
;
181 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
183 const gchar
* ok_btn_stock
;
184 if ( style
& wxFD_SAVE
)
186 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
187 ok_btn_stock
= GTK_STOCK_SAVE
;
191 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
192 ok_btn_stock
= GTK_STOCK_OPEN
;
195 m_widget
= gtk_file_chooser_dialog_new(
196 wxGTK_CONV(m_message
),
199 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
200 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
203 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
205 if ( style
& wxFD_MULTIPLE
)
206 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), true);
208 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
209 // the dialog when the user press ESC on the dialog: in that case a second call to
210 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
211 g_signal_connect (G_OBJECT(m_widget
),
213 G_CALLBACK (gtk_widget_hide_on_delete
),
216 // local-only property could be set to false to allow non-local files to be loaded.
217 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
218 // and the GtkFileChooserDialog should probably also be created with a backend,
219 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
220 // Currently local-only is kept 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 ( style
& wxFD_SAVE
)
230 if ( !defaultDir
.empty() )
231 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
232 wxConvFileName
->cWX2MB(defaultDir
));
234 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
235 wxGTK_CONV(defaultFileName
));
237 #if GTK_CHECK_VERSION(2,7,3)
238 if ((style
& wxFD_OVERWRITE_PROMPT
) && !gtk_check_version(2,7,3))
239 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget
), TRUE
);
244 if ( !defaultFileName
.empty() )
247 if ( defaultDir
.empty() )
252 gtk_file_chooser_set_filename(
253 GTK_FILE_CHOOSER(m_widget
),
254 wxConvFileName
->cWX2MB( wxFileName(dir
, defaultFileName
).GetFullPath() ) );
256 else if ( !defaultDir
.empty() )
258 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
259 wxConvFileName
->cWX2MB(defaultDir
) );
263 #if GTK_CHECK_VERSION(2,4,0)
264 if ( style
& wxFD_PREVIEW
)
266 GtkWidget
*previewImage
= gtk_image_new();
268 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget
),
270 g_signal_connect(m_widget
, "update-preview",
271 G_CALLBACK(gtk_filedialog_update_preview_callback
),
277 void wxFileDialog::OnFakeOk( wxCommandEvent
&event
)
279 if (!gtk_check_version(2,4,0))
282 wxGenericFileDialog::OnListOk( event
);
285 int wxFileDialog::ShowModal()
287 if (!gtk_check_version(2,4,0))
288 return wxDialog::ShowModal();
290 return wxGenericFileDialog::ShowModal();
293 bool wxFileDialog::Show( bool show
)
295 if (!gtk_check_version(2,4,0))
296 return wxDialog::Show( show
);
298 return wxGenericFileDialog::Show( show
);
301 void wxFileDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
306 wxGenericFileDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
309 wxString
wxFileDialog::GetPath() const
311 if (!gtk_check_version(2,4,0))
313 gchar
*str
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
));
314 wxString ret
= wxConvFileName
->cMB2WX(str
);
315 if (str
) g_free(str
);
320 return wxGenericFileDialog::GetPath();
323 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
325 if (!gtk_check_version(2,4,0))
328 for (size_t n
= 0; n
< files
.GetCount(); ++n
)
330 wxFileName
file(files
[n
]);
331 files
[n
] = file
.GetFullName();
335 wxGenericFileDialog::GetFilenames( files
);
338 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
340 if (!gtk_check_version(2,4,0))
343 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget
)))
345 GSList
*gpathsi
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
346 GSList
*gpaths
= gpathsi
;
349 wxString
file(wxConvFileName
->cMB2WX((gchar
*) gpathsi
->data
));
351 g_free(gpathsi
->data
);
352 gpathsi
= gpathsi
->next
;
355 g_slist_free(gpaths
);
358 paths
.Add(GetPath());
361 wxGenericFileDialog::GetPaths( paths
);
364 void wxFileDialog::SetMessage(const wxString
& message
)
366 if (!gtk_check_version(2,4,0))
372 wxGenericFileDialog::SetMessage( message
);
375 void wxFileDialog::SetPath(const wxString
& path
)
377 if (!gtk_check_version(2,4,0))
379 if (path
.empty()) return;
381 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(path
));
384 wxGenericFileDialog::SetPath( path
);
387 void wxFileDialog::SetDirectory(const wxString
& dir
)
389 if (!gtk_check_version(2,4,0))
391 if (wxDirExists(dir
))
393 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
397 wxGenericFileDialog::SetDirectory( dir
);
400 wxString
wxFileDialog::GetDirectory() const
402 if (!gtk_check_version(2,4,0))
404 gchar
*str
= gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget
) );
405 wxString ret
= wxConvFileName
->cMB2WX(str
);
406 if (str
) g_free(str
);
411 return wxGenericFileDialog::GetDirectory();
414 void wxFileDialog::SetFilename(const wxString
& name
)
416 if (!gtk_check_version(2,4,0))
418 if (HasFlag(wxFD_SAVE
))
419 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
), wxGTK_CONV(name
));
421 SetPath(wxFileName(GetDirectory(), name
).GetFullPath());
424 wxGenericFileDialog::SetFilename( name
);
427 wxString
wxFileDialog::GetFilename() const
429 if (!gtk_check_version(2,4,0))
430 return wxFileName(GetPath()).GetFullName();
432 return wxGenericFileDialog::GetFilename();
435 void wxFileDialog::SetWildcard(const wxString
& wildCard
)
437 if (!gtk_check_version(2,4,0))
440 wxArrayString wildDescriptions
, wildFilters
;
441 if (!wxParseCommonDialogsFilter(wildCard
, wildDescriptions
, wildFilters
))
443 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
447 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
448 m_wildCard
= wildCard
;
450 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
452 // empty current filter list:
453 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
454 GSList
* filters
= ifilters
;
458 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
459 ifilters
= ifilters
->next
;
461 g_slist_free(filters
);
463 // add parsed to GtkChooser
464 for (size_t n
= 0; n
< wildFilters
.GetCount(); ++n
)
466 GtkFileFilter
* filter
= gtk_file_filter_new();
467 gtk_file_filter_set_name(filter
, wxGTK_CONV(wildDescriptions
[n
]));
469 wxStringTokenizer
exttok(wildFilters
[n
], wxT(";"));
470 while (exttok
.HasMoreTokens())
472 wxString token
= exttok
.GetNextToken();
473 gtk_file_filter_add_pattern(filter
, wxGTK_CONV(token
));
476 gtk_file_chooser_add_filter(chooser
, filter
);
479 // Reset the filter index
484 wxGenericFileDialog::SetWildcard( wildCard
);
487 void wxFileDialog::SetFilterIndex(int filterIndex
)
490 if (!gtk_check_version(2,4,0))
493 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
494 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
496 filter
= g_slist_nth_data(filters
, filterIndex
);
500 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(filter
));
504 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
507 g_slist_free(filters
);
510 wxGenericFileDialog::SetFilterIndex( filterIndex
);
513 int wxFileDialog::GetFilterIndex() const
515 if (!gtk_check_version(2,4,0))
517 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
518 GtkFileFilter
*filter
= gtk_file_chooser_get_filter(chooser
);
519 GSList
*filters
= gtk_file_chooser_list_filters(chooser
);
520 gint index
= g_slist_index(filters
, filter
);
521 g_slist_free(filters
);
525 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
532 return wxGenericFileDialog::GetFilterIndex();
535 #endif // wxUSE_FILEDLG && __WXGTK24__