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