]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
9b5f1895 | 2 | // Name: src/gtk/filedlg.cpp |
9755e73b | 3 | // Purpose: native implementation of wxFileDialog |
f8bc53eb | 4 | // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp |
a81258be | 5 | // Id: $Id$ |
f8bc53eb | 6 | // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
ff3e84ff | 13 | #if wxUSE_FILEDLG && defined(__WXGTK24__) |
9755e73b | 14 | |
c801d85f | 15 | #include "wx/filedlg.h" |
4e1901b7 | 16 | |
88a7a4e1 WS |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/intl.h" | |
246c5004 | 19 | #include "wx/msgdlg.h" |
88a7a4e1 WS |
20 | #endif |
21 | ||
f8bc53eb | 22 | #include <gtk/gtk.h> |
9755e73b | 23 | #include "wx/gtk/private.h" |
83624f79 | 24 | |
f8bc53eb JS |
25 | #include <unistd.h> // chdir |
26 | ||
f8bc53eb JS |
27 | #include "wx/filename.h" // wxFilename |
28 | #include "wx/tokenzr.h" // wxStringTokenizer | |
29 | #include "wx/filefn.h" // ::wxGetCwd | |
f8bc53eb | 30 | |
291a8f20 RR |
31 | //----------------------------------------------------------------------------- |
32 | // "clicked" for OK-button | |
c801d85f KB |
33 | //----------------------------------------------------------------------------- |
34 | ||
865bb325 | 35 | extern "C" { |
9755e73b | 36 | static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog) |
c801d85f | 37 | { |
ff3e84ff | 38 | int style = dialog->GetWindowStyle(); |
e808cf8a | 39 | wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget))); |
035b704a | 40 | |
b5ab6d19 MR |
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) | |
44 | #endif | |
ff3e84ff | 45 | if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT)) |
bfc6fde4 | 46 | { |
f8bc53eb | 47 | if ( g_file_test(filename, G_FILE_TEST_EXISTS) ) |
0e1399b3 VZ |
48 | { |
49 | wxString msg; | |
f8bc53eb | 50 | |
9755e73b | 51 | msg.Printf( |
f8bc53eb | 52 | _("File '%s' already exists, do you really want to overwrite it?"), |
86501081 | 53 | wxString(filename, *wxConvFileName)); |
0e1399b3 | 54 | |
9755e73b VS |
55 | wxMessageDialog dlg(dialog, msg, _("Confirm"), |
56 | wxYES_NO | wxICON_QUESTION); | |
57 | if (dlg.ShowModal() != wxID_YES) | |
0e1399b3 VZ |
58 | return; |
59 | } | |
83624f79 | 60 | } |
035b704a | 61 | |
4c84a0dc RR |
62 | if (style & wxFD_FILE_MUST_EXIST) |
63 | { | |
64 | if ( !g_file_test(filename, G_FILE_TEST_EXISTS) ) | |
65 | { | |
66 | wxMessageDialog dlg( dialog, _("Please choose an existing file."), | |
67 | _("Error"), wxOK| wxICON_ERROR); | |
68 | dlg.ShowModal(); | |
69 | return; | |
70 | } | |
71 | } | |
72 | ||
3f6638b8 | 73 | // change to the directory where the user went if asked |
ff3e84ff | 74 | if (style & wxFD_CHANGE_DIR) |
3f6638b8 | 75 | { |
f8bc53eb | 76 | // Use chdir to not care about filename encodings |
e808cf8a | 77 | wxGtkString folder(g_path_get_dirname(filename)); |
f8bc53eb | 78 | chdir(folder); |
3f6638b8 VZ |
79 | } |
80 | ||
bfc6fde4 | 81 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK); |
9755e73b VS |
82 | event.SetEventObject(dialog); |
83 | dialog->GetEventHandler()->ProcessEvent(event); | |
ff7b1510 | 84 | } |
865bb325 | 85 | } |
c801d85f | 86 | |
291a8f20 RR |
87 | //----------------------------------------------------------------------------- |
88 | // "clicked" for Cancel-button | |
89 | //----------------------------------------------------------------------------- | |
90 | ||
9f057af5 VZ |
91 | extern "C" |
92 | { | |
93 | ||
a552d120 | 94 | static void gtk_filedialog_cancel_callback(GtkWidget *w, wxFileDialog *dialog) |
c801d85f | 95 | { |
bfc6fde4 | 96 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
9755e73b VS |
97 | event.SetEventObject(dialog); |
98 | dialog->GetEventHandler()->ProcessEvent(event); | |
99 | } | |
100 | ||
101 | static void gtk_filedialog_response_callback(GtkWidget *w, | |
f8bc53eb | 102 | gint response, |
9755e73b VS |
103 | wxFileDialog *dialog) |
104 | { | |
a7334928 | 105 | if (response == GTK_RESPONSE_ACCEPT) |
9755e73b | 106 | gtk_filedialog_ok_callback(w, dialog); |
a552d120 | 107 | else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE |
c2740a5a | 108 | gtk_filedialog_cancel_callback(w, dialog); |
ff7b1510 | 109 | } |
9f057af5 VZ |
110 | |
111 | static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser, | |
112 | gpointer user_data) | |
113 | { | |
114 | #if GTK_CHECK_VERSION(2,4,0) | |
115 | GtkWidget *preview = GTK_WIDGET(user_data); | |
3ab296d9 | 116 | |
e808cf8a | 117 | wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser)); |
3ab296d9 | 118 | |
9f057af5 VZ |
119 | if ( !filename ) |
120 | return; | |
121 | ||
122 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL); | |
123 | gboolean have_preview = pixbuf != NULL; | |
124 | ||
125 | gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf); | |
126 | if ( pixbuf ) | |
127 | g_object_unref (pixbuf); | |
128 | ||
129 | gtk_file_chooser_set_preview_widget_active(chooser, have_preview); | |
130 | #else | |
131 | wxUnusedVar(chooser); | |
132 | wxUnusedVar(user_data); | |
133 | #endif // GTK+ 2.4+ | |
865bb325 VZ |
134 | } |
135 | ||
9f057af5 VZ |
136 | } // extern "C" |
137 | ||
c801d85f | 138 | |
291a8f20 RR |
139 | //----------------------------------------------------------------------------- |
140 | // wxFileDialog | |
141 | //----------------------------------------------------------------------------- | |
142 | ||
4e1901b7 RR |
143 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog) |
144 | ||
145 | BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog) | |
f8bc53eb | 146 | EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk) |
4e1901b7 | 147 | END_EVENT_TABLE() |
c801d85f | 148 | |
9755e73b VS |
149 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, |
150 | const wxString& defaultDir, | |
151 | const wxString& defaultFileName, | |
152 | const wxString& wildCard, | |
ff3e84ff VZ |
153 | long style, const wxPoint& pos, |
154 | const wxSize& sz, | |
155 | const wxString& name) | |
4e1901b7 | 156 | : wxGenericFileDialog(parent, message, defaultDir, defaultFileName, |
ff3e84ff | 157 | wildCard, style, pos, sz, name, true ) |
c801d85f | 158 | { |
9f057af5 | 159 | if (gtk_check_version(2,4,0)) |
77f70672 | 160 | { |
9f057af5 VZ |
161 | wxGenericFileDialog::Create( parent, message, defaultDir, |
162 | defaultFileName, wildCard, style, pos ); | |
163 | return; | |
164 | } | |
83624f79 | 165 | |
2229243b VZ |
166 | parent = GetParentForModalDialog(parent); |
167 | ||
9f057af5 VZ |
168 | if (!PreCreation(parent, pos, wxDefaultSize) || |
169 | !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style, | |
170 | wxDefaultValidator, wxT("filedialog"))) | |
171 | { | |
172 | wxFAIL_MSG( wxT("wxFileDialog creation failed") ); | |
173 | return; | |
174 | } | |
3f6638b8 | 175 | |
9f057af5 VZ |
176 | GtkFileChooserAction gtk_action; |
177 | GtkWindow* gtk_parent = NULL; | |
178 | if (parent) | |
179 | gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) ); | |
27b2dd53 | 180 | |
9f057af5 VZ |
181 | const gchar* ok_btn_stock; |
182 | if ( style & wxFD_SAVE ) | |
183 | { | |
184 | gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE; | |
185 | ok_btn_stock = GTK_STOCK_SAVE; | |
186 | } | |
187 | else | |
188 | { | |
189 | gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN; | |
190 | ok_btn_stock = GTK_STOCK_OPEN; | |
191 | } | |
f8bc53eb | 192 | |
9f057af5 VZ |
193 | m_widget = gtk_file_chooser_dialog_new( |
194 | wxGTK_CONV(m_message), | |
195 | gtk_parent, | |
196 | gtk_action, | |
197 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | |
198 | ok_btn_stock, GTK_RESPONSE_ACCEPT, | |
199 | NULL); | |
9755e73b | 200 | |
0cf3e587 VZ |
201 | m_fc.SetWidget( GTK_FILE_CHOOSER(m_widget) ); |
202 | ||
9f057af5 | 203 | gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT); |
c1dfe97c | 204 | |
9f057af5 VZ |
205 | if ( style & wxFD_MULTIPLE ) |
206 | gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true); | |
27b2dd53 | 207 | |
bf345206 VZ |
208 | // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically |
209 | // destroys the dialog when the user press ESC on the dialog: in that case | |
210 | // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL | |
211 | // errors... | |
9f057af5 VZ |
212 | g_signal_connect (G_OBJECT(m_widget), |
213 | "delete_event", | |
214 | G_CALLBACK (gtk_widget_hide_on_delete), | |
215 | (gpointer)this); | |
a552d120 | 216 | |
bf345206 VZ |
217 | // local-only property could be set to false to allow non-local files to be |
218 | // loaded. In that case get/set_uri(s) should be used instead of | |
219 | // get/set_filename(s) everywhere and the GtkFileChooserDialog should | |
220 | // probably also be created with a backend, e.g "gnome-vfs", "default", ... | |
221 | // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept | |
222 | // as the default - true: | |
9f057af5 | 223 | // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true); |
27b2dd53 | 224 | |
9f057af5 VZ |
225 | g_signal_connect (m_widget, "response", |
226 | G_CALLBACK (gtk_filedialog_response_callback), this); | |
27b2dd53 | 227 | |
9f057af5 | 228 | SetWildcard(wildCard); |
f8bc53eb | 229 | |
bf345206 VZ |
230 | // if defaultDir is specified it should contain the directory and |
231 | // defaultFileName should contain the default name of the file, however if | |
232 | // directory is not given, defaultFileName contains both | |
233 | wxFileName fn; | |
234 | if ( defaultDir.empty() ) | |
235 | fn.Assign(defaultFileName); | |
236 | else if ( !defaultFileName.empty() ) | |
237 | fn.Assign(defaultDir, defaultFileName); | |
f241631e RD |
238 | else |
239 | fn.AssignDir(defaultDir); | |
bf345206 VZ |
240 | |
241 | // set the initial file name and/or directory | |
78355ffc | 242 | fn.MakeAbsolute(); // GTK+ needs absolute path |
3a41827a VZ |
243 | const wxString dir = fn.GetPath(); |
244 | if ( !dir.empty() ) | |
9f057af5 | 245 | { |
3a41827a VZ |
246 | gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), |
247 | dir.fn_str()); | |
bf345206 | 248 | } |
f8bc53eb | 249 | |
3a41827a | 250 | const wxString fname = fn.GetFullName(); |
bf345206 VZ |
251 | if ( style & wxFD_SAVE ) |
252 | { | |
253 | if ( !fname.empty() ) | |
254 | { | |
255 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), | |
256 | fname.fn_str()); | |
257 | } | |
b5ab6d19 MR |
258 | |
259 | #if GTK_CHECK_VERSION(2,7,3) | |
9f057af5 VZ |
260 | if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3)) |
261 | gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget), TRUE); | |
b5ab6d19 | 262 | #endif |
9f057af5 VZ |
263 | } |
264 | else // wxFD_OPEN | |
265 | { | |
bf345206 | 266 | if ( !fname.empty() ) |
f8bc53eb | 267 | { |
bf345206 VZ |
268 | gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), |
269 | fn.GetFullPath().fn_str()); | |
f8bc53eb | 270 | } |
77f70672 | 271 | } |
9f057af5 VZ |
272 | |
273 | #if GTK_CHECK_VERSION(2,4,0) | |
274 | if ( style & wxFD_PREVIEW ) | |
275 | { | |
276 | GtkWidget *previewImage = gtk_image_new(); | |
277 | ||
278 | gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget), | |
279 | previewImage); | |
280 | g_signal_connect(m_widget, "update-preview", | |
281 | G_CALLBACK(gtk_filedialog_update_preview_callback), | |
282 | previewImage); | |
283 | } | |
284 | #endif // GTK+ 2.4+ | |
9755e73b | 285 | } |
0e1399b3 | 286 | |
4e1901b7 RR |
287 | void wxFileDialog::OnFakeOk( wxCommandEvent &event ) |
288 | { | |
77f70672 | 289 | if (!gtk_check_version(2,4,0)) |
86508681 | 290 | EndDialog(wxID_OK); |
77f70672 | 291 | else |
0cf3e587 | 292 | wxGenericFileDialog::OnOk( event ); |
4e1901b7 RR |
293 | } |
294 | ||
295 | int wxFileDialog::ShowModal() | |
296 | { | |
77f70672 RR |
297 | if (!gtk_check_version(2,4,0)) |
298 | return wxDialog::ShowModal(); | |
299 | else | |
77f70672 | 300 | return wxGenericFileDialog::ShowModal(); |
4e1901b7 RR |
301 | } |
302 | ||
303 | bool wxFileDialog::Show( bool show ) | |
304 | { | |
77f70672 RR |
305 | if (!gtk_check_version(2,4,0)) |
306 | return wxDialog::Show( show ); | |
307 | else | |
77f70672 | 308 | return wxGenericFileDialog::Show( show ); |
9755e73b | 309 | } |
0e1399b3 | 310 | |
5b2e23bf RR |
311 | void wxFileDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags ) |
312 | { | |
313 | if (!m_wxwindow) | |
314 | return; | |
315 | else | |
316 | wxGenericFileDialog::DoSetSize( x, y, width, height, sizeFlags ); | |
317 | } | |
318 | ||
f8bc53eb JS |
319 | wxString wxFileDialog::GetPath() const |
320 | { | |
f8bc53eb | 321 | if (!gtk_check_version(2,4,0)) |
3ab296d9 | 322 | { |
0cf3e587 | 323 | return m_fc.GetPath(); |
3ab296d9 | 324 | } |
e808cf8a PC |
325 | |
326 | return wxGenericFileDialog::GetPath(); | |
f8bc53eb JS |
327 | } |
328 | ||
27b2dd53 | 329 | void wxFileDialog::GetFilenames(wxArrayString& files) const |
9755e73b | 330 | { |
77f70672 | 331 | if (!gtk_check_version(2,4,0)) |
9755e73b | 332 | { |
0cf3e587 | 333 | m_fc.GetFilenames( files ); |
9755e73b | 334 | } |
77f70672 | 335 | else |
77f70672 | 336 | wxGenericFileDialog::GetFilenames( files ); |
9755e73b | 337 | } |
76840ed0 | 338 | |
27b2dd53 | 339 | void wxFileDialog::GetPaths(wxArrayString& paths) const |
9755e73b | 340 | { |
77f70672 | 341 | if (!gtk_check_version(2,4,0)) |
9755e73b | 342 | { |
0cf3e587 | 343 | m_fc.GetPaths( paths ); |
9755e73b VS |
344 | } |
345 | else | |
77f70672 | 346 | wxGenericFileDialog::GetPaths( paths ); |
9755e73b | 347 | } |
035b704a | 348 | |
9755e73b VS |
349 | void wxFileDialog::SetMessage(const wxString& message) |
350 | { | |
77f70672 RR |
351 | if (!gtk_check_version(2,4,0)) |
352 | { | |
353 | m_message = message; | |
354 | SetTitle(message); | |
355 | } | |
356 | else | |
77f70672 | 357 | wxGenericFileDialog::SetMessage( message ); |
9755e73b VS |
358 | } |
359 | ||
76840ed0 RR |
360 | void wxFileDialog::SetPath(const wxString& path) |
361 | { | |
77f70672 RR |
362 | if (!gtk_check_version(2,4,0)) |
363 | { | |
0cf3e587 | 364 | m_fc.SetPath( path ); |
77f70672 RR |
365 | } |
366 | else | |
77f70672 | 367 | wxGenericFileDialog::SetPath( path ); |
76840ed0 RR |
368 | } |
369 | ||
9755e73b VS |
370 | void wxFileDialog::SetDirectory(const wxString& dir) |
371 | { | |
77f70672 | 372 | if (!gtk_check_version(2,4,0)) |
76840ed0 | 373 | { |
0cf3e587 | 374 | m_fc.SetDirectory( dir ); |
76840ed0 | 375 | } |
77f70672 | 376 | else |
77f70672 | 377 | wxGenericFileDialog::SetDirectory( dir ); |
9755e73b VS |
378 | } |
379 | ||
f8bc53eb JS |
380 | wxString wxFileDialog::GetDirectory() const |
381 | { | |
f8bc53eb | 382 | if (!gtk_check_version(2,4,0)) |
3ab296d9 | 383 | { |
0cf3e587 | 384 | m_fc.GetDirectory(); |
3ab296d9 | 385 | } |
e808cf8a PC |
386 | |
387 | return wxGenericFileDialog::GetDirectory(); | |
f8bc53eb JS |
388 | } |
389 | ||
9755e73b VS |
390 | void wxFileDialog::SetFilename(const wxString& name) |
391 | { | |
77f70672 RR |
392 | if (!gtk_check_version(2,4,0)) |
393 | { | |
b014db05 | 394 | if (HasFdFlag(wxFD_SAVE)) |
f0f1afb8 | 395 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name)); |
f8bc53eb JS |
396 | else |
397 | SetPath(wxFileName(GetDirectory(), name).GetFullPath()); | |
77f70672 RR |
398 | } |
399 | else | |
77f70672 | 400 | wxGenericFileDialog::SetFilename( name ); |
9755e73b | 401 | } |
035b704a | 402 | |
f8bc53eb JS |
403 | wxString wxFileDialog::GetFilename() const |
404 | { | |
f8bc53eb | 405 | if (!gtk_check_version(2,4,0)) |
0cf3e587 | 406 | return m_fc.GetFilename(); |
f8bc53eb | 407 | else |
f8bc53eb JS |
408 | return wxGenericFileDialog::GetFilename(); |
409 | } | |
410 | ||
9755e73b VS |
411 | void wxFileDialog::SetWildcard(const wxString& wildCard) |
412 | { | |
77f70672 | 413 | if (!gtk_check_version(2,4,0)) |
9755e73b | 414 | { |
0cf3e587 | 415 | m_fc.SetWildcard( wildCard ); |
9755e73b | 416 | } |
77f70672 | 417 | else |
77f70672 | 418 | wxGenericFileDialog::SetWildcard( wildCard ); |
9755e73b | 419 | } |
a3622daa | 420 | |
9755e73b VS |
421 | void wxFileDialog::SetFilterIndex(int filterIndex) |
422 | { | |
ff3e84ff | 423 | |
77f70672 | 424 | if (!gtk_check_version(2,4,0)) |
9755e73b | 425 | { |
0cf3e587 | 426 | m_fc.SetFilterIndex( filterIndex); |
9755e73b | 427 | } |
77f70672 | 428 | else |
77f70672 | 429 | wxGenericFileDialog::SetFilterIndex( filterIndex ); |
4e1901b7 RR |
430 | } |
431 | ||
f8bc53eb | 432 | int wxFileDialog::GetFilterIndex() const |
4e1901b7 | 433 | { |
f8bc53eb | 434 | if (!gtk_check_version(2,4,0)) |
4e1901b7 | 435 | { |
0cf3e587 | 436 | return m_fc.GetFilterIndex(); |
9755e73b | 437 | } |
f8bc53eb | 438 | else |
f8bc53eb | 439 | return wxGenericFileDialog::GetFilterIndex(); |
9755e73b | 440 | } |
3f6638b8 | 441 | |
ff3e84ff | 442 | #endif // wxUSE_FILEDLG && __WXGTK24__ |