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(_T("*.")) + 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
) == _T('|') )
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
] == _T('|'))
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
: _T(""), defaultDir
? defaultDir
: _T(""),
165 defaultFileName
? defaultFileName
: _T(""), filter
? filter
: _T(""), 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
;
185 m_fileName
= defaultFileName
;
187 m_wildCard
= wildCard
;
191 int wxFileDialog::ShowModal(void)
194 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
196 static wxChar fileNameBuffer
[ MAXPATH
]; // the file-name
197 wxChar titleBuffer
[ MAXFILE
+1+MAXEXT
]; // the file-name, without path
199 *fileNameBuffer
= _T('\0');
200 *titleBuffer
= _T('\0');
203 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
204 msw_flags
|= OFN_HIDEREADONLY
;
205 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
206 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
209 memset(&of
, 0, sizeof(OPENFILENAME
));
211 of
.lpstrCustomFilter
= NULL
; // system should not save custom filter
212 of
.nMaxCustFilter
= 0L;
214 of
.nFileOffset
= 0; // 0-based pointer to filname in lpstFile
215 of
.nFileExtension
= 0; // 0-based pointer to extension in lpstrFile
216 of
.lpstrDefExt
= NULL
; // no default extension
218 of
.lStructSize
= sizeof(OPENFILENAME
);
220 of
.lpstrTitle
= WXSTRINGCAST m_message
;
223 of
.lpstrFileTitle
= titleBuffer
;
224 of
.nMaxFileTitle
= MAXFILE
+ 1 + MAXEXT
; // Windows 3.0 and 3.1
226 // Convert forward slashes to backslashes (file selector doesn't like
229 size_t len
= m_dir
.Length();
230 for (i
= 0; i
< len
; i
++)
231 if (m_dir
[i
] == _T('/'))
234 of
.lpstrInitialDir
= m_dir
.c_str();
236 of
.Flags
= msw_flags
;
239 //=== Like Alejandro Sierra's wildcard modification >>===================
241 In wxFileSelector you can put, instead of a single wild_card,
242 pairs of strings separated by '|'.
243 The first string is a description, and the
244 second is the wild card. You can put any number of pairs.
246 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
248 If you put a single wild card, it works as before the modification.
250 //=======================================================================
253 if ( wxStrlen(m_wildCard
) == 0 )
254 theFilter
= wxString(_T("*.*"));
256 theFilter
= m_wildCard
;
257 wxString filterBuffer
;
259 if ( !wxStrchr( theFilter
, _T('|') ) ) { // only one filter ==> default text
260 filterBuffer
.Printf(_("Files (%s)|%s"),
261 theFilter
.c_str(), theFilter
.c_str());
263 else { // more then one filter
264 filterBuffer
= theFilter
;
268 filterBuffer
+= _T("|");
270 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
271 if ( filterBuffer
.GetChar(i
) == _T('|') ) {
272 filterBuffer
[i
] = _T('\0');
276 of
.lpstrFilter
= (LPTSTR
)(const wxChar
*)filterBuffer
;
277 of
.nFilterIndex
= m_filterIndex
;
279 //=== Setting defaultFileName >>=========================================
281 wxStrncpy( fileNameBuffer
, (const wxChar
*)m_fileName
, MAXPATH
-1 );
282 fileNameBuffer
[ MAXPATH
-1 ] = _T('\0');
284 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
285 of
.nMaxFile
= MAXPATH
;
287 //== Execute FileDialog >>=================================================
289 bool success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
290 : (GetOpenFileName(&of
) != 0);
294 const wxChar
* extension
= NULL
;
296 //=== Adding the correct extension >>=================================
298 m_filterIndex
= (int)of
.nFilterIndex
;
300 if ( of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != _T('.') )
301 { // user has typed an filename
302 // without an extension:
304 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
305 extension
= filterBuffer
;
307 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
308 extension
= extension
+ wxStrlen( extension
) +1;
311 extension
= wxStrrchr( extension
, _T('.') );
312 if ( extension
// != "blabla"
313 && !wxStrrchr( extension
, _T('*') ) // != "blabla.*"
314 && !wxStrrchr( extension
, _T('?') ) // != "blabla.?"
315 && extension
[1] // != "blabla."
316 && extension
[1] != _T(' ') ) // != "blabla. "
318 // now concat extension to the fileName:
319 m_fileName
= wxString(fileNameBuffer
) + extension
;
321 int len
= wxStrlen( fileNameBuffer
);
322 wxStrncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
323 fileNameBuffer
[ MAXPATH
-1 ] = _T('\0');
327 m_path
= fileNameBuffer
;
328 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
329 m_dir
= wxPathOnly(fileNameBuffer
);
332 //=== Simulating the wxOVERWRITE_PROMPT >>============================
334 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
335 ::wxFileExists( fileNameBuffer
) )
337 wxString messageText
;
338 messageText
.Printf(_("Replace file '%s'?"), fileNameBuffer
);
340 if ( wxMessageBox(messageText
, m_message
, wxYES_NO
) != wxYES
)
349 // common dialog failed - why?
351 DWORD dwErr
= CommDlgExtendedError();
354 // this msg is only for developers
355 wxLogError(_T("Common dialog failed with error code %0lx."),
358 //else: it was just cancelled
362 return success
? wxID_OK
: wxID_CANCEL
;
366 // Generic file load/save dialog (for internal use only)
368 wxString
wxDefaultFileSelector(bool load
,
370 const wxChar
*extension
,
371 const wxChar
*default_name
,
376 if (load
) str
= _("Load %s file");
377 else str
= _("Save %s file");
378 prompt
.Printf(str
, what
);
380 const wxChar
*ext
= extension
;
385 wild
.Printf(_T("*.%s"), ext
);
387 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
390 // Generic file load dialog
391 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
392 const wxChar
*extension
,
393 const wxChar
*default_name
,
396 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
399 // Generic file save dialog
400 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
401 const wxChar
*extension
,
402 const wxChar
*default_name
,
405 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);