]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/filedlg.cpp
updated setup.h for OpenVMS
[wxWidgets.git] / src / gtk / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/filedlg.cpp
3 // Purpose: native implementation of wxFileDialog
4 // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_FILEDLG
14
15 #include "wx/filedlg.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/msgdlg.h"
20 #endif
21
22 #include <gtk/gtk.h>
23 #include "wx/gtk/private.h"
24
25 #ifdef __UNIX__
26 #include <unistd.h> // chdir
27 #endif
28
29 #include "wx/filename.h" // wxFilename
30 #include "wx/tokenzr.h" // wxStringTokenizer
31 #include "wx/filefn.h" // ::wxGetCwd
32
33 //-----------------------------------------------------------------------------
34 // "clicked" for OK-button
35 //-----------------------------------------------------------------------------
36
37 extern "C" {
38 static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
39 {
40 int style = dialog->GetWindowStyle();
41 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
42
43 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
44 #ifndef __WXGTK3__
45 #if GTK_CHECK_VERSION(2,7,3)
46 if (gtk_check_version(2, 7, 3) != NULL)
47 #endif
48 {
49 if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
50 {
51 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
52 {
53 wxString msg;
54
55 msg.Printf(
56 _("File '%s' already exists, do you really want to overwrite it?"),
57 wxString::FromUTF8(filename));
58
59 wxMessageDialog dlg(dialog, msg, _("Confirm"),
60 wxYES_NO | wxICON_QUESTION);
61 if (dlg.ShowModal() != wxID_YES)
62 return;
63 }
64 }
65 }
66 #endif
67
68 if (style & wxFD_FILE_MUST_EXIST)
69 {
70 if ( !g_file_test(filename, G_FILE_TEST_EXISTS) )
71 {
72 wxMessageDialog dlg( dialog, _("Please choose an existing file."),
73 _("Error"), wxOK| wxICON_ERROR);
74 dlg.ShowModal();
75 return;
76 }
77 }
78
79 // change to the directory where the user went if asked
80 if (style & wxFD_CHANGE_DIR)
81 {
82 // Use chdir to not care about filename encodings
83 wxGtkString folder(g_path_get_dirname(filename));
84 chdir(folder);
85 }
86
87 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
88 event.SetEventObject(dialog);
89 dialog->HandleWindowEvent(event);
90 }
91 }
92
93 //-----------------------------------------------------------------------------
94 // "clicked" for Cancel-button
95 //-----------------------------------------------------------------------------
96
97 extern "C"
98 {
99
100 static void
101 gtk_filedialog_cancel_callback(GtkWidget * WXUNUSED(w), wxFileDialog *dialog)
102 {
103 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
104 event.SetEventObject(dialog);
105 dialog->HandleWindowEvent(event);
106 }
107
108 static void gtk_filedialog_response_callback(GtkWidget *w,
109 gint response,
110 wxFileDialog *dialog)
111 {
112 if (response == GTK_RESPONSE_ACCEPT)
113 gtk_filedialog_ok_callback(w, dialog);
114 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
115 gtk_filedialog_cancel_callback(w, dialog);
116 }
117
118 static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser,
119 gpointer user_data)
120 {
121 GtkWidget *preview = GTK_WIDGET(user_data);
122
123 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
124
125 if ( !filename )
126 return;
127
128 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL);
129 gboolean have_preview = pixbuf != NULL;
130
131 gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf);
132 if ( pixbuf )
133 g_object_unref (pixbuf);
134
135 gtk_file_chooser_set_preview_widget_active(chooser, have_preview);
136 }
137
138 } // extern "C"
139
140 void wxFileDialog::AddChildGTK(wxWindowGTK* child)
141 {
142 // allow dialog to be resized smaller horizontally
143 gtk_widget_set_size_request(
144 child->m_widget, child->GetMinWidth(), child->m_height);
145
146 gtk_file_chooser_set_extra_widget(
147 GTK_FILE_CHOOSER(m_widget), child->m_widget);
148 }
149
150 //-----------------------------------------------------------------------------
151 // wxFileDialog
152 //-----------------------------------------------------------------------------
153
154 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
155
156 BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase)
157 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
158 EVT_SIZE(wxFileDialog::OnSize)
159 END_EVENT_TABLE()
160
161 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
162 const wxString& defaultDir,
163 const wxString& defaultFileName,
164 const wxString& wildCard,
165 long style, const wxPoint& pos,
166 const wxSize& sz,
167 const wxString& name)
168 : wxFileDialogBase()
169 {
170 Create(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name);
171 }
172
173 bool wxFileDialog::Create(wxWindow *parent, const wxString& message,
174 const wxString& defaultDir,
175 const wxString& defaultFileName,
176 const wxString& wildCard,
177 long style, const wxPoint& pos,
178 const wxSize& sz,
179 const wxString& name)
180 {
181 parent = GetParentForModalDialog(parent, style);
182
183 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName,
184 wildCard, style, pos, sz, name))
185 {
186 return false;
187 }
188
189 if (!PreCreation(parent, pos, wxDefaultSize) ||
190 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
191 wxDefaultValidator, wxT("filedialog")))
192 {
193 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
194 return false;
195 }
196
197 GtkFileChooserAction gtk_action;
198 GtkWindow* gtk_parent = NULL;
199 if (parent)
200 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
201
202 const gchar* ok_btn_stock;
203 if ( style & wxFD_SAVE )
204 {
205 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
206 ok_btn_stock = GTK_STOCK_SAVE;
207 }
208 else
209 {
210 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
211 ok_btn_stock = GTK_STOCK_OPEN;
212 }
213
214 m_widget = gtk_file_chooser_dialog_new(
215 wxGTK_CONV(m_message),
216 gtk_parent,
217 gtk_action,
218 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
219 ok_btn_stock, GTK_RESPONSE_ACCEPT,
220 NULL);
221 g_object_ref(m_widget);
222 GtkFileChooser* file_chooser = GTK_FILE_CHOOSER(m_widget);
223
224 m_fc.SetWidget(file_chooser);
225
226 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
227
228 if ( style & wxFD_MULTIPLE )
229 gtk_file_chooser_set_select_multiple(file_chooser, true);
230
231 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
232 // destroys the dialog when the user press ESC on the dialog: in that case
233 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
234 // errors...
235 g_signal_connect(m_widget,
236 "delete_event",
237 G_CALLBACK (gtk_widget_hide_on_delete),
238 this);
239
240 // local-only property could be set to false to allow non-local files to be
241 // loaded. In that case get/set_uri(s) should be used instead of
242 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
243 // probably also be created with a backend, e.g. "gnome-vfs", "default", ...
244 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
245 // as the default - true:
246 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
247
248 g_signal_connect (m_widget, "response",
249 G_CALLBACK (gtk_filedialog_response_callback), this);
250
251
252 // deal with extensions/filters
253 SetWildcard(wildCard);
254
255 wxString defaultFileNameWithExt = defaultFileName;
256 if ( !wildCard.empty() && !defaultFileName.empty() &&
257 !wxFileName(defaultFileName).HasExt() )
258 {
259 // append the default extension to the initial file name: GTK won't do
260 // it for us by default (unlike e.g. MSW)
261 const wxString defaultExt = m_fc.GetCurrentWildCard().AfterFirst('.');
262 if ( defaultExt.find_first_of("?*") == wxString::npos )
263 defaultFileNameWithExt += "." + defaultExt;
264 }
265
266
267 // if defaultDir is specified it should contain the directory and
268 // defaultFileName should contain the default name of the file, however if
269 // directory is not given, defaultFileName contains both
270 wxFileName fn;
271 if ( defaultDir.empty() )
272 fn.Assign(defaultFileNameWithExt);
273 else if ( !defaultFileNameWithExt.empty() )
274 fn.Assign(defaultDir, defaultFileNameWithExt);
275 else
276 fn.AssignDir(defaultDir);
277
278 // set the initial file name and/or directory
279 fn.MakeAbsolute(); // GTK+ needs absolute path
280 const wxString dir = fn.GetPath();
281 if ( !dir.empty() )
282 {
283 gtk_file_chooser_set_current_folder(file_chooser, wxGTK_CONV_FN(dir));
284 }
285
286 const wxString fname = fn.GetFullName();
287 if ( style & wxFD_SAVE )
288 {
289 if ( !fname.empty() )
290 {
291 gtk_file_chooser_set_current_name(file_chooser, wxGTK_CONV_FN(fname));
292 }
293
294 #if GTK_CHECK_VERSION(2,7,3)
295 if ((style & wxFD_OVERWRITE_PROMPT)
296 #ifndef __WXGTK3__
297 && gtk_check_version(2,7,3) == NULL
298 #endif
299 )
300 {
301 gtk_file_chooser_set_do_overwrite_confirmation(file_chooser, true);
302 }
303 #endif
304 }
305 else // wxFD_OPEN
306 {
307 if ( !fname.empty() )
308 {
309 gtk_file_chooser_set_filename(file_chooser,
310 wxGTK_CONV_FN(fn.GetFullPath()));
311 }
312 }
313
314 if ( style & wxFD_PREVIEW )
315 {
316 GtkWidget *previewImage = gtk_image_new();
317
318 gtk_file_chooser_set_preview_widget(file_chooser, previewImage);
319 g_signal_connect(m_widget, "update-preview",
320 G_CALLBACK(gtk_filedialog_update_preview_callback),
321 previewImage);
322 }
323
324 return true;
325 }
326
327 wxFileDialog::~wxFileDialog()
328 {
329 if (m_extraControl)
330 {
331 // get chooser to drop its reference right now, allowing wxWindow dtor
332 // to verify that ref count drops to zero
333 gtk_file_chooser_set_extra_widget(
334 GTK_FILE_CHOOSER(m_widget), NULL);
335 }
336 }
337
338 void wxFileDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event))
339 {
340 EndDialog(wxID_OK);
341 }
342
343 int wxFileDialog::ShowModal()
344 {
345 CreateExtraControl();
346
347 return wxDialog::ShowModal();
348 }
349
350 void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
351 int WXUNUSED(width), int WXUNUSED(height),
352 int WXUNUSED(sizeFlags))
353 {
354 }
355
356 void wxFileDialog::OnSize(wxSizeEvent&)
357 {
358 // avoid calling DoLayout(), which will set the (wrong) size of
359 // m_extraControl, its size is managed by GtkFileChooser
360 }
361
362 wxString wxFileDialog::GetPath() const
363 {
364 return m_fc.GetPath();
365 }
366
367 void wxFileDialog::GetFilenames(wxArrayString& files) const
368 {
369 m_fc.GetFilenames( files );
370 }
371
372 void wxFileDialog::GetPaths(wxArrayString& paths) const
373 {
374 m_fc.GetPaths( paths );
375 }
376
377 void wxFileDialog::SetMessage(const wxString& message)
378 {
379 m_message = message;
380 SetTitle(message);
381 }
382
383 void wxFileDialog::SetPath(const wxString& path)
384 {
385 // Don't do anything if no path is specified, in particular don't set the
386 // path to m_dir below as this would result in opening the dialog in the
387 // parent directory of this one instead of m_dir itself.
388 if ( path.empty() )
389 return;
390
391 // we need an absolute path for GTK native chooser so ensure that we have
392 // it: use the initial directory if it was set or just CWD otherwise (this
393 // is the default behaviour if m_dir is empty)
394 wxFileName fn(path);
395 fn.MakeAbsolute(m_dir);
396 m_fc.SetPath(fn.GetFullPath());
397 }
398
399 void wxFileDialog::SetDirectory(const wxString& dir)
400 {
401 if (m_fc.SetDirectory( dir ))
402 {
403 // Cache the dir, as gtk_file_chooser_get_current_folder()
404 // doesn't return anything until the dialog has been shown
405 m_dir = dir;
406 }
407 }
408
409 wxString wxFileDialog::GetDirectory() const
410 {
411 wxString currentDir( m_fc.GetDirectory() );
412 if (currentDir.empty())
413 {
414 // m_fc.GetDirectory() will return empty until the dialog has been shown
415 // in which case use any previously provided value
416 currentDir = m_dir;
417 }
418 return currentDir;
419 }
420
421 void wxFileDialog::SetFilename(const wxString& name)
422 {
423 if (HasFdFlag(wxFD_SAVE))
424 {
425 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
426 m_fileName = name;
427 }
428
429 else
430 {
431 wxString path( GetDirectory() );
432 if (path.empty())
433 {
434 // SetPath() fires an assert if fed other than filepaths
435 return;
436 }
437 SetPath(wxFileName(path, name).GetFullPath());
438 m_fileName = name;
439 }
440 }
441
442 wxString wxFileDialog::GetFilename() const
443 {
444 wxString currentFilename( m_fc.GetFilename() );
445 if (currentFilename.empty())
446 {
447 // m_fc.GetFilename() will return empty until the dialog has been shown
448 // in which case use any previously provided value
449 currentFilename = m_fileName;
450 }
451 return currentFilename;
452 }
453
454 void wxFileDialog::SetWildcard(const wxString& wildCard)
455 {
456 wxFileDialogBase::SetWildcard(wildCard);
457 m_fc.SetWildcard( GetWildcard() );
458 }
459
460 void wxFileDialog::SetFilterIndex(int filterIndex)
461 {
462 m_fc.SetFilterIndex( filterIndex);
463 }
464
465 int wxFileDialog::GetFilterIndex() const
466 {
467 return m_fc.GetFilterIndex();
468 }
469
470 #endif // wxUSE_FILEDLG