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
));
88 int filterFind
,filterIndex
=0;
90 for(ii
=0;ii
<filter2
.Length();ii
++)
92 if(filter2
[ii
] == '|')
96 for(;ii
<filter2
.Length();ii
++)
97 if(filter2
[ii
] == '|')
99 if(ii
-is
-1 > 0 && is
+1 < filter2
.Length())
100 if(filter2
.Mid(is
+1,ii
-is
-1) == defaultExtension
)
102 filterFind
=filterIndex
;
107 fileDialog
.SetFilterIndex(filterFind
);
110 if ( fileDialog
.ShowModal() == wxID_OK
)
112 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
120 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
140 char *wxFileSelectorEx(const char *title
,
141 const char *defaultDir
,
142 const char *defaultFileName
,
143 int* defaultFilterIndex
,
151 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
152 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
154 if ( fileDialog
.ShowModal() == wxID_OK
)
156 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
157 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
164 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
165 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
166 long style
, const wxPoint
& pos
)
169 m_dialogStyle
= style
;
172 m_fileName
= defaultFileName
;
174 m_wildCard
= wildCard
;
178 int wxFileDialog::ShowModal(void)
181 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
183 static char fileNameBuffer
[ MAXPATH
]; // the file-name
184 char titleBuffer
[ MAXFILE
+1+MAXEXT
]; // the file-name, without path
186 *fileNameBuffer
= '\0';
189 char* filterBuffer
= NULL
;
190 char* extension
= NULL
;
191 char* theFilter
= (char *)(const char *)m_wildCard
;
194 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
195 msw_flags
|= OFN_HIDEREADONLY
;
196 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
197 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
200 memset(&of
, 0, sizeof(OPENFILENAME
));
202 of
.lpstrCustomFilter
= NULL
; // system should not save custom filter
203 of
.nMaxCustFilter
= 0L;
205 of
.nFileOffset
= 0; // 0-based pointer to filname in lpstFile
206 of
.nFileExtension
= 0; // 0-based pointer to extension in lpstrFile
207 of
.lpstrDefExt
= NULL
; // no default extension
209 of
.lStructSize
= sizeof(OPENFILENAME
);
211 of
.lpstrTitle
= (char *)(const char *)m_message
;
214 of
.lpstrFileTitle
= titleBuffer
;
215 of
.nMaxFileTitle
= MAXFILE
+ 1 + MAXEXT
; // Windows 3.0 and 3.1
217 of
.lpstrInitialDir
= (const char *) m_dir
;
219 of
.Flags
= msw_flags
;
223 //=== Like Alejandro Sierra's wildcard modification >>===================
225 In wxFileSelector you can put, instead of a single wild_card,
226 pairs of strings separated by '|'.
227 The first string is a description, and the
228 second is the wild card. You can put any number of pairs.
230 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
232 If you put a single wild card, it works as before the modification.
234 //=======================================================================
236 if ( !theFilter
|| (strcmp(theFilter
, "") == 0)) theFilter
= "*.*";
238 int filterBufferLen
= 0;
240 if ( !strchr( theFilter
, '|' ) ) { // only one filter ==> default text:
241 char buffText
[] = "Files (%s)|%s";
242 filterBufferLen
= strlen( theFilter
)*2 + strlen( buffText
) -4;
243 filterBuffer
= new char[ filterBufferLen
+2 ];
245 if ( filterBuffer
) {
246 sprintf( filterBuffer
, buffText
, theFilter
, theFilter
);
249 else { // more then one filter
250 filterBufferLen
= strlen( theFilter
);
251 filterBuffer
= new char[ filterBufferLen
+2 ];
253 if ( filterBuffer
) {
254 strcpy( filterBuffer
, theFilter
);
258 if ( filterBuffer
) { // Substituting '|' with '\0'
259 for ( int i
= 0; i
< filterBufferLen
; i
++ ) {
260 if ( filterBuffer
[i
] == '|' ) { filterBuffer
[i
] = '\0'; }
264 filterBuffer
[filterBufferLen
+1] = '\0';
266 of
.lpstrFilter
= (LPSTR
)filterBuffer
;
267 of
.nFilterIndex
= m_filterIndex
;
269 //=== Setting defaultFileName >>=========================================
271 strncpy( fileNameBuffer
, (const char *)m_fileName
, MAXPATH
-1 );
272 fileNameBuffer
[ MAXPATH
-1 ] = '\0';
274 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
275 of
.nMaxFile
= MAXPATH
;
277 //== Execute FileDialog >>=================================================
279 bool success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0) : (GetOpenFileName(&of
) != 0);
283 //=== Adding the correct extension >>=================================
285 m_filterIndex
= (int)of
.nFilterIndex
;
287 if ( of
.nFileExtension
&& fileNameBuffer
[ of
.nFileExtension
-1] != '.' )
288 { // user has typed an filename
289 // without an extension:
291 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
292 extension
= filterBuffer
;
294 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
295 extension
= extension
+ strlen( extension
) +1;
298 extension
= strrchr( extension
, '.' );
299 if ( extension
// != "blabla"
300 && !strrchr( extension
, '*' ) // != "blabla.*"
301 && !strrchr( extension
, '?' ) // != "blabla.?"
302 && extension
[1] // != "blabla."
303 && extension
[1] != ' ' ) // != "blabla. "
305 // now concat extension to the fileName:
306 m_fileName
= wxString(fileNameBuffer
) + wxString(extension
);
308 int len
= strlen( fileNameBuffer
);
309 strncpy( fileNameBuffer
+ len
, extension
, MAXPATH
- len
);
310 fileNameBuffer
[ MAXPATH
-1 ] = '\0';
314 m_path
= fileNameBuffer
;
315 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
318 //=== Simulating the wxOVERWRITE_PROMPT >>============================
320 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) && ::wxFileExists( fileNameBuffer
) )
322 char questionText
[] = "Replace file\n%s?";
323 char* messageText
= new char[strlen(questionText
)+strlen(fileNameBuffer
)-1];
324 sprintf( messageText
, questionText
, fileNameBuffer
);
326 if ( messageText
&& ( wxMessageBox( (const char *)messageText
, m_message
, wxYES_NO
) != wxYES
) )
331 delete[] messageText
;
334 } // END: if ( success )
337 delete[] filterBuffer
;
339 return (success
? wxID_OK
: wxID_CANCEL
) ;
343 #define wxDIALOG_DEFAULT_X 300
344 #define wxDIALOG_DEFAULT_Y 300
346 // Generic file load/save dialog
347 // static inline char * // HP compiler complains
349 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
351 char *ext
= (char *)extension
;
356 str
= "Load %s file";
358 str
= "Save %s file";
359 sprintf(prompt
, wxGetTranslation(str
), what
);
361 if (*ext
== '.') ext
++;
363 sprintf(wild
, "*.%s", ext
);
365 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
368 // Generic file load dialog
370 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
372 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
376 // Generic file save dialog
378 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
380 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);