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