/
FolderDlg.cpp
299 lines (265 loc) · 8.51 KB
/
FolderDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
////////////////////////////////////////////////////////////////
// MSDN Magazine -- June 2005
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 (V7.1) on Windows XP. Tab size=3.
//
#include "stdafx.h"
#include "FolderDlg.h"
#include <shlwapi.h>
// You must link shlwapi.lib for StrRetToBuf
#pragma comment(lib, "shlwapi.lib")
#if 0 //CAL!
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BOOL CFolderDialog::bTRACE=0; // controls tracing
//////////////////
// For deugging: names of interfaces easier to read than GUIDs!
//
DEBUG_BEGIN_INTERFACE_NAMES()
DEBUG_INTERFACE_NAME(IFolderFilterSite)
DEBUG_INTERFACE_NAME(IFolderFilter)
DEBUG_END_INTERFACE_NAMES();
#endif
IMPLEMENT_DYNAMIC(CFolderDialog, CCmdTarget);
//////////////////
// ctor: initialize most stuff to NULL
//
CFolderDialog::CFolderDialog(CWnd* pWnd)
{
//CAL! Modified the following to allow us to call this
// routine without a parent window defined. This
// is a temporary workaround so that I can call
// CFolderDialog() from the JPEGsnoopDoc class.
//CAL! ASSERT(pWnd);
memset(&m_brinfo,0,sizeof(m_brinfo));
//CAL! m_brinfo.hwndOwner=pWnd->m_hWnd; // use parent window
m_bFilter = FALSE; // default: no filtering
SHGetDesktopFolder(&m_shfRoot); // get root IShellFolder
}
//////////////////
// dtor: detach browser window before it's destroyed!
//
CFolderDialog::~CFolderDialog()
{
}
//////////////////
// Browse for folder. Args are same as for SHBrowseForFolder, but with extra
// bFilter that tells whether to do custom filtering. Note this requires
// BIF_NEWDIALOGSTYLE, which is inconsistent with some other flags--be
// careful!
//
LPCITEMIDLIST CFolderDialog::BrowseForFolder(LPCTSTR title, UINT flags,
LPCITEMIDLIST root, BOOL bFilter)
{
//BFTRACE(_T("CFolderDialog::BrowseForFolder\n"));
TCHAR* buf = m_sDisplayName.GetBuffer(MAX_PATH);
m_brinfo.pidlRoot = root;
m_brinfo.pszDisplayName = buf;
m_brinfo.lpszTitle = title;
m_brinfo.ulFlags = flags;
m_brinfo.lpfn = CallbackProc;
m_brinfo.lParam = (LPARAM)this;
// filtering only supported for new-style dialogs
m_bFilter = bFilter;
ASSERT(!bFilter||(m_brinfo.ulFlags & BIF_NEWDIALOGSTYLE));
LPCITEMIDLIST pidl = SHBrowseForFolder(&m_brinfo); // do it
m_sDisplayName.ReleaseBuffer();
return pidl;
}
//////////////////
// Handy function to get the string pathname from pidl.
//
CString CFolderDialog::GetPathName(LPCITEMIDLIST pidl)
{
CString path;
TCHAR* buf = path.GetBuffer(MAX_PATH);
SHGetPathFromIDList(pidl, buf);
path.ReleaseBuffer();
return path;
}
//////////////////
// Handy function to get the display name from pidl.
//
CString CFolderDialog::GetDisplayNameOf(IShellFolder* psf, LPCITEMIDLIST pidl,
DWORD uFlags)
{
CString dn;
STRRET strret; // special struct for GetDisplayNameOf
strret.uType = STRRET_CSTR; // get as CSTR
if (SUCCEEDED(psf->GetDisplayNameOf(pidl, uFlags, &strret))) {
StrRetToBuf(&strret, pidl, dn.GetBuffer(MAX_PATH), MAX_PATH);
dn.ReleaseBuffer();
}
return dn;
}
//////////////////
// Free PIDL using shell's IMalloc
//
void CFolderDialog::FreePIDL(LPCITEMIDLIST pidl)
{
CComQIPtr<IMalloc> iMalloc; // shell's IMalloc
HRESULT hr = SHGetMalloc(&iMalloc);
ASSERT(SUCCEEDED(hr));
iMalloc->Free((void*)pidl);
}
//////////////////
// Internal callback proc used for SHBrowseForFolder passes control to
// appropriate virtual function after attaching browser window.
//
int CALLBACK CFolderDialog::CallbackProc(HWND hwnd,
UINT msg, LPARAM lp, LPARAM lpData)
{
CFolderDialog* pDlg = (CFolderDialog*)lpData;
ASSERT(pDlg);
if (pDlg->m_hWnd!=hwnd) {
if (pDlg->m_hWnd)
pDlg->UnsubclassWindow();
pDlg->SubclassWindow(hwnd);
}
return pDlg->OnMessage(msg, lp);
}
//////////////////
// Handle notification from browser window: parse args and pass to specific
// virtual handler function.
//
int CFolderDialog::OnMessage(UINT msg, LPARAM lp)
{
switch (msg) {
case BFFM_INITIALIZED:
OnInitialized();
return 0;
case BFFM_IUNKNOWN:
OnIUnknown((IUnknown*)lp);
return 0;
case BFFM_SELCHANGED:
OnSelChanged((LPCITEMIDLIST)lp);
return 0;
case BFFM_VALIDATEFAILED:
return OnValidateFailed((LPCTSTR)lp);
default:
TRACE(_T("***Warning: unknown message %d in CFolderDialog::OnMessage\n"));
}
return 0;
}
/////////////////
// Browser window initialized.
//
void CFolderDialog::OnInitialized()
{
//BFTRACE(_T("CFolderDialog::OnInitialized\n"));
}
/////////////////
// Browser is notifying me with its IUnknown: use it to set filter if
// requested. Note this can be called with punk=NULL when shutting down!
//
void CFolderDialog::OnIUnknown(IUnknown* punk)
{
//BFTRACE(_T("CFolderDialog::OnIUnknown: %p\n"), punk);
if (punk && m_bFilter) {
CComQIPtr<IFolderFilterSite> iffs;
VERIFY(SUCCEEDED(punk->QueryInterface(IID_IFolderFilterSite, (void**)&iffs)));
iffs->SetFilter((IFolderFilter*)&m_xFolderFilter);
// smart pointer automatically Releases iffs,
// no longer needed once you call SetFilter
}
}
//////////////////
// User selected a different folder.
//
void CFolderDialog::OnSelChanged(LPCITEMIDLIST pidl)
{
//BFTRACE(_T("CFolderDialog::OnSelChanged: %s\n"),
// GetDisplayNameOf(m_shfRoot, pidl, SHGDN_FORPARSING));
}
//////////////////
// User attempted to enter a name in the edit box that isn't a folder.
//
BOOL CFolderDialog::OnValidateFailed(LPCTSTR lpsz)
{
//BFTRACE(_T("CFolderDialog::OnValidateFailed: %s\n"), lpsz);
return TRUE; // don't close dialog.
}
//////////////////
// Used for custom filtering. You must override to specify filter flags.
//
HRESULT CFolderDialog::OnGetEnumFlags(
IShellFolder* psf, // this folder's IShellFolder
LPCITEMIDLIST pidlFolder, // folder's PIDL
DWORD *pgrfFlags) // [out] return flags you want to allow
{
//BFTRACE(_T("CFolderDialog::OnGetEnumFlags(%p): %s\n"),
// psf, GetPathName(pidlFolder));
return S_OK;
}
//////////////////
// Used for custom filtering. You must override to filter items.
//
HRESULT CFolderDialog::OnShouldShow(
IShellFolder* psf, // This folder's IShellFolder
LPCITEMIDLIST pidlFolder, // PIDL for folder containing item
LPCITEMIDLIST pidlItem) // PIDL for item
{
//BFTRACE(_T("CFolderDialog::OnShouldShow(%p): %s: %s\n"), psf,
// GetDisplayNameOf(psf,pidlFolder,SHGDN_NORMAL),
// GetDisplayNameOf(psf,pidlItem,SHGDN_NORMAL));
return S_OK;
}
//////////////// Standard MFC IUnknown -- nested classes call these ////////////////
STDMETHODIMP_(ULONG) CFolderDialog::AddRef()
{
//BFTRACE(_T("CFolderDialog(%p)::AddRef\n"),this);
return ExternalAddRef();
}
STDMETHODIMP_(ULONG) CFolderDialog::Release()
{
//BFTRACE(_T("CFolderDialog(%p)::Release\n"), this);
return ExternalRelease();
}
STDMETHODIMP CFolderDialog::QueryInterface(REFIID iid, LPVOID* ppvRet)
{
if (ppvRet==NULL)
return E_INVALIDARG;
//BFTRACE(_T("CFolderDialog(%p)::QueryInterface(%s)\n"),this,_TR(iid));
HRESULT hr = ExternalQueryInterface(&iid, ppvRet);
//BFTRACE(_T(">CFolderDialog::QueryInterface returns %s, *ppv=%p\n"),_TR(hr),*ppvRet);
return hr;
}
//////////////////////////////// IFolderFilter ////////////////////////////////
//
// Implementation passes control to parent class CFolderDialog (pThis)
//
BEGIN_INTERFACE_MAP(CFolderDialog, CCmdTarget)
INTERFACE_PART(CFolderDialog, IID_IFolderFilter, FolderFilter)
END_INTERFACE_MAP()
STDMETHODIMP_(ULONG) CFolderDialog::XFolderFilter::AddRef()
{
METHOD_PROLOGUE(CFolderDialog, FolderFilter);
return pThis->AddRef();
}
STDMETHODIMP_(ULONG) CFolderDialog::XFolderFilter::Release()
{
METHOD_PROLOGUE(CFolderDialog, FolderFilter);
return pThis->Release();
}
STDMETHODIMP CFolderDialog::XFolderFilter::QueryInterface(REFIID iid, LPVOID* ppv)
{
METHOD_PROLOGUE(CFolderDialog, FolderFilter);
return pThis->QueryInterface(iid, ppv);
}
// Note: pHwnd is always NULL here as far as I can tell.
STDMETHODIMP CFolderDialog::XFolderFilter::GetEnumFlags(IShellFolder* psf,
LPCITEMIDLIST pidlFolder, HWND *pHwnd, DWORD *pgrfFlags)
{
METHOD_PROLOGUE(CFolderDialog, FolderFilter);
return pThis->OnGetEnumFlags(psf, pidlFolder, pgrfFlags);
}
STDMETHODIMP CFolderDialog::XFolderFilter::ShouldShow(IShellFolder* psf,
LPCITEMIDLIST pidlFolder, LPCITEMIDLIST pidlItem)
{
METHOD_PROLOGUE(CFolderDialog, FolderFilter);
return pThis->OnShouldShow(psf, pidlFolder, pidlItem);
}