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