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