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