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