]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
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 | |
e15e548b | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
ba681060 | 13 | #pragma implementation "filedlg.h" |
2bda0e17 KB |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
ba681060 | 20 | #pragma hdrstop |
2bda0e17 KB |
21 | #endif |
22 | ||
23 | #ifndef WX_PRECOMP | |
ba681060 VZ |
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" | |
2662e49e | 29 | #include "wx/log.h" |
2bda0e17 | 30 | |
8f177c8e VZ |
31 | #include "wx/msw/private.h" |
32 | #endif | |
2bda0e17 | 33 | |
5ea105e0 | 34 | #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__) |
ba681060 | 35 | #include <commdlg.h> |
2bda0e17 KB |
36 | #endif |
37 | ||
2bda0e17 KB |
38 | #include <math.h> |
39 | #include <stdlib.h> | |
40 | #include <string.h> | |
41 | ||
8f177c8e VZ |
42 | #include "wx/tokenzr.h" |
43 | ||
44 | IMPLEMENT_CLASS(wxFileDialog, wxDialog) | |
2bda0e17 | 45 | |
837e5743 OK |
46 | wxString wxFileSelector(const wxChar *title, |
47 | const wxChar *defaultDir, | |
48 | const wxChar *defaultFileName, | |
49 | const wxChar *defaultExtension, | |
50 | const wxChar *filter, | |
ba681060 VZ |
51 | int flags, |
52 | wxWindow *parent, | |
53 | int x, int y) | |
2bda0e17 | 54 | { |
1f2f0331 VZ |
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; | |
e15e548b | 68 | if ( defaultExtension && !filter ) |
223d09f6 | 69 | filter2 = wxString(wxT("*.")) + defaultExtension; |
e15e548b VZ |
70 | else if ( filter ) |
71 | filter2 = filter; | |
72 | ||
73 | wxString defaultDirString; | |
74 | if (defaultDir) | |
75 | defaultDirString = defaultDir; | |
e15e548b VZ |
76 | |
77 | wxString defaultFilenameString; | |
78 | if (defaultFileName) | |
79 | defaultFilenameString = defaultFileName; | |
1f2f0331 VZ |
80 | |
81 | wxFileDialog fileDialog(parent, title, defaultDirString, | |
82 | defaultFilenameString, filter2, | |
83 | flags, wxPoint(x, y)); | |
837e5743 | 84 | if( wxStrlen(defaultExtension) != 0 ) |
1f2f0331 VZ |
85 | { |
86 | int filterFind = 1, | |
87 | filterIndex = 0; | |
88 | ||
89 | for( unsigned int i = 0; i < filter2.Len(); i++ ) | |
90 | { | |
223d09f6 | 91 | if( filter2.GetChar(i) == wxT('|') ) |
1f2f0331 VZ |
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 | { | |
223d09f6 | 100 | if(filter2[i] == wxT('|')) |
1f2f0331 VZ |
101 | break; |
102 | } | |
103 | ||
104 | if( i-is-1 > 0 && is+1 < filter2.Len() ) | |
105 | { | |
574c0bbf JS |
106 | if( filter2.Mid(is+1,i-is-1).Contains(defaultExtension) ) |
107 | // if( filter2.Mid(is+1,i-is-1) == defaultExtension ) | |
1f2f0331 VZ |
108 | { |
109 | filterFind = filterIndex; | |
110 | break; | |
111 | } | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | fileDialog.SetFilterIndex(filterFind); | |
117 | } | |
118 | ||
e15e548b | 119 | if ( fileDialog.ShowModal() == wxID_OK ) |
1f2f0331 | 120 | { |
837e5743 | 121 | wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath()); |
e15e548b | 122 | return wxBuffer; |
1f2f0331 | 123 | } |
e15e548b | 124 | else |
ba681060 | 125 | return wxGetEmptyString(); |
2bda0e17 KB |
126 | } |
127 | ||
128 | # if __BORLANDC__ | |
1f2f0331 | 129 | # include <dir.h> // for MAXPATH etc. ( Borland 3.1 ) |
2bda0e17 KB |
130 | # endif |
131 | ||
132 | # ifndef MAXPATH | |
1f2f0331 | 133 | # define MAXPATH 400 |
2bda0e17 KB |
134 | # endif |
135 | ||
136 | # ifndef MAXDRIVE | |
137 | # define MAXDRIVE 3 | |
138 | # endif | |
139 | ||
1f2f0331 | 140 | # ifndef MAXFILE |
2bda0e17 KB |
141 | # define MAXFILE 9 |
142 | # endif | |
143 | ||
144 | # ifndef MAXEXT | |
145 | # define MAXEXT 5 | |
146 | # endif | |
147 | ||
148 | ||
837e5743 OK |
149 | wxString wxFileSelectorEx(const wxChar *title, |
150 | const wxChar *defaultDir, | |
151 | const wxChar *defaultFileName, | |
e15e548b | 152 | int* defaultFilterIndex, |
837e5743 | 153 | const wxChar *filter, |
e15e548b VZ |
154 | int flags, |
155 | wxWindow* parent, | |
156 | int x, | |
157 | int y) | |
2bda0e17 KB |
158 | |
159 | { | |
223d09f6 KB |
160 | wxFileDialog fileDialog(parent, title ? title : wxT(""), defaultDir ? defaultDir : wxT(""), |
161 | defaultFileName ? defaultFileName : wxT(""), filter ? filter : wxT(""), flags, wxPoint(x, y)); | |
2bda0e17 | 162 | |
e15e548b VZ |
163 | if ( fileDialog.ShowModal() == wxID_OK ) |
164 | { | |
165 | *defaultFilterIndex = fileDialog.GetFilterIndex(); | |
837e5743 | 166 | wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath()); |
e15e548b VZ |
167 | return wxBuffer; |
168 | } | |
169 | else | |
ba681060 | 170 | return wxGetEmptyString(); |
2bda0e17 KB |
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; | |
c61f4f6d VZ |
179 | if ( ( m_dialogStyle & wxMULTIPLE ) && ( m_dialogStyle & wxSAVE ) ) |
180 | m_dialogStyle &= ~wxMULTIPLE; | |
2bda0e17 | 181 | m_parent = parent; |
223d09f6 | 182 | m_path = wxT(""); |
e15e548b VZ |
183 | m_fileName = defaultFileName; |
184 | m_dir = defaultDir; | |
185 | m_wildCard = wildCard; | |
c61f4f6d | 186 | m_filterIndex = 1; |
2bda0e17 KB |
187 | } |
188 | ||
c61f4f6d VZ |
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() | |
2bda0e17 | 205 | { |
1f2f0331 VZ |
206 | HWND hWnd = 0; |
207 | if (m_parent) hWnd = (HWND) m_parent->GetHWND(); | |
2bda0e17 | 208 | |
837e5743 OK |
209 | static wxChar fileNameBuffer [ MAXPATH ]; // the file-name |
210 | wxChar titleBuffer [ MAXFILE+1+MAXEXT ]; // the file-name, without path | |
2bda0e17 | 211 | |
223d09f6 KB |
212 | *fileNameBuffer = wxT('\0'); |
213 | *titleBuffer = wxT('\0'); | |
2bda0e17 | 214 | |
2bda0e17 | 215 | long msw_flags = 0; |
e15e548b | 216 | if ( (m_dialogStyle & wxHIDE_READONLY) || (m_dialogStyle & wxSAVE) ) |
1f2f0331 | 217 | msw_flags |= OFN_HIDEREADONLY; |
e15e548b | 218 | if ( m_dialogStyle & wxFILE_MUST_EXIST ) |
1f2f0331 | 219 | msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; |
c61f4f6d VZ |
220 | if (m_dialogStyle & wxMULTIPLE ) |
221 | msw_flags |= | |
222 | #if defined(OFN_EXPLORER) | |
223 | OFN_EXPLORER | | |
224 | #endif // OFN_EXPLORER | |
225 | OFN_ALLOWMULTISELECT; | |
2bda0e17 | 226 | |
e15e548b VZ |
227 | OPENFILENAME of; |
228 | memset(&of, 0, sizeof(OPENFILENAME)); | |
2bda0e17 KB |
229 | |
230 | of.lpstrCustomFilter = NULL; // system should not save custom filter | |
e15e548b | 231 | of.nMaxCustFilter = 0L; |
2bda0e17 | 232 | |
e15e548b VZ |
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 | |
2bda0e17 | 236 | |
e15e548b VZ |
237 | of.lStructSize = sizeof(OPENFILENAME); |
238 | of.hwndOwner = hWnd; | |
837e5743 | 239 | of.lpstrTitle = WXSTRINGCAST m_message; |
2bda0e17 KB |
240 | |
241 | ||
e15e548b VZ |
242 | of.lpstrFileTitle = titleBuffer; |
243 | of.nMaxFileTitle = MAXFILE + 1 + MAXEXT; // Windows 3.0 and 3.1 | |
2bda0e17 | 244 | |
0bc9b25e JS |
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++) | |
223d09f6 KB |
250 | if (m_dir[i] == wxT('/')) |
251 | m_dir[i] = wxT('\\'); | |
0bc9b25e | 252 | |
837e5743 | 253 | of.lpstrInitialDir = m_dir.c_str(); |
2bda0e17 | 254 | |
e15e548b | 255 | of.Flags = msw_flags; |
2bda0e17 KB |
256 | |
257 | ||
2bda0e17 KB |
258 | //=== Like Alejandro Sierra's wildcard modification >>=================== |
259 | /* | |
1f2f0331 VZ |
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. | |
2bda0e17 | 264 | |
1f2f0331 | 265 | eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2" |
2bda0e17 | 266 | |
1f2f0331 VZ |
267 | If you put a single wild card, it works as before the modification. |
268 | */ | |
2bda0e17 KB |
269 | //======================================================================= |
270 | ||
4dba84be | 271 | wxString theFilter; |
837e5743 | 272 | if ( wxStrlen(m_wildCard) == 0 ) |
223d09f6 | 273 | theFilter = wxString(wxT("*.*")); |
4dba84be JS |
274 | else |
275 | theFilter = m_wildCard ; | |
1f2f0331 | 276 | wxString filterBuffer; |
2bda0e17 | 277 | |
223d09f6 | 278 | if ( !wxStrchr( theFilter, wxT('|') ) ) { // only one filter ==> default text |
1f2f0331 VZ |
279 | filterBuffer.Printf(_("Files (%s)|%s"), |
280 | theFilter.c_str(), theFilter.c_str()); | |
e15e548b | 281 | } |
1f2f0331 VZ |
282 | else { // more then one filter |
283 | filterBuffer = theFilter; | |
2bda0e17 | 284 | |
574c0bbf JS |
285 | } |
286 | ||
223d09f6 | 287 | filterBuffer += wxT("|"); |
574c0bbf | 288 | // Replace | with \0 |
0bc9b25e | 289 | for (i = 0; i < filterBuffer.Len(); i++ ) { |
223d09f6 KB |
290 | if ( filterBuffer.GetChar(i) == wxT('|') ) { |
291 | filterBuffer[i] = wxT('\0'); | |
e15e548b VZ |
292 | } |
293 | } | |
2bda0e17 | 294 | |
837e5743 | 295 | of.lpstrFilter = (LPTSTR)(const wxChar *)filterBuffer; |
c61f4f6d | 296 | of.nFilterIndex = m_filterIndex; |
2bda0e17 KB |
297 | |
298 | //=== Setting defaultFileName >>========================================= | |
299 | ||
837e5743 | 300 | wxStrncpy( fileNameBuffer, (const wxChar *)m_fileName, MAXPATH-1 ); |
223d09f6 | 301 | fileNameBuffer[ MAXPATH-1 ] = wxT('\0'); |
2bda0e17 | 302 | |
e15e548b VZ |
303 | of.lpstrFile = fileNameBuffer; // holds returned filename |
304 | of.nMaxFile = MAXPATH; | |
2bda0e17 KB |
305 | |
306 | //== Execute FileDialog >>================================================= | |
307 | ||
1f2f0331 VZ |
308 | bool success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0) |
309 | : (GetOpenFileName(&of) != 0); | |
2bda0e17 KB |
310 | |
311 | if ( success ) | |
312 | { | |
c61f4f6d VZ |
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; | |
1f2f0331 | 354 | |
c61f4f6d | 355 | //=== Adding the correct extension >>================================= |
2bda0e17 | 356 | |
c61f4f6d | 357 | m_filterIndex = (int)of.nFilterIndex; |
2bda0e17 | 358 | |
c61f4f6d VZ |
359 | if ( of.nFileExtension && fileNameBuffer[ of.nFileExtension-1] != wxT('.') ) |
360 | { // user has typed an filename | |
361 | // without an extension: | |
2bda0e17 | 362 | |
c61f4f6d VZ |
363 | int maxFilter = (int)(of.nFilterIndex*2L-1L); |
364 | extension = filterBuffer; | |
2bda0e17 | 365 | |
c61f4f6d VZ |
366 | for( int i = 0; i < maxFilter; i++ ) { // get extension |
367 | extension = extension + wxStrlen( extension ) +1; | |
368 | } | |
2bda0e17 | 369 | |
c61f4f6d VZ |
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; | |
2bda0e17 | 379 | |
c61f4f6d VZ |
380 | int len = wxStrlen( fileNameBuffer ); |
381 | wxStrncpy( fileNameBuffer + len, extension, MAXPATH - len ); | |
382 | fileNameBuffer[ MAXPATH -1 ] = wxT('\0'); | |
383 | } | |
2bda0e17 | 384 | } |
2bda0e17 | 385 | |
c61f4f6d VZ |
386 | m_path = fileNameBuffer; |
387 | m_fileName = wxFileNameFromPath(fileNameBuffer); | |
388 | m_fileNames.Add(m_fileName); | |
389 | m_dir = wxPathOnly(fileNameBuffer); | |
390 | } | |
2bda0e17 KB |
391 | |
392 | ||
e15e548b | 393 | //=== Simulating the wxOVERWRITE_PROMPT >>============================ |
2bda0e17 | 394 | |
1f2f0331 VZ |
395 | if ( (m_dialogStyle & wxOVERWRITE_PROMPT) && |
396 | ::wxFileExists( fileNameBuffer ) ) | |
2bda0e17 | 397 | { |
1f2f0331 VZ |
398 | wxString messageText; |
399 | messageText.Printf(_("Replace file '%s'?"), fileNameBuffer); | |
2bda0e17 | 400 | |
1f2f0331 | 401 | if ( wxMessageBox(messageText, m_message, wxYES_NO ) != wxYES ) |
2bda0e17 KB |
402 | { |
403 | success = FALSE; | |
e15e548b | 404 | } |
e15e548b | 405 | } |
2bda0e17 | 406 | |
7cc98b3e VZ |
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 | |
223d09f6 | 416 | wxLogError(wxT("Common dialog failed with error code %0lx."), |
7cc98b3e VZ |
417 | dwErr); |
418 | } | |
419 | //else: it was just cancelled | |
420 | #endif | |
421 | } | |
2bda0e17 | 422 | |
7cc98b3e | 423 | return success ? wxID_OK : wxID_CANCEL; |
2bda0e17 KB |
424 | |
425 | } | |
426 | ||
ba681060 VZ |
427 | // Generic file load/save dialog (for internal use only) |
428 | static | |
429 | wxString wxDefaultFileSelector(bool load, | |
837e5743 OK |
430 | const wxChar *what, |
431 | const wxChar *extension, | |
432 | const wxChar *default_name, | |
ba681060 | 433 | wxWindow *parent) |
2bda0e17 | 434 | { |
1f2f0331 | 435 | wxString prompt; |
837e5743 OK |
436 | wxString str; |
437 | if (load) str = _("Load %s file"); | |
438 | else str = _("Save %s file"); | |
1f2f0331 VZ |
439 | prompt.Printf(str, what); |
440 | ||
837e5743 | 441 | const wxChar *ext = extension; |
223d09f6 | 442 | if (*ext == wxT('.')) |
1f2f0331 | 443 | ext++; |
2bda0e17 | 444 | |
1f2f0331 | 445 | wxString wild; |
223d09f6 | 446 | wild.Printf(wxT("*.%s"), ext); |
2bda0e17 KB |
447 | |
448 | return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent); | |
449 | } | |
450 | ||
451 | // Generic file load dialog | |
837e5743 OK |
452 | WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what, |
453 | const wxChar *extension, | |
454 | const wxChar *default_name, | |
ba681060 | 455 | wxWindow *parent) |
2bda0e17 | 456 | { |
ba681060 | 457 | return wxDefaultFileSelector(TRUE, what, extension, default_name, parent); |
2bda0e17 KB |
458 | } |
459 | ||
2bda0e17 | 460 | // Generic file save dialog |
837e5743 OK |
461 | WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what, |
462 | const wxChar *extension, | |
463 | const wxChar *default_name, | |
ba681060 | 464 | wxWindow *parent) |
2bda0e17 | 465 | { |
ba681060 | 466 | return wxDefaultFileSelector(FALSE, what, extension, default_name, parent); |
2bda0e17 KB |
467 | } |
468 | ||
c61f4f6d | 469 |