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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
#include "envshell.h"
#include "env.h"
#include <log.h>
#include <utility.h>
#include <windowsx.h>
namespace env
{
using namespace MOBase;
const int QCM_FIRST = 1;
const int QCM_LAST = 0x7ff;
class MenuFailed : public std::runtime_error
{
public:
MenuFailed(HRESULT r, const std::string& what)
: runtime_error(fmt::format(
"{}, {}",
what, QString::fromStdWString(formatSystemMessage(r)).toStdString()))
{
}
};
class WndProcFilter : public QAbstractNativeEventFilter
{
public:
WndProcFilter(QMainWindow* mw, IContextMenu* cm)
: m_mw(mw), m_cm(cm), m_cm2(nullptr), m_cm3(nullptr)
{
IContextMenu2* cm2 = nullptr;
if (SUCCEEDED(cm->QueryInterface(IID_IContextMenu2, (void**)&cm2))) {
m_cm2.reset(cm2);
}
IContextMenu3* cm3 = nullptr;
if (SUCCEEDED(cm->QueryInterface(IID_IContextMenu3, (void**)&cm3))) {
m_cm3.reset(cm3);
}
}
~WndProcFilter()
{
if (auto* sb=m_mw->statusBar()) {
sb->clearMessage();
}
}
bool nativeEventFilter(const QByteArray& type, void* m, long* lresultOut) override
{
MSG* msg = (MSG*)m;
if (msg->message == WM_MENUSELECT) {
HANDLE_WM_MENUSELECT(msg->hwnd, msg->wParam, msg->lParam, onMenuSelect);
return true;
}
if (m_cm3) {
LRESULT lresult = 0;
const auto r = m_cm3->HandleMenuMsg2(
msg->message, msg->wParam, msg->lParam, &lresult);
if (SUCCEEDED(r)) {
if (lresultOut) {
*lresultOut = lresult;
}
return true;
}
}
if (m_cm2) {
const auto r = m_cm2->HandleMenuMsg(
msg->message, msg->wParam, msg->lParam);
if (SUCCEEDED(r)) {
if (lresultOut) {
*lresultOut = 0;
}
return true;
}
}
return false;
}
private:
QMainWindow* m_mw;
IContextMenu* m_cm;
COMPtr<IContextMenu2> m_cm2;
COMPtr<IContextMenu3> m_cm3;
// adapted from
// https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723
//
void onMenuSelect(
HWND hwnd, HMENU hmenu, int item, HMENU hmenuPopup, UINT flags)
{
if (m_cm && item >= QCM_FIRST && item <= QCM_LAST) {
WCHAR szBuf[MAX_PATH];
const auto r = IContextMenu_GetCommandString(
m_cm, item - QCM_FIRST, GCS_HELPTEXTW, NULL, szBuf, MAX_PATH);
if (FAILED(r)) {
lstrcpynW(szBuf, L"No help available.", MAX_PATH);
}
if (m_mw) {
if (auto* sb=m_mw->statusBar()) {
sb->showMessage(QString::fromWCharArray(szBuf));
}
}
}
}
// adapted from
// https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723
//
HRESULT IContextMenu_GetCommandString(
IContextMenu *pcm, UINT_PTR idCmd, UINT uFlags,
UINT *pwReserved, LPWSTR pszName, UINT cchMax)
{
// Callers are expected to be using Unicode.
if (!(uFlags & GCS_UNICODE)) {
return E_INVALIDARG;
}
// Some context menu handlers have off-by-one bugs and will
// overflow the output buffer. Let’s artificially reduce the
// buffer size so a one-character overflow won’t corrupt memory.
if (cchMax <= 1) {
return E_FAIL;
}
cchMax--;
// First try the Unicode message. Preset the output buffer
// with a known value because some handlers return S_OK without
// doing anything.
pszName[0] = L'\0';
HRESULT hr = pcm->GetCommandString(
idCmd, uFlags, pwReserved, (LPSTR)pszName, cchMax);
if (SUCCEEDED(hr) && pszName[0] == L'\0') {
// Rats, a buggy IContextMenu handler that returned success
// even though it failed.
hr = E_NOTIMPL;
}
if (FAILED(hr)) {
// try again with ANSI – pad the buffer with one extra character
// to compensate for context menu handlers that overflow by
// one character.
LPSTR pszAnsi = (LPSTR)LocalAlloc(
LMEM_FIXED, (cchMax + 1) * sizeof(CHAR));
if (pszAnsi) {
pszAnsi[0] = '\0';
hr = pcm->GetCommandString(
idCmd, uFlags & ~GCS_UNICODE, pwReserved, pszAnsi, cchMax);
if (SUCCEEDED(hr) && pszAnsi[0] == '\0') {
// Rats, a buggy IContextMenu handler that returned success
// even though it failed.
hr = E_NOTIMPL;
}
if (SUCCEEDED(hr)) {
if (MultiByteToWideChar(CP_ACP, 0, pszAnsi, -1, pszName, cchMax) == 0) {
hr = E_FAIL;
}
}
LocalFree(pszAnsi);
} else {
hr = E_OUTOFMEMORY;
}
}
return hr;
}
};
CoTaskMemPtr<LPITEMIDLIST> getIDL(const wchar_t* path)
{
LPITEMIDLIST pidl;
SFGAOF sfgao;
const auto r = SHParseDisplayName(path, nullptr, &pidl, 0, &sfgao);
if (FAILED(r)) {
throw MenuFailed(r, "SHParseDisplayName failed");
}
return CoTaskMemPtr<LPITEMIDLIST>(pidl);
}
std::pair<COMPtr<IShellFolder>, LPCITEMIDLIST> getShellFolder(LPITEMIDLIST idl)
{
IShellFolder* psf = nullptr;
LPCITEMIDLIST pidlChild = nullptr;
const auto r = SHBindToParent(
idl, IID_IShellFolder, reinterpret_cast<void**>(&psf), &pidlChild);
if (FAILED(r)) {
throw MenuFailed(r, "SHBindToParent failed");
}
return {COMPtr<IShellFolder>(psf), pidlChild};
}
COMPtr<IContextMenu> getContextMenu(IShellFolder* psf, LPCITEMIDLIST idl)
{
IContextMenu* pcm = nullptr;
const auto r = psf->GetUIObjectOf(
0, 1, &idl, IID_IContextMenu, nullptr,
reinterpret_cast<void**>(&pcm));
if (FAILED(r)) {
throw MenuFailed(r, "GetUIObjectOf failed");
}
return COMPtr<IContextMenu>(pcm);
}
HMenuPtr createMenu(IContextMenu* cm)
{
HMENU hmenu = CreatePopupMenu();
if (!hmenu) {
const auto e = GetLastError();
throw MenuFailed(e, "CreatePopupMenu failed");
}
const auto r = cm->QueryContextMenu(
hmenu, 0, QCM_FIRST, QCM_LAST, CMF_EXTENDEDVERBS);
if (FAILED(r)) {
throw MenuFailed(r, "QueryContextMenu failed");
}
return HMenuPtr(hmenu);
}
int runMenu(QMainWindow* mw, IContextMenu* cm, HMENU menu, const QPoint& p)
{
const auto hwnd = (HWND)mw->winId();
auto filter = std::make_unique<WndProcFilter>(mw, cm);
QCoreApplication::instance()->installNativeEventFilter(filter.get());
return TrackPopupMenuEx(menu, TPM_RETURNCMD, p.x(), p.y(), hwnd, nullptr);
}
void invoke(QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm)
{
const auto hwnd = (HWND)mw->winId();
CMINVOKECOMMANDINFOEX info = {};
info.cbSize = sizeof(info);
info.fMask = CMIC_MASK_UNICODE | CMIC_MASK_PTINVOKE;
info.hwnd = hwnd;
info.lpVerb = MAKEINTRESOURCEA(cmd);
info.lpVerbW = MAKEINTRESOURCEW(cmd);
info.nShow = SW_SHOWNORMAL;
info.ptInvoke = {p.x(), p.y()};
// note: this calls the query version because the Qt even loop hasn't run
// yet and shift is still considered pressed
const auto m = QApplication::queryKeyboardModifiers();
if (m & Qt::ShiftModifier) {
info.fMask |= CMIC_MASK_SHIFT_DOWN;
}
if (m & Qt::ControlModifier) {
info.fMask |= CMIC_MASK_CONTROL_DOWN;
}
const auto r = cm->InvokeCommand((CMINVOKECOMMANDINFO*)&info);
if (FAILED(r)) {
throw MenuFailed(r, fmt::format("InvokeCommand failed, verb={}", cmd));
}
}
QMainWindow* getMainWindow(QWidget* w)
{
QWidget* p = w;
while (p) {
if (auto* mw=dynamic_cast<QMainWindow*>(p)) {
return mw;
}
p = p->parentWidget();
}
return nullptr;
}
void showShellMenu(QWidget* parent, const QFileInfo& file, const QPoint& pos)
{
const auto path = QDir::toNativeSeparators(file.absoluteFilePath());
try
{
auto* mw = getMainWindow(parent);
auto idl = getIDL(path.toStdWString().c_str());
auto [sf, childIdl] = getShellFolder(idl.get());
auto cm = getContextMenu(sf.get(), childIdl);
auto hmenu = createMenu(cm.get());
const int cmd = runMenu(mw, cm.get(), hmenu.get(), pos);
if (cmd <= 0) {
return;
}
invoke(mw, pos, cmd - QCM_FIRST, cm.get());
}
catch(MenuFailed& e)
{
log::error("can't create shell menu for '{}': {}", path, e.what());
}
}
} // namespace
|