]> git.saurik.com Git - wxWidgets.git/blame - src/msw/filedlg.cpp
removed pnghand from MSW sources - obsoleted
[wxWidgets.git] / src / msw / filedlg.cpp
CommitLineData
2bda0e17
KB
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
e15e548b 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
ba681060 13 #pragma implementation "filedlg.h"
2bda0e17
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
ba681060 20 #pragma hdrstop
2bda0e17
KB
21#endif
22
23#ifndef WX_PRECOMP
ba681060
VZ
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"
2662e49e 31 #include "wx/log.h"
2bda0e17
KB
32#endif
33
34#include <windows.h>
35
5ea105e0 36#if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
ba681060 37 #include <commdlg.h>
2bda0e17
KB
38#endif
39
40#include "wx/msw/private.h"
41
42#include <math.h>
43#include <stdlib.h>
44#include <string.h>
45
2bda0e17 46#if !USE_SHARED_LIBRARY
ba681060 47 IMPLEMENT_CLASS(wxFileDialog, wxDialog)
2bda0e17
KB
48#endif
49
837e5743
OK
50wxString wxFileSelector(const wxChar *title,
51 const wxChar *defaultDir,
52 const wxChar *defaultFileName,
53 const wxChar *defaultExtension,
54 const wxChar *filter,
ba681060
VZ
55 int flags,
56 wxWindow *parent,
57 int x, int y)
2bda0e17 58{
1f2f0331
VZ
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;
e15e548b 72 if ( defaultExtension && !filter )
223d09f6 73 filter2 = wxString(wxT("*.")) + defaultExtension;
e15e548b
VZ
74 else if ( filter )
75 filter2 = filter;
76
77 wxString defaultDirString;
78 if (defaultDir)
79 defaultDirString = defaultDir;
e15e548b
VZ
80
81 wxString defaultFilenameString;
82 if (defaultFileName)
83 defaultFilenameString = defaultFileName;
1f2f0331
VZ
84
85 wxFileDialog fileDialog(parent, title, defaultDirString,
86 defaultFilenameString, filter2,
87 flags, wxPoint(x, y));
837e5743 88 if( wxStrlen(defaultExtension) != 0 )
1f2f0331
VZ
89 {
90 int filterFind = 1,
91 filterIndex = 0;
92
93 for( unsigned int i = 0; i < filter2.Len(); i++ )
94 {
223d09f6 95 if( filter2.GetChar(i) == wxT('|') )
1f2f0331
VZ
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 {
223d09f6 104 if(filter2[i] == wxT('|'))
1f2f0331
VZ
105 break;
106 }
107
108 if( i-is-1 > 0 && is+1 < filter2.Len() )
109 {
574c0bbf
JS
110 if( filter2.Mid(is+1,i-is-1).Contains(defaultExtension) )
111// if( filter2.Mid(is+1,i-is-1) == defaultExtension )
1f2f0331
VZ
112 {
113 filterFind = filterIndex;
114 break;
115 }
116 }
117 }
118 }
119
120 fileDialog.SetFilterIndex(filterFind);
121 }
122
e15e548b 123 if ( fileDialog.ShowModal() == wxID_OK )
1f2f0331 124 {
837e5743 125 wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath());
e15e548b 126 return wxBuffer;
1f2f0331 127 }
e15e548b 128 else
ba681060 129 return wxGetEmptyString();
2bda0e17
KB
130}
131
132# if __BORLANDC__
1f2f0331 133# include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
2bda0e17
KB
134# endif
135
136# ifndef MAXPATH
1f2f0331 137# define MAXPATH 400
2bda0e17
KB
138# endif
139
140# ifndef MAXDRIVE
141# define MAXDRIVE 3
142# endif
143
1f2f0331 144# ifndef MAXFILE
2bda0e17
KB
145# define MAXFILE 9
146# endif
147
148# ifndef MAXEXT
149# define MAXEXT 5
150# endif
151
152
837e5743
OK
153wxString wxFileSelectorEx(const wxChar *title,
154 const wxChar *defaultDir,
155 const wxChar *defaultFileName,
e15e548b 156 int* defaultFilterIndex,
837e5743 157 const wxChar *filter,
e15e548b
VZ
158 int flags,
159 wxWindow* parent,
160 int x,
161 int y)
2bda0e17
KB
162
163{
223d09f6
KB
164 wxFileDialog fileDialog(parent, title ? title : wxT(""), defaultDir ? defaultDir : wxT(""),
165 defaultFileName ? defaultFileName : wxT(""), filter ? filter : wxT(""), flags, wxPoint(x, y));
2bda0e17 166
e15e548b
VZ
167 if ( fileDialog.ShowModal() == wxID_OK )
168 {
169 *defaultFilterIndex = fileDialog.GetFilterIndex();
837e5743 170 wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath());
e15e548b
VZ
171 return wxBuffer;
172 }
173 else
ba681060 174 return wxGetEmptyString();
2bda0e17
KB
175}
176
177wxFileDialog::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;
c61f4f6d
VZ
183 if ( ( m_dialogStyle & wxMULTIPLE ) && ( m_dialogStyle & wxSAVE ) )
184 m_dialogStyle &= ~wxMULTIPLE;
2bda0e17 185 m_parent = parent;
223d09f6 186 m_path = wxT("");
e15e548b
VZ
187 m_fileName = defaultFileName;
188 m_dir = defaultDir;
189 m_wildCard = wildCard;
c61f4f6d 190 m_filterIndex = 1;
2bda0e17
KB
191}
192
c61f4f6d
VZ
193void 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
208int wxFileDialog::ShowModal()
2bda0e17 209{
1f2f0331
VZ
210 HWND hWnd = 0;
211 if (m_parent) hWnd = (HWND) m_parent->GetHWND();
2bda0e17 212
837e5743
OK
213 static wxChar fileNameBuffer [ MAXPATH ]; // the file-name
214 wxChar titleBuffer [ MAXFILE+1+MAXEXT ]; // the file-name, without path
2bda0e17 215
223d09f6
KB
216 *fileNameBuffer = wxT('\0');
217 *titleBuffer = wxT('\0');
2bda0e17 218
2bda0e17 219 long msw_flags = 0;
e15e548b 220 if ( (m_dialogStyle & wxHIDE_READONLY) || (m_dialogStyle & wxSAVE) )
1f2f0331 221 msw_flags |= OFN_HIDEREADONLY;
e15e548b 222 if ( m_dialogStyle & wxFILE_MUST_EXIST )
1f2f0331 223 msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
c61f4f6d
VZ
224 if (m_dialogStyle & wxMULTIPLE )
225 msw_flags |=
226#if defined(OFN_EXPLORER)
227 OFN_EXPLORER |
228#endif // OFN_EXPLORER
229 OFN_ALLOWMULTISELECT;
2bda0e17 230
e15e548b
VZ
231 OPENFILENAME of;
232 memset(&of, 0, sizeof(OPENFILENAME));
2bda0e17
KB
233
234 of.lpstrCustomFilter = NULL; // system should not save custom filter
e15e548b 235 of.nMaxCustFilter = 0L;
2bda0e17 236
e15e548b
VZ
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
2bda0e17 240
e15e548b
VZ
241 of.lStructSize = sizeof(OPENFILENAME);
242 of.hwndOwner = hWnd;
837e5743 243 of.lpstrTitle = WXSTRINGCAST m_message;
2bda0e17
KB
244
245
e15e548b
VZ
246 of.lpstrFileTitle = titleBuffer;
247 of.nMaxFileTitle = MAXFILE + 1 + MAXEXT; // Windows 3.0 and 3.1
2bda0e17 248
0bc9b25e
JS
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++)
223d09f6
KB
254 if (m_dir[i] == wxT('/'))
255 m_dir[i] = wxT('\\');
0bc9b25e 256
837e5743 257 of.lpstrInitialDir = m_dir.c_str();
2bda0e17 258
e15e548b 259 of.Flags = msw_flags;
2bda0e17
KB
260
261
2bda0e17
KB
262 //=== Like Alejandro Sierra's wildcard modification >>===================
263 /*
1f2f0331
VZ
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.
2bda0e17 268
1f2f0331 269 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
2bda0e17 270
1f2f0331
VZ
271 If you put a single wild card, it works as before the modification.
272 */
2bda0e17
KB
273 //=======================================================================
274
4dba84be 275 wxString theFilter;
837e5743 276 if ( wxStrlen(m_wildCard) == 0 )
223d09f6 277 theFilter = wxString(wxT("*.*"));
4dba84be
JS
278 else
279 theFilter = m_wildCard ;
1f2f0331 280 wxString filterBuffer;
2bda0e17 281
223d09f6 282 if ( !wxStrchr( theFilter, wxT('|') ) ) { // only one filter ==> default text
1f2f0331
VZ
283 filterBuffer.Printf(_("Files (%s)|%s"),
284 theFilter.c_str(), theFilter.c_str());
e15e548b 285 }
1f2f0331
VZ
286 else { // more then one filter
287 filterBuffer = theFilter;
2bda0e17 288
574c0bbf
JS
289 }
290
223d09f6 291 filterBuffer += wxT("|");
574c0bbf 292 // Replace | with \0
0bc9b25e 293 for (i = 0; i < filterBuffer.Len(); i++ ) {
223d09f6
KB
294 if ( filterBuffer.GetChar(i) == wxT('|') ) {
295 filterBuffer[i] = wxT('\0');
e15e548b
VZ
296 }
297 }
2bda0e17 298
837e5743 299 of.lpstrFilter = (LPTSTR)(const wxChar *)filterBuffer;
c61f4f6d 300 of.nFilterIndex = m_filterIndex;
2bda0e17
KB
301
302 //=== Setting defaultFileName >>=========================================
303
837e5743 304 wxStrncpy( fileNameBuffer, (const wxChar *)m_fileName, MAXPATH-1 );
223d09f6 305 fileNameBuffer[ MAXPATH-1 ] = wxT('\0');
2bda0e17 306
e15e548b
VZ
307 of.lpstrFile = fileNameBuffer; // holds returned filename
308 of.nMaxFile = MAXPATH;
2bda0e17
KB
309
310 //== Execute FileDialog >>=================================================
311
1f2f0331
VZ
312 bool success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
313 : (GetOpenFileName(&of) != 0);
2bda0e17
KB
314
315 if ( success )
316 {
c61f4f6d
VZ
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;
1f2f0331 358
c61f4f6d 359 //=== Adding the correct extension >>=================================
2bda0e17 360
c61f4f6d 361 m_filterIndex = (int)of.nFilterIndex;
2bda0e17 362
c61f4f6d
VZ
363 if ( of.nFileExtension && fileNameBuffer[ of.nFileExtension-1] != wxT('.') )
364 { // user has typed an filename
365 // without an extension:
2bda0e17 366
c61f4f6d
VZ
367 int maxFilter = (int)(of.nFilterIndex*2L-1L);
368 extension = filterBuffer;
2bda0e17 369
c61f4f6d
VZ
370 for( int i = 0; i < maxFilter; i++ ) { // get extension
371 extension = extension + wxStrlen( extension ) +1;
372 }
2bda0e17 373
c61f4f6d
VZ
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;
2bda0e17 383
c61f4f6d
VZ
384 int len = wxStrlen( fileNameBuffer );
385 wxStrncpy( fileNameBuffer + len, extension, MAXPATH - len );
386 fileNameBuffer[ MAXPATH -1 ] = wxT('\0');
387 }
2bda0e17 388 }
2bda0e17 389
c61f4f6d
VZ
390 m_path = fileNameBuffer;
391 m_fileName = wxFileNameFromPath(fileNameBuffer);
392 m_fileNames.Add(m_fileName);
393 m_dir = wxPathOnly(fileNameBuffer);
394 }
2bda0e17
KB
395
396
e15e548b 397 //=== Simulating the wxOVERWRITE_PROMPT >>============================
2bda0e17 398
1f2f0331
VZ
399 if ( (m_dialogStyle & wxOVERWRITE_PROMPT) &&
400 ::wxFileExists( fileNameBuffer ) )
2bda0e17 401 {
1f2f0331
VZ
402 wxString messageText;
403 messageText.Printf(_("Replace file '%s'?"), fileNameBuffer);
2bda0e17 404
1f2f0331 405 if ( wxMessageBox(messageText, m_message, wxYES_NO ) != wxYES )
2bda0e17
KB
406 {
407 success = FALSE;
e15e548b 408 }
e15e548b 409 }
2bda0e17 410
7cc98b3e
VZ
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
223d09f6 420 wxLogError(wxT("Common dialog failed with error code %0lx."),
7cc98b3e
VZ
421 dwErr);
422 }
423 //else: it was just cancelled
424#endif
425 }
2bda0e17 426
7cc98b3e 427 return success ? wxID_OK : wxID_CANCEL;
2bda0e17
KB
428
429}
430
ba681060
VZ
431// Generic file load/save dialog (for internal use only)
432static
433wxString wxDefaultFileSelector(bool load,
837e5743
OK
434 const wxChar *what,
435 const wxChar *extension,
436 const wxChar *default_name,
ba681060 437 wxWindow *parent)
2bda0e17 438{
1f2f0331 439 wxString prompt;
837e5743
OK
440 wxString str;
441 if (load) str = _("Load %s file");
442 else str = _("Save %s file");
1f2f0331
VZ
443 prompt.Printf(str, what);
444
837e5743 445 const wxChar *ext = extension;
223d09f6 446 if (*ext == wxT('.'))
1f2f0331 447 ext++;
2bda0e17 448
1f2f0331 449 wxString wild;
223d09f6 450 wild.Printf(wxT("*.%s"), ext);
2bda0e17
KB
451
452 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
453}
454
455// Generic file load dialog
837e5743
OK
456WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
457 const wxChar *extension,
458 const wxChar *default_name,
ba681060 459 wxWindow *parent)
2bda0e17 460{
ba681060 461 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
2bda0e17
KB
462}
463
2bda0e17 464// Generic file save dialog
837e5743
OK
465WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
466 const wxChar *extension,
467 const wxChar *default_name,
ba681060 468 wxWindow *parent)
2bda0e17 469{
ba681060 470 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
2bda0e17
KB
471}
472
c61f4f6d 473