]>
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"
31 #if wxUSE_FILEDLG && !wxUSE_SMARTPHONE
35 #include "wx/msgdlg.h"
36 #include "wx/filedlg.h"
37 #include "wx/filefn.h"
43 #include "wx/msw/private.h"
45 #if !defined(__WIN32__) || defined(__WXWINCE__)
53 #include "wx/filename.h"
54 #include "wx/tokenzr.h"
57 #define OFN_EXPLORER 0x00080000
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
65 # define wxMAXPATH 65534
67 # define wxMAXPATH 1024
70 # define wxMAXFILE 1024
74 // ============================================================================
76 // ============================================================================
78 IMPLEMENT_CLASS(wxFileDialog
, wxFileDialogBase
)
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 wxFileDialog::wxFileDialog(wxWindow
*parent
,
85 const wxString
& message
,
86 const wxString
& defaultDir
,
87 const wxString
& defaultFileName
,
88 const wxString
& wildCard
,
91 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
, wildCard
, style
, pos
)
94 if ( ( m_dialogStyle
& wxMULTIPLE
) && ( m_dialogStyle
& wxSAVE
) )
95 m_dialogStyle
&= ~wxMULTIPLE
;
98 void wxFileDialog::GetPaths(wxArrayString
& paths
) const
103 if ( m_dir
.Last() != _T('\\') )
106 size_t count
= m_fileNames
.GetCount();
107 for ( size_t n
= 0; n
< count
; n
++ )
109 if (wxFileName(m_fileNames
[n
]).IsAbsolute())
110 paths
.Add(m_fileNames
[n
]);
112 paths
.Add(dir
+ m_fileNames
[n
]);
116 void wxFileDialog::SetPath(const wxString
& path
)
119 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
121 m_fileName
<< _T('.') << ext
;
124 int wxFileDialog::ShowModal()
127 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
128 if (!hWnd
&& wxTheApp
->GetTopWindow())
129 hWnd
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
131 static wxChar fileNameBuffer
[ wxMAXPATH
]; // the file-name
132 wxChar titleBuffer
[ wxMAXFILE
+1+wxMAXEXT
]; // the file-name, without path
134 *fileNameBuffer
= wxT('\0');
135 *titleBuffer
= wxT('\0');
138 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
139 msw_flags
|= OFN_HIDEREADONLY
;
140 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
141 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
143 if (m_dialogStyle
& wxMULTIPLE
)
145 // OFN_EXPLORER must always be specified with OFN_ALLOWMULTISELECT
146 msw_flags
|= OFN_EXPLORER
| OFN_ALLOWMULTISELECT
;
149 // if wxCHANGE_DIR flag is not given we shouldn't change the CWD which the
150 // standard dialog does by default
151 if ( !(m_dialogStyle
& wxCHANGE_DIR
) )
153 msw_flags
|= OFN_NOCHANGEDIR
;
155 /* chris elliott for some reason this does not work usefully if no extension
156 is given, as it test for junk instead of junk.ext
157 if ( m_dialogStyle & wxOVERWRITE_PROMPT )
159 msw_flags |= OFN_OVERWRITEPROMPT;
165 // the OPENFILENAME struct has been extended in newer version of
166 // comcdlg32.dll, but as we don't use the extended fields anyhow, set
167 // the struct size to the old value - otherwise, the programs compiled
168 // with new headers will not work with the old libraries
169 #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500)
170 of
.lStructSize
= sizeof(OPENFILENAME
) -
171 (sizeof(void *) + 2*sizeof(DWORD
));
173 of
.lStructSize
= sizeof(OPENFILENAME
);
177 of
.lpstrTitle
= WXSTRINGCAST m_message
;
178 of
.lpstrFileTitle
= titleBuffer
;
179 of
.nMaxFileTitle
= wxMAXFILE
+ 1 + wxMAXEXT
; // Windows 3.0 and 3.1
181 // Convert forward slashes to backslashes (file selector doesn't like
182 // forward slashes) and also squeeze multiple consecutive slashes into one
183 // as it doesn't like two backslashes in a row neither
186 size_t i
, len
= m_dir
.length();
188 for ( i
= 0; i
< len
; i
++ )
190 wxChar ch
= m_dir
[i
];
194 // convert to backslash
200 while ( i
< len
- 1 )
202 wxChar chNext
= m_dir
[i
+ 1];
203 if ( chNext
!= _T('\\') && chNext
!= _T('/') )
206 // ignore the next one, unless it is at the start of a UNC path
220 of
.lpstrInitialDir
= dir
.c_str();
222 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 //=======================================================================
239 if ( wxStrlen(m_wildCard
) == 0 )
240 theFilter
= wxString(wxT("*.*"));
242 theFilter
= m_wildCard
;
243 wxString filterBuffer
;
245 if ( !wxStrchr( theFilter
, wxT('|') ) ) { // only one filter ==> default text
246 filterBuffer
.Printf(_("Files (%s)|%s"),
247 theFilter
.c_str(), theFilter
.c_str());
249 else { // more then one filter
250 filterBuffer
= theFilter
;
254 filterBuffer
+= wxT("|");
256 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
257 if ( filterBuffer
.GetChar(i
) == wxT('|') ) {
258 filterBuffer
[i
] = wxT('\0');
262 of
.lpstrFilter
= (LPTSTR
)(const wxChar
*)filterBuffer
;
263 of
.nFilterIndex
= m_filterIndex
+ 1;
265 //=== Setting defaultFileName >>=========================================
267 wxStrncpy( fileNameBuffer
, (const wxChar
*)m_fileName
, wxMAXPATH
-1 );
268 fileNameBuffer
[ wxMAXPATH
-1 ] = wxT('\0');
270 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
271 of
.nMaxFile
= wxMAXPATH
;
273 //== Execute FileDialog >>=================================================
275 bool success
= (m_dialogStyle
& wxSAVE
? GetSaveFileName(&of
)
276 : GetOpenFileName(&of
)) != 0;
278 DWORD errCode
= CommDlgExtendedError();
281 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
283 // The struct size has changed so try a smaller or bigger size
285 int oldStructSize
= of
.lStructSize
;
286 of
.lStructSize
= oldStructSize
- (sizeof(void *) + 2*sizeof(DWORD
));
287 success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
288 : (GetOpenFileName(&of
) != 0);
289 errCode
= CommDlgExtendedError();
291 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
293 of
.lStructSize
= oldStructSize
+ (sizeof(void *) + 2*sizeof(DWORD
));
294 success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
295 : (GetOpenFileName(&of
) != 0);
304 if ( ( m_dialogStyle
& wxMULTIPLE
) &&
305 #if defined(OFN_EXPLORER)
306 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT('\0') ) )
308 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT(' ') ) )
309 #endif // OFN_EXPLORER
311 #if defined(OFN_EXPLORER)
312 m_dir
= fileNameBuffer
;
314 m_fileName
= &fileNameBuffer
[i
];
315 m_fileNames
.Add(m_fileName
);
316 i
+= m_fileName
.Len() + 1;
318 while (fileNameBuffer
[i
] != wxT('\0'))
320 m_fileNames
.Add(&fileNameBuffer
[i
]);
321 i
+= wxStrlen(&fileNameBuffer
[i
]) + 1;
324 wxStringTokenizer
toke(fileNameBuffer
, _T(" \t\r\n"));
325 m_dir
= toke
.GetNextToken();
326 m_fileName
= toke
.GetNextToken();
327 m_fileNames
.Add(m_fileName
);
329 while (toke
.HasMoreTokens())
330 m_fileNames
.Add(toke
.GetNextToken());
331 #endif // OFN_EXPLORER
334 if ( m_dir
.Last() != _T('\\') )
337 m_fileNames
.Sort(wxStringSortAscending
);
338 m_path
= dir
+ m_fileName
;
342 //=== Adding the correct extension >>=================================
344 m_filterIndex
= (int)of
.nFilterIndex
- 1;
346 if ( !of
.nFileExtension
||
347 (of
.nFileExtension
&& fileNameBuffer
[of
.nFileExtension
] == wxT('\0')) )
349 // User has typed a filename without an extension:
350 const wxChar
* extension
= filterBuffer
;
351 int maxFilter
= (int)(of
.nFilterIndex
*2L) - 1;
353 for( int i
= 0; i
< maxFilter
; i
++ ) // get extension
354 extension
= extension
+ wxStrlen( extension
) + 1;
356 m_fileName
= AppendExtension(fileNameBuffer
, extension
);
357 wxStrncpy(fileNameBuffer
, m_fileName
.c_str(), wxMin(m_fileName
.Len(), wxMAXPATH
-1));
358 fileNameBuffer
[wxMin(m_fileName
.Len(), wxMAXPATH
-1)] = wxT('\0');
361 m_path
= fileNameBuffer
;
362 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
363 m_fileNames
.Add(m_fileName
);
364 m_dir
= wxPathOnly(fileNameBuffer
);
366 //=== Simulating the wxOVERWRITE_PROMPT >>============================
367 //should we also test for file save style ??
368 if ( (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
369 ::wxFileExists( fileNameBuffer
) )
371 wxString messageText
;
372 messageText
.Printf(_("File '%s' already exists.\nDo you want to replace it?"), fileNameBuffer
);
373 if ( wxMessageBox(messageText
, wxT("Save File As"), wxYES_NO
| wxICON_EXCLAMATION
) != wxYES
)
381 // common dialog failed - why?
383 DWORD dwErr
= CommDlgExtendedError();
386 // this msg is only for developers
387 wxLogError(wxT("Common dialog failed with error code %0lx."),
390 //else: it was just cancelled
394 return success
? wxID_OK
: wxID_CANCEL
;
398 #endif // wxUSE_FILEDLG