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 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 // Convert forward slashes to backslashes (file selector doesn't like
228 size_t len
= m_dir
.Length();
229 for (i
= 0; i
< len
; i
++)
233 of
.lpstrInitialDir
= (const char *) m_dir
;
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 ( Strlen(m_wildCard
) == 0 )
253 theFilter
= wxString("*.*");
255 theFilter
= m_wildCard
;
256 wxString filterBuffer
;
258 if ( !strchr( theFilter
, '|' ) ) { // 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
;
269 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
270 if ( filterBuffer
.GetChar(i
) == '|' ) {
271 filterBuffer
[i
] = '\0';
275 of
.lpstrFilter
= (LPSTR
)(const char *)filterBuffer
;
276 of
.nFilterIndex
= m_filterIndex
;
278 //=== Setting defaultFileName >>=========================================
280 strncpy( fileNameBuffer
, (const char *)m_fileName
, MAXPATH
-1 );
281 fileNameBuffer
[ MAXPATH
-1 ] = '\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 char* extension
= NULL
;
295 //=== Adding the correct extension >>=================================
297 m_filterIndex
= (int)of
.nFilterIndex
;
299 if ( of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != '.' )
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
+ strlen( extension
) +1;
310 extension
= strrchr( extension
, '.' );
311 if ( extension
// != "blabla"
312 && !strrchr( extension
, '*' ) // != "blabla.*"
313 && !strrchr( extension
, '?' ) // != "blabla.?"
314 && extension
[1] // != "blabla."
315 && extension
[1] != ' ' ) // != "blabla. "
317 // now concat extension to the fileName:
318 m_fileName
= wxString(fileNameBuffer
) + extension
;
320 int len
= strlen( fileNameBuffer
);
321 strncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
322 fileNameBuffer
[ MAXPATH
-1 ] = '\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 char *extension
,
355 const char *default_name
,
359 wxString str
= load
? _("Load %s file") : _("Save %s file");
360 prompt
.Printf(str
, what
);
362 const char *ext
= extension
;
367 wild
.Printf("*.%s", ext
);
369 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
372 // Generic file load dialog
373 WXDLLEXPORT wxString
wxLoadFileSelector(const char *what
,
374 const char *extension
,
375 const char *default_name
,
378 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
381 // Generic file save dialog
382 WXDLLEXPORT wxString
wxSaveFileSelector(const char *what
,
383 const char *extension
,
384 const char *default_name
,
387 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);