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