]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/filedlg.cpp
fix for AUI pane shrinking each time it is floated
[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
VS
82 event.SetEventObject(dialog);
83 dialog->GetEventHandler()->ProcessEvent(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
VS
98 event.SetEventObject(dialog);
99 dialog->GetEventHandler()->ProcessEvent(event);
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
c801d85f 134
291a8f20
RR
135//-----------------------------------------------------------------------------
136// wxFileDialog
137//-----------------------------------------------------------------------------
138
700d08c1 139IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
4e1901b7 140
700d08c1 141BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase)
f8bc53eb 142 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
4e1901b7 143END_EVENT_TABLE()
c801d85f 144
9755e73b
VS
145wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
146 const wxString& defaultDir,
147 const wxString& defaultFileName,
148 const wxString& wildCard,
ff3e84ff
VZ
149 long style, const wxPoint& pos,
150 const wxSize& sz,
151 const wxString& name)
700d08c1 152 : wxFileDialogBase()
c801d85f 153{
700d08c1
RR
154 parent = GetParentForModalDialog(parent);
155
156 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName,
157 wildCard, style, pos, sz, name))
77f70672 158 {
9f057af5
VZ
159 return;
160 }
83624f79 161
9f057af5
VZ
162 if (!PreCreation(parent, pos, wxDefaultSize) ||
163 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
164 wxDefaultValidator, wxT("filedialog")))
165 {
166 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
167 return;
168 }
3f6638b8 169
9f057af5
VZ
170 GtkFileChooserAction gtk_action;
171 GtkWindow* gtk_parent = NULL;
172 if (parent)
173 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
27b2dd53 174
9f057af5
VZ
175 const gchar* ok_btn_stock;
176 if ( style & wxFD_SAVE )
177 {
178 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
179 ok_btn_stock = GTK_STOCK_SAVE;
180 }
181 else
182 {
183 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
184 ok_btn_stock = GTK_STOCK_OPEN;
185 }
f8bc53eb 186
9f057af5
VZ
187 m_widget = gtk_file_chooser_dialog_new(
188 wxGTK_CONV(m_message),
189 gtk_parent,
190 gtk_action,
191 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
192 ok_btn_stock, GTK_RESPONSE_ACCEPT,
193 NULL);
9755e73b 194
0cf3e587
VZ
195 m_fc.SetWidget( GTK_FILE_CHOOSER(m_widget) );
196
9f057af5 197 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
c1dfe97c 198
9f057af5
VZ
199 if ( style & wxFD_MULTIPLE )
200 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true);
27b2dd53 201
bf345206
VZ
202 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
203 // destroys the dialog when the user press ESC on the dialog: in that case
204 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
205 // errors...
9f057af5
VZ
206 g_signal_connect (G_OBJECT(m_widget),
207 "delete_event",
208 G_CALLBACK (gtk_widget_hide_on_delete),
209 (gpointer)this);
a552d120 210
bf345206
VZ
211 // local-only property could be set to false to allow non-local files to be
212 // loaded. In that case get/set_uri(s) should be used instead of
213 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
214 // probably also be created with a backend, e.g "gnome-vfs", "default", ...
215 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
216 // as the default - true:
9f057af5 217 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
27b2dd53 218
9f057af5
VZ
219 g_signal_connect (m_widget, "response",
220 G_CALLBACK (gtk_filedialog_response_callback), this);
27b2dd53 221
9f057af5 222 SetWildcard(wildCard);
f8bc53eb 223
bf345206
VZ
224 // if defaultDir is specified it should contain the directory and
225 // defaultFileName should contain the default name of the file, however if
226 // directory is not given, defaultFileName contains both
227 wxFileName fn;
228 if ( defaultDir.empty() )
229 fn.Assign(defaultFileName);
230 else if ( !defaultFileName.empty() )
231 fn.Assign(defaultDir, defaultFileName);
f241631e
RD
232 else
233 fn.AssignDir(defaultDir);
bf345206
VZ
234
235 // set the initial file name and/or directory
78355ffc 236 fn.MakeAbsolute(); // GTK+ needs absolute path
3a41827a
VZ
237 const wxString dir = fn.GetPath();
238 if ( !dir.empty() )
9f057af5 239 {
3a41827a
VZ
240 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
241 dir.fn_str());
bf345206 242 }
f8bc53eb 243
3a41827a 244 const wxString fname = fn.GetFullName();
bf345206
VZ
245 if ( style & wxFD_SAVE )
246 {
247 if ( !fname.empty() )
248 {
249 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
250 fname.fn_str());
251 }
b5ab6d19
MR
252
253#if GTK_CHECK_VERSION(2,7,3)
9f057af5
VZ
254 if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3))
255 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget), TRUE);
b5ab6d19 256#endif
9f057af5
VZ
257 }
258 else // wxFD_OPEN
259 {
bf345206 260 if ( !fname.empty() )
f8bc53eb 261 {
bf345206
VZ
262 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
263 fn.GetFullPath().fn_str());
f8bc53eb 264 }
77f70672 265 }
9f057af5 266
9f057af5
VZ
267 if ( style & wxFD_PREVIEW )
268 {
269 GtkWidget *previewImage = gtk_image_new();
270
271 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget),
272 previewImage);
273 g_signal_connect(m_widget, "update-preview",
274 G_CALLBACK(gtk_filedialog_update_preview_callback),
275 previewImage);
276 }
9755e73b 277}
0e1399b3 278
ff654490 279void wxFileDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event))
4e1901b7 280{
700d08c1 281 EndDialog(wxID_OK);
4e1901b7
RR
282}
283
284int wxFileDialog::ShowModal()
285{
700d08c1 286 return wxDialog::ShowModal();
4e1901b7
RR
287}
288
289bool wxFileDialog::Show( bool show )
290{
700d08c1 291 return wxDialog::Show( show );
9755e73b 292}
0e1399b3 293
700d08c1
RR
294void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
295 int WXUNUSED(width), int WXUNUSED(height),
296 int WXUNUSED(sizeFlags))
5b2e23bf 297{
5b2e23bf
RR
298}
299
f8bc53eb
JS
300wxString wxFileDialog::GetPath() const
301{
700d08c1 302 return m_fc.GetPath();
f8bc53eb
JS
303}
304
27b2dd53 305void wxFileDialog::GetFilenames(wxArrayString& files) const
9755e73b 306{
700d08c1 307 m_fc.GetFilenames( files );
9755e73b 308}
76840ed0 309
27b2dd53 310void wxFileDialog::GetPaths(wxArrayString& paths) const
9755e73b 311{
700d08c1 312 m_fc.GetPaths( paths );
9755e73b 313}
035b704a 314
9755e73b
VS
315void wxFileDialog::SetMessage(const wxString& message)
316{
700d08c1
RR
317 m_message = message;
318 SetTitle(message);
9755e73b
VS
319}
320
76840ed0
RR
321void wxFileDialog::SetPath(const wxString& path)
322{
700d08c1 323 m_fc.SetPath( path );
76840ed0
RR
324}
325
9755e73b
VS
326void wxFileDialog::SetDirectory(const wxString& dir)
327{
700d08c1 328 m_fc.SetDirectory( dir );
9755e73b
VS
329}
330
f8bc53eb
JS
331wxString wxFileDialog::GetDirectory() const
332{
700d08c1 333 return m_fc.GetDirectory();
f8bc53eb
JS
334}
335
9755e73b
VS
336void wxFileDialog::SetFilename(const wxString& name)
337{
700d08c1
RR
338 if (HasFdFlag(wxFD_SAVE))
339 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
77f70672 340 else
700d08c1 341 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
9755e73b 342}
035b704a 343
f8bc53eb
JS
344wxString wxFileDialog::GetFilename() const
345{
700d08c1 346 return m_fc.GetFilename();
f8bc53eb
JS
347}
348
9755e73b
VS
349void wxFileDialog::SetWildcard(const wxString& wildCard)
350{
700d08c1 351 m_fc.SetWildcard( wildCard );
9755e73b 352}
a3622daa 353
9755e73b
VS
354void wxFileDialog::SetFilterIndex(int filterIndex)
355{
ff3e84ff 356
700d08c1 357 m_fc.SetFilterIndex( filterIndex);
4e1901b7
RR
358}
359
f8bc53eb 360int wxFileDialog::GetFilterIndex() const
4e1901b7 361{
700d08c1 362 return m_fc.GetFilterIndex();
9755e73b 363}
3f6638b8 364
700d08c1 365#endif // wxUSE_FILEDLG