]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/filedlg.cpp
62fc00864ac85c74969cabff6f65cbe39b8b1af8
[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 #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->HandleWindowEvent(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->HandleWindowEvent(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 GtkWidget *preview = GTK_WIDGET(user_data);
116
117 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
118
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 }
131
132 } // extern "C"
133
134 static void wxInsertChildInFileDialog(wxWindow* WXUNUSED(parent),
135 wxWindow* WXUNUSED(child))
136 {
137 }
138
139
140 //-----------------------------------------------------------------------------
141 // wxFileDialog
142 //-----------------------------------------------------------------------------
143
144 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
145
146 BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase)
147 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
148 EVT_SIZE(wxFileDialog::OnSize)
149 END_EVENT_TABLE()
150
151 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
152 const wxString& defaultDir,
153 const wxString& defaultFileName,
154 const wxString& wildCard,
155 long style, const wxPoint& pos,
156 const wxSize& sz,
157 const wxString& name)
158 : wxFileDialogBase()
159 {
160 m_insertCallback = wxInsertChildInFileDialog;
161 parent = GetParentForModalDialog(parent);
162
163 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName,
164 wildCard, style, pos, sz, name))
165 {
166 return;
167 }
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 ( 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 }
285
286
287 void wxFileDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event))
288 {
289 EndDialog(wxID_OK);
290 }
291
292 int wxFileDialog::ShowModal()
293 {
294 if (CreateExtraControl())
295 {
296 GtkWidget *control = m_extraControl->m_widget;
297
298 wxASSERT(control->parent == NULL);
299
300 gtk_widget_show(control);
301 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(m_widget), control);
302 }
303
304 return wxDialog::ShowModal();
305 }
306
307 void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
308 int WXUNUSED(width), int WXUNUSED(height),
309 int WXUNUSED(sizeFlags))
310 {
311 }
312
313 void wxFileDialog::OnSize(wxSizeEvent&)
314 {
315 // avoid calling DoLayout(), which will set the (wrong) size of
316 // m_extraControl, its size is managed by GtkFileChooser
317 }
318
319 wxString wxFileDialog::GetPath() const
320 {
321 return m_fc.GetPath();
322 }
323
324 void wxFileDialog::GetFilenames(wxArrayString& files) const
325 {
326 m_fc.GetFilenames( files );
327 }
328
329 void wxFileDialog::GetPaths(wxArrayString& paths) const
330 {
331 m_fc.GetPaths( paths );
332 }
333
334 void wxFileDialog::SetMessage(const wxString& message)
335 {
336 m_message = message;
337 SetTitle(message);
338 }
339
340 void wxFileDialog::SetPath(const wxString& path)
341 {
342 m_fc.SetPath( path );
343 }
344
345 void wxFileDialog::SetDirectory(const wxString& dir)
346 {
347 m_fc.SetDirectory( dir );
348 }
349
350 wxString wxFileDialog::GetDirectory() const
351 {
352 return m_fc.GetDirectory();
353 }
354
355 void wxFileDialog::SetFilename(const wxString& name)
356 {
357 if (HasFdFlag(wxFD_SAVE))
358 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
359 else
360 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
361 }
362
363 wxString wxFileDialog::GetFilename() const
364 {
365 return m_fc.GetFilename();
366 }
367
368 void wxFileDialog::SetWildcard(const wxString& wildCard)
369 {
370 m_fc.SetWildcard( wildCard );
371 }
372
373 void wxFileDialog::SetFilterIndex(int filterIndex)
374 {
375
376 m_fc.SetFilterIndex( filterIndex);
377 }
378
379 int wxFileDialog::GetFilterIndex() const
380 {
381 return m_fc.GetFilterIndex();
382 }
383
384 #endif // wxUSE_FILEDLG