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"
35 #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
39 #include "wx/msw/private.h"
45 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
49 wxString
wxFileSelector(const wxChar
*title
,
50 const wxChar
*defaultDir
,
51 const wxChar
*defaultFileName
,
52 const wxChar
*defaultExtension
,
58 // In the original implementation, defaultExtension is passed to the
59 // lpstrDefExt member of OPENFILENAME. This extension, if non-NULL, is
60 // appended to the filename if the user fails to type an extension. The new
61 // implementation (taken from wxFileSelectorEx) appends the extension
62 // automatically, by looking at the filter specification. In fact this
63 // should be better than the native Microsoft implementation because
64 // Windows only allows *one* default extension, whereas here we do the
65 // right thing depending on the filter the user has chosen.
67 // If there's a default extension specified but no filter, we create a
71 if ( defaultExtension
&& !filter
)
72 filter2
= wxString(_T("*.")) + defaultExtension
;
76 wxString defaultDirString
;
78 defaultDirString
= defaultDir
;
80 wxString defaultFilenameString
;
82 defaultFilenameString
= defaultFileName
;
84 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
85 defaultFilenameString
, filter2
,
86 flags
, wxPoint(x
, y
));
87 if( wxStrlen(defaultExtension
) != 0 )
92 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
94 if( filter2
.GetChar(i
) == _T('|') )
96 // save the start index of the new filter
97 unsigned int is
= i
++;
100 // find the end of the filter
101 for( ; i
< filter2
.Len(); i
++ )
103 if(filter2
[i
] == _T('|'))
107 if( i
-is
-1 > 0 && is
+1 < filter2
.Len() )
109 if( filter2
.Mid(is
+1,i
-is
-1).Contains(defaultExtension
) )
110 // if( filter2.Mid(is+1,i-is-1) == defaultExtension )
112 filterFind
= filterIndex
;
119 fileDialog
.SetFilterIndex(filterFind
);
122 if ( fileDialog
.ShowModal() == wxID_OK
)
124 wxStrcpy(wxBuffer
, (const wxChar
*)fileDialog
.GetPath());
128 return wxGetEmptyString();
132 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
152 wxString
wxFileSelectorEx(const wxChar
*title
,
153 const wxChar
*defaultDir
,
154 const wxChar
*defaultFileName
,
155 int* defaultFilterIndex
,
156 const wxChar
*filter
,
163 wxFileDialog
fileDialog(parent
, title
? title
: _T(""), defaultDir
? defaultDir
: _T(""),
164 defaultFileName
? defaultFileName
: _T(""), filter
? filter
: _T(""), flags
, wxPoint(x
, y
));
166 if ( fileDialog
.ShowModal() == wxID_OK
)
168 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
169 wxStrcpy(wxBuffer
, (const wxChar
*)fileDialog
.GetPath());
173 return wxGetEmptyString();
176 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
177 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
178 long style
, const wxPoint
& pos
)
181 m_dialogStyle
= style
;
184 m_fileName
= defaultFileName
;
186 m_wildCard
= wildCard
;
190 int wxFileDialog::ShowModal(void)
193 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
195 static wxChar fileNameBuffer
[ MAXPATH
]; // the file-name
196 wxChar titleBuffer
[ MAXFILE
+1+MAXEXT
]; // the file-name, without path
198 *fileNameBuffer
= _T('\0');
199 *titleBuffer
= _T('\0');
202 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
203 msw_flags
|= OFN_HIDEREADONLY
;
204 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
205 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
208 memset(&of
, 0, sizeof(OPENFILENAME
));
210 of
.lpstrCustomFilter
= NULL
; // system should not save custom filter
211 of
.nMaxCustFilter
= 0L;
213 of
.nFileOffset
= 0; // 0-based pointer to filname in lpstFile
214 of
.nFileExtension
= 0; // 0-based pointer to extension in lpstrFile
215 of
.lpstrDefExt
= NULL
; // no default extension
217 of
.lStructSize
= sizeof(OPENFILENAME
);
219 of
.lpstrTitle
= WXSTRINGCAST m_message
;
222 of
.lpstrFileTitle
= titleBuffer
;
223 of
.nMaxFileTitle
= MAXFILE
+ 1 + MAXEXT
; // Windows 3.0 and 3.1
225 // Convert forward slashes to backslashes (file selector doesn't like
228 size_t len
= m_dir
.Length();
229 for (i
= 0; i
< len
; i
++)
230 if (m_dir
[i
] == _T('/'))
233 of
.lpstrInitialDir
= m_dir
.c_str();
235 of
.Flags
= msw_flags
;
238 //=== Like Alejandro Sierra's wildcard modification >>===================
240 In wxFileSelector you can put, instead of a single wild_card,
241 pairs of strings separated by '|'.
242 The first string is a description, and the
243 second is the wild card. You can put any number of pairs.
245 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
247 If you put a single wild card, it works as before the modification.
249 //=======================================================================
252 if ( wxStrlen(m_wildCard
) == 0 )
253 theFilter
= wxString(_T("*.*"));
255 theFilter
= m_wildCard
;
256 wxString filterBuffer
;
258 if ( !wxStrchr( theFilter
, _T('|') ) ) { // only one filter ==> default text
259 filterBuffer
.Printf(_("Files (%s)|%s"),
260 theFilter
.c_str(), theFilter
.c_str());
262 else { // more then one filter
263 filterBuffer
= theFilter
;
267 filterBuffer
+= _T("|");
269 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
270 if ( filterBuffer
.GetChar(i
) == _T('|') ) {
271 filterBuffer
[i
] = _T('\0');
275 of
.lpstrFilter
= (LPTSTR
)(const wxChar
*)filterBuffer
;
276 of
.nFilterIndex
= m_filterIndex
;
278 //=== Setting defaultFileName >>=========================================
280 wxStrncpy( fileNameBuffer
, (const wxChar
*)m_fileName
, MAXPATH
-1 );
281 fileNameBuffer
[ MAXPATH
-1 ] = _T('\0');
283 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
284 of
.nMaxFile
= MAXPATH
;
286 //== Execute FileDialog >>=================================================
288 bool success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
289 : (GetOpenFileName(&of
) != 0);
293 const wxChar
* extension
= NULL
;
295 //=== Adding the correct extension >>=================================
297 m_filterIndex
= (int)of
.nFilterIndex
;
299 if ( of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != _T('.') )
300 { // user has typed an filename
301 // without an extension:
303 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
304 extension
= filterBuffer
;
306 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
307 extension
= extension
+ wxStrlen( extension
) +1;
310 extension
= wxStrrchr( extension
, _T('.') );
311 if ( extension
// != "blabla"
312 && !wxStrrchr( extension
, _T('*') ) // != "blabla.*"
313 && !wxStrrchr( extension
, _T('?') ) // != "blabla.?"
314 && extension
[1] // != "blabla."
315 && extension
[1] != _T(' ') ) // != "blabla. "
317 // now concat extension to the fileName:
318 m_fileName
= wxString(fileNameBuffer
) + extension
;
320 int len
= wxStrlen( fileNameBuffer
);
321 wxStrncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
322 fileNameBuffer
[ MAXPATH
-1 ] = _T('\0');
326 m_path
= fileNameBuffer
;
327 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
330 //=== Simulating the wxOVERWRITE_PROMPT >>============================
332 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
333 ::wxFileExists( fileNameBuffer
) )
335 wxString messageText
;
336 messageText
.Printf(_("Replace file '%s'?"), fileNameBuffer
);
338 if ( wxMessageBox(messageText
, m_message
, wxYES_NO
) != wxYES
)
344 } // END: if ( success )
346 return (success
? wxID_OK
: wxID_CANCEL
) ;
350 // Generic file load/save dialog (for internal use only)
352 wxString
wxDefaultFileSelector(bool load
,
354 const wxChar
*extension
,
355 const wxChar
*default_name
,
360 if (load
) str
= _("Load %s file");
361 else str
= _("Save %s file");
362 prompt
.Printf(str
, what
);
364 const wxChar
*ext
= extension
;
369 wild
.Printf(_T("*.%s"), ext
);
371 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
374 // Generic file load dialog
375 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
376 const wxChar
*extension
,
377 const wxChar
*default_name
,
380 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
383 // Generic file save dialog
384 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
385 const wxChar
*extension
,
386 const wxChar
*default_name
,
389 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);