-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFolderDlg.cpp
262 lines (205 loc) · 6.3 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
/////////////////////////////////////////////////////////////////////////////
/*
DESCRIPTION:
CFolderDialog - Folder Selection Dialog Class
http://www.codeproject.com/dialog/cfolderdialog.asp
NOTES:
Copyright(C) Armen Hakobyan, 2002
mailto:[email protected]
VERSION HISTORY:
24 Mar 2002 - First release
30 Mar 2003 - Some minor changes
- Added missing in old Platform SDK new flag definitions
- Added support for both MFC 6.0 and 7.0
- Added OnIUnknown handler for Windows XP folder filtration
- Added SetExpanded and SetOKText and GetSelectedFolder functions
24 May 2003 - Added OnSelChanged implementation
14 Jul 2003 - Added custom filtration for Windows XP, thanks to Arik Poznanski
29 Nov 2003 - Added SetRootFolder, thanks to Eckhard Schwabe ( and Jose Insa )
02 Jan 2004 - Added GetRootFolder, uncomment if needed
*/
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FolderDlg.h"
/////////////////////////////////////////////////////////////////////////////
#ifndef BFFM_VALIDATEFAILED
#ifndef UNICODE
#define BFFM_VALIDATEFAILED 3
#else
#define BFFM_VALIDATEFAILED 4
#endif
#endif
#ifndef BFFM_IUNKNOWN
#define BFFM_IUNKNOWN 5
#endif
/////////////////////////////////////////////////////////////////////////////
// CFolderDialog
IMPLEMENT_DYNAMIC( CFolderDialog, CDialog )
CFolderDialog::CFolderDialog( IN LPCTSTR pszTitle /*NULL*/,
IN LPCTSTR pszSelPath /*NULL*/,
IN CWnd* pWndParent /*NULL*/,
IN UINT uFlags /*BIF_RETURNONLYFSDIRS*/ )
: CCommonDialog( pWndParent )
, m_hWnd( NULL )
{
ASSERT( AfxIsValidString( pszSelPath, MAX_PATH ) );
::ZeroMemory( &m_bi, sizeof( BROWSEINFO ) );
::ZeroMemory( m_szFolPath, MAX_PATH );
::ZeroMemory( m_szSelPath, MAX_PATH );
if( pszSelPath != NULL )
SetSelectedFolder( pszSelPath );
// Fill data
m_bi.hwndOwner = pWndParent->GetSafeHwnd();
m_bi.pidlRoot = NULL;
m_bi.lpszTitle = pszTitle;
m_bi.ulFlags = uFlags;
m_bi.lpfn = (BFFCALLBACK)BrowseCallbackProc;
m_bi.lParam = (LPARAM)this;
// The size of this buffer is assumed to be MAX_PATH bytes:
m_bi.pszDisplayName = new TCHAR[ MAX_PATH ];
ASSERT( m_bi.pszDisplayName != NULL );
::ZeroMemory( m_bi.pszDisplayName, ( MAX_PATH * sizeof( TCHAR ) ) );
}
CFolderDialog::~CFolderDialog( void )
{
_coTaskMemFree( m_bi.pidlRoot );
_delete2( m_bi.pszDisplayName );
::ZeroMemory( &m_bi, sizeof( BROWSEINFO ) );
}
BEGIN_MESSAGE_MAP( CFolderDialog, CCommonDialog )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFolderDialog message handlers
// SetRootFolder By Jose Insa
// Microsoft knowledge Base Article
// ID Q132750: Convert a File Path to an ITEMIDLIST
BOOL CFolderDialog::SetRootFolder( IN LPCTSTR pszPath )
{
ASSERT_VALID( this );
if( pszPath == NULL )
{
_coTaskMemFree( m_bi.pidlRoot );
return ( TRUE );
}
ASSERT( AfxIsValidString( pszPath, MAX_PATH ) );
HRESULT hResult = S_FALSE;
IShellFolder* pDeskFolder = NULL;
if ( ( hResult = ::SHGetDesktopFolder( &pDeskFolder ) ) == S_OK )
{
LPITEMIDLIST pidlRoot = NULL;
LPTSTR pszRoot = const_cast< LPTSTR >( pszPath );
// Convert the path to an ITEMIDLIST:
USES_CONVERSION;
hResult = pDeskFolder->ParseDisplayName(
NULL, NULL, T2W( pszRoot ), NULL, &pidlRoot, NULL
);
if ( hResult == S_OK )
{
_coTaskMemFree( m_bi.pidlRoot );
m_bi.pidlRoot = pidlRoot;
}
_releaseInterface( pDeskFolder );
}
return ( hResult == S_OK );
}
// NOTE: pszPath buffer must be at least
// MAX_PATH characters in size:
BOOL CFolderDialog::GetRootFolder( IN OUT LPTSTR pszPath )
{
ASSERT_VALID( this );
ASSERT( AfxIsValidString( pszPath, MAX_PATH ) );
return ::SHGetPathFromIDList( m_bi.pidlRoot, pszPath );
}
/////////////////////////////////////////////////////////////////////////////
#if ( _MFC_VER >= 0x0700 )
INT_PTR CFolderDialog::DoModal( void )
#else
INT CFolderDialog::DoModal( void )
#endif
{
ASSERT_VALID( this );
ASSERT( m_bi.lpfn != NULL );
m_bi.hwndOwner = PreModal();
INT_PTR nRet = -1;
LPITEMIDLIST pItemIDList = ::SHBrowseForFolder( &m_bi );
if( pItemIDList )
{
if( ::SHGetPathFromIDList( pItemIDList, m_szFolPath ) )
nRet = IDOK;
else
nRet = IDCANCEL;
_coTaskMemFree( pItemIDList );
}
PostModal();
return ( nRet );
}
/////////////////////////////////////////////////////////////////////////////
// Overridables:
void CFolderDialog::OnInitialized( void )
{
if( ::lstrlen( m_szSelPath ) > 0 )
SetSelection( m_szSelPath );
}
void CFolderDialog::OnSelChanged( IN LPITEMIDLIST pItemIDList )
{
if( m_bi.ulFlags & BIF_STATUSTEXT )
{
TCHAR szSelFol[ MAX_PATH ] = { 0 };
if( ::SHGetPathFromIDList( pItemIDList, szSelFol ) )
SetStatusText( szSelFol );
}
}
INT CFolderDialog::OnValidateFailed( IN LPCTSTR /*pszPath*/ )
{
::MessageBeep( MB_ICONHAND );
return 1; // Return 1 to leave dialog open, 0 - to end one
}
void CFolderDialog::OnIUnknown( IN IUnknown* /*pIUnknown*/ )
{
}
/////////////////////////////////////////////////////////////////////////////
// Callback function used with the SHBrowseForFolder function.
INT CALLBACK CFolderDialog::BrowseCallbackProc( IN HWND hWnd,
IN UINT uMsg,
IN LPARAM lParam,
IN LPARAM lpData )
{
CFolderDialog* pThis = (CFolderDialog*)lpData;
pThis->m_hWnd = hWnd;
INT nRet = 0;
switch( uMsg )
{
case BFFM_INITIALIZED:
pThis->OnInitialized();
break;
case BFFM_SELCHANGED:
pThis->OnSelChanged( (LPITEMIDLIST)lParam );
break;
case BFFM_VALIDATEFAILED:
nRet = pThis->OnValidateFailed( (LPCTSTR)lParam );
break;
case BFFM_IUNKNOWN:
pThis->OnIUnknown( (IUnknown*)lParam );
break;
default:
ASSERT( FALSE );
break;
}
pThis->m_hWnd = NULL;
return nRet;
}
/////////////////////////////////////////////////////////////////////////////
void CFolderDialog::SetExpanded( IN LPCTSTR pszPath )
{
USES_CONVERSION;
ASSERT( m_hWnd != NULL );
ASSERT( AfxIsValidString( pszPath, MAX_PATH ) );
::SendMessage( m_hWnd, BFFM_SETEXPANDED, (WPARAM)TRUE, (LPARAM)T2CW( pszPath ) );
}
void CFolderDialog::SetOKText( IN LPCTSTR pszText )
{
USES_CONVERSION;
ASSERT( m_hWnd != NULL );
::SendMessage( m_hWnd, BFFM_SETOKTEXT, (WPARAM)0, (LPARAM)T2CW( pszText ) );
}
/////////////////////////////////////////////////////////////////////////////