1. wxFileDialog patch for multiple selection applied (with some small changes),
[wxWidgets.git] / src / msw / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "filedlg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/defs.h"
26 #include "wx/utils.h"
27 #include "wx/msgdlg.h"
28 #include "wx/dialog.h"
29 #include "wx/filedlg.h"
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #endif
33
34 #include <windows.h>
35
36 #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
37 #include <commdlg.h>
38 #endif
39
40 #include "wx/msw/private.h"
41
42 #include <math.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #if !USE_SHARED_LIBRARY
47 IMPLEMENT_CLASS(wxFileDialog, wxDialog)
48 #endif
49
50 wxString wxFileSelector(const wxChar *title,
51 const wxChar *defaultDir,
52 const wxChar *defaultFileName,
53 const wxChar *defaultExtension,
54 const wxChar *filter,
55 int flags,
56 wxWindow *parent,
57 int x, int y)
58 {
59 // In the original implementation, defaultExtension is passed to the
60 // lpstrDefExt member of OPENFILENAME. This extension, if non-NULL, is
61 // appended to the filename if the user fails to type an extension. The new
62 // implementation (taken from wxFileSelectorEx) appends the extension
63 // automatically, by looking at the filter specification. In fact this
64 // should be better than the native Microsoft implementation because
65 // Windows only allows *one* default extension, whereas here we do the
66 // right thing depending on the filter the user has chosen.
67
68 // If there's a default extension specified but no filter, we create a
69 // suitable filter.
70
71 wxString filter2;
72 if ( defaultExtension && !filter )
73 filter2 = wxString(wxT("*.")) + defaultExtension;
74 else if ( filter )
75 filter2 = filter;
76
77 wxString defaultDirString;
78 if (defaultDir)
79 defaultDirString = defaultDir;
80
81 wxString defaultFilenameString;
82 if (defaultFileName)
83 defaultFilenameString = defaultFileName;
84
85 wxFileDialog fileDialog(parent, title, defaultDirString,
86 defaultFilenameString, filter2,
87 flags, wxPoint(x, y));
88 if( wxStrlen(defaultExtension) != 0 )
89 {
90 int filterFind = 1,
91 filterIndex = 0;
92
93 for( unsigned int i = 0; i < filter2.Len(); i++ )
94 {
95 if( filter2.GetChar(i) == wxT('|') )
96 {
97 // save the start index of the new filter
98 unsigned int is = i++;
99 filterIndex++;
100
101 // find the end of the filter
102 for( ; i < filter2.Len(); i++ )
103 {
104 if(filter2[i] == wxT('|'))
105 break;
106 }
107
108 if( i-is-1 > 0 && is+1 < filter2.Len() )
109 {
110 if( filter2.Mid(is+1,i-is-1).Contains(defaultExtension) )
111 // if( filter2.Mid(is+1,i-is-1) == defaultExtension )
112 {
113 filterFind = filterIndex;
114 break;
115 }
116 }
117 }
118 }
119
120 fileDialog.SetFilterIndex(filterFind);
121 }
122
123 if ( fileDialog.ShowModal() == wxID_OK )
124 {
125 wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath());
126 return wxBuffer;
127 }
128 else
129 return wxGetEmptyString();
130 }
131
132 # if __BORLANDC__
133 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
134 # endif
135
136 # ifndef MAXPATH
137 # define MAXPATH 400
138 # endif
139
140 # ifndef MAXDRIVE
141 # define MAXDRIVE 3
142 # endif
143
144 # ifndef MAXFILE
145 # define MAXFILE 9
146 # endif
147
148 # ifndef MAXEXT
149 # define MAXEXT 5
150 # endif
151
152
153 wxString wxFileSelectorEx(const wxChar *title,
154 const wxChar *defaultDir,
155 const wxChar *defaultFileName,
156 int* defaultFilterIndex,
157 const wxChar *filter,
158 int flags,
159 wxWindow* parent,
160 int x,
161 int y)
162
163 {
164 wxFileDialog fileDialog(parent, title ? title : wxT(""), defaultDir ? defaultDir : wxT(""),
165 defaultFileName ? defaultFileName : wxT(""), filter ? filter : wxT(""), flags, wxPoint(x, y));
166
167 if ( fileDialog.ShowModal() == wxID_OK )
168 {
169 *defaultFilterIndex = fileDialog.GetFilterIndex();
170 wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath());
171 return wxBuffer;
172 }
173 else
174 return wxGetEmptyString();
175 }
176
177 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
178 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
179 long style, const wxPoint& pos)
180 {
181 m_message = message;
182 m_dialogStyle = style;
183 if ( ( m_dialogStyle & wxMULTIPLE ) && ( m_dialogStyle & wxSAVE ) )
184 m_dialogStyle &= ~wxMULTIPLE;
185 m_parent = parent;
186 m_path = wxT("");
187 m_fileName = defaultFileName;
188 m_dir = defaultDir;
189 m_wildCard = wildCard;
190 m_filterIndex = 1;
191 }
192
193 void wxFileDialog::GetPaths(wxArrayString& paths) const
194 {
195 paths.Empty();
196
197 wxString dir(m_dir);
198 if ( m_dir.Last() != _T('\\') )
199 dir += _T('\\');
200
201 size_t count = m_fileNames.GetCount();
202 for ( size_t n = 0; n < count; n++ )
203 {
204 paths.Add(dir + m_fileNames[n]);
205 }
206 }
207
208 int wxFileDialog::ShowModal()
209 {
210 HWND hWnd = 0;
211 if (m_parent) hWnd = (HWND) m_parent->GetHWND();
212
213 static wxChar fileNameBuffer [ MAXPATH ]; // the file-name
214 wxChar titleBuffer [ MAXFILE+1+MAXEXT ]; // the file-name, without path
215
216 *fileNameBuffer = wxT('\0');
217 *titleBuffer = wxT('\0');
218
219 long msw_flags = 0;
220 if ( (m_dialogStyle & wxHIDE_READONLY) || (m_dialogStyle & wxSAVE) )
221 msw_flags |= OFN_HIDEREADONLY;
222 if ( m_dialogStyle & wxFILE_MUST_EXIST )
223 msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
224 if (m_dialogStyle & wxMULTIPLE )
225 msw_flags |=
226 #if defined(OFN_EXPLORER)
227 OFN_EXPLORER |
228 #endif // OFN_EXPLORER
229 OFN_ALLOWMULTISELECT;
230
231 OPENFILENAME of;
232 memset(&of, 0, sizeof(OPENFILENAME));
233
234 of.lpstrCustomFilter = NULL; // system should not save custom filter
235 of.nMaxCustFilter = 0L;
236
237 of.nFileOffset = 0; // 0-based pointer to filname in lpstFile
238 of.nFileExtension = 0; // 0-based pointer to extension in lpstrFile
239 of.lpstrDefExt = NULL; // no default extension
240
241 of.lStructSize = sizeof(OPENFILENAME);
242 of.hwndOwner = hWnd;
243 of.lpstrTitle = WXSTRINGCAST m_message;
244
245
246 of.lpstrFileTitle = titleBuffer;
247 of.nMaxFileTitle = MAXFILE + 1 + MAXEXT; // Windows 3.0 and 3.1
248
249 // Convert forward slashes to backslashes (file selector doesn't like
250 // forward slashes)
251 size_t i = 0;
252 size_t len = m_dir.Length();
253 for (i = 0; i < len; i++)
254 if (m_dir[i] == wxT('/'))
255 m_dir[i] = wxT('\\');
256
257 of.lpstrInitialDir = m_dir.c_str();
258
259 of.Flags = msw_flags;
260
261
262 //=== Like Alejandro Sierra's wildcard modification >>===================
263 /*
264 In wxFileSelector you can put, instead of a single wild_card,
265 pairs of strings separated by '|'.
266 The first string is a description, and the
267 second is the wild card. You can put any number of pairs.
268
269 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
270
271 If you put a single wild card, it works as before the modification.
272 */
273 //=======================================================================
274
275 wxString theFilter;
276 if ( wxStrlen(m_wildCard) == 0 )
277 theFilter = wxString(wxT("*.*"));
278 else
279 theFilter = m_wildCard ;
280 wxString filterBuffer;
281
282 if ( !wxStrchr( theFilter, wxT('|') ) ) { // only one filter ==> default text
283 filterBuffer.Printf(_("Files (%s)|%s"),
284 theFilter.c_str(), theFilter.c_str());
285 }
286 else { // more then one filter
287 filterBuffer = theFilter;
288
289 }
290
291 filterBuffer += wxT("|");
292 // Replace | with \0
293 for (i = 0; i < filterBuffer.Len(); i++ ) {
294 if ( filterBuffer.GetChar(i) == wxT('|') ) {
295 filterBuffer[i] = wxT('\0');
296 }
297 }
298
299 of.lpstrFilter = (LPTSTR)(const wxChar *)filterBuffer;
300 of.nFilterIndex = m_filterIndex;
301
302 //=== Setting defaultFileName >>=========================================
303
304 wxStrncpy( fileNameBuffer, (const wxChar *)m_fileName, MAXPATH-1 );
305 fileNameBuffer[ MAXPATH-1 ] = wxT('\0');
306
307 of.lpstrFile = fileNameBuffer; // holds returned filename
308 of.nMaxFile = MAXPATH;
309
310 //== Execute FileDialog >>=================================================
311
312 bool success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
313 : (GetOpenFileName(&of) != 0);
314
315 if ( success )
316 {
317 m_fileNames.Empty();
318
319 if ( ( m_dialogStyle & wxMULTIPLE ) &&
320 #if defined(OFN_EXPLORER)
321 ( fileNameBuffer[of.nFileOffset-1] == wxT('\0') ) )
322 #else
323 ( fileNameBuffer[of.nFileOffset-1] == wxT(' ') ) )
324 #endif // OFN_EXPLORER
325 {
326 #if defined(OFN_EXPLORER)
327 m_dir = fileNameBuffer;
328 i = of.nFileOffset;
329 m_fileName = &fileNameBuffer[i];
330 m_fileNames.Add(m_fileName);
331 i += m_fileName.Len() + 1;
332
333 while (fileNameBuffer[i] != wxT('\0'))
334 {
335 m_fileNames.Add(&fileNameBuffer[i]);
336 i += wxStrlen(&fileNameBuffer[i]) + 1;
337 }
338 #else
339 wxStringTokenizer toke(fileNameBuffer, " \t\r\n");
340 m_dir = toke.GetNextToken();
341 m_fileName = toke.GetNextToken();
342 m_fileNames.Add(m_fileName);
343
344 while (toke.HasMoreTokens())
345 m_fileNames.Add(toke.GetNextToken());
346 #endif // OFN_EXPLORER
347
348 wxString dir(m_dir);
349 if ( m_dir.Last() != _T('\\') )
350 dir += _T('\\');
351
352 m_fileNames.Sort();
353 m_path = dir + m_fileName;
354 }
355 else
356 {
357 const wxChar* extension = NULL;
358
359 //=== Adding the correct extension >>=================================
360
361 m_filterIndex = (int)of.nFilterIndex;
362
363 if ( of.nFileExtension && fileNameBuffer[ of.nFileExtension-1] != wxT('.') )
364 { // user has typed an filename
365 // without an extension:
366
367 int maxFilter = (int)(of.nFilterIndex*2L-1L);
368 extension = filterBuffer;
369
370 for( int i = 0; i < maxFilter; i++ ) { // get extension
371 extension = extension + wxStrlen( extension ) +1;
372 }
373
374 extension = wxStrrchr( extension, wxT('.') );
375 if ( extension // != "blabla"
376 && !wxStrrchr( extension, wxT('*') ) // != "blabla.*"
377 && !wxStrrchr( extension, wxT('?') ) // != "blabla.?"
378 && extension[1] // != "blabla."
379 && extension[1] != wxT(' ') ) // != "blabla. "
380 {
381 // now concat extension to the fileName:
382 m_fileName = wxString(fileNameBuffer) + extension;
383
384 int len = wxStrlen( fileNameBuffer );
385 wxStrncpy( fileNameBuffer + len, extension, MAXPATH - len );
386 fileNameBuffer[ MAXPATH -1 ] = wxT('\0');
387 }
388 }
389
390 m_path = fileNameBuffer;
391 m_fileName = wxFileNameFromPath(fileNameBuffer);
392 m_fileNames.Add(m_fileName);
393 m_dir = wxPathOnly(fileNameBuffer);
394 }
395
396
397 //=== Simulating the wxOVERWRITE_PROMPT >>============================
398
399 if ( (m_dialogStyle & wxOVERWRITE_PROMPT) &&
400 ::wxFileExists( fileNameBuffer ) )
401 {
402 wxString messageText;
403 messageText.Printf(_("Replace file '%s'?"), fileNameBuffer);
404
405 if ( wxMessageBox(messageText, m_message, wxYES_NO ) != wxYES )
406 {
407 success = FALSE;
408 }
409 }
410
411 }
412 else
413 {
414 // common dialog failed - why?
415 #ifdef __WXDEBUG__
416 DWORD dwErr = CommDlgExtendedError();
417 if ( dwErr != 0 )
418 {
419 // this msg is only for developers
420 wxLogError(wxT("Common dialog failed with error code %0lx."),
421 dwErr);
422 }
423 //else: it was just cancelled
424 #endif
425 }
426
427 return success ? wxID_OK : wxID_CANCEL;
428
429 }
430
431 // Generic file load/save dialog (for internal use only)
432 static
433 wxString wxDefaultFileSelector(bool load,
434 const wxChar *what,
435 const wxChar *extension,
436 const wxChar *default_name,
437 wxWindow *parent)
438 {
439 wxString prompt;
440 wxString str;
441 if (load) str = _("Load %s file");
442 else str = _("Save %s file");
443 prompt.Printf(str, what);
444
445 const wxChar *ext = extension;
446 if (*ext == wxT('.'))
447 ext++;
448
449 wxString wild;
450 wild.Printf(wxT("*.%s"), ext);
451
452 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
453 }
454
455 // Generic file load dialog
456 WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
457 const wxChar *extension,
458 const wxChar *default_name,
459 wxWindow *parent)
460 {
461 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
462 }
463
464 // Generic file save dialog
465 WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
466 const wxChar *extension,
467 const wxChar *default_name,
468 wxWindow *parent)
469 {
470 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
471 }
472
473