]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/filedlg.cpp
decouple item index from string value (patch 1905702)
[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
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(filename, *wxConvFileName));
54
55 wxMessageDialog dlg(dialog, msg, _("Confirm"),
56 wxYES_NO | wxICON_QUESTION);
57 if (dlg.ShowModal() != wxID_YES)
58 return;
59 }
60 }
61
62 if (style & wxFD_FILE_MUST_EXIST)
63 {
64 if ( !g_file_test(filename, G_FILE_TEST_EXISTS) )
65 {
66 wxMessageDialog dlg( dialog, _("Please choose an existing file."),
67 _("Error"), wxOK| wxICON_ERROR);
68 dlg.ShowModal();
69 return;
70 }
71 }
72
73 // change to the directory where the user went if asked
74 if (style & wxFD_CHANGE_DIR)
75 {
76 // Use chdir to not care about filename encodings
77 wxGtkString folder(g_path_get_dirname(filename));
78 chdir(folder);
79 }
80
81 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
82 event.SetEventObject(dialog);
83 dialog->HandleWindowEvent(event);
84}
85}
86
87//-----------------------------------------------------------------------------
88// "clicked" for Cancel-button
89//-----------------------------------------------------------------------------
90
91extern "C"
92{
93
94static void
95gtk_filedialog_cancel_callback(GtkWidget * WXUNUSED(w), wxFileDialog *dialog)
96{
97 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
98 event.SetEventObject(dialog);
99 dialog->HandleWindowEvent(event);
100}
101
102static void gtk_filedialog_response_callback(GtkWidget *w,
103 gint response,
104 wxFileDialog *dialog)
105{
106 if (response == GTK_RESPONSE_ACCEPT)
107 gtk_filedialog_ok_callback(w, dialog);
108 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
109 gtk_filedialog_cancel_callback(w, dialog);
110}
111
112static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser,
113 gpointer user_data)
114{
115 GtkWidget *preview = GTK_WIDGET(user_data);
116
117 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
118
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);
130}
131
132} // extern "C"
133
134static void wxInsertChildInFileDialog(wxWindow* WXUNUSED(parent),
135 wxWindow* WXUNUSED(child))
136{
137}
138
139
140//-----------------------------------------------------------------------------
141// wxFileDialog
142//-----------------------------------------------------------------------------
143
144IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
145
146BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase)
147 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
148END_EVENT_TABLE()
149
150wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
151 const wxString& defaultDir,
152 const wxString& defaultFileName,
153 const wxString& wildCard,
154 long style, const wxPoint& pos,
155 const wxSize& sz,
156 const wxString& name)
157 : wxFileDialogBase()
158{
159 m_insertCallback = wxInsertChildInFileDialog;
160 parent = GetParentForModalDialog(parent);
161
162 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName,
163 wildCard, style, pos, sz, name))
164 {
165 return;
166 }
167
168 if (!PreCreation(parent, pos, wxDefaultSize) ||
169 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
170 wxDefaultValidator, wxT("filedialog")))
171 {
172 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
173 return;
174 }
175
176 GtkFileChooserAction gtk_action;
177 GtkWindow* gtk_parent = NULL;
178 if (parent)
179 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
180
181 const gchar* ok_btn_stock;
182 if ( style & wxFD_SAVE )
183 {
184 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
185 ok_btn_stock = GTK_STOCK_SAVE;
186 }
187 else
188 {
189 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
190 ok_btn_stock = GTK_STOCK_OPEN;
191 }
192
193 m_widget = gtk_file_chooser_dialog_new(
194 wxGTK_CONV(m_message),
195 gtk_parent,
196 gtk_action,
197 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
198 ok_btn_stock, GTK_RESPONSE_ACCEPT,
199 NULL);
200
201 m_fc.SetWidget( GTK_FILE_CHOOSER(m_widget) );
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
209 // destroys the dialog when the user press ESC on the dialog: in that case
210 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
211 // errors...
212 g_signal_connect (G_OBJECT(m_widget),
213 "delete_event",
214 G_CALLBACK (gtk_widget_hide_on_delete),
215 (gpointer)this);
216
217 // local-only property could be set to false to allow non-local files to be
218 // loaded. In that case get/set_uri(s) should be used instead of
219 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
220 // probably also be created with a backend, e.g "gnome-vfs", "default", ...
221 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
222 // as the default - true:
223 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
224
225 g_signal_connect (m_widget, "response",
226 G_CALLBACK (gtk_filedialog_response_callback), this);
227
228 SetWildcard(wildCard);
229
230 // if defaultDir is specified it should contain the directory and
231 // defaultFileName should contain the default name of the file, however if
232 // directory is not given, defaultFileName contains both
233 wxFileName fn;
234 if ( defaultDir.empty() )
235 fn.Assign(defaultFileName);
236 else if ( !defaultFileName.empty() )
237 fn.Assign(defaultDir, defaultFileName);
238 else
239 fn.AssignDir(defaultDir);
240
241 // set the initial file name and/or directory
242 fn.MakeAbsolute(); // GTK+ needs absolute path
243 const wxString dir = fn.GetPath();
244 if ( !dir.empty() )
245 {
246 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
247 dir.fn_str());
248 }
249
250 const wxString fname = fn.GetFullName();
251 if ( style & wxFD_SAVE )
252 {
253 if ( !fname.empty() )
254 {
255 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
256 fname.fn_str());
257 }
258
259#if GTK_CHECK_VERSION(2,7,3)
260 if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3))
261 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget), TRUE);
262#endif
263 }
264 else // wxFD_OPEN
265 {
266 if ( !fname.empty() )
267 {
268 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
269 fn.GetFullPath().fn_str());
270 }
271 }
272
273 if ( style & wxFD_PREVIEW )
274 {
275 GtkWidget *previewImage = gtk_image_new();
276
277 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget),
278 previewImage);
279 g_signal_connect(m_widget, "update-preview",
280 G_CALLBACK(gtk_filedialog_update_preview_callback),
281 previewImage);
282 }
283}
284
285
286void wxFileDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event))
287{
288 EndDialog(wxID_OK);
289}
290
291int wxFileDialog::ShowModal()
292{
293 if (CreateExtraControl())
294 {
295 GtkWidget *control = m_extraControl->m_widget;
296
297 // see wxNotebook::InsertPage() for explaination
298 // why gtk_widget_unparent() is not used here
299 control->parent = NULL;
300
301 gtk_widget_show(control);
302 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(m_widget), control);
303 }
304
305 return wxDialog::ShowModal();
306}
307
308void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
309 int WXUNUSED(width), int WXUNUSED(height),
310 int WXUNUSED(sizeFlags))
311{
312}
313
314wxString wxFileDialog::GetPath() const
315{
316 return m_fc.GetPath();
317}
318
319void wxFileDialog::GetFilenames(wxArrayString& files) const
320{
321 m_fc.GetFilenames( files );
322}
323
324void wxFileDialog::GetPaths(wxArrayString& paths) const
325{
326 m_fc.GetPaths( paths );
327}
328
329void wxFileDialog::SetMessage(const wxString& message)
330{
331 m_message = message;
332 SetTitle(message);
333}
334
335void wxFileDialog::SetPath(const wxString& path)
336{
337 m_fc.SetPath( path );
338}
339
340void wxFileDialog::SetDirectory(const wxString& dir)
341{
342 m_fc.SetDirectory( dir );
343}
344
345wxString wxFileDialog::GetDirectory() const
346{
347 return m_fc.GetDirectory();
348}
349
350void wxFileDialog::SetFilename(const wxString& name)
351{
352 if (HasFdFlag(wxFD_SAVE))
353 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
354 else
355 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
356}
357
358wxString wxFileDialog::GetFilename() const
359{
360 return m_fc.GetFilename();
361}
362
363void wxFileDialog::SetWildcard(const wxString& wildCard)
364{
365 m_fc.SetWildcard( wildCard );
366}
367
368void wxFileDialog::SetFilterIndex(int filterIndex)
369{
370
371 m_fc.SetFilterIndex( filterIndex);
372}
373
374int wxFileDialog::GetFilterIndex() const
375{
376 return m_fc.GetFilterIndex();
377}
378
379#endif // wxUSE_FILEDLG