1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filedlg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/msgdlg.h"
26 #include "wx/dialog.h"
27 #include "wx/filedlg.h"
31 #include "wx/msw/private.h"
34 #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
42 #include "wx/tokenzr.h"
44 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
46 wxString
wxFileSelector(const wxChar
*title
,
47 const wxChar
*defaultDir
,
48 const wxChar
*defaultFileName
,
49 const wxChar
*defaultExtension
,
55 // In the original implementation, defaultExtension is passed to the
56 // lpstrDefExt member of OPENFILENAME. This extension, if non-NULL, is
57 // appended to the filename if the user fails to type an extension. The new
58 // implementation (taken from wxFileSelectorEx) appends the extension
59 // automatically, by looking at the filter specification. In fact this
60 // should be better than the native Microsoft implementation because
61 // Windows only allows *one* default extension, whereas here we do the
62 // right thing depending on the filter the user has chosen.
64 // If there's a default extension specified but no filter, we create a
68 if ( defaultExtension
&& !filter
)
69 filter2
= wxString(wxT("*.")) + defaultExtension
;
73 wxString defaultDirString
;
75 defaultDirString
= defaultDir
;
77 wxString defaultFilenameString
;
79 defaultFilenameString
= defaultFileName
;
81 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
82 defaultFilenameString
, filter2
,
83 flags
, wxPoint(x
, y
));
84 if( wxStrlen(defaultExtension
) != 0 )
89 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
91 if( filter2
.GetChar(i
) == wxT('|') )
93 // save the start index of the new filter
94 unsigned int is
= i
++;
97 // find the end of the filter
98 for( ; i
< filter2
.Len(); i
++ )
100 if(filter2
[i
] == wxT('|'))
104 if( i
-is
-1 > 0 && is
+1 < filter2
.Len() )
106 if( filter2
.Mid(is
+1,i
-is
-1).Contains(defaultExtension
) )
107 // if( filter2.Mid(is+1,i-is-1) == defaultExtension )
109 filterFind
= filterIndex
;
116 fileDialog
.SetFilterIndex(filterFind
);
119 if ( fileDialog
.ShowModal() == wxID_OK
)
121 wxStrcpy(wxBuffer
, (const wxChar
*)fileDialog
.GetPath());
125 return wxGetEmptyString();
129 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
149 wxString
wxFileSelectorEx(const wxChar
*title
,
150 const wxChar
*defaultDir
,
151 const wxChar
*defaultFileName
,
152 int* defaultFilterIndex
,
153 const wxChar
*filter
,
160 wxFileDialog
fileDialog(parent
, title
? title
: wxT(""), defaultDir
? defaultDir
: wxT(""),
161 defaultFileName
? defaultFileName
: wxT(""), filter
? filter
: wxT(""), flags
, wxPoint(x
, y
));
163 if ( fileDialog
.ShowModal() == wxID_OK
)
165 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
166 wxStrcpy(wxBuffer
, (const wxChar
*)fileDialog
.GetPath());
170 return wxGetEmptyString();
173 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
174 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
175 long style
, const wxPoint
& pos
)
178 m_dialogStyle
= style
;
179 if ( ( m_dialogStyle
& wxMULTIPLE
) && ( m_dialogStyle
& wxSAVE
) )
180 m_dialogStyle
&= ~wxMULTIPLE
;
183 m_fileName
= defaultFileName
;
185 m_wildCard
= wildCard
;
189 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
194 if ( m_dir
.Last() != _T('\\') )
197 size_t count
= m_fileNames
.GetCount();
198 for ( size_t n
= 0; n
< count
; n
++ )
200 paths
.Add(dir
+ m_fileNames
[n
]);
204 int wxFileDialog::ShowModal()
207 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
209 static wxChar fileNameBuffer
[ MAXPATH
]; // the file-name
210 wxChar titleBuffer
[ MAXFILE
+1+MAXEXT
]; // the file-name, without path
212 *fileNameBuffer
= wxT('\0');
213 *titleBuffer
= wxT('\0');
216 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
217 msw_flags
|= OFN_HIDEREADONLY
;
218 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
219 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
220 if (m_dialogStyle
& wxMULTIPLE
)
222 #if defined(OFN_EXPLORER)
224 #endif // OFN_EXPLORER
225 OFN_ALLOWMULTISELECT
;
228 memset(&of
, 0, sizeof(OPENFILENAME
));
230 of
.lpstrCustomFilter
= NULL
; // system should not save custom filter
231 of
.nMaxCustFilter
= 0L;
233 of
.nFileOffset
= 0; // 0-based pointer to filname in lpstFile
234 of
.nFileExtension
= 0; // 0-based pointer to extension in lpstrFile
235 of
.lpstrDefExt
= NULL
; // no default extension
237 of
.lStructSize
= sizeof(OPENFILENAME
);
239 of
.lpstrTitle
= WXSTRINGCAST m_message
;
242 of
.lpstrFileTitle
= titleBuffer
;
243 of
.nMaxFileTitle
= MAXFILE
+ 1 + MAXEXT
; // Windows 3.0 and 3.1
245 // Convert forward slashes to backslashes (file selector doesn't like
248 size_t len
= m_dir
.Length();
249 for (i
= 0; i
< len
; i
++)
250 if (m_dir
[i
] == wxT('/'))
251 m_dir
[i
] = wxT('\\');
253 of
.lpstrInitialDir
= m_dir
.c_str();
255 of
.Flags
= msw_flags
;
258 //=== Like Alejandro Sierra's wildcard modification >>===================
260 In wxFileSelector you can put, instead of a single wild_card,
261 pairs of strings separated by '|'.
262 The first string is a description, and the
263 second is the wild card. You can put any number of pairs.
265 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
267 If you put a single wild card, it works as before the modification.
269 //=======================================================================
272 if ( wxStrlen(m_wildCard
) == 0 )
273 theFilter
= wxString(wxT("*.*"));
275 theFilter
= m_wildCard
;
276 wxString filterBuffer
;
278 if ( !wxStrchr( theFilter
, wxT('|') ) ) { // only one filter ==> default text
279 filterBuffer
.Printf(_("Files (%s)|%s"),
280 theFilter
.c_str(), theFilter
.c_str());
282 else { // more then one filter
283 filterBuffer
= theFilter
;
287 filterBuffer
+= wxT("|");
289 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
290 if ( filterBuffer
.GetChar(i
) == wxT('|') ) {
291 filterBuffer
[i
] = wxT('\0');
295 of
.lpstrFilter
= (LPTSTR
)(const wxChar
*)filterBuffer
;
296 of
.nFilterIndex
= m_filterIndex
+ 1;
298 //=== Setting defaultFileName >>=========================================
300 wxStrncpy( fileNameBuffer
, (const wxChar
*)m_fileName
, MAXPATH
-1 );
301 fileNameBuffer
[ MAXPATH
-1 ] = wxT('\0');
303 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
304 of
.nMaxFile
= MAXPATH
;
306 //== Execute FileDialog >>=================================================
308 bool success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
309 : (GetOpenFileName(&of
) != 0);
315 if ( ( m_dialogStyle
& wxMULTIPLE
) &&
316 #if defined(OFN_EXPLORER)
317 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT('\0') ) )
319 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT(' ') ) )
320 #endif // OFN_EXPLORER
322 #if defined(OFN_EXPLORER)
323 m_dir
= fileNameBuffer
;
325 m_fileName
= &fileNameBuffer
[i
];
326 m_fileNames
.Add(m_fileName
);
327 i
+= m_fileName
.Len() + 1;
329 while (fileNameBuffer
[i
] != wxT('\0'))
331 m_fileNames
.Add(&fileNameBuffer
[i
]);
332 i
+= wxStrlen(&fileNameBuffer
[i
]) + 1;
335 wxStringTokenizer
toke(fileNameBuffer
, " \t\r\n");
336 m_dir
= toke
.GetNextToken();
337 m_fileName
= toke
.GetNextToken();
338 m_fileNames
.Add(m_fileName
);
340 while (toke
.HasMoreTokens())
341 m_fileNames
.Add(toke
.GetNextToken());
342 #endif // OFN_EXPLORER
345 if ( m_dir
.Last() != _T('\\') )
349 m_path
= dir
+ m_fileName
;
353 const wxChar
* extension
= NULL
;
355 //=== Adding the correct extension >>=================================
357 m_filterIndex
= (int)of
.nFilterIndex
- 1;
359 if ( !of
.nFileExtension
|| (of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != wxT('.')) )
360 { // user has typed an filename
361 // without an extension:
363 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
364 extension
= filterBuffer
;
366 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
367 extension
= extension
+ wxStrlen( extension
) +1;
370 extension
= wxStrrchr( extension
, wxT('.') );
371 if ( extension
// != "blabla"
372 && !wxStrrchr( extension
, wxT('*') ) // != "blabla.*"
373 && !wxStrrchr( extension
, wxT('?') ) // != "blabla.?"
374 && extension
[1] // != "blabla."
375 && extension
[1] != wxT(' ') ) // != "blabla. "
377 // now concat extension to the fileName:
378 m_fileName
= wxString(fileNameBuffer
) + extension
;
380 int len
= wxStrlen( fileNameBuffer
);
381 wxStrncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
382 fileNameBuffer
[ MAXPATH
-1 ] = wxT('\0');
386 m_path
= fileNameBuffer
;
387 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
388 m_fileNames
.Add(m_fileName
);
389 m_dir
= wxPathOnly(fileNameBuffer
);
393 //=== Simulating the wxOVERWRITE_PROMPT >>============================
395 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
396 ::wxFileExists( fileNameBuffer
) )
398 wxString messageText
;
399 messageText
.Printf(_("Replace file '%s'?"), fileNameBuffer
);
401 if ( wxMessageBox(messageText
, m_message
, wxYES_NO
) != wxYES
)
410 // common dialog failed - why?
412 DWORD dwErr
= CommDlgExtendedError();
415 // this msg is only for developers
416 wxLogError(wxT("Common dialog failed with error code %0lx."),
419 //else: it was just cancelled
423 return success
? wxID_OK
: wxID_CANCEL
;
427 // Generic file load/save dialog (for internal use only)
429 wxString
wxDefaultFileSelector(bool load
,
431 const wxChar
*extension
,
432 const wxChar
*default_name
,
437 if (load
) str
= _("Load %s file");
438 else str
= _("Save %s file");
439 prompt
.Printf(str
, what
);
441 const wxChar
*ext
= extension
;
442 if (*ext
== wxT('.'))
446 wild
.Printf(wxT("*.%s"), ext
);
448 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
451 // Generic file load dialog
452 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
453 const wxChar
*extension
,
454 const wxChar
*default_name
,
457 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
460 // Generic file save dialog
461 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
462 const wxChar
*extension
,
463 const wxChar
*default_name
,
466 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);