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__)
39 #include "wx/msw/private.h"
45 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
49 wxString
wxFileSelector(const char *title
,
50 const char *defaultDir
,
51 const char *defaultFileName
,
52 const char *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("*.") + 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( Strlen(defaultExtension
) != 0 )
92 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
94 if( filter2
.GetChar(i
) == '|' )
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
] == '|')
107 if( i
-is
-1 > 0 && is
+1 < filter2
.Len() )
109 if( filter2
.Mid(is
+1,i
-is
-1).Contains(defaultExtension
) )
111 filterFind
= filterIndex
;
118 fileDialog
.SetFilterIndex(filterFind
);
121 if ( fileDialog
.ShowModal() == wxID_OK
)
123 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
127 return wxGetEmptyString();
131 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
151 wxString
wxFileSelectorEx(const char *title
,
152 const char *defaultDir
,
153 const char *defaultFileName
,
154 int* defaultFilterIndex
,
162 wxFileDialog
fileDialog(parent
, title ? title
: "", defaultDir ? defaultDir
: "",
163 defaultFileName ? defaultFileName
: "", filter ? filter
: "", flags
, wxPoint(x
, y
));
165 if ( fileDialog
.ShowModal() == wxID_OK
)
167 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
168 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
172 return wxGetEmptyString();
175 wxFileDialog
::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
176 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
177 long style
, const wxPoint
& pos
)
180 m_dialogStyle
= style
;
183 m_fileName
= defaultFileName
;
185 m_wildCard
= wildCard
;
189 int wxFileDialog
::ShowModal(void)
192 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
194 static char fileNameBuffer
[ MAXPATH
]; // the file-name
195 char titleBuffer
[ MAXFILE
+1+MAXEXT
]; // the file-name, without path
197 *fileNameBuffer
= '\0';
201 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
202 msw_flags
|= OFN_HIDEREADONLY
;
203 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
204 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
207 memset(&of
, 0, sizeof(OPENFILENAME
));
209 of
.lpstrCustomFilter
= NULL
; // system should not save custom filter
210 of
.nMaxCustFilter
= 0L;
212 of
.nFileOffset
= 0; // 0-based pointer to filname in lpstFile
213 of
.nFileExtension
= 0; // 0-based pointer to extension in lpstrFile
214 of
.lpstrDefExt
= NULL
; // no default extension
216 of
.lStructSize
= sizeof(OPENFILENAME
);
218 of
.lpstrTitle
= (char *)(const char *)m_message
;
221 of
.lpstrFileTitle
= titleBuffer
;
222 of
.nMaxFileTitle
= MAXFILE
+ 1 + MAXEXT
; // Windows 3.0 and 3.1
224 of
.lpstrInitialDir
= (const char *) m_dir
;
226 of
.Flags
= msw_flags
;
230 //=== Like Alejandro Sierra's wildcard modification >>===================
232 In wxFileSelector you can put, instead of a single wild_card,
233 pairs of strings separated by '|'.
234 The first string is a description, and the
235 second is the wild card. You can put any number of pairs.
237 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
239 If you put a single wild card, it works as before the modification.
241 //=======================================================================
243 wxString theFilter
= ( Strlen(m_wildCard
) == 0 ) ?
wxString("*.*") : m_wildCard
;
244 wxString filterBuffer
;
246 if ( !strchr( theFilter
, '|' ) ) { // only one filter ==> default text
247 filterBuffer
.Printf(_("Files (%s)|%s"),
248 theFilter
.c_str(), theFilter
.c_str());
250 else { // more then one filter
251 filterBuffer
= theFilter
;
253 for ( unsigned int i
= 0; i
< filterBuffer
.Len(); i
++ ) {
254 if ( filterBuffer
.GetChar(i
) == '|' ) {
255 filterBuffer
[i
] = '\0';
260 of
.lpstrFilter
= (LPSTR
)(const char *)filterBuffer
;
261 of
.nFilterIndex
= m_filterIndex
;
263 //=== Setting defaultFileName >>=========================================
265 strncpy( fileNameBuffer
, (const char *)m_fileName
, MAXPATH
-1 );
266 fileNameBuffer
[ MAXPATH
-1 ] = '\0';
268 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
269 of
.nMaxFile
= MAXPATH
;
271 //== Execute FileDialog >>=================================================
273 bool success
= (m_dialogStyle
& wxSAVE
) ?
(GetSaveFileName(&of
) != 0)
274 : (GetOpenFileName(&of
) != 0);
278 const char* extension
= NULL
;
280 //=== Adding the correct extension >>=================================
282 m_filterIndex
= (int)of
.nFilterIndex
;
284 if ( of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != '.' )
285 { // user has typed an filename
286 // without an extension:
288 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
289 extension
= filterBuffer
;
291 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
292 extension
= extension
+ strlen( extension
) +1;
295 extension
= strrchr( extension
, '.' );
296 if ( extension
// != "blabla"
297 && !strrchr( extension
, '*' ) // != "blabla.*"
298 && !strrchr( extension
, '?' ) // != "blabla.?"
299 && extension
[1] // != "blabla."
300 && extension
[1] != ' ' ) // != "blabla. "
302 // now concat extension to the fileName:
303 m_fileName
= wxString(fileNameBuffer
) + extension
;
305 int len
= strlen( fileNameBuffer
);
306 strncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
307 fileNameBuffer
[ MAXPATH
-1 ] = '\0';
311 m_path
= fileNameBuffer
;
312 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
315 //=== Simulating the wxOVERWRITE_PROMPT >>============================
317 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
318 ::wxFileExists( fileNameBuffer
) )
320 wxString messageText
;
321 messageText
.Printf(_("Replace file '%s'?"), fileNameBuffer
);
323 if ( wxMessageBox(messageText
, m_message
, wxYES_NO
) != wxYES
)
329 } // END: if ( success )
331 return (success ? wxID_OK
: wxID_CANCEL
) ;
335 // Generic file load/save dialog (for internal use only)
337 wxString
wxDefaultFileSelector(bool load
,
339 const char *extension
,
340 const char *default_name
,
344 wxString str
= load ?
_("Load %s file") : _("Save %s file");
345 prompt
.Printf(str
, what
);
347 const char *ext
= extension
;
352 wild
.Printf("*.%s", ext
);
354 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
357 // Generic file load dialog
358 WXDLLEXPORT wxString
wxLoadFileSelector(const char *what
,
359 const char *extension
,
360 const char *default_name
,
363 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
366 // Generic file save dialog
367 WXDLLEXPORT wxString
wxSaveFileSelector(const char *what
,
368 const char *extension
,
369 const char *default_name
,
372 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);