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