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 char *wxFileSelector(const char *title
,
50 const char *defaultDir
, const char *defaultFileName
,
51 const char *defaultExtension
, const char *filter
, int flags
,
52 wxWindow
*parent
, int x
, int y
)
54 // In the original implementation, defaultExtension is passed to the
55 // lpstrDefExt member of OPENFILENAME. This extension, if non-NULL, is
56 // appended to the filename if the user fails to type an extension. The new
57 // implementation (taken from wxFileSelectorEx) appends the extension
58 // automatically, by looking at the filter specification. In fact this
59 // should be better than the native Microsoft implementation because
60 // Windows only allows *one* default extension, whereas here we do the
61 // right thing depending on the filter the user has chosen.
63 // If there's a default extension specified but no filter, we create a
67 if ( defaultExtension
&& !filter
)
68 filter2
= wxString("*.") + defaultExtension
;
72 wxString defaultDirString
;
74 defaultDirString
= defaultDir
;
76 wxString defaultFilenameString
;
78 defaultFilenameString
= defaultFileName
;
80 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
81 defaultFilenameString
, filter2
,
82 flags
, wxPoint(x
, y
));
83 if( Strlen(defaultExtension
) != 0 )
88 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
90 if( filter2
.GetChar(i
) == '|' )
92 // save the start index of the new filter
93 unsigned int is
= i
++;
96 // find the end of the filter
97 for( ; i
< filter2
.Len(); i
++ )
103 if( i
-is
-1 > 0 && is
+1 < filter2
.Len() )
105 if( filter2
.Mid(is
+1,i
-is
-1).Contains(defaultExtension
) )
107 filterFind
= filterIndex
;
114 fileDialog
.SetFilterIndex(filterFind
);
117 if ( fileDialog
.ShowModal() == wxID_OK
)
119 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
127 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
147 char *wxFileSelectorEx(const char *title
,
148 const char *defaultDir
,
149 const char *defaultFileName
,
150 int* defaultFilterIndex
,
158 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
159 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
161 if ( fileDialog
.ShowModal() == wxID_OK
)
163 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
164 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
171 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
172 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
173 long style
, const wxPoint
& pos
)
176 m_dialogStyle
= style
;
179 m_fileName
= defaultFileName
;
181 m_wildCard
= wildCard
;
185 int wxFileDialog::ShowModal(void)
188 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
190 static char fileNameBuffer
[ MAXPATH
]; // the file-name
191 char titleBuffer
[ MAXFILE
+1+MAXEXT
]; // the file-name, without path
193 *fileNameBuffer
= '\0';
197 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
198 msw_flags
|= OFN_HIDEREADONLY
;
199 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
200 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
203 memset(&of
, 0, sizeof(OPENFILENAME
));
205 of
.lpstrCustomFilter
= NULL
; // system should not save custom filter
206 of
.nMaxCustFilter
= 0L;
208 of
.nFileOffset
= 0; // 0-based pointer to filname in lpstFile
209 of
.nFileExtension
= 0; // 0-based pointer to extension in lpstrFile
210 of
.lpstrDefExt
= NULL
; // no default extension
212 of
.lStructSize
= sizeof(OPENFILENAME
);
214 of
.lpstrTitle
= (char *)(const char *)m_message
;
217 of
.lpstrFileTitle
= titleBuffer
;
218 of
.nMaxFileTitle
= MAXFILE
+ 1 + MAXEXT
; // Windows 3.0 and 3.1
220 of
.lpstrInitialDir
= (const char *) m_dir
;
222 of
.Flags
= msw_flags
;
226 //=== Like Alejandro Sierra's wildcard modification >>===================
228 In wxFileSelector you can put, instead of a single wild_card,
229 pairs of strings separated by '|'.
230 The first string is a description, and the
231 second is the wild card. You can put any number of pairs.
233 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
235 If you put a single wild card, it works as before the modification.
237 //=======================================================================
239 wxString theFilter
= ( Strlen(m_wildCard
) == 0 ) ? wxString("*.*") : m_wildCard
;
240 wxString filterBuffer
;
242 if ( !strchr( theFilter
, '|' ) ) { // only one filter ==> default text
243 filterBuffer
.Printf(_("Files (%s)|%s"),
244 theFilter
.c_str(), theFilter
.c_str());
246 else { // more then one filter
247 filterBuffer
= theFilter
;
249 for ( unsigned int i
= 0; i
< filterBuffer
.Len(); i
++ ) {
250 if ( filterBuffer
.GetChar(i
) == '|' ) {
251 filterBuffer
[i
] = '\0';
256 of
.lpstrFilter
= (LPSTR
)(const char *)filterBuffer
;
257 of
.nFilterIndex
= m_filterIndex
;
259 //=== Setting defaultFileName >>=========================================
261 strncpy( fileNameBuffer
, (const char *)m_fileName
, MAXPATH
-1 );
262 fileNameBuffer
[ MAXPATH
-1 ] = '\0';
264 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
265 of
.nMaxFile
= MAXPATH
;
267 //== Execute FileDialog >>=================================================
269 bool success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
270 : (GetOpenFileName(&of
) != 0);
274 const char* extension
= NULL
;
276 //=== Adding the correct extension >>=================================
278 m_filterIndex
= (int)of
.nFilterIndex
;
280 if ( of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != '.' )
281 { // user has typed an filename
282 // without an extension:
284 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
285 extension
= filterBuffer
;
287 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
288 extension
= extension
+ strlen( extension
) +1;
291 extension
= strrchr( extension
, '.' );
292 if ( extension
// != "blabla"
293 && !strrchr( extension
, '*' ) // != "blabla.*"
294 && !strrchr( extension
, '?' ) // != "blabla.?"
295 && extension
[1] // != "blabla."
296 && extension
[1] != ' ' ) // != "blabla. "
298 // now concat extension to the fileName:
299 m_fileName
= wxString(fileNameBuffer
) + extension
;
301 int len
= strlen( fileNameBuffer
);
302 strncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
303 fileNameBuffer
[ MAXPATH
-1 ] = '\0';
307 m_path
= fileNameBuffer
;
308 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
311 //=== Simulating the wxOVERWRITE_PROMPT >>============================
313 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
314 ::wxFileExists( fileNameBuffer
) )
316 wxString messageText
;
317 messageText
.Printf(_("Replace file '%s'?"), fileNameBuffer
);
319 if ( wxMessageBox(messageText
, m_message
, wxYES_NO
) != wxYES
)
325 } // END: if ( success )
327 return (success
? wxID_OK
: wxID_CANCEL
) ;
331 // Generic file load/save dialog
333 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
339 str
= _("Load %s file");
341 str
= _("Save %s file");
342 prompt
.Printf(str
, what
);
344 const char *ext
= extension
;
349 wild
.Printf("*.%s", ext
);
351 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
354 // Generic file load dialog
356 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
358 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
362 // Generic file save dialog
364 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
366 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);