fixed subtle SetDirectory bug re-introduced by latest changes (explained in comment...
[wxWidgets.git] / src / gtk / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/filedlg.cpp
3 // Purpose: native implementation of wxFileDialog
4 // Author: Robert Roebling, Zbigniew Zagorski
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "filedlg.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #if wxUSE_FILEDLG && defined(__WXGTK24__)
18
19 #include "wx/filedlg.h"
20 #include "wx/utils.h"
21 #include "wx/intl.h"
22 #include "wx/filename.h"
23 #include "wx/msgdlg.h"
24
25 #include <gtk/gtk.h>
26 #include "wx/gtk/private.h"
27
28 //-----------------------------------------------------------------------------
29 // idle system
30 //-----------------------------------------------------------------------------
31
32 extern void wxapp_install_idle_handler();
33 extern bool g_isIdle;
34
35 //-----------------------------------------------------------------------------
36 // "clicked" for OK-button
37 //-----------------------------------------------------------------------------
38
39 static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
40 {
41 int style = dialog->GetStyle();
42 gchar* text = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
43 wxString filename(wxGTK_CONV_BACK(text));
44
45 if ((style & wxSAVE) && (style & wxOVERWRITE_PROMPT))
46 {
47 if (wxFileExists(filename))
48 {
49 wxString msg;
50 msg.Printf(
51 _("File '%s' already exists, do you really want to overwrite it?"),
52 filename.c_str());
53
54 wxMessageDialog dlg(dialog, msg, _("Confirm"),
55 wxYES_NO | wxICON_QUESTION);
56 if (dlg.ShowModal() != wxID_YES)
57 return;
58 }
59 }
60 else if ((style & wxOPEN) && ( style & wxFILE_MUST_EXIST))
61 {
62 if (!wxFileExists( filename ))
63 {
64 wxMessageDialog dlg(dialog,
65 _("Please choose an existing file."),
66 _("Error"), wxOK | wxICON_ERROR);
67 dlg.ShowModal();
68
69 return;
70 }
71 }
72
73 // change to the directory where the user went if asked
74 if (style & wxCHANGE_DIR)
75 {
76 wxString cwd;
77 wxSplitPath(filename, &cwd, NULL, NULL);
78
79 if (cwd != wxGetCwd())
80 {
81 wxSetWorkingDirectory(cwd);
82 }
83 }
84
85 dialog->SetPath(filename);
86 dialog->UpdateFromDialog();
87
88 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
89 event.SetEventObject(dialog);
90 dialog->GetEventHandler()->ProcessEvent(event);
91 }
92
93 //-----------------------------------------------------------------------------
94 // "clicked" for Cancel-button
95 //-----------------------------------------------------------------------------
96
97 static void gtk_filedialog_cancel_callback(GtkWidget *WXUNUSED(w),
98 wxFileDialog *dialog)
99 {
100 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
101 event.SetEventObject(dialog);
102 dialog->GetEventHandler()->ProcessEvent(event);
103 }
104
105 static void gtk_filedialog_response_callback(GtkWidget *w,
106 int response,
107 wxFileDialog *dialog)
108 {
109 wxapp_install_idle_handler();
110
111 if (response == GTK_RESPONSE_CANCEL)
112 gtk_filedialog_cancel_callback(w, dialog);
113 else
114 gtk_filedialog_ok_callback(w, dialog);
115 }
116
117 //-----------------------------------------------------------------------------
118 // wxFileDialog
119 //-----------------------------------------------------------------------------
120
121 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
122
123 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
124 const wxString& defaultDir,
125 const wxString& defaultFileName,
126 const wxString& wildCard,
127 long style, const wxPoint& pos)
128 : wxFileDialogBase(parent, message, defaultDir, defaultFileName,
129 wildCard, style, pos)
130 {
131 m_needParent = FALSE;
132
133 if (!PreCreation(parent, pos, wxDefaultSize) ||
134 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
135 wxDefaultValidator, wxT("filedialog")))
136 {
137 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
138 return;
139 }
140
141 bool multiple = (style & wxMULTIPLE) == wxMULTIPLE;
142 GtkFileChooserAction gtk_action;
143 GtkWindow* gtk_parent = NULL;
144 if (parent)
145 gtk_parent = GTK_WINDOW(parent->m_widget);
146
147 gchar* ok_btn_stock;
148 if ((style & wxSAVE) == wxSAVE)
149 {
150 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
151 ok_btn_stock = GTK_STOCK_SAVE;
152 }
153 else
154 {
155 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
156 ok_btn_stock = GTK_STOCK_OPEN;
157 }
158 m_widget = gtk_file_chooser_dialog_new(
159 wxGTK_CONV(m_message),
160 gtk_parent,
161 gtk_action,
162 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
163 ok_btn_stock, GTK_RESPONSE_ACCEPT,
164 NULL);
165
166 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), multiple);
167
168 gtk_signal_connect(GTK_OBJECT(m_widget),
169 "response",
170 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback),
171 (gpointer*)this);
172
173 m_path = m_dir;
174 if (!m_path.empty() && m_path.Last() != wxT('/'))
175 m_path += wxT('/');
176 m_path += m_fileName;
177 SetPath(m_path);
178
179 SetWildcard(wildCard);
180 SetFilterIndex(0);
181 }
182
183 wxFileDialog::~wxFileDialog()
184 {
185 m_widget = NULL;
186 }
187
188 void wxFileDialog::GetFilenames(wxArrayString& files) const
189 {
190 GetPaths(files);
191 for (size_t n = 0; n < files.GetCount(); n++ )
192 {
193 wxString name,ext;
194 wxSplitPath(files[n], NULL, &name, &ext);
195 if (!ext.IsEmpty())
196 {
197 name += wxT(".");
198 name += ext;
199 }
200 files[n] = name;
201 }
202 }
203
204 void wxFileDialog::GetPaths(wxArrayString& paths) const
205 {
206 paths.Empty();
207 if (GetWindowStyle() & wxMULTIPLE)
208 {
209 GSList *gpathsi =
210 gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget));
211 GSList *gpaths = gpathsi;
212 while (gpathsi)
213 {
214 wxString file = wxGTK_CONV_BACK((gchar*) gpathsi->data);
215 paths.Add(file);
216 g_free(gpathsi->data);
217 gpathsi = gpathsi->next;
218 }
219 if (gpaths)
220 g_slist_free(gpaths);
221 }
222 else
223 {
224 paths.Add(m_fileName);
225 }
226 }
227
228 void wxFileDialog::SetMessage(const wxString& message)
229 {
230 m_message = message;
231 SetTitle(message);
232 }
233
234 void wxFileDialog::SetPath(const wxString& path)
235 {
236 if (path.empty()) return;
237
238 wxFileName fn(path);
239 m_path = fn.GetFullPath();
240 m_dir = fn.GetPath();
241 m_fileName = fn.GetFullName();
242 UpdateDialog();
243 }
244
245 void wxFileDialog::SetDirectory(const wxString& dir)
246 {
247 if (wxDirExists(dir))
248 {
249 m_dir = dir;
250 m_path = wxFileName(m_dir, m_fileName).GetFullPath();
251 UpdateDialog();
252 }
253 }
254
255 void wxFileDialog::SetFilename(const wxString& name)
256 {
257 m_fileName = name;
258 m_path = wxFileName(m_dir, m_fileName).GetFullPath();
259 UpdateDialog();
260 }
261
262 void wxFileDialog::UpdateDialog()
263 {
264 // set currently selected directory to match the path:
265 if (!m_dir.empty() && wxDirExists(m_dir))
266 {
267 // NB: This is important -- if we set directory only and not the path,
268 // then dialog will still remember old path set using previous
269 // call to gtk_chooser_set_filename. If the previous directory
270 // was a subdirectory of the directory we want to select now,
271 // the dialog would still contain directory selector controls
272 // for the subdirectory (with the parent directory selected),
273 // instead of showing only the parent directory as expected.
274 // This way, we force GtkFileChooser to really change the
275 // directory. Finally, it doesn't have to be done if filename
276 // is not empty because of the code that sets the filename below.
277 if (m_fileName.empty())
278 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
279 wxGTK_CONV(m_dir));
280
281 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
282 wxGTK_CONV(m_dir));
283 }
284
285 // if the user set only the directory (e.g. by calling SetDirectory)
286 // and not the default filename, then we don't want to set the filename:
287 if (!m_fileName.empty())
288 {
289 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
290 wxGTK_CONV(m_path));
291
292 // pre-fill the filename when saving, too (there's no text entry
293 // control when opening a file, so it doesn't make sense to
294 // do this when opening files):
295 if (GetWindowStyle() & wxSAVE)
296 {
297 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
298 wxGTK_CONV(m_fileName));
299 }
300 }
301 }
302
303 void wxFileDialog::SetWildcard(const wxString& wildCard)
304 {
305 m_wildCard = wildCard;
306
307 GtkFileChooser* chooser = GTK_FILE_CHOOSER(m_widget);
308
309 // empty current filter list:
310 GSList* ifilters = gtk_file_chooser_list_filters(chooser);
311 GSList* filters = ifilters;
312 while (ifilters)
313 {
314 gtk_file_chooser_remove_filter(chooser,GTK_FILE_FILTER(ifilters->data));
315 ifilters = ifilters->next;
316 }
317 g_slist_free(filters);
318
319 // parse filters
320 wxArrayString wildDescriptions, wildFilters;
321 if (!wxParseCommonDialogsFilter(m_wildCard, wildDescriptions, wildFilters))
322 {
323 wxFAIL_MSG( wxT("Wrong file type description") );
324 }
325 else
326 {
327 // add parsed to GtkChooser
328 for (size_t n = 0; n < wildFilters.GetCount(); n++)
329 {
330 GtkFileFilter* filter = gtk_file_filter_new();
331 gtk_file_filter_set_name(filter,wxGTK_CONV(wildDescriptions[n]));
332 wxString after = wildFilters[n];
333 do
334 {
335 wxString ext = after.BeforeFirst(wxT(';'));
336 gtk_file_filter_add_pattern(filter,wxGTK_CONV(ext));
337 if (after.Find(wxT(';')) == wxNOT_FOUND)
338 break;
339 after = after.AfterLast(wxT(';'));
340 }
341 while (!after.empty());
342
343 gtk_file_chooser_add_filter(chooser, filter);
344 }
345 }
346 }
347
348 void wxFileDialog::SetFilterIndex(int filterIndex)
349 {
350 m_filterIndex = filterIndex;
351
352 GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget);
353 GSList *fnode = gtk_file_chooser_list_filters(chooser);
354 GSList *filters = fnode;
355 int i = 0;
356 while (fnode)
357 {
358 if (i == filterIndex)
359 {
360 gtk_file_chooser_set_filter(chooser, GTK_FILE_FILTER(fnode->data));
361 m_filterIndex = i;
362 break;
363 }
364 i++;
365 fnode = fnode->next;
366 }
367 g_slist_free(filters);
368 }
369
370 void wxFileDialog::UpdateFromDialog()
371 {
372 // update filterIndex
373 GSList *fnode = gtk_file_chooser_list_filters(GTK_FILE_CHOOSER(m_widget));
374 GSList *filters = fnode;
375 GtkFileFilter *current =
376 gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(m_widget));
377
378 int i = 0;
379 m_filterIndex = 0;
380 while (fnode)
381 {
382 if (fnode->data == (gpointer)current)
383 {
384 m_filterIndex = i;
385 break;
386 }
387 i++;
388 fnode = fnode->next;
389 }
390 g_slist_free(filters);
391 }
392
393 #endif // wxUSE_FILEDLG && defined(__WXGTK24__)