]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/filedlg.cpp
move frame decorations drawing to the base class
[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
117 gchar *str = gtk_file_chooser_get_preview_filename(chooser);
118 wxGtkString filename(str);
119 if (str) g_free(str);
120
121 if ( !filename )
122 return;
123
124 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL);
125 gboolean have_preview = pixbuf != NULL;
126
127 gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf);
128 if ( pixbuf )
129 g_object_unref (pixbuf);
130
131 gtk_file_chooser_set_preview_widget_active(chooser, have_preview);
132 #else
133 wxUnusedVar(chooser);
134 wxUnusedVar(user_data);
135 #endif // GTK+ 2.4+
136 }
137
138 } // extern "C"
139
140
141 //-----------------------------------------------------------------------------
142 // wxFileDialog
143 //-----------------------------------------------------------------------------
144
145 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog)
146
147 BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog)
148 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
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 : wxGenericFileDialog(parent, message, defaultDir, defaultFileName,
159 wildCard, style, pos, sz, name, true )
160 {
161 if (gtk_check_version(2,4,0))
162 {
163 wxGenericFileDialog::Create( parent, message, defaultDir,
164 defaultFileName, wildCard, style, pos );
165 return;
166 }
167
168 m_needParent = false;
169
170 if (!PreCreation(parent, pos, wxDefaultSize) ||
171 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
172 wxDefaultValidator, wxT("filedialog")))
173 {
174 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
175 return;
176 }
177
178 GtkFileChooserAction gtk_action;
179 GtkWindow* gtk_parent = NULL;
180 if (parent)
181 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
182
183 const gchar* ok_btn_stock;
184 if ( style & wxFD_SAVE )
185 {
186 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
187 ok_btn_stock = GTK_STOCK_SAVE;
188 }
189 else
190 {
191 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
192 ok_btn_stock = GTK_STOCK_OPEN;
193 }
194
195 m_widget = gtk_file_chooser_dialog_new(
196 wxGTK_CONV(m_message),
197 gtk_parent,
198 gtk_action,
199 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
200 ok_btn_stock, GTK_RESPONSE_ACCEPT,
201 NULL);
202
203 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
204
205 if ( style & wxFD_MULTIPLE )
206 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true);
207
208 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
209 // the dialog when the user press ESC on the dialog: in that case a second call to
210 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
211 g_signal_connect (G_OBJECT(m_widget),
212 "delete_event",
213 G_CALLBACK (gtk_widget_hide_on_delete),
214 (gpointer)this);
215
216 // local-only property could be set to false to allow non-local files to be loaded.
217 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
218 // and the GtkFileChooserDialog should probably also be created with a backend,
219 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
220 // Currently local-only is kept as the default - true:
221 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
222
223 g_signal_connect (m_widget, "response",
224 G_CALLBACK (gtk_filedialog_response_callback), this);
225
226 SetWildcard(wildCard);
227
228 if ( style & wxFD_SAVE )
229 {
230 if ( !defaultDir.empty() )
231 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
232 wxConvFileName->cWX2MB(defaultDir));
233
234 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
235 wxGTK_CONV(defaultFileName));
236
237 #if GTK_CHECK_VERSION(2,7,3)
238 if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3))
239 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget), TRUE);
240 #endif
241 }
242 else // wxFD_OPEN
243 {
244 if ( !defaultFileName.empty() )
245 {
246 wxString dir;
247 if ( defaultDir.empty() )
248 dir = ::wxGetCwd();
249 else
250 dir = defaultDir;
251
252 gtk_file_chooser_set_filename(
253 GTK_FILE_CHOOSER(m_widget),
254 wxConvFileName->cWX2MB( wxFileName(dir, defaultFileName).GetFullPath() ) );
255 }
256 else if ( !defaultDir.empty() )
257 {
258 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget),
259 wxConvFileName->cWX2MB(defaultDir) );
260 }
261 }
262
263 #if GTK_CHECK_VERSION(2,4,0)
264 if ( style & wxFD_PREVIEW )
265 {
266 GtkWidget *previewImage = gtk_image_new();
267
268 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget),
269 previewImage);
270 g_signal_connect(m_widget, "update-preview",
271 G_CALLBACK(gtk_filedialog_update_preview_callback),
272 previewImage);
273 }
274 #endif // GTK+ 2.4+
275 }
276
277 void wxFileDialog::OnFakeOk( wxCommandEvent &event )
278 {
279 if (!gtk_check_version(2,4,0))
280 EndDialog(wxID_OK);
281 else
282 wxGenericFileDialog::OnListOk( event );
283 }
284
285 int wxFileDialog::ShowModal()
286 {
287 if (!gtk_check_version(2,4,0))
288 return wxDialog::ShowModal();
289 else
290 return wxGenericFileDialog::ShowModal();
291 }
292
293 bool wxFileDialog::Show( bool show )
294 {
295 if (!gtk_check_version(2,4,0))
296 return wxDialog::Show( show );
297 else
298 return wxGenericFileDialog::Show( show );
299 }
300
301 void wxFileDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags )
302 {
303 if (!m_wxwindow)
304 return;
305 else
306 wxGenericFileDialog::DoSetSize( x, y, width, height, sizeFlags );
307 }
308
309 wxString wxFileDialog::GetPath() const
310 {
311 if (!gtk_check_version(2,4,0))
312 {
313 gchar *str = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget));
314 wxString ret = wxConvFileName->cMB2WX(str);
315 if (str) g_free(str);
316
317 return ret;
318 }
319 else
320 return wxGenericFileDialog::GetPath();
321 }
322
323 void wxFileDialog::GetFilenames(wxArrayString& files) const
324 {
325 if (!gtk_check_version(2,4,0))
326 {
327 GetPaths(files);
328 for (size_t n = 0; n < files.GetCount(); ++n )
329 {
330 wxFileName file(files[n]);
331 files[n] = file.GetFullName();
332 }
333 }
334 else
335 wxGenericFileDialog::GetFilenames( files );
336 }
337
338 void wxFileDialog::GetPaths(wxArrayString& paths) const
339 {
340 if (!gtk_check_version(2,4,0))
341 {
342 paths.Empty();
343 if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget)))
344 {
345 GSList *gpathsi = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget));
346 GSList *gpaths = gpathsi;
347 while (gpathsi)
348 {
349 wxString file(wxConvFileName->cMB2WX((gchar*) gpathsi->data));
350 paths.Add(file);
351 g_free(gpathsi->data);
352 gpathsi = gpathsi->next;
353 }
354
355 g_slist_free(gpaths);
356 }
357 else
358 paths.Add(GetPath());
359 }
360 else
361 wxGenericFileDialog::GetPaths( paths );
362 }
363
364 void wxFileDialog::SetMessage(const wxString& message)
365 {
366 if (!gtk_check_version(2,4,0))
367 {
368 m_message = message;
369 SetTitle(message);
370 }
371 else
372 wxGenericFileDialog::SetMessage( message );
373 }
374
375 void wxFileDialog::SetPath(const wxString& path)
376 {
377 if (!gtk_check_version(2,4,0))
378 {
379 if (path.empty()) return;
380
381 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(path));
382 }
383 else
384 wxGenericFileDialog::SetPath( path );
385 }
386
387 void wxFileDialog::SetDirectory(const wxString& dir)
388 {
389 if (!gtk_check_version(2,4,0))
390 {
391 if (wxDirExists(dir))
392 {
393 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir));
394 }
395 }
396 else
397 wxGenericFileDialog::SetDirectory( dir );
398 }
399
400 wxString wxFileDialog::GetDirectory() const
401 {
402 if (!gtk_check_version(2,4,0))
403 {
404 gchar *str = gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(m_widget) );
405 wxString ret = wxConvFileName->cMB2WX(str);
406 if (str) g_free(str);
407
408 return ret;
409 }
410 else
411 return wxGenericFileDialog::GetDirectory();
412 }
413
414 void wxFileDialog::SetFilename(const wxString& name)
415 {
416 if (!gtk_check_version(2,4,0))
417 {
418 if (HasFlag(wxFD_SAVE))
419 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
420 else
421 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
422 }
423 else
424 wxGenericFileDialog::SetFilename( name );
425 }
426
427 wxString wxFileDialog::GetFilename() const
428 {
429 if (!gtk_check_version(2,4,0))
430 return wxFileName(GetPath()).GetFullName();
431 else
432 return wxGenericFileDialog::GetFilename();
433 }
434
435 void wxFileDialog::SetWildcard(const wxString& wildCard)
436 {
437 if (!gtk_check_version(2,4,0))
438 {
439 // parse filters
440 wxArrayString wildDescriptions, wildFilters;
441 if (!wxParseCommonDialogsFilter(wildCard, wildDescriptions, wildFilters))
442 {
443 wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") );
444 }
445 else
446 {
447 // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard
448 m_wildCard = wildCard;
449
450 GtkFileChooser* chooser = GTK_FILE_CHOOSER(m_widget);
451
452 // empty current filter list:
453 GSList* ifilters = gtk_file_chooser_list_filters(chooser);
454 GSList* filters = ifilters;
455
456 while (ifilters)
457 {
458 gtk_file_chooser_remove_filter(chooser,GTK_FILE_FILTER(ifilters->data));
459 ifilters = ifilters->next;
460 }
461 g_slist_free(filters);
462
463 // add parsed to GtkChooser
464 for (size_t n = 0; n < wildFilters.GetCount(); ++n)
465 {
466 GtkFileFilter* filter = gtk_file_filter_new();
467 gtk_file_filter_set_name(filter, wxGTK_CONV(wildDescriptions[n]));
468
469 wxStringTokenizer exttok(wildFilters[n], wxT(";"));
470 while (exttok.HasMoreTokens())
471 {
472 wxString token = exttok.GetNextToken();
473 gtk_file_filter_add_pattern(filter, wxGTK_CONV(token));
474 }
475
476 gtk_file_chooser_add_filter(chooser, filter);
477 }
478
479 // Reset the filter index
480 SetFilterIndex(0);
481 }
482 }
483 else
484 wxGenericFileDialog::SetWildcard( wildCard );
485 }
486
487 void wxFileDialog::SetFilterIndex(int filterIndex)
488 {
489
490 if (!gtk_check_version(2,4,0))
491 {
492 gpointer filter;
493 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
494 GSList *filters = gtk_file_chooser_list_filters(chooser);
495
496 filter = g_slist_nth_data(filters, filterIndex);
497
498 if (filter != NULL)
499 {
500 gtk_file_chooser_set_filter(chooser, GTK_FILE_FILTER(filter));
501 }
502 else
503 {
504 wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") );
505 }
506
507 g_slist_free(filters);
508 }
509 else
510 wxGenericFileDialog::SetFilterIndex( filterIndex );
511 }
512
513 int wxFileDialog::GetFilterIndex() const
514 {
515 if (!gtk_check_version(2,4,0))
516 {
517 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
518 GtkFileFilter *filter = gtk_file_chooser_get_filter(chooser);
519 GSList *filters = gtk_file_chooser_list_filters(chooser);
520 gint index = g_slist_index(filters, filter);
521 g_slist_free(filters);
522
523 if (index == -1)
524 {
525 wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") );
526 return 0;
527 }
528 else
529 return index;
530 }
531 else
532 return wxGenericFileDialog::GetFilterIndex();
533 }
534
535 #endif // wxUSE_FILEDLG && __WXGTK24__