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