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