4 * Copyright 1998 Juergen Schmied <juergen.schmied@metronet.de>
9 #include "debugtools.h"
12 #include "wine/obj_base.h"
13 #include "wine/obj_contextmenu.h"
14 #include "wine/obj_shellbrowser.h"
15 #include "wine/obj_shellextinit.h"
16 #include "wine/undocshell.h"
18 #include "shell32_main.h"
20 DEFAULT_DEBUG_CHANNEL(shell)
22 /**************************************************************************
27 static HRESULT WINAPI IContextMenu_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj);
28 static ULONG WINAPI IContextMenu_fnAddRef(IContextMenu *iface);
29 static ULONG WINAPI IContextMenu_fnRelease(IContextMenu *iface);
30 static HRESULT WINAPI IContextMenu_fnQueryContextMenu(IContextMenu *iface, HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags);
31 static HRESULT WINAPI IContextMenu_fnInvokeCommand(IContextMenu *iface, LPCMINVOKECOMMANDINFO lpcmi);
32 static HRESULT WINAPI IContextMenu_fnGetCommandString(IContextMenu *iface, UINT idCommand, UINT uFlags, LPUINT lpReserved, LPSTR lpszName, UINT uMaxNameLen);
33 static HRESULT WINAPI IContextMenu_fnHandleMenuMsg(IContextMenu *iface, UINT uMsg, WPARAM wParam, LPARAM lParam);
35 static struct ICOM_VTABLE(IContextMenu) cmvt =
37 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
38 IContextMenu_fnQueryInterface,
39 IContextMenu_fnAddRef,
40 IContextMenu_fnRelease,
41 IContextMenu_fnQueryContextMenu,
42 IContextMenu_fnInvokeCommand,
43 IContextMenu_fnGetCommandString,
44 IContextMenu_fnHandleMenuMsg,
45 (void *) 0xdeadbabe /* just paranoia */
48 /**************************************************************************
49 * IContextMenu Implementation
52 { ICOM_VFIELD(IContextMenu);
54 IShellFolder* pSFParent;
55 LPITEMIDLIST pidl; /* root pidl */
56 LPITEMIDLIST *aPidls; /* array of child pidls */
61 static struct ICOM_VTABLE(IContextMenu) cmvt;
63 /**************************************************************************
64 * IContextMenu_AllocPidlTable()
66 static BOOL IContextMenu_AllocPidlTable(IContextMenuImpl *This, DWORD dwEntries)
68 TRACE("(%p)->(entrys=%lu)\n",This, dwEntries);
70 /*add one for NULL terminator */
73 This->aPidls = (LPITEMIDLIST*)SHAlloc(dwEntries * sizeof(LPITEMIDLIST));
76 { ZeroMemory(This->aPidls, dwEntries * sizeof(LPITEMIDLIST)); /*set all of the entries to NULL*/
78 return (This->aPidls != NULL);
81 /**************************************************************************
82 * IContextMenu_FreePidlTable()
84 static void IContextMenu_FreePidlTable(IContextMenuImpl *This)
88 TRACE("(%p)->()\n",This);
91 { for(i = 0; This->aPidls[i]; i++)
92 { SHFree(This->aPidls[i]);
100 /**************************************************************************
101 * IContextMenu_FillPidlTable()
103 static BOOL IContextMenu_FillPidlTable(IContextMenuImpl *This, LPCITEMIDLIST *aPidls, UINT uItemCount)
107 TRACE("(%p)->(apidl=%p count=%u)\n",This, aPidls, uItemCount);
110 { for(i = 0; i < uItemCount; i++)
111 { This->aPidls[i] = ILClone(aPidls[i]);
118 /**************************************************************************
119 * IContextMenu_CanRenameItems()
121 static BOOL IContextMenu_CanRenameItems(IContextMenuImpl *This)
125 TRACE("(%p)->()\n",This);
129 for(i = 0; This->aPidls[i]; i++){} /*get the number of items assigned to This object*/
130 { if(i > 1) /*you can't rename more than one item at a time*/
134 dwAttributes = SFGAO_CANRENAME;
135 IShellFolder_GetAttributesOf(This->pSFParent, i, (LPCITEMIDLIST*)This->aPidls, &dwAttributes);
137 return dwAttributes & SFGAO_CANRENAME;
142 /**************************************************************************
143 * IContextMenu_Constructor()
145 IContextMenu *IContextMenu_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *aPidls, UINT uItemCount)
146 { IContextMenuImpl* cm;
149 cm = (IContextMenuImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IContextMenuImpl));
152 cm->pidl = ILClone(pidl);
154 cm->pSFParent = pSFParent;
157 IShellFolder_AddRef(pSFParent);
161 IContextMenu_AllocPidlTable(cm, uItemCount);
164 { IContextMenu_FillPidlTable(cm, aPidls, uItemCount);
168 for(u = 0; u < uItemCount; u++)
169 { cm->bAllValues &= (_ILIsValue(aPidls[u]) ? 1 : 0);
171 TRACE("(%p)->()\n",cm);
173 return (IContextMenu*)cm;
176 /**************************************************************************
177 * IContextMenu_fnQueryInterface
179 static HRESULT WINAPI IContextMenu_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj)
181 ICOM_THIS(IContextMenuImpl, iface);
183 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
187 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
190 else if(IsEqualIID(riid, &IID_IContextMenu)) /*IContextMenu*/
193 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
194 { FIXME("-- LPSHELLEXTINIT pointer requested\n");
199 IContextMenu_AddRef((IContextMenu*)*ppvObj);
200 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
203 TRACE("-- Interface: E_NOINTERFACE\n");
204 return E_NOINTERFACE;
207 /**************************************************************************
208 * IContextMenu_fnAddRef
210 static ULONG WINAPI IContextMenu_fnAddRef(IContextMenu *iface)
212 ICOM_THIS(IContextMenuImpl, iface);
214 TRACE("(%p)->(count=%lu)\n",This, This->ref);
217 return ++(This->ref);
220 /**************************************************************************
221 * IContextMenu_fnRelease
223 static ULONG WINAPI IContextMenu_fnRelease(IContextMenu *iface)
225 ICOM_THIS(IContextMenuImpl, iface);
227 TRACE("(%p)->()\n",This);
232 { TRACE(" destroying IContextMenu(%p)\n",This);
235 IShellFolder_Release(This->pSFParent);
240 /*make sure the pidl is freed*/
242 { IContextMenu_FreePidlTable(This);
245 HeapFree(GetProcessHeap(),0,This);
251 /**************************************************************************
254 void WINAPI _InsertMenuItem (
265 ZeroMemory(&mii, sizeof(mii));
266 mii.cbSize = sizeof(mii);
267 if (fType == MFT_SEPARATOR)
268 { mii.fMask = MIIM_ID | MIIM_TYPE;
271 { mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
272 mii.dwTypeData = dwTypeData;
277 InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
279 /**************************************************************************
280 * IContextMenu_fnQueryContextMenu()
283 static HRESULT WINAPI IContextMenu_fnQueryContextMenu(
291 ICOM_THIS(IContextMenuImpl, iface);
295 TRACE("(%p)->(hmenu=%x indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
297 if(!(CMF_DEFAULTONLY & uFlags))
298 { if(!This->bAllValues)
300 fExplore = uFlags & CMF_EXPLORE;
302 { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
303 _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
306 { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED|MFS_DEFAULT);
307 _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
310 if(uFlags & CMF_CANRENAME)
311 { _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
312 _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_RENAME, MFT_STRING, "&Rename", (IContextMenu_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED));
316 { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED|MFS_DEFAULT);
317 if(uFlags & CMF_CANRENAME)
318 { _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
319 _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_RENAME, MFT_STRING, "&Rename", (IContextMenu_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED));
322 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (IDM_LAST + 1));
324 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
327 /**************************************************************************
328 * IContextMenu_fnInvokeCommand()
330 static HRESULT WINAPI IContextMenu_fnInvokeCommand(
332 LPCMINVOKECOMMANDINFO lpcmi)
334 ICOM_THIS(IContextMenuImpl, iface);
337 SHELLEXECUTEINFOA sei;
340 TRACE("(%p)->(invcom=%p verb=%p wnd=%x)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
342 if(LOWORD(lpcmi->lpVerb) > IDM_LAST)
345 switch(LOWORD(lpcmi->lpVerb))
348 /* Find the first item in the list that is not a value. These commands
349 should never be invoked if there isn't at least one folder item in the list.*/
351 for(i = 0; This->aPidls[i]; i++)
352 { if(!_ILIsValue(This->aPidls[i]))
356 pidlFQ = ILCombine(This->pidl, This->aPidls[i]);
358 ZeroMemory(&sei, sizeof(sei));
359 sei.cbSize = sizeof(sei);
360 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
361 sei.lpIDList = pidlFQ;
362 sei.lpClass = "folder";
363 sei.hwnd = lpcmi->hwnd;
364 sei.nShow = SW_SHOWNORMAL;
366 if(LOWORD(lpcmi->lpVerb) == IDM_EXPLORE)
367 { sei.lpVerb = "explore";
370 { sei.lpVerb = "open";
372 ShellExecuteExA(&sei);
378 /*handle rename for the view here*/
384 /**************************************************************************
385 * IContextMenu_fnGetCommandString()
387 static HRESULT WINAPI IContextMenu_fnGetCommandString(
395 ICOM_THIS(IContextMenuImpl, iface);
397 HRESULT hr = E_INVALIDARG;
399 TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
409 strcpy((LPSTR)lpszName, "rename");
415 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
416 case, you need to do the lstrcpyW to the pointer passed.*/
420 lstrcpyAtoW((LPWSTR)lpszName, "rename");
430 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
434 /**************************************************************************
435 * IContextMenu_fnHandleMenuMsg()
437 * should be only in IContextMenu2 and IContextMenu3
438 * is nevertheless called from word95
440 static HRESULT WINAPI IContextMenu_fnHandleMenuMsg(
446 ICOM_THIS(IContextMenuImpl, iface);
448 TRACE("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);