fixed broken filetype filters in file dialogs
[wxWidgets.git] / src / common / fldlgcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/fldlgcmn.cpp
3 // Purpose: wxFileDialog common functions
4 // Author: John Labenski
5 // Modified by:
6 // Created: 14.06.03 (extracted from src/*/filedlg.cpp)
7 // RCS-ID:
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "filedlg.h"
14 #endif
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/string.h"
25 #include "wx/intl.h"
26 #include "wx/window.h"
27 #endif // WX_PRECOMP
28
29 #include "wx/filedlg.h"
30
31 #if wxUSE_FILEDLG
32
33 //----------------------------------------------------------------------------
34 // wxFileDialogBase
35 //----------------------------------------------------------------------------
36
37 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase, wxDialog)
38
39 wxFileDialogBase::wxFileDialogBase(wxWindow *parent,
40 const wxString& message,
41 const wxString& defaultDir,
42 const wxString& defaultFile,
43 const wxString& wildCard,
44 long style,
45 const wxPoint& WXUNUSED(pos))
46 : m_message(message),
47 m_dir(defaultDir),
48 m_fileName(defaultFile),
49 m_wildCard(wildCard)
50 {
51 m_parent = parent;
52 m_dialogStyle = style;
53 m_filterIndex = 0;
54
55 if ( wildCard.empty() )
56 {
57 m_wildCard = wxString::Format(_("All files (%s)|%s"),
58 wxFileSelectorDefaultWildcardStr,
59 wxFileSelectorDefaultWildcardStr);
60 }
61 else // have wild card
62 {
63 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
64 if ( m_wildCard.Find(wxT('|')) == wxNOT_FOUND )
65 {
66 wxString::size_type nDot = m_wildCard.find(_T("*."));
67 if ( nDot != wxString::npos )
68 nDot++;
69 else
70 nDot = 0;
71
72 m_wildCard = wxString::Format
73 (
74 _("%s files (%s)|%s"),
75 m_wildCard.c_str() + nDot,
76 m_wildCard.c_str(),
77 m_wildCard.c_str()
78 );
79 }
80 }
81 }
82
83 // Parses the filterStr, returning the number of filters.
84 // Returns 0 if none or if there's a problem.
85 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
86
87 int wxFileDialogBase::ParseWildcard(const wxString& filterStr,
88 wxArrayString& descriptions,
89 wxArrayString& filters)
90 {
91 descriptions.Clear();
92 filters.Clear();
93
94 wxString str(filterStr);
95
96 wxString description, filter;
97 for ( int pos = 0; pos != wxNOT_FOUND; )
98 {
99 pos = str.Find(wxT('|'));
100 if ( pos == wxNOT_FOUND )
101 {
102 // if there are no '|'s at all in the string just take the entire
103 // string as filter
104 if ( filters.IsEmpty() )
105 {
106 descriptions.Add(filterStr);
107 filters.Add(filterStr);
108 }
109 else
110 {
111 wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
112 }
113
114 break;
115 }
116
117 description = str.Left(pos);
118 str = str.Mid(pos + 1);
119 pos = str.Find(wxT('|'));
120 if ( pos == wxNOT_FOUND )
121 {
122 filter = str;
123 }
124 else
125 {
126 filter = str.Left(pos);
127 str = str.Mid(pos + 1);
128 }
129
130 descriptions.Add(description);
131 filters.Add(filter);
132 }
133
134 return filters.GetCount();
135 }
136
137 wxString wxFileDialogBase::AppendExtension(const wxString &filePath,
138 const wxString &extensionList)
139 {
140 // strip off path, to avoid problems with "path.bar/foo"
141 wxString fileName = filePath.AfterLast(wxFILE_SEP_PATH);
142
143 // if fileName is of form "foo.bar" it's ok, return it
144 int idx_dot = fileName.Find(wxT('.'), TRUE);
145 if ((idx_dot != wxNOT_FOUND) && (idx_dot < (int)fileName.Len() - 1))
146 return filePath;
147
148 // get the first extension from extensionList, or all of it
149 wxString ext = extensionList.BeforeFirst(wxT(';'));
150
151 // if ext == "foo" or "foo." there's no extension
152 int idx_ext_dot = ext.Find(wxT('.'), TRUE);
153 if ((idx_ext_dot == wxNOT_FOUND) || (idx_ext_dot == (int)ext.Len() - 1))
154 return filePath;
155 else
156 ext = ext.AfterLast(wxT('.'));
157
158 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
159 if ((ext.Find(wxT('*')) != wxNOT_FOUND) ||
160 (ext.Find(wxT('?')) != wxNOT_FOUND) ||
161 (ext.Strip(wxString::both).IsEmpty()))
162 return filePath;
163
164 // if fileName doesn't have a '.' then add one
165 if (filePath.Last() != wxT('.'))
166 ext = wxT(".") + ext;
167
168 return filePath + ext;
169 }
170
171 //----------------------------------------------------------------------------
172 // wxFileDialog convenience functions
173 //----------------------------------------------------------------------------
174
175 wxString wxFileSelector(const wxChar *title,
176 const wxChar *defaultDir,
177 const wxChar *defaultFileName,
178 const wxChar *defaultExtension,
179 const wxChar *filter,
180 int flags,
181 wxWindow *parent,
182 int x, int y)
183 {
184 // The defaultExtension, if non-NULL, is
185 // appended to the filename if the user fails to type an extension. The new
186 // implementation (taken from wxFileSelectorEx) appends the extension
187 // automatically, by looking at the filter specification. In fact this
188 // should be better than the native Microsoft implementation because
189 // Windows only allows *one* default extension, whereas here we do the
190 // right thing depending on the filter the user has chosen.
191
192 // If there's a default extension specified but no filter, we create a
193 // suitable filter.
194
195 wxString filter2;
196 if ( defaultExtension && !filter )
197 filter2 = wxString(wxT("*.")) + defaultExtension;
198 else if ( filter )
199 filter2 = filter;
200
201 wxString defaultDirString;
202 if (defaultDir)
203 defaultDirString = defaultDir;
204
205 wxString defaultFilenameString;
206 if (defaultFileName)
207 defaultFilenameString = defaultFileName;
208
209 wxFileDialog fileDialog(parent, title, defaultDirString,
210 defaultFilenameString, filter2,
211 flags, wxPoint(x, y));
212
213 // if filter is of form "All files (*)|*|..." set correct filter index
214 if((wxStrlen(defaultExtension) != 0) && (filter2.Find(wxT('|')) != wxNOT_FOUND))
215 {
216 int filterIndex = 0;
217
218 wxArrayString descriptions, filters;
219 // don't care about errors, handled already by wxFileDialog
220 (void)wxFileDialogBase::ParseWildcard(filter2, descriptions, filters);
221 for (size_t n=0; n<filters.GetCount(); n++)
222 {
223 if (filters[n].Contains(defaultExtension))
224 {
225 filterIndex = n;
226 break;
227 }
228 }
229
230 if (filterIndex > 0)
231 fileDialog.SetFilterIndex(filterIndex);
232 }
233
234 wxString filename;
235 if ( fileDialog.ShowModal() == wxID_OK )
236 {
237 filename = fileDialog.GetPath();
238 }
239
240 return filename;
241 }
242
243 //----------------------------------------------------------------------------
244 // wxFileSelectorEx
245 //----------------------------------------------------------------------------
246
247 wxString wxFileSelectorEx(const wxChar *title,
248 const wxChar *defaultDir,
249 const wxChar *defaultFileName,
250 int* defaultFilterIndex,
251 const wxChar *filter,
252 int flags,
253 wxWindow* parent,
254 int x,
255 int y)
256
257 {
258 wxFileDialog fileDialog(parent,
259 title ? title : wxT(""),
260 defaultDir ? defaultDir : wxT(""),
261 defaultFileName ? defaultFileName : wxT(""),
262 filter ? filter : wxT(""),
263 flags, wxPoint(x, y));
264
265 wxString filename;
266 if ( fileDialog.ShowModal() == wxID_OK )
267 {
268 if ( defaultFilterIndex )
269 *defaultFilterIndex = fileDialog.GetFilterIndex();
270
271 filename = fileDialog.GetPath();
272 }
273
274 return filename;
275 }
276
277 //----------------------------------------------------------------------------
278 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
279 //----------------------------------------------------------------------------
280
281 static wxString wxDefaultFileSelector(bool load,
282 const wxChar *what,
283 const wxChar *extension,
284 const wxChar *default_name,
285 wxWindow *parent)
286 {
287 wxString prompt;
288 wxString str;
289 if (load)
290 str = _("Load %s file");
291 else
292 str = _("Save %s file");
293 prompt.Printf(str, what);
294
295 wxString wild;
296 const wxChar *ext = extension;
297 if ( ext )
298 {
299 if ( *ext == wxT('.') )
300 ext++;
301
302 wild.Printf(wxT("*.%s"), ext);
303 }
304 else // no extension specified
305 {
306 wild = wxFileSelectorDefaultWildcardStr;
307 }
308
309 return wxFileSelector(prompt, NULL, default_name, ext, wild,
310 load ? wxOPEN : wxSAVE, parent);
311 }
312
313 //----------------------------------------------------------------------------
314 // wxLoadFileSelector
315 //----------------------------------------------------------------------------
316
317 WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
318 const wxChar *extension,
319 const wxChar *default_name,
320 wxWindow *parent)
321 {
322 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
323 }
324
325 //----------------------------------------------------------------------------
326 // wxSaveFileSelector
327 //----------------------------------------------------------------------------
328
329 WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
330 const wxChar *extension,
331 const wxChar *default_name,
332 wxWindow *parent)
333 {
334 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
335 }
336
337 #endif // wxUSE_FILEDLG
338