1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "filedlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dialog.h"
37 #include "wx/filedlg.h"
38 #include "wx/filefn.h"
44 #include "wx/msw/private.h"
46 #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
54 #include "wx/tokenzr.h"
57 #define OFN_EXPLORER 0x00080000
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
65 # define wxMAXPATH 65534
67 # define wxMAXPATH 1024
70 # define wxMAXFILE 1024
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 wxString
wxFileSelector(const wxChar
*title
,
89 const wxChar
*defaultDir
,
90 const wxChar
*defaultFileName
,
91 const wxChar
*defaultExtension
,
97 // In the original implementation, defaultExtension is passed to the
98 // lpstrDefExt member of OPENFILENAME. This extension, if non-NULL, is
99 // appended to the filename if the user fails to type an extension. The new
100 // implementation (taken from wxFileSelectorEx) appends the extension
101 // automatically, by looking at the filter specification. In fact this
102 // should be better than the native Microsoft implementation because
103 // Windows only allows *one* default extension, whereas here we do the
104 // right thing depending on the filter the user has chosen.
106 // If there's a default extension specified but no filter, we create a
110 if ( defaultExtension
&& !filter
)
111 filter2
= wxString(wxT("*.")) + defaultExtension
;
115 wxString defaultDirString
;
117 defaultDirString
= defaultDir
;
119 wxString defaultFilenameString
;
121 defaultFilenameString
= defaultFileName
;
123 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
124 defaultFilenameString
, filter2
,
125 flags
, wxPoint(x
, y
));
126 if( wxStrlen(defaultExtension
) != 0 )
131 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
133 if( filter2
.GetChar(i
) == wxT('|') )
135 // save the start index of the new filter
136 unsigned int is
= i
++;
138 // find the end of the filter
139 for( ; i
< filter2
.Len(); i
++ )
141 if(filter2
[i
] == wxT('|'))
145 if( i
-is
-1 > 0 && is
+1 < filter2
.Len() )
147 if( filter2
.Mid(is
+1,i
-is
-1).Contains(defaultExtension
) )
149 filterFind
= filterIndex
;
158 fileDialog
.SetFilterIndex(filterFind
);
162 if ( fileDialog
.ShowModal() == wxID_OK
)
164 filename
= fileDialog
.GetPath();
171 wxString
wxFileSelectorEx(const wxChar
*title
,
172 const wxChar
*defaultDir
,
173 const wxChar
*defaultFileName
,
174 int* defaultFilterIndex
,
175 const wxChar
*filter
,
182 wxFileDialog
fileDialog(parent
,
183 title
? title
: wxT(""),
184 defaultDir
? defaultDir
: wxT(""),
185 defaultFileName
? defaultFileName
: wxT(""),
186 filter
? filter
: wxT(""),
187 flags
, wxPoint(x
, y
));
190 if ( fileDialog
.ShowModal() == wxID_OK
)
192 if ( defaultFilterIndex
)
193 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
195 filename
= fileDialog
.GetPath();
201 wxFileDialog::wxFileDialog(wxWindow
*parent
,
202 const wxString
& message
,
203 const wxString
& defaultDir
,
204 const wxString
& defaultFileName
,
205 const wxString
& wildCard
,
207 const wxPoint
& WXUNUSED(pos
))
210 m_dialogStyle
= style
;
211 if ( ( m_dialogStyle
& wxMULTIPLE
) && ( m_dialogStyle
& wxSAVE
) )
212 m_dialogStyle
&= ~wxMULTIPLE
;
215 m_fileName
= defaultFileName
;
217 m_wildCard
= wildCard
;
221 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
226 if ( m_dir
.Last() != _T('\\') )
229 size_t count
= m_fileNames
.GetCount();
230 for ( size_t n
= 0; n
< count
; n
++ )
232 paths
.Add(dir
+ m_fileNames
[n
]);
236 void wxFileDialog::SetPath(const wxString
& path
)
239 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
241 m_fileName
<< _T('.') << ext
;
244 int wxFileDialog::ShowModal()
247 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
248 if (!hWnd
&& wxTheApp
->GetTopWindow())
249 hWnd
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
251 static wxChar fileNameBuffer
[ wxMAXPATH
]; // the file-name
252 wxChar titleBuffer
[ wxMAXFILE
+1+wxMAXEXT
]; // the file-name, without path
254 *fileNameBuffer
= wxT('\0');
255 *titleBuffer
= wxT('\0');
258 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
259 msw_flags
|= OFN_HIDEREADONLY
;
260 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
261 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
263 if (m_dialogStyle
& wxMULTIPLE
)
265 // OFN_EXPLORER must always be specified with OFN_ALLOWMULTISELECT
266 msw_flags
|= OFN_EXPLORER
| OFN_ALLOWMULTISELECT
;
269 // if wxCHANGE_DIR flag is not given we shouldn't change the CWD which the
270 // standard dialog does by default
271 if ( !(m_dialogStyle
& wxCHANGE_DIR
) )
273 msw_flags
|= OFN_NOCHANGEDIR
;
275 /* chris elliott for some reason this does not work usefully if no extension
276 is given, as it test for junk instead of junk.ext
277 if ( m_dialogStyle & wxOVERWRITE_PROMPT )
279 msw_flags |= OFN_OVERWRITEPROMPT;
285 // the OPENFILENAME struct has been extended in newer version of
286 // comcdlg32.dll, but as we don't use the extended fields anyhow, set
287 // the struct size to the old value - otherwise, the programs compiled
288 // with new headers will not work with the old libraries
289 #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500)
290 of
.lStructSize
= sizeof(OPENFILENAME
) -
291 (sizeof(void *) + 2*sizeof(DWORD
));
293 of
.lStructSize
= sizeof(OPENFILENAME
);
297 of
.lpstrTitle
= WXSTRINGCAST m_message
;
298 of
.lpstrFileTitle
= titleBuffer
;
299 of
.nMaxFileTitle
= wxMAXFILE
+ 1 + wxMAXEXT
; // Windows 3.0 and 3.1
301 // Convert forward slashes to backslashes (file selector doesn't like
302 // forward slashes) and also squeeze multiple consecutive slashes into one
303 // as it doesn't like two backslashes in a row neither
306 size_t i
, len
= m_dir
.length();
308 for ( i
= 0; i
< len
; i
++ )
310 wxChar ch
= m_dir
[i
];
314 // convert to backslash
320 while ( i
< len
- 1 )
322 wxChar chNext
= m_dir
[i
+ 1];
323 if ( chNext
!= _T('\\') && chNext
!= _T('/') )
326 // ignore the next one, unless it is at the start of a UNC path
340 of
.lpstrInitialDir
= dir
.c_str();
342 of
.Flags
= msw_flags
;
345 //=== Like Alejandro Sierra's wildcard modification >>===================
347 In wxFileSelector you can put, instead of a single wild_card,
348 pairs of strings separated by '|'.
349 The first string is a description, and the
350 second is the wild card. You can put any number of pairs.
352 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
354 If you put a single wild card, it works as before the modification.
356 //=======================================================================
359 if ( wxStrlen(m_wildCard
) == 0 )
360 theFilter
= wxString(wxT("*.*"));
362 theFilter
= m_wildCard
;
363 wxString filterBuffer
;
365 if ( !wxStrchr( theFilter
, wxT('|') ) ) { // only one filter ==> default text
366 filterBuffer
.Printf(_("Files (%s)|%s"),
367 theFilter
.c_str(), theFilter
.c_str());
369 else { // more then one filter
370 filterBuffer
= theFilter
;
374 filterBuffer
+= wxT("|");
376 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
377 if ( filterBuffer
.GetChar(i
) == wxT('|') ) {
378 filterBuffer
[i
] = wxT('\0');
382 of
.lpstrFilter
= (LPTSTR
)(const wxChar
*)filterBuffer
;
383 of
.nFilterIndex
= m_filterIndex
+ 1;
385 //=== Setting defaultFileName >>=========================================
387 wxStrncpy( fileNameBuffer
, (const wxChar
*)m_fileName
, wxMAXPATH
-1 );
388 fileNameBuffer
[ wxMAXPATH
-1 ] = wxT('\0');
390 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
391 of
.nMaxFile
= wxMAXPATH
;
393 //== Execute FileDialog >>=================================================
395 bool success
= (m_dialogStyle
& wxSAVE
? GetSaveFileName(&of
)
396 : GetOpenFileName(&of
)) != 0;
398 DWORD errCode
= CommDlgExtendedError();
401 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
403 // The struct size has changed so try a smaller or bigger size
405 int oldStructSize
= of
.lStructSize
;
406 of
.lStructSize
= oldStructSize
- (sizeof(void *) + 2*sizeof(DWORD
));
407 success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
408 : (GetOpenFileName(&of
) != 0);
409 errCode
= CommDlgExtendedError();
411 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
413 of
.lStructSize
= oldStructSize
+ (sizeof(void *) + 2*sizeof(DWORD
));
414 success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
415 : (GetOpenFileName(&of
) != 0);
424 if ( ( m_dialogStyle
& wxMULTIPLE
) &&
425 #if defined(OFN_EXPLORER)
426 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT('\0') ) )
428 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT(' ') ) )
429 #endif // OFN_EXPLORER
431 #if defined(OFN_EXPLORER)
432 m_dir
= fileNameBuffer
;
434 m_fileName
= &fileNameBuffer
[i
];
435 m_fileNames
.Add(m_fileName
);
436 i
+= m_fileName
.Len() + 1;
438 while (fileNameBuffer
[i
] != wxT('\0'))
440 m_fileNames
.Add(&fileNameBuffer
[i
]);
441 i
+= wxStrlen(&fileNameBuffer
[i
]) + 1;
444 wxStringTokenizer
toke(fileNameBuffer
, _T(" \t\r\n"));
445 m_dir
= toke
.GetNextToken();
446 m_fileName
= toke
.GetNextToken();
447 m_fileNames
.Add(m_fileName
);
449 while (toke
.HasMoreTokens())
450 m_fileNames
.Add(toke
.GetNextToken());
451 #endif // OFN_EXPLORER
454 if ( m_dir
.Last() != _T('\\') )
458 m_path
= dir
+ m_fileName
;
462 const wxChar
* extension
= NULL
;
464 //=== Adding the correct extension >>=================================
466 m_filterIndex
= (int)of
.nFilterIndex
- 1;
468 if ( !of
.nFileExtension
||
469 (of
.nFileExtension
&& fileNameBuffer
[of
.nFileExtension
] == wxT('\0')) )
471 // User has typed a filename without an extension:
473 // A filename can end in a "." here ("abc."), this means it
474 // does not have an extension. Because later on a "." with
475 // the default extension is appended we remove the "." if
476 // filename ends with one (We don't want files called
478 int idx
= wxStrlen(fileNameBuffer
) - 1;
479 if ( fileNameBuffer
[idx
] == wxT('.') )
481 fileNameBuffer
[idx
] = wxT('\0');
484 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
485 extension
= filterBuffer
;
487 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
488 extension
= extension
+ wxStrlen( extension
) +1;
491 extension
= wxStrrchr( extension
, wxT('.') );
492 if ( extension
// != "blabla"
493 && !wxStrrchr( extension
, wxT('*') ) // != "blabla.*"
494 && !wxStrrchr( extension
, wxT('?') ) // != "blabla.?"
495 && extension
[1] // != "blabla."
496 && extension
[1] != wxT(' ') ) // != "blabla. "
498 // now concat extension to the fileName:
499 m_fileName
= wxString(fileNameBuffer
) + extension
;
501 int len
= wxStrlen( fileNameBuffer
);
502 wxStrncpy( fileNameBuffer
+ len
, extension
, wxMAXPATH
- len
);
503 fileNameBuffer
[ wxMAXPATH
-1 ] = wxT('\0');
507 m_path
= fileNameBuffer
;
508 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
509 m_fileNames
.Add(m_fileName
);
510 m_dir
= wxPathOnly(fileNameBuffer
);
512 //=== Simulating the wxOVERWRITE_PROMPT >>============================
513 //should we also test for file save style ??
514 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
515 ::wxFileExists( fileNameBuffer
) )
517 wxString messageText
;
518 messageText
.Printf(_("File '%s' already exists.\nDo you want to replace it?"), fileNameBuffer
);
519 if ( wxMessageBox(messageText
, wxT("Save File As"), wxYES_NO
| wxICON_EXCLAMATION
) != wxYES
)
527 // common dialog failed - why?
529 DWORD dwErr
= CommDlgExtendedError();
532 // this msg is only for developers
533 wxLogError(wxT("Common dialog failed with error code %0lx."),
536 //else: it was just cancelled
540 return success
? wxID_OK
: wxID_CANCEL
;
544 // Generic file load/save dialog (for internal use only)
546 wxString
wxDefaultFileSelector(bool load
,
548 const wxChar
*extension
,
549 const wxChar
*default_name
,
555 str
= _("Load %s file");
557 str
= _("Save %s file");
558 prompt
.Printf(str
, what
);
560 const wxChar
*ext
= extension
;
561 if (*ext
== wxT('.'))
565 wild
.Printf(wxT("*.%s"), ext
);
567 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
568 load
? wxOPEN
: wxSAVE
, parent
);
571 // Generic file load dialog
572 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
573 const wxChar
*extension
,
574 const wxChar
*default_name
,
577 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
580 // Generic file save dialog
581 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
582 const wxChar
*extension
,
583 const wxChar
*default_name
,
586 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);
589 #endif // wxUSE_FILEDLG