]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: filedlg.cpp | |
3 | // Purpose: wxFileDialog | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "filedlg.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/utils.h" | |
25 | #include "wx/msgdlg.h" | |
26 | #include "wx/dialog.h" | |
27 | #include "wx/filedlg.h" | |
28 | #include "wx/intl.h" | |
29 | #include "wx/log.h" | |
30 | ||
31 | #include "wx/msw/private.h" | |
32 | #endif | |
33 | ||
34 | #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__) | |
35 | #include <commdlg.h> | |
36 | #endif | |
37 | ||
38 | #include <math.h> | |
39 | #include <stdlib.h> | |
40 | #include <string.h> | |
41 | ||
42 | #include "wx/tokenzr.h" | |
43 | ||
44 | IMPLEMENT_CLASS(wxFileDialog, wxDialog) | |
45 | ||
46 | wxString wxFileSelector(const wxChar *title, | |
47 | const wxChar *defaultDir, | |
48 | const wxChar *defaultFileName, | |
49 | const wxChar *defaultExtension, | |
50 | const wxChar *filter, | |
51 | int flags, | |
52 | wxWindow *parent, | |
53 | int x, int y) | |
54 | { | |
55 | // In the original implementation, defaultExtension is passed to the | |
56 | // lpstrDefExt member of OPENFILENAME. This extension, if non-NULL, is | |
57 | // appended to the filename if the user fails to type an extension. The new | |
58 | // implementation (taken from wxFileSelectorEx) appends the extension | |
59 | // automatically, by looking at the filter specification. In fact this | |
60 | // should be better than the native Microsoft implementation because | |
61 | // Windows only allows *one* default extension, whereas here we do the | |
62 | // right thing depending on the filter the user has chosen. | |
63 | ||
64 | // If there's a default extension specified but no filter, we create a | |
65 | // suitable filter. | |
66 | ||
67 | wxString filter2; | |
68 | if ( defaultExtension && !filter ) | |
69 | filter2 = wxString(wxT("*.")) + defaultExtension; | |
70 | else if ( filter ) | |
71 | filter2 = filter; | |
72 | ||
73 | wxString defaultDirString; | |
74 | if (defaultDir) | |
75 | defaultDirString = defaultDir; | |
76 | ||
77 | wxString defaultFilenameString; | |
78 | if (defaultFileName) | |
79 | defaultFilenameString = defaultFileName; | |
80 | ||
81 | wxFileDialog fileDialog(parent, title, defaultDirString, | |
82 | defaultFilenameString, filter2, | |
83 | flags, wxPoint(x, y)); | |
84 | if( wxStrlen(defaultExtension) != 0 ) | |
85 | { | |
86 | int filterFind = 1, | |
87 | filterIndex = 0; | |
88 | ||
89 | for( unsigned int i = 0; i < filter2.Len(); i++ ) | |
90 | { | |
91 | if( filter2.GetChar(i) == wxT('|') ) | |
92 | { | |
93 | // save the start index of the new filter | |
94 | unsigned int is = i++; | |
95 | filterIndex++; | |
96 | ||
97 | // find the end of the filter | |
98 | for( ; i < filter2.Len(); i++ ) | |
99 | { | |
100 | if(filter2[i] == wxT('|')) | |
101 | break; | |
102 | } | |
103 | ||
104 | if( i-is-1 > 0 && is+1 < filter2.Len() ) | |
105 | { | |
106 | if( filter2.Mid(is+1,i-is-1).Contains(defaultExtension) ) | |
107 | // if( filter2.Mid(is+1,i-is-1) == defaultExtension ) | |
108 | { | |
109 | filterFind = filterIndex; | |
110 | break; | |
111 | } | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | fileDialog.SetFilterIndex(filterFind); | |
117 | } | |
118 | ||
119 | if ( fileDialog.ShowModal() == wxID_OK ) | |
120 | { | |
121 | wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath()); | |
122 | return wxBuffer; | |
123 | } | |
124 | else | |
125 | return wxGetEmptyString(); | |
126 | } | |
127 | ||
128 | # if __BORLANDC__ | |
129 | # include <dir.h> // for MAXPATH etc. ( Borland 3.1 ) | |
130 | # endif | |
131 | ||
132 | # ifndef MAXPATH | |
133 | # define MAXPATH 400 | |
134 | # endif | |
135 | ||
136 | # ifndef MAXDRIVE | |
137 | # define MAXDRIVE 3 | |
138 | # endif | |
139 | ||
140 | # ifndef MAXFILE | |
141 | # define MAXFILE 9 | |
142 | # endif | |
143 | ||
144 | # ifndef MAXEXT | |
145 | # define MAXEXT 5 | |
146 | # endif | |
147 | ||
148 | ||
149 | wxString wxFileSelectorEx(const wxChar *title, | |
150 | const wxChar *defaultDir, | |
151 | const wxChar *defaultFileName, | |
152 | int* defaultFilterIndex, | |
153 | const wxChar *filter, | |
154 | int flags, | |
155 | wxWindow* parent, | |
156 | int x, | |
157 | int y) | |
158 | ||
159 | { | |
160 | wxFileDialog fileDialog(parent, title ? title : wxT(""), defaultDir ? defaultDir : wxT(""), | |
161 | defaultFileName ? defaultFileName : wxT(""), filter ? filter : wxT(""), flags, wxPoint(x, y)); | |
162 | ||
163 | if ( fileDialog.ShowModal() == wxID_OK ) | |
164 | { | |
165 | *defaultFilterIndex = fileDialog.GetFilterIndex(); | |
166 | wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath()); | |
167 | return wxBuffer; | |
168 | } | |
169 | else | |
170 | return wxGetEmptyString(); | |
171 | } | |
172 | ||
173 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, | |
174 | const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard, | |
175 | long style, const wxPoint& pos) | |
176 | { | |
177 | m_message = message; | |
178 | m_dialogStyle = style; | |
179 | if ( ( m_dialogStyle & wxMULTIPLE ) && ( m_dialogStyle & wxSAVE ) ) | |
180 | m_dialogStyle &= ~wxMULTIPLE; | |
181 | m_parent = parent; | |
182 | m_path = wxT(""); | |
183 | m_fileName = defaultFileName; | |
184 | m_dir = defaultDir; | |
185 | m_wildCard = wildCard; | |
186 | m_filterIndex = 1; | |
187 | } | |
188 | ||
189 | void wxFileDialog::GetPaths(wxArrayString& paths) const | |
190 | { | |
191 | paths.Empty(); | |
192 | ||
193 | wxString dir(m_dir); | |
194 | if ( m_dir.Last() != _T('\\') ) | |
195 | dir += _T('\\'); | |
196 | ||
197 | size_t count = m_fileNames.GetCount(); | |
198 | for ( size_t n = 0; n < count; n++ ) | |
199 | { | |
200 | paths.Add(dir + m_fileNames[n]); | |
201 | } | |
202 | } | |
203 | ||
204 | int wxFileDialog::ShowModal() | |
205 | { | |
206 | HWND hWnd = 0; | |
207 | if (m_parent) hWnd = (HWND) m_parent->GetHWND(); | |
208 | ||
209 | static wxChar fileNameBuffer [ MAXPATH ]; // the file-name | |
210 | wxChar titleBuffer [ MAXFILE+1+MAXEXT ]; // the file-name, without path | |
211 | ||
212 | *fileNameBuffer = wxT('\0'); | |
213 | *titleBuffer = wxT('\0'); | |
214 | ||
215 | long msw_flags = 0; | |
216 | if ( (m_dialogStyle & wxHIDE_READONLY) || (m_dialogStyle & wxSAVE) ) | |
217 | msw_flags |= OFN_HIDEREADONLY; | |
218 | if ( m_dialogStyle & wxFILE_MUST_EXIST ) | |
219 | msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; | |
220 | if (m_dialogStyle & wxMULTIPLE ) | |
221 | msw_flags |= | |
222 | #if defined(OFN_EXPLORER) | |
223 | OFN_EXPLORER | | |
224 | #endif // OFN_EXPLORER | |
225 | OFN_ALLOWMULTISELECT; | |
226 | ||
227 | OPENFILENAME of; | |
228 | memset(&of, 0, sizeof(OPENFILENAME)); | |
229 | ||
230 | of.lpstrCustomFilter = NULL; // system should not save custom filter | |
231 | of.nMaxCustFilter = 0L; | |
232 | ||
233 | of.nFileOffset = 0; // 0-based pointer to filname in lpstFile | |
234 | of.nFileExtension = 0; // 0-based pointer to extension in lpstrFile | |
235 | of.lpstrDefExt = NULL; // no default extension | |
236 | ||
237 | of.lStructSize = sizeof(OPENFILENAME); | |
238 | of.hwndOwner = hWnd; | |
239 | of.lpstrTitle = WXSTRINGCAST m_message; | |
240 | ||
241 | ||
242 | of.lpstrFileTitle = titleBuffer; | |
243 | of.nMaxFileTitle = MAXFILE + 1 + MAXEXT; // Windows 3.0 and 3.1 | |
244 | ||
245 | // Convert forward slashes to backslashes (file selector doesn't like | |
246 | // forward slashes) | |
247 | size_t i = 0; | |
248 | size_t len = m_dir.Length(); | |
249 | for (i = 0; i < len; i++) | |
250 | if (m_dir[i] == wxT('/')) | |
251 | m_dir[i] = wxT('\\'); | |
252 | ||
253 | of.lpstrInitialDir = m_dir.c_str(); | |
254 | ||
255 | of.Flags = msw_flags; | |
256 | ||
257 | ||
258 | //=== Like Alejandro Sierra's wildcard modification >>=================== | |
259 | /* | |
260 | In wxFileSelector you can put, instead of a single wild_card, | |
261 | pairs of strings separated by '|'. | |
262 | The first string is a description, and the | |
263 | second is the wild card. You can put any number of pairs. | |
264 | ||
265 | eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2" | |
266 | ||
267 | If you put a single wild card, it works as before the modification. | |
268 | */ | |
269 | //======================================================================= | |
270 | ||
271 | wxString theFilter; | |
272 | if ( wxStrlen(m_wildCard) == 0 ) | |
273 | theFilter = wxString(wxT("*.*")); | |
274 | else | |
275 | theFilter = m_wildCard ; | |
276 | wxString filterBuffer; | |
277 | ||
278 | if ( !wxStrchr( theFilter, wxT('|') ) ) { // only one filter ==> default text | |
279 | filterBuffer.Printf(_("Files (%s)|%s"), | |
280 | theFilter.c_str(), theFilter.c_str()); | |
281 | } | |
282 | else { // more then one filter | |
283 | filterBuffer = theFilter; | |
284 | ||
285 | } | |
286 | ||
287 | filterBuffer += wxT("|"); | |
288 | // Replace | with \0 | |
289 | for (i = 0; i < filterBuffer.Len(); i++ ) { | |
290 | if ( filterBuffer.GetChar(i) == wxT('|') ) { | |
291 | filterBuffer[i] = wxT('\0'); | |
292 | } | |
293 | } | |
294 | ||
295 | of.lpstrFilter = (LPTSTR)(const wxChar *)filterBuffer; | |
296 | of.nFilterIndex = m_filterIndex + 1; | |
297 | ||
298 | //=== Setting defaultFileName >>========================================= | |
299 | ||
300 | wxStrncpy( fileNameBuffer, (const wxChar *)m_fileName, MAXPATH-1 ); | |
301 | fileNameBuffer[ MAXPATH-1 ] = wxT('\0'); | |
302 | ||
303 | of.lpstrFile = fileNameBuffer; // holds returned filename | |
304 | of.nMaxFile = MAXPATH; | |
305 | ||
306 | //== Execute FileDialog >>================================================= | |
307 | ||
308 | bool success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0) | |
309 | : (GetOpenFileName(&of) != 0); | |
310 | ||
311 | if ( success ) | |
312 | { | |
313 | m_fileNames.Empty(); | |
314 | ||
315 | if ( ( m_dialogStyle & wxMULTIPLE ) && | |
316 | #if defined(OFN_EXPLORER) | |
317 | ( fileNameBuffer[of.nFileOffset-1] == wxT('\0') ) ) | |
318 | #else | |
319 | ( fileNameBuffer[of.nFileOffset-1] == wxT(' ') ) ) | |
320 | #endif // OFN_EXPLORER | |
321 | { | |
322 | #if defined(OFN_EXPLORER) | |
323 | m_dir = fileNameBuffer; | |
324 | i = of.nFileOffset; | |
325 | m_fileName = &fileNameBuffer[i]; | |
326 | m_fileNames.Add(m_fileName); | |
327 | i += m_fileName.Len() + 1; | |
328 | ||
329 | while (fileNameBuffer[i] != wxT('\0')) | |
330 | { | |
331 | m_fileNames.Add(&fileNameBuffer[i]); | |
332 | i += wxStrlen(&fileNameBuffer[i]) + 1; | |
333 | } | |
334 | #else | |
335 | wxStringTokenizer toke(fileNameBuffer, " \t\r\n"); | |
336 | m_dir = toke.GetNextToken(); | |
337 | m_fileName = toke.GetNextToken(); | |
338 | m_fileNames.Add(m_fileName); | |
339 | ||
340 | while (toke.HasMoreTokens()) | |
341 | m_fileNames.Add(toke.GetNextToken()); | |
342 | #endif // OFN_EXPLORER | |
343 | ||
344 | wxString dir(m_dir); | |
345 | if ( m_dir.Last() != _T('\\') ) | |
346 | dir += _T('\\'); | |
347 | ||
348 | m_fileNames.Sort(); | |
349 | m_path = dir + m_fileName; | |
350 | } | |
351 | else | |
352 | { | |
353 | const wxChar* extension = NULL; | |
354 | ||
355 | //=== Adding the correct extension >>================================= | |
356 | ||
357 | m_filterIndex = (int)of.nFilterIndex - 1; | |
358 | ||
359 | if ( !of.nFileExtension || (of.nFileExtension && fileNameBuffer[ of.nFileExtension-1] != wxT('.')) ) | |
360 | { // user has typed an filename | |
361 | // without an extension: | |
362 | ||
363 | int maxFilter = (int)(of.nFilterIndex*2L-1L); | |
364 | extension = filterBuffer; | |
365 | ||
366 | for( int i = 0; i < maxFilter; i++ ) { // get extension | |
367 | extension = extension + wxStrlen( extension ) +1; | |
368 | } | |
369 | ||
370 | extension = wxStrrchr( extension, wxT('.') ); | |
371 | if ( extension // != "blabla" | |
372 | && !wxStrrchr( extension, wxT('*') ) // != "blabla.*" | |
373 | && !wxStrrchr( extension, wxT('?') ) // != "blabla.?" | |
374 | && extension[1] // != "blabla." | |
375 | && extension[1] != wxT(' ') ) // != "blabla. " | |
376 | { | |
377 | // now concat extension to the fileName: | |
378 | m_fileName = wxString(fileNameBuffer) + extension; | |
379 | ||
380 | int len = wxStrlen( fileNameBuffer ); | |
381 | wxStrncpy( fileNameBuffer + len, extension, MAXPATH - len ); | |
382 | fileNameBuffer[ MAXPATH -1 ] = wxT('\0'); | |
383 | } | |
384 | } | |
385 | ||
386 | m_path = fileNameBuffer; | |
387 | m_fileName = wxFileNameFromPath(fileNameBuffer); | |
388 | m_fileNames.Add(m_fileName); | |
389 | m_dir = wxPathOnly(fileNameBuffer); | |
390 | } | |
391 | ||
392 | ||
393 | //=== Simulating the wxOVERWRITE_PROMPT >>============================ | |
394 | ||
395 | if ( (m_dialogStyle & wxOVERWRITE_PROMPT) && | |
396 | ::wxFileExists( fileNameBuffer ) ) | |
397 | { | |
398 | wxString messageText; | |
399 | messageText.Printf(_("Replace file '%s'?"), fileNameBuffer); | |
400 | ||
401 | if ( wxMessageBox(messageText, m_message, wxYES_NO ) != wxYES ) | |
402 | { | |
403 | success = FALSE; | |
404 | } | |
405 | } | |
406 | ||
407 | } | |
408 | else | |
409 | { | |
410 | // common dialog failed - why? | |
411 | #ifdef __WXDEBUG__ | |
412 | DWORD dwErr = CommDlgExtendedError(); | |
413 | if ( dwErr != 0 ) | |
414 | { | |
415 | // this msg is only for developers | |
416 | wxLogError(wxT("Common dialog failed with error code %0lx."), | |
417 | dwErr); | |
418 | } | |
419 | //else: it was just cancelled | |
420 | #endif | |
421 | } | |
422 | ||
423 | return success ? wxID_OK : wxID_CANCEL; | |
424 | ||
425 | } | |
426 | ||
427 | // Generic file load/save dialog (for internal use only) | |
428 | static | |
429 | wxString wxDefaultFileSelector(bool load, | |
430 | const wxChar *what, | |
431 | const wxChar *extension, | |
432 | const wxChar *default_name, | |
433 | wxWindow *parent) | |
434 | { | |
435 | wxString prompt; | |
436 | wxString str; | |
437 | if (load) str = _("Load %s file"); | |
438 | else str = _("Save %s file"); | |
439 | prompt.Printf(str, what); | |
440 | ||
441 | const wxChar *ext = extension; | |
442 | if (*ext == wxT('.')) | |
443 | ext++; | |
444 | ||
445 | wxString wild; | |
446 | wild.Printf(wxT("*.%s"), ext); | |
447 | ||
448 | return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent); | |
449 | } | |
450 | ||
451 | // Generic file load dialog | |
452 | WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what, | |
453 | const wxChar *extension, | |
454 | const wxChar *default_name, | |
455 | wxWindow *parent) | |
456 | { | |
457 | return wxDefaultFileSelector(TRUE, what, extension, default_name, parent); | |
458 | } | |
459 | ||
460 | // Generic file save dialog | |
461 | WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what, | |
462 | const wxChar *extension, | |
463 | const wxChar *default_name, | |
464 | wxWindow *parent) | |
465 | { | |
466 | return wxDefaultFileSelector(FALSE, what, extension, default_name, parent); | |
467 | } | |
468 | ||
469 |