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"
18 #include "wx/msw/private.h"
26 #include "wx/msgdlg.h"
27 #include "wx/dialog.h"
28 #include "wx/filedlg.h"
32 // #include "wx/msw/private.h"
35 #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
43 #include "wx/tokenzr.h"
45 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
47 wxString
wxFileSelector(const wxChar
*title
,
48 const wxChar
*defaultDir
,
49 const wxChar
*defaultFileName
,
50 const wxChar
*defaultExtension
,
56 // In the original implementation, defaultExtension is passed to the
57 // lpstrDefExt member of OPENFILENAME. This extension, if non-NULL, is
58 // appended to the filename if the user fails to type an extension. The new
59 // implementation (taken from wxFileSelectorEx) appends the extension
60 // automatically, by looking at the filter specification. In fact this
61 // should be better than the native Microsoft implementation because
62 // Windows only allows *one* default extension, whereas here we do the
63 // right thing depending on the filter the user has chosen.
65 // If there's a default extension specified but no filter, we create a
69 if ( defaultExtension
&& !filter
)
70 filter2
= wxString(wxT("*.")) + defaultExtension
;
74 wxString defaultDirString
;
76 defaultDirString
= defaultDir
;
78 wxString defaultFilenameString
;
80 defaultFilenameString
= defaultFileName
;
82 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
83 defaultFilenameString
, filter2
,
84 flags
, wxPoint(x
, y
));
85 if( wxStrlen(defaultExtension
) != 0 )
90 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
92 if( filter2
.GetChar(i
) == wxT('|') )
94 // save the start index of the new filter
95 unsigned int is
= i
++;
98 // find the end of the filter
99 for( ; i
< filter2
.Len(); i
++ )
101 if(filter2
[i
] == wxT('|'))
105 if( i
-is
-1 > 0 && is
+1 < filter2
.Len() )
107 if( filter2
.Mid(is
+1,i
-is
-1).Contains(defaultExtension
) )
108 // if( filter2.Mid(is+1,i-is-1) == defaultExtension )
110 filterFind
= filterIndex
;
117 fileDialog
.SetFilterIndex(filterFind
);
120 if ( fileDialog
.ShowModal() == wxID_OK
)
122 wxStrcpy(wxBuffer
, (const wxChar
*)fileDialog
.GetPath());
126 return wxGetEmptyString();
130 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
150 wxString
wxFileSelectorEx(const wxChar
*title
,
151 const wxChar
*defaultDir
,
152 const wxChar
*defaultFileName
,
153 int* defaultFilterIndex
,
154 const wxChar
*filter
,
161 wxFileDialog
fileDialog(parent
, title
? title
: wxT(""), defaultDir
? defaultDir
: wxT(""),
162 defaultFileName
? defaultFileName
: wxT(""), filter
? filter
: wxT(""), flags
, wxPoint(x
, y
));
164 if ( fileDialog
.ShowModal() == wxID_OK
)
166 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
167 wxStrcpy(wxBuffer
, (const wxChar
*)fileDialog
.GetPath());
171 return wxGetEmptyString();
174 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
175 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
176 long style
, const wxPoint
& pos
)
179 m_dialogStyle
= style
;
180 if ( ( m_dialogStyle
& wxMULTIPLE
) && ( m_dialogStyle
& wxSAVE
) )
181 m_dialogStyle
&= ~wxMULTIPLE
;
184 m_fileName
= defaultFileName
;
186 m_wildCard
= wildCard
;
190 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
195 if ( m_dir
.Last() != _T('\\') )
198 size_t count
= m_fileNames
.GetCount();
199 for ( size_t n
= 0; n
< count
; n
++ )
201 paths
.Add(dir
+ m_fileNames
[n
]);
205 int wxFileDialog::ShowModal()
208 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
210 static wxChar fileNameBuffer
[ MAXPATH
]; // the file-name
211 wxChar titleBuffer
[ MAXFILE
+1+MAXEXT
]; // the file-name, without path
213 *fileNameBuffer
= wxT('\0');
214 *titleBuffer
= wxT('\0');
217 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
218 msw_flags
|= OFN_HIDEREADONLY
;
219 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
220 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
221 if (m_dialogStyle
& wxMULTIPLE
)
223 #if defined(OFN_EXPLORER)
225 #endif // OFN_EXPLORER
226 OFN_ALLOWMULTISELECT
;
229 memset(&of
, 0, sizeof(OPENFILENAME
));
231 of
.lpstrCustomFilter
= NULL
; // system should not save custom filter
232 of
.nMaxCustFilter
= 0L;
234 of
.nFileOffset
= 0; // 0-based pointer to filname in lpstFile
235 of
.nFileExtension
= 0; // 0-based pointer to extension in lpstrFile
236 of
.lpstrDefExt
= NULL
; // no default extension
238 of
.lStructSize
= sizeof(OPENFILENAME
);
240 of
.lpstrTitle
= WXSTRINGCAST m_message
;
243 of
.lpstrFileTitle
= titleBuffer
;
244 of
.nMaxFileTitle
= MAXFILE
+ 1 + MAXEXT
; // Windows 3.0 and 3.1
246 // Convert forward slashes to backslashes (file selector doesn't like
249 size_t len
= m_dir
.Length();
250 for (i
= 0; i
< len
; i
++)
251 if (m_dir
[i
] == wxT('/'))
252 m_dir
[i
] = wxT('\\');
254 of
.lpstrInitialDir
= m_dir
.c_str();
256 of
.Flags
= msw_flags
;
259 //=== Like Alejandro Sierra's wildcard modification >>===================
261 In wxFileSelector you can put, instead of a single wild_card,
262 pairs of strings separated by '|'.
263 The first string is a description, and the
264 second is the wild card. You can put any number of pairs.
266 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
268 If you put a single wild card, it works as before the modification.
270 //=======================================================================
273 if ( wxStrlen(m_wildCard
) == 0 )
274 theFilter
= wxString(wxT("*.*"));
276 theFilter
= m_wildCard
;
277 wxString filterBuffer
;
279 if ( !wxStrchr( theFilter
, wxT('|') ) ) { // only one filter ==> default text
280 filterBuffer
.Printf(_("Files (%s)|%s"),
281 theFilter
.c_str(), theFilter
.c_str());
283 else { // more then one filter
284 filterBuffer
= theFilter
;
288 filterBuffer
+= wxT("|");
290 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
291 if ( filterBuffer
.GetChar(i
) == wxT('|') ) {
292 filterBuffer
[i
] = wxT('\0');
296 of
.lpstrFilter
= (LPTSTR
)(const wxChar
*)filterBuffer
;
297 of
.nFilterIndex
= m_filterIndex
+ 1;
299 //=== Setting defaultFileName >>=========================================
301 wxStrncpy( fileNameBuffer
, (const wxChar
*)m_fileName
, MAXPATH
-1 );
302 fileNameBuffer
[ MAXPATH
-1 ] = wxT('\0');
304 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
305 of
.nMaxFile
= MAXPATH
;
307 //== Execute FileDialog >>=================================================
309 bool success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
310 : (GetOpenFileName(&of
) != 0);
316 if ( ( m_dialogStyle
& wxMULTIPLE
) &&
317 #if defined(OFN_EXPLORER)
318 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT('\0') ) )
320 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT(' ') ) )
321 #endif // OFN_EXPLORER
323 #if defined(OFN_EXPLORER)
324 m_dir
= fileNameBuffer
;
326 m_fileName
= &fileNameBuffer
[i
];
327 m_fileNames
.Add(m_fileName
);
328 i
+= m_fileName
.Len() + 1;
330 while (fileNameBuffer
[i
] != wxT('\0'))
332 m_fileNames
.Add(&fileNameBuffer
[i
]);
333 i
+= wxStrlen(&fileNameBuffer
[i
]) + 1;
336 wxStringTokenizer
toke(fileNameBuffer
, " \t\r\n");
337 m_dir
= toke
.GetNextToken();
338 m_fileName
= toke
.GetNextToken();
339 m_fileNames
.Add(m_fileName
);
341 while (toke
.HasMoreTokens())
342 m_fileNames
.Add(toke
.GetNextToken());
343 #endif // OFN_EXPLORER
346 if ( m_dir
.Last() != _T('\\') )
350 m_path
= dir
+ m_fileName
;
354 const wxChar
* extension
= NULL
;
356 //=== Adding the correct extension >>=================================
358 m_filterIndex
= (int)of
.nFilterIndex
- 1;
360 if ( !of
.nFileExtension
|| (of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != wxT('.')) )
361 { // user has typed an filename
362 // without an extension:
364 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
365 extension
= filterBuffer
;
367 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
368 extension
= extension
+ wxStrlen( extension
) +1;
371 extension
= wxStrrchr( extension
, wxT('.') );
372 if ( extension
// != "blabla"
373 && !wxStrrchr( extension
, wxT('*') ) // != "blabla.*"
374 && !wxStrrchr( extension
, wxT('?') ) // != "blabla.?"
375 && extension
[1] // != "blabla."
376 && extension
[1] != wxT(' ') ) // != "blabla. "
378 // now concat extension to the fileName:
379 m_fileName
= wxString(fileNameBuffer
) + extension
;
381 int len
= wxStrlen( fileNameBuffer
);
382 wxStrncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
383 fileNameBuffer
[ MAXPATH
-1 ] = wxT('\0');
387 m_path
= fileNameBuffer
;
388 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
389 m_fileNames
.Add(m_fileName
);
390 m_dir
= wxPathOnly(fileNameBuffer
);
394 //=== Simulating the wxOVERWRITE_PROMPT >>============================
396 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
397 ::wxFileExists( fileNameBuffer
) )
399 wxString messageText
;
400 messageText
.Printf(_("Replace file '%s'?"), fileNameBuffer
);
402 if ( wxMessageBox(messageText
, m_message
, wxYES_NO
) != wxYES
)
411 // common dialog failed - why?
413 DWORD dwErr
= CommDlgExtendedError();
416 // this msg is only for developers
417 wxLogError(wxT("Common dialog failed with error code %0lx."),
420 //else: it was just cancelled
424 return success
? wxID_OK
: wxID_CANCEL
;
428 // Generic file load/save dialog (for internal use only)
430 wxString
wxDefaultFileSelector(bool load
,
432 const wxChar
*extension
,
433 const wxChar
*default_name
,
438 if (load
) str
= _("Load %s file");
439 else str
= _("Save %s file");
440 prompt
.Printf(str
, what
);
442 const wxChar
*ext
= extension
;
443 if (*ext
== wxT('.'))
447 wild
.Printf(wxT("*.%s"), ext
);
449 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
452 // Generic file load dialog
453 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
454 const wxChar
*extension
,
455 const wxChar
*default_name
,
458 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
461 // Generic file save dialog
462 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
463 const wxChar
*extension
,
464 const wxChar
*default_name
,
467 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);