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"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
60 # define wxMAXPATH 4096
62 # define wxMAXPATH 1024
65 # define wxMAXFILE 1024
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 wxString
wxFileSelector(const wxChar
*title
,
84 const wxChar
*defaultDir
,
85 const wxChar
*defaultFileName
,
86 const wxChar
*defaultExtension
,
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.
101 // If there's a default extension specified but no filter, we create a
105 if ( defaultExtension
&& !filter
)
106 filter2
= wxString(wxT("*.")) + defaultExtension
;
110 wxString defaultDirString
;
112 defaultDirString
= defaultDir
;
114 wxString defaultFilenameString
;
116 defaultFilenameString
= defaultFileName
;
118 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
119 defaultFilenameString
, filter2
,
120 flags
, wxPoint(x
, y
));
121 if( wxStrlen(defaultExtension
) != 0 )
126 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
128 if( filter2
.GetChar(i
) == wxT('|') )
130 // save the start index of the new filter
131 unsigned int is
= i
++;
133 // find the end of the filter
134 for( ; i
< filter2
.Len(); i
++ )
136 if(filter2
[i
] == wxT('|'))
140 if( i
-is
-1 > 0 && is
+1 < filter2
.Len() )
142 if( filter2
.Mid(is
+1,i
-is
-1).Contains(defaultExtension
) )
144 filterFind
= filterIndex
;
153 fileDialog
.SetFilterIndex(filterFind
);
157 if ( fileDialog
.ShowModal() == wxID_OK
)
159 filename
= fileDialog
.GetPath();
166 wxString
wxFileSelectorEx(const wxChar
*title
,
167 const wxChar
*defaultDir
,
168 const wxChar
*defaultFileName
,
169 int* defaultFilterIndex
,
170 const wxChar
*filter
,
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
));
185 if ( fileDialog
.ShowModal() == wxID_OK
)
187 if ( defaultFilterIndex
)
188 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
190 filename
= fileDialog
.GetPath();
196 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
197 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
198 long style
, const wxPoint
& WXUNUSED(pos
))
201 m_dialogStyle
= style
;
202 if ( ( m_dialogStyle
& wxMULTIPLE
) && ( m_dialogStyle
& wxSAVE
) )
203 m_dialogStyle
&= ~wxMULTIPLE
;
206 m_fileName
= defaultFileName
;
208 m_wildCard
= wildCard
;
212 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
217 if ( m_dir
.Last() != _T('\\') )
220 size_t count
= m_fileNames
.GetCount();
221 for ( size_t n
= 0; n
< count
; n
++ )
223 paths
.Add(dir
+ m_fileNames
[n
]);
227 int wxFileDialog::ShowModal()
230 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
231 if (!hWnd
&& wxTheApp
->GetTopWindow())
232 hWnd
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
234 static wxChar fileNameBuffer
[ wxMAXPATH
]; // the file-name
235 wxChar titleBuffer
[ wxMAXFILE
+1+wxMAXEXT
]; // the file-name, without path
237 *fileNameBuffer
= wxT('\0');
238 *titleBuffer
= wxT('\0');
241 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
242 msw_flags
|= OFN_HIDEREADONLY
;
243 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
244 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
245 if (m_dialogStyle
& wxMULTIPLE
)
247 #if defined(OFN_EXPLORER)
249 #endif // OFN_EXPLORER
250 OFN_ALLOWMULTISELECT
;
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
));
263 of
.lStructSize
= sizeof(OPENFILENAME
);
267 of
.lpstrTitle
= WXSTRINGCAST m_message
;
268 of
.lpstrFileTitle
= titleBuffer
;
269 of
.nMaxFileTitle
= wxMAXFILE
+ 1 + wxMAXEXT
; // Windows 3.0 and 3.1
271 // Convert forward slashes to backslashes (file selector doesn't like
274 size_t len
= m_dir
.Length();
275 for (i
= 0; i
< len
; i
++)
276 if (m_dir
[i
] == wxT('/'))
277 m_dir
[i
] = wxT('\\');
279 of
.lpstrInitialDir
= m_dir
.c_str();
281 of
.Flags
= msw_flags
;
284 //=== Like Alejandro Sierra's wildcard modification >>===================
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.
291 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
293 If you put a single wild card, it works as before the modification.
295 //=======================================================================
298 if ( wxStrlen(m_wildCard
) == 0 )
299 theFilter
= wxString(wxT("*.*"));
301 theFilter
= m_wildCard
;
302 wxString filterBuffer
;
304 if ( !wxStrchr( theFilter
, wxT('|') ) ) { // only one filter ==> default text
305 filterBuffer
.Printf(_("Files (%s)|%s"),
306 theFilter
.c_str(), theFilter
.c_str());
308 else { // more then one filter
309 filterBuffer
= theFilter
;
313 filterBuffer
+= wxT("|");
315 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
316 if ( filterBuffer
.GetChar(i
) == wxT('|') ) {
317 filterBuffer
[i
] = wxT('\0');
321 of
.lpstrFilter
= (LPTSTR
)(const wxChar
*)filterBuffer
;
322 of
.nFilterIndex
= m_filterIndex
+ 1;
324 //=== Setting defaultFileName >>=========================================
326 wxStrncpy( fileNameBuffer
, (const wxChar
*)m_fileName
, wxMAXPATH
-1 );
327 fileNameBuffer
[ wxMAXPATH
-1 ] = wxT('\0');
329 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
330 of
.nMaxFile
= wxMAXPATH
;
332 //== Execute FileDialog >>=================================================
334 bool success
= (m_dialogStyle
& wxSAVE
? GetSaveFileName(&of
)
335 : GetOpenFileName(&of
)) != 0;
337 DWORD errCode
= CommDlgExtendedError();
340 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
342 // The struct size has changed so try a smaller or bigger size
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();
350 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
352 of
.lStructSize
= oldStructSize
+ (sizeof(void *) + 2*sizeof(DWORD
));
353 success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
354 : (GetOpenFileName(&of
) != 0);
363 if ( ( m_dialogStyle
& wxMULTIPLE
) &&
364 #if defined(OFN_EXPLORER)
365 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT('\0') ) )
367 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT(' ') ) )
368 #endif // OFN_EXPLORER
370 #if defined(OFN_EXPLORER)
371 m_dir
= fileNameBuffer
;
373 m_fileName
= &fileNameBuffer
[i
];
374 m_fileNames
.Add(m_fileName
);
375 i
+= m_fileName
.Len() + 1;
377 while (fileNameBuffer
[i
] != wxT('\0'))
379 m_fileNames
.Add(&fileNameBuffer
[i
]);
380 i
+= wxStrlen(&fileNameBuffer
[i
]) + 1;
383 wxStringTokenizer
toke(fileNameBuffer
, " \t\r\n");
384 m_dir
= toke
.GetNextToken();
385 m_fileName
= toke
.GetNextToken();
386 m_fileNames
.Add(m_fileName
);
388 while (toke
.HasMoreTokens())
389 m_fileNames
.Add(toke
.GetNextToken());
390 #endif // OFN_EXPLORER
393 if ( m_dir
.Last() != _T('\\') )
397 m_path
= dir
+ m_fileName
;
401 const wxChar
* extension
= NULL
;
403 //=== Adding the correct extension >>=================================
405 m_filterIndex
= (int)of
.nFilterIndex
- 1;
407 if ( !of
.nFileExtension
|| (of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != wxT('.')) )
408 { // user has typed an filename
409 // without an extension:
411 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
412 extension
= filterBuffer
;
414 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
415 extension
= extension
+ wxStrlen( extension
) +1;
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. "
425 // now concat extension to the fileName:
426 m_fileName
= wxString(fileNameBuffer
) + extension
;
428 int len
= wxStrlen( fileNameBuffer
);
429 wxStrncpy( fileNameBuffer
+ len
, extension
, wxMAXPATH
- len
);
430 fileNameBuffer
[ wxMAXPATH
-1 ] = wxT('\0');
434 m_path
= fileNameBuffer
;
435 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
436 m_fileNames
.Add(m_fileName
);
437 m_dir
= wxPathOnly(fileNameBuffer
);
441 //=== Simulating the wxOVERWRITE_PROMPT >>============================
443 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
444 ::wxFileExists( fileNameBuffer
) )
446 wxString messageText
;
447 messageText
.Printf(_("Replace file '%s'?"), fileNameBuffer
);
449 if ( wxMessageBox(messageText
, m_message
, wxYES_NO
) != wxYES
)
458 // common dialog failed - why?
460 DWORD dwErr
= CommDlgExtendedError();
463 // this msg is only for developers
464 wxLogError(wxT("Common dialog failed with error code %0lx."),
467 //else: it was just cancelled
471 return success
? wxID_OK
: wxID_CANCEL
;
475 // Generic file load/save dialog (for internal use only)
477 wxString
wxDefaultFileSelector(bool load
,
479 const wxChar
*extension
,
480 const wxChar
*default_name
,
486 str
= _("Load %s file");
488 str
= _("Save %s file");
489 prompt
.Printf(str
, what
);
491 const wxChar
*ext
= extension
;
492 if (*ext
== wxT('.'))
496 wild
.Printf(wxT("*.%s"), ext
);
498 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
499 load
? wxOPEN
: wxSAVE
, parent
);
502 // Generic file load dialog
503 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
504 const wxChar
*extension
,
505 const wxChar
*default_name
,
508 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
511 // Generic file save dialog
512 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
513 const wxChar
*extension
,
514 const wxChar
*default_name
,
517 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);
520 #endif // wxUSE_FILEDLG