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