]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/filedlg.cpp
don't call OnCloseDocument() from OnNewDocument(), this plainly doesn't make sense...
[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
700d08c1 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
f8bc53eb
JS
25#include <unistd.h> // chdir
26
f8bc53eb
JS
27#include "wx/filename.h" // wxFilename
28#include "wx/tokenzr.h" // wxStringTokenizer
29#include "wx/filefn.h" // ::wxGetCwd
f8bc53eb 30
291a8f20
RR
31//-----------------------------------------------------------------------------
32// "clicked" for OK-button
c801d85f
KB
33//-----------------------------------------------------------------------------
34
865bb325 35extern "C" {
9755e73b 36static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
c801d85f 37{
ff3e84ff 38 int style = dialog->GetWindowStyle();
e808cf8a 39 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
035b704a 40
b5ab6d19
MR
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
ff3e84ff 45 if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
bfc6fde4 46 {
f8bc53eb 47 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
0e1399b3
VZ
48 {
49 wxString msg;
f8bc53eb 50
9755e73b 51 msg.Printf(
f8bc53eb 52 _("File '%s' already exists, do you really want to overwrite it?"),
86501081 53 wxString(filename, *wxConvFileName));
0e1399b3 54
9755e73b
VS
55 wxMessageDialog dlg(dialog, msg, _("Confirm"),
56 wxYES_NO | wxICON_QUESTION);
57 if (dlg.ShowModal() != wxID_YES)
0e1399b3
VZ
58 return;
59 }
83624f79 60 }
035b704a 61
4c84a0dc
RR
62 if (style & wxFD_FILE_MUST_EXIST)
63 {
64 if ( !g_file_test(filename, G_FILE_TEST_EXISTS) )
65 {
e4161a2a 66 wxMessageDialog dlg( dialog, _("Please choose an existing file."),
4c84a0dc
RR
67 _("Error"), wxOK| wxICON_ERROR);
68 dlg.ShowModal();
69 return;
70 }
71 }
e4161a2a 72
3f6638b8 73 // change to the directory where the user went if asked
ff3e84ff 74 if (style & wxFD_CHANGE_DIR)
3f6638b8 75 {
f8bc53eb 76 // Use chdir to not care about filename encodings
e808cf8a 77 wxGtkString folder(g_path_get_dirname(filename));
f8bc53eb 78 chdir(folder);
3f6638b8
VZ
79 }
80
bfc6fde4 81 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
9755e73b 82 event.SetEventObject(dialog);
937013e0 83 dialog->HandleWindowEvent(event);
ff7b1510 84}
865bb325 85}
c801d85f 86
291a8f20
RR
87//-----------------------------------------------------------------------------
88// "clicked" for Cancel-button
89//-----------------------------------------------------------------------------
90
9f057af5
VZ
91extern "C"
92{
93
e4161a2a
VZ
94static void
95gtk_filedialog_cancel_callback(GtkWidget * WXUNUSED(w), wxFileDialog *dialog)
c801d85f 96{
bfc6fde4 97 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
9755e73b 98 event.SetEventObject(dialog);
937013e0 99 dialog->HandleWindowEvent(event);
9755e73b
VS
100}
101
102static void gtk_filedialog_response_callback(GtkWidget *w,
f8bc53eb 103 gint response,
9755e73b
VS
104 wxFileDialog *dialog)
105{
a7334928 106 if (response == GTK_RESPONSE_ACCEPT)
9755e73b 107 gtk_filedialog_ok_callback(w, dialog);
a552d120 108 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
c2740a5a 109 gtk_filedialog_cancel_callback(w, dialog);
ff7b1510 110}
9f057af5
VZ
111
112static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser,
113 gpointer user_data)
114{
9f057af5 115 GtkWidget *preview = GTK_WIDGET(user_data);
3ab296d9 116
e808cf8a 117 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
3ab296d9 118
9f057af5
VZ
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);
865bb325
VZ
130}
131
9f057af5
VZ
132} // extern "C"
133
8ce68f7f
VZ
134static void wxInsertChildInFileDialog(wxWindow* WXUNUSED(parent),
135 wxWindow* WXUNUSED(child))
136{
137}
138
c801d85f 139
291a8f20
RR
140//-----------------------------------------------------------------------------
141// wxFileDialog
142//-----------------------------------------------------------------------------
143
700d08c1 144IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
4e1901b7 145
700d08c1 146BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase)
f8bc53eb 147 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
f1d5aa4e 148 EVT_SIZE(wxFileDialog::OnSize)
4e1901b7 149END_EVENT_TABLE()
c801d85f 150
9755e73b
VS
151wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
152 const wxString& defaultDir,
153 const wxString& defaultFileName,
154 const wxString& wildCard,
ff3e84ff
VZ
155 long style, const wxPoint& pos,
156 const wxSize& sz,
157 const wxString& name)
700d08c1 158 : wxFileDialogBase()
c801d85f 159{
8ce68f7f 160 m_insertCallback = wxInsertChildInFileDialog;
700d08c1
RR
161 parent = GetParentForModalDialog(parent);
162
163 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName,
164 wildCard, style, pos, sz, name))
77f70672 165 {
9f057af5
VZ
166 return;
167 }
83624f79 168
9f057af5
VZ
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 }
3f6638b8 176
9f057af5
VZ
177 GtkFileChooserAction gtk_action;
178 GtkWindow* gtk_parent = NULL;
179 if (parent)
180 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
27b2dd53 181
9f057af5
VZ
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 }
f8bc53eb 193
9f057af5
VZ
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);
9755e73b 201
0cf3e587
VZ
202 m_fc.SetWidget( GTK_FILE_CHOOSER(m_widget) );
203
9f057af5 204 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
c1dfe97c 205
9f057af5
VZ
206 if ( style & wxFD_MULTIPLE )
207 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true);
27b2dd53 208
bf345206
VZ
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...
9f057af5
VZ
213 g_signal_connect (G_OBJECT(m_widget),
214 "delete_event",
215 G_CALLBACK (gtk_widget_hide_on_delete),
216 (gpointer)this);
a552d120 217
bf345206
VZ
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:
9f057af5 224 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
27b2dd53 225
9f057af5
VZ
226 g_signal_connect (m_widget, "response",
227 G_CALLBACK (gtk_filedialog_response_callback), this);
27b2dd53 228
9f057af5 229 SetWildcard(wildCard);
f8bc53eb 230
bf345206
VZ
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);
f241631e
RD
239 else
240 fn.AssignDir(defaultDir);
bf345206
VZ
241
242 // set the initial file name and/or directory
78355ffc 243 fn.MakeAbsolute(); // GTK+ needs absolute path
3a41827a
VZ
244 const wxString dir = fn.GetPath();
245 if ( !dir.empty() )
9f057af5 246 {
3a41827a
VZ
247 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
248 dir.fn_str());
bf345206 249 }
f8bc53eb 250
3a41827a 251 const wxString fname = fn.GetFullName();
bf345206
VZ
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 }
b5ab6d19
MR
259
260#if GTK_CHECK_VERSION(2,7,3)
9f057af5
VZ
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);
b5ab6d19 263#endif
9f057af5
VZ
264 }
265 else // wxFD_OPEN
266 {
bf345206 267 if ( !fname.empty() )
f8bc53eb 268 {
bf345206
VZ
269 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
270 fn.GetFullPath().fn_str());
f8bc53eb 271 }
77f70672 272 }
9f057af5 273
9f057af5
VZ
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 }
9755e73b 284}
0e1399b3 285
8ce68f7f 286
ff654490 287void wxFileDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event))
4e1901b7 288{
700d08c1 289 EndDialog(wxID_OK);
4e1901b7
RR
290}
291
292int wxFileDialog::ShowModal()
293{
8ce68f7f
VZ
294 if (CreateExtraControl())
295 {
296 GtkWidget *control = m_extraControl->m_widget;
4e1901b7 297
f1d5aa4e 298 wxASSERT(control->parent == NULL);
8ce68f7f
VZ
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();
9755e73b 305}
0e1399b3 306
700d08c1
RR
307void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
308 int WXUNUSED(width), int WXUNUSED(height),
309 int WXUNUSED(sizeFlags))
5b2e23bf 310{
5b2e23bf
RR
311}
312
f1d5aa4e
PC
313void 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
f8bc53eb
JS
319wxString wxFileDialog::GetPath() const
320{
700d08c1 321 return m_fc.GetPath();
f8bc53eb
JS
322}
323
27b2dd53 324void wxFileDialog::GetFilenames(wxArrayString& files) const
9755e73b 325{
700d08c1 326 m_fc.GetFilenames( files );
9755e73b 327}
76840ed0 328
27b2dd53 329void wxFileDialog::GetPaths(wxArrayString& paths) const
9755e73b 330{
700d08c1 331 m_fc.GetPaths( paths );
9755e73b 332}
035b704a 333
9755e73b
VS
334void wxFileDialog::SetMessage(const wxString& message)
335{
700d08c1
RR
336 m_message = message;
337 SetTitle(message);
9755e73b
VS
338}
339
76840ed0
RR
340void wxFileDialog::SetPath(const wxString& path)
341{
700d08c1 342 m_fc.SetPath( path );
76840ed0
RR
343}
344
9755e73b
VS
345void wxFileDialog::SetDirectory(const wxString& dir)
346{
700d08c1 347 m_fc.SetDirectory( dir );
9755e73b
VS
348}
349
f8bc53eb
JS
350wxString wxFileDialog::GetDirectory() const
351{
700d08c1 352 return m_fc.GetDirectory();
f8bc53eb
JS
353}
354
9755e73b
VS
355void wxFileDialog::SetFilename(const wxString& name)
356{
700d08c1
RR
357 if (HasFdFlag(wxFD_SAVE))
358 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
77f70672 359 else
700d08c1 360 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
9755e73b 361}
035b704a 362
f8bc53eb
JS
363wxString wxFileDialog::GetFilename() const
364{
700d08c1 365 return m_fc.GetFilename();
f8bc53eb
JS
366}
367
9755e73b
VS
368void wxFileDialog::SetWildcard(const wxString& wildCard)
369{
700d08c1 370 m_fc.SetWildcard( wildCard );
9755e73b 371}
a3622daa 372
9755e73b
VS
373void wxFileDialog::SetFilterIndex(int filterIndex)
374{
ff3e84ff 375
700d08c1 376 m_fc.SetFilterIndex( filterIndex);
4e1901b7
RR
377}
378
f8bc53eb 379int wxFileDialog::GetFilterIndex() const
4e1901b7 380{
700d08c1 381 return m_fc.GetFilterIndex();
9755e73b 382}
3f6638b8 383
700d08c1 384#endif // wxUSE_FILEDLG