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"
39 #include "wx/msw/private.h"
45 #define wxDIALOG_DEFAULT_X 300
46 #define wxDIALOG_DEFAULT_Y 300
48 #if !USE_SHARED_LIBRARY
49 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
52 char *wxFileSelector(const char *title
,
53 const char *defaultDir
, const char *defaultFileName
,
54 const char *defaultExtension
, const char *filter
, int flags
,
55 wxWindow
*parent
, int x
, int y
)
57 // In the original implementation, defaultExtension is passed to the lpstrDefExt member
58 // of OPENFILENAME. This extension, if non-NULL, is appended to the filename if the user
59 // fails to type an extension.
60 // The new implementation (taken from wxFileSelectorEx) appends the extension automatically,
61 // by looking at the filter specification. In fact this should be better than the
62 // native Microsoft implementation because Windows only allows *one* default extension,
63 // whereas here we do the right thing depending on the filter the user has chosen.
65 // If there's a default extension specified but no filter, we create a suitable
69 if ( defaultExtension
&& !filter
)
70 filter2
= wxString("*.") + wxString(defaultExtension
) ;
74 wxString defaultDirString
;
76 defaultDirString
= defaultDir
;
78 defaultDirString
= "";
80 wxString defaultFilenameString
;
82 defaultFilenameString
= defaultFileName
;
84 defaultFilenameString
= "";
86 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
90 int filterFind
,filterIndex
=0;
92 for(ii
=0;ii
<filter2
.Length();ii
++)
94 if(filter2
[ii
] == '|')
98 for(;ii
<filter2
.Length();ii
++)
99 if(filter2
[ii
] == '|')
101 if(ii
-is
-1 > 0 && is
+1 < filter2
.Length())
102 if(filter2
.Mid(is
+1,ii
-is
-1) == defaultExtension
)
104 filterFind
=filterIndex
;
109 fileDialog
.SetFilterIndex(filterFind
);
112 if ( fileDialog
.ShowModal() == wxID_OK
)
114 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
122 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
142 char *wxFileSelectorEx(const char *title
,
143 const char *defaultDir
,
144 const char *defaultFileName
,
145 int* defaultFilterIndex
,
153 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
154 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
156 if ( fileDialog
.ShowModal() == wxID_OK
)
158 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
159 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
166 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
167 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
168 long style
, const wxPoint
& pos
)
171 m_dialogStyle
= style
;
174 m_fileName
= defaultFileName
;
176 m_wildCard
= wildCard
;
180 int wxFileDialog::ShowModal(void)
183 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
185 static char fileNameBuffer
[ MAXPATH
]; // the file-name
186 char titleBuffer
[ MAXFILE
+1+MAXEXT
]; // the file-name, without path
188 *fileNameBuffer
= '\0';
191 char* filterBuffer
= NULL
;
192 char* extension
= NULL
;
193 char* theFilter
= (char *)(const char *)m_wildCard
;
196 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
197 msw_flags
|= OFN_HIDEREADONLY
;
198 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
199 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
202 memset(&of
, 0, sizeof(OPENFILENAME
));
204 of
.lpstrCustomFilter
= NULL
; // system should not save custom filter
205 of
.nMaxCustFilter
= 0L;
207 of
.nFileOffset
= 0; // 0-based pointer to filname in lpstFile
208 of
.nFileExtension
= 0; // 0-based pointer to extension in lpstrFile
209 of
.lpstrDefExt
= NULL
; // no default extension
211 of
.lStructSize
= sizeof(OPENFILENAME
);
213 of
.lpstrTitle
= (char *)(const char *)m_message
;
216 of
.lpstrFileTitle
= titleBuffer
;
217 of
.nMaxFileTitle
= MAXFILE
+ 1 + MAXEXT
; // Windows 3.0 and 3.1
219 of
.lpstrInitialDir
= (const char *) m_dir
;
221 of
.Flags
= msw_flags
;
225 //=== Like Alejandro Sierra's wildcard modification >>===================
227 In wxFileSelector you can put, instead of a single wild_card,
228 pairs of strings separated by '|'.
229 The first string is a description, and the
230 second is the wild card. You can put any number of pairs.
232 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
234 If you put a single wild card, it works as before the modification.
236 //=======================================================================
238 if ( !theFilter
|| (strcmp(theFilter
, "") == 0)) theFilter
= "*.*";
240 int filterBufferLen
= 0;
242 if ( !strchr( theFilter
, '|' ) ) { // only one filter ==> default text:
243 char buffText
[] = "Files (%s)|%s";
244 filterBufferLen
= strlen( theFilter
)*2 + strlen( buffText
) -4;
245 filterBuffer
= new char[ filterBufferLen
+2 ];
247 if ( filterBuffer
) {
248 sprintf( filterBuffer
, buffText
, theFilter
, theFilter
);
251 else { // more then one filter
252 filterBufferLen
= strlen( theFilter
);
253 filterBuffer
= new char[ filterBufferLen
+2 ];
255 if ( filterBuffer
) {
256 strcpy( filterBuffer
, theFilter
);
260 if ( filterBuffer
) { // Substituting '|' with '\0'
261 for ( int i
= 0; i
< filterBufferLen
; i
++ ) {
262 if ( filterBuffer
[i
] == '|' ) { filterBuffer
[i
] = '\0'; }
266 filterBuffer
[filterBufferLen
+1] = '\0';
268 of
.lpstrFilter
= (LPSTR
)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) : (GetOpenFileName(&of
) != 0);
285 //=== Adding the correct extension >>=================================
287 m_filterIndex
= (int)of
.nFilterIndex
;
289 if ( of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != '.' )
290 { // user has typed an filename
291 // without an extension:
293 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
294 extension
= filterBuffer
;
296 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
297 extension
= extension
+ strlen( extension
) +1;
300 extension
= strrchr( extension
, '.' );
301 if ( extension
// != "blabla"
302 && !strrchr( extension
, '*' ) // != "blabla.*"
303 && !strrchr( extension
, '?' ) // != "blabla.?"
304 && extension
[1] // != "blabla."
305 && extension
[1] != ' ' ) // != "blabla. "
307 // now concat extension to the fileName:
308 m_fileName
= wxString(fileNameBuffer
) + wxString(extension
);
310 int len
= strlen( fileNameBuffer
);
311 strncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
312 fileNameBuffer
[ MAXPATH
-1 ] = '\0';
316 m_path
= fileNameBuffer
;
317 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
320 //=== Simulating the wxOVERWRITE_PROMPT >>============================
322 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) && ::wxFileExists( fileNameBuffer
) )
324 char questionText
[] = "Replace file\n%s?";
325 char* messageText
= new char[strlen(questionText
)+strlen(fileNameBuffer
)-1];
326 sprintf( messageText
, questionText
, fileNameBuffer
);
328 if ( messageText
&& ( wxMessageBox( (const char *)messageText
, m_message
, wxYES_NO
) != wxYES
) )
333 delete[] messageText
;
336 } // END: if ( success )
339 delete[] filterBuffer
;
341 return (success
? wxID_OK
: wxID_CANCEL
) ;
345 #define wxDIALOG_DEFAULT_X 300
346 #define wxDIALOG_DEFAULT_Y 300
348 // Generic file load/save dialog
349 // static inline char * // HP compiler complains
351 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
353 char *ext
= (char *)extension
;
358 str
= "Load %s file";
360 str
= "Save %s file";
361 sprintf(prompt
, wxGetTranslation(str
), what
);
363 if (*ext
== '.') ext
++;
365 sprintf(wild
, "*.%s", ext
);
367 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
370 // Generic file load dialog
372 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
374 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
378 // Generic file save dialog
380 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
382 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);