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