use -Wunused-parameter with gcc for consistency with MSVC and other compilers which...
[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 && defined(__WXGTK24__)
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 #include <unistd.h> // chdir
26
27 #include "wx/filename.h" // wxFilename
28 #include "wx/tokenzr.h" // wxStringTokenizer
29 #include "wx/filefn.h" // ::wxGetCwd
30
31 //-----------------------------------------------------------------------------
32 // "clicked" for OK-button
33 //-----------------------------------------------------------------------------
34
35 extern "C" {
36 static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
37 {
38 int style = dialog->GetWindowStyle();
39 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
40
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
45 if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
46 {
47 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
48 {
49 wxString msg;
50
51 msg.Printf(
52 _("File '%s' already exists, do you really want to overwrite it?"),
53 wxString(filename, *wxConvFileName));
54
55 wxMessageDialog dlg(dialog, msg, _("Confirm"),
56 wxYES_NO | wxICON_QUESTION);
57 if (dlg.ShowModal() != wxID_YES)
58 return;
59 }
60 }
61
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
73 // change to the directory where the user went if asked
74 if (style & wxFD_CHANGE_DIR)
75 {
76 // Use chdir to not care about filename encodings
77 wxGtkString folder(g_path_get_dirname(filename));
78 chdir(folder);
79 }
80
81 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
82 event.SetEventObject(dialog);
83 dialog->GetEventHandler()->ProcessEvent(event);
84 }
85 }
86
87 //-----------------------------------------------------------------------------
88 // "clicked" for Cancel-button
89 //-----------------------------------------------------------------------------
90
91 extern "C"
92 {
93
94 static void
95 gtk_filedialog_cancel_callback(GtkWidget * WXUNUSED(w), wxFileDialog *dialog)
96 {
97 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
98 event.SetEventObject(dialog);
99 dialog->GetEventHandler()->ProcessEvent(event);
100 }
101
102 static void gtk_filedialog_response_callback(GtkWidget *w,
103 gint response,
104 wxFileDialog *dialog)
105 {
106 if (response == GTK_RESPONSE_ACCEPT)
107 gtk_filedialog_ok_callback(w, dialog);
108 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
109 gtk_filedialog_cancel_callback(w, dialog);
110 }
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);
117
118 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
119
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+
135 }
136
137 } // extern "C"
138
139
140 //-----------------------------------------------------------------------------
141 // wxFileDialog
142 //-----------------------------------------------------------------------------
143
144 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog)
145
146 BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog)
147 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
148 END_EVENT_TABLE()
149
150 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
151 const wxString& defaultDir,
152 const wxString& defaultFileName,
153 const wxString& wildCard,
154 long style, const wxPoint& pos,
155 const wxSize& sz,
156 const wxString& name)
157 : wxGenericFileDialog(parent, message, defaultDir, defaultFileName,
158 wildCard, style, pos, sz, name, true )
159 {
160 if (gtk_check_version(2,4,0))
161 {
162 wxGenericFileDialog::Create( parent, message, defaultDir,
163 defaultFileName, wildCard, style, pos );
164 return;
165 }
166
167 parent = GetParentForModalDialog(parent);
168
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 }
176
177 GtkFileChooserAction gtk_action;
178 GtkWindow* gtk_parent = NULL;
179 if (parent)
180 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
181
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 }
193
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);
201
202 m_fc.SetWidget( GTK_FILE_CHOOSER(m_widget) );
203
204 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
205
206 if ( style & wxFD_MULTIPLE )
207 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true);
208
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...
213 g_signal_connect (G_OBJECT(m_widget),
214 "delete_event",
215 G_CALLBACK (gtk_widget_hide_on_delete),
216 (gpointer)this);
217
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:
224 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
225
226 g_signal_connect (m_widget, "response",
227 G_CALLBACK (gtk_filedialog_response_callback), this);
228
229 SetWildcard(wildCard);
230
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);
239 else
240 fn.AssignDir(defaultDir);
241
242 // set the initial file name and/or directory
243 fn.MakeAbsolute(); // GTK+ needs absolute path
244 const wxString dir = fn.GetPath();
245 if ( !dir.empty() )
246 {
247 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
248 dir.fn_str());
249 }
250
251 const wxString fname = fn.GetFullName();
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 }
259
260 #if GTK_CHECK_VERSION(2,7,3)
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);
263 #endif
264 }
265 else // wxFD_OPEN
266 {
267 if ( !fname.empty() )
268 {
269 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
270 fn.GetFullPath().fn_str());
271 }
272 }
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+
286 }
287
288 void wxFileDialog::OnFakeOk( wxCommandEvent &event )
289 {
290 if (!gtk_check_version(2,4,0))
291 EndDialog(wxID_OK);
292 else
293 wxGenericFileDialog::OnOk( event );
294 }
295
296 int wxFileDialog::ShowModal()
297 {
298 if (!gtk_check_version(2,4,0))
299 return wxDialog::ShowModal();
300 else
301 return wxGenericFileDialog::ShowModal();
302 }
303
304 bool wxFileDialog::Show( bool show )
305 {
306 if (!gtk_check_version(2,4,0))
307 return wxDialog::Show( show );
308 else
309 return wxGenericFileDialog::Show( show );
310 }
311
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
320 wxString wxFileDialog::GetPath() const
321 {
322 if (!gtk_check_version(2,4,0))
323 {
324 return m_fc.GetPath();
325 }
326
327 return wxGenericFileDialog::GetPath();
328 }
329
330 void wxFileDialog::GetFilenames(wxArrayString& files) const
331 {
332 if (!gtk_check_version(2,4,0))
333 {
334 m_fc.GetFilenames( files );
335 }
336 else
337 wxGenericFileDialog::GetFilenames( files );
338 }
339
340 void wxFileDialog::GetPaths(wxArrayString& paths) const
341 {
342 if (!gtk_check_version(2,4,0))
343 {
344 m_fc.GetPaths( paths );
345 }
346 else
347 wxGenericFileDialog::GetPaths( paths );
348 }
349
350 void wxFileDialog::SetMessage(const wxString& message)
351 {
352 if (!gtk_check_version(2,4,0))
353 {
354 m_message = message;
355 SetTitle(message);
356 }
357 else
358 wxGenericFileDialog::SetMessage( message );
359 }
360
361 void wxFileDialog::SetPath(const wxString& path)
362 {
363 if (!gtk_check_version(2,4,0))
364 {
365 m_fc.SetPath( path );
366 }
367 else
368 wxGenericFileDialog::SetPath( path );
369 }
370
371 void wxFileDialog::SetDirectory(const wxString& dir)
372 {
373 if (!gtk_check_version(2,4,0))
374 {
375 m_fc.SetDirectory( dir );
376 }
377 else
378 wxGenericFileDialog::SetDirectory( dir );
379 }
380
381 wxString wxFileDialog::GetDirectory() const
382 {
383 if (!gtk_check_version(2,4,0))
384 {
385 m_fc.GetDirectory();
386 }
387
388 return wxGenericFileDialog::GetDirectory();
389 }
390
391 void wxFileDialog::SetFilename(const wxString& name)
392 {
393 if (!gtk_check_version(2,4,0))
394 {
395 if (HasFdFlag(wxFD_SAVE))
396 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
397 else
398 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
399 }
400 else
401 wxGenericFileDialog::SetFilename( name );
402 }
403
404 wxString wxFileDialog::GetFilename() const
405 {
406 if (!gtk_check_version(2,4,0))
407 return m_fc.GetFilename();
408 else
409 return wxGenericFileDialog::GetFilename();
410 }
411
412 void wxFileDialog::SetWildcard(const wxString& wildCard)
413 {
414 if (!gtk_check_version(2,4,0))
415 {
416 m_fc.SetWildcard( wildCard );
417 }
418 else
419 wxGenericFileDialog::SetWildcard( wildCard );
420 }
421
422 void wxFileDialog::SetFilterIndex(int filterIndex)
423 {
424
425 if (!gtk_check_version(2,4,0))
426 {
427 m_fc.SetFilterIndex( filterIndex);
428 }
429 else
430 wxGenericFileDialog::SetFilterIndex( filterIndex );
431 }
432
433 int wxFileDialog::GetFilterIndex() const
434 {
435 if (!gtk_check_version(2,4,0))
436 {
437 return m_fc.GetFilterIndex();
438 }
439 else
440 return wxGenericFileDialog::GetFilterIndex();
441 }
442
443 #endif // wxUSE_FILEDLG && __WXGTK24__