ReactOS 0.4.16-dev-737-g3368adc
CSendToMenu.cpp
Go to the documentation of this file.
1/*
2 * provides SendTo shell item service
3 *
4 * Copyright 2019 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21#include "precomp.h"
22
24
26 : m_hSubMenu(NULL)
27 , m_pItems(NULL)
28 , m_idCmdFirst(0)
29{
31 if (FAILED(hr))
32 {
33 ERR("SHGetDesktopFolder: %08lX\n", hr);
34 }
35
37}
38
40{
42
43 if (m_hSubMenu)
44 {
47 }
48}
49
51{
52 DWORD dwEffect = DROPEFFECT_MOVE | DROPEFFECT_COPY | DROPEFFECT_LINK;
53
54 BOOL bShift = (GetAsyncKeyState(VK_SHIFT) < 0);
55 BOOL bCtrl = (GetAsyncKeyState(VK_CONTROL) < 0);
56
57 // THIS CODE IS NOT HUMAN-FRIENDLY. SORRY.
58 // (We have to translate a SendTo action to a Drop action)
59 DWORD dwKeyState = MK_LBUTTON;
60 if (bShift && bCtrl)
61 dwKeyState |= MK_SHIFT | MK_CONTROL;
62 else if (!bShift)
63 dwKeyState |= MK_CONTROL;
64 if (bCtrl)
65 dwKeyState |= MK_SHIFT;
66
67 POINTL ptl = { 0, 0 };
68 HRESULT hr = pDropTarget->DragEnter(pDataObject, dwKeyState, ptl, &dwEffect);
70 {
71 pDropTarget->DragLeave();
72 return hr;
73 }
74
75 if (dwEffect == DROPEFFECT_NONE)
76 {
77 ERR("DROPEFFECT_NONE\n");
78 pDropTarget->DragLeave();
79 return E_FAIL;
80 }
81
82 // THIS CODE IS NOT HUMAN-FRIENDLY. SORRY.
83 // (We have to translate a SendTo action to a Drop action)
84 if (bShift && bCtrl)
85 dwEffect = DROPEFFECT_LINK;
86 else if (!bShift)
87 dwEffect = DROPEFFECT_MOVE;
88 else
89 dwEffect = DROPEFFECT_COPY;
90
91 hr = pDropTarget->Drop(pDataObject, dwKeyState, ptl, &dwEffect);
93 return hr;
94
95 return hr;
96}
97
98// get an IShellFolder from CSIDL
101 int csidl, PIDLIST_ABSOLUTE *ppidl)
102{
103 if (!ppFolder)
104 return E_POINTER;
105 *ppFolder = NULL;
106
107 if (ppidl)
108 *ppidl = NULL;
109
111 HRESULT hr = SHGetSpecialFolderLocation(hwnd, csidl, &pidl);
113 return hr;
114
115 IShellFolder *pFolder = NULL;
116 hr = m_pDesktop->BindToObject(pidl, NULL, IID_PPV_ARG(IShellFolder, &pFolder));
117
118 if (ppidl)
119 *ppidl = pidl.Detach();
120
122 return hr;
123
124 *ppFolder = pFolder;
125 return hr;
126}
127
128// get a UI object from PIDL
130 REFIID riid, LPVOID *ppvOut)
131{
132 *ppvOut = NULL;
135 return hr;
136
137 return hr;
138}
139
141{
143 m_pItems = NULL;
144 while (pItems)
145 {
146 SENDTO_ITEM *pCurItem = pItems;
147 pItems = pItems->pNext;
148 delete pCurItem;
149 }
150}
151
153{
155
157
161 return hr;
162
163 CComPtr<IEnumIDList> pEnumIDList;
164 hr = m_pSendTo->EnumObjects(hwnd,
165 SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,
166 &pEnumIDList);
168 return hr;
169
170 hr = S_OK;
172 while (pEnumIDList->Next(1, &child, NULL) == S_OK)
173 {
175
176 STRRET strret;
177 hr = m_pSendTo->GetDisplayNameOf(pidlChild, SHGDN_NORMAL, &strret);
179 continue;
180
181 CComHeapPtr<WCHAR> pszText;
182 hr = StrRetToStrW(&strret, pidlChild, &pszText);
184 continue;
185
187 pidlAbsolute.Attach(ILCombine(pidlSendTo, pidlChild));
188
189 SHFILEINFOW fi = { NULL };
192 SHGetFileInfoW(reinterpret_cast<LPWSTR>(static_cast<PIDLIST_ABSOLUTE>(pidlAbsolute)), 0,
193 &fi, sizeof(fi), uFlags);
194
195 SENDTO_ITEM *pNewItem =
196 new SENDTO_ITEM(pidlChild.Detach(), pszText.Detach(), fi.hIcon);
197 if (m_pItems)
198 {
199 pNewItem->pNext = m_pItems;
200 }
201 m_pItems = pNewItem;
202 }
203
204 return hr;
205}
206
208{
209 if (m_pItems == NULL)
210 {
213 return 0;
214 }
215
216 m_idCmdFirst = idCmdFirst;
217
218 UINT idCmd = idCmdFirst;
219 for (SENDTO_ITEM *pCurItem = m_pItems; pCurItem; pCurItem = pCurItem->pNext)
220 {
222 if (InsertMenuW(hMenu, Pos, uFlags, idCmd, pCurItem->pszText))
223 {
224 MENUITEMINFOW mii;
225 mii.cbSize = sizeof(mii);
227 mii.dwItemData = reinterpret_cast<ULONG_PTR>(pCurItem);
228 mii.hbmpItem = HBMMENU_CALLBACK;
229 SetMenuItemInfoW(hMenu, idCmd, FALSE, &mii);
230 ++idCmd;
231
232 // successful
233 }
234 }
235
236 if (idCmd == idCmdFirst)
237 {
239 AppendMenuW(hMenu, MF_GRAYED | MF_DISABLED | MF_STRING, idCmd, strNone);
240 ++idCmd;
241 }
242
243 return idCmd - idCmdFirst;
244}
245
247{
248 UINT idCmd = m_idCmdFirst + IdOffset;
249
250 MENUITEMINFOW mii = { sizeof(mii) };
251 mii.fMask = MIIM_DATA;
252 if (GetMenuItemInfoW(m_hSubMenu, idCmd, FALSE, &mii))
253 return reinterpret_cast<SENDTO_ITEM *>(mii.dwItemData);
254
255 ERR("GetMenuItemInfoW: %ld\n", GetLastError());
256 return NULL;
257}
258
260{
261 if (!m_pDataObject)
262 {
263 ERR("!m_pDataObject\n");
264 return E_FAIL;
265 }
266
267 HRESULT hr;
268 CComPtr<IDropTarget> pDropTarget;
269 hr = m_pSendTo->GetUIObjectOf(NULL, 1, &pItem->pidlChild, IID_IDropTarget,
270 NULL, (LPVOID *)&pDropTarget);
272 return hr;
273
274 hr = DoDrop(m_pDataObject, pDropTarget);
276 return hr;
277
278 return hr;
279}
280
283 UINT indexMenu,
284 UINT idCmdFirst,
285 UINT idCmdLast,
286 UINT uFlags)
287{
288 TRACE("%p %p %u %u %u %u\n", this,
289 hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
290
291 if (uFlags & (CMF_NOVERBS | CMF_VERBSONLY))
292 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, idCmdFirst);
293
294 HMENU hSubMenu = CreateMenu();
295 if (!hSubMenu)
296 {
297 ERR("CreateMenu: %ld\n", GetLastError());
298 return E_FAIL;
299 }
300
301 UINT cItems = InsertSendToItems(hSubMenu, idCmdFirst, 0);
302
304
305 MENUITEMINFOW mii = { sizeof(mii) };
307 mii.fType = MFT_STRING;
308 mii.wID = -1;
309 mii.dwTypeData = strSendTo.GetBuffer();
310 mii.cch = wcslen(mii.dwTypeData);
311 mii.fState = MFS_ENABLED;
312 mii.hSubMenu = hSubMenu;
313 if (!InsertMenuItemW(hMenu, indexMenu, TRUE, &mii))
314 {
315 ERR("InsertMenuItemW: %ld\n", GetLastError());
316 return E_FAIL;
317 }
318
319 HMENU hOldSubMenu = m_hSubMenu;
320 m_hSubMenu = hSubMenu;
321 DestroyMenu(hOldSubMenu);
322
323 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, idCmdFirst + cItems);
324}
325
328{
329 HRESULT hr = E_FAIL;
330
331 WORD idCmd = LOWORD(lpici->lpVerb);
332 TRACE("idCmd: %d\n", idCmd);
333
334 SENDTO_ITEM *pItem = FindItemFromIdOffset(idCmd);
335 if (pItem)
336 {
337 hr = DoSendToItem(pItem, lpici);
338 }
339
340 TRACE("CSendToMenu::InvokeCommand %x\n", hr);
341 return hr;
342}
343
346 UINT uType,
347 UINT *pwReserved,
348 LPSTR pszName,
349 UINT cchMax)
350{
351 FIXME("%p %lu %u %p %p %u\n", this,
352 idCmd, uType, pwReserved, pszName, cchMax);
353
354 return E_NOTIMPL;
355}
356
359{
360 return S_OK;
361}
362
365 LRESULT *plResult)
366{
369
370 switch (uMsg)
371 {
372 case WM_MEASUREITEM:
373 {
374 MEASUREITEMSTRUCT* lpmis = reinterpret_cast<MEASUREITEMSTRUCT*>(lParam);
375 if (!lpmis || lpmis->CtlType != ODT_MENU)
376 break;
377
378 UINT cxMenuCheck = GetSystemMetrics(SM_CXMENUCHECK);
379 if (lpmis->itemWidth < cxMenuCheck)
380 lpmis->itemWidth = cxMenuCheck;
381 if (lpmis->itemHeight < cySmall)
382 lpmis->itemHeight = cySmall;
383
384 if (plResult)
385 *plResult = TRUE;
386 break;
387 }
388 case WM_DRAWITEM:
389 {
390 DRAWITEMSTRUCT* lpdis = reinterpret_cast<DRAWITEMSTRUCT*>(lParam);
391 if (!lpdis || lpdis->CtlType != ODT_MENU)
392 break;
393
394 SENDTO_ITEM *pItem = reinterpret_cast<SENDTO_ITEM *>(lpdis->itemData);
395 HICON hIcon = NULL;
396 if (pItem)
397 hIcon = pItem->hIcon;
398 if (!hIcon)
399 break;
400
401 const RECT& rcItem = lpdis->rcItem;
402 INT x = 4;
403 INT y = lpdis->rcItem.top;
404 y += (rcItem.bottom - rcItem.top - cySmall) / 2;
405 DrawIconEx(lpdis->hDC, x, y, hIcon, cxSmall, cySmall,
406 0, NULL, DI_NORMAL);
407
408 if (plResult)
409 *plResult = TRUE;
410 }
411 }
412
413 return S_OK;
414}
415
418 IDataObject *pdtobj, HKEY hkeyProgID)
419{
420 m_pDataObject = pdtobj;
421 return S_OK;
422}
HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
UINT cchMax
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define IDS_NONE
Definition: resource.h:133
void shell(int argc, const char *argv[])
Definition: cmds.c:1231
#define FIXME(fmt,...)
Definition: precomp.h:53
#define ERR(fmt,...)
Definition: precomp.h:57
#define STDMETHODIMP
Definition: basetyps.h:43
void Release()
Definition: atlcomcli.h:170
T * Detach()
Definition: atlalloc.h:168
void Attach(T *lp)
Definition: atlalloc.h:162
SENDTO_ITEM * FindItemFromIdOffset(UINT IdOffset)
HRESULT GetUIObjectFromPidl(HWND hwnd, PIDLIST_ABSOLUTE pidl, REFIID riid, LPVOID *ppvOut)
SENDTO_ITEM * m_pItems
Definition: CSendToMenu.h:62
CComPtr< IShellFolder > m_pSendTo
Definition: CSendToMenu.h:66
STDMETHODIMP Initialize(PCIDLIST_ABSOLUTE pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID)
UINT InsertSendToItems(HMENU hMenu, UINT idFirst, UINT idMenu)
HRESULT LoadAllItems(HWND hwnd)
CComPtr< IShellFolder > m_pDesktop
Definition: CSendToMenu.h:65
void UnloadAllItems()
HRESULT GetSpecialFolder(HWND hwnd, IShellFolder **ppFolder, int csidl, PIDLIST_ABSOLUTE *ppidl=NULL)
STDMETHODIMP QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
HMENU m_hSubMenu
Definition: CSendToMenu.h:61
STDMETHODIMP HandleMenuMsg(UINT uMsg, WPARAM wParam, LPARAM lParam)
STDMETHODIMP HandleMenuMsg2(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *plResult)
STDMETHODIMP GetCommandString(UINT_PTR idCommand, UINT uFlags, UINT *lpReserved, LPSTR lpszName, UINT uMaxNameLen)
HRESULT DoSendToItem(SENDTO_ITEM *pItem, LPCMINVOKECOMMANDINFO lpici)
HRESULT DoDrop(IDataObject *pDataObject, IDropTarget *pDropTarget)
Definition: CSendToMenu.cpp:50
CComPtr< IDataObject > m_pDataObject
Definition: CSendToMenu.h:67
STDMETHODIMP InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
UINT m_idCmdFirst
Definition: CSendToMenu.h:63
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
#define E_NOTIMPL
Definition: ddrawi.h:99
#define E_FAIL
Definition: ddrawi.h:102
ush Pos
Definition: deflate.h:92
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
UINT uFlags
Definition: api.c:59
#define FAILED_UNEXPECTEDLY(hr)
Definition: precomp.h:121
EXTERN_C HRESULT SHELL_GetUIObjectOfAbsoluteItem(_In_opt_ HWND hWnd, _In_ PCIDLIST_ABSOLUTE pidl, _In_ REFIID riid, _Out_ void **ppvObj)
Definition: utils.cpp:380
HRESULT WINAPI SHGetSpecialFolderLocation(HWND hwndOwner, INT nFolder, LPITEMIDLIST *ppidl)
Definition: shellpath.c:3257
HRESULT WINAPI StrRetToStrW(LPSTRRET lpStrRet, const ITEMIDLIST *pidl, LPWSTR *ppszName)
Definition: string.c:1631
#define MAKE_HRESULT(sev, fac, code)
Definition: dmerror.h:30
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned short WORD
Definition: ntddk_ex.h:93
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
REFIID riid
Definition: atlbase.h:39
HRESULT Drop([in, unique] IDataObject *pDataObj, [in] DWORD grfKeyState, [in] POINTL pt, [in, out] DWORD *pdwEffect)
HRESULT DragLeave()
HRESULT DragEnter([in, unique] IDataObject *pDataObj, [in] DWORD grfKeyState, [in] POINTL pt, [in, out] DWORD *pdwEffect)
#define S_OK
Definition: intsafe.h:52
#define FAILED(hr)
Definition: intsafe.h:51
static HICON
Definition: imagelist.c:80
static const CLSID *static CLSID *static const GUID VARIANT VARIANT *static IServiceProvider DWORD *static HMENU
Definition: ordinal.c:63
static HWND child
Definition: cursoricon.c:298
static int int const SCRIPT_CONTROL const SCRIPT_STATE SCRIPT_ITEM * pItems
Definition: usp10.c:62
HICON hIcon
Definition: msconfig.c:44
unsigned __int3264 UINT_PTR
Definition: mstsclib_h.h:274
unsigned int UINT
Definition: ndis.h:50
#define LOWORD(l)
Definition: pedump.c:82
LPITEMIDLIST WINAPI ILCombine(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
Definition: pidl.c:816
#define REFIID
Definition: guiddef.h:118
DWORD_PTR WINAPI SHGetFileInfoW(LPCWSTR path, DWORD dwFileAttributes, SHFILEINFOW *psfi, UINT sizeofpsfi, UINT flags)
Definition: shell32_main.c:430
#define SHGFI_ICON
Definition: shellapi.h:165
#define SHGFI_TYPENAME
Definition: shellapi.h:168
#define SHGFI_SMALLICON
Definition: shellapi.h:177
#define SHGFI_PIDL
Definition: shellapi.h:181
HRESULT hr
Definition: shlfolder.c:183
#define CSIDL_SENDTO
Definition: shlobj.h:2182
#define IDS_SENDTO_MENU
Definition: shresdef.h:247
#define TRACE(s)
Definition: solgame.cpp:4
PITEMID_CHILD pidlChild
Definition: CSendToMenu.h:36
HICON hIcon
Definition: shellapi.h:373
ULONG_PTR itemData
Definition: winuser.h:3096
ULONG_PTR dwItemData
Definition: winuser.h:3271
LPWSTR dwTypeData
Definition: winuser.h:3272
LONG bottom
Definition: windef.h:309
LONG top
Definition: windef.h:307
int32_t INT
Definition: typedefs.h:58
uint32_t ULONG_PTR
Definition: typedefs.h:65
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
LONG_PTR LPARAM
Definition: windef.h:208
LONG_PTR LRESULT
Definition: windef.h:209
UINT_PTR WPARAM
Definition: windef.h:207
#define SEVERITY_SUCCESS
Definition: winerror.h:64
#define E_POINTER
Definition: winerror.h:2365
#define DI_NORMAL
Definition: wingdi.h:72
#define MK_SHIFT
Definition: winuser.h:2372
#define MIIM_ID
Definition: winuser.h:725
#define SM_CXMENUCHECK
Definition: winuser.h:1034
BOOL WINAPI InsertMenuW(_In_ HMENU, _In_ UINT, _In_ UINT, _In_ UINT_PTR, _In_opt_ LPCWSTR)
HMENU WINAPI CreateMenu(void)
Definition: menu.c:829
#define MF_STRING
Definition: winuser.h:138
#define VK_CONTROL
Definition: winuser.h:2206
#define SM_CYSMICON
Definition: winuser.h:1016
BOOL WINAPI SetMenuItemInfoW(_In_ HMENU, _In_ UINT, _In_ BOOL, _In_ LPCMENUITEMINFOW)
#define WM_DRAWITEM
Definition: winuser.h:1648
#define MIIM_STATE
Definition: winuser.h:724
#define SM_CXSMICON
Definition: winuser.h:1015
#define MIIM_SUBMENU
Definition: winuser.h:726
#define MF_ENABLED
Definition: winuser.h:128
#define MK_CONTROL
Definition: winuser.h:2373
#define MIIM_BITMAP
Definition: winuser.h:731
#define MF_BYPOSITION
Definition: winuser.h:203
BOOL WINAPI DrawIconEx(_In_ HDC, _In_ int, _In_ int, _In_ HICON, _In_ int, _In_ int, _In_ UINT, _In_opt_ HBRUSH, _In_ UINT)
Definition: cursoricon.c:2114
#define WM_MEASUREITEM
Definition: winuser.h:1649
#define MFS_ENABLED
Definition: winuser.h:753
BOOL WINAPI DestroyMenu(_In_ HMENU)
BOOL WINAPI GetMenuItemInfoW(_In_ HMENU, _In_ UINT, _In_ BOOL, _Inout_ LPMENUITEMINFOW)
#define MK_LBUTTON
Definition: winuser.h:2370
#define VK_SHIFT
Definition: winuser.h:2205
#define MFT_STRING
Definition: winuser.h:749
SHORT WINAPI GetAsyncKeyState(_In_ int)
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
#define ODT_MENU
Definition: winuser.h:2540
#define HBMMENU_CALLBACK
Definition: winuser.h:2633
int WINAPI GetSystemMetrics(_In_ int)
#define MIIM_DATA
Definition: winuser.h:729
#define MIIM_TYPE
Definition: winuser.h:728
BOOL WINAPI InsertMenuItemW(_In_ HMENU, _In_ UINT, _In_ BOOL, _In_ LPCMENUITEMINFOW)
BOOL WINAPI AppendMenuW(_In_ HMENU, _In_ UINT, _In_ UINT_PTR, _In_opt_ LPCWSTR)
#define MF_GRAYED
Definition: winuser.h:129
#define MF_DISABLED
Definition: winuser.h:130
#define IID_PPV_ARG(Itype, ppType)
char * LPSTR
Definition: xmlstorage.h:182
WCHAR * LPWSTR
Definition: xmlstorage.h:184