]>
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 | ||
700d08c1 | 13 | #if wxUSE_FILEDLG |
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) | |
f2448f21 | 43 | if (gtk_check_version(2, 7, 3) != NULL) |
b5ab6d19 | 44 | #endif |
bfc6fde4 | 45 | { |
f2448f21 | 46 | if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT)) |
0e1399b3 | 47 | { |
f2448f21 PC |
48 | if ( g_file_test(filename, G_FILE_TEST_EXISTS) ) |
49 | { | |
50 | wxString msg; | |
51 | ||
52 | msg.Printf( | |
53 | _("File '%s' already exists, do you really want to overwrite it?"), | |
54 | wxString(filename, *wxConvFileName)); | |
55 | ||
56 | wxMessageDialog dlg(dialog, msg, _("Confirm"), | |
57 | wxYES_NO | wxICON_QUESTION); | |
58 | if (dlg.ShowModal() != wxID_YES) | |
59 | return; | |
60 | } | |
0e1399b3 | 61 | } |
83624f79 | 62 | } |
035b704a | 63 | |
4c84a0dc RR |
64 | if (style & wxFD_FILE_MUST_EXIST) |
65 | { | |
66 | if ( !g_file_test(filename, G_FILE_TEST_EXISTS) ) | |
67 | { | |
e4161a2a | 68 | wxMessageDialog dlg( dialog, _("Please choose an existing file."), |
4c84a0dc RR |
69 | _("Error"), wxOK| wxICON_ERROR); |
70 | dlg.ShowModal(); | |
71 | return; | |
72 | } | |
73 | } | |
e4161a2a | 74 | |
3f6638b8 | 75 | // change to the directory where the user went if asked |
ff3e84ff | 76 | if (style & wxFD_CHANGE_DIR) |
3f6638b8 | 77 | { |
f8bc53eb | 78 | // Use chdir to not care about filename encodings |
e808cf8a | 79 | wxGtkString folder(g_path_get_dirname(filename)); |
f8bc53eb | 80 | chdir(folder); |
3f6638b8 VZ |
81 | } |
82 | ||
bfc6fde4 | 83 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK); |
9755e73b | 84 | event.SetEventObject(dialog); |
937013e0 | 85 | dialog->HandleWindowEvent(event); |
ff7b1510 | 86 | } |
865bb325 | 87 | } |
c801d85f | 88 | |
291a8f20 RR |
89 | //----------------------------------------------------------------------------- |
90 | // "clicked" for Cancel-button | |
91 | //----------------------------------------------------------------------------- | |
92 | ||
9f057af5 VZ |
93 | extern "C" |
94 | { | |
95 | ||
e4161a2a VZ |
96 | static void |
97 | gtk_filedialog_cancel_callback(GtkWidget * WXUNUSED(w), wxFileDialog *dialog) | |
c801d85f | 98 | { |
bfc6fde4 | 99 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
9755e73b | 100 | event.SetEventObject(dialog); |
937013e0 | 101 | dialog->HandleWindowEvent(event); |
9755e73b VS |
102 | } |
103 | ||
104 | static void gtk_filedialog_response_callback(GtkWidget *w, | |
f8bc53eb | 105 | gint response, |
9755e73b VS |
106 | wxFileDialog *dialog) |
107 | { | |
a7334928 | 108 | if (response == GTK_RESPONSE_ACCEPT) |
9755e73b | 109 | gtk_filedialog_ok_callback(w, dialog); |
a552d120 | 110 | else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE |
c2740a5a | 111 | gtk_filedialog_cancel_callback(w, dialog); |
ff7b1510 | 112 | } |
9f057af5 VZ |
113 | |
114 | static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser, | |
115 | gpointer user_data) | |
116 | { | |
9f057af5 | 117 | GtkWidget *preview = GTK_WIDGET(user_data); |
3ab296d9 | 118 | |
e808cf8a | 119 | wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser)); |
3ab296d9 | 120 | |
9f057af5 VZ |
121 | if ( !filename ) |
122 | return; | |
123 | ||
124 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL); | |
125 | gboolean have_preview = pixbuf != NULL; | |
126 | ||
127 | gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf); | |
128 | if ( pixbuf ) | |
129 | g_object_unref (pixbuf); | |
130 | ||
131 | gtk_file_chooser_set_preview_widget_active(chooser, have_preview); | |
865bb325 VZ |
132 | } |
133 | ||
9f057af5 VZ |
134 | } // extern "C" |
135 | ||
38a36cf5 PC |
136 | //----------------------------------------------------------------------------- |
137 | // "size_request" from m_extraControl | |
138 | //----------------------------------------------------------------------------- | |
139 | ||
140 | extern "C" { | |
fd6c5101 | 141 | static void extra_widget_size_request(GtkWidget*, GtkRequisition* req, wxWindow* win) |
38a36cf5 PC |
142 | { |
143 | // allow dialog to be resized smaller horizontally | |
fd6c5101 | 144 | req->width = win->GetMinWidth(); |
38a36cf5 PC |
145 | } |
146 | } | |
147 | ||
3abfbb43 | 148 | static void wxInsertChildInFileDialog(wxWindow* parent, wxWindow* child) |
8ce68f7f | 149 | { |
3abfbb43 PC |
150 | g_signal_connect_after(child->m_widget, "size_request", |
151 | G_CALLBACK(extra_widget_size_request), child); | |
152 | gtk_file_chooser_set_extra_widget( | |
153 | GTK_FILE_CHOOSER(parent->m_widget), child->m_widget); | |
8ce68f7f VZ |
154 | } |
155 | ||
291a8f20 RR |
156 | //----------------------------------------------------------------------------- |
157 | // wxFileDialog | |
158 | //----------------------------------------------------------------------------- | |
159 | ||
700d08c1 | 160 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase) |
4e1901b7 | 161 | |
700d08c1 | 162 | BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase) |
f8bc53eb | 163 | EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk) |
f1d5aa4e | 164 | EVT_SIZE(wxFileDialog::OnSize) |
4e1901b7 | 165 | END_EVENT_TABLE() |
c801d85f | 166 | |
9755e73b VS |
167 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, |
168 | const wxString& defaultDir, | |
169 | const wxString& defaultFileName, | |
170 | const wxString& wildCard, | |
ff3e84ff VZ |
171 | long style, const wxPoint& pos, |
172 | const wxSize& sz, | |
173 | const wxString& name) | |
700d08c1 | 174 | : wxFileDialogBase() |
c801d85f | 175 | { |
8ce68f7f | 176 | m_insertCallback = wxInsertChildInFileDialog; |
700d08c1 | 177 | parent = GetParentForModalDialog(parent); |
f2448f21 | 178 | |
700d08c1 RR |
179 | if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName, |
180 | wildCard, style, pos, sz, name)) | |
77f70672 | 181 | { |
9f057af5 VZ |
182 | return; |
183 | } | |
83624f79 | 184 | |
9f057af5 VZ |
185 | if (!PreCreation(parent, pos, wxDefaultSize) || |
186 | !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style, | |
187 | wxDefaultValidator, wxT("filedialog"))) | |
188 | { | |
189 | wxFAIL_MSG( wxT("wxFileDialog creation failed") ); | |
190 | return; | |
191 | } | |
3f6638b8 | 192 | |
9f057af5 VZ |
193 | GtkFileChooserAction gtk_action; |
194 | GtkWindow* gtk_parent = NULL; | |
195 | if (parent) | |
196 | gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) ); | |
27b2dd53 | 197 | |
9f057af5 VZ |
198 | const gchar* ok_btn_stock; |
199 | if ( style & wxFD_SAVE ) | |
200 | { | |
201 | gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE; | |
202 | ok_btn_stock = GTK_STOCK_SAVE; | |
203 | } | |
204 | else | |
205 | { | |
206 | gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN; | |
207 | ok_btn_stock = GTK_STOCK_OPEN; | |
208 | } | |
f8bc53eb | 209 | |
9f057af5 VZ |
210 | m_widget = gtk_file_chooser_dialog_new( |
211 | wxGTK_CONV(m_message), | |
212 | gtk_parent, | |
213 | gtk_action, | |
214 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | |
215 | ok_btn_stock, GTK_RESPONSE_ACCEPT, | |
216 | NULL); | |
9ff9d30c | 217 | g_object_ref(m_widget); |
f2448f21 | 218 | GtkFileChooser* file_chooser = GTK_FILE_CHOOSER(m_widget); |
9755e73b | 219 | |
f2448f21 | 220 | m_fc.SetWidget(file_chooser); |
0cf3e587 | 221 | |
9f057af5 | 222 | gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT); |
c1dfe97c | 223 | |
9f057af5 | 224 | if ( style & wxFD_MULTIPLE ) |
f2448f21 | 225 | gtk_file_chooser_set_select_multiple(file_chooser, true); |
27b2dd53 | 226 | |
bf345206 VZ |
227 | // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically |
228 | // destroys the dialog when the user press ESC on the dialog: in that case | |
229 | // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL | |
230 | // errors... | |
f2448f21 | 231 | g_signal_connect(m_widget, |
9f057af5 VZ |
232 | "delete_event", |
233 | G_CALLBACK (gtk_widget_hide_on_delete), | |
f2448f21 | 234 | this); |
a552d120 | 235 | |
bf345206 VZ |
236 | // local-only property could be set to false to allow non-local files to be |
237 | // loaded. In that case get/set_uri(s) should be used instead of | |
238 | // get/set_filename(s) everywhere and the GtkFileChooserDialog should | |
239 | // probably also be created with a backend, e.g "gnome-vfs", "default", ... | |
240 | // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept | |
241 | // as the default - true: | |
9f057af5 | 242 | // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true); |
27b2dd53 | 243 | |
9f057af5 VZ |
244 | g_signal_connect (m_widget, "response", |
245 | G_CALLBACK (gtk_filedialog_response_callback), this); | |
27b2dd53 | 246 | |
9f057af5 | 247 | SetWildcard(wildCard); |
f8bc53eb | 248 | |
bf345206 VZ |
249 | // if defaultDir is specified it should contain the directory and |
250 | // defaultFileName should contain the default name of the file, however if | |
251 | // directory is not given, defaultFileName contains both | |
252 | wxFileName fn; | |
253 | if ( defaultDir.empty() ) | |
254 | fn.Assign(defaultFileName); | |
255 | else if ( !defaultFileName.empty() ) | |
256 | fn.Assign(defaultDir, defaultFileName); | |
f241631e RD |
257 | else |
258 | fn.AssignDir(defaultDir); | |
bf345206 VZ |
259 | |
260 | // set the initial file name and/or directory | |
78355ffc | 261 | fn.MakeAbsolute(); // GTK+ needs absolute path |
3a41827a VZ |
262 | const wxString dir = fn.GetPath(); |
263 | if ( !dir.empty() ) | |
9f057af5 | 264 | { |
f2448f21 | 265 | gtk_file_chooser_set_current_folder(file_chooser, dir.fn_str()); |
bf345206 | 266 | } |
f8bc53eb | 267 | |
3a41827a | 268 | const wxString fname = fn.GetFullName(); |
bf345206 VZ |
269 | if ( style & wxFD_SAVE ) |
270 | { | |
271 | if ( !fname.empty() ) | |
272 | { | |
f2448f21 | 273 | gtk_file_chooser_set_current_name(file_chooser, fname.fn_str()); |
bf345206 | 274 | } |
b5ab6d19 MR |
275 | |
276 | #if GTK_CHECK_VERSION(2,7,3) | |
9f057af5 | 277 | if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3)) |
f2448f21 | 278 | gtk_file_chooser_set_do_overwrite_confirmation(file_chooser, true); |
b5ab6d19 | 279 | #endif |
9f057af5 VZ |
280 | } |
281 | else // wxFD_OPEN | |
282 | { | |
bf345206 | 283 | if ( !fname.empty() ) |
f8bc53eb | 284 | { |
f2448f21 | 285 | gtk_file_chooser_set_filename(file_chooser, |
bf345206 | 286 | fn.GetFullPath().fn_str()); |
f8bc53eb | 287 | } |
77f70672 | 288 | } |
9f057af5 | 289 | |
9f057af5 VZ |
290 | if ( style & wxFD_PREVIEW ) |
291 | { | |
292 | GtkWidget *previewImage = gtk_image_new(); | |
293 | ||
f2448f21 | 294 | gtk_file_chooser_set_preview_widget(file_chooser, previewImage); |
9f057af5 VZ |
295 | g_signal_connect(m_widget, "update-preview", |
296 | G_CALLBACK(gtk_filedialog_update_preview_callback), | |
297 | previewImage); | |
298 | } | |
9755e73b | 299 | } |
0e1399b3 | 300 | |
9ff9d30c PC |
301 | wxFileDialog::~wxFileDialog() |
302 | { | |
303 | if (m_extraControl) | |
304 | { | |
305 | // get chooser to drop its reference right now, allowing wxWindow dtor | |
306 | // to verify that ref count drops to zero | |
307 | gtk_file_chooser_set_extra_widget( | |
308 | GTK_FILE_CHOOSER(m_widget), NULL); | |
309 | } | |
310 | } | |
311 | ||
ff654490 | 312 | void wxFileDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event)) |
4e1901b7 | 313 | { |
700d08c1 | 314 | EndDialog(wxID_OK); |
4e1901b7 RR |
315 | } |
316 | ||
317 | int wxFileDialog::ShowModal() | |
318 | { | |
3abfbb43 | 319 | CreateExtraControl(); |
8ce68f7f VZ |
320 | |
321 | return wxDialog::ShowModal(); | |
9755e73b | 322 | } |
0e1399b3 | 323 | |
700d08c1 RR |
324 | void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y), |
325 | int WXUNUSED(width), int WXUNUSED(height), | |
326 | int WXUNUSED(sizeFlags)) | |
5b2e23bf | 327 | { |
5b2e23bf RR |
328 | } |
329 | ||
f1d5aa4e PC |
330 | void wxFileDialog::OnSize(wxSizeEvent&) |
331 | { | |
332 | // avoid calling DoLayout(), which will set the (wrong) size of | |
333 | // m_extraControl, its size is managed by GtkFileChooser | |
334 | } | |
335 | ||
f8bc53eb JS |
336 | wxString wxFileDialog::GetPath() const |
337 | { | |
700d08c1 | 338 | return m_fc.GetPath(); |
f8bc53eb JS |
339 | } |
340 | ||
27b2dd53 | 341 | void wxFileDialog::GetFilenames(wxArrayString& files) const |
9755e73b | 342 | { |
700d08c1 | 343 | m_fc.GetFilenames( files ); |
9755e73b | 344 | } |
76840ed0 | 345 | |
27b2dd53 | 346 | void wxFileDialog::GetPaths(wxArrayString& paths) const |
9755e73b | 347 | { |
700d08c1 | 348 | m_fc.GetPaths( paths ); |
9755e73b | 349 | } |
035b704a | 350 | |
9755e73b VS |
351 | void wxFileDialog::SetMessage(const wxString& message) |
352 | { | |
700d08c1 RR |
353 | m_message = message; |
354 | SetTitle(message); | |
9755e73b VS |
355 | } |
356 | ||
76840ed0 RR |
357 | void wxFileDialog::SetPath(const wxString& path) |
358 | { | |
700d08c1 | 359 | m_fc.SetPath( path ); |
76840ed0 RR |
360 | } |
361 | ||
9755e73b VS |
362 | void wxFileDialog::SetDirectory(const wxString& dir) |
363 | { | |
700d08c1 | 364 | m_fc.SetDirectory( dir ); |
9755e73b VS |
365 | } |
366 | ||
f8bc53eb JS |
367 | wxString wxFileDialog::GetDirectory() const |
368 | { | |
700d08c1 | 369 | return m_fc.GetDirectory(); |
f8bc53eb JS |
370 | } |
371 | ||
9755e73b VS |
372 | void wxFileDialog::SetFilename(const wxString& name) |
373 | { | |
700d08c1 RR |
374 | if (HasFdFlag(wxFD_SAVE)) |
375 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name)); | |
77f70672 | 376 | else |
700d08c1 | 377 | SetPath(wxFileName(GetDirectory(), name).GetFullPath()); |
9755e73b | 378 | } |
035b704a | 379 | |
f8bc53eb JS |
380 | wxString wxFileDialog::GetFilename() const |
381 | { | |
700d08c1 | 382 | return m_fc.GetFilename(); |
f8bc53eb JS |
383 | } |
384 | ||
9755e73b VS |
385 | void wxFileDialog::SetWildcard(const wxString& wildCard) |
386 | { | |
700d08c1 | 387 | m_fc.SetWildcard( wildCard ); |
9755e73b | 388 | } |
a3622daa | 389 | |
9755e73b VS |
390 | void wxFileDialog::SetFilterIndex(int filterIndex) |
391 | { | |
ff3e84ff | 392 | |
700d08c1 | 393 | m_fc.SetFilterIndex( filterIndex); |
4e1901b7 RR |
394 | } |
395 | ||
f8bc53eb | 396 | int wxFileDialog::GetFilterIndex() const |
4e1901b7 | 397 | { |
700d08c1 | 398 | return m_fc.GetFilterIndex(); |
9755e73b | 399 | } |
3f6638b8 | 400 | |
700d08c1 | 401 | #endif // wxUSE_FILEDLG |