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/dialog.h"
28 #include "wx/filedlg.h"
37 #include "wx/msw/private.h"
43 #define wxDIALOG_DEFAULT_X 300
44 #define wxDIALOG_DEFAULT_Y 300
46 #if !USE_SHARED_LIBRARY
47 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
50 char *wxFileSelector(const char *title
,
51 const char *defaultDir
, const char *defaultFileName
,
52 const char *defaultExtension
, const char *filter
, int flags
,
53 wxWindow
*parent
, int x
, int y
)
55 // In the original implementation, defaultExtension is passed to the lpstrDefExt member
56 // of OPENFILENAME. This extension, if non-NULL, is appended to the filename if the user
57 // fails to type an extension.
58 // The new implementation (taken from wxFileSelectorEx) appends the extension automatically,
59 // by looking at the filter specification. In fact this should be better than the
60 // native Microsoft implementation because Windows only allows *one* default extension,
61 // whereas here we do the right thing depending on the filter the user has chosen.
63 // If there's a default extension specified but no filter, we create a suitable
67 if ( defaultExtension
&& !filter
)
68 filter2
= wxString("*.") + wxString(defaultExtension
) ;
72 wxString defaultDirString
;
74 defaultDirString
= defaultDir
;
76 defaultDirString
= "";
78 wxString defaultFilenameString
;
80 defaultFilenameString
= defaultFileName
;
82 defaultFilenameString
= "";
84 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
86 if ( fileDialog
.ShowModal() == wxID_OK
)
88 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
96 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
116 char *wxFileSelectorEx( const char *title
,
117 const char *defaultDir
,
118 const char *defaultFileName
,
119 int* defaultFilterIndex
,
127 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
128 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
130 if ( fileDialog
.ShowModal() == wxID_OK
)
132 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
133 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
140 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
141 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
142 long style
, const wxPoint
& pos
)
145 m_dialogStyle
= style
;
148 m_fileName
= defaultFileName
;
150 m_wildCard
= wildCard
;
154 int wxFileDialog::ShowModal(void)
157 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
159 static char fileNameBuffer
[ MAXPATH
]; // the file-name
160 char titleBuffer
[ MAXFILE
+1+MAXEXT
]; // the file-name, without path
162 *fileNameBuffer
= '\0';
165 char* filterBuffer
= NULL
;
166 char* extension
= NULL
;
167 char* theFilter
= (char *)(const char *)m_wildCard
;
170 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) ) { msw_flags
|= OFN_HIDEREADONLY
; }
174 memset(&of
, 0, sizeof(OPENFILENAME
));
176 of
.lpstrCustomFilter
= NULL
; // system should not save custom filter
177 of
.nMaxCustFilter
= 0L;
179 of
.nFileOffset
= 0; // 0-based pointer to filname in lpstFile
180 of
.nFileExtension
= 0; // 0-based pointer to extension in lpstrFile
181 of
.lpstrDefExt
= NULL
; // no default extension
183 of
.lStructSize
= sizeof(OPENFILENAME
);
185 of
.lpstrTitle
= (char *)(const char *)m_message
;
188 of
.lpstrFileTitle
= titleBuffer
;
189 of
.nMaxFileTitle
= MAXFILE
+ 1 + MAXEXT
; // Windows 3.0 and 3.1
191 of
.lpstrInitialDir
= (const char *) m_dir
;
193 of
.Flags
= msw_flags
;
197 //=== Like Alejandro Sierra's wildcard modification >>===================
199 In wxFileSelector you can put, instead of a single wild_card,
200 pairs of strings separated by '|'.
201 The first string is a description, and the
202 second is the wild card. You can put any number of pairs.
204 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
206 If you put a single wild card, it works as before the modification.
208 //=======================================================================
210 if ( !theFilter
|| (strcmp(theFilter
, "") == 0)) theFilter
= "*.*";
212 int filterBufferLen
= 0;
214 if ( !strchr( theFilter
, '|' ) ) { // only one filter ==> default text:
215 char buffText
[] = "Files (%s)|%s";
216 filterBufferLen
= strlen( theFilter
)*2 + strlen( buffText
) -4;
217 filterBuffer
= new char[ filterBufferLen
+2 ];
219 if ( filterBuffer
) {
220 sprintf( filterBuffer
, buffText
, theFilter
, theFilter
);
223 else { // more then one filter
224 filterBufferLen
= strlen( theFilter
);
225 filterBuffer
= new char[ filterBufferLen
+2 ];
227 if ( filterBuffer
) {
228 strcpy( filterBuffer
, theFilter
);
232 if ( filterBuffer
) { // Substituting '|' with '\0'
233 for ( int i
= 0; i
< filterBufferLen
; i
++ ) {
234 if ( filterBuffer
[i
] == '|' ) { filterBuffer
[i
] = '\0'; }
238 filterBuffer
[filterBufferLen
+1] = '\0';
240 of
.lpstrFilter
= (LPSTR
)filterBuffer
;
241 of
.nFilterIndex
= m_filterIndex
;
243 //=== Setting defaultFileName >>=========================================
245 strncpy( fileNameBuffer
, (const char *)m_fileName
, MAXPATH
-1 );
246 fileNameBuffer
[ MAXPATH
-1 ] = '\0';
248 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
249 of
.nMaxFile
= MAXPATH
;
251 //== Execute FileDialog >>=================================================
253 bool success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0) : (GetOpenFileName(&of
) != 0);
257 //=== Adding the correct extension >>=================================
259 m_filterIndex
= (int)of
.nFilterIndex
;
261 if ( of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != '.' )
262 { // user has typed an filename
263 // without an extension:
265 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
266 extension
= filterBuffer
;
268 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
269 extension
= extension
+ strlen( extension
) +1;
272 if ( (extension
= strrchr( extension
, '.' )) // != "blabla"
273 && !strrchr( extension
, '*' ) // != "blabla.*"
274 && !strrchr( extension
, '?' ) // != "blabla.?"
275 && extension
[1] // != "blabla."
276 && extension
[1] != ' ' ) // != "blabla. "
278 // now concat extension to the fileName:
279 m_fileName
= wxString(fileNameBuffer
) + wxString(extension
);
281 int len
= strlen( fileNameBuffer
);
282 strncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
283 fileNameBuffer
[ MAXPATH
-1 ] = '\0';
287 m_path
= fileNameBuffer
;
288 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
291 //=== Simulating the wxOVERWRITE_PROMPT >>============================
293 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) && ::wxFileExists( fileNameBuffer
) )
295 char questionText
[] = "Replace file\n%s?";
296 char* messageText
= new char[strlen(questionText
)+strlen(fileNameBuffer
)-1];
297 sprintf( messageText
, questionText
, fileNameBuffer
);
299 if ( messageText
&& ( wxMessageBox( (const char *)messageText
, m_message
, wxYES_NO
) != wxYES
) )
304 delete[] messageText
;
307 } // END: if ( success )
310 delete[] filterBuffer
;
312 return (success
? wxID_OK
: wxID_CANCEL
) ;
316 #define wxDIALOG_DEFAULT_X 300
317 #define wxDIALOG_DEFAULT_Y 300
319 // Generic file load/save dialog
320 // static inline char * // HP compiler complains
322 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
324 char *ext
= (char *)extension
;
329 str
= (const char*) wxTString("Load %s file");
331 str
= (const char*) wxTString("Save %s file");
332 sprintf(prompt
, str
, what
);
334 if (*ext
== '.') ext
++;
336 sprintf(wild
, "*.%s", ext
);
338 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
341 // Generic file load dialog
343 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
345 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
349 // Generic file save dialog
351 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
353 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);