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