]>
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 | { | |
e4161a2a | 66 | wxMessageDialog dlg( dialog, _("Please choose an existing file."), |
4c84a0dc RR |
67 | _("Error"), wxOK| wxICON_ERROR); |
68 | dlg.ShowModal(); | |
69 | return; | |
70 | } | |
71 | } | |
e4161a2a | 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 | ||
e4161a2a VZ |
94 | static void |
95 | gtk_filedialog_cancel_callback(GtkWidget * WXUNUSED(w), wxFileDialog *dialog) | |
c801d85f | 96 | { |
bfc6fde4 | 97 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
9755e73b VS |
98 | event.SetEventObject(dialog); |
99 | dialog->GetEventHandler()->ProcessEvent(event); | |
100 | } | |
101 | ||
102 | static void gtk_filedialog_response_callback(GtkWidget *w, | |
f8bc53eb | 103 | gint response, |
9755e73b VS |
104 | wxFileDialog *dialog) |
105 | { | |
a7334928 | 106 | if (response == GTK_RESPONSE_ACCEPT) |
9755e73b | 107 | gtk_filedialog_ok_callback(w, dialog); |
a552d120 | 108 | else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE |
c2740a5a | 109 | gtk_filedialog_cancel_callback(w, dialog); |
ff7b1510 | 110 | } |
9f057af5 VZ |
111 | |
112 | static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser, | |
113 | gpointer user_data) | |
114 | { | |
115 | #if GTK_CHECK_VERSION(2,4,0) | |
116 | GtkWidget *preview = GTK_WIDGET(user_data); | |
3ab296d9 | 117 | |
e808cf8a | 118 | wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser)); |
3ab296d9 | 119 | |
9f057af5 VZ |
120 | if ( !filename ) |
121 | return; | |
122 | ||
123 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL); | |
124 | gboolean have_preview = pixbuf != NULL; | |
125 | ||
126 | gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf); | |
127 | if ( pixbuf ) | |
128 | g_object_unref (pixbuf); | |
129 | ||
130 | gtk_file_chooser_set_preview_widget_active(chooser, have_preview); | |
131 | #else | |
132 | wxUnusedVar(chooser); | |
133 | wxUnusedVar(user_data); | |
134 | #endif // GTK+ 2.4+ | |
865bb325 VZ |
135 | } |
136 | ||
9f057af5 VZ |
137 | } // extern "C" |
138 | ||
c801d85f | 139 | |
291a8f20 RR |
140 | //----------------------------------------------------------------------------- |
141 | // wxFileDialog | |
142 | //----------------------------------------------------------------------------- | |
143 | ||
4e1901b7 RR |
144 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog) |
145 | ||
146 | BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog) | |
f8bc53eb | 147 | EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk) |
4e1901b7 | 148 | END_EVENT_TABLE() |
c801d85f | 149 | |
9755e73b VS |
150 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, |
151 | const wxString& defaultDir, | |
152 | const wxString& defaultFileName, | |
153 | const wxString& wildCard, | |
ff3e84ff VZ |
154 | long style, const wxPoint& pos, |
155 | const wxSize& sz, | |
156 | const wxString& name) | |
4e1901b7 | 157 | : wxGenericFileDialog(parent, message, defaultDir, defaultFileName, |
ff3e84ff | 158 | wildCard, style, pos, sz, name, true ) |
c801d85f | 159 | { |
9f057af5 | 160 | if (gtk_check_version(2,4,0)) |
77f70672 | 161 | { |
9f057af5 VZ |
162 | wxGenericFileDialog::Create( parent, message, defaultDir, |
163 | defaultFileName, wildCard, style, pos ); | |
164 | return; | |
165 | } | |
83624f79 | 166 | |
2229243b VZ |
167 | parent = GetParentForModalDialog(parent); |
168 | ||
9f057af5 VZ |
169 | if (!PreCreation(parent, pos, wxDefaultSize) || |
170 | !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style, | |
171 | wxDefaultValidator, wxT("filedialog"))) | |
172 | { | |
173 | wxFAIL_MSG( wxT("wxFileDialog creation failed") ); | |
174 | return; | |
175 | } | |
3f6638b8 | 176 | |
9f057af5 VZ |
177 | GtkFileChooserAction gtk_action; |
178 | GtkWindow* gtk_parent = NULL; | |
179 | if (parent) | |
180 | gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) ); | |
27b2dd53 | 181 | |
9f057af5 VZ |
182 | const gchar* ok_btn_stock; |
183 | if ( style & wxFD_SAVE ) | |
184 | { | |
185 | gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE; | |
186 | ok_btn_stock = GTK_STOCK_SAVE; | |
187 | } | |
188 | else | |
189 | { | |
190 | gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN; | |
191 | ok_btn_stock = GTK_STOCK_OPEN; | |
192 | } | |
f8bc53eb | 193 | |
9f057af5 VZ |
194 | m_widget = gtk_file_chooser_dialog_new( |
195 | wxGTK_CONV(m_message), | |
196 | gtk_parent, | |
197 | gtk_action, | |
198 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | |
199 | ok_btn_stock, GTK_RESPONSE_ACCEPT, | |
200 | NULL); | |
9755e73b | 201 | |
0cf3e587 VZ |
202 | m_fc.SetWidget( GTK_FILE_CHOOSER(m_widget) ); |
203 | ||
9f057af5 | 204 | gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT); |
c1dfe97c | 205 | |
9f057af5 VZ |
206 | if ( style & wxFD_MULTIPLE ) |
207 | gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true); | |
27b2dd53 | 208 | |
bf345206 VZ |
209 | // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically |
210 | // destroys the dialog when the user press ESC on the dialog: in that case | |
211 | // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL | |
212 | // errors... | |
9f057af5 VZ |
213 | g_signal_connect (G_OBJECT(m_widget), |
214 | "delete_event", | |
215 | G_CALLBACK (gtk_widget_hide_on_delete), | |
216 | (gpointer)this); | |
a552d120 | 217 | |
bf345206 VZ |
218 | // local-only property could be set to false to allow non-local files to be |
219 | // loaded. In that case get/set_uri(s) should be used instead of | |
220 | // get/set_filename(s) everywhere and the GtkFileChooserDialog should | |
221 | // probably also be created with a backend, e.g "gnome-vfs", "default", ... | |
222 | // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept | |
223 | // as the default - true: | |
9f057af5 | 224 | // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true); |
27b2dd53 | 225 | |
9f057af5 VZ |
226 | g_signal_connect (m_widget, "response", |
227 | G_CALLBACK (gtk_filedialog_response_callback), this); | |
27b2dd53 | 228 | |
9f057af5 | 229 | SetWildcard(wildCard); |
f8bc53eb | 230 | |
bf345206 VZ |
231 | // if defaultDir is specified it should contain the directory and |
232 | // defaultFileName should contain the default name of the file, however if | |
233 | // directory is not given, defaultFileName contains both | |
234 | wxFileName fn; | |
235 | if ( defaultDir.empty() ) | |
236 | fn.Assign(defaultFileName); | |
237 | else if ( !defaultFileName.empty() ) | |
238 | fn.Assign(defaultDir, defaultFileName); | |
f241631e RD |
239 | else |
240 | fn.AssignDir(defaultDir); | |
bf345206 VZ |
241 | |
242 | // set the initial file name and/or directory | |
78355ffc | 243 | fn.MakeAbsolute(); // GTK+ needs absolute path |
3a41827a VZ |
244 | const wxString dir = fn.GetPath(); |
245 | if ( !dir.empty() ) | |
9f057af5 | 246 | { |
3a41827a VZ |
247 | gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), |
248 | dir.fn_str()); | |
bf345206 | 249 | } |
f8bc53eb | 250 | |
3a41827a | 251 | const wxString fname = fn.GetFullName(); |
bf345206 VZ |
252 | if ( style & wxFD_SAVE ) |
253 | { | |
254 | if ( !fname.empty() ) | |
255 | { | |
256 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), | |
257 | fname.fn_str()); | |
258 | } | |
b5ab6d19 MR |
259 | |
260 | #if GTK_CHECK_VERSION(2,7,3) | |
9f057af5 VZ |
261 | if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3)) |
262 | gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget), TRUE); | |
b5ab6d19 | 263 | #endif |
9f057af5 VZ |
264 | } |
265 | else // wxFD_OPEN | |
266 | { | |
bf345206 | 267 | if ( !fname.empty() ) |
f8bc53eb | 268 | { |
bf345206 VZ |
269 | gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), |
270 | fn.GetFullPath().fn_str()); | |
f8bc53eb | 271 | } |
77f70672 | 272 | } |
9f057af5 VZ |
273 | |
274 | #if GTK_CHECK_VERSION(2,4,0) | |
275 | if ( style & wxFD_PREVIEW ) | |
276 | { | |
277 | GtkWidget *previewImage = gtk_image_new(); | |
278 | ||
279 | gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget), | |
280 | previewImage); | |
281 | g_signal_connect(m_widget, "update-preview", | |
282 | G_CALLBACK(gtk_filedialog_update_preview_callback), | |
283 | previewImage); | |
284 | } | |
285 | #endif // GTK+ 2.4+ | |
9755e73b | 286 | } |
0e1399b3 | 287 | |
4e1901b7 RR |
288 | void wxFileDialog::OnFakeOk( wxCommandEvent &event ) |
289 | { | |
77f70672 | 290 | if (!gtk_check_version(2,4,0)) |
86508681 | 291 | EndDialog(wxID_OK); |
77f70672 | 292 | else |
0cf3e587 | 293 | wxGenericFileDialog::OnOk( event ); |
4e1901b7 RR |
294 | } |
295 | ||
296 | int wxFileDialog::ShowModal() | |
297 | { | |
77f70672 RR |
298 | if (!gtk_check_version(2,4,0)) |
299 | return wxDialog::ShowModal(); | |
300 | else | |
77f70672 | 301 | return wxGenericFileDialog::ShowModal(); |
4e1901b7 RR |
302 | } |
303 | ||
304 | bool wxFileDialog::Show( bool show ) | |
305 | { | |
77f70672 RR |
306 | if (!gtk_check_version(2,4,0)) |
307 | return wxDialog::Show( show ); | |
308 | else | |
77f70672 | 309 | return wxGenericFileDialog::Show( show ); |
9755e73b | 310 | } |
0e1399b3 | 311 | |
5b2e23bf RR |
312 | void wxFileDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags ) |
313 | { | |
314 | if (!m_wxwindow) | |
315 | return; | |
316 | else | |
317 | wxGenericFileDialog::DoSetSize( x, y, width, height, sizeFlags ); | |
318 | } | |
319 | ||
f8bc53eb JS |
320 | wxString wxFileDialog::GetPath() const |
321 | { | |
f8bc53eb | 322 | if (!gtk_check_version(2,4,0)) |
3ab296d9 | 323 | { |
0cf3e587 | 324 | return m_fc.GetPath(); |
3ab296d9 | 325 | } |
e808cf8a PC |
326 | |
327 | return wxGenericFileDialog::GetPath(); | |
f8bc53eb JS |
328 | } |
329 | ||
27b2dd53 | 330 | void wxFileDialog::GetFilenames(wxArrayString& files) const |
9755e73b | 331 | { |
77f70672 | 332 | if (!gtk_check_version(2,4,0)) |
9755e73b | 333 | { |
0cf3e587 | 334 | m_fc.GetFilenames( files ); |
9755e73b | 335 | } |
77f70672 | 336 | else |
77f70672 | 337 | wxGenericFileDialog::GetFilenames( files ); |
9755e73b | 338 | } |
76840ed0 | 339 | |
27b2dd53 | 340 | void wxFileDialog::GetPaths(wxArrayString& paths) const |
9755e73b | 341 | { |
77f70672 | 342 | if (!gtk_check_version(2,4,0)) |
9755e73b | 343 | { |
0cf3e587 | 344 | m_fc.GetPaths( paths ); |
9755e73b VS |
345 | } |
346 | else | |
77f70672 | 347 | wxGenericFileDialog::GetPaths( paths ); |
9755e73b | 348 | } |
035b704a | 349 | |
9755e73b VS |
350 | void wxFileDialog::SetMessage(const wxString& message) |
351 | { | |
77f70672 RR |
352 | if (!gtk_check_version(2,4,0)) |
353 | { | |
354 | m_message = message; | |
355 | SetTitle(message); | |
356 | } | |
357 | else | |
77f70672 | 358 | wxGenericFileDialog::SetMessage( message ); |
9755e73b VS |
359 | } |
360 | ||
76840ed0 RR |
361 | void wxFileDialog::SetPath(const wxString& path) |
362 | { | |
77f70672 RR |
363 | if (!gtk_check_version(2,4,0)) |
364 | { | |
0cf3e587 | 365 | m_fc.SetPath( path ); |
77f70672 RR |
366 | } |
367 | else | |
77f70672 | 368 | wxGenericFileDialog::SetPath( path ); |
76840ed0 RR |
369 | } |
370 | ||
9755e73b VS |
371 | void wxFileDialog::SetDirectory(const wxString& dir) |
372 | { | |
77f70672 | 373 | if (!gtk_check_version(2,4,0)) |
76840ed0 | 374 | { |
0cf3e587 | 375 | m_fc.SetDirectory( dir ); |
76840ed0 | 376 | } |
77f70672 | 377 | else |
77f70672 | 378 | wxGenericFileDialog::SetDirectory( dir ); |
9755e73b VS |
379 | } |
380 | ||
f8bc53eb JS |
381 | wxString wxFileDialog::GetDirectory() const |
382 | { | |
f8bc53eb | 383 | if (!gtk_check_version(2,4,0)) |
3ab296d9 | 384 | { |
0cf3e587 | 385 | m_fc.GetDirectory(); |
3ab296d9 | 386 | } |
e808cf8a PC |
387 | |
388 | return wxGenericFileDialog::GetDirectory(); | |
f8bc53eb JS |
389 | } |
390 | ||
9755e73b VS |
391 | void wxFileDialog::SetFilename(const wxString& name) |
392 | { | |
77f70672 RR |
393 | if (!gtk_check_version(2,4,0)) |
394 | { | |
b014db05 | 395 | if (HasFdFlag(wxFD_SAVE)) |
f0f1afb8 | 396 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name)); |
f8bc53eb JS |
397 | else |
398 | SetPath(wxFileName(GetDirectory(), name).GetFullPath()); | |
77f70672 RR |
399 | } |
400 | else | |
77f70672 | 401 | wxGenericFileDialog::SetFilename( name ); |
9755e73b | 402 | } |
035b704a | 403 | |
f8bc53eb JS |
404 | wxString wxFileDialog::GetFilename() const |
405 | { | |
f8bc53eb | 406 | if (!gtk_check_version(2,4,0)) |
0cf3e587 | 407 | return m_fc.GetFilename(); |
f8bc53eb | 408 | else |
f8bc53eb JS |
409 | return wxGenericFileDialog::GetFilename(); |
410 | } | |
411 | ||
9755e73b VS |
412 | void wxFileDialog::SetWildcard(const wxString& wildCard) |
413 | { | |
77f70672 | 414 | if (!gtk_check_version(2,4,0)) |
9755e73b | 415 | { |
0cf3e587 | 416 | m_fc.SetWildcard( wildCard ); |
9755e73b | 417 | } |
77f70672 | 418 | else |
77f70672 | 419 | wxGenericFileDialog::SetWildcard( wildCard ); |
9755e73b | 420 | } |
a3622daa | 421 | |
9755e73b VS |
422 | void wxFileDialog::SetFilterIndex(int filterIndex) |
423 | { | |
ff3e84ff | 424 | |
77f70672 | 425 | if (!gtk_check_version(2,4,0)) |
9755e73b | 426 | { |
0cf3e587 | 427 | m_fc.SetFilterIndex( filterIndex); |
9755e73b | 428 | } |
77f70672 | 429 | else |
77f70672 | 430 | wxGenericFileDialog::SetFilterIndex( filterIndex ); |
4e1901b7 RR |
431 | } |
432 | ||
f8bc53eb | 433 | int wxFileDialog::GetFilterIndex() const |
4e1901b7 | 434 | { |
f8bc53eb | 435 | if (!gtk_check_version(2,4,0)) |
4e1901b7 | 436 | { |
0cf3e587 | 437 | return m_fc.GetFilterIndex(); |
9755e73b | 438 | } |
f8bc53eb | 439 | else |
f8bc53eb | 440 | return wxGenericFileDialog::GetFilterIndex(); |
9755e73b | 441 | } |
3f6638b8 | 442 | |
ff3e84ff | 443 | #endif // wxUSE_FILEDLG && __WXGTK24__ |