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