]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
f6bcfd97 | 2 | // Name: src/msw/filedlg.cpp |
2bda0e17 KB |
3 | // Purpose: wxFileDialog |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
f6bcfd97 BP |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
ba681060 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
3180bc0e | 27 | #if wxUSE_FILEDLG && !(defined(__SMARTPHONE__) && defined(__WXWINCE__)) |
1e6feb95 | 28 | |
949c9f74 WS |
29 | #include "wx/filedlg.h" |
30 | ||
2bda0e17 | 31 | #ifndef WX_PRECOMP |
57bd4c60 WS |
32 | #include "wx/msw/wrapcdlg.h" |
33 | #include "wx/msw/missing.h" | |
ba681060 VZ |
34 | #include "wx/utils.h" |
35 | #include "wx/msgdlg.h" | |
2b5f62a0 | 36 | #include "wx/filefn.h" |
ba681060 | 37 | #include "wx/intl.h" |
2662e49e | 38 | #include "wx/log.h" |
f6bcfd97 | 39 | #include "wx/app.h" |
18680f86 | 40 | #include "wx/math.h" |
8f177c8e | 41 | #endif |
2bda0e17 | 42 | |
2bda0e17 KB |
43 | #include <stdlib.h> |
44 | #include <string.h> | |
45 | ||
8ad9ca97 | 46 | #include "wx/filename.h" |
8f177c8e VZ |
47 | #include "wx/tokenzr.h" |
48 | ||
f6bcfd97 BP |
49 | // ---------------------------------------------------------------------------- |
50 | // constants | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | #ifdef __WIN32__ | |
2b5f62a0 | 54 | # define wxMAXPATH 65534 |
f6bcfd97 BP |
55 | #else |
56 | # define wxMAXPATH 1024 | |
57 | #endif | |
58 | ||
59 | # define wxMAXFILE 1024 | |
60 | ||
61 | # define wxMAXEXT 5 | |
62 | ||
0b11099d VZ |
63 | // ---------------------------------------------------------------------------- |
64 | // globals | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
0a7b0229 VZ |
67 | // standard dialog size for the old Windows systems where the dialog wasn't |
68 | // resizeable | |
0b11099d VZ |
69 | static wxRect gs_rectDialog(0, 0, 428, 266); |
70 | ||
f6bcfd97 BP |
71 | // ============================================================================ |
72 | // implementation | |
73 | // ============================================================================ | |
74 | ||
f74172ab | 75 | IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase) |
2bda0e17 | 76 | |
0b11099d VZ |
77 | // ---------------------------------------------------------------------------- |
78 | // hook function for moving the dialog | |
79 | // ---------------------------------------------------------------------------- | |
80 | ||
106d80ad | 81 | UINT_PTR APIENTRY |
0b11099d VZ |
82 | wxFileDialogHookFunction(HWND hDlg, |
83 | UINT iMsg, | |
84 | WPARAM WXUNUSED(wParam), | |
85 | LPARAM lParam) | |
86 | { | |
cb80db46 | 87 | switch ( iMsg ) |
0b11099d | 88 | { |
d6dae1b4 | 89 | #ifndef __WXWINCE__ |
6fa6d659 VZ |
90 | case WM_INITDIALOG: |
91 | { | |
92 | OPENFILENAME* ofn = reinterpret_cast<OPENFILENAME *>(lParam); | |
93 | reinterpret_cast<wxFileDialog *>(ofn->lCustData) | |
94 | ->MSWOnInitDialogHook((WXHWND)hDlg); | |
95 | } | |
96 | break; | |
d6dae1b4 | 97 | #endif // __WXWINCE__ |
6fa6d659 | 98 | |
0b11099d VZ |
99 | case WM_NOTIFY: |
100 | { | |
5c33522f | 101 | OFNOTIFY *pNotifyCode = reinterpret_cast<OFNOTIFY *>(lParam); |
cb80db46 | 102 | if ( pNotifyCode->hdr.code == CDN_INITDONE ) |
0b11099d | 103 | { |
5c33522f | 104 | reinterpret_cast<wxFileDialog *>( |
0a7b0229 VZ |
105 | pNotifyCode->lpOFN->lCustData) |
106 | ->MSWOnInitDone((WXHWND)hDlg); | |
0b11099d VZ |
107 | } |
108 | } | |
109 | break; | |
cb80db46 VZ |
110 | |
111 | case WM_DESTROY: | |
112 | // reuse the position used for the dialog the next time by default | |
113 | // | |
114 | // NB: at least under Windows 2003 this is useless as after the | |
115 | // first time it's shown the dialog always remembers its size | |
116 | // and position itself and ignores any later SetWindowPos calls | |
117 | wxCopyRECTToRect(wxGetWindowRect(::GetParent(hDlg)), gs_rectDialog); | |
118 | break; | |
0b11099d VZ |
119 | } |
120 | ||
121 | // do the default processing | |
122 | return 0; | |
123 | } | |
124 | ||
f6bcfd97 | 125 | // ---------------------------------------------------------------------------- |
b600ed13 | 126 | // wxFileDialog |
f6bcfd97 BP |
127 | // ---------------------------------------------------------------------------- |
128 | ||
2b5f62a0 VZ |
129 | wxFileDialog::wxFileDialog(wxWindow *parent, |
130 | const wxString& message, | |
131 | const wxString& defaultDir, | |
132 | const wxString& defaultFileName, | |
133 | const wxString& wildCard, | |
134 | long style, | |
ff3e84ff VZ |
135 | const wxPoint& pos, |
136 | const wxSize& sz, | |
137 | const wxString& name) | |
0b11099d | 138 | : wxFileDialogBase(parent, message, defaultDir, defaultFileName, |
ff3e84ff | 139 | wildCard, style, pos, sz, name) |
f74172ab | 140 | |
2bda0e17 | 141 | { |
556151f5 | 142 | // NB: all style checks are done by wxFileDialogBase::Create |
2bda0e17 | 143 | |
0b11099d | 144 | m_bMovedWindow = false; |
0a7b0229 | 145 | m_centreDir = 0; |
0b11099d VZ |
146 | |
147 | // Must set to zero, otherwise the wx routines won't size the window | |
148 | // the second time you call the file dialog, because it thinks it is | |
149 | // already at the requested size.. (when centering) | |
150 | gs_rectDialog.x = | |
151 | gs_rectDialog.y = 0; | |
0b11099d | 152 | } |
0a7b0229 | 153 | |
c61f4f6d VZ |
154 | void wxFileDialog::GetPaths(wxArrayString& paths) const |
155 | { | |
156 | paths.Empty(); | |
157 | ||
158 | wxString dir(m_dir); | |
159 | if ( m_dir.Last() != _T('\\') ) | |
160 | dir += _T('\\'); | |
161 | ||
162 | size_t count = m_fileNames.GetCount(); | |
163 | for ( size_t n = 0; n < count; n++ ) | |
164 | { | |
8ad9ca97 JS |
165 | if (wxFileName(m_fileNames[n]).IsAbsolute()) |
166 | paths.Add(m_fileNames[n]); | |
167 | else | |
168 | paths.Add(dir + m_fileNames[n]); | |
c61f4f6d VZ |
169 | } |
170 | } | |
171 | ||
89654c9a VZ |
172 | void wxFileDialog::GetFilenames(wxArrayString& files) const |
173 | { | |
174 | files = m_fileNames; | |
175 | } | |
176 | ||
2b5f62a0 VZ |
177 | void wxFileDialog::SetPath(const wxString& path) |
178 | { | |
179 | wxString ext; | |
bd365871 | 180 | wxFileName::SplitPath(path, &m_dir, &m_fileName, &ext); |
2b5f62a0 VZ |
181 | if ( !ext.empty() ) |
182 | m_fileName << _T('.') << ext; | |
183 | } | |
184 | ||
cb80db46 | 185 | void wxFileDialog::DoGetPosition(int *x, int *y) const |
0b11099d | 186 | { |
cb80db46 VZ |
187 | if ( x ) |
188 | *x = gs_rectDialog.x; | |
189 | if ( y ) | |
190 | *y = gs_rectDialog.y; | |
0b11099d VZ |
191 | } |
192 | ||
0b11099d VZ |
193 | void wxFileDialog::DoGetSize(int *width, int *height) const |
194 | { | |
cb80db46 VZ |
195 | if ( width ) |
196 | *width = gs_rectDialog.width; | |
197 | if ( height ) | |
198 | *height = gs_rectDialog.height; | |
0b11099d VZ |
199 | } |
200 | ||
cb80db46 | 201 | void wxFileDialog::DoMoveWindow(int x, int y, int WXUNUSED(w), int WXUNUSED(h)) |
0b11099d | 202 | { |
0b11099d VZ |
203 | gs_rectDialog.x = x; |
204 | gs_rectDialog.y = y; | |
205 | ||
0a7b0229 VZ |
206 | // our HWND is only set when we're called from MSWOnInitDone(), test if |
207 | // this is the case | |
208 | HWND hwnd = GetHwnd(); | |
209 | if ( hwnd ) | |
210 | { | |
211 | // size of the dialog can't be changed because the controls are not | |
212 | // laid out correctly then | |
213 | ::SetWindowPos(hwnd, HWND_TOP, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE); | |
214 | } | |
215 | else // just remember that we were requested to move the window | |
216 | { | |
217 | m_bMovedWindow = true; | |
218 | ||
219 | // if Centre() had been called before, it shouldn't be taken into | |
220 | // account now | |
221 | m_centreDir = 0; | |
222 | } | |
223 | } | |
224 | ||
225 | void wxFileDialog::DoCentre(int dir) | |
226 | { | |
227 | m_centreDir = dir; | |
228 | m_bMovedWindow = true; | |
229 | ||
230 | // it's unnecessary to do anything else at this stage as we'll redo it in | |
231 | // MSWOnInitDone() anyhow | |
232 | } | |
233 | ||
234 | void wxFileDialog::MSWOnInitDone(WXHWND hDlg) | |
235 | { | |
236 | // note the the dialog is the parent window: hDlg is a child of it when | |
237 | // OFN_EXPLORER is used | |
238 | HWND hFileDlg = ::GetParent((HWND)hDlg); | |
239 | ||
240 | // set HWND so that our DoMoveWindow() works correctly | |
241 | SetHWND((WXHWND)hFileDlg); | |
242 | ||
243 | if ( m_centreDir ) | |
244 | { | |
245 | // now we have the real dialog size, remember it | |
246 | RECT rect; | |
247 | GetWindowRect(hFileDlg, &rect); | |
248 | gs_rectDialog = wxRectFromRECT(rect); | |
249 | ||
250 | // and position the window correctly: notice that we must use the base | |
251 | // class version as our own doesn't do anything except setting flags | |
252 | wxFileDialogBase::DoCentre(m_centreDir); | |
253 | } | |
254 | else // need to just move it to the correct place | |
255 | { | |
256 | SetPosition(gs_rectDialog.GetPosition()); | |
257 | } | |
258 | ||
259 | // we shouldn't destroy this HWND | |
260 | SetHWND(NULL); | |
0b11099d VZ |
261 | } |
262 | ||
fe048d77 VZ |
263 | // helper used below in ShowCommFileDialog(): style is used to determine |
264 | // whether to show the "Save file" dialog (if it contains wxFD_SAVE bit) or | |
265 | // "Open file" one; returns true on success or false on failure in which case | |
266 | // err is filled with the CDERR_XXX constant | |
c46c1fb8 VZ |
267 | static bool DoShowCommFileDialog(OPENFILENAME *of, long style, DWORD *err) |
268 | { | |
e031f1df | 269 | if ( style & wxFD_SAVE ? GetSaveFileName(of) : GetOpenFileName(of) ) |
c46c1fb8 VZ |
270 | return true; |
271 | ||
272 | if ( err ) | |
273 | { | |
274 | #ifdef __WXWINCE__ | |
275 | // according to MSDN, CommDlgExtendedError() should work under CE as | |
276 | // well but apparently in practice it doesn't (anybody has more | |
277 | // details?) | |
278 | *err = GetLastError(); | |
279 | #else | |
280 | *err = CommDlgExtendedError(); | |
281 | #endif | |
282 | } | |
283 | ||
284 | return false; | |
285 | } | |
286 | ||
04227efc VZ |
287 | // We want to use OPENFILENAME struct version 5 (Windows 2000/XP) but we don't |
288 | // know if the OPENFILENAME declared in the currently used headers is a V5 or | |
289 | // V4 (smaller) one so we try to manually extend the struct in case it is the | |
290 | // old one. | |
5bb37216 | 291 | // |
04227efc VZ |
292 | // We don't do this on Windows CE nor under Win64, however, as there are no |
293 | // compilers with old headers for these architectures | |
294 | #if defined(__WXWINCE__) || defined(__WIN64__) | |
5bb37216 VZ |
295 | typedef OPENFILENAME wxOPENFILENAME; |
296 | ||
04227efc VZ |
297 | static const DWORD gs_ofStructSize = sizeof(OPENFILENAME); |
298 | #else // !__WXWINCE__ || __WIN64__ | |
299 | #define wxTRY_SMALLER_OPENFILENAME | |
300 | ||
5bb37216 VZ |
301 | struct wxOPENFILENAME : public OPENFILENAME |
302 | { | |
303 | // fields added in Windows 2000/XP comdlg32.dll version | |
304 | void *pVoid; | |
305 | DWORD dw1; | |
306 | DWORD dw2; | |
307 | }; | |
308 | ||
309 | // hardcoded sizeof(OPENFILENAME) in the Platform SDK: we have to do it | |
310 | // because sizeof(OPENFILENAME) in the headers we use when compiling the | |
311 | // library could be less if _WIN32_WINNT is not >= 0x500 | |
312 | static const DWORD wxOPENFILENAME_V5_SIZE = 88; | |
313 | ||
314 | // this is hardcoded sizeof(OPENFILENAME_NT4) from Platform SDK | |
315 | static const DWORD wxOPENFILENAME_V4_SIZE = 76; | |
5bb37216 | 316 | |
04227efc VZ |
317 | // always try the new one first |
318 | static DWORD gs_ofStructSize = wxOPENFILENAME_V5_SIZE; | |
319 | #endif // __WXWINCE__ || __WIN64__/!... | |
5bb37216 | 320 | |
fe048d77 VZ |
321 | static bool ShowCommFileDialog(OPENFILENAME *of, long style) |
322 | { | |
323 | DWORD errCode; | |
324 | bool success = DoShowCommFileDialog(of, style, &errCode); | |
325 | ||
326 | #ifdef wxTRY_SMALLER_OPENFILENAME | |
327 | // the system might be too old to support the new version file dialog | |
328 | // boxes, try with the old size | |
329 | if ( !success && errCode == CDERR_STRUCTSIZE && | |
330 | of->lStructSize != wxOPENFILENAME_V4_SIZE ) | |
331 | { | |
332 | of->lStructSize = wxOPENFILENAME_V4_SIZE; | |
333 | ||
334 | success = DoShowCommFileDialog(of, style, &errCode); | |
335 | ||
336 | if ( success || !errCode ) | |
337 | { | |
338 | // use this struct size for subsequent dialogs | |
339 | gs_ofStructSize = of->lStructSize; | |
340 | } | |
341 | } | |
342 | #endif // wxTRY_SMALLER_OPENFILENAME | |
343 | ||
d4380371 VZ |
344 | if ( !success && |
345 | // FNERR_INVALIDFILENAME is not defined under CE (besides we don't | |
346 | // use CommDlgExtendedError() there anyhow) | |
347 | #ifndef __WXWINCE__ | |
348 | errCode == FNERR_INVALIDFILENAME && | |
349 | #endif // !__WXWINCE__ | |
350 | of->lpstrFile[0] ) | |
fe048d77 VZ |
351 | { |
352 | // this can happen if the default file name is invalid, try without it | |
353 | // now | |
354 | of->lpstrFile[0] = _T('\0'); | |
355 | success = DoShowCommFileDialog(of, style, &errCode); | |
356 | } | |
357 | ||
358 | if ( !success ) | |
359 | { | |
360 | // common dialog failed - why? | |
361 | if ( errCode != 0 ) | |
362 | { | |
363 | wxLogError(_("File dialog failed with error code %0lx."), errCode); | |
364 | } | |
365 | //else: it was just cancelled | |
366 | ||
367 | return false; | |
368 | } | |
369 | ||
370 | return true; | |
371 | } | |
372 | ||
d6dae1b4 | 373 | #ifndef __WXWINCE__ |
6fa6d659 VZ |
374 | void wxFileDialog::MSWOnInitDialogHook(WXHWND hwnd) |
375 | { | |
376 | SetHWND(hwnd); | |
377 | ||
378 | CreateExtraControl(); | |
379 | ||
380 | SetHWND(NULL); | |
381 | } | |
d6dae1b4 | 382 | #endif // __WXWINCE__ |
6fa6d659 | 383 | |
c61f4f6d | 384 | int wxFileDialog::ShowModal() |
2bda0e17 | 385 | { |
1f2f0331 VZ |
386 | HWND hWnd = 0; |
387 | if (m_parent) hWnd = (HWND) m_parent->GetHWND(); | |
f6bcfd97 BP |
388 | if (!hWnd && wxTheApp->GetTopWindow()) |
389 | hWnd = (HWND) wxTheApp->GetTopWindow()->GetHWND(); | |
2bda0e17 | 390 | |
f6bcfd97 BP |
391 | static wxChar fileNameBuffer [ wxMAXPATH ]; // the file-name |
392 | wxChar titleBuffer [ wxMAXFILE+1+wxMAXEXT ]; // the file-name, without path | |
2bda0e17 | 393 | |
223d09f6 KB |
394 | *fileNameBuffer = wxT('\0'); |
395 | *titleBuffer = wxT('\0'); | |
2bda0e17 | 396 | |
21416306 | 397 | long msw_flags = OFN_HIDEREADONLY; |
21416306 | 398 | |
b014db05 | 399 | if ( HasFdFlag(wxFD_FILE_MUST_EXIST) ) |
1f2f0331 | 400 | msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; |
0b11099d VZ |
401 | /* |
402 | If the window has been moved the programmer is probably | |
403 | trying to center or position it. Thus we set the callback | |
404 | or hook function so that we can actually adjust the position. | |
405 | Without moving or centering the dlg, it will just stay | |
406 | in the upper left of the frame, it does not center | |
5bb37216 | 407 | automatically. |
0b11099d | 408 | */ |
6fa6d659 | 409 | if (m_bMovedWindow || HasExtraControlCreator()) // we need these flags. |
503528dc JS |
410 | { |
411 | msw_flags |= OFN_EXPLORER|OFN_ENABLEHOOK; | |
412 | #ifndef __WXWINCE__ | |
413 | msw_flags |= OFN_ENABLESIZING; | |
414 | #endif | |
415 | } | |
6e8aa701 | 416 | |
b014db05 | 417 | if ( HasFdFlag(wxFD_MULTIPLE) ) |
6e8aa701 VZ |
418 | { |
419 | // OFN_EXPLORER must always be specified with OFN_ALLOWMULTISELECT | |
420 | msw_flags |= OFN_EXPLORER | OFN_ALLOWMULTISELECT; | |
421 | } | |
422 | ||
e031f1df | 423 | // if wxFD_CHANGE_DIR flag is not given we shouldn't change the CWD which the |
af1f0a76 VZ |
424 | // standard dialog does by default (notice that under NT it does it anyhow, |
425 | // OFN_NOCHANGEDIR or not, see below) | |
b014db05 | 426 | if ( !HasFdFlag(wxFD_CHANGE_DIR) ) |
6e8aa701 VZ |
427 | { |
428 | msw_flags |= OFN_NOCHANGEDIR; | |
429 | } | |
ac95e671 | 430 | |
b014db05 | 431 | if ( HasFdFlag(wxFD_OVERWRITE_PROMPT) ) |
99d1b93d VZ |
432 | { |
433 | msw_flags |= OFN_OVERWRITEPROMPT; | |
434 | } | |
ac95e671 | 435 | |
5bb37216 | 436 | wxOPENFILENAME of; |
f6bcfd97 BP |
437 | wxZeroMemory(of); |
438 | ||
5bb37216 | 439 | of.lStructSize = gs_ofStructSize; |
e15e548b | 440 | of.hwndOwner = hWnd; |
e0a050e3 | 441 | of.lpstrTitle = m_message.wx_str(); |
e15e548b | 442 | of.lpstrFileTitle = titleBuffer; |
5bb37216 | 443 | of.nMaxFileTitle = wxMAXFILE + 1 + wxMAXEXT; |
2bda0e17 | 444 | |
6fa6d659 VZ |
445 | #ifndef __WXWINCE__ |
446 | GlobalPtr hgbl; | |
447 | if ( HasExtraControlCreator() ) | |
448 | { | |
449 | msw_flags |= OFN_ENABLETEMPLATEHANDLE; | |
450 | ||
451 | hgbl.Init(256, GMEM_ZEROINIT); | |
452 | GlobalPtrLock hgblLock(hgbl); | |
453 | LPDLGTEMPLATE lpdt = static_cast<LPDLGTEMPLATE>(hgblLock.Get()); | |
454 | ||
455 | // Define a dialog box. | |
456 | ||
457 | lpdt->style = DS_CONTROL | WS_CHILD | WS_CLIPSIBLINGS; | |
458 | lpdt->cdit = 0; // Number of controls | |
459 | lpdt->x = 0; | |
460 | lpdt->y = 0; | |
461 | ||
462 | wxSize extra_size = GetExtraControlSize(); | |
463 | // setting cx doesn't change the width of the dialog | |
464 | lpdt->cx = extra_size.GetWidth(); | |
465 | // Dividing by 2 gives expected height on WinXP and Wine. | |
466 | // I don't know why (MW). | |
467 | lpdt->cy = extra_size.GetHeight() / 2; | |
468 | ||
469 | // after the DLGTEMPLATE there are 3 additional WORDs for dialog menu, | |
470 | // class and title, all three set to zeros. | |
471 | ||
472 | of.hInstance = (HINSTANCE)lpdt; | |
473 | } | |
474 | #endif // __WXWINCE__ | |
475 | ||
0bc9b25e | 476 | // Convert forward slashes to backslashes (file selector doesn't like |
99d1b93d VZ |
477 | // forward slashes) and also squeeze multiple consecutive slashes into one |
478 | // as it doesn't like two backslashes in a row neither | |
0627d091 | 479 | |
cbe874bd WS |
480 | wxString dir; |
481 | size_t i, len = m_dir.length(); | |
99d1b93d | 482 | dir.reserve(len); |
0627d091 | 483 | for ( i = 0; i < len; i++ ) |
99d1b93d VZ |
484 | { |
485 | wxChar ch = m_dir[i]; | |
486 | switch ( ch ) | |
487 | { | |
488 | case _T('/'): | |
489 | // convert to backslash | |
490 | ch = _T('\\'); | |
491 | ||
492 | // fall through | |
0bc9b25e | 493 | |
99d1b93d VZ |
494 | case _T('\\'): |
495 | while ( i < len - 1 ) | |
496 | { | |
497 | wxChar chNext = m_dir[i + 1]; | |
498 | if ( chNext != _T('\\') && chNext != _T('/') ) | |
499 | break; | |
500 | ||
04d93c3a CE |
501 | // ignore the next one, unless it is at the start of a UNC path |
502 | if (i > 0) | |
503 | i++; | |
504 | else | |
0b11099d | 505 | break; |
99d1b93d VZ |
506 | } |
507 | // fall through | |
508 | ||
509 | default: | |
510 | // normal char | |
511 | dir += ch; | |
512 | } | |
513 | } | |
514 | ||
515 | of.lpstrInitialDir = dir.c_str(); | |
2bda0e17 | 516 | |
e15e548b | 517 | of.Flags = msw_flags; |
0b11099d | 518 | of.lpfnHook = wxFileDialogHookFunction; |
0a7b0229 | 519 | of.lCustData = (LPARAM)this; |
2bda0e17 | 520 | |
daf32463 | 521 | wxArrayString wildDescriptions, wildFilters; |
2bda0e17 | 522 | |
daf32463 | 523 | size_t items = wxParseCommonDialogsFilter(m_wildCard, wildDescriptions, wildFilters); |
2bda0e17 | 524 | |
daf32463 | 525 | wxASSERT_MSG( items > 0 , _T("empty wildcard list") ); |
2bda0e17 | 526 | |
1f2f0331 | 527 | wxString filterBuffer; |
2bda0e17 | 528 | |
daf32463 WS |
529 | for (i = 0; i < items ; i++) |
530 | { | |
531 | filterBuffer += wildDescriptions[i]; | |
532 | filterBuffer += wxT("|"); | |
533 | filterBuffer += wildFilters[i]; | |
534 | filterBuffer += wxT("|"); | |
574c0bbf JS |
535 | } |
536 | ||
574c0bbf | 537 | // Replace | with \0 |
e031f1df | 538 | for (i = 0; i < filterBuffer.length(); i++ ) { |
223d09f6 KB |
539 | if ( filterBuffer.GetChar(i) == wxT('|') ) { |
540 | filterBuffer[i] = wxT('\0'); | |
e15e548b VZ |
541 | } |
542 | } | |
2bda0e17 | 543 | |
c9f78968 | 544 | of.lpstrFilter = (LPTSTR)filterBuffer.wx_str(); |
cc42eb7a | 545 | of.nFilterIndex = m_filterIndex + 1; |
2bda0e17 KB |
546 | |
547 | //=== Setting defaultFileName >>========================================= | |
548 | ||
64accea5 | 549 | wxStrlcpy(fileNameBuffer, m_fileName.c_str(), WXSIZEOF(fileNameBuffer)); |
2bda0e17 | 550 | |
e15e548b | 551 | of.lpstrFile = fileNameBuffer; // holds returned filename |
f6bcfd97 | 552 | of.nMaxFile = wxMAXPATH; |
2bda0e17 | 553 | |
90bddb85 | 554 | // we must set the default extension because otherwise Windows would check |
e031f1df | 555 | // for the existing of a wrong file with wxFD_OVERWRITE_PROMPT (i.e. if the |
90bddb85 VZ |
556 | // user types "foo" and the default extension is ".bar" we should force it |
557 | // to check for "foo.bar" existence and not "foo") | |
558 | wxString defextBuffer; // we need it to be alive until GetSaveFileName()! | |
b014db05 | 559 | if (HasFdFlag(wxFD_SAVE)) |
90bddb85 | 560 | { |
e0a050e3 | 561 | const wxChar* extension = filterBuffer.wx_str(); |
90bddb85 VZ |
562 | int maxFilter = (int)(of.nFilterIndex*2L) - 1; |
563 | ||
564 | for( int i = 0; i < maxFilter; i++ ) // get extension | |
565 | extension = extension + wxStrlen( extension ) + 1; | |
566 | ||
567 | // use dummy name a to avoid assert in AppendExtension | |
568 | defextBuffer = AppendExtension(wxT("a"), extension); | |
569 | if (defextBuffer.StartsWith(wxT("a."))) | |
570 | { | |
026ff75b | 571 | defextBuffer = defextBuffer.Mid(2); // remove "a." |
90bddb85 VZ |
572 | of.lpstrDefExt = defextBuffer.c_str(); |
573 | } | |
574 | } | |
0b11099d | 575 | |
af1f0a76 VZ |
576 | // store off before the standard windows dialog can possibly change it |
577 | const wxString cwdOrig = wxGetCwd(); | |
578 | ||
2bda0e17 KB |
579 | //== Execute FileDialog >>================================================= |
580 | ||
fe048d77 VZ |
581 | if ( !ShowCommFileDialog(&of, m_windowStyle) ) |
582 | return wxID_CANCEL; | |
2bda0e17 | 583 | |
fe048d77 VZ |
584 | // GetOpenFileName will always change the current working directory on |
585 | // (according to MSDN) "Windows NT 4.0/2000/XP" because the flag | |
586 | // OFN_NOCHANGEDIR has no effect. If the user did not specify | |
587 | // wxFD_CHANGE_DIR let's restore the current working directory to what it | |
588 | // was before the dialog was shown. | |
589 | if ( msw_flags & OFN_NOCHANGEDIR ) | |
f6bcfd97 | 590 | { |
fe048d77 | 591 | wxSetWorkingDirectory(cwdOrig); |
f6bcfd97 | 592 | } |
c46c1fb8 | 593 | |
fe048d77 | 594 | m_fileNames.Empty(); |
c61f4f6d | 595 | |
fe048d77 | 596 | if ( ( HasFdFlag(wxFD_MULTIPLE) ) && |
c61f4f6d | 597 | #if defined(OFN_EXPLORER) |
fe048d77 | 598 | ( fileNameBuffer[of.nFileOffset-1] == wxT('\0') ) |
c61f4f6d | 599 | #else |
fe048d77 | 600 | ( fileNameBuffer[of.nFileOffset-1] == wxT(' ') ) |
c61f4f6d | 601 | #endif // OFN_EXPLORER |
fe048d77 VZ |
602 | ) |
603 | { | |
c61f4f6d | 604 | #if defined(OFN_EXPLORER) |
fe048d77 VZ |
605 | m_dir = fileNameBuffer; |
606 | i = of.nFileOffset; | |
607 | m_fileName = &fileNameBuffer[i]; | |
608 | m_fileNames.Add(m_fileName); | |
609 | i += m_fileName.length() + 1; | |
c61f4f6d | 610 | |
fe048d77 VZ |
611 | while (fileNameBuffer[i] != wxT('\0')) |
612 | { | |
613 | m_fileNames.Add(&fileNameBuffer[i]); | |
614 | i += wxStrlen(&fileNameBuffer[i]) + 1; | |
615 | } | |
c61f4f6d | 616 | #else |
fe048d77 VZ |
617 | wxStringTokenizer toke(fileNameBuffer, _T(" \t\r\n")); |
618 | m_dir = toke.GetNextToken(); | |
619 | m_fileName = toke.GetNextToken(); | |
620 | m_fileNames.Add(m_fileName); | |
c61f4f6d | 621 | |
fe048d77 VZ |
622 | while (toke.HasMoreTokens()) |
623 | m_fileNames.Add(toke.GetNextToken()); | |
c61f4f6d VZ |
624 | #endif // OFN_EXPLORER |
625 | ||
fe048d77 VZ |
626 | wxString dir(m_dir); |
627 | if ( m_dir.Last() != _T('\\') ) | |
628 | dir += _T('\\'); | |
2bda0e17 | 629 | |
fe048d77 VZ |
630 | m_path = dir + m_fileName; |
631 | m_filterIndex = (int)of.nFilterIndex - 1; | |
632 | } | |
633 | else | |
634 | { | |
635 | //=== Adding the correct extension >>================================= | |
2bda0e17 | 636 | |
fe048d77 | 637 | m_filterIndex = (int)of.nFilterIndex - 1; |
2bda0e17 | 638 | |
fe048d77 VZ |
639 | if ( !of.nFileExtension || |
640 | (of.nFileExtension && fileNameBuffer[of.nFileExtension] == wxT('\0')) ) | |
641 | { | |
642 | // User has typed a filename without an extension: | |
643 | const wxChar* extension = filterBuffer.wx_str(); | |
644 | int maxFilter = (int)(of.nFilterIndex*2L) - 1; | |
a039ccbf | 645 | |
fe048d77 VZ |
646 | for( int i = 0; i < maxFilter; i++ ) // get extension |
647 | extension = extension + wxStrlen( extension ) + 1; | |
2bda0e17 | 648 | |
fe048d77 | 649 | m_fileName = AppendExtension(fileNameBuffer, extension); |
e408bf52 | 650 | wxStrlcpy(fileNameBuffer, m_fileName.c_str(), WXSIZEOF(fileNameBuffer)); |
c61f4f6d | 651 | } |
fe048d77 VZ |
652 | |
653 | m_path = fileNameBuffer; | |
654 | m_fileName = wxFileNameFromPath(fileNameBuffer); | |
655 | m_fileNames.Add(m_fileName); | |
656 | m_dir = wxPathOnly(fileNameBuffer); | |
7cc98b3e | 657 | } |
2bda0e17 | 658 | |
fe048d77 | 659 | return wxID_OK; |
2bda0e17 KB |
660 | |
661 | } | |
662 | ||
3180bc0e | 663 | #endif // wxUSE_FILEDLG && !(__SMARTPHONE__ && __WXWINCE__) |