shell32: Update to IContextMenu3.
[wine/multimedia.git] / dlls / shell32 / shlview_cmenu.c
blob31db01e1877cf62f65fa902251b9ea9396ef28de
1 /*
2 * IContextMenu for items in the shellview
4 * Copyright 1998-2000 Juergen Schmied <juergen.schmied@debitel.net>,
5 * <juergen.schmied@metronet.de>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <string.h>
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
28 #include "winerror.h"
29 #include "wine/debug.h"
31 #include "windef.h"
32 #include "wingdi.h"
33 #include "pidl.h"
34 #include "undocshell.h"
35 #include "shlobj.h"
36 #include "winreg.h"
37 #include "prsht.h"
39 #include "shell32_main.h"
40 #include "shellfolder.h"
42 #include "shresdef.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
46 typedef struct
48 IContextMenu3 IContextMenu3_iface;
49 LONG ref;
51 IShellFolder* parent;
52 UINT verb_offset;
54 /* item menu data */
55 LPITEMIDLIST pidl; /* root pidl */
56 LPITEMIDLIST *apidl; /* array of child pidls */
57 UINT cidl;
58 BOOL allvalues;
60 /* background menu data */
61 BOOL desktop;
62 } ContextMenu;
64 static inline ContextMenu *impl_from_IContextMenu3(IContextMenu3 *iface)
66 return CONTAINING_RECORD(iface, ContextMenu, IContextMenu3_iface);
69 static HRESULT WINAPI ContextMenu_QueryInterface(IContextMenu3 *iface, REFIID riid, LPVOID *ppvObj)
71 ContextMenu *This = impl_from_IContextMenu3(iface);
73 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObj);
75 *ppvObj = NULL;
77 if (IsEqualIID(riid, &IID_IUnknown) ||
78 IsEqualIID(riid, &IID_IContextMenu) ||
79 IsEqualIID(riid, &IID_IContextMenu2) ||
80 IsEqualIID(riid, &IID_IContextMenu3))
82 *ppvObj = This;
84 else if (IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
86 FIXME("-- LPSHELLEXTINIT pointer requested\n");
89 if(*ppvObj)
91 IContextMenu3_AddRef(iface);
92 return S_OK;
95 TRACE("-- Interface: E_NOINTERFACE\n");
96 return E_NOINTERFACE;
99 static ULONG WINAPI ContextMenu_AddRef(IContextMenu3 *iface)
101 ContextMenu *This = impl_from_IContextMenu3(iface);
102 ULONG ref = InterlockedIncrement(&This->ref);
103 TRACE("(%p)->(%u)\n", This, ref);
104 return ref;
107 static ULONG WINAPI ContextMenu_Release(IContextMenu3 *iface)
109 ContextMenu *This = impl_from_IContextMenu3(iface);
110 ULONG ref = InterlockedDecrement(&This->ref);
112 TRACE("(%p)->(%u)\n", This, ref);
114 if (!ref)
116 if(This->parent)
117 IShellFolder_Release(This->parent);
119 SHFree(This->pidl);
120 _ILFreeaPidl(This->apidl, This->cidl);
122 HeapFree(GetProcessHeap(), 0, This);
125 return ref;
128 static HRESULT WINAPI ItemMenu_QueryContextMenu(
129 IContextMenu3 *iface,
130 HMENU hmenu,
131 UINT indexMenu,
132 UINT idCmdFirst,
133 UINT idCmdLast,
134 UINT uFlags)
136 ContextMenu *This = impl_from_IContextMenu3(iface);
137 INT uIDMax;
139 TRACE("(%p)->(%p %d 0x%x 0x%x 0x%x )\n", This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
141 This->verb_offset = idCmdFirst;
143 if(!(CMF_DEFAULTONLY & uFlags) && This->cidl > 0)
145 HMENU hmenures = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_SHV_FILE));
147 if(uFlags & CMF_EXPLORE)
148 RemoveMenu(hmenures, FCIDM_SHVIEW_OPEN, MF_BYCOMMAND);
150 uIDMax = Shell_MergeMenus(hmenu, GetSubMenu(hmenures, 0), indexMenu, idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
152 DestroyMenu(hmenures);
154 if(This->allvalues)
156 MENUITEMINFOW mi;
157 WCHAR str[255];
158 mi.cbSize = sizeof(mi);
159 mi.fMask = MIIM_ID | MIIM_STRING | MIIM_FTYPE;
160 mi.dwTypeData = str;
161 mi.cch = 255;
162 GetMenuItemInfoW(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND, &mi);
163 RemoveMenu(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND);
165 mi.cbSize = sizeof(mi);
166 mi.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
167 mi.dwTypeData = str;
168 mi.fState = MFS_ENABLED;
169 mi.wID = FCIDM_SHVIEW_EXPLORE;
170 mi.fType = MFT_STRING;
171 InsertMenuItemW(hmenu, (uFlags & CMF_EXPLORE) ? 1 : 2, MF_BYPOSITION, &mi);
174 SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
176 if(uFlags & ~CMF_CANRENAME)
177 RemoveMenu(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND);
178 else
180 UINT enable = MF_BYCOMMAND;
182 /* can't rename more than one item at a time*/
183 if (!This->apidl || This->cidl > 1)
184 enable |= MFS_DISABLED;
185 else
187 DWORD attr = SFGAO_CANRENAME;
189 IShellFolder_GetAttributesOf(This->parent, 1, (LPCITEMIDLIST*)This->apidl, &attr);
190 enable |= (attr & SFGAO_CANRENAME) ? MFS_ENABLED : MFS_DISABLED;
193 EnableMenuItem(hmenu, FCIDM_SHVIEW_RENAME, enable);
196 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, uIDMax-idCmdFirst);
198 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
201 /**************************************************************************
202 * DoOpenExplore
204 * for folders only
207 static void DoOpenExplore(ContextMenu *This, HWND hwnd, LPCSTR verb)
209 UINT i, bFolderFound = FALSE;
210 LPITEMIDLIST pidlFQ;
211 SHELLEXECUTEINFOA sei;
213 /* Find the first item in the list that is not a value. These commands
214 should never be invoked if there isn't at least one folder item in the list.*/
216 for(i = 0; i<This->cidl; i++)
218 if(!_ILIsValue(This->apidl[i]))
220 bFolderFound = TRUE;
221 break;
225 if (!bFolderFound) return;
227 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
229 ZeroMemory(&sei, sizeof(sei));
230 sei.cbSize = sizeof(sei);
231 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
232 sei.lpIDList = pidlFQ;
233 sei.lpClass = "Folder";
234 sei.hwnd = hwnd;
235 sei.nShow = SW_SHOWNORMAL;
236 sei.lpVerb = verb;
237 ShellExecuteExA(&sei);
238 SHFree(pidlFQ);
241 /**************************************************************************
242 * DoDelete
244 * deletes the currently selected items
246 static void DoDelete(ContextMenu *This)
248 ISFHelper *helper;
250 IShellFolder_QueryInterface(This->parent, &IID_ISFHelper, (void**)&helper);
251 if (helper)
253 ISFHelper_DeleteItems(helper, This->cidl, (LPCITEMIDLIST*)This->apidl);
254 ISFHelper_Release(helper);
258 /**************************************************************************
259 * DoCopyOrCut
261 * copies the currently selected items into the clipboard
263 static BOOL DoCopyOrCut(ContextMenu *This, HWND hwnd, BOOL cut)
265 IDataObject *dataobject;
266 IShellBrowser *browser;
267 IShellView *view;
269 TRACE("(%p)->(wnd=%p, cut=%d)\n", This, hwnd, cut);
271 /* get the active IShellView */
272 if ((browser = (IShellBrowser*)SendMessageA(hwnd, CWM_GETISHELLBROWSER, 0, 0)))
274 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(browser, &view)))
276 if (SUCCEEDED(IShellView_GetItemObject(view, SVGIO_SELECTION, &IID_IDataObject, (void**)&dataobject)))
278 OleSetClipboard(dataobject);
279 IDataObject_Release(dataobject);
281 IShellView_Release(view);
285 return TRUE;
288 /**************************************************************************
289 * Properties_AddPropSheetCallback
291 * Used by DoOpenProperties through SHCreatePropSheetExtArrayEx to add
292 * propertysheet pages from shell extensions.
294 static BOOL CALLBACK Properties_AddPropSheetCallback(HPROPSHEETPAGE hpage, LPARAM lparam)
296 LPPROPSHEETHEADERW psh = (LPPROPSHEETHEADERW) lparam;
297 psh->u3.phpage[psh->nPages++] = hpage;
299 return TRUE;
302 static void DoOpenProperties(ContextMenu *This, HWND hwnd)
304 static const UINT MAX_PROP_PAGES = 99;
305 static const WCHAR wszFolder[] = {'F','o','l','d','e','r', 0};
306 static const WCHAR wszFiletypeAll[] = {'*',0};
307 LPSHELLFOLDER lpDesktopSF;
308 LPSHELLFOLDER lpSF;
309 LPDATAOBJECT lpDo;
310 WCHAR wszFiletype[MAX_PATH];
311 WCHAR wszFilename[MAX_PATH];
312 PROPSHEETHEADERW psh;
313 HPROPSHEETPAGE hpages[MAX_PROP_PAGES];
314 HPSXA hpsxa;
315 UINT ret;
317 TRACE("(%p)->(wnd=%p)\n", This, hwnd);
319 ZeroMemory(&psh, sizeof(PROPSHEETHEADERW));
320 psh.dwSize = sizeof (PROPSHEETHEADERW);
321 psh.hwndParent = hwnd;
322 psh.dwFlags = PSH_PROPTITLE;
323 psh.nPages = 0;
324 psh.u3.phpage = hpages;
325 psh.u2.nStartPage = 0;
327 _ILSimpleGetTextW(This->apidl[0], (LPVOID)wszFilename, MAX_PATH);
328 psh.pszCaption = (LPCWSTR)wszFilename;
330 /* Find out where to look for the shell extensions */
331 if (_ILIsValue(This->apidl[0]))
333 char sTemp[64];
334 sTemp[0] = 0;
335 if (_ILGetExtension(This->apidl[0], sTemp, 64))
337 HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE);
338 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, wszFiletype, MAX_PATH);
340 else
342 wszFiletype[0] = 0;
345 else if (_ILIsFolder(This->apidl[0]))
347 lstrcpynW(wszFiletype, wszFolder, 64);
349 else if (_ILIsSpecialFolder(This->apidl[0]))
351 LPGUID folderGUID;
352 static const WCHAR wszclsid[] = {'C','L','S','I','D','\\', 0};
353 folderGUID = _ILGetGUIDPointer(This->apidl[0]);
354 lstrcpyW(wszFiletype, wszclsid);
355 StringFromGUID2(folderGUID, &wszFiletype[6], MAX_PATH - 6);
357 else
359 FIXME("Requested properties for unknown type.\n");
360 return;
363 /* Get a suitable DataObject for accessing the files */
364 SHGetDesktopFolder(&lpDesktopSF);
365 if (_ILIsPidlSimple(This->pidl))
367 ret = IShellFolder_GetUIObjectOf(lpDesktopSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
368 &IID_IDataObject, NULL, (LPVOID *)&lpDo);
369 IShellFolder_Release(lpDesktopSF);
371 else
373 IShellFolder_BindToObject(lpDesktopSF, This->pidl, NULL, &IID_IShellFolder, (LPVOID*) &lpSF);
374 ret = IShellFolder_GetUIObjectOf(lpSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
375 &IID_IDataObject, NULL, (LPVOID *)&lpDo);
376 IShellFolder_Release(lpSF);
377 IShellFolder_Release(lpDesktopSF);
380 if (SUCCEEDED(ret))
382 hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletype, MAX_PROP_PAGES - psh.nPages, lpDo);
383 if (hpsxa != NULL)
385 SHAddFromPropSheetExtArray(hpsxa, Properties_AddPropSheetCallback, (LPARAM)&psh);
386 SHDestroyPropSheetExtArray(hpsxa);
388 hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletypeAll, MAX_PROP_PAGES - psh.nPages, lpDo);
389 if (hpsxa != NULL)
391 SHAddFromPropSheetExtArray(hpsxa, Properties_AddPropSheetCallback, (LPARAM)&psh);
392 SHDestroyPropSheetExtArray(hpsxa);
394 IDataObject_Release(lpDo);
397 if (psh.nPages)
398 PropertySheetW(&psh);
399 else
400 FIXME("No property pages found.\n");
403 static HRESULT WINAPI ItemMenu_InvokeCommand(
404 IContextMenu3 *iface,
405 LPCMINVOKECOMMANDINFO lpcmi)
407 ContextMenu *This = impl_from_IContextMenu3(iface);
409 if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
410 FIXME("Is an EX structure\n");
412 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
414 if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
416 TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
417 return E_INVALIDARG;
420 if (HIWORD(lpcmi->lpVerb) == 0)
422 switch(LOWORD(lpcmi->lpVerb - This->verb_offset))
424 case FCIDM_SHVIEW_EXPLORE:
425 TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
426 DoOpenExplore(This, lpcmi->hwnd, "explore");
427 break;
428 case FCIDM_SHVIEW_OPEN:
429 TRACE("Verb FCIDM_SHVIEW_OPEN\n");
430 DoOpenExplore(This, lpcmi->hwnd, "open");
431 break;
432 case FCIDM_SHVIEW_RENAME:
434 IShellBrowser *browser;
436 /* get the active IShellView */
437 browser = (IShellBrowser*)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER, 0, 0);
438 if (browser)
440 IShellView *view;
442 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(browser, &view)))
444 TRACE("(shellview=%p)\n", view);
445 IShellView_SelectItem(view, This->apidl[0],
446 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
447 IShellView_Release(view);
450 break;
452 case FCIDM_SHVIEW_DELETE:
453 TRACE("Verb FCIDM_SHVIEW_DELETE\n");
454 DoDelete(This);
455 break;
456 case FCIDM_SHVIEW_COPY:
457 TRACE("Verb FCIDM_SHVIEW_COPY\n");
458 DoCopyOrCut(This, lpcmi->hwnd, FALSE);
459 break;
460 case FCIDM_SHVIEW_CUT:
461 TRACE("Verb FCIDM_SHVIEW_CUT\n");
462 DoCopyOrCut(This, lpcmi->hwnd, TRUE);
463 break;
464 case FCIDM_SHVIEW_PROPERTIES:
465 TRACE("Verb FCIDM_SHVIEW_PROPERTIES\n");
466 DoOpenProperties(This, lpcmi->hwnd);
467 break;
468 default:
469 FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb)-This->verb_offset);
470 return E_INVALIDARG;
473 else
475 TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
476 if (strcmp(lpcmi->lpVerb,"delete")==0)
477 DoDelete(This);
478 else if (strcmp(lpcmi->lpVerb,"properties")==0)
479 DoOpenProperties(This, lpcmi->hwnd);
480 else {
481 FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
482 return E_FAIL;
485 return S_OK;
488 static HRESULT WINAPI ItemMenu_GetCommandString(
489 IContextMenu3 *iface,
490 UINT_PTR idCommand,
491 UINT uFlags,
492 UINT* lpReserved,
493 LPSTR lpszName,
494 UINT uMaxNameLen)
496 ContextMenu *This = impl_from_IContextMenu3(iface);
497 HRESULT hr = E_INVALIDARG;
499 TRACE("(%p)->(%lx flags=%x %p name=%p len=%x)\n", This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
501 switch(uFlags)
503 case GCS_HELPTEXTA:
504 case GCS_HELPTEXTW:
505 hr = E_NOTIMPL;
506 break;
508 case GCS_VERBA:
509 switch(idCommand-This->verb_offset)
511 case FCIDM_SHVIEW_RENAME:
512 strcpy(lpszName, "rename");
513 hr = S_OK;
514 break;
516 break;
518 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
519 case, you need to do the lstrcpyW to the pointer passed.*/
520 case GCS_VERBW:
521 switch(idCommand-This->verb_offset)
523 case FCIDM_SHVIEW_RENAME:
524 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
525 hr = S_OK;
526 break;
528 break;
530 case GCS_VALIDATEA:
531 case GCS_VALIDATEW:
532 hr = S_OK;
533 break;
535 TRACE("-- (%p)->(name=%s)\n", This, lpszName);
536 return hr;
539 /**************************************************************************
540 * NOTES
541 * should be only in IContextMenu2 and IContextMenu3
542 * is nevertheless called from word95
544 static HRESULT WINAPI ContextMenu_HandleMenuMsg(IContextMenu3 *iface, UINT msg,
545 WPARAM wParam, LPARAM lParam)
547 ContextMenu *This = impl_from_IContextMenu3(iface);
548 FIXME("(%p)->(0x%x 0x%lx 0x%lx): stub\n", This, msg, wParam, lParam);
549 return E_NOTIMPL;
552 static HRESULT WINAPI ContextMenu_HandleMenuMsg2(IContextMenu3 *iface, UINT msg,
553 WPARAM wParam, LPARAM lParam, LRESULT *result)
555 ContextMenu *This = impl_from_IContextMenu3(iface);
556 FIXME("(%p)->(0x%x 0x%lx 0x%lx %p): stub\n", This, msg, wParam, lParam, result);
557 return E_NOTIMPL;
560 static const IContextMenu3Vtbl ItemContextMenuVtbl =
562 ContextMenu_QueryInterface,
563 ContextMenu_AddRef,
564 ContextMenu_Release,
565 ItemMenu_QueryContextMenu,
566 ItemMenu_InvokeCommand,
567 ItemMenu_GetCommandString,
568 ContextMenu_HandleMenuMsg,
569 ContextMenu_HandleMenuMsg2
572 IContextMenu2 *ItemMenu_Constructor(IShellFolder *parent, LPCITEMIDLIST pidl, const LPCITEMIDLIST *apidl, UINT cidl)
574 ContextMenu* This;
575 UINT u;
577 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
578 This->IContextMenu3_iface.lpVtbl = &ItemContextMenuVtbl;
579 This->ref = 1;
580 This->verb_offset = 0;
581 This->parent = parent;
582 if (parent) IShellFolder_AddRef(parent);
584 This->pidl = ILClone(pidl);
585 This->apidl = _ILCopyaPidl(apidl, cidl);
586 This->cidl = cidl;
587 This->allvalues = TRUE;
589 This->desktop = FALSE;
591 for(u = 0; u < cidl; u++)
592 This->allvalues &= (_ILIsValue(apidl[u]) ? 1 : 0);
594 TRACE("(%p)\n", This);
596 return (IContextMenu2*)&This->IContextMenu3_iface;
599 /* Background menu implementation */
600 static HRESULT WINAPI BackgroundMenu_QueryContextMenu(
601 IContextMenu3 *iface,
602 HMENU hMenu,
603 UINT indexMenu,
604 UINT idCmdFirst,
605 UINT idCmdLast,
606 UINT uFlags)
608 ContextMenu *This = impl_from_IContextMenu3(iface);
609 HMENU hMyMenu;
610 UINT idMax;
611 HRESULT hr;
613 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",
614 This, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
616 This->verb_offset = idCmdFirst;
618 hMyMenu = LoadMenuA(shell32_hInstance, "MENU_002");
619 if (uFlags & CMF_DEFAULTONLY)
621 HMENU ourMenu = GetSubMenu(hMyMenu,0);
622 UINT oldDef = GetMenuDefaultItem(hMenu,TRUE,GMDI_USEDISABLED);
623 UINT newDef = GetMenuDefaultItem(ourMenu,TRUE,GMDI_USEDISABLED);
624 if (newDef != oldDef)
625 SetMenuDefaultItem(hMenu,newDef,TRUE);
626 if (newDef!=0xFFFFFFFF)
627 hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, newDef+1);
628 else
629 hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
631 else
633 idMax = Shell_MergeMenus (hMenu, GetSubMenu(hMyMenu,0), indexMenu,
634 idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
635 hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, idMax-idCmdFirst);
637 DestroyMenu(hMyMenu);
639 TRACE("(%p)->returning 0x%x\n",This,hr);
640 return hr;
643 static void DoNewFolder(ContextMenu *This, IShellView *view)
645 ISFHelper *helper;
647 IShellFolder_QueryInterface(This->parent, &IID_ISFHelper, (void**)&helper);
648 if (helper)
650 WCHAR nameW[MAX_PATH];
651 LPITEMIDLIST pidl;
653 ISFHelper_GetUniqueName(helper, nameW, MAX_PATH);
654 ISFHelper_AddFolder(helper, 0, nameW, &pidl);
656 if (view)
658 /* if we are in a shellview do labeledit */
659 IShellView_SelectItem(view,
660 pidl,(SVSI_DESELECTOTHERS | SVSI_EDIT | SVSI_ENSUREVISIBLE
661 |SVSI_FOCUSED|SVSI_SELECT));
664 SHFree(pidl);
665 ISFHelper_Release(helper);
669 static BOOL DoPaste(ContextMenu *This)
671 BOOL bSuccess = FALSE;
672 IDataObject * pda;
674 TRACE("\n");
676 if(SUCCEEDED(OleGetClipboard(&pda)))
678 STGMEDIUM medium;
679 FORMATETC formatetc;
681 TRACE("pda=%p\n", pda);
683 /* Set the FORMATETC structure*/
684 InitFormatEtc(formatetc, RegisterClipboardFormatW(CFSTR_SHELLIDLISTW), TYMED_HGLOBAL);
686 /* Get the pidls from IDataObject */
687 if(SUCCEEDED(IDataObject_GetData(pda,&formatetc,&medium)))
689 LPITEMIDLIST * apidl;
690 LPITEMIDLIST pidl;
691 IShellFolder *psfFrom = NULL, *psfDesktop;
693 LPIDA lpcida = GlobalLock(medium.u.hGlobal);
694 TRACE("cida=%p\n", lpcida);
696 apidl = _ILCopyCidaToaPidl(&pidl, lpcida);
698 /* bind to the source shellfolder */
699 SHGetDesktopFolder(&psfDesktop);
700 if(psfDesktop)
702 IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (LPVOID*)&psfFrom);
703 IShellFolder_Release(psfDesktop);
706 if (psfFrom)
708 /* get source and destination shellfolder */
709 ISFHelper *psfhlpdst, *psfhlpsrc;
710 IShellFolder_QueryInterface(This->parent, &IID_ISFHelper, (void**)&psfhlpdst);
711 IShellFolder_QueryInterface(psfFrom, &IID_ISFHelper, (void**)&psfhlpsrc);
713 /* do the copy/move */
714 if (psfhlpdst && psfhlpsrc)
716 ISFHelper_CopyItems(psfhlpdst, psfFrom, lpcida->cidl, (LPCITEMIDLIST*)apidl);
717 /* FIXME handle move
718 ISFHelper_DeleteItems(psfhlpsrc, lpcida->cidl, apidl);
721 if(psfhlpdst) ISFHelper_Release(psfhlpdst);
722 if(psfhlpsrc) ISFHelper_Release(psfhlpsrc);
723 IShellFolder_Release(psfFrom);
726 _ILFreeaPidl(apidl, lpcida->cidl);
727 SHFree(pidl);
729 /* release the medium*/
730 ReleaseStgMedium(&medium);
732 IDataObject_Release(pda);
734 #if 0
735 HGLOBAL hMem;
737 OpenClipboard(NULL);
738 hMem = GetClipboardData(CF_HDROP);
740 if(hMem)
742 char * pDropFiles = GlobalLock(hMem);
743 if(pDropFiles)
745 int len, offset = sizeof(DROPFILESTRUCT);
747 while( pDropFiles[offset] != 0)
749 len = strlen(pDropFiles + offset);
750 TRACE("%s\n", pDropFiles + offset);
751 offset += len+1;
754 GlobalUnlock(hMem);
756 CloseClipboard();
757 #endif
758 return bSuccess;
761 static HRESULT WINAPI BackgroundMenu_InvokeCommand(
762 IContextMenu3 *iface,
763 LPCMINVOKECOMMANDINFO lpcmi)
765 ContextMenu *This = impl_from_IContextMenu3(iface);
766 IShellBrowser *browser;
767 IShellView *view = NULL;
768 HWND hWnd = NULL;
770 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n", This, lpcmi, lpcmi->lpVerb, lpcmi->hwnd);
772 /* get the active IShellView */
773 if ((browser = (IShellBrowser*)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER, 0, 0)))
775 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(browser, &view)))
776 IShellView_GetWindow(view, &hWnd);
779 if(HIWORD(lpcmi->lpVerb))
781 TRACE("%s\n", debugstr_a(lpcmi->lpVerb));
783 if (!strcmp(lpcmi->lpVerb, CMDSTR_NEWFOLDERA))
785 DoNewFolder(This, view);
787 else if (!strcmp(lpcmi->lpVerb, CMDSTR_VIEWLISTA))
789 if (hWnd) SendMessageA(hWnd, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_LISTVIEW, 0), 0);
791 else if (!strcmp(lpcmi->lpVerb, CMDSTR_VIEWDETAILSA))
793 if (hWnd) SendMessageA(hWnd, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_REPORTVIEW, 0), 0);
795 else
797 FIXME("please report: unknown verb %s\n", debugstr_a(lpcmi->lpVerb));
800 else
802 switch (LOWORD(lpcmi->lpVerb) - This->verb_offset)
804 case FCIDM_SHVIEW_REFRESH:
805 if (view) IShellView_Refresh(view);
806 break;
808 case FCIDM_SHVIEW_NEWFOLDER:
809 DoNewFolder(This, view);
810 break;
812 case FCIDM_SHVIEW_INSERT:
813 DoPaste(This);
814 break;
816 case FCIDM_SHVIEW_PROPERTIES:
817 if (This->desktop) {
818 ShellExecuteA(lpcmi->hwnd, "open", "rundll32.exe shell32.dll,Control_RunDLL desk.cpl", NULL, NULL, SW_SHOWNORMAL);
819 } else {
820 FIXME("launch item properties dialog\n");
822 break;
824 default:
825 /* if it's an id just pass it to the parent shv */
826 if (hWnd) SendMessageA(hWnd, WM_COMMAND, MAKEWPARAM(LOWORD(lpcmi->lpVerb), 0), 0);
827 break;
831 if (view)
832 IShellView_Release(view);
834 return S_OK;
837 static HRESULT WINAPI BackgroundMenu_GetCommandString(
838 IContextMenu3 *iface,
839 UINT_PTR idCommand,
840 UINT uFlags,
841 UINT* lpReserved,
842 LPSTR lpszName,
843 UINT uMaxNameLen)
845 ContextMenu *This = impl_from_IContextMenu3(iface);
847 TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
849 /* test the existence of the menu items, the file dialog enables
850 the buttons according to this */
851 if (uFlags == GCS_VALIDATEA)
853 if(HIWORD(idCommand))
855 if (!strcmp((LPSTR)idCommand, CMDSTR_VIEWLISTA) ||
856 !strcmp((LPSTR)idCommand, CMDSTR_VIEWDETAILSA) ||
857 !strcmp((LPSTR)idCommand, CMDSTR_NEWFOLDERA))
859 return S_OK;
864 FIXME("unknown command string\n");
865 return E_FAIL;
868 static const IContextMenu3Vtbl BackgroundContextMenuVtbl =
870 ContextMenu_QueryInterface,
871 ContextMenu_AddRef,
872 ContextMenu_Release,
873 BackgroundMenu_QueryContextMenu,
874 BackgroundMenu_InvokeCommand,
875 BackgroundMenu_GetCommandString,
876 ContextMenu_HandleMenuMsg,
877 ContextMenu_HandleMenuMsg2
880 IContextMenu2 *BackgroundMenu_Constructor(IShellFolder *parent, BOOL desktop)
882 ContextMenu *This;
884 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
885 This->IContextMenu3_iface.lpVtbl = &BackgroundContextMenuVtbl;
886 This->ref = 1;
887 This->parent = parent;
888 This->verb_offset = 0;
890 This->pidl = NULL;
891 This->apidl = NULL;
892 This->cidl = 0;
893 This->allvalues = FALSE;
895 This->desktop = desktop;
896 if (parent) IShellFolder_AddRef(parent);
898 TRACE("(%p)\n", This);
899 return (IContextMenu2*)&This->IContextMenu3_iface;