]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/filedlg.cpp
Add missing wxEVT_COMMAND_TEXT_ENTER
[wxWidgets.git] / src / gtk / filedlg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
9b5f1895 2// Name: src/gtk/filedlg.cpp
9755e73b 3// Purpose: native implementation of wxFileDialog
f8bc53eb 4// Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
a81258be 5// Id: $Id$
f8bc53eb 6// Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
03647350 13#if wxUSE_FILEDLG
9755e73b 14
c801d85f 15#include "wx/filedlg.h"
4e1901b7 16
88a7a4e1
WS
17#ifndef WX_PRECOMP
18 #include "wx/intl.h"
246c5004 19 #include "wx/msgdlg.h"
88a7a4e1
WS
20#endif
21
f8bc53eb 22#include <gtk/gtk.h>
9755e73b 23#include "wx/gtk/private.h"
83624f79 24
f04f570f 25#ifdef __UNIX__
f8bc53eb 26#include <unistd.h> // chdir
f04f570f 27#endif
f8bc53eb 28
f8bc53eb
JS
29#include "wx/filename.h" // wxFilename
30#include "wx/tokenzr.h" // wxStringTokenizer
31#include "wx/filefn.h" // ::wxGetCwd
f8bc53eb 32
291a8f20
RR
33//-----------------------------------------------------------------------------
34// "clicked" for OK-button
c801d85f
KB
35//-----------------------------------------------------------------------------
36
865bb325 37extern "C" {
9755e73b 38static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
c801d85f 39{
ff3e84ff 40 int style = dialog->GetWindowStyle();
e808cf8a 41 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
035b704a 42
b5ab6d19 43 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
9dc44eff 44#ifndef __WXGTK3__
b5ab6d19 45#if GTK_CHECK_VERSION(2,7,3)
f2448f21 46 if (gtk_check_version(2, 7, 3) != NULL)
b5ab6d19 47#endif
bfc6fde4 48 {
f2448f21 49 if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
0e1399b3 50 {
f2448f21
PC
51 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
52 {
53 wxString msg;
54
55 msg.Printf(
56 _("File '%s' already exists, do you really want to overwrite it?"),
b74d7c85 57 wxString::FromUTF8(filename));
f2448f21
PC
58
59 wxMessageDialog dlg(dialog, msg, _("Confirm"),
60 wxYES_NO | wxICON_QUESTION);
61 if (dlg.ShowModal() != wxID_YES)
62 return;
63 }
0e1399b3 64 }
83624f79 65 }
9dc44eff 66#endif
035b704a 67
4c84a0dc
RR
68 if (style & wxFD_FILE_MUST_EXIST)
69 {
70 if ( !g_file_test(filename, G_FILE_TEST_EXISTS) )
71 {
e4161a2a 72 wxMessageDialog dlg( dialog, _("Please choose an existing file."),
4c84a0dc
RR
73 _("Error"), wxOK| wxICON_ERROR);
74 dlg.ShowModal();
75 return;
76 }
77 }
e4161a2a 78
3f6638b8 79 // change to the directory where the user went if asked
ff3e84ff 80 if (style & wxFD_CHANGE_DIR)
3f6638b8 81 {
f8bc53eb 82 // Use chdir to not care about filename encodings
e808cf8a 83 wxGtkString folder(g_path_get_dirname(filename));
f8bc53eb 84 chdir(folder);
3f6638b8
VZ
85 }
86
bfc6fde4 87 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
9755e73b 88 event.SetEventObject(dialog);
937013e0 89 dialog->HandleWindowEvent(event);
ff7b1510 90}
865bb325 91}
c801d85f 92
291a8f20
RR
93//-----------------------------------------------------------------------------
94// "clicked" for Cancel-button
95//-----------------------------------------------------------------------------
96
9f057af5
VZ
97extern "C"
98{
99
e4161a2a
VZ
100static void
101gtk_filedialog_cancel_callback(GtkWidget * WXUNUSED(w), wxFileDialog *dialog)
c801d85f 102{
bfc6fde4 103 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
9755e73b 104 event.SetEventObject(dialog);
937013e0 105 dialog->HandleWindowEvent(event);
9755e73b
VS
106}
107
108static void gtk_filedialog_response_callback(GtkWidget *w,
f8bc53eb 109 gint response,
9755e73b
VS
110 wxFileDialog *dialog)
111{
a7334928 112 if (response == GTK_RESPONSE_ACCEPT)
9755e73b 113 gtk_filedialog_ok_callback(w, dialog);
a552d120 114 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
c2740a5a 115 gtk_filedialog_cancel_callback(w, dialog);
ff7b1510 116}
9f057af5
VZ
117
118static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser,
119 gpointer user_data)
120{
9f057af5 121 GtkWidget *preview = GTK_WIDGET(user_data);
3ab296d9 122
e808cf8a 123 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
3ab296d9 124
9f057af5
VZ
125 if ( !filename )
126 return;
127
128 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL);
129 gboolean have_preview = pixbuf != NULL;
130
131 gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf);
132 if ( pixbuf )
133 g_object_unref (pixbuf);
134
135 gtk_file_chooser_set_preview_widget_active(chooser, have_preview);
865bb325
VZ
136}
137
9f057af5
VZ
138} // extern "C"
139
3b7067a0 140void wxFileDialog::AddChildGTK(wxWindowGTK* child)
38a36cf5
PC
141{
142 // allow dialog to be resized smaller horizontally
3b7067a0
PC
143 gtk_widget_set_size_request(
144 child->m_widget, child->GetMinWidth(), child->m_height);
38a36cf5 145
3abfbb43 146 gtk_file_chooser_set_extra_widget(
48200154 147 GTK_FILE_CHOOSER(m_widget), child->m_widget);
8ce68f7f
VZ
148}
149
291a8f20
RR
150//-----------------------------------------------------------------------------
151// wxFileDialog
152//-----------------------------------------------------------------------------
153
700d08c1 154IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
4e1901b7 155
700d08c1 156BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase)
f8bc53eb 157 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
f1d5aa4e 158 EVT_SIZE(wxFileDialog::OnSize)
4e1901b7 159END_EVENT_TABLE()
c801d85f 160
9755e73b
VS
161wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
162 const wxString& defaultDir,
163 const wxString& defaultFileName,
164 const wxString& wildCard,
ff3e84ff
VZ
165 long style, const wxPoint& pos,
166 const wxSize& sz,
167 const wxString& name)
700d08c1 168 : wxFileDialogBase()
e3f54c8f
VZ
169{
170 Create(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name);
171}
172
173bool wxFileDialog::Create(wxWindow *parent, const wxString& message,
174 const wxString& defaultDir,
175 const wxString& defaultFileName,
176 const wxString& wildCard,
177 long style, const wxPoint& pos,
178 const wxSize& sz,
179 const wxString& name)
c801d85f 180{
cdc48273 181 parent = GetParentForModalDialog(parent, style);
f2448f21 182
700d08c1
RR
183 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName,
184 wildCard, style, pos, sz, name))
77f70672 185 {
e3f54c8f 186 return false;
9f057af5 187 }
83624f79 188
9f057af5
VZ
189 if (!PreCreation(parent, pos, wxDefaultSize) ||
190 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
191 wxDefaultValidator, wxT("filedialog")))
192 {
193 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
e3f54c8f 194 return false;
9f057af5 195 }
3f6638b8 196
9f057af5
VZ
197 GtkFileChooserAction gtk_action;
198 GtkWindow* gtk_parent = NULL;
199 if (parent)
200 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
27b2dd53 201
9f057af5
VZ
202 const gchar* ok_btn_stock;
203 if ( style & wxFD_SAVE )
204 {
205 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
206 ok_btn_stock = GTK_STOCK_SAVE;
207 }
208 else
209 {
210 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
211 ok_btn_stock = GTK_STOCK_OPEN;
212 }
f8bc53eb 213
9f057af5
VZ
214 m_widget = gtk_file_chooser_dialog_new(
215 wxGTK_CONV(m_message),
216 gtk_parent,
217 gtk_action,
218 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
219 ok_btn_stock, GTK_RESPONSE_ACCEPT,
220 NULL);
9ff9d30c 221 g_object_ref(m_widget);
f2448f21 222 GtkFileChooser* file_chooser = GTK_FILE_CHOOSER(m_widget);
9755e73b 223
f2448f21 224 m_fc.SetWidget(file_chooser);
0cf3e587 225
9f057af5 226 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
c1dfe97c 227
9f057af5 228 if ( style & wxFD_MULTIPLE )
f2448f21 229 gtk_file_chooser_set_select_multiple(file_chooser, true);
27b2dd53 230
bf345206
VZ
231 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
232 // destroys the dialog when the user press ESC on the dialog: in that case
233 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
234 // errors...
f2448f21 235 g_signal_connect(m_widget,
9f057af5
VZ
236 "delete_event",
237 G_CALLBACK (gtk_widget_hide_on_delete),
f2448f21 238 this);
a552d120 239
bf345206
VZ
240 // local-only property could be set to false to allow non-local files to be
241 // loaded. In that case get/set_uri(s) should be used instead of
242 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
d13b34d3 243 // probably also be created with a backend, e.g. "gnome-vfs", "default", ...
bf345206
VZ
244 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
245 // as the default - true:
9f057af5 246 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
27b2dd53 247
9f057af5
VZ
248 g_signal_connect (m_widget, "response",
249 G_CALLBACK (gtk_filedialog_response_callback), this);
27b2dd53 250
fa333077
VZ
251
252 // deal with extensions/filters
9f057af5 253 SetWildcard(wildCard);
f8bc53eb 254
fa333077
VZ
255 wxString defaultFileNameWithExt = defaultFileName;
256 if ( !wildCard.empty() && !defaultFileName.empty() &&
257 !wxFileName(defaultFileName).HasExt() )
258 {
259 // append the default extension to the initial file name: GTK won't do
260 // it for us by default (unlike e.g. MSW)
261 const wxString defaultExt = m_fc.GetCurrentWildCard().AfterFirst('.');
262 if ( defaultExt.find_first_of("?*") == wxString::npos )
263 defaultFileNameWithExt += "." + defaultExt;
264 }
265
266
bf345206
VZ
267 // if defaultDir is specified it should contain the directory and
268 // defaultFileName should contain the default name of the file, however if
269 // directory is not given, defaultFileName contains both
270 wxFileName fn;
271 if ( defaultDir.empty() )
fa333077
VZ
272 fn.Assign(defaultFileNameWithExt);
273 else if ( !defaultFileNameWithExt.empty() )
274 fn.Assign(defaultDir, defaultFileNameWithExt);
f241631e
RD
275 else
276 fn.AssignDir(defaultDir);
bf345206
VZ
277
278 // set the initial file name and/or directory
78355ffc 279 fn.MakeAbsolute(); // GTK+ needs absolute path
3a41827a
VZ
280 const wxString dir = fn.GetPath();
281 if ( !dir.empty() )
9f057af5 282 {
c282e47d 283 gtk_file_chooser_set_current_folder(file_chooser, wxGTK_CONV_FN(dir));
bf345206 284 }
f8bc53eb 285
3a41827a 286 const wxString fname = fn.GetFullName();
bf345206
VZ
287 if ( style & wxFD_SAVE )
288 {
289 if ( !fname.empty() )
290 {
c282e47d 291 gtk_file_chooser_set_current_name(file_chooser, wxGTK_CONV_FN(fname));
bf345206 292 }
b5ab6d19
MR
293
294#if GTK_CHECK_VERSION(2,7,3)
9dc44eff
PC
295 if ((style & wxFD_OVERWRITE_PROMPT)
296#ifndef __WXGTK3__
297 && gtk_check_version(2,7,3) == NULL
298#endif
299 )
300 {
f2448f21 301 gtk_file_chooser_set_do_overwrite_confirmation(file_chooser, true);
9dc44eff 302 }
b5ab6d19 303#endif
9f057af5
VZ
304 }
305 else // wxFD_OPEN
306 {
bf345206 307 if ( !fname.empty() )
f8bc53eb 308 {
f2448f21 309 gtk_file_chooser_set_filename(file_chooser,
c282e47d 310 wxGTK_CONV_FN(fn.GetFullPath()));
f8bc53eb 311 }
77f70672 312 }
9f057af5 313
9f057af5
VZ
314 if ( style & wxFD_PREVIEW )
315 {
316 GtkWidget *previewImage = gtk_image_new();
317
f2448f21 318 gtk_file_chooser_set_preview_widget(file_chooser, previewImage);
9f057af5
VZ
319 g_signal_connect(m_widget, "update-preview",
320 G_CALLBACK(gtk_filedialog_update_preview_callback),
321 previewImage);
322 }
e3f54c8f
VZ
323
324 return true;
9755e73b 325}
0e1399b3 326
9ff9d30c
PC
327wxFileDialog::~wxFileDialog()
328{
329 if (m_extraControl)
330 {
331 // get chooser to drop its reference right now, allowing wxWindow dtor
332 // to verify that ref count drops to zero
333 gtk_file_chooser_set_extra_widget(
334 GTK_FILE_CHOOSER(m_widget), NULL);
335 }
336}
337
ff654490 338void wxFileDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event))
4e1901b7 339{
700d08c1 340 EndDialog(wxID_OK);
4e1901b7
RR
341}
342
343int wxFileDialog::ShowModal()
344{
3abfbb43 345 CreateExtraControl();
8ce68f7f
VZ
346
347 return wxDialog::ShowModal();
9755e73b 348}
0e1399b3 349
03647350
VZ
350void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
351 int WXUNUSED(width), int WXUNUSED(height),
700d08c1 352 int WXUNUSED(sizeFlags))
5b2e23bf 353{
5b2e23bf
RR
354}
355
f1d5aa4e
PC
356void wxFileDialog::OnSize(wxSizeEvent&)
357{
358 // avoid calling DoLayout(), which will set the (wrong) size of
359 // m_extraControl, its size is managed by GtkFileChooser
360}
361
f8bc53eb
JS
362wxString wxFileDialog::GetPath() const
363{
fa333077 364 return m_fc.GetPath();
f8bc53eb
JS
365}
366
27b2dd53 367void wxFileDialog::GetFilenames(wxArrayString& files) const
9755e73b 368{
700d08c1 369 m_fc.GetFilenames( files );
9755e73b 370}
76840ed0 371
27b2dd53 372void wxFileDialog::GetPaths(wxArrayString& paths) const
9755e73b 373{
700d08c1 374 m_fc.GetPaths( paths );
9755e73b 375}
035b704a 376
9755e73b
VS
377void wxFileDialog::SetMessage(const wxString& message)
378{
700d08c1
RR
379 m_message = message;
380 SetTitle(message);
9755e73b
VS
381}
382
76840ed0
RR
383void wxFileDialog::SetPath(const wxString& path)
384{
dea43e6e
VZ
385 // Don't do anything if no path is specified, in particular don't set the
386 // path to m_dir below as this would result in opening the dialog in the
387 // parent directory of this one instead of m_dir itself.
388 if ( path.empty() )
389 return;
390
8f79ece3 391 // we need an absolute path for GTK native chooser so ensure that we have
257d35ae
VZ
392 // it: use the initial directory if it was set or just CWD otherwise (this
393 // is the default behaviour if m_dir is empty)
8f79ece3 394 wxFileName fn(path);
257d35ae 395 fn.MakeAbsolute(m_dir);
8f79ece3 396 m_fc.SetPath(fn.GetFullPath());
76840ed0
RR
397}
398
9755e73b
VS
399void wxFileDialog::SetDirectory(const wxString& dir)
400{
02a40b8a
JS
401 if (m_fc.SetDirectory( dir ))
402 {
403 // Cache the dir, as gtk_file_chooser_get_current_folder()
404 // doesn't return anything until the dialog has been shown
405 m_dir = dir;
406 }
9755e73b
VS
407}
408
f8bc53eb
JS
409wxString wxFileDialog::GetDirectory() const
410{
02a40b8a
JS
411 wxString currentDir( m_fc.GetDirectory() );
412 if (currentDir.empty())
413 {
414 // m_fc.GetDirectory() will return empty until the dialog has been shown
415 // in which case use any previously provided value
416 currentDir = m_dir;
417 }
418 return currentDir;
f8bc53eb
JS
419}
420
9755e73b
VS
421void wxFileDialog::SetFilename(const wxString& name)
422{
700d08c1 423 if (HasFdFlag(wxFD_SAVE))
02a40b8a 424 {
700d08c1 425 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
02a40b8a
JS
426 m_fileName = name;
427 }
428
77f70672 429 else
02a40b8a
JS
430 {
431 wxString path( GetDirectory() );
432 if (path.empty())
433 {
434 // SetPath() fires an assert if fed other than filepaths
435 return;
436 }
437 SetPath(wxFileName(path, name).GetFullPath());
438 m_fileName = name;
439 }
9755e73b 440}
035b704a 441
f8bc53eb
JS
442wxString wxFileDialog::GetFilename() const
443{
02a40b8a
JS
444 wxString currentFilename( m_fc.GetFilename() );
445 if (currentFilename.empty())
446 {
447 // m_fc.GetFilename() will return empty until the dialog has been shown
448 // in which case use any previously provided value
449 currentFilename = m_fileName;
450 }
451 return currentFilename;
f8bc53eb
JS
452}
453
9755e73b
VS
454void wxFileDialog::SetWildcard(const wxString& wildCard)
455{
b534aeb2
VZ
456 wxFileDialogBase::SetWildcard(wildCard);
457 m_fc.SetWildcard( GetWildcard() );
9755e73b 458}
a3622daa 459
9755e73b
VS
460void wxFileDialog::SetFilterIndex(int filterIndex)
461{
700d08c1 462 m_fc.SetFilterIndex( filterIndex);
4e1901b7
RR
463}
464
f8bc53eb 465int wxFileDialog::GetFilterIndex() const
4e1901b7 466{
700d08c1 467 return m_fc.GetFilterIndex();
9755e73b 468}
3f6638b8 469
03647350 470#endif // wxUSE_FILEDLG