]> git.saurik.com Git - wxWidgets.git/blob - src/msw/filedlg.cpp
background colour of a combobox may now be set (bug 805442)
[wxWidgets.git] / src / msw / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "filedlg.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_FILEDLG && !wxUSE_SMARTPHONE
32
33 #ifndef WX_PRECOMP
34 #include "wx/utils.h"
35 #include "wx/msgdlg.h"
36 #include "wx/filedlg.h"
37 #include "wx/filefn.h"
38 #include "wx/intl.h"
39 #include "wx/log.h"
40 #include "wx/app.h"
41 #endif
42
43 #include "wx/msw/private.h"
44
45 #if !defined(__WIN32__) || defined(__WXWINCE__)
46 #include <commdlg.h>
47 #endif
48
49 #include <math.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include "wx/filename.h"
54 #include "wx/tokenzr.h"
55
56 #ifndef OFN_EXPLORER
57 #define OFN_EXPLORER 0x00080000
58 #endif
59
60 // ----------------------------------------------------------------------------
61 // constants
62 // ----------------------------------------------------------------------------
63
64 #ifdef __WIN32__
65 # define wxMAXPATH 65534
66 #else
67 # define wxMAXPATH 1024
68 #endif
69
70 # define wxMAXFILE 1024
71
72 # define wxMAXEXT 5
73
74 // ============================================================================
75 // implementation
76 // ============================================================================
77
78 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
79
80 // ----------------------------------------------------------------------------
81 // wxFileDialog
82 // ----------------------------------------------------------------------------
83
84 wxFileDialog::wxFileDialog(wxWindow *parent,
85 const wxString& message,
86 const wxString& defaultDir,
87 const wxString& defaultFileName,
88 const wxString& wildCard,
89 long style,
90 const wxPoint& pos)
91 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
92
93 {
94 if ( ( m_dialogStyle & wxMULTIPLE ) && ( m_dialogStyle & wxSAVE ) )
95 m_dialogStyle &= ~wxMULTIPLE;
96 }
97
98 void wxFileDialog::GetPaths(wxArrayString& paths) const
99 {
100 paths.Empty();
101
102 wxString dir(m_dir);
103 if ( m_dir.Last() != _T('\\') )
104 dir += _T('\\');
105
106 size_t count = m_fileNames.GetCount();
107 for ( size_t n = 0; n < count; n++ )
108 {
109 if (wxFileName(m_fileNames[n]).IsAbsolute())
110 paths.Add(m_fileNames[n]);
111 else
112 paths.Add(dir + m_fileNames[n]);
113 }
114 }
115
116 void wxFileDialog::GetFilenames(wxArrayString& files) const
117 {
118 files = m_fileNames;
119 }
120
121 void wxFileDialog::SetPath(const wxString& path)
122 {
123 wxString ext;
124 wxSplitPath(path, &m_dir, &m_fileName, &ext);
125 if ( !ext.empty() )
126 m_fileName << _T('.') << ext;
127 }
128
129 int wxFileDialog::ShowModal()
130 {
131 HWND hWnd = 0;
132 if (m_parent) hWnd = (HWND) m_parent->GetHWND();
133 if (!hWnd && wxTheApp->GetTopWindow())
134 hWnd = (HWND) wxTheApp->GetTopWindow()->GetHWND();
135
136 static wxChar fileNameBuffer [ wxMAXPATH ]; // the file-name
137 wxChar titleBuffer [ wxMAXFILE+1+wxMAXEXT ]; // the file-name, without path
138
139 *fileNameBuffer = wxT('\0');
140 *titleBuffer = wxT('\0');
141
142 long msw_flags = 0;
143 if ( (m_dialogStyle & wxHIDE_READONLY) || (m_dialogStyle & wxSAVE) )
144 msw_flags |= OFN_HIDEREADONLY;
145 if ( m_dialogStyle & wxFILE_MUST_EXIST )
146 msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
147
148 if (m_dialogStyle & wxMULTIPLE )
149 {
150 // OFN_EXPLORER must always be specified with OFN_ALLOWMULTISELECT
151 msw_flags |= OFN_EXPLORER | OFN_ALLOWMULTISELECT;
152 }
153
154 // if wxCHANGE_DIR flag is not given we shouldn't change the CWD which the
155 // standard dialog does by default
156 if ( !(m_dialogStyle & wxCHANGE_DIR) )
157 {
158 msw_flags |= OFN_NOCHANGEDIR;
159 }
160
161 if ( m_dialogStyle & wxOVERWRITE_PROMPT )
162 {
163 msw_flags |= OFN_OVERWRITEPROMPT;
164 }
165
166 OPENFILENAME of;
167 wxZeroMemory(of);
168
169 // the OPENFILENAME struct has been extended in newer version of
170 // comcdlg32.dll, but as we don't use the extended fields anyhow, set
171 // the struct size to the old value - otherwise, the programs compiled
172 // with new headers will not work with the old libraries
173 #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0500)
174 of.lStructSize = sizeof(OPENFILENAME) -
175 (sizeof(void *) + 2*sizeof(DWORD));
176 #else // old headers
177 of.lStructSize = sizeof(OPENFILENAME);
178 #endif
179
180 of.hwndOwner = hWnd;
181 of.lpstrTitle = WXSTRINGCAST m_message;
182 of.lpstrFileTitle = titleBuffer;
183 of.nMaxFileTitle = wxMAXFILE + 1 + wxMAXEXT; // Windows 3.0 and 3.1
184
185 // Convert forward slashes to backslashes (file selector doesn't like
186 // forward slashes) and also squeeze multiple consecutive slashes into one
187 // as it doesn't like two backslashes in a row neither
188
189 wxString dir;
190 size_t i, len = m_dir.length();
191 dir.reserve(len);
192 for ( i = 0; i < len; i++ )
193 {
194 wxChar ch = m_dir[i];
195 switch ( ch )
196 {
197 case _T('/'):
198 // convert to backslash
199 ch = _T('\\');
200
201 // fall through
202
203 case _T('\\'):
204 while ( i < len - 1 )
205 {
206 wxChar chNext = m_dir[i + 1];
207 if ( chNext != _T('\\') && chNext != _T('/') )
208 break;
209
210 // ignore the next one, unless it is at the start of a UNC path
211 if (i > 0)
212 i++;
213 else
214 break;
215 }
216 // fall through
217
218 default:
219 // normal char
220 dir += ch;
221 }
222 }
223
224 of.lpstrInitialDir = dir.c_str();
225
226 of.Flags = msw_flags;
227
228
229 //=== Like Alejandro Sierra's wildcard modification >>===================
230 /*
231 In wxFileSelector you can put, instead of a single wild_card,
232 pairs of strings separated by '|'.
233 The first string is a description, and the
234 second is the wild card. You can put any number of pairs.
235
236 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
237
238 If you put a single wild card, it works as before the modification.
239 */
240 //=======================================================================
241
242 wxString theFilter;
243 if ( wxStrlen(m_wildCard) == 0 )
244 theFilter = wxString(wxT("*.*"));
245 else
246 theFilter = m_wildCard ;
247 wxString filterBuffer;
248
249 if ( !wxStrchr( theFilter, wxT('|') ) ) { // only one filter ==> default text
250 filterBuffer.Printf(_("Files (%s)|%s"),
251 theFilter.c_str(), theFilter.c_str());
252 }
253 else { // more then one filter
254 filterBuffer = theFilter;
255
256 }
257
258 filterBuffer += wxT("|");
259 // Replace | with \0
260 for (i = 0; i < filterBuffer.Len(); i++ ) {
261 if ( filterBuffer.GetChar(i) == wxT('|') ) {
262 filterBuffer[i] = wxT('\0');
263 }
264 }
265
266 of.lpstrFilter = (LPTSTR)(const wxChar *)filterBuffer;
267 of.nFilterIndex = m_filterIndex + 1;
268
269 //=== Setting defaultFileName >>=========================================
270
271 wxStrncpy( fileNameBuffer, (const wxChar *)m_fileName, wxMAXPATH-1 );
272 fileNameBuffer[ wxMAXPATH-1 ] = wxT('\0');
273
274 of.lpstrFile = fileNameBuffer; // holds returned filename
275 of.nMaxFile = wxMAXPATH;
276
277 //== Execute FileDialog >>=================================================
278
279 bool success = (m_dialogStyle & wxSAVE ? GetSaveFileName(&of)
280 : GetOpenFileName(&of)) != 0;
281
282 DWORD errCode = CommDlgExtendedError();
283
284 #ifdef __WIN32__
285 if (!success && (errCode == CDERR_STRUCTSIZE))
286 {
287 // The struct size has changed so try a smaller or bigger size
288
289 int oldStructSize = of.lStructSize;
290 of.lStructSize = oldStructSize - (sizeof(void *) + 2*sizeof(DWORD));
291 success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
292 : (GetOpenFileName(&of) != 0);
293 errCode = CommDlgExtendedError();
294
295 if (!success && (errCode == CDERR_STRUCTSIZE))
296 {
297 of.lStructSize = oldStructSize + (sizeof(void *) + 2*sizeof(DWORD));
298 success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
299 : (GetOpenFileName(&of) != 0);
300 }
301 }
302 #endif // __WIN32__
303
304 if ( success )
305 {
306 m_fileNames.Empty();
307
308 if ( ( m_dialogStyle & wxMULTIPLE ) &&
309 #if defined(OFN_EXPLORER)
310 ( fileNameBuffer[of.nFileOffset-1] == wxT('\0') )
311 #else
312 ( fileNameBuffer[of.nFileOffset-1] == wxT(' ') )
313 #endif // OFN_EXPLORER
314 )
315 {
316 #if defined(OFN_EXPLORER)
317 m_dir = fileNameBuffer;
318 i = of.nFileOffset;
319 m_fileName = &fileNameBuffer[i];
320 m_fileNames.Add(m_fileName);
321 i += m_fileName.Len() + 1;
322
323 while (fileNameBuffer[i] != wxT('\0'))
324 {
325 m_fileNames.Add(&fileNameBuffer[i]);
326 i += wxStrlen(&fileNameBuffer[i]) + 1;
327 }
328 #else
329 wxStringTokenizer toke(fileNameBuffer, _T(" \t\r\n"));
330 m_dir = toke.GetNextToken();
331 m_fileName = toke.GetNextToken();
332 m_fileNames.Add(m_fileName);
333
334 while (toke.HasMoreTokens())
335 m_fileNames.Add(toke.GetNextToken());
336 #endif // OFN_EXPLORER
337
338 wxString dir(m_dir);
339 if ( m_dir.Last() != _T('\\') )
340 dir += _T('\\');
341
342 m_path = dir + m_fileName;
343 m_filterIndex = (int)of.nFilterIndex - 1;
344 }
345 else
346 {
347 //=== Adding the correct extension >>=================================
348
349 m_filterIndex = (int)of.nFilterIndex - 1;
350
351 if ( !of.nFileExtension ||
352 (of.nFileExtension && fileNameBuffer[of.nFileExtension] == wxT('\0')) )
353 {
354 // User has typed a filename without an extension:
355 const wxChar* extension = filterBuffer;
356 int maxFilter = (int)(of.nFilterIndex*2L) - 1;
357
358 for( int i = 0; i < maxFilter; i++ ) // get extension
359 extension = extension + wxStrlen( extension ) + 1;
360
361 m_fileName = AppendExtension(fileNameBuffer, extension);
362 wxStrncpy(fileNameBuffer, m_fileName.c_str(), wxMin(m_fileName.Len(), wxMAXPATH-1));
363 fileNameBuffer[wxMin(m_fileName.Len(), wxMAXPATH-1)] = wxT('\0');
364 }
365
366 m_path = fileNameBuffer;
367 m_fileName = wxFileNameFromPath(fileNameBuffer);
368 m_fileNames.Add(m_fileName);
369 m_dir = wxPathOnly(fileNameBuffer);
370 }
371 }
372 else
373 {
374 // common dialog failed - why?
375 #ifdef __WXDEBUG__
376 DWORD dwErr = CommDlgExtendedError();
377 if ( dwErr != 0 )
378 {
379 // this msg is only for developers
380 wxLogError(wxT("Common dialog failed with error code %0lx."),
381 dwErr);
382 }
383 //else: it was just cancelled
384 #endif
385 }
386
387 return success ? wxID_OK : wxID_CANCEL;
388
389 }
390
391 #endif // wxUSE_FILEDLG
392