winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / shell32 / shlview_cmenu.c
blob8170011ea65cd5335daaa562986a56baa8e2f6c9
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;
53 /* item menu data */
54 LPITEMIDLIST pidl; /* root pidl */
55 LPITEMIDLIST *apidl; /* array of child pidls */
56 UINT cidl;
57 BOOL allvalues;
59 /* background menu data */
60 BOOL desktop;
61 } ContextMenu;
63 static inline ContextMenu *impl_from_IContextMenu3(IContextMenu3 *iface)
65 return CONTAINING_RECORD(iface, ContextMenu, IContextMenu3_iface);
68 static HRESULT WINAPI ContextMenu_QueryInterface(IContextMenu3 *iface, REFIID riid, LPVOID *ppvObj)
70 ContextMenu *This = impl_from_IContextMenu3(iface);
72 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObj);
74 *ppvObj = NULL;
76 if (IsEqualIID(riid, &IID_IUnknown) ||
77 IsEqualIID(riid, &IID_IContextMenu) ||
78 IsEqualIID(riid, &IID_IContextMenu2) ||
79 IsEqualIID(riid, &IID_IContextMenu3))
81 *ppvObj = &This->IContextMenu3_iface;
83 else if (IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
85 FIXME("-- LPSHELLEXTINIT pointer requested\n");
88 if(*ppvObj)
90 IContextMenu3_AddRef(iface);
91 return S_OK;
94 TRACE("-- Interface: E_NOINTERFACE\n");
95 return E_NOINTERFACE;
98 static ULONG WINAPI ContextMenu_AddRef(IContextMenu3 *iface)
100 ContextMenu *This = impl_from_IContextMenu3(iface);
101 ULONG ref = InterlockedIncrement(&This->ref);
102 TRACE("(%p)->(%u)\n", This, ref);
103 return ref;
106 static ULONG WINAPI ContextMenu_Release(IContextMenu3 *iface)
108 ContextMenu *This = impl_from_IContextMenu3(iface);
109 ULONG ref = InterlockedDecrement(&This->ref);
111 TRACE("(%p)->(%u)\n", This, ref);
113 if (!ref)
115 if(This->parent)
116 IShellFolder_Release(This->parent);
118 SHFree(This->pidl);
119 _ILFreeaPidl(This->apidl, This->cidl);
121 HeapFree(GetProcessHeap(), 0, This);
124 return ref;
127 static HRESULT WINAPI ItemMenu_QueryContextMenu(
128 IContextMenu3 *iface,
129 HMENU hmenu,
130 UINT indexMenu,
131 UINT idCmdFirst,
132 UINT idCmdLast,
133 UINT uFlags)
135 ContextMenu *This = impl_from_IContextMenu3(iface);
136 INT uIDMax;
138 TRACE("(%p)->(%p %d 0x%x 0x%x 0x%x )\n", This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
140 if(!(CMF_DEFAULTONLY & uFlags) && This->cidl > 0)
142 HMENU hmenures = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_SHV_FILE));
144 if(uFlags & CMF_EXPLORE)
145 RemoveMenu(hmenures, FCIDM_SHVIEW_OPEN, MF_BYCOMMAND);
147 uIDMax = Shell_MergeMenus(hmenu, GetSubMenu(hmenures, 0), indexMenu, idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
149 DestroyMenu(hmenures);
151 if(This->allvalues)
153 MENUITEMINFOW mi;
154 WCHAR str[255];
155 mi.cbSize = sizeof(mi);
156 mi.fMask = MIIM_ID | MIIM_STRING | MIIM_FTYPE;
157 mi.dwTypeData = str;
158 mi.cch = 255;
159 GetMenuItemInfoW(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND, &mi);
160 RemoveMenu(hmenu, FCIDM_SHVIEW_EXPLORE + idCmdFirst, MF_BYCOMMAND);
162 mi.cbSize = sizeof(mi);
163 mi.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_STRING;
164 mi.dwTypeData = str;
165 mi.fState = MFS_ENABLED;
166 mi.wID = FCIDM_SHVIEW_EXPLORE;
167 mi.fType = MFT_STRING;
168 InsertMenuItemW(hmenu, (uFlags & CMF_EXPLORE) ? 1 : 2, MF_BYPOSITION, &mi);
171 SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
173 if(uFlags & ~CMF_CANRENAME)
174 RemoveMenu(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND);
175 else
177 UINT enable = MF_BYCOMMAND;
179 /* can't rename more than one item at a time*/
180 if (!This->apidl || This->cidl > 1)
181 enable |= MFS_DISABLED;
182 else
184 DWORD attr = SFGAO_CANRENAME;
186 IShellFolder_GetAttributesOf(This->parent, 1, (LPCITEMIDLIST*)This->apidl, &attr);
187 enable |= (attr & SFGAO_CANRENAME) ? MFS_ENABLED : MFS_DISABLED;
190 EnableMenuItem(hmenu, FCIDM_SHVIEW_RENAME, enable);
193 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, uIDMax-idCmdFirst);
195 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
198 /**************************************************************************
199 * DoOpenExplore
201 * for folders only
204 static void DoOpenExplore(ContextMenu *This, HWND hwnd, LPCSTR verb)
206 UINT i;
207 BOOL bFolderFound = FALSE;
208 LPITEMIDLIST pidlFQ;
209 SHELLEXECUTEINFOA sei;
211 /* Find the first item in the list that is not a value. These commands
212 should never be invoked if there isn't at least one folder item in the list.*/
214 for(i = 0; i<This->cidl; i++)
216 if(!_ILIsValue(This->apidl[i]))
218 bFolderFound = TRUE;
219 break;
223 if (!bFolderFound) return;
225 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
227 ZeroMemory(&sei, sizeof(sei));
228 sei.cbSize = sizeof(sei);
229 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
230 sei.lpIDList = pidlFQ;
231 sei.lpClass = "Folder";
232 sei.hwnd = hwnd;
233 sei.nShow = SW_SHOWNORMAL;
234 sei.lpVerb = verb;
235 ShellExecuteExA(&sei);
236 SHFree(pidlFQ);
239 /**************************************************************************
240 * DoDelete
242 * deletes the currently selected items
244 static void DoDelete(ContextMenu *This)
246 ISFHelper *helper;
248 IShellFolder_QueryInterface(This->parent, &IID_ISFHelper, (void**)&helper);
249 if (helper)
251 ISFHelper_DeleteItems(helper, This->cidl, (LPCITEMIDLIST*)This->apidl);
252 ISFHelper_Release(helper);
256 /**************************************************************************
257 * DoCopyOrCut
259 * copies the currently selected items into the clipboard
261 static void DoCopyOrCut(ContextMenu *This, HWND hwnd, BOOL cut)
263 IDataObject *dataobject;
265 TRACE("(%p)->(wnd=%p, cut=%d)\n", This, hwnd, cut);
267 if (SUCCEEDED(IShellFolder_GetUIObjectOf(This->parent, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl, &IID_IDataObject, 0, (void**)&dataobject)))
269 OleSetClipboard(dataobject);
270 IDataObject_Release(dataobject);
274 /**************************************************************************
275 * Properties_AddPropSheetCallback
277 * Used by DoOpenProperties through SHCreatePropSheetExtArrayEx to add
278 * propertysheet pages from shell extensions.
280 static BOOL CALLBACK Properties_AddPropSheetCallback(HPROPSHEETPAGE hpage, LPARAM lparam)
282 LPPROPSHEETHEADERW psh = (LPPROPSHEETHEADERW) lparam;
283 psh->u3.phpage[psh->nPages++] = hpage;
285 return TRUE;
288 #define MAX_PROP_PAGES 99
290 static void DoOpenProperties(ContextMenu *This, HWND hwnd)
292 static const WCHAR wszFolder[] = {'F','o','l','d','e','r', 0};
293 static const WCHAR wszFiletypeAll[] = {'*',0};
294 LPSHELLFOLDER lpDesktopSF;
295 LPSHELLFOLDER lpSF;
296 LPDATAOBJECT lpDo;
297 WCHAR wszFiletype[MAX_PATH];
298 WCHAR wszFilename[MAX_PATH];
299 PROPSHEETHEADERW psh;
300 HPROPSHEETPAGE hpages[MAX_PROP_PAGES];
301 HPSXA hpsxa;
302 UINT ret;
304 TRACE("(%p)->(wnd=%p)\n", This, hwnd);
306 ZeroMemory(&psh, sizeof(PROPSHEETHEADERW));
307 psh.dwSize = sizeof (PROPSHEETHEADERW);
308 psh.hwndParent = hwnd;
309 psh.dwFlags = PSH_PROPTITLE;
310 psh.nPages = 0;
311 psh.u3.phpage = hpages;
312 psh.u2.nStartPage = 0;
314 _ILSimpleGetTextW(This->apidl[0], (LPVOID)wszFilename, MAX_PATH);
315 psh.pszCaption = (LPCWSTR)wszFilename;
317 /* Find out where to look for the shell extensions */
318 if (_ILIsValue(This->apidl[0]))
320 char sTemp[64];
321 sTemp[0] = 0;
322 if (_ILGetExtension(This->apidl[0], sTemp, 64))
324 HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE);
325 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, wszFiletype, MAX_PATH);
327 else
329 wszFiletype[0] = 0;
332 else if (_ILIsFolder(This->apidl[0]))
334 lstrcpynW(wszFiletype, wszFolder, 64);
336 else if (_ILIsSpecialFolder(This->apidl[0]))
338 LPGUID folderGUID;
339 static const WCHAR wszclsid[] = {'C','L','S','I','D','\\', 0};
340 folderGUID = _ILGetGUIDPointer(This->apidl[0]);
341 lstrcpyW(wszFiletype, wszclsid);
342 StringFromGUID2(folderGUID, &wszFiletype[6], MAX_PATH - 6);
344 else
346 FIXME("Requested properties for unknown type.\n");
347 return;
350 /* Get a suitable DataObject for accessing the files */
351 SHGetDesktopFolder(&lpDesktopSF);
352 if (_ILIsPidlSimple(This->pidl))
354 ret = IShellFolder_GetUIObjectOf(lpDesktopSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
355 &IID_IDataObject, NULL, (LPVOID *)&lpDo);
356 IShellFolder_Release(lpDesktopSF);
358 else
360 IShellFolder_BindToObject(lpDesktopSF, This->pidl, NULL, &IID_IShellFolder, (LPVOID*) &lpSF);
361 ret = IShellFolder_GetUIObjectOf(lpSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
362 &IID_IDataObject, NULL, (LPVOID *)&lpDo);
363 IShellFolder_Release(lpSF);
364 IShellFolder_Release(lpDesktopSF);
367 if (SUCCEEDED(ret))
369 hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletype, MAX_PROP_PAGES - psh.nPages, lpDo);
370 if (hpsxa != NULL)
372 SHAddFromPropSheetExtArray(hpsxa, Properties_AddPropSheetCallback, (LPARAM)&psh);
373 SHDestroyPropSheetExtArray(hpsxa);
375 hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletypeAll, MAX_PROP_PAGES - psh.nPages, lpDo);
376 if (hpsxa != NULL)
378 SHAddFromPropSheetExtArray(hpsxa, Properties_AddPropSheetCallback, (LPARAM)&psh);
379 SHDestroyPropSheetExtArray(hpsxa);
381 IDataObject_Release(lpDo);
384 if (psh.nPages)
385 PropertySheetW(&psh);
386 else
387 FIXME("No property pages found.\n");
390 static HRESULT WINAPI ItemMenu_InvokeCommand(
391 IContextMenu3 *iface,
392 LPCMINVOKECOMMANDINFO lpcmi)
394 ContextMenu *This = impl_from_IContextMenu3(iface);
396 if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
397 FIXME("Is an EX structure\n");
399 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
401 if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
403 TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
404 return E_INVALIDARG;
407 if (HIWORD(lpcmi->lpVerb) == 0)
409 switch(LOWORD(lpcmi->lpVerb))
411 case FCIDM_SHVIEW_EXPLORE:
412 TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
413 DoOpenExplore(This, lpcmi->hwnd, "explore");
414 break;
415 case FCIDM_SHVIEW_OPEN:
416 TRACE("Verb FCIDM_SHVIEW_OPEN\n");
417 DoOpenExplore(This, lpcmi->hwnd, "open");
418 break;
419 case FCIDM_SHVIEW_RENAME:
421 IShellBrowser *browser;
423 /* get the active IShellView */
424 browser = (IShellBrowser*)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER, 0, 0);
425 if (browser)
427 IShellView *view;
429 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(browser, &view)))
431 TRACE("(shellview=%p)\n", view);
432 IShellView_SelectItem(view, This->apidl[0],
433 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
434 IShellView_Release(view);
437 break;
439 case FCIDM_SHVIEW_DELETE:
440 TRACE("Verb FCIDM_SHVIEW_DELETE\n");
441 DoDelete(This);
442 break;
443 case FCIDM_SHVIEW_COPY:
444 TRACE("Verb FCIDM_SHVIEW_COPY\n");
445 DoCopyOrCut(This, lpcmi->hwnd, FALSE);
446 break;
447 case FCIDM_SHVIEW_CUT:
448 TRACE("Verb FCIDM_SHVIEW_CUT\n");
449 DoCopyOrCut(This, lpcmi->hwnd, TRUE);
450 break;
451 case FCIDM_SHVIEW_PROPERTIES:
452 TRACE("Verb FCIDM_SHVIEW_PROPERTIES\n");
453 DoOpenProperties(This, lpcmi->hwnd);
454 break;
455 default:
456 FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb));
457 return E_INVALIDARG;
460 else
462 TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
463 if (strcmp(lpcmi->lpVerb,"delete")==0)
464 DoDelete(This);
465 else if (strcmp(lpcmi->lpVerb,"properties")==0)
466 DoOpenProperties(This, lpcmi->hwnd);
467 else {
468 FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
469 return E_FAIL;
472 return S_OK;
475 static HRESULT WINAPI ItemMenu_GetCommandString(
476 IContextMenu3 *iface,
477 UINT_PTR idCommand,
478 UINT uFlags,
479 UINT* lpReserved,
480 LPSTR lpszName,
481 UINT uMaxNameLen)
483 ContextMenu *This = impl_from_IContextMenu3(iface);
484 HRESULT hr = E_INVALIDARG;
486 TRACE("(%p)->(%lx flags=%x %p name=%p len=%x)\n", This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
488 switch(uFlags)
490 case GCS_HELPTEXTA:
491 case GCS_HELPTEXTW:
492 hr = E_NOTIMPL;
493 break;
495 case GCS_VERBA:
496 switch(idCommand)
498 case FCIDM_SHVIEW_OPEN:
499 strcpy(lpszName, "open");
500 hr = S_OK;
501 break;
502 case FCIDM_SHVIEW_EXPLORE:
503 strcpy(lpszName, "explore");
504 hr = S_OK;
505 break;
506 case FCIDM_SHVIEW_CUT:
507 strcpy(lpszName, "cut");
508 hr = S_OK;
509 break;
510 case FCIDM_SHVIEW_COPY:
511 strcpy(lpszName, "copy");
512 hr = S_OK;
513 break;
514 case FCIDM_SHVIEW_CREATELINK:
515 strcpy(lpszName, "link");
516 hr = S_OK;
517 break;
518 case FCIDM_SHVIEW_DELETE:
519 strcpy(lpszName, "delete");
520 hr = S_OK;
521 break;
522 case FCIDM_SHVIEW_PROPERTIES:
523 strcpy(lpszName, "properties");
524 hr = S_OK;
525 break;
526 case FCIDM_SHVIEW_RENAME:
527 strcpy(lpszName, "rename");
528 hr = S_OK;
529 break;
531 break;
533 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
534 case, you need to do the lstrcpyW to the pointer passed.*/
535 case GCS_VERBW:
536 switch(idCommand)
538 case FCIDM_SHVIEW_OPEN:
539 MultiByteToWideChar(CP_ACP, 0, "open", -1, (LPWSTR)lpszName, uMaxNameLen);
540 hr = S_OK;
541 break;
542 case FCIDM_SHVIEW_EXPLORE:
543 MultiByteToWideChar(CP_ACP, 0, "explore", -1, (LPWSTR)lpszName, uMaxNameLen);
544 hr = S_OK;
545 break;
546 case FCIDM_SHVIEW_CUT:
547 MultiByteToWideChar(CP_ACP, 0, "cut", -1, (LPWSTR)lpszName, uMaxNameLen);
548 hr = S_OK;
549 break;
550 case FCIDM_SHVIEW_COPY:
551 MultiByteToWideChar(CP_ACP, 0, "copy", -1, (LPWSTR)lpszName, uMaxNameLen);
552 hr = S_OK;
553 break;
554 case FCIDM_SHVIEW_CREATELINK:
555 MultiByteToWideChar(CP_ACP, 0, "link", -1, (LPWSTR)lpszName, uMaxNameLen);
556 hr = S_OK;
557 break;
558 case FCIDM_SHVIEW_DELETE:
559 MultiByteToWideChar(CP_ACP, 0, "delete", -1, (LPWSTR)lpszName, uMaxNameLen);
560 hr = S_OK;
561 break;
562 case FCIDM_SHVIEW_PROPERTIES:
563 MultiByteToWideChar(CP_ACP, 0, "properties", -1, (LPWSTR)lpszName, uMaxNameLen);
564 hr = S_OK;
565 break;
566 case FCIDM_SHVIEW_RENAME:
567 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
568 hr = S_OK;
569 break;
571 break;
573 case GCS_VALIDATEA:
574 case GCS_VALIDATEW:
575 hr = S_OK;
576 break;
578 TRACE("-- (%p)->(name=%s)\n", This, lpszName);
579 return hr;
582 /**************************************************************************
583 * NOTES
584 * should be only in IContextMenu2 and IContextMenu3
585 * is nevertheless called from word95
587 static HRESULT WINAPI ContextMenu_HandleMenuMsg(IContextMenu3 *iface, UINT msg,
588 WPARAM wParam, LPARAM lParam)
590 ContextMenu *This = impl_from_IContextMenu3(iface);
591 FIXME("(%p)->(0x%x 0x%lx 0x%lx): stub\n", This, msg, wParam, lParam);
592 return E_NOTIMPL;
595 static HRESULT WINAPI ContextMenu_HandleMenuMsg2(IContextMenu3 *iface, UINT msg,
596 WPARAM wParam, LPARAM lParam, LRESULT *result)
598 ContextMenu *This = impl_from_IContextMenu3(iface);
599 FIXME("(%p)->(0x%x 0x%lx 0x%lx %p): stub\n", This, msg, wParam, lParam, result);
600 return E_NOTIMPL;
603 static const IContextMenu3Vtbl ItemContextMenuVtbl =
605 ContextMenu_QueryInterface,
606 ContextMenu_AddRef,
607 ContextMenu_Release,
608 ItemMenu_QueryContextMenu,
609 ItemMenu_InvokeCommand,
610 ItemMenu_GetCommandString,
611 ContextMenu_HandleMenuMsg,
612 ContextMenu_HandleMenuMsg2
615 HRESULT ItemMenu_Constructor(IShellFolder *parent, LPCITEMIDLIST pidl, const LPCITEMIDLIST *apidl, UINT cidl,
616 REFIID riid, void **pObj)
618 ContextMenu* This;
619 HRESULT hr;
620 UINT i;
622 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
623 if (!This) return E_OUTOFMEMORY;
625 This->IContextMenu3_iface.lpVtbl = &ItemContextMenuVtbl;
626 This->ref = 1;
627 This->parent = parent;
628 if (parent) IShellFolder_AddRef(parent);
630 This->pidl = ILClone(pidl);
631 This->apidl = _ILCopyaPidl(apidl, cidl);
632 This->cidl = cidl;
633 This->allvalues = TRUE;
635 This->desktop = FALSE;
637 for (i = 0; i < cidl; i++)
638 This->allvalues &= (_ILIsValue(apidl[i]) ? 1 : 0);
640 hr = IContextMenu3_QueryInterface(&This->IContextMenu3_iface, riid, pObj);
641 IContextMenu3_Release(&This->IContextMenu3_iface);
643 return hr;
646 /* Background menu implementation */
647 static HRESULT WINAPI BackgroundMenu_QueryContextMenu(
648 IContextMenu3 *iface,
649 HMENU hMenu,
650 UINT indexMenu,
651 UINT idCmdFirst,
652 UINT idCmdLast,
653 UINT uFlags)
655 ContextMenu *This = impl_from_IContextMenu3(iface);
656 HMENU hMyMenu;
657 UINT idMax;
658 HRESULT hr;
660 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",
661 This, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
663 hMyMenu = LoadMenuA(shell32_hInstance, "MENU_002");
664 if (uFlags & CMF_DEFAULTONLY)
666 HMENU ourMenu = GetSubMenu(hMyMenu,0);
667 UINT oldDef = GetMenuDefaultItem(hMenu,TRUE,GMDI_USEDISABLED);
668 UINT newDef = GetMenuDefaultItem(ourMenu,TRUE,GMDI_USEDISABLED);
669 if (newDef != oldDef)
670 SetMenuDefaultItem(hMenu,newDef,TRUE);
671 if (newDef!=0xFFFFFFFF)
672 hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, newDef+1);
673 else
674 hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
676 else
678 idMax = Shell_MergeMenus (hMenu, GetSubMenu(hMyMenu,0), indexMenu,
679 idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
680 hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, idMax-idCmdFirst);
682 DestroyMenu(hMyMenu);
684 TRACE("(%p)->returning 0x%x\n",This,hr);
685 return hr;
688 static void DoNewFolder(ContextMenu *This, IShellView *view)
690 ISFHelper *helper;
692 IShellFolder_QueryInterface(This->parent, &IID_ISFHelper, (void**)&helper);
693 if (helper)
695 WCHAR nameW[MAX_PATH];
696 LPITEMIDLIST pidl;
698 ISFHelper_GetUniqueName(helper, nameW, MAX_PATH);
699 ISFHelper_AddFolder(helper, 0, nameW, &pidl);
701 if (view)
703 /* if we are in a shellview do labeledit */
704 IShellView_SelectItem(view,
705 pidl,(SVSI_DESELECTOTHERS | SVSI_EDIT | SVSI_ENSUREVISIBLE
706 |SVSI_FOCUSED|SVSI_SELECT));
709 SHFree(pidl);
710 ISFHelper_Release(helper);
714 static BOOL DoPaste(ContextMenu *This)
716 BOOL bSuccess = FALSE;
717 IDataObject * pda;
719 TRACE("\n");
721 if(SUCCEEDED(OleGetClipboard(&pda)))
723 STGMEDIUM medium;
724 FORMATETC formatetc;
726 TRACE("pda=%p\n", pda);
728 /* Set the FORMATETC structure*/
729 InitFormatEtc(formatetc, RegisterClipboardFormatW(CFSTR_SHELLIDLISTW), TYMED_HGLOBAL);
731 /* Get the pidls from IDataObject */
732 if(SUCCEEDED(IDataObject_GetData(pda,&formatetc,&medium)))
734 LPITEMIDLIST * apidl;
735 LPITEMIDLIST pidl;
736 IShellFolder *psfFrom = NULL, *psfDesktop;
738 LPIDA lpcida = GlobalLock(medium.u.hGlobal);
739 TRACE("cida=%p\n", lpcida);
741 apidl = _ILCopyCidaToaPidl(&pidl, lpcida);
743 /* bind to the source shellfolder */
744 SHGetDesktopFolder(&psfDesktop);
745 if(psfDesktop)
747 IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (LPVOID*)&psfFrom);
748 IShellFolder_Release(psfDesktop);
751 if (psfFrom)
753 /* get source and destination shellfolder */
754 ISFHelper *psfhlpdst, *psfhlpsrc;
755 IShellFolder_QueryInterface(This->parent, &IID_ISFHelper, (void**)&psfhlpdst);
756 IShellFolder_QueryInterface(psfFrom, &IID_ISFHelper, (void**)&psfhlpsrc);
758 /* do the copy/move */
759 if (psfhlpdst && psfhlpsrc)
761 ISFHelper_CopyItems(psfhlpdst, psfFrom, lpcida->cidl, (LPCITEMIDLIST*)apidl);
762 /* FIXME handle move
763 ISFHelper_DeleteItems(psfhlpsrc, lpcida->cidl, apidl);
766 if(psfhlpdst) ISFHelper_Release(psfhlpdst);
767 if(psfhlpsrc) ISFHelper_Release(psfhlpsrc);
768 IShellFolder_Release(psfFrom);
771 _ILFreeaPidl(apidl, lpcida->cidl);
772 SHFree(pidl);
774 /* release the medium*/
775 ReleaseStgMedium(&medium);
777 IDataObject_Release(pda);
779 #if 0
780 HGLOBAL hMem;
782 OpenClipboard(NULL);
783 hMem = GetClipboardData(CF_HDROP);
785 if(hMem)
787 char * pDropFiles = GlobalLock(hMem);
788 if(pDropFiles)
790 int len, offset = sizeof(DROPFILESTRUCT);
792 while( pDropFiles[offset] != 0)
794 len = strlen(pDropFiles + offset);
795 TRACE("%s\n", pDropFiles + offset);
796 offset += len+1;
799 GlobalUnlock(hMem);
801 CloseClipboard();
802 #endif
803 return bSuccess;
806 static HRESULT WINAPI BackgroundMenu_InvokeCommand(
807 IContextMenu3 *iface,
808 LPCMINVOKECOMMANDINFO lpcmi)
810 ContextMenu *This = impl_from_IContextMenu3(iface);
811 IShellBrowser *browser;
812 IShellView *view = NULL;
813 HWND hWnd = NULL;
815 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n", This, lpcmi, lpcmi->lpVerb, lpcmi->hwnd);
817 /* get the active IShellView */
818 if ((browser = (IShellBrowser*)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER, 0, 0)))
820 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(browser, &view)))
821 IShellView_GetWindow(view, &hWnd);
824 if(HIWORD(lpcmi->lpVerb))
826 TRACE("%s\n", debugstr_a(lpcmi->lpVerb));
828 if (!strcmp(lpcmi->lpVerb, CMDSTR_NEWFOLDERA))
830 DoNewFolder(This, view);
832 else if (!strcmp(lpcmi->lpVerb, CMDSTR_VIEWLISTA))
834 if (hWnd) SendMessageA(hWnd, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_LISTVIEW, 0), 0);
836 else if (!strcmp(lpcmi->lpVerb, CMDSTR_VIEWDETAILSA))
838 if (hWnd) SendMessageA(hWnd, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_REPORTVIEW, 0), 0);
840 else
842 FIXME("please report: unknown verb %s\n", debugstr_a(lpcmi->lpVerb));
845 else
847 switch (LOWORD(lpcmi->lpVerb))
849 case FCIDM_SHVIEW_REFRESH:
850 if (view) IShellView_Refresh(view);
851 break;
853 case FCIDM_SHVIEW_NEWFOLDER:
854 DoNewFolder(This, view);
855 break;
857 case FCIDM_SHVIEW_INSERT:
858 DoPaste(This);
859 break;
861 case FCIDM_SHVIEW_PROPERTIES:
862 if (This->desktop) {
863 ShellExecuteA(lpcmi->hwnd, "open", "rundll32.exe shell32.dll,Control_RunDLL desk.cpl", NULL, NULL, SW_SHOWNORMAL);
864 } else {
865 FIXME("launch item properties dialog\n");
867 break;
869 default:
870 /* if it's an id just pass it to the parent shv */
871 if (hWnd) SendMessageA(hWnd, WM_COMMAND, MAKEWPARAM(LOWORD(lpcmi->lpVerb), 0), 0);
872 break;
876 if (view)
877 IShellView_Release(view);
879 return S_OK;
882 static HRESULT WINAPI BackgroundMenu_GetCommandString(
883 IContextMenu3 *iface,
884 UINT_PTR idCommand,
885 UINT uFlags,
886 UINT* lpReserved,
887 LPSTR lpszName,
888 UINT uMaxNameLen)
890 ContextMenu *This = impl_from_IContextMenu3(iface);
892 TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
894 /* test the existence of the menu items, the file dialog enables
895 the buttons according to this */
896 if (uFlags == GCS_VALIDATEA)
898 if(HIWORD(idCommand))
900 if (!strcmp((LPSTR)idCommand, CMDSTR_VIEWLISTA) ||
901 !strcmp((LPSTR)idCommand, CMDSTR_VIEWDETAILSA) ||
902 !strcmp((LPSTR)idCommand, CMDSTR_NEWFOLDERA))
904 return S_OK;
909 FIXME("unknown command string\n");
910 return E_FAIL;
913 static const IContextMenu3Vtbl BackgroundContextMenuVtbl =
915 ContextMenu_QueryInterface,
916 ContextMenu_AddRef,
917 ContextMenu_Release,
918 BackgroundMenu_QueryContextMenu,
919 BackgroundMenu_InvokeCommand,
920 BackgroundMenu_GetCommandString,
921 ContextMenu_HandleMenuMsg,
922 ContextMenu_HandleMenuMsg2
925 HRESULT BackgroundMenu_Constructor(IShellFolder *parent, BOOL desktop, REFIID riid, void **pObj)
927 ContextMenu *This;
928 HRESULT hr;
930 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
931 if (!This) return E_OUTOFMEMORY;
933 This->IContextMenu3_iface.lpVtbl = &BackgroundContextMenuVtbl;
934 This->ref = 1;
935 This->parent = parent;
937 This->pidl = NULL;
938 This->apidl = NULL;
939 This->cidl = 0;
940 This->allvalues = FALSE;
942 This->desktop = desktop;
943 if (parent) IShellFolder_AddRef(parent);
945 hr = IContextMenu3_QueryInterface(&This->IContextMenu3_iface, riid, pObj);
946 IContextMenu3_Release(&This->IContextMenu3_iface);
948 return hr;