]> git.saurik.com Git - wxWidgets.git/blame - src/msw/filedlg.cpp
dllwidget demo updates
[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
256 // if wxCHANGE_DIR flag is not given we shouldn't change the CWD
257 if ( !(m_dialogStyle & wxCHANGE_DIR) )
258 {
259 msw_flags |= OFN_NOCHANGEDIR;
260 }
2bda0e17 261
e15e548b 262 OPENFILENAME of;
f6bcfd97
BP
263 wxZeroMemory(of);
264
265 // the OPENFILENAME struct has been extended in newer version of
266 // comcdlg32.dll, but as we don't use the extended fields anyhow, set
267 // the struct size to the old value - otherwise, the programs compiled
268 // with new headers will not work with the old libraries
269#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500)
270 of.lStructSize = sizeof(OPENFILENAME) -
271 (sizeof(void *) + 2*sizeof(DWORD));
272#else // old headers
e15e548b 273 of.lStructSize = sizeof(OPENFILENAME);
f6bcfd97
BP
274#endif
275
e15e548b 276 of.hwndOwner = hWnd;
837e5743 277 of.lpstrTitle = WXSTRINGCAST m_message;
e15e548b 278 of.lpstrFileTitle = titleBuffer;
f6bcfd97 279 of.nMaxFileTitle = wxMAXFILE + 1 + wxMAXEXT; // Windows 3.0 and 3.1
2bda0e17 280
0bc9b25e
JS
281 // Convert forward slashes to backslashes (file selector doesn't like
282 // forward slashes)
283 size_t i = 0;
284 size_t len = m_dir.Length();
285 for (i = 0; i < len; i++)
223d09f6
KB
286 if (m_dir[i] == wxT('/'))
287 m_dir[i] = wxT('\\');
0bc9b25e 288
837e5743 289 of.lpstrInitialDir = m_dir.c_str();
2bda0e17 290
e15e548b 291 of.Flags = msw_flags;
2bda0e17
KB
292
293
2bda0e17
KB
294 //=== Like Alejandro Sierra's wildcard modification >>===================
295 /*
1f2f0331
VZ
296 In wxFileSelector you can put, instead of a single wild_card,
297 pairs of strings separated by '|'.
298 The first string is a description, and the
299 second is the wild card. You can put any number of pairs.
2bda0e17 300
1f2f0331 301 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
2bda0e17 302
1f2f0331
VZ
303 If you put a single wild card, it works as before the modification.
304 */
2bda0e17
KB
305 //=======================================================================
306
4dba84be 307 wxString theFilter;
837e5743 308 if ( wxStrlen(m_wildCard) == 0 )
223d09f6 309 theFilter = wxString(wxT("*.*"));
4dba84be
JS
310 else
311 theFilter = m_wildCard ;
1f2f0331 312 wxString filterBuffer;
2bda0e17 313
223d09f6 314 if ( !wxStrchr( theFilter, wxT('|') ) ) { // only one filter ==> default text
1f2f0331
VZ
315 filterBuffer.Printf(_("Files (%s)|%s"),
316 theFilter.c_str(), theFilter.c_str());
e15e548b 317 }
1f2f0331
VZ
318 else { // more then one filter
319 filterBuffer = theFilter;
2bda0e17 320
574c0bbf
JS
321 }
322
223d09f6 323 filterBuffer += wxT("|");
574c0bbf 324 // Replace | with \0
0bc9b25e 325 for (i = 0; i < filterBuffer.Len(); i++ ) {
223d09f6
KB
326 if ( filterBuffer.GetChar(i) == wxT('|') ) {
327 filterBuffer[i] = wxT('\0');
e15e548b
VZ
328 }
329 }
2bda0e17 330
837e5743 331 of.lpstrFilter = (LPTSTR)(const wxChar *)filterBuffer;
cc42eb7a 332 of.nFilterIndex = m_filterIndex + 1;
2bda0e17
KB
333
334 //=== Setting defaultFileName >>=========================================
335
f6bcfd97
BP
336 wxStrncpy( fileNameBuffer, (const wxChar *)m_fileName, wxMAXPATH-1 );
337 fileNameBuffer[ wxMAXPATH-1 ] = wxT('\0');
2bda0e17 338
e15e548b 339 of.lpstrFile = fileNameBuffer; // holds returned filename
f6bcfd97 340 of.nMaxFile = wxMAXPATH;
2bda0e17
KB
341
342 //== Execute FileDialog >>=================================================
343
3f6638b8
VZ
344 bool success = (m_dialogStyle & wxSAVE ? GetSaveFileName(&of)
345 : GetOpenFileName(&of)) != 0;
2bda0e17 346
f6bcfd97
BP
347 DWORD errCode = CommDlgExtendedError();
348
349#ifdef __WIN32__
350 if (!success && (errCode == CDERR_STRUCTSIZE))
351 {
352 // The struct size has changed so try a smaller or bigger size
353
354 int oldStructSize = of.lStructSize;
355 of.lStructSize = oldStructSize - (sizeof(void *) + 2*sizeof(DWORD));
356 success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
357 : (GetOpenFileName(&of) != 0);
358 errCode = CommDlgExtendedError();
359
360 if (!success && (errCode == CDERR_STRUCTSIZE))
361 {
362 of.lStructSize = oldStructSize + (sizeof(void *) + 2*sizeof(DWORD));
363 success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
364 : (GetOpenFileName(&of) != 0);
365 }
366 }
367#endif
368
2bda0e17
KB
369 if ( success )
370 {
c61f4f6d
VZ
371 m_fileNames.Empty();
372
373 if ( ( m_dialogStyle & wxMULTIPLE ) &&
374#if defined(OFN_EXPLORER)
375 ( fileNameBuffer[of.nFileOffset-1] == wxT('\0') ) )
376#else
377 ( fileNameBuffer[of.nFileOffset-1] == wxT(' ') ) )
378#endif // OFN_EXPLORER
379 {
380#if defined(OFN_EXPLORER)
381 m_dir = fileNameBuffer;
382 i = of.nFileOffset;
383 m_fileName = &fileNameBuffer[i];
384 m_fileNames.Add(m_fileName);
385 i += m_fileName.Len() + 1;
386
387 while (fileNameBuffer[i] != wxT('\0'))
388 {
389 m_fileNames.Add(&fileNameBuffer[i]);
390 i += wxStrlen(&fileNameBuffer[i]) + 1;
391 }
392#else
393 wxStringTokenizer toke(fileNameBuffer, " \t\r\n");
394 m_dir = toke.GetNextToken();
395 m_fileName = toke.GetNextToken();
396 m_fileNames.Add(m_fileName);
397
398 while (toke.HasMoreTokens())
399 m_fileNames.Add(toke.GetNextToken());
400#endif // OFN_EXPLORER
401
402 wxString dir(m_dir);
403 if ( m_dir.Last() != _T('\\') )
404 dir += _T('\\');
405
406 m_fileNames.Sort();
407 m_path = dir + m_fileName;
408 }
409 else
410 {
411 const wxChar* extension = NULL;
1f2f0331 412
c61f4f6d 413 //=== Adding the correct extension >>=================================
2bda0e17 414
cc42eb7a 415 m_filterIndex = (int)of.nFilterIndex - 1;
2bda0e17 416
60fe7303 417 if ( !of.nFileExtension || (of.nFileExtension && fileNameBuffer[ of.nFileExtension-1] != wxT('.')) )
c61f4f6d
VZ
418 { // user has typed an filename
419 // without an extension:
2bda0e17 420
c61f4f6d
VZ
421 int maxFilter = (int)(of.nFilterIndex*2L-1L);
422 extension = filterBuffer;
2bda0e17 423
c61f4f6d
VZ
424 for( int i = 0; i < maxFilter; i++ ) { // get extension
425 extension = extension + wxStrlen( extension ) +1;
426 }
2bda0e17 427
c61f4f6d
VZ
428 extension = wxStrrchr( extension, wxT('.') );
429 if ( extension // != "blabla"
430 && !wxStrrchr( extension, wxT('*') ) // != "blabla.*"
431 && !wxStrrchr( extension, wxT('?') ) // != "blabla.?"
432 && extension[1] // != "blabla."
433 && extension[1] != wxT(' ') ) // != "blabla. "
434 {
435 // now concat extension to the fileName:
436 m_fileName = wxString(fileNameBuffer) + extension;
2bda0e17 437
c61f4f6d 438 int len = wxStrlen( fileNameBuffer );
f6bcfd97
BP
439 wxStrncpy( fileNameBuffer + len, extension, wxMAXPATH - len );
440 fileNameBuffer[ wxMAXPATH -1 ] = wxT('\0');
c61f4f6d 441 }
2bda0e17 442 }
2bda0e17 443
c61f4f6d
VZ
444 m_path = fileNameBuffer;
445 m_fileName = wxFileNameFromPath(fileNameBuffer);
446 m_fileNames.Add(m_fileName);
447 m_dir = wxPathOnly(fileNameBuffer);
448 }
2bda0e17
KB
449
450
e15e548b 451 //=== Simulating the wxOVERWRITE_PROMPT >>============================
2bda0e17 452
1f2f0331
VZ
453 if ( (m_dialogStyle & wxOVERWRITE_PROMPT) &&
454 ::wxFileExists( fileNameBuffer ) )
2bda0e17 455 {
1f2f0331
VZ
456 wxString messageText;
457 messageText.Printf(_("Replace file '%s'?"), fileNameBuffer);
2bda0e17 458
1f2f0331 459 if ( wxMessageBox(messageText, m_message, wxYES_NO ) != wxYES )
2bda0e17
KB
460 {
461 success = FALSE;
e15e548b 462 }
e15e548b 463 }
2bda0e17 464
7cc98b3e
VZ
465 }
466 else
467 {
468 // common dialog failed - why?
469#ifdef __WXDEBUG__
470 DWORD dwErr = CommDlgExtendedError();
471 if ( dwErr != 0 )
472 {
473 // this msg is only for developers
223d09f6 474 wxLogError(wxT("Common dialog failed with error code %0lx."),
7cc98b3e
VZ
475 dwErr);
476 }
477 //else: it was just cancelled
478#endif
479 }
2bda0e17 480
7cc98b3e 481 return success ? wxID_OK : wxID_CANCEL;
2bda0e17
KB
482
483}
484
ba681060
VZ
485// Generic file load/save dialog (for internal use only)
486static
487wxString wxDefaultFileSelector(bool load,
837e5743
OK
488 const wxChar *what,
489 const wxChar *extension,
490 const wxChar *default_name,
ba681060 491 wxWindow *parent)
2bda0e17 492{
b121fa31
VZ
493 wxString prompt;
494 wxString str;
495 if (load)
496 str = _("Load %s file");
497 else
498 str = _("Save %s file");
499 prompt.Printf(str, what);
1f2f0331 500
b121fa31
VZ
501 const wxChar *ext = extension;
502 if (*ext == wxT('.'))
503 ext++;
2bda0e17 504
b121fa31
VZ
505 wxString wild;
506 wild.Printf(wxT("*.%s"), ext);
2bda0e17 507
b121fa31
VZ
508 return wxFileSelector(prompt, NULL, default_name, ext, wild,
509 load ? wxOPEN : wxSAVE, parent);
2bda0e17
KB
510}
511
512// Generic file load dialog
837e5743
OK
513WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
514 const wxChar *extension,
515 const wxChar *default_name,
ba681060 516 wxWindow *parent)
2bda0e17 517{
ba681060 518 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
2bda0e17
KB
519}
520
2bda0e17 521// Generic file save dialog
837e5743
OK
522WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
523 const wxChar *extension,
524 const wxChar *default_name,
ba681060 525 wxWindow *parent)
2bda0e17 526{
ba681060 527 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
2bda0e17
KB
528}
529
1e6feb95 530#endif // wxUSE_FILEDLG
c61f4f6d 531