]> git.saurik.com Git - wxWidgets.git/blame - src/msw/filedlg.cpp
fixed wxTempFile::Commit() broken by last check in
[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
4676948b 46#if !defined(__WIN32__) || defined(__WXWINCE__)
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 85// ----------------------------------------------------------------------------
b600ed13 86// wxFileDialog
f6bcfd97
BP
87// ----------------------------------------------------------------------------
88
2b5f62a0
VZ
89wxFileDialog::wxFileDialog(wxWindow *parent,
90 const wxString& message,
91 const wxString& defaultDir,
92 const wxString& defaultFileName,
93 const wxString& wildCard,
94 long style,
95 const wxPoint& WXUNUSED(pos))
2bda0e17
KB
96{
97 m_message = message;
98 m_dialogStyle = style;
c61f4f6d
VZ
99 if ( ( m_dialogStyle & wxMULTIPLE ) && ( m_dialogStyle & wxSAVE ) )
100 m_dialogStyle &= ~wxMULTIPLE;
2bda0e17 101 m_parent = parent;
fda7962d 102 m_path = wxEmptyString;
e15e548b
VZ
103 m_fileName = defaultFileName;
104 m_dir = defaultDir;
105 m_wildCard = wildCard;
b922ef5a 106 m_filterIndex = 0;
2bda0e17
KB
107}
108
c61f4f6d
VZ
109void wxFileDialog::GetPaths(wxArrayString& paths) const
110{
111 paths.Empty();
112
113 wxString dir(m_dir);
114 if ( m_dir.Last() != _T('\\') )
115 dir += _T('\\');
116
117 size_t count = m_fileNames.GetCount();
118 for ( size_t n = 0; n < count; n++ )
119 {
8ad9ca97
JS
120 if (wxFileName(m_fileNames[n]).IsAbsolute())
121 paths.Add(m_fileNames[n]);
122 else
123 paths.Add(dir + m_fileNames[n]);
c61f4f6d
VZ
124 }
125}
126
2b5f62a0
VZ
127void wxFileDialog::SetPath(const wxString& path)
128{
129 wxString ext;
130 wxSplitPath(path, &m_dir, &m_fileName, &ext);
131 if ( !ext.empty() )
132 m_fileName << _T('.') << ext;
133}
134
c61f4f6d 135int wxFileDialog::ShowModal()
2bda0e17 136{
1f2f0331
VZ
137 HWND hWnd = 0;
138 if (m_parent) hWnd = (HWND) m_parent->GetHWND();
f6bcfd97
BP
139 if (!hWnd && wxTheApp->GetTopWindow())
140 hWnd = (HWND) wxTheApp->GetTopWindow()->GetHWND();
2bda0e17 141
f6bcfd97
BP
142 static wxChar fileNameBuffer [ wxMAXPATH ]; // the file-name
143 wxChar titleBuffer [ wxMAXFILE+1+wxMAXEXT ]; // the file-name, without path
2bda0e17 144
223d09f6
KB
145 *fileNameBuffer = wxT('\0');
146 *titleBuffer = wxT('\0');
2bda0e17 147
2bda0e17 148 long msw_flags = 0;
e15e548b 149 if ( (m_dialogStyle & wxHIDE_READONLY) || (m_dialogStyle & wxSAVE) )
1f2f0331 150 msw_flags |= OFN_HIDEREADONLY;
e15e548b 151 if ( m_dialogStyle & wxFILE_MUST_EXIST )
1f2f0331 152 msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
6e8aa701 153
c61f4f6d 154 if (m_dialogStyle & wxMULTIPLE )
6e8aa701
VZ
155 {
156 // OFN_EXPLORER must always be specified with OFN_ALLOWMULTISELECT
157 msw_flags |= OFN_EXPLORER | OFN_ALLOWMULTISELECT;
158 }
159
99d1b93d
VZ
160 // if wxCHANGE_DIR flag is not given we shouldn't change the CWD which the
161 // standard dialog does by default
6e8aa701
VZ
162 if ( !(m_dialogStyle & wxCHANGE_DIR) )
163 {
164 msw_flags |= OFN_NOCHANGEDIR;
165 }
012a01fc
JS
166/* chris elliott for some reason this does not work usefully if no extension
167 is given, as it test for junk instead of junk.ext
99d1b93d
VZ
168 if ( m_dialogStyle & wxOVERWRITE_PROMPT )
169 {
170 msw_flags |= OFN_OVERWRITEPROMPT;
171 }
012a01fc 172*/
e15e548b 173 OPENFILENAME of;
f6bcfd97
BP
174 wxZeroMemory(of);
175
176 // the OPENFILENAME struct has been extended in newer version of
177 // comcdlg32.dll, but as we don't use the extended fields anyhow, set
178 // the struct size to the old value - otherwise, the programs compiled
179 // with new headers will not work with the old libraries
180#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500)
181 of.lStructSize = sizeof(OPENFILENAME) -
182 (sizeof(void *) + 2*sizeof(DWORD));
183#else // old headers
e15e548b 184 of.lStructSize = sizeof(OPENFILENAME);
f6bcfd97
BP
185#endif
186
e15e548b 187 of.hwndOwner = hWnd;
837e5743 188 of.lpstrTitle = WXSTRINGCAST m_message;
e15e548b 189 of.lpstrFileTitle = titleBuffer;
f6bcfd97 190 of.nMaxFileTitle = wxMAXFILE + 1 + wxMAXEXT; // Windows 3.0 and 3.1
2bda0e17 191
0bc9b25e 192 // Convert forward slashes to backslashes (file selector doesn't like
99d1b93d
VZ
193 // forward slashes) and also squeeze multiple consecutive slashes into one
194 // as it doesn't like two backslashes in a row neither
0627d091
RL
195
196 wxString dir;
197 size_t i, len = m_dir.length();
99d1b93d 198 dir.reserve(len);
0627d091 199 for ( i = 0; i < len; i++ )
99d1b93d
VZ
200 {
201 wxChar ch = m_dir[i];
202 switch ( ch )
203 {
204 case _T('/'):
205 // convert to backslash
206 ch = _T('\\');
207
208 // fall through
0bc9b25e 209
99d1b93d
VZ
210 case _T('\\'):
211 while ( i < len - 1 )
212 {
213 wxChar chNext = m_dir[i + 1];
214 if ( chNext != _T('\\') && chNext != _T('/') )
215 break;
216
04d93c3a
CE
217 // ignore the next one, unless it is at the start of a UNC path
218 if (i > 0)
219 i++;
220 else
221 break;
99d1b93d
VZ
222 }
223 // fall through
224
225 default:
226 // normal char
227 dir += ch;
228 }
229 }
230
231 of.lpstrInitialDir = dir.c_str();
2bda0e17 232
e15e548b 233 of.Flags = msw_flags;
2bda0e17
KB
234
235
2bda0e17
KB
236 //=== Like Alejandro Sierra's wildcard modification >>===================
237 /*
1f2f0331
VZ
238 In wxFileSelector you can put, instead of a single wild_card,
239 pairs of strings separated by '|'.
240 The first string is a description, and the
241 second is the wild card. You can put any number of pairs.
2bda0e17 242
1f2f0331 243 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
2bda0e17 244
1f2f0331
VZ
245 If you put a single wild card, it works as before the modification.
246 */
2bda0e17
KB
247 //=======================================================================
248
4dba84be 249 wxString theFilter;
837e5743 250 if ( wxStrlen(m_wildCard) == 0 )
223d09f6 251 theFilter = wxString(wxT("*.*"));
4dba84be
JS
252 else
253 theFilter = m_wildCard ;
1f2f0331 254 wxString filterBuffer;
2bda0e17 255
223d09f6 256 if ( !wxStrchr( theFilter, wxT('|') ) ) { // only one filter ==> default text
1f2f0331
VZ
257 filterBuffer.Printf(_("Files (%s)|%s"),
258 theFilter.c_str(), theFilter.c_str());
e15e548b 259 }
1f2f0331
VZ
260 else { // more then one filter
261 filterBuffer = theFilter;
2bda0e17 262
574c0bbf
JS
263 }
264
223d09f6 265 filterBuffer += wxT("|");
574c0bbf 266 // Replace | with \0
0bc9b25e 267 for (i = 0; i < filterBuffer.Len(); i++ ) {
223d09f6
KB
268 if ( filterBuffer.GetChar(i) == wxT('|') ) {
269 filterBuffer[i] = wxT('\0');
e15e548b
VZ
270 }
271 }
2bda0e17 272
837e5743 273 of.lpstrFilter = (LPTSTR)(const wxChar *)filterBuffer;
cc42eb7a 274 of.nFilterIndex = m_filterIndex + 1;
2bda0e17
KB
275
276 //=== Setting defaultFileName >>=========================================
277
f6bcfd97
BP
278 wxStrncpy( fileNameBuffer, (const wxChar *)m_fileName, wxMAXPATH-1 );
279 fileNameBuffer[ wxMAXPATH-1 ] = wxT('\0');
2bda0e17 280
e15e548b 281 of.lpstrFile = fileNameBuffer; // holds returned filename
f6bcfd97 282 of.nMaxFile = wxMAXPATH;
2bda0e17
KB
283
284 //== Execute FileDialog >>=================================================
285
3f6638b8
VZ
286 bool success = (m_dialogStyle & wxSAVE ? GetSaveFileName(&of)
287 : GetOpenFileName(&of)) != 0;
2bda0e17 288
f6bcfd97
BP
289 DWORD errCode = CommDlgExtendedError();
290
291#ifdef __WIN32__
292 if (!success && (errCode == CDERR_STRUCTSIZE))
293 {
294 // The struct size has changed so try a smaller or bigger size
295
296 int oldStructSize = of.lStructSize;
297 of.lStructSize = oldStructSize - (sizeof(void *) + 2*sizeof(DWORD));
298 success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
299 : (GetOpenFileName(&of) != 0);
300 errCode = CommDlgExtendedError();
301
302 if (!success && (errCode == CDERR_STRUCTSIZE))
303 {
304 of.lStructSize = oldStructSize + (sizeof(void *) + 2*sizeof(DWORD));
305 success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
306 : (GetOpenFileName(&of) != 0);
307 }
308 }
c6603ac2 309#endif // __WIN32__
f6bcfd97 310
2bda0e17
KB
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
c6603ac2 335 wxStringTokenizer toke(fileNameBuffer, _T(" \t\r\n"));
c61f4f6d
VZ
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
222ed1d6 348 m_fileNames.Sort(wxStringSortAscending);
c61f4f6d
VZ
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
cc42eb7a 357 m_filterIndex = (int)of.nFilterIndex - 1;
2bda0e17 358
c6603ac2
VS
359 if ( !of.nFileExtension ||
360 (of.nFileExtension && fileNameBuffer[of.nFileExtension] == wxT('\0')) )
361 {
362 // User has typed a filename without an extension:
2bda0e17 363
a039ccbf
VS
364 // A filename can end in a "." here ("abc."), this means it
365 // does not have an extension. Because later on a "." with
366 // the default extension is appended we remove the "." if
367 // filename ends with one (We don't want files called
368 // "abc..ext")
369 int idx = wxStrlen(fileNameBuffer) - 1;
370 if ( fileNameBuffer[idx] == wxT('.') )
371 {
372 fileNameBuffer[idx] = wxT('\0');
373 }
374
c61f4f6d
VZ
375 int maxFilter = (int)(of.nFilterIndex*2L-1L);
376 extension = filterBuffer;
2bda0e17 377
c61f4f6d
VZ
378 for( int i = 0; i < maxFilter; i++ ) { // get extension
379 extension = extension + wxStrlen( extension ) +1;
380 }
2bda0e17 381
c61f4f6d
VZ
382 extension = wxStrrchr( extension, wxT('.') );
383 if ( extension // != "blabla"
384 && !wxStrrchr( extension, wxT('*') ) // != "blabla.*"
385 && !wxStrrchr( extension, wxT('?') ) // != "blabla.?"
386 && extension[1] // != "blabla."
387 && extension[1] != wxT(' ') ) // != "blabla. "
388 {
389 // now concat extension to the fileName:
390 m_fileName = wxString(fileNameBuffer) + extension;
2bda0e17 391
c61f4f6d 392 int len = wxStrlen( fileNameBuffer );
f6bcfd97
BP
393 wxStrncpy( fileNameBuffer + len, extension, wxMAXPATH - len );
394 fileNameBuffer[ wxMAXPATH -1 ] = wxT('\0');
c61f4f6d 395 }
2bda0e17 396 }
2bda0e17 397
c61f4f6d
VZ
398 m_path = fileNameBuffer;
399 m_fileName = wxFileNameFromPath(fileNameBuffer);
400 m_fileNames.Add(m_fileName);
401 m_dir = wxPathOnly(fileNameBuffer);
402 }
012a01fc
JS
403 //=== Simulating the wxOVERWRITE_PROMPT >>============================
404 //should we also test for file save style ??
405 if ( (m_dialogStyle & wxOVERWRITE_PROMPT) &&
406 ::wxFileExists( fileNameBuffer ) )
407 {
408 wxString messageText;
409 messageText.Printf(_("File '%s' already exists.\nDo you want to replace it?"), fileNameBuffer);
410 if ( wxMessageBox(messageText, wxT("Save File As"), wxYES_NO | wxICON_EXCLAMATION ) != wxYES )
411 {
412 success = FALSE;
413 }
414 }
7cc98b3e
VZ
415 }
416 else
417 {
418 // common dialog failed - why?
419#ifdef __WXDEBUG__
420 DWORD dwErr = CommDlgExtendedError();
421 if ( dwErr != 0 )
422 {
423 // this msg is only for developers
223d09f6 424 wxLogError(wxT("Common dialog failed with error code %0lx."),
7cc98b3e
VZ
425 dwErr);
426 }
427 //else: it was just cancelled
428#endif
429 }
2bda0e17 430
7cc98b3e 431 return success ? wxID_OK : wxID_CANCEL;
2bda0e17
KB
432
433}
434
1e6feb95 435#endif // wxUSE_FILEDLG
c61f4f6d 436