-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathFilteredFolderDlg.cpp
269 lines (228 loc) · 6.75 KB
/
FilteredFolderDlg.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
//////////////////////////////////////////////////////////////////////
// Custom filtering is based on Arik Poznanski's article
// C# does Shell, Part 1
// http://www.codeproject.com/csharp/csdoesshell1.asp
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FilteredFolderDlg.h"
//////////////////////////////////////////////////////////////////////
#ifdef USE_XP_FILTRATION
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// CFilteredFolderDialog class
CFilteredFolderDialog::CFilteredFolderDialog(LPCTSTR pszTitle,
LPCTSTR pszSelPath,
CWnd* pParentWnd,
UINT uFlags)
: CFolderDialog(pszTitle, pszSelPath, pParentWnd, uFlags)
, m_pIMyFolderFilter(NULL)
, m_pszFilter(NULL)
{
}
CFilteredFolderDialog::~CFilteredFolderDialog(VOID)
{
// SAFE_FREE( m_pszFilter );
// SAFE_RELEASEINTERFACE( m_pIFolderFilter )
}
VOID CFilteredFolderDialog::OnIUnknown(IN IUnknown* pIUnknown)
{
ASSERT(pIUnknown != NULL);
if (m_pIMyFolderFilter == NULL)
{
IFolderFilterSite* pIFolderFilterSite = NULL;
HRESULT hResult = pIUnknown->QueryInterface(IID_IFolderFilterSite, (LPVOID*)&pIFolderFilterSite);
if (SUCCEEDED(hResult))
{
m_pIMyFolderFilter = new IMyFolderFilter;
ASSERT(m_pIMyFolderFilter != NULL);
if (m_pIMyFolderFilter)
pIFolderFilterSite->SetFilter((IUnknown*)m_pIMyFolderFilter);
SAFE_RELEASE(pIFolderFilterSite);
}
}
if (m_pIMyFolderFilter)
m_pIMyFolderFilter->m_pszFilter = m_pszFilter;
}
VOID CFilteredFolderDialog::SetFilter(IN LPCTSTR pszFilter)
{
ASSERT(pszFilter != NULL);
// SAFE_FREE(m_pszFilter);
m_pszFilter = _tcsdup(pszFilter);
}
//////////////////////////////////////////////////////////////////////
// IMyFolderFilter
IMyFolderFilter::IMyFolderFilter(VOID)
: m_ulRef(0)
, m_pszFilter(NULL)
{
}
STDMETHODIMP IMyFolderFilter::QueryInterface(IN REFIID riid,
IN OUT LPVOID* ppvObj)
{
ATLASSERT(ppvObj != NULL);
if (!ppvObj)
return (E_FAIL);
HRESULT hResult = (E_NOINTERFACE);
*ppvObj = NULL;
if (IsEqualIID(riid, IID_IUnknown))
*ppvObj = static_cast<IUnknown*>(this);
else if (IsEqualIID(riid, IID_IFolderFilter))
*ppvObj = static_cast<IFolderFilter*>(this);
if (*ppvObj)
{
reinterpret_cast<IUnknown*>(*ppvObj)->AddRef();
hResult = (S_OK);
}
return (hResult);
}
STDMETHODIMP_(ULONG) IMyFolderFilter::AddRef(VOID)
{
return ::InterlockedIncrement((PLONG)&m_ulRef);
}
STDMETHODIMP_(ULONG) IMyFolderFilter::Release(VOID)
{
ULONG ulNewRef = ::InterlockedDecrement((PLONG)&m_ulRef);
if (ulNewRef <= 0)
{
delete this;
return (0);
}
return (ulNewRef);
}
STDMETHODIMP IMyFolderFilter::ShouldShow(IN IShellFolder* pIShellFolder,
IN LPCITEMIDLIST /*pidlFolder*/,
IN LPCITEMIDLIST pidlItem)
{
ASSERT(pIShellFolder != NULL);
ASSERT(pidlItem != NULL);
ASSERT(m_pszFilter != NULL);
if (!m_pszFilter)
return (E_FAIL);
// If an item is a folder, then accept it
LPCITEMIDLIST pidl[1] = { pidlItem };
SFGAOF ulAttr = SFGAO_FOLDER;
pIShellFolder->GetAttributesOf(1, pidl, &ulAttr);
if ((ulAttr & SFGAO_FOLDER) == SFGAO_FOLDER)
return (S_OK);
// If an item is a file, then filter it
STRRET strRet = { 0 };
HRESULT hResult = pIShellFolder->GetDisplayNameOf(
pidlItem, SHGDN_NORMAL | SHGDN_FORPARSING, &strRet
);
if (SUCCEEDED(hResult))
{
LPTSTR pszDisplayName = NULL;
hResult = ::StrRetToStr(&strRet, pidlItem, &pszDisplayName);
if (SUCCEEDED(hResult))
{
hResult = (S_FALSE);
if (::PathMatchSpec(pszDisplayName, m_pszFilter))
hResult = (S_OK);
SAFE_COTASKMEMFREE(pszDisplayName);
}
}
return (hResult);
}
STDMETHODIMP IMyFolderFilter::GetEnumFlags(IN IShellFolder* /*pIShellFolder*/,
IN LPCITEMIDLIST /*pidlFolder*/,
IN HWND* /*phWnd*/,
OUT LPDWORD pdwFlags)
{
ASSERT(pdwFlags != NULL);
*pdwFlags = (DWORD)(SHCONTF_FOLDERS | SHCONTF_NONFOLDERS);
return (S_OK);
}
//////////////////////////////////////////////////////////////////////
// CFolderFilter
//IMPLEMENT_DYNCREATE( CFolderFilter, CCmdTarget );
//
//BEGIN_INTERFACE_MAP( CFolderFilter, CCmdTarget )
// INTERFACE_PART( CFolderFilter, IID_IFolderFilter, FolderFilterObj )
//END_INTERFACE_MAP()
//
//STDMETHODIMP_( ULONG ) CFolderFilter::XFolderFilterObj::AddRef( VOID )
//{
// METHOD_PROLOGUE( CFolderFilter, FolderFilterObj )
// return ( pThis->ExternalAddRef() );
//}
//
//STDMETHODIMP_( ULONG ) CFolderFilter::XFolderFilterObj::Release( VOID )
//{
// METHOD_PROLOGUE( CFolderFilter, FolderFilterObj )
// return ( pThis->ExternalRelease() );
//}
//
//STDMETHODIMP CFolderFilter::XFolderFilterObj::QueryInterface( IN REFIID refIid,
// IN LPVOID* ppvObj )
//{
// METHOD_PROLOGUE( CFolderFilter, FolderFilterObj )
// return ( (HRESULT)pThis->ExternalQueryInterface( &refIid, ppvObj ) );
//}
//
//STDMETHODIMP CFolderFilter::XFolderFilterObj::ShouldShow( IN IShellFolder* pIShellFolder,
// IN LPCITEMIDLIST /*pidlFolder*/,
// IN LPCITEMIDLIST pidlItem )
//{
// ASSERT( pIShellFolder != NULL );
// ASSERT( pidlItem != NULL );
//
// METHOD_PROLOGUE( CFolderFilter, FolderFilterObj )
//
// ASSERT_VALID( pThis->m_pStraFilters );
// if( !pThis->m_pStraFilters )
// return ( S_FALSE );
//
// // If an item is a folder, then accept it
//
// LPCITEMIDLIST pidl[ 1 ] = { pidlItem };
// SFGAOF ulAttr = SFGAO_FOLDER;
//
// pIShellFolder->GetAttributesOf( 1, pidl, &ulAttr );
//
// if ( ( ulAttr & SFGAO_FOLDER ) == SFGAO_FOLDER )
// return ( S_OK );
//
// // If an item is a file, then filter it
//
// STRRET strRet = { 0 };
// HRESULT hResult = pIShellFolder->GetDisplayNameOf(
// pidlItem, SHGDN_NORMAL | SHGDN_FORPARSING, &strRet
// );
//
// if( SUCCEEDED( hResult ) )
// {
// LPTSTR pszDisplayName = NULL;
// hResult = ::StrRetToStr( &strRet, pidlItem, &pszDisplayName );
//
// if( SUCCEEDED( hResult ) )
// {
// LPCTSTR pszExt = ::CharNext( ::PathFindExtension( pszDisplayName ) );
//
// for( INT n = 0; pszExt && ( n < pThis->m_pStraFilters->GetSize() ); n++ )
// {
// if( ::lstrcmpi( pszExt, pThis->m_pStraFilters->GetAt( n ) ) == 0 )
// return ( S_OK );
// }
//
// SAFE_COTASKMEMFREE( pszDisplayName );
// }
// }
//
// return ( S_FALSE );
//
//}
//
//STDMETHODIMP CFolderFilter::XFolderFilterObj::GetEnumFlags( IN IShellFolder* /*pIShellFolder*/,
// IN LPCITEMIDLIST /*pidlFolder*/,
// IN HWND* /*phWnd*/,
// IN LPDWORD pdwFlags )
//{
// ASSERT( pdwFlags != NULL );
// //METHOD_PROLOGUE( CFolderFilter, FolderFilterObj )
//
// *pdwFlags = (DWORD)( SHCONTF_FOLDERS | SHCONTF_NONFOLDERS );
// return ( S_OK );
//}
//////////////////////////////////////////////////////////////////////
#endif //USE_XP_FILTRATION
//////////////////////////////////////////////////////////////////////