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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "filedlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if wxUSE_FILEDLG && !defined(__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::GetFilenames(wxArrayString
& files
) const
121 void wxFileDialog::SetPath(const wxString
& path
)
124 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
126 m_fileName
<< _T('.') << ext
;
129 int wxFileDialog::ShowModal()
132 if (m_parent
) hWnd
= (HWND
) m_parent
->GetHWND();
133 if (!hWnd
&& wxTheApp
->GetTopWindow())
134 hWnd
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
136 static wxChar fileNameBuffer
[ wxMAXPATH
]; // the file-name
137 wxChar titleBuffer
[ wxMAXFILE
+1+wxMAXEXT
]; // the file-name, without path
139 *fileNameBuffer
= wxT('\0');
140 *titleBuffer
= wxT('\0');
142 #if WXWIN_COMPATIBILITY_2_4
144 if ( (m_dialogStyle
& wxHIDE_READONLY
) || (m_dialogStyle
& wxSAVE
) )
145 msw_flags
|= OFN_HIDEREADONLY
;
147 long msw_flags
= OFN_HIDEREADONLY
;
150 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
151 msw_flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
153 if (m_dialogStyle
& wxMULTIPLE
)
155 // OFN_EXPLORER must always be specified with OFN_ALLOWMULTISELECT
156 msw_flags
|= OFN_EXPLORER
| OFN_ALLOWMULTISELECT
;
159 // if wxCHANGE_DIR flag is not given we shouldn't change the CWD which the
160 // standard dialog does by default
161 if ( !(m_dialogStyle
& wxCHANGE_DIR
) )
163 msw_flags
|= OFN_NOCHANGEDIR
;
166 if ( m_dialogStyle
& wxOVERWRITE_PROMPT
)
168 msw_flags
|= OFN_OVERWRITEPROMPT
;
174 // the OPENFILENAME struct has been extended in newer version of
175 // comcdlg32.dll, but as we don't use the extended fields anyhow, set
176 // the struct size to the old value - otherwise, the programs compiled
177 // with new headers will not work with the old libraries
178 #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500)
179 of
.lStructSize
= sizeof(OPENFILENAME
) -
180 (sizeof(void *) + 2*sizeof(DWORD
));
182 of
.lStructSize
= sizeof(OPENFILENAME
);
186 of
.lpstrTitle
= WXSTRINGCAST m_message
;
187 of
.lpstrFileTitle
= titleBuffer
;
188 of
.nMaxFileTitle
= wxMAXFILE
+ 1 + wxMAXEXT
; // Windows 3.0 and 3.1
190 // Convert forward slashes to backslashes (file selector doesn't like
191 // forward slashes) and also squeeze multiple consecutive slashes into one
192 // as it doesn't like two backslashes in a row neither
195 size_t i
, len
= m_dir
.length();
197 for ( i
= 0; i
< len
; i
++ )
199 wxChar ch
= m_dir
[i
];
203 // convert to backslash
209 while ( i
< len
- 1 )
211 wxChar chNext
= m_dir
[i
+ 1];
212 if ( chNext
!= _T('\\') && chNext
!= _T('/') )
215 // ignore the next one, unless it is at the start of a UNC path
229 of
.lpstrInitialDir
= dir
.c_str();
231 of
.Flags
= msw_flags
;
233 wxArrayString wildDescriptions
, wildFilters
;
235 size_t items
= wxParseCommonDialogsFilter(m_wildCard
, wildDescriptions
, wildFilters
);
237 wxASSERT_MSG( items
> 0 , _T("empty wildcard list") );
239 wxString filterBuffer
;
241 for (i
= 0; i
< items
; i
++)
243 filterBuffer
+= wildDescriptions
[i
];
244 filterBuffer
+= wxT("|");
245 filterBuffer
+= wildFilters
[i
];
246 filterBuffer
+= wxT("|");
250 for (i
= 0; i
< filterBuffer
.Len(); i
++ ) {
251 if ( filterBuffer
.GetChar(i
) == wxT('|') ) {
252 filterBuffer
[i
] = wxT('\0');
256 of
.lpstrFilter
= (LPTSTR
)filterBuffer
.c_str();
257 of
.nFilterIndex
= m_filterIndex
+ 1;
259 //=== Setting defaultFileName >>=========================================
261 wxStrncpy( fileNameBuffer
, (const wxChar
*)m_fileName
, wxMAXPATH
-1 );
262 fileNameBuffer
[ wxMAXPATH
-1 ] = wxT('\0');
264 of
.lpstrFile
= fileNameBuffer
; // holds returned filename
265 of
.nMaxFile
= wxMAXPATH
;
267 //== Execute FileDialog >>=================================================
269 bool success
= (m_dialogStyle
& wxSAVE
? GetSaveFileName(&of
)
270 : GetOpenFileName(&of
)) != 0;
272 DWORD errCode
= CommDlgExtendedError();
275 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
277 // The struct size has changed so try a smaller or bigger size
279 int oldStructSize
= of
.lStructSize
;
280 of
.lStructSize
= oldStructSize
- (sizeof(void *) + 2*sizeof(DWORD
));
281 success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
282 : (GetOpenFileName(&of
) != 0);
283 errCode
= CommDlgExtendedError();
285 if (!success
&& (errCode
== CDERR_STRUCTSIZE
))
287 of
.lStructSize
= oldStructSize
+ (sizeof(void *) + 2*sizeof(DWORD
));
288 success
= (m_dialogStyle
& wxSAVE
) ? (GetSaveFileName(&of
) != 0)
289 : (GetOpenFileName(&of
) != 0);
298 if ( ( m_dialogStyle
& wxMULTIPLE
) &&
299 #if defined(OFN_EXPLORER)
300 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT('\0') )
302 ( fileNameBuffer
[of
.nFileOffset
-1] == wxT(' ') )
303 #endif // OFN_EXPLORER
306 #if defined(OFN_EXPLORER)
307 m_dir
= fileNameBuffer
;
309 m_fileName
= &fileNameBuffer
[i
];
310 m_fileNames
.Add(m_fileName
);
311 i
+= m_fileName
.Len() + 1;
313 while (fileNameBuffer
[i
] != wxT('\0'))
315 m_fileNames
.Add(&fileNameBuffer
[i
]);
316 i
+= wxStrlen(&fileNameBuffer
[i
]) + 1;
319 wxStringTokenizer
toke(fileNameBuffer
, _T(" \t\r\n"));
320 m_dir
= toke
.GetNextToken();
321 m_fileName
= toke
.GetNextToken();
322 m_fileNames
.Add(m_fileName
);
324 while (toke
.HasMoreTokens())
325 m_fileNames
.Add(toke
.GetNextToken());
326 #endif // OFN_EXPLORER
329 if ( m_dir
.Last() != _T('\\') )
332 m_path
= dir
+ m_fileName
;
333 m_filterIndex
= (int)of
.nFilterIndex
- 1;
337 //=== Adding the correct extension >>=================================
339 m_filterIndex
= (int)of
.nFilterIndex
- 1;
341 if ( !of
.nFileExtension
||
342 (of
.nFileExtension
&& fileNameBuffer
[of
.nFileExtension
] == wxT('\0')) )
344 // User has typed a filename without an extension:
345 const wxChar
* extension
= filterBuffer
;
346 int maxFilter
= (int)(of
.nFilterIndex
*2L) - 1;
348 for( int i
= 0; i
< maxFilter
; i
++ ) // get extension
349 extension
= extension
+ wxStrlen( extension
) + 1;
351 m_fileName
= AppendExtension(fileNameBuffer
, extension
);
352 wxStrncpy(fileNameBuffer
, m_fileName
.c_str(), wxMin(m_fileName
.Len(), wxMAXPATH
-1));
353 fileNameBuffer
[wxMin(m_fileName
.Len(), wxMAXPATH
-1)] = wxT('\0');
356 m_path
= fileNameBuffer
;
357 m_fileName
= wxFileNameFromPath(fileNameBuffer
);
358 m_fileNames
.Add(m_fileName
);
359 m_dir
= wxPathOnly(fileNameBuffer
);
364 // common dialog failed - why?
366 DWORD dwErr
= CommDlgExtendedError();
369 // this msg is only for developers
370 wxLogError(wxT("Common dialog failed with error code %0lx."),
373 //else: it was just cancelled
377 return success
? wxID_OK
: wxID_CANCEL
;
381 #endif // wxUSE_FILEDLG