]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "filedlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dialog.h"
37 #include "wx/filedlg.h"
38 #include "wx/filefn.h"
44 #include "wx/msw/private.h"
46 #if !defined(__WIN32__) || defined(__WXWINCE__)
54 #include "wx/filename.h"
55 #include "wx/tokenzr.h"
58 #define OFN_EXPLORER 0x00080000
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
66 # define wxMAXPATH 65534
68 # define wxMAXPATH 1024
71 # define wxMAXFILE 1024
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 wxFileDialog::wxFileDialog(wxWindow
*parent
,
90 const wxString
& message
,
91 const wxString
& defaultDir
,
92 const wxString
& defaultFileName
,
93 const wxString
& wildCard
,
95 const wxPoint
& WXUNUSED(pos
))
98 m_dialogStyle
= style
;
99 if ( ( m_dialogStyle
& wxMULTIPLE
) && ( m_dialogStyle
& wxSAVE
) )
100 m_dialogStyle
&= ~wxMULTIPLE
;
102 m_path
= wxEmptyString
;
103 m_fileName
= defaultFileName
;
105 m_wildCard
= wildCard
;
109 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
114 if ( m_dir
.Last() != _T('\\') )
117 size_t count
= m_fileNames
.GetCount();
118 for ( size_t n
= 0; n
< count
; n
++ )
120 if (wxFileName(m_fileNames
[n
]).IsAbsolute())
121 paths
.Add(m_fileNames
[n
]);
123 paths
.Add(dir
+ m_fileNames
[n
]);
127 void wxFileDialog::SetPath(const wxString
& path
)
130 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
132 m_fileName
<< _T('.') << ext
;
135 int wxFileDialog::ShowModal()
138 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
139 if (!hWnd
&& wxTheApp
->GetTopWindow())
140 hWnd
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
142 static wxChar fileNameBuffer
[ wxMAXPATH
]; // the file-name
143 wxChar titleBuffer
[ wxMAXFILE
+1+wxMAXEXT
]; // the file-name, without path
145 *fileNameBuffer
= wxT('\0');
146 *titleBuffer
= wxT('\0');
149 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
150 msw_flags
|= OFN_HIDEREADONLY
;
151 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
152 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
154 if (m_dialogStyle
& wxMULTIPLE
)
156 // OFN_EXPLORER must always be specified with OFN_ALLOWMULTISELECT
157 msw_flags
|= OFN_EXPLORER
| OFN_ALLOWMULTISELECT
;
160 // if wxCHANGE_DIR flag is not given we shouldn't change the CWD which the
161 // standard dialog does by default
162 if ( !(m_dialogStyle
& wxCHANGE_DIR
) )
164 msw_flags
|= OFN_NOCHANGEDIR
;
166 /* chris elliott for some reason this does not work usefully if no extension
167 is given, as it test for junk instead of junk.ext
168 if ( m_dialogStyle & wxOVERWRITE_PROMPT )
170 msw_flags |= OFN_OVERWRITEPROMPT;
176 // the OPENFILENAME struct has been extended in newer version of
177 // comcdlg32.dll, but as we don't use the extended fields anyhow, set
178 // the struct size to the old value - otherwise, the programs compiled
179 // with new headers will not work with the old libraries
180 #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500)
181 of
.lStructSize
= sizeof(OPENFILENAME
) -
182 (sizeof(void *) + 2*sizeof(DWORD
));
184 of
.lStructSize
= sizeof(OPENFILENAME
);
188 of
.lpstrTitle
= WXSTRINGCAST m_message
;
189 of
.lpstrFileTitle
= titleBuffer
;
190 of
.nMaxFileTitle
= wxMAXFILE
+ 1 + wxMAXEXT
; // Windows 3.0 and 3.1
192 // Convert forward slashes to backslashes (file selector doesn't like
193 // forward slashes) and also squeeze multiple consecutive slashes into one
194 // as it doesn't like two backslashes in a row neither
197 size_t i
, len
= m_dir
.length();
199 for ( i
= 0; i
< len
; i
++ )
201 wxChar ch
= m_dir
[i
];
205 // convert to backslash
211 while ( i
< len
- 1 )
213 wxChar chNext
= m_dir
[i
+ 1];
214 if ( chNext
!= _T('\\') && chNext
!= _T('/') )
217 // ignore the next one, unless it is at the start of a UNC path
231 of
.lpstrInitialDir
= dir
.c_str();
233 of
.Flags
= msw_flags
;
236 //=== Like Alejandro Sierra's wildcard modification >>===================
238 In wxFileSelector you can put, instead of a single wild_card,
239 pairs of strings separated by '|'.
240 The first string is a description, and the
241 second is the wild card. You can put any number of pairs.
243 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
245 If you put a single wild card, it works as before the modification.
247 //=======================================================================
250 if ( wxStrlen(m_wildCard
) == 0 )
251 theFilter
= wxString(wxT("*.*"));
253 theFilter
= m_wildCard
;
254 wxString filterBuffer
;
256 if ( !wxStrchr( theFilter
, wxT('|') ) ) { // only one filter ==> default text
257 filterBuffer
.Printf(_("Files (%s)|%s"),
258 theFilter
.c_str(), theFilter
.c_str());
260 else { // more then one filter
261 filterBuffer
= theFilter
;
265 filterBuffer
+= wxT("|");
267 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
268 if ( filterBuffer
.GetChar(i
) == wxT('|') ) {
269 filterBuffer
[i
] = wxT('\0');
273 of
.lpstrFilter
= (LPTSTR
)(const wxChar
*)filterBuffer
;
274 of
.nFilterIndex
= m_filterIndex
+ 1;
276 //=== Setting defaultFileName >>=========================================
278 wxStrncpy( fileNameBuffer
, (const wxChar
*)m_fileName
, wxMAXPATH
-1 );
279 fileNameBuffer
[ wxMAXPATH
-1 ] = wxT('\0');
281 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
282 of
.nMaxFile
= wxMAXPATH
;
284 //== Execute FileDialog >>=================================================
286 bool success
= (m_dialogStyle
& wxSAVE
? GetSaveFileName(&of
)
287 : GetOpenFileName(&of
)) != 0;
289 DWORD errCode
= CommDlgExtendedError();
292 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
294 // The struct size has changed so try a smaller or bigger size
296 int oldStructSize
= of
.lStructSize
;
297 of
.lStructSize
= oldStructSize
- (sizeof(void *) + 2*sizeof(DWORD
));
298 success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
299 : (GetOpenFileName(&of
) != 0);
300 errCode
= CommDlgExtendedError();
302 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
304 of
.lStructSize
= oldStructSize
+ (sizeof(void *) + 2*sizeof(DWORD
));
305 success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
306 : (GetOpenFileName(&of
) != 0);
315 if ( ( m_dialogStyle
& wxMULTIPLE
) &&
316 #if defined(OFN_EXPLORER)
317 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT('\0') ) )
319 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT(' ') ) )
320 #endif // OFN_EXPLORER
322 #if defined(OFN_EXPLORER)
323 m_dir
= fileNameBuffer
;
325 m_fileName
= &fileNameBuffer
[i
];
326 m_fileNames
.Add(m_fileName
);
327 i
+= m_fileName
.Len() + 1;
329 while (fileNameBuffer
[i
] != wxT('\0'))
331 m_fileNames
.Add(&fileNameBuffer
[i
]);
332 i
+= wxStrlen(&fileNameBuffer
[i
]) + 1;
335 wxStringTokenizer
toke(fileNameBuffer
, _T(" \t\r\n"));
336 m_dir
= toke
.GetNextToken();
337 m_fileName
= toke
.GetNextToken();
338 m_fileNames
.Add(m_fileName
);
340 while (toke
.HasMoreTokens())
341 m_fileNames
.Add(toke
.GetNextToken());
342 #endif // OFN_EXPLORER
345 if ( m_dir
.Last() != _T('\\') )
348 m_fileNames
.Sort(wxStringSortAscending
);
349 m_path
= dir
+ m_fileName
;
353 const wxChar
* extension
= NULL
;
355 //=== Adding the correct extension >>=================================
357 m_filterIndex
= (int)of
.nFilterIndex
- 1;
359 if ( !of
.nFileExtension
||
360 (of
.nFileExtension
&& fileNameBuffer
[of
.nFileExtension
] == wxT('\0')) )
362 // User has typed a filename without an extension:
364 // A filename can end in a "." here ("abc."), this means it
365 // does not have an extension. Because later on a "." with
366 // the default extension is appended we remove the "." if
367 // filename ends with one (We don't want files called
369 int idx
= wxStrlen(fileNameBuffer
) - 1;
370 if ( fileNameBuffer
[idx
] == wxT('.') )
372 fileNameBuffer
[idx
] = wxT('\0');
375 int maxFilter
= (int)(of
.nFilterIndex
*2L-1L);
376 extension
= filterBuffer
;
378 for( int i
= 0; i
< maxFilter
; i
++ ) { // get extension
379 extension
= extension
+ wxStrlen( extension
) +1;
382 extension
= wxStrrchr( extension
, wxT('.') );
383 if ( extension
// != "blabla"
384 && !wxStrrchr( extension
, wxT('*') ) // != "blabla.*"
385 && !wxStrrchr( extension
, wxT('?') ) // != "blabla.?"
386 && extension
[1] // != "blabla."
387 && extension
[1] != wxT(' ') ) // != "blabla. "
389 // now concat extension to the fileName:
390 m_fileName
= wxString(fileNameBuffer
) + extension
;
392 int len
= wxStrlen( fileNameBuffer
);
393 wxStrncpy( fileNameBuffer
+ len
, extension
, wxMAXPATH
- len
);
394 fileNameBuffer
[ wxMAXPATH
-1 ] = wxT('\0');
398 m_path
= fileNameBuffer
;
399 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
400 m_fileNames
.Add(m_fileName
);
401 m_dir
= wxPathOnly(fileNameBuffer
);
403 //=== Simulating the wxOVERWRITE_PROMPT >>============================
404 //should we also test for file save style ??
405 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
406 ::wxFileExists( fileNameBuffer
) )
408 wxString messageText
;
409 messageText
.Printf(_("File '%s' already exists.\nDo you want to replace it?"), fileNameBuffer
);
410 if ( wxMessageBox(messageText
, wxT("Save File As"), wxYES_NO
| wxICON_EXCLAMATION
) != wxYES
)
418 // common dialog failed - why?
420 DWORD dwErr
= CommDlgExtendedError();
423 // this msg is only for developers
424 wxLogError(wxT("Common dialog failed with error code %0lx."),
427 //else: it was just cancelled
431 return success
? wxID_OK
: wxID_CANCEL
;
435 #endif // wxUSE_FILEDLG