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