]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dirdlg.cpp
Add virtual ~wxAnyScrollHelperBase() to fix compiler warning.
[wxWidgets.git] / src / msw / dirdlg.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
a71d815b 2// Name: src/msw/dirdlg.cpp
2bda0e17
KB
3// Purpose: wxDirDialog
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
6c9a19aa 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
2bda0e17
KB
9/////////////////////////////////////////////////////////////////////////////
10
3f2711d5
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
1e6feb95 18
2bda0e17
KB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
3f2711d5 23 #pragma hdrstop
2bda0e17
KB
24#endif
25
08bce501 26#if wxUSE_DIRDLG
1e6feb95 27
141d782d
VZ
28#if wxUSE_OLE && !defined(__GNUWIN32_OLD__) && (!defined(__WXWINCE__) || \
29 (defined(__HANDHELDPC__) && (_WIN32_WCE >= 500)))
54744d3a 30
1ab440bc 31#include "wx/dirdlg.h"
691745ab 32#include "wx/modalhook.h"
1ab440bc 33
2bda0e17 34#ifndef WX_PRECOMP
3f2711d5
VZ
35 #include "wx/utils.h"
36 #include "wx/dialog.h"
77902671 37 #include "wx/log.h"
15ad98b7 38 #include "wx/app.h" // for GetComCtl32Version()
2bda0e17
KB
39#endif
40
3f2711d5 41#include "wx/msw/private.h"
6bb09706 42#include "wx/msw/wrapshl.h"
580ffdf4
VZ
43#include "wx/msw/private/comptr.h"
44#include "wx/dynlib.h"
45
46#include <initguid.h>
47
48// We can only use IFileDialog under desktop Windows and we need
49// wxDynamicLibrary for it.
50#if wxUSE_DYNLIB_CLASS && !defined(__WXWINCE__)
51 #define wxUSE_IFILEDIALOG 1
52#else
5fce622a 53 #define wxUSE_IFILEDIALOG 0
580ffdf4
VZ
54#endif
55
56#if wxUSE_IFILEDIALOG
57// IFileDialog related declarations missing from some compilers headers.
58
59// IShellItem
60#ifndef __IShellItem_INTERFACE_DEFINED__
61
62#ifndef SIGDN_FILESYSPATH
63 #define SIGDN_FILESYSPATH 0x80058000
64#endif
65
66struct IShellItem : public IUnknown
67{
68 virtual HRESULT wxSTDCALL BindToHandler(IBindCtx*, REFGUID, REFIID, void**) = 0;
69 virtual HRESULT wxSTDCALL GetParent(IShellItem**) = 0;
70 virtual HRESULT wxSTDCALL GetDisplayName(DWORD, LPWSTR*) = 0;
71 virtual HRESULT wxSTDCALL GetAttributes(ULONG, ULONG*) = 0;
72 virtual HRESULT wxSTDCALL Compare(IShellItem*, DWORD, int*) = 0;
73};
74
f797518e
VZ
75#endif // #ifndef __IShellItem_INTERFACE_DEFINED__
76
77// Define this GUID in any case, even when __IShellItem_INTERFACE_DEFINED__ is
78// defined in the headers we might still not have it in the actual uuid.lib,
79// this happens with at least VC7 used with its original (i.e. not updated) SDK
80// and there is no harm in defining the GUID unconditionally.
580ffdf4
VZ
81DEFINE_GUID(IID_IShellItem,
82 0x43826D1E, 0xE718, 0x42EE, 0xBC, 0x55, 0xA1, 0xE2, 0x61, 0xC3, 0x7B, 0xFE);
83
580ffdf4
VZ
84struct IShellItemFilter;
85struct IFileDialogEvents;
86
87// IModalWindow
88#ifndef __IModalWindow_INTERFACE_DEFINED__
89
90struct IModalWindow : public IUnknown
91{
92 virtual HRESULT wxSTDCALL Show(HWND) = 0;
93};
94
95#endif // #ifndef __IModalWindow_INTERFACE_DEFINED__
96
97// IFileDialog
98#ifndef __IFileDialog_INTERFACE_DEFINED__
99
100#ifndef FOS_PICKFOLDERS
101 #define FOS_PICKFOLDERS 0x20
102#endif
103
104#ifndef FOS_FORCEFILESYSTEM
105 #define FOS_FORCEFILESYSTEM 0x40
106#endif
107
108struct COMDLG_FILTERSPEC;
109
110struct IFileDialog : public IModalWindow
111{
112 virtual HRESULT wxSTDCALL SetFileTypes(UINT, const COMDLG_FILTERSPEC*) = 0;
113 virtual HRESULT wxSTDCALL SetFileTypeIndex(UINT) = 0;
114 virtual HRESULT wxSTDCALL GetFileTypeIndex(UINT*) = 0;
115 virtual HRESULT wxSTDCALL Advise(IFileDialogEvents*, DWORD*) = 0;
116 virtual HRESULT wxSTDCALL Unadvise(DWORD) = 0;
117 virtual HRESULT wxSTDCALL SetOptions(DWORD) = 0;
118 virtual HRESULT wxSTDCALL GetOptions(DWORD*) = 0;
119 virtual HRESULT wxSTDCALL SetDefaultFolder(IShellItem*) = 0;
120 virtual HRESULT wxSTDCALL SetFolder(IShellItem*) = 0;
121 virtual HRESULT wxSTDCALL GetFolder(IShellItem**) = 0;
122 virtual HRESULT wxSTDCALL GetCurrentSelection(IShellItem**) = 0;
123 virtual HRESULT wxSTDCALL SetFileName(LPCWSTR) = 0;
124 virtual HRESULT wxSTDCALL GetFileName(LPWSTR*) = 0;
125 virtual HRESULT wxSTDCALL SetTitle(LPCWSTR) = 0;
126 virtual HRESULT wxSTDCALL SetOkButtonLabel(LPCWSTR) = 0;
127 virtual HRESULT wxSTDCALL SetFileNameLabel(LPCWSTR) = 0;
128 virtual HRESULT wxSTDCALL GetResult(IShellItem**) = 0;
129 virtual HRESULT wxSTDCALL AddPlace(IShellItem*, DWORD) = 0;
130 virtual HRESULT wxSTDCALL SetDefaultExtension(LPCWSTR) = 0;
131 virtual HRESULT wxSTDCALL Close(HRESULT) = 0;
132 virtual HRESULT wxSTDCALL SetClientGuid(REFGUID) = 0;
133 virtual HRESULT wxSTDCALL ClearClientData() = 0;
134 virtual HRESULT wxSTDCALL SetFilter(IShellItemFilter*) = 0;
135};
136
137DEFINE_GUID(CLSID_FileOpenDialog,
138 0xDC1C5A9C, 0xE88A, 0x4dde, 0xA5, 0xA1, 0x60, 0xF8, 0x2A, 0x20, 0xAE, 0xF7);
139
140DEFINE_GUID(IID_IFileDialog,
141 0x42F85136, 0xDB7E, 0x439C, 0x85, 0xF1, 0xE4, 0x07, 0x5D, 0x13, 0x5F, 0xC8);
142
143#endif // #ifndef __IFileDialog_INTERFACE_DEFINED__
144
145#endif // wxUSE_IFILEDIALOG
2bda0e17 146
3f2711d5
VZ
147// ----------------------------------------------------------------------------
148// constants
149// ----------------------------------------------------------------------------
2bda0e17 150
cf54972e
VZ
151#ifndef BIF_NEWDIALOGSTYLE
152 #define BIF_NEWDIALOGSTYLE 0x0040
153#endif
154
155#ifndef BIF_NONEWFOLDERBUTTON
156 #define BIF_NONEWFOLDERBUTTON 0x0200
157#endif
158
94311eef
MB
159#ifndef BIF_EDITBOX
160 #define BIF_EDITBOX 16
161#endif
162
3f2711d5 163// ----------------------------------------------------------------------------
77ffb593 164// wxWidgets macros
3f2711d5 165// ----------------------------------------------------------------------------
2bda0e17 166
54744d3a 167IMPLEMENT_CLASS(wxDirDialog, wxDialog)
2bda0e17 168
3f2711d5
VZ
169// ----------------------------------------------------------------------------
170// private functions prototypes
171// ----------------------------------------------------------------------------
172
3f2711d5
VZ
173// the callback proc for the dir dlg
174static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp,
175 LPARAM pData);
bfa2e032 176
54744d3a 177
3f2711d5
VZ
178// ============================================================================
179// implementation
180// ============================================================================
bfa2e032 181
3f2711d5
VZ
182// ----------------------------------------------------------------------------
183// wxDirDialog
184// ----------------------------------------------------------------------------
185
186wxDirDialog::wxDirDialog(wxWindow *parent,
187 const wxString& message,
188 const wxString& defaultPath,
90a6f1cc 189 long style,
e78d4a23
VZ
190 const wxPoint& WXUNUSED(pos),
191 const wxSize& WXUNUSED(size),
192 const wxString& WXUNUSED(name))
2bda0e17
KB
193{
194 m_message = message;
2bda0e17 195 m_parent = parent;
90a6f1cc 196
141d782d 197 SetWindowStyle(style);
f3558acc
VZ
198 SetPath(defaultPath);
199}
200
201void wxDirDialog::SetPath(const wxString& path)
202{
203 m_path = path;
e7b0dcd9
VZ
204
205 // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes
9a83f860 206 m_path.Replace(wxT("/"), wxT("\\"));
359cde15
VZ
207
208 while ( !m_path.empty() && (*(m_path.end() - 1) == wxT('\\')) )
209 {
210 m_path.erase(m_path.length() - 1);
211 }
212
213 // but the root drive should have a trailing slash (again, this is just
214 // the way the native dialog works)
215 if ( !m_path.empty() && (*(m_path.end() - 1) == wxT(':')) )
e7b0dcd9 216 {
359cde15 217 m_path += wxT('\\');
e7b0dcd9 218 }
2bda0e17
KB
219}
220
3f2711d5 221int wxDirDialog::ShowModal()
2bda0e17 222{
691745ab 223 WX_HOOK_MODAL_DIALOG();
643e9cf9 224
580ffdf4
VZ
225 wxWindow* const parent = GetParent();
226 WXHWND hWndParent = parent ? GetHwndOf(parent) : NULL;
227
228 // Use IFileDialog under new enough Windows, it's more user-friendly.
229 int rc;
230#if wxUSE_IFILEDIALOG
231 if ( wxGetWinVersion() >= wxWinVersion_Vista )
232 {
233 rc = ShowIFileDialog(hWndParent);
234 }
235 else
236 {
237 rc = wxID_NONE;
238 }
239
240 if ( rc == wxID_NONE )
241#endif // wxUSE_IFILEDIALOG
242 {
243 rc = ShowSHBrowseForFolder(hWndParent);
244 }
245
246 // change current working directory if asked so
247 if ( rc == wxID_OK && HasFlag(wxDD_CHANGE_DIR) )
248 wxSetWorkingDirectory(m_path);
f3558acc 249
580ffdf4
VZ
250 return rc;
251}
252
253int wxDirDialog::ShowSHBrowseForFolder(WXHWND owner)
254{
2bda0e17 255 BROWSEINFO bi;
580ffdf4 256 bi.hwndOwner = owner;
3f2711d5
VZ
257 bi.pidlRoot = NULL;
258 bi.pszDisplayName = NULL;
58e0338a
JS
259 // Please don't change this without checking it compiles
260 // with eVC++ first.
261#if defined(__POCKETPC__) || defined(__SMARTPHONE__)
262 bi.lpszTitle = m_message.mb_str();
263#else
3f2711d5 264 bi.lpszTitle = m_message.c_str();
58e0338a 265#endif
3f2711d5
VZ
266 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
267 bi.lpfn = BrowseCallbackProc;
017dc06b 268 bi.lParam = wxMSW_CONV_LPARAM(m_path); // param for the callback
3f2711d5 269
cf54972e
VZ
270 static const int verComCtl32 = wxApp::GetComCtl32Version();
271
272 // we always add the edit box (it doesn't hurt anybody, does it?) if it is
273 // supported by the system
274 if ( verComCtl32 >= 471 )
275 {
276 bi.ulFlags |= BIF_EDITBOX;
277 }
278
cf54972e
VZ
279 // to have the "New Folder" button we must use the "new" dialog style which
280 // is also the only way to have a resizable dialog
281 //
282 // "new" style is only available in the version 5.0+ of comctl32.dll
ff3e84ff 283 const bool needNewDir = !HasFlag(wxDD_DIR_MUST_EXIST);
cf54972e
VZ
284 if ( (needNewDir || HasFlag(wxRESIZE_BORDER)) && (verComCtl32 >= 500) )
285 {
03776753
JS
286 if (needNewDir)
287 {
288 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
289 }
290 else
291 {
292 // Versions < 600 doesn't support BIF_NONEWFOLDERBUTTON
293 // The only way to get rid of the Make New Folder button is use
294 // the old dialog style which doesn't have the button thus we
295 // simply don't set the New Dialog Style for such comctl versions.
296 if (verComCtl32 >= 600)
297 {
298 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
299 bi.ulFlags |= BIF_NONEWFOLDERBUTTON;
300 }
301 }
90a6f1cc
RD
302 }
303
cf54972e 304 // do show the dialog
6bb09706 305 wxItemIdList pidl(SHBrowseForFolder(&bi));
3f2711d5 306
6bb09706 307 wxItemIdList::Free((LPITEMIDLIST)bi.pidlRoot);
3f2711d5
VZ
308
309 if ( !pidl )
2bda0e17 310 {
3f2711d5 311 // Cancel button pressed
2bda0e17
KB
312 return wxID_CANCEL;
313 }
314
6bb09706 315 m_path = pidl.GetPath();
3f2711d5 316
6bb09706 317 return m_path.empty() ? wxID_CANCEL : wxID_OK;
3f2711d5
VZ
318}
319
580ffdf4
VZ
320// Function for obtaining folder name on Vista and newer.
321//
322// Returns wxID_OK on success, wxID_CANCEL if cancelled by user or wxID_NONE if
323// an error occurred and we should fall back onto the old dialog.
324#if wxUSE_IFILEDIALOG
325
326int wxDirDialog::ShowIFileDialog(WXHWND owner)
327{
328 HRESULT hr;
329 wxCOMPtr<IFileDialog> fileDialog;
330
331 hr = ::CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER,
332 wxIID_PPV_ARGS(IFileDialog, &fileDialog));
333 if ( FAILED(hr) )
334 {
335 wxLogApiError(wxS("CoCreateInstance(CLSID_FileOpenDialog)"), hr);
336 return wxID_NONE;
337 }
338
339 // allow user to select only a file system folder
340 hr = fileDialog->SetOptions(FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM);
341 if ( FAILED(hr) )
342 {
343 wxLogApiError(wxS("IFileDialog::SetOptions"), hr);
344 return wxID_NONE;
345 }
346
347 hr = fileDialog->SetTitle(m_message.wc_str());
348 if ( FAILED(hr) )
349 {
350 // This error is not serious, let's just log it and continue even
351 // without the title set.
352 wxLogApiError(wxS("IFileDialog::SetTitle"), hr);
353 }
354
355 // set the initial path
356 if ( !m_path.empty() )
357 {
358 // We need to link SHCreateItemFromParsingName() dynamically as it's
359 // not available on pre-Vista systems.
360 typedef HRESULT
361 (WINAPI *SHCreateItemFromParsingName_t)(PCWSTR,
362 IBindCtx*,
363 REFIID,
364 void**);
365
6f3320bf 366 SHCreateItemFromParsingName_t pfnSHCreateItemFromParsingName = NULL;
580ffdf4
VZ
367 wxDynamicLibrary dllShell32;
368 if ( dllShell32.Load(wxS("shell32.dll"), wxDL_VERBATIM | wxDL_QUIET) )
369 {
6f3320bf 370 wxDL_INIT_FUNC(pfn, SHCreateItemFromParsingName, dllShell32);
580ffdf4
VZ
371 }
372
6f3320bf 373 if ( !pfnSHCreateItemFromParsingName )
580ffdf4
VZ
374 {
375 wxLogLastError(wxS("SHCreateItemFromParsingName() not found"));
376 return wxID_NONE;
377 }
378
379 wxCOMPtr<IShellItem> folder;
6f3320bf 380 hr = pfnSHCreateItemFromParsingName(m_path.wc_str(),
580ffdf4
VZ
381 NULL,
382 wxIID_PPV_ARGS(IShellItem,
383 &folder));
384 if ( FAILED(hr) )
385 {
386 wxLogApiError(wxS("SHCreateItemFromParsingName"), hr);
387 return wxID_NONE;
388 }
389
390 hr = fileDialog->SetFolder(folder);
391 if ( FAILED(hr) )
392 {
393 wxLogApiError(wxS("IFileDialog::SetFolder"), hr);
394 return wxID_NONE;
395 }
396 }
397
398
399 wxString path;
400
401 hr = fileDialog->Show(owner);
402 if ( SUCCEEDED(hr) )
403 {
404 wxCOMPtr<IShellItem> folder;
405
406 hr = fileDialog->GetResult(&folder);
407 if ( SUCCEEDED(hr) )
408 {
409 LPOLESTR pathOLE = NULL;
410
411 hr = folder->GetDisplayName(SIGDN_FILESYSPATH, &pathOLE);
412 if ( SUCCEEDED(hr) )
413 {
414 path = pathOLE;
415 CoTaskMemFree(pathOLE);
416 }
417 else
418 {
419 wxLogApiError(wxS("IShellItem::GetDisplayName"), hr);
420 }
421 }
422 else
423 {
424 wxLogApiError(wxS("IFileDialog::GetResult"), hr);
425 }
426 }
427 else if ( hr == HRESULT_FROM_WIN32(ERROR_CANCELLED) )
428 {
429 return wxID_CANCEL; // the user cancelled the dialog
430 }
431 else
432 {
433 wxLogApiError(wxS("IFileDialog::Show"), hr);
434 }
435
436 if ( path.empty() )
437 {
438 // the user didn't cancel the dialog and yet the path is empty
439 // it means there was an error, already logged by wxLogApiError()
440 // now report the error to the user and return
441 wxLogSysError(_("Couldn't obtain folder name"), hr);
442 return wxID_CANCEL;
443 }
444
445 m_path = path;
446 return wxID_OK;
447}
448
449#endif // wxUSE_IFILEDIALOG
450
3f2711d5
VZ
451// ----------------------------------------------------------------------------
452// private functions
453// ----------------------------------------------------------------------------
454
455static int CALLBACK
456BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
457{
458 switch(uMsg)
459 {
6bb09706 460#ifdef BFFM_SETSELECTION
3f2711d5
VZ
461 case BFFM_INITIALIZED:
462 // sent immediately after initialisation and so we may set the
463 // initial selection here
464 //
465 // wParam = TRUE => lParam is a string and not a PIDL
d71cc120 466 ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
3f2711d5 467 break;
6bb09706
VZ
468#endif // BFFM_SETSELECTION
469
3f2711d5
VZ
470
471 case BFFM_SELCHANGED:
cf54972e
VZ
472 // note that this doesn't work with the new style UI (MSDN doesn't
473 // say anything about it, but the comments in shlobj.h do!) but we
474 // still execute this code in case it starts working again with the
475 // "new new UI" (or would it be "NewUIEx" according to tradition?)
3f2711d5
VZ
476 {
477 // Set the status window to the currently selected path.
cf54972e
VZ
478 wxString strDir;
479 if ( SHGetPathFromIDList((LPITEMIDLIST)lp,
480 wxStringBuffer(strDir, MAX_PATH)) )
3f2711d5 481 {
cf54972e
VZ
482 // NB: this shouldn't be necessary with the new style box
483 // (which is resizable), but as for now it doesn't work
484 // anyhow (see the comment above) no harm in doing it
485
486 // need to truncate or it displays incorrectly
487 static const size_t maxChars = 37;
488 if ( strDir.length() > maxChars )
6a097cd6 489 {
cf54972e 490 strDir = strDir.Right(maxChars);
6a097cd6
JS
491 strDir = wxString(wxT("...")) + strDir;
492 }
cf54972e
VZ
493
494 SendMessage(hwnd, BFFM_SETSTATUSTEXT,
017dc06b 495 0, wxMSW_CONV_LPARAM(strDir));
3f2711d5
VZ
496 }
497 }
498 break;
499
500 //case BFFM_VALIDATEFAILED: -- might be used to provide custom message
501 // if the user types in invalid dir name
502 }
503
504 return 0;
505}
506
54744d3a 507#endif // compiler/platform on which the code here compiles
1e6feb95 508
08bce501 509#endif // wxUSE_DIRDLG