wxFD_PREVIEW implementation for wxGTK (slightly modified patch 1520690)
[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 // idle system
33 //-----------------------------------------------------------------------------
34
35 extern void wxapp_install_idle_handler();
36
37 //-----------------------------------------------------------------------------
38 // "clicked" for OK-button
39 //-----------------------------------------------------------------------------
40
41 extern "C" {
42 static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
43 {
44 int style = dialog->GetWindowStyle();
45 gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
46
47 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
48 #if GTK_CHECK_VERSION(2,7,3)
49 if(gtk_check_version(2,7,3) != NULL)
50 #endif
51 if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
52 {
53 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
54 {
55 wxString msg;
56
57 msg.Printf(
58 _("File '%s' already exists, do you really want to overwrite it?"),
59 wxString(wxConvFileName->cMB2WX(filename)).c_str());
60
61 wxMessageDialog dlg(dialog, msg, _("Confirm"),
62 wxYES_NO | wxICON_QUESTION);
63 if (dlg.ShowModal() != wxID_YES)
64 return;
65 }
66 }
67
68 // change to the directory where the user went if asked
69 if (style & wxFD_CHANGE_DIR)
70 {
71 // Use chdir to not care about filename encodings
72 gchar* folder = g_path_get_dirname(filename);
73 chdir(folder);
74 g_free(folder);
75 }
76
77 g_free(filename);
78
79 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
80 event.SetEventObject(dialog);
81 dialog->GetEventHandler()->ProcessEvent(event);
82 }
83 }
84
85 //-----------------------------------------------------------------------------
86 // "clicked" for Cancel-button
87 //-----------------------------------------------------------------------------
88
89 extern "C"
90 {
91
92 static void gtk_filedialog_cancel_callback(GtkWidget *w, wxFileDialog *dialog)
93 {
94 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
95 event.SetEventObject(dialog);
96 dialog->GetEventHandler()->ProcessEvent(event);
97 }
98
99 static void gtk_filedialog_response_callback(GtkWidget *w,
100 gint response,
101 wxFileDialog *dialog)
102 {
103 wxapp_install_idle_handler();
104
105 if (response == GTK_RESPONSE_ACCEPT)
106 gtk_filedialog_ok_callback(w, dialog);
107 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
108 gtk_filedialog_cancel_callback(w, dialog);
109 }
110
111 static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser,
112 gpointer user_data)
113 {
114 #if GTK_CHECK_VERSION(2,4,0)
115 GtkWidget *preview = GTK_WIDGET(user_data);
116 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
117 if ( !filename )
118 return;
119
120 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL);
121 gboolean have_preview = pixbuf != NULL;
122
123 gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf);
124 if ( pixbuf )
125 g_object_unref (pixbuf);
126
127 gtk_file_chooser_set_preview_widget_active(chooser, have_preview);
128 #else
129 wxUnusedVar(chooser);
130 wxUnusedVar(user_data);
131 #endif // GTK+ 2.4+
132 }
133
134 } // extern "C"
135
136
137 //-----------------------------------------------------------------------------
138 // wxFileDialog
139 //-----------------------------------------------------------------------------
140
141 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog)
142
143 BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog)
144 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
145 END_EVENT_TABLE()
146
147 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
148 const wxString& defaultDir,
149 const wxString& defaultFileName,
150 const wxString& wildCard,
151 long style, const wxPoint& pos,
152 const wxSize& sz,
153 const wxString& name)
154 : wxGenericFileDialog(parent, message, defaultDir, defaultFileName,
155 wildCard, style, pos, sz, name, true )
156 {
157 if (gtk_check_version(2,4,0))
158 {
159 wxGenericFileDialog::Create( parent, message, defaultDir,
160 defaultFileName, wildCard, style, pos );
161 return;
162 }
163
164 m_needParent = false;
165
166 if (!PreCreation(parent, pos, wxDefaultSize) ||
167 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
168 wxDefaultValidator, wxT("filedialog")))
169 {
170 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
171 return;
172 }
173
174 GtkFileChooserAction gtk_action;
175 GtkWindow* gtk_parent = NULL;
176 if (parent)
177 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
178
179 const gchar* ok_btn_stock;
180 if ( style & wxFD_SAVE )
181 {
182 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
183 ok_btn_stock = GTK_STOCK_SAVE;
184 }
185 else
186 {
187 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
188 ok_btn_stock = GTK_STOCK_OPEN;
189 }
190
191 m_widget = gtk_file_chooser_dialog_new(
192 wxGTK_CONV(m_message),
193 gtk_parent,
194 gtk_action,
195 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
196 ok_btn_stock, GTK_RESPONSE_ACCEPT,
197 NULL);
198
199 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
200
201 if ( style & wxFD_MULTIPLE )
202 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true);
203
204 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
205 // the dialog when the user press ESC on the dialog: in that case a second call to
206 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
207 g_signal_connect (G_OBJECT(m_widget),
208 "delete_event",
209 G_CALLBACK (gtk_widget_hide_on_delete),
210 (gpointer)this);
211
212 // local-only property could be set to false to allow non-local files to be loaded.
213 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
214 // and the GtkFileChooserDialog should probably also be created with a backend,
215 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
216 // Currently local-only is kept as the default - true:
217 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
218
219 g_signal_connect (m_widget, "response",
220 G_CALLBACK (gtk_filedialog_response_callback), this);
221
222 SetWildcard(wildCard);
223
224 if ( style & wxFD_SAVE )
225 {
226 if ( !defaultDir.empty() )
227 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
228 wxConvFileName->cWX2MB(defaultDir));
229
230 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
231 wxConvFileName->cWX2MB(defaultFileName));
232
233 #if GTK_CHECK_VERSION(2,7,3)
234 if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3))
235 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget), TRUE);
236 #endif
237 }
238 else // wxFD_OPEN
239 {
240 if ( !defaultFileName.empty() )
241 {
242 wxString dir;
243 if ( defaultDir.empty() )
244 dir = ::wxGetCwd();
245 else
246 dir = defaultDir;
247
248 gtk_file_chooser_set_filename(
249 GTK_FILE_CHOOSER(m_widget),
250 wxConvFileName->cWX2MB( wxFileName(dir, defaultFileName).GetFullPath() ) );
251 }
252 else if ( !defaultDir.empty() )
253 {
254 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget),
255 wxConvFileName->cWX2MB(defaultDir) );
256 }
257 }
258
259 #if GTK_CHECK_VERSION(2,4,0)
260 if ( style & wxFD_PREVIEW )
261 {
262 GtkWidget *previewImage = gtk_image_new();
263
264 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget),
265 previewImage);
266 g_signal_connect(m_widget, "update-preview",
267 G_CALLBACK(gtk_filedialog_update_preview_callback),
268 previewImage);
269 }
270 #endif // GTK+ 2.4+
271 }
272
273 void wxFileDialog::OnFakeOk( wxCommandEvent &event )
274 {
275 if (!gtk_check_version(2,4,0))
276 wxDialog::OnOK( event );
277 else
278 wxGenericFileDialog::OnListOk( event );
279 }
280
281 int wxFileDialog::ShowModal()
282 {
283 if (!gtk_check_version(2,4,0))
284 return wxDialog::ShowModal();
285 else
286 return wxGenericFileDialog::ShowModal();
287 }
288
289 bool wxFileDialog::Show( bool show )
290 {
291 if (!gtk_check_version(2,4,0))
292 return wxDialog::Show( show );
293 else
294 return wxGenericFileDialog::Show( show );
295 }
296
297 void wxFileDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags )
298 {
299 if (!m_wxwindow)
300 return;
301 else
302 wxGenericFileDialog::DoSetSize( x, y, width, height, sizeFlags );
303 }
304
305 wxString wxFileDialog::GetPath() const
306 {
307 if (!gtk_check_version(2,4,0))
308 return wxConvFileName->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget)));
309 else
310 return wxGenericFileDialog::GetPath();
311 }
312
313 void wxFileDialog::GetFilenames(wxArrayString& files) const
314 {
315 if (!gtk_check_version(2,4,0))
316 {
317 GetPaths(files);
318 for (size_t n = 0; n < files.GetCount(); ++n )
319 {
320 wxFileName file(files[n]);
321 files[n] = file.GetFullName();
322 }
323 }
324 else
325 wxGenericFileDialog::GetFilenames( files );
326 }
327
328 void wxFileDialog::GetPaths(wxArrayString& paths) const
329 {
330 if (!gtk_check_version(2,4,0))
331 {
332 paths.Empty();
333 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget)))
334 {
335 GSList *gpathsi = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget));
336 GSList *gpaths = gpathsi;
337 while (gpathsi)
338 {
339 wxString file(wxConvFileName->cMB2WX((gchar*) gpathsi->data));
340 paths.Add(file);
341 g_free(gpathsi->data);
342 gpathsi = gpathsi->next;
343 }
344
345 g_slist_free(gpaths);
346 }
347 else
348 paths.Add(GetPath());
349 }
350 else
351 wxGenericFileDialog::GetPaths( paths );
352 }
353
354 void wxFileDialog::SetMessage(const wxString& message)
355 {
356 if (!gtk_check_version(2,4,0))
357 {
358 m_message = message;
359 SetTitle(message);
360 }
361 else
362 wxGenericFileDialog::SetMessage( message );
363 }
364
365 void wxFileDialog::SetPath(const wxString& path)
366 {
367 if (!gtk_check_version(2,4,0))
368 {
369 if (path.empty()) return;
370
371 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(path));
372 }
373 else
374 wxGenericFileDialog::SetPath( path );
375 }
376
377 void wxFileDialog::SetDirectory(const wxString& dir)
378 {
379 if (!gtk_check_version(2,4,0))
380 {
381 if (wxDirExists(dir))
382 {
383 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir));
384 }
385 }
386 else
387 wxGenericFileDialog::SetDirectory( dir );
388 }
389
390 wxString wxFileDialog::GetDirectory() const
391 {
392 if (!gtk_check_version(2,4,0))
393 return wxConvFileName->cMB2WX(
394 gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget) ) );
395 else
396 return wxGenericFileDialog::GetDirectory();
397 }
398
399 void wxFileDialog::SetFilename(const wxString& name)
400 {
401 if (!gtk_check_version(2,4,0))
402 {
403 if (HasFlag(wxFD_SAVE))
404 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(name));
405 else
406 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
407 }
408 else
409 wxGenericFileDialog::SetFilename( name );
410 }
411
412 wxString wxFileDialog::GetFilename() const
413 {
414 if (!gtk_check_version(2,4,0))
415 return wxFileName(
416 wxConvFileName->cMB2WX(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget))) ).GetFullName();
417 else
418 return wxGenericFileDialog::GetFilename();
419 }
420
421 void wxFileDialog::SetWildcard(const wxString& wildCard)
422 {
423 if (!gtk_check_version(2,4,0))
424 {
425 // parse filters
426 wxArrayString wildDescriptions, wildFilters;
427 if (!wxParseCommonDialogsFilter(wildCard, wildDescriptions, wildFilters))
428 {
429 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
430 }
431 else
432 {
433 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
434 m_wildCard = wildCard;
435
436 GtkFileChooser* chooser = GTK_FILE_CHOOSER(m_widget);
437
438 // empty current filter list:
439 GSList* ifilters = gtk_file_chooser_list_filters(chooser);
440 GSList* filters = ifilters;
441
442 while (ifilters)
443 {
444 gtk_file_chooser_remove_filter(chooser,GTK_FILE_FILTER(ifilters->data));
445 ifilters = ifilters->next;
446 }
447 g_slist_free(filters);
448
449 // add parsed to GtkChooser
450 for (size_t n = 0; n < wildFilters.GetCount(); ++n)
451 {
452 GtkFileFilter* filter = gtk_file_filter_new();
453 gtk_file_filter_set_name(filter, wxGTK_CONV(wildDescriptions[n]));
454
455 wxStringTokenizer exttok(wildFilters[n], wxT(";"));
456 while (exttok.HasMoreTokens())
457 {
458 wxString token = exttok.GetNextToken();
459 gtk_file_filter_add_pattern(filter, wxGTK_CONV(token));
460 }
461
462 gtk_file_chooser_add_filter(chooser, filter);
463 }
464
465 // Reset the filter index
466 SetFilterIndex(0);
467 }
468 }
469 else
470 wxGenericFileDialog::SetWildcard( wildCard );
471 }
472
473 void wxFileDialog::SetFilterIndex(int filterIndex)
474 {
475
476 if (!gtk_check_version(2,4,0))
477 {
478 gpointer filter;
479 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
480 GSList *filters = gtk_file_chooser_list_filters(chooser);
481
482 filter = g_slist_nth_data(filters, filterIndex);
483
484 if (filter != NULL)
485 {
486 gtk_file_chooser_set_filter(chooser, GTK_FILE_FILTER(filter));
487 }
488 else
489 {
490 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
491 }
492
493 g_slist_free(filters);
494 }
495 else
496 wxGenericFileDialog::SetFilterIndex( filterIndex );
497 }
498
499 int wxFileDialog::GetFilterIndex() const
500 {
501 if (!gtk_check_version(2,4,0))
502 {
503 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
504 GtkFileFilter *filter = gtk_file_chooser_get_filter(chooser);
505 GSList *filters = gtk_file_chooser_list_filters(chooser);
506 gint index = g_slist_index(filters, filter);
507 g_slist_free(filters);
508
509 if (index == -1)
510 {
511 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
512 return 0;
513 }
514 else
515 return index;
516 }
517 else
518 return wxGenericFileDialog::GetFilterIndex();
519 }
520
521 #endif // wxUSE_FILEDLG && __WXGTK24__