]> git.saurik.com Git - wxWidgets.git/blame - src/msw/filedlg.cpp
compilation fix
[wxWidgets.git] / src / msw / filedlg.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
f6bcfd97 2// Name: src/msw/filedlg.cpp
2bda0e17
KB
3// Purpose: wxFileDialog
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
e15e548b 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
f6bcfd97
BP
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
ba681060 21 #pragma implementation "filedlg.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
ba681060 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_FILEDLG
32
2bda0e17 33#ifndef WX_PRECOMP
ba681060
VZ
34 #include "wx/utils.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dialog.h"
37 #include "wx/filedlg.h"
2b5f62a0 38 #include "wx/filefn.h"
ba681060 39 #include "wx/intl.h"
2662e49e 40 #include "wx/log.h"
f6bcfd97 41 #include "wx/app.h"
8f177c8e 42#endif
2bda0e17 43
f6bcfd97
BP
44#include "wx/msw/private.h"
45
b4da152e 46#if !defined(__WIN32__) || defined(__SALFORDC__)
ba681060 47 #include <commdlg.h>
2bda0e17
KB
48#endif
49
2bda0e17
KB
50#include <math.h>
51#include <stdlib.h>
52#include <string.h>
53
8ad9ca97 54#include "wx/filename.h"
8f177c8e
VZ
55#include "wx/tokenzr.h"
56
6e8aa701
VZ
57#ifndef OFN_EXPLORER
58 #define OFN_EXPLORER 0x00080000
59#endif
60
f6bcfd97
BP
61// ----------------------------------------------------------------------------
62// constants
63// ----------------------------------------------------------------------------
64
65#ifdef __WIN32__
2b5f62a0 66# define wxMAXPATH 65534
f6bcfd97
BP
67#else
68# define wxMAXPATH 1024
69#endif
70
71# define wxMAXFILE 1024
72
73# define wxMAXEXT 5
74
75// ============================================================================
76// implementation
77// ============================================================================
78
79// ----------------------------------------------------------------------------
80// wxWin macros
81// ----------------------------------------------------------------------------
82
8f177c8e 83IMPLEMENT_CLASS(wxFileDialog, wxDialog)
2bda0e17 84
f6bcfd97
BP
85// ----------------------------------------------------------------------------
86// global functions
87// ----------------------------------------------------------------------------
88
837e5743
OK
89wxString wxFileSelector(const wxChar *title,
90 const wxChar *defaultDir,
91 const wxChar *defaultFileName,
92 const wxChar *defaultExtension,
93 const wxChar *filter,
ba681060
VZ
94 int flags,
95 wxWindow *parent,
96 int x, int y)
2bda0e17 97{
1f2f0331
VZ
98 // In the original implementation, defaultExtension is passed to the
99 // lpstrDefExt member of OPENFILENAME. This extension, if non-NULL, is
100 // appended to the filename if the user fails to type an extension. The new
101 // implementation (taken from wxFileSelectorEx) appends the extension
102 // automatically, by looking at the filter specification. In fact this
103 // should be better than the native Microsoft implementation because
104 // Windows only allows *one* default extension, whereas here we do the
105 // right thing depending on the filter the user has chosen.
106
107 // If there's a default extension specified but no filter, we create a
108 // suitable filter.
109
110 wxString filter2;
e15e548b 111 if ( defaultExtension && !filter )
223d09f6 112 filter2 = wxString(wxT("*.")) + defaultExtension;
e15e548b
VZ
113 else if ( filter )
114 filter2 = filter;
115
116 wxString defaultDirString;
117 if (defaultDir)
118 defaultDirString = defaultDir;
e15e548b
VZ
119
120 wxString defaultFilenameString;
121 if (defaultFileName)
122 defaultFilenameString = defaultFileName;
1f2f0331
VZ
123
124 wxFileDialog fileDialog(parent, title, defaultDirString,
125 defaultFilenameString, filter2,
126 flags, wxPoint(x, y));
837e5743 127 if( wxStrlen(defaultExtension) != 0 )
1f2f0331 128 {
954f4710 129 int filterFind = 0,
1f2f0331
VZ
130 filterIndex = 0;
131
132 for( unsigned int i = 0; i < filter2.Len(); i++ )
133 {
223d09f6 134 if( filter2.GetChar(i) == wxT('|') )
1f2f0331
VZ
135 {
136 // save the start index of the new filter
137 unsigned int is = i++;
1f2f0331
VZ
138
139 // find the end of the filter
140 for( ; i < filter2.Len(); i++ )
141 {
223d09f6 142 if(filter2[i] == wxT('|'))
1f2f0331
VZ
143 break;
144 }
145
146 if( i-is-1 > 0 && is+1 < filter2.Len() )
147 {
574c0bbf 148 if( filter2.Mid(is+1,i-is-1).Contains(defaultExtension) )
1f2f0331
VZ
149 {
150 filterFind = filterIndex;
151 break;
152 }
153 }
954f4710
VZ
154
155 filterIndex++;
1f2f0331
VZ
156 }
157 }
158
159 fileDialog.SetFilterIndex(filterFind);
160 }
161
3ca6a5f0 162 wxString filename;
e15e548b 163 if ( fileDialog.ShowModal() == wxID_OK )
1f2f0331 164 {
3ca6a5f0 165 filename = fileDialog.GetPath();
1f2f0331 166 }
3ca6a5f0
BP
167
168 return filename;
2bda0e17
KB
169}
170
2bda0e17 171
837e5743
OK
172wxString wxFileSelectorEx(const wxChar *title,
173 const wxChar *defaultDir,
174 const wxChar *defaultFileName,
e15e548b 175 int* defaultFilterIndex,
837e5743 176 const wxChar *filter,
e15e548b
VZ
177 int flags,
178 wxWindow* parent,
179 int x,
180 int y)
2bda0e17
KB
181
182{
3ca6a5f0
BP
183 wxFileDialog fileDialog(parent,
184 title ? title : wxT(""),
185 defaultDir ? defaultDir : wxT(""),
186 defaultFileName ? defaultFileName : wxT(""),
187 filter ? filter : wxT(""),
188 flags, wxPoint(x, y));
2bda0e17 189
3ca6a5f0 190 wxString filename;
e15e548b
VZ
191 if ( fileDialog.ShowModal() == wxID_OK )
192 {
3ca6a5f0
BP
193 if ( defaultFilterIndex )
194 *defaultFilterIndex = fileDialog.GetFilterIndex();
195
196 filename = fileDialog.GetPath();
e15e548b 197 }
3ca6a5f0
BP
198
199 return filename;
2bda0e17
KB
200}
201
2b5f62a0
VZ
202wxFileDialog::wxFileDialog(wxWindow *parent,
203 const wxString& message,
204 const wxString& defaultDir,
205 const wxString& defaultFileName,
206 const wxString& wildCard,
207 long style,
208 const wxPoint& WXUNUSED(pos))
2bda0e17
KB
209{
210 m_message = message;
211 m_dialogStyle = style;
c61f4f6d
VZ
212 if ( ( m_dialogStyle & wxMULTIPLE ) && ( m_dialogStyle & wxSAVE ) )
213 m_dialogStyle &= ~wxMULTIPLE;
2bda0e17 214 m_parent = parent;
223d09f6 215 m_path = wxT("");
e15e548b
VZ
216 m_fileName = defaultFileName;
217 m_dir = defaultDir;
218 m_wildCard = wildCard;
b922ef5a 219 m_filterIndex = 0;
2bda0e17
KB
220}
221
c61f4f6d
VZ
222void wxFileDialog::GetPaths(wxArrayString& paths) const
223{
224 paths.Empty();
225
226 wxString dir(m_dir);
227 if ( m_dir.Last() != _T('\\') )
228 dir += _T('\\');
229
230 size_t count = m_fileNames.GetCount();
231 for ( size_t n = 0; n < count; n++ )
232 {
8ad9ca97
JS
233 if (wxFileName(m_fileNames[n]).IsAbsolute())
234 paths.Add(m_fileNames[n]);
235 else
236 paths.Add(dir + m_fileNames[n]);
c61f4f6d
VZ
237 }
238}
239
2b5f62a0
VZ
240void wxFileDialog::SetPath(const wxString& path)
241{
242 wxString ext;
243 wxSplitPath(path, &m_dir, &m_fileName, &ext);
244 if ( !ext.empty() )
245 m_fileName << _T('.') << ext;
246}
247
c61f4f6d 248int wxFileDialog::ShowModal()
2bda0e17 249{
1f2f0331
VZ
250 HWND hWnd = 0;
251 if (m_parent) hWnd = (HWND) m_parent->GetHWND();
f6bcfd97
BP
252 if (!hWnd && wxTheApp->GetTopWindow())
253 hWnd = (HWND) wxTheApp->GetTopWindow()->GetHWND();
2bda0e17 254
f6bcfd97
BP
255 static wxChar fileNameBuffer [ wxMAXPATH ]; // the file-name
256 wxChar titleBuffer [ wxMAXFILE+1+wxMAXEXT ]; // the file-name, without path
2bda0e17 257
223d09f6
KB
258 *fileNameBuffer = wxT('\0');
259 *titleBuffer = wxT('\0');
2bda0e17 260
2bda0e17 261 long msw_flags = 0;
e15e548b 262 if ( (m_dialogStyle & wxHIDE_READONLY) || (m_dialogStyle & wxSAVE) )
1f2f0331 263 msw_flags |= OFN_HIDEREADONLY;
e15e548b 264 if ( m_dialogStyle & wxFILE_MUST_EXIST )
1f2f0331 265 msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
6e8aa701 266
c61f4f6d 267 if (m_dialogStyle & wxMULTIPLE )
6e8aa701
VZ
268 {
269 // OFN_EXPLORER must always be specified with OFN_ALLOWMULTISELECT
270 msw_flags |= OFN_EXPLORER | OFN_ALLOWMULTISELECT;
271 }
272
99d1b93d
VZ
273 // if wxCHANGE_DIR flag is not given we shouldn't change the CWD which the
274 // standard dialog does by default
6e8aa701
VZ
275 if ( !(m_dialogStyle & wxCHANGE_DIR) )
276 {
277 msw_flags |= OFN_NOCHANGEDIR;
278 }
012a01fc
JS
279/* chris elliott for some reason this does not work usefully if no extension
280 is given, as it test for junk instead of junk.ext
99d1b93d
VZ
281 if ( m_dialogStyle & wxOVERWRITE_PROMPT )
282 {
283 msw_flags |= OFN_OVERWRITEPROMPT;
284 }
012a01fc 285*/
e15e548b 286 OPENFILENAME of;
f6bcfd97
BP
287 wxZeroMemory(of);
288
289 // the OPENFILENAME struct has been extended in newer version of
290 // comcdlg32.dll, but as we don't use the extended fields anyhow, set
291 // the struct size to the old value - otherwise, the programs compiled
292 // with new headers will not work with the old libraries
293#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500)
294 of.lStructSize = sizeof(OPENFILENAME) -
295 (sizeof(void *) + 2*sizeof(DWORD));
296#else // old headers
e15e548b 297 of.lStructSize = sizeof(OPENFILENAME);
f6bcfd97
BP
298#endif
299
e15e548b 300 of.hwndOwner = hWnd;
837e5743 301 of.lpstrTitle = WXSTRINGCAST m_message;
e15e548b 302 of.lpstrFileTitle = titleBuffer;
f6bcfd97 303 of.nMaxFileTitle = wxMAXFILE + 1 + wxMAXEXT; // Windows 3.0 and 3.1
2bda0e17 304
0bc9b25e 305 // Convert forward slashes to backslashes (file selector doesn't like
99d1b93d
VZ
306 // forward slashes) and also squeeze multiple consecutive slashes into one
307 // as it doesn't like two backslashes in a row neither
0627d091
RL
308
309 wxString dir;
310 size_t i, len = m_dir.length();
99d1b93d 311 dir.reserve(len);
0627d091 312 for ( i = 0; i < len; i++ )
99d1b93d
VZ
313 {
314 wxChar ch = m_dir[i];
315 switch ( ch )
316 {
317 case _T('/'):
318 // convert to backslash
319 ch = _T('\\');
320
321 // fall through
0bc9b25e 322
99d1b93d
VZ
323 case _T('\\'):
324 while ( i < len - 1 )
325 {
326 wxChar chNext = m_dir[i + 1];
327 if ( chNext != _T('\\') && chNext != _T('/') )
328 break;
329
04d93c3a
CE
330 // ignore the next one, unless it is at the start of a UNC path
331 if (i > 0)
332 i++;
333 else
334 break;
99d1b93d
VZ
335 }
336 // fall through
337
338 default:
339 // normal char
340 dir += ch;
341 }
342 }
343
344 of.lpstrInitialDir = dir.c_str();
2bda0e17 345
e15e548b 346 of.Flags = msw_flags;
2bda0e17
KB
347
348
2bda0e17
KB
349 //=== Like Alejandro Sierra's wildcard modification >>===================
350 /*
1f2f0331
VZ
351 In wxFileSelector you can put, instead of a single wild_card,
352 pairs of strings separated by '|'.
353 The first string is a description, and the
354 second is the wild card. You can put any number of pairs.
2bda0e17 355
1f2f0331 356 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
2bda0e17 357
1f2f0331
VZ
358 If you put a single wild card, it works as before the modification.
359 */
2bda0e17
KB
360 //=======================================================================
361
4dba84be 362 wxString theFilter;
837e5743 363 if ( wxStrlen(m_wildCard) == 0 )
223d09f6 364 theFilter = wxString(wxT("*.*"));
4dba84be
JS
365 else
366 theFilter = m_wildCard ;
1f2f0331 367 wxString filterBuffer;
2bda0e17 368
223d09f6 369 if ( !wxStrchr( theFilter, wxT('|') ) ) { // only one filter ==> default text
1f2f0331
VZ
370 filterBuffer.Printf(_("Files (%s)|%s"),
371 theFilter.c_str(), theFilter.c_str());
e15e548b 372 }
1f2f0331
VZ
373 else { // more then one filter
374 filterBuffer = theFilter;
2bda0e17 375
574c0bbf
JS
376 }
377
223d09f6 378 filterBuffer += wxT("|");
574c0bbf 379 // Replace | with \0
0bc9b25e 380 for (i = 0; i < filterBuffer.Len(); i++ ) {
223d09f6
KB
381 if ( filterBuffer.GetChar(i) == wxT('|') ) {
382 filterBuffer[i] = wxT('\0');
e15e548b
VZ
383 }
384 }
2bda0e17 385
837e5743 386 of.lpstrFilter = (LPTSTR)(const wxChar *)filterBuffer;
cc42eb7a 387 of.nFilterIndex = m_filterIndex + 1;
2bda0e17
KB
388
389 //=== Setting defaultFileName >>=========================================
390
f6bcfd97
BP
391 wxStrncpy( fileNameBuffer, (const wxChar *)m_fileName, wxMAXPATH-1 );
392 fileNameBuffer[ wxMAXPATH-1 ] = wxT('\0');
2bda0e17 393
e15e548b 394 of.lpstrFile = fileNameBuffer; // holds returned filename
f6bcfd97 395 of.nMaxFile = wxMAXPATH;
2bda0e17
KB
396
397 //== Execute FileDialog >>=================================================
398
3f6638b8
VZ
399 bool success = (m_dialogStyle & wxSAVE ? GetSaveFileName(&of)
400 : GetOpenFileName(&of)) != 0;
2bda0e17 401
f6bcfd97
BP
402 DWORD errCode = CommDlgExtendedError();
403
404#ifdef __WIN32__
405 if (!success && (errCode == CDERR_STRUCTSIZE))
406 {
407 // The struct size has changed so try a smaller or bigger size
408
409 int oldStructSize = of.lStructSize;
410 of.lStructSize = oldStructSize - (sizeof(void *) + 2*sizeof(DWORD));
411 success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
412 : (GetOpenFileName(&of) != 0);
413 errCode = CommDlgExtendedError();
414
415 if (!success && (errCode == CDERR_STRUCTSIZE))
416 {
417 of.lStructSize = oldStructSize + (sizeof(void *) + 2*sizeof(DWORD));
418 success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
419 : (GetOpenFileName(&of) != 0);
420 }
421 }
c6603ac2 422#endif // __WIN32__
f6bcfd97 423
2bda0e17
KB
424 if ( success )
425 {
c61f4f6d
VZ
426 m_fileNames.Empty();
427
428 if ( ( m_dialogStyle & wxMULTIPLE ) &&
429#if defined(OFN_EXPLORER)
430 ( fileNameBuffer[of.nFileOffset-1] == wxT('\0') ) )
431#else
432 ( fileNameBuffer[of.nFileOffset-1] == wxT(' ') ) )
433#endif // OFN_EXPLORER
434 {
435#if defined(OFN_EXPLORER)
436 m_dir = fileNameBuffer;
437 i = of.nFileOffset;
438 m_fileName = &fileNameBuffer[i];
439 m_fileNames.Add(m_fileName);
440 i += m_fileName.Len() + 1;
441
442 while (fileNameBuffer[i] != wxT('\0'))
443 {
444 m_fileNames.Add(&fileNameBuffer[i]);
445 i += wxStrlen(&fileNameBuffer[i]) + 1;
446 }
447#else
c6603ac2 448 wxStringTokenizer toke(fileNameBuffer, _T(" \t\r\n"));
c61f4f6d
VZ
449 m_dir = toke.GetNextToken();
450 m_fileName = toke.GetNextToken();
451 m_fileNames.Add(m_fileName);
452
453 while (toke.HasMoreTokens())
454 m_fileNames.Add(toke.GetNextToken());
455#endif // OFN_EXPLORER
456
457 wxString dir(m_dir);
458 if ( m_dir.Last() != _T('\\') )
459 dir += _T('\\');
460
461 m_fileNames.Sort();
462 m_path = dir + m_fileName;
463 }
464 else
465 {
466 const wxChar* extension = NULL;
1f2f0331 467
c61f4f6d 468 //=== Adding the correct extension >>=================================
2bda0e17 469
cc42eb7a 470 m_filterIndex = (int)of.nFilterIndex - 1;
2bda0e17 471
c6603ac2
VS
472 if ( !of.nFileExtension ||
473 (of.nFileExtension && fileNameBuffer[of.nFileExtension] == wxT('\0')) )
474 {
475 // User has typed a filename without an extension:
2bda0e17 476
a039ccbf
VS
477 // A filename can end in a "." here ("abc."), this means it
478 // does not have an extension. Because later on a "." with
479 // the default extension is appended we remove the "." if
480 // filename ends with one (We don't want files called
481 // "abc..ext")
482 int idx = wxStrlen(fileNameBuffer) - 1;
483 if ( fileNameBuffer[idx] == wxT('.') )
484 {
485 fileNameBuffer[idx] = wxT('\0');
486 }
487
c61f4f6d
VZ
488 int maxFilter = (int)(of.nFilterIndex*2L-1L);
489 extension = filterBuffer;
2bda0e17 490
c61f4f6d
VZ
491 for( int i = 0; i < maxFilter; i++ ) { // get extension
492 extension = extension + wxStrlen( extension ) +1;
493 }
2bda0e17 494
c61f4f6d
VZ
495 extension = wxStrrchr( extension, wxT('.') );
496 if ( extension // != "blabla"
497 && !wxStrrchr( extension, wxT('*') ) // != "blabla.*"
498 && !wxStrrchr( extension, wxT('?') ) // != "blabla.?"
499 && extension[1] // != "blabla."
500 && extension[1] != wxT(' ') ) // != "blabla. "
501 {
502 // now concat extension to the fileName:
503 m_fileName = wxString(fileNameBuffer) + extension;
2bda0e17 504
c61f4f6d 505 int len = wxStrlen( fileNameBuffer );
f6bcfd97
BP
506 wxStrncpy( fileNameBuffer + len, extension, wxMAXPATH - len );
507 fileNameBuffer[ wxMAXPATH -1 ] = wxT('\0');
c61f4f6d 508 }
2bda0e17 509 }
2bda0e17 510
c61f4f6d
VZ
511 m_path = fileNameBuffer;
512 m_fileName = wxFileNameFromPath(fileNameBuffer);
513 m_fileNames.Add(m_fileName);
514 m_dir = wxPathOnly(fileNameBuffer);
515 }
012a01fc
JS
516 //=== Simulating the wxOVERWRITE_PROMPT >>============================
517 //should we also test for file save style ??
518 if ( (m_dialogStyle & wxOVERWRITE_PROMPT) &&
519 ::wxFileExists( fileNameBuffer ) )
520 {
521 wxString messageText;
522 messageText.Printf(_("File '%s' already exists.\nDo you want to replace it?"), fileNameBuffer);
523 if ( wxMessageBox(messageText, wxT("Save File As"), wxYES_NO | wxICON_EXCLAMATION ) != wxYES )
524 {
525 success = FALSE;
526 }
527 }
7cc98b3e
VZ
528 }
529 else
530 {
531 // common dialog failed - why?
532#ifdef __WXDEBUG__
533 DWORD dwErr = CommDlgExtendedError();
534 if ( dwErr != 0 )
535 {
536 // this msg is only for developers
223d09f6 537 wxLogError(wxT("Common dialog failed with error code %0lx."),
7cc98b3e
VZ
538 dwErr);
539 }
540 //else: it was just cancelled
541#endif
542 }
2bda0e17 543
7cc98b3e 544 return success ? wxID_OK : wxID_CANCEL;
2bda0e17
KB
545
546}
547
ba681060
VZ
548// Generic file load/save dialog (for internal use only)
549static
550wxString wxDefaultFileSelector(bool load,
837e5743
OK
551 const wxChar *what,
552 const wxChar *extension,
553 const wxChar *default_name,
ba681060 554 wxWindow *parent)
2bda0e17 555{
b121fa31
VZ
556 wxString prompt;
557 wxString str;
558 if (load)
559 str = _("Load %s file");
560 else
561 str = _("Save %s file");
562 prompt.Printf(str, what);
1f2f0331 563
b121fa31
VZ
564 const wxChar *ext = extension;
565 if (*ext == wxT('.'))
566 ext++;
2bda0e17 567
b121fa31
VZ
568 wxString wild;
569 wild.Printf(wxT("*.%s"), ext);
2bda0e17 570
b121fa31
VZ
571 return wxFileSelector(prompt, NULL, default_name, ext, wild,
572 load ? wxOPEN : wxSAVE, parent);
2bda0e17
KB
573}
574
575// Generic file load dialog
837e5743
OK
576WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
577 const wxChar *extension,
578 const wxChar *default_name,
ba681060 579 wxWindow *parent)
2bda0e17 580{
ba681060 581 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
2bda0e17
KB
582}
583
2bda0e17 584// Generic file save dialog
837e5743
OK
585WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
586 const wxChar *extension,
587 const wxChar *default_name,
ba681060 588 wxWindow *parent)
2bda0e17 589{
ba681060 590 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
2bda0e17
KB
591}
592
1e6feb95 593#endif // wxUSE_FILEDLG
c61f4f6d 594