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"
27 #include "wx/msgdlg.h"
28 #include "wx/dialog.h"
29 #include "wx/filedlg.h"
36 #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
40 #include "wx/msw/private.h"
46 #if !USE_SHARED_LIBRARY
47 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
50 wxString
wxFileSelector(const wxChar
*title
,
51 const wxChar
*defaultDir
,
52 const wxChar
*defaultFileName
,
53 const wxChar
*defaultExtension
,
59 // In the original implementation, defaultExtension is passed to the
60 // lpstrDefExt member of OPENFILENAME. This extension, if non-NULL, is
61 // appended to the filename if the user fails to type an extension. The new
62 // implementation (taken from wxFileSelectorEx) appends the extension
63 // automatically, by looking at the filter specification. In fact this
64 // should be better than the native Microsoft implementation because
65 // Windows only allows *one* default extension, whereas here we do the
66 // right thing depending on the filter the user has chosen.
68 // If there's a default extension specified but no filter, we create a
72 if ( defaultExtension
&& !filter
)
73 filter2
= wxString(wxT("*.")) + defaultExtension
;
77 wxString defaultDirString
;
79 defaultDirString
= defaultDir
;
81 wxString defaultFilenameString
;
83 defaultFilenameString
= defaultFileName
;
85 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
86 defaultFilenameString
, filter2
,
87 flags
, wxPoint(x
, y
));
88 if( wxStrlen(defaultExtension
) != 0 )
93 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
95 if( filter2
.GetChar(i
) == wxT('|') )
97 // save the start index of the new filter
98 unsigned int is
= i
++;
101 // find the end of the filter
102 for( ; i
< filter2
.Len(); i
++ )
104 if(filter2
[i
] == wxT('|'))
108 if( i
-is
-1 > 0 && is
+1 < filter2
.Len() )
110 if( filter2
.Mid(is
+1,i
-is
-1).Contains(defaultExtension
) )
111 // if( filter2.Mid(is+1,i-is-1) == defaultExtension )
113 filterFind
= filterIndex
;
120 fileDialog
.SetFilterIndex(filterFind
);
123 if ( fileDialog
.ShowModal() == wxID_OK
)
125 wxStrcpy(wxBuffer
, (const wxChar
*)fileDialog
.GetPath());
129 return wxGetEmptyString();
133 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
153 wxString
wxFileSelectorEx(const wxChar
*title
,
154 const wxChar
*defaultDir
,
155 const wxChar
*defaultFileName
,
156 int* defaultFilterIndex
,
157 const wxChar
*filter
,
164 wxFileDialog
fileDialog(parent
, title
? title
: wxT(""), defaultDir
? defaultDir
: wxT(""),
165 defaultFileName
? defaultFileName
: wxT(""), filter
? filter
: wxT(""), flags
, wxPoint(x
, y
));
167 if ( fileDialog
.ShowModal() == wxID_OK
)
169 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
170 wxStrcpy(wxBuffer
, (const wxChar
*)fileDialog
.GetPath());
174 return wxGetEmptyString();
177 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
178 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
179 long style
, const wxPoint
& pos
)
182 m_dialogStyle
= style
;
183 if ( ( m_dialogStyle
& wxMULTIPLE
) && ( m_dialogStyle
& wxSAVE
) )
184 m_dialogStyle
&= ~wxMULTIPLE
;
187 m_fileName
= defaultFileName
;
189 m_wildCard
= wildCard
;
193 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
198 if ( m_dir
.Last() != _T('\\') )
201 size_t count
= m_fileNames
.GetCount();
202 for ( size_t n
= 0; n
< count
; n
++ )
204 paths
.Add(dir
+ m_fileNames
[n
]);
208 int wxFileDialog::ShowModal()
211 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
213 static wxChar fileNameBuffer
[ MAXPATH
]; // the file-name
214 wxChar titleBuffer
[ MAXFILE
+1+MAXEXT
]; // the file-name, without path
216 *fileNameBuffer
= wxT('\0');
217 *titleBuffer
= wxT('\0');
220 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
221 msw_flags
|= OFN_HIDEREADONLY
;
222 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
223 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
224 if (m_dialogStyle
& wxMULTIPLE
)
226 #if defined(OFN_EXPLORER)
228 #endif // OFN_EXPLORER
229 OFN_ALLOWMULTISELECT
;
232 memset(&of
, 0, sizeof(OPENFILENAME
));
234 of
.lpstrCustomFilter
= NULL
; // system should not save custom filter
235 of
.nMaxCustFilter
= 0L;
237 of
.nFileOffset
= 0; // 0-based pointer to filname in lpstFile
238 of
.nFileExtension
= 0; // 0-based pointer to extension in lpstrFile
239 of
.lpstrDefExt
= NULL
; // no default extension
241 of
.lStructSize
= sizeof(OPENFILENAME
);
243 of
.lpstrTitle
= WXSTRINGCAST m_message
;
246 of
.lpstrFileTitle
= titleBuffer
;
247 of
.nMaxFileTitle
= MAXFILE
+ 1 + MAXEXT
; // Windows 3.0 and 3.1
249 // Convert forward slashes to backslashes (file selector doesn't like
252 size_t len
= m_dir
.Length();
253 for (i
= 0; i
< len
; i
++)
254 if (m_dir
[i
] == wxT('/'))
255 m_dir
[i
] = wxT('\\');
257 of
.lpstrInitialDir
= m_dir
.c_str();
259 of
.Flags
= msw_flags
;
262 //=== Like Alejandro Sierra's wildcard modification >>===================
264 In wxFileSelector you can put, instead of a single wild_card,
265 pairs of strings separated by '|'.
266 The first string is a description, and the
267 second is the wild card. You can put any number of pairs.
269 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
271 If you put a single wild card, it works as before the modification.
273 //=======================================================================
276 if ( wxStrlen(m_wildCard
) == 0 )
277 theFilter
= wxString(wxT("*.*"));
279 theFilter
= m_wildCard
;
280 wxString filterBuffer
;
282 if ( !wxStrchr( theFilter
, wxT('|') ) ) { // only one filter ==> default text
283 filterBuffer
.Printf(_("Files (%s)|%s"),
284 theFilter
.c_str(), theFilter
.c_str());
286 else { // more then one filter
287 filterBuffer
= theFilter
;
291 filterBuffer
+= wxT("|");
293 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
294 if ( filterBuffer
.GetChar(i
) == wxT('|') ) {
295 filterBuffer
[i
] = wxT('\0');
299 of
.lpstrFilter
= (LPTSTR
)(const wxChar
*)filterBuffer
;
300 of
.nFilterIndex
= m_filterIndex
;
302 //=== Setting defaultFileName >>=========================================
304 wxStrncpy( fileNameBuffer
, (const wxChar
*)m_fileName
, MAXPATH
-1 );
305 fileNameBuffer
[ MAXPATH
-1 ] = wxT('\0');
307 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
308 of
.nMaxFile
= MAXPATH
;
310 //== Execute FileDialog >>=================================================
312 bool success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
313 : (GetOpenFileName(&of
) != 0);
319 if ( ( m_dialogStyle
& wxMULTIPLE
) &&
320 #if defined(OFN_EXPLORER)
321 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT('\0') ) )
323 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT(' ') ) )
324 #endif // OFN_EXPLORER
326 #if defined(OFN_EXPLORER)
327 m_dir
= fileNameBuffer
;
329 m_fileName
= &fileNameBuffer
[i
];
330 m_fileNames
.Add(m_fileName
);
331 i
+= m_fileName
.Len() + 1;
333 while (fileNameBuffer
[i
] != wxT('\0'))
335 m_fileNames
.Add(&fileNameBuffer
[i
]);
336 i
+= wxStrlen(&fileNameBuffer
[i
]) + 1;
339 wxStringTokenizer
toke(fileNameBuffer
, " \t\r\n");
340 m_dir
= toke
.GetNextToken();
341 m_fileName
= toke
.GetNextToken();
342 m_fileNames
.Add(m_fileName
);
344 while (toke
.HasMoreTokens())
345 m_fileNames
.Add(toke
.GetNextToken());
346 #endif // OFN_EXPLORER
349 if ( m_dir
.Last() != _T('\\') )
353 m_path
= dir
+ m_fileName
;
357 const wxChar
* extension
= NULL
;
359 //=== Adding the correct extension >>=================================
361 m_filterIndex
= (int)of
.nFilterIndex
;
363 if ( of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != wxT('.') )
364 { // user has typed an filename
365 // without an extension:
367 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
368 extension
= filterBuffer
;
370 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
371 extension
= extension
+ wxStrlen( extension
) +1;
374 extension
= wxStrrchr( extension
, wxT('.') );
375 if ( extension
// != "blabla"
376 && !wxStrrchr( extension
, wxT('*') ) // != "blabla.*"
377 && !wxStrrchr( extension
, wxT('?') ) // != "blabla.?"
378 && extension
[1] // != "blabla."
379 && extension
[1] != wxT(' ') ) // != "blabla. "
381 // now concat extension to the fileName:
382 m_fileName
= wxString(fileNameBuffer
) + extension
;
384 int len
= wxStrlen( fileNameBuffer
);
385 wxStrncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
386 fileNameBuffer
[ MAXPATH
-1 ] = wxT('\0');
390 m_path
= fileNameBuffer
;
391 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
392 m_fileNames
.Add(m_fileName
);
393 m_dir
= wxPathOnly(fileNameBuffer
);
397 //=== Simulating the wxOVERWRITE_PROMPT >>============================
399 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
400 ::wxFileExists( fileNameBuffer
) )
402 wxString messageText
;
403 messageText
.Printf(_("Replace file '%s'?"), fileNameBuffer
);
405 if ( wxMessageBox(messageText
, m_message
, wxYES_NO
) != wxYES
)
414 // common dialog failed - why?
416 DWORD dwErr
= CommDlgExtendedError();
419 // this msg is only for developers
420 wxLogError(wxT("Common dialog failed with error code %0lx."),
423 //else: it was just cancelled
427 return success
? wxID_OK
: wxID_CANCEL
;
431 // Generic file load/save dialog (for internal use only)
433 wxString
wxDefaultFileSelector(bool load
,
435 const wxChar
*extension
,
436 const wxChar
*default_name
,
441 if (load
) str
= _("Load %s file");
442 else str
= _("Save %s file");
443 prompt
.Printf(str
, what
);
445 const wxChar
*ext
= extension
;
446 if (*ext
== wxT('.'))
450 wild
.Printf(wxT("*.%s"), ext
);
452 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
455 // Generic file load dialog
456 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
457 const wxChar
*extension
,
458 const wxChar
*default_name
,
461 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
464 // Generic file save dialog
465 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
466 const wxChar
*extension
,
467 const wxChar
*default_name
,
470 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);