forked from eneam/mboxviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowseForFolder.cpp
More file actions
212 lines (175 loc) · 4.38 KB
/
BrowseForFolder.cpp
File metadata and controls
212 lines (175 loc) · 4.38 KB
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
//////////////////////////////////////////////////////////////////////
//
// ShellBrowser.cpp: implementation of the CShellBrowser class.
//
#include "stdafx.h"
#include "BrowseForFolder.h"
#ifdef _DEBUG
#undef THIS_FILE
#define THIS_FILE __FILE__
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
//
// Construction/Destruction
//
CBrowseForFolder::CBrowseForFolder(const HWND hParent /*= NULL*/, const LPITEMIDLIST pidl /*= NULL*/, const int nTitleID /*= 0*/)
{
m_hwnd = 0;
SetOwner(hParent);
SetRoot(pidl);
SetTitle(nTitleID);
m_bi.lpfn = BrowseCallbackProc;
m_bi.lParam = reinterpret_cast<LPARAM>(this);
m_bi.pszDisplayName = &m_szSelected[0];
}
CBrowseForFolder::CBrowseForFolder(const HWND hParent , const int clsid, const int nTitleID /*= 0*/)
{
LPITEMIDLIST pidlRoot;
if (0 != SHGetSpecialFolderLocation(HWND_DESKTOP, clsid, &pidlRoot))
pidlRoot = NULL;
m_hwnd = NULL;
SetOwner(hParent);
SetRoot(pidlRoot);
m_pchTitle[0] = 0;
SetTitle(nTitleID);
m_bi.lpfn = BrowseCallbackProc;
m_bi.lParam = reinterpret_cast<LPARAM>(this);
m_bi.pszDisplayName = &m_szSelected[0];
}
CBrowseForFolder::CBrowseForFolder(const HWND hParent, const LPITEMIDLIST pidl, const CString& strTitle)
{
m_hwnd = NULL;
SetOwner(hParent);
SetRoot(pidl);
m_pchTitle[0] = 0;
SetTitle(strTitle);
m_bi.lpfn = BrowseCallbackProc;
m_bi.lParam = reinterpret_cast<LPARAM>(this);
m_bi.pszDisplayName = &m_szSelected[0];
}
CBrowseForFolder::~CBrowseForFolder()
{
}
//////////////////////////////////////////////////////////////////////
//
// Implementation
//
void CBrowseForFolder::SetOwner(const HWND hwndOwner)
{
if (m_hwnd != NULL)
return;
m_bi.hwndOwner = hwndOwner;
}
void CBrowseForFolder::SetRoot(const LPITEMIDLIST pidl)
{
if (m_hwnd != NULL)
return;
m_bi.pidlRoot = pidl;
}
CString CBrowseForFolder::GetTitle() const
{
return m_bi.lpszTitle;
}
void CBrowseForFolder::SetTitle(const CString& strTitle)
{
if (m_hwnd != NULL)
return;
lstrcpy(&m_pchTitle[0], strTitle);
m_bi.lpszTitle = &m_pchTitle[0];
}
bool CBrowseForFolder::SetTitle(const int nTitle)
{
if (nTitle <= 0)
return false;
CString strTitle;
if(!strTitle.LoadString(static_cast<size_t>(nTitle)))
{
return false;
}
SetTitle(strTitle);
return true;
}
void CBrowseForFolder::SetFlags(const UINT ulFlags)
{
if (m_hwnd != NULL)
return;
m_bi.ulFlags = ulFlags;
}
CString CBrowseForFolder::GetSelectedFolder() const
{
return &m_szSelected[0];
}
bool CBrowseForFolder::SelectFolder()
{
bool bRet = false;
LPITEMIDLIST pidl;
m_bi.ulFlags |= BIF_USENEWUI;
if ((pidl = ::SHBrowseForFolder(&m_bi)) != NULL)
{
m_strPath.Empty();
if (::SHGetPathFromIDList(pidl, m_szSelected))
{
bRet = true;
m_strPath = &m_szSelected[0];
}
LPMALLOC pMalloc;
//Retrieve a pointer to the shell's IMalloc interface
if (SUCCEEDED(SHGetMalloc(&pMalloc)))
{
if (pMalloc) {
// free the PIDL that SHBrowseForFolder returned to us.
pMalloc->Free(pidl);
// release the shell's IMalloc interface
(void)pMalloc->Release();
}
}
}
m_hwnd = NULL;
return bRet;
}
void CBrowseForFolder::OnInit() const
{
if( ! m_defaultFolder.IsEmpty() )
SetSelection(m_defaultFolder);
}
void CBrowseForFolder::OnSelChanged(const LPITEMIDLIST pidl) const
{
(void)pidl;
}
void CBrowseForFolder::EnableOK(const bool bEnable) const
{
if (m_hwnd == NULL)
return;
(void)SendMessage(m_hwnd, BFFM_ENABLEOK, static_cast<WPARAM>(bEnable), NULL);
}
void CBrowseForFolder::SetSelection(const LPITEMIDLIST pidl) const
{
if (m_hwnd == NULL)
return;
(void)SendMessage(m_hwnd, BFFM_SETSELECTION, FALSE, reinterpret_cast<LPARAM>(pidl));
}
void CBrowseForFolder::SetSelection(const CString& strPath) const
{
if (m_hwnd == NULL)
return;
(void)SendMessage(m_hwnd, BFFM_SETSELECTION, TRUE, reinterpret_cast<LPARAM>(LPCWSTR(strPath)));
}
void CBrowseForFolder::SetStatusText(const CString& strText) const
{
if (m_hwnd == NULL)
return;
(void)SendMessage(m_hwnd, BFFM_SETSTATUSTEXT, NULL, reinterpret_cast<LPARAM>(LPCWSTR(strText)));
}
int __stdcall CBrowseForFolder::BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
CBrowseForFolder* pbff = reinterpret_cast<CBrowseForFolder*>(lpData);
if (pbff) {
pbff->m_hwnd = hwnd;
if (uMsg == BFFM_INITIALIZED)
pbff->OnInit();
else if (uMsg == BFFM_SELCHANGED)
pbff->OnSelChanged(reinterpret_cast<LPITEMIDLIST>(lParam));
}
return 0;
}