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"
43 #include "wx/msw/private.h"
45 #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
53 #include "wx/tokenzr.h"
56 #define OFN_EXPLORER 0x00080000
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
64 # define wxMAXPATH 4096
66 # define wxMAXPATH 1024
69 # define wxMAXFILE 1024
73 // ============================================================================
75 // ============================================================================
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 wxString
wxFileSelector(const wxChar
*title
,
88 const wxChar
*defaultDir
,
89 const wxChar
*defaultFileName
,
90 const wxChar
*defaultExtension
,
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.
105 // If there's a default extension specified but no filter, we create a
109 if ( defaultExtension
&& !filter
)
110 filter2
= wxString(wxT("*.")) + defaultExtension
;
114 wxString defaultDirString
;
116 defaultDirString
= defaultDir
;
118 wxString defaultFilenameString
;
120 defaultFilenameString
= defaultFileName
;
122 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
123 defaultFilenameString
, filter2
,
124 flags
, wxPoint(x
, y
));
125 if( wxStrlen(defaultExtension
) != 0 )
130 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
132 if( filter2
.GetChar(i
) == wxT('|') )
134 // save the start index of the new filter
135 unsigned int is
= i
++;
137 // find the end of the filter
138 for( ; i
< filter2
.Len(); i
++ )
140 if(filter2
[i
] == wxT('|'))
144 if( i
-is
-1 > 0 && is
+1 < filter2
.Len() )
146 if( filter2
.Mid(is
+1,i
-is
-1).Contains(defaultExtension
) )
148 filterFind
= filterIndex
;
157 fileDialog
.SetFilterIndex(filterFind
);
161 if ( fileDialog
.ShowModal() == wxID_OK
)
163 filename
= fileDialog
.GetPath();
170 wxString
wxFileSelectorEx(const wxChar
*title
,
171 const wxChar
*defaultDir
,
172 const wxChar
*defaultFileName
,
173 int* defaultFilterIndex
,
174 const wxChar
*filter
,
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
));
189 if ( fileDialog
.ShowModal() == wxID_OK
)
191 if ( defaultFilterIndex
)
192 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
194 filename
= fileDialog
.GetPath();
200 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
201 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
202 long style
, const wxPoint
& WXUNUSED(pos
))
205 m_dialogStyle
= style
;
206 if ( ( m_dialogStyle
& wxMULTIPLE
) && ( m_dialogStyle
& wxSAVE
) )
207 m_dialogStyle
&= ~wxMULTIPLE
;
210 m_fileName
= defaultFileName
;
212 m_wildCard
= wildCard
;
216 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
221 if ( m_dir
.Last() != _T('\\') )
224 size_t count
= m_fileNames
.GetCount();
225 for ( size_t n
= 0; n
< count
; n
++ )
227 paths
.Add(dir
+ m_fileNames
[n
]);
231 int wxFileDialog::ShowModal()
234 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
235 if (!hWnd
&& wxTheApp
->GetTopWindow())
236 hWnd
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
238 static wxChar fileNameBuffer
[ wxMAXPATH
]; // the file-name
239 wxChar titleBuffer
[ wxMAXFILE
+1+wxMAXEXT
]; // the file-name, without path
241 *fileNameBuffer
= wxT('\0');
242 *titleBuffer
= wxT('\0');
245 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
246 msw_flags
|= OFN_HIDEREADONLY
;
247 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
248 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
250 if (m_dialogStyle
& wxMULTIPLE
)
252 // OFN_EXPLORER must always be specified with OFN_ALLOWMULTISELECT
253 msw_flags
|= OFN_EXPLORER
| OFN_ALLOWMULTISELECT
;
256 // if wxCHANGE_DIR flag is not given we shouldn't change the CWD which the
257 // standard dialog does by default
258 if ( !(m_dialogStyle
& wxCHANGE_DIR
) )
260 msw_flags
|= OFN_NOCHANGEDIR
;
263 if ( m_dialogStyle
& wxOVERWRITE_PROMPT
)
265 msw_flags
|= OFN_OVERWRITEPROMPT
;
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
));
279 of
.lStructSize
= sizeof(OPENFILENAME
);
283 of
.lpstrTitle
= WXSTRINGCAST m_message
;
284 of
.lpstrFileTitle
= titleBuffer
;
285 of
.nMaxFileTitle
= wxMAXFILE
+ 1 + wxMAXEXT
; // Windows 3.0 and 3.1
287 // Convert forward slashes to backslashes (file selector doesn't like
288 // forward slashes) and also squeeze multiple consecutive slashes into one
289 // as it doesn't like two backslashes in a row neither
291 size_t len
= m_dir
.length();
293 for ( size_t i
= 0; i
< len
; i
++ )
295 wxChar ch
= m_dir
[i
];
299 // convert to backslash
305 while ( i
< len
- 1 )
307 wxChar chNext
= m_dir
[i
+ 1];
308 if ( chNext
!= _T('\\') && chNext
!= _T('/') )
311 // ignore the next one
322 of
.lpstrInitialDir
= dir
.c_str();
324 of
.Flags
= msw_flags
;
327 //=== Like Alejandro Sierra's wildcard modification >>===================
329 In wxFileSelector you can put, instead of a single wild_card,
330 pairs of strings separated by '|'.
331 The first string is a description, and the
332 second is the wild card. You can put any number of pairs.
334 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
336 If you put a single wild card, it works as before the modification.
338 //=======================================================================
341 if ( wxStrlen(m_wildCard
) == 0 )
342 theFilter
= wxString(wxT("*.*"));
344 theFilter
= m_wildCard
;
345 wxString filterBuffer
;
347 if ( !wxStrchr( theFilter
, wxT('|') ) ) { // only one filter ==> default text
348 filterBuffer
.Printf(_("Files (%s)|%s"),
349 theFilter
.c_str(), theFilter
.c_str());
351 else { // more then one filter
352 filterBuffer
= theFilter
;
356 filterBuffer
+= wxT("|");
358 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
359 if ( filterBuffer
.GetChar(i
) == wxT('|') ) {
360 filterBuffer
[i
] = wxT('\0');
364 of
.lpstrFilter
= (LPTSTR
)(const wxChar
*)filterBuffer
;
365 of
.nFilterIndex
= m_filterIndex
+ 1;
367 //=== Setting defaultFileName >>=========================================
369 wxStrncpy( fileNameBuffer
, (const wxChar
*)m_fileName
, wxMAXPATH
-1 );
370 fileNameBuffer
[ wxMAXPATH
-1 ] = wxT('\0');
372 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
373 of
.nMaxFile
= wxMAXPATH
;
375 //== Execute FileDialog >>=================================================
377 bool success
= (m_dialogStyle
& wxSAVE
? GetSaveFileName(&of
)
378 : GetOpenFileName(&of
)) != 0;
380 DWORD errCode
= CommDlgExtendedError();
383 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
385 // The struct size has changed so try a smaller or bigger size
387 int oldStructSize
= of
.lStructSize
;
388 of
.lStructSize
= oldStructSize
- (sizeof(void *) + 2*sizeof(DWORD
));
389 success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
390 : (GetOpenFileName(&of
) != 0);
391 errCode
= CommDlgExtendedError();
393 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
395 of
.lStructSize
= oldStructSize
+ (sizeof(void *) + 2*sizeof(DWORD
));
396 success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
397 : (GetOpenFileName(&of
) != 0);
406 if ( ( m_dialogStyle
& wxMULTIPLE
) &&
407 #if defined(OFN_EXPLORER)
408 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT('\0') ) )
410 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT(' ') ) )
411 #endif // OFN_EXPLORER
413 #if defined(OFN_EXPLORER)
414 m_dir
= fileNameBuffer
;
416 m_fileName
= &fileNameBuffer
[i
];
417 m_fileNames
.Add(m_fileName
);
418 i
+= m_fileName
.Len() + 1;
420 while (fileNameBuffer
[i
] != wxT('\0'))
422 m_fileNames
.Add(&fileNameBuffer
[i
]);
423 i
+= wxStrlen(&fileNameBuffer
[i
]) + 1;
426 wxStringTokenizer
toke(fileNameBuffer
, " \t\r\n");
427 m_dir
= toke
.GetNextToken();
428 m_fileName
= toke
.GetNextToken();
429 m_fileNames
.Add(m_fileName
);
431 while (toke
.HasMoreTokens())
432 m_fileNames
.Add(toke
.GetNextToken());
433 #endif // OFN_EXPLORER
436 if ( m_dir
.Last() != _T('\\') )
440 m_path
= dir
+ m_fileName
;
444 const wxChar
* extension
= NULL
;
446 //=== Adding the correct extension >>=================================
448 m_filterIndex
= (int)of
.nFilterIndex
- 1;
450 if ( !of
.nFileExtension
|| (of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != wxT('.')) )
451 { // user has typed an filename
452 // without an extension:
454 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
455 extension
= filterBuffer
;
457 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
458 extension
= extension
+ wxStrlen( extension
) +1;
461 extension
= wxStrrchr( extension
, wxT('.') );
462 if ( extension
// != "blabla"
463 && !wxStrrchr( extension
, wxT('*') ) // != "blabla.*"
464 && !wxStrrchr( extension
, wxT('?') ) // != "blabla.?"
465 && extension
[1] // != "blabla."
466 && extension
[1] != wxT(' ') ) // != "blabla. "
468 // now concat extension to the fileName:
469 m_fileName
= wxString(fileNameBuffer
) + extension
;
471 int len
= wxStrlen( fileNameBuffer
);
472 wxStrncpy( fileNameBuffer
+ len
, extension
, wxMAXPATH
- len
);
473 fileNameBuffer
[ wxMAXPATH
-1 ] = wxT('\0');
477 m_path
= fileNameBuffer
;
478 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
479 m_fileNames
.Add(m_fileName
);
480 m_dir
= wxPathOnly(fileNameBuffer
);
485 // common dialog failed - why?
487 DWORD dwErr
= CommDlgExtendedError();
490 // this msg is only for developers
491 wxLogError(wxT("Common dialog failed with error code %0lx."),
494 //else: it was just cancelled
498 return success
? wxID_OK
: wxID_CANCEL
;
502 // Generic file load/save dialog (for internal use only)
504 wxString
wxDefaultFileSelector(bool load
,
506 const wxChar
*extension
,
507 const wxChar
*default_name
,
513 str
= _("Load %s file");
515 str
= _("Save %s file");
516 prompt
.Printf(str
, what
);
518 const wxChar
*ext
= extension
;
519 if (*ext
== wxT('.'))
523 wild
.Printf(wxT("*.%s"), ext
);
525 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
526 load
? wxOPEN
: wxSAVE
, parent
);
529 // Generic file load dialog
530 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
531 const wxChar
*extension
,
532 const wxChar
*default_name
,
535 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
538 // Generic file save dialog
539 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
540 const wxChar
*extension
,
541 const wxChar
*default_name
,
544 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);
547 #endif // wxUSE_FILEDLG