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
) )
110 // if( filter2.Mid(is+1,i-is-1) == defaultExtension )
112 filterFind
= filterIndex
;
119 fileDialog
.SetFilterIndex(filterFind
);
122 if ( fileDialog
.ShowModal() == wxID_OK
)
124 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
128 return wxGetEmptyString();
132 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
152 wxString
wxFileSelectorEx(const char *title
,
153 const char *defaultDir
,
154 const char *defaultFileName
,
155 int* defaultFilterIndex
,
163 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
164 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
166 if ( fileDialog
.ShowModal() == wxID_OK
)
168 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
169 strcpy(wxBuffer
, (const char *)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 char fileNameBuffer
[ MAXPATH
]; // the file-name
196 char titleBuffer
[ MAXFILE
+1+MAXEXT
]; // the file-name, without path
198 *fileNameBuffer
= '\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
= (char *)(const char *)m_message
;
222 of
.lpstrFileTitle
= titleBuffer
;
223 of
.nMaxFileTitle
= MAXFILE
+ 1 + MAXEXT
; // Windows 3.0 and 3.1
225 of
.lpstrInitialDir
= (const char *) m_dir
;
227 of
.Flags
= msw_flags
;
231 //=== Like Alejandro Sierra's wildcard modification >>===================
233 In wxFileSelector you can put, instead of a single wild_card,
234 pairs of strings separated by '|'.
235 The first string is a description, and the
236 second is the wild card. You can put any number of pairs.
238 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
240 If you put a single wild card, it works as before the modification.
242 //=======================================================================
245 if ( Strlen(m_wildCard
) == 0 )
246 theFilter
= wxString("*.*");
248 theFilter
= m_wildCard
;
249 wxString filterBuffer
;
251 if ( !strchr( theFilter
, '|' ) ) { // only one filter ==> default text
252 filterBuffer
.Printf(_("Files (%s)|%s"),
253 theFilter
.c_str(), theFilter
.c_str());
255 else { // more then one filter
256 filterBuffer
= theFilter
;
262 for ( unsigned int i
= 0; i
< filterBuffer
.Len(); i
++ ) {
263 if ( filterBuffer
.GetChar(i
) == '|' ) {
264 filterBuffer
[i
] = '\0';
268 of
.lpstrFilter
= (LPSTR
)(const char *)filterBuffer
;
269 of
.nFilterIndex
= m_filterIndex
;
271 //=== Setting defaultFileName >>=========================================
273 strncpy( fileNameBuffer
, (const char *)m_fileName
, MAXPATH
-1 );
274 fileNameBuffer
[ MAXPATH
-1 ] = '\0';
276 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
277 of
.nMaxFile
= MAXPATH
;
279 //== Execute FileDialog >>=================================================
281 bool success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
282 : (GetOpenFileName(&of
) != 0);
286 const char* extension
= NULL
;
288 //=== Adding the correct extension >>=================================
290 m_filterIndex
= (int)of
.nFilterIndex
;
292 if ( of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != '.' )
293 { // user has typed an filename
294 // without an extension:
296 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
297 extension
= filterBuffer
;
299 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
300 extension
= extension
+ strlen( extension
) +1;
303 extension
= strrchr( extension
, '.' );
304 if ( extension
// != "blabla"
305 && !strrchr( extension
, '*' ) // != "blabla.*"
306 && !strrchr( extension
, '?' ) // != "blabla.?"
307 && extension
[1] // != "blabla."
308 && extension
[1] != ' ' ) // != "blabla. "
310 // now concat extension to the fileName:
311 m_fileName
= wxString(fileNameBuffer
) + extension
;
313 int len
= strlen( fileNameBuffer
);
314 strncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
315 fileNameBuffer
[ MAXPATH
-1 ] = '\0';
319 m_path
= fileNameBuffer
;
320 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
323 //=== Simulating the wxOVERWRITE_PROMPT >>============================
325 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
326 ::wxFileExists( fileNameBuffer
) )
328 wxString messageText
;
329 messageText
.Printf(_("Replace file '%s'?"), fileNameBuffer
);
331 if ( wxMessageBox(messageText
, m_message
, wxYES_NO
) != wxYES
)
337 } // END: if ( success )
339 return (success
? wxID_OK
: wxID_CANCEL
) ;
343 // Generic file load/save dialog (for internal use only)
345 wxString
wxDefaultFileSelector(bool load
,
347 const char *extension
,
348 const char *default_name
,
352 wxString str
= load
? _("Load %s file") : _("Save %s file");
353 prompt
.Printf(str
, what
);
355 const char *ext
= extension
;
360 wild
.Printf("*.%s", ext
);
362 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
365 // Generic file load dialog
366 WXDLLEXPORT wxString
wxLoadFileSelector(const char *what
,
367 const char *extension
,
368 const char *default_name
,
371 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
374 // Generic file save dialog
375 WXDLLEXPORT wxString
wxSaveFileSelector(const char *what
,
376 const char *extension
,
377 const char *default_name
,
380 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);