New debug scheme with explicit debug channels declaration.
[wine.git] / dlls / shell32 / contmenu.c
blob58c352c2eed04b5e8a5f9060d1103ea3f0ea1a0a
1 /*
2 * IContextMenu
4 * Copyright 1998 Juergen Schmied <juergen.schmied@metronet.de>
5 */
6 #include <string.h>
8 #include "winerror.h"
9 #include "debug.h"
11 #include "pidl.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"
17 #include "shell32_main.h"
19 DEFAULT_DEBUG_CHANNEL(shell)
21 /**************************************************************************
22 * IContextMenu Implementation
24 typedef struct
25 { ICOM_VTABLE(IContextMenu)* lpvtbl;
26 DWORD ref;
27 IShellFolder* pSFParent;
28 LPITEMIDLIST *aPidls;
29 BOOL bAllValues;
30 } IContextMenuImpl;
33 static struct ICOM_VTABLE(IContextMenu) cmvt;
35 /**************************************************************************
36 * IContextMenu_AllocPidlTable()
38 BOOL IContextMenu_AllocPidlTable(IContextMenuImpl *This, DWORD dwEntries)
40 TRACE(shell,"(%p)->(entrys=%lu)\n",This, dwEntries);
42 /*add one for NULL terminator */
43 dwEntries++;
45 This->aPidls = (LPITEMIDLIST*)SHAlloc(dwEntries * sizeof(LPITEMIDLIST));
47 if(This->aPidls)
48 { ZeroMemory(This->aPidls, dwEntries * sizeof(LPITEMIDLIST)); /*set all of the entries to NULL*/
50 return (This->aPidls != NULL);
53 /**************************************************************************
54 * IContextMenu_FreePidlTable()
56 void IContextMenu_FreePidlTable(IContextMenuImpl *This)
58 int i;
60 TRACE(shell,"(%p)->()\n",This);
62 if(This->aPidls)
63 { for(i = 0; This->aPidls[i]; i++)
64 { SHFree(This->aPidls[i]);
67 SHFree(This->aPidls);
68 This->aPidls = NULL;
72 /**************************************************************************
73 * IContextMenu_FillPidlTable()
75 BOOL IContextMenu_FillPidlTable(IContextMenuImpl *This, LPCITEMIDLIST *aPidls, UINT uItemCount)
77 UINT i;
79 TRACE(shell,"(%p)->(apidl=%p count=%u)\n",This, aPidls, uItemCount);
81 if(This->aPidls)
82 { for(i = 0; i < uItemCount; i++)
83 { This->aPidls[i] = ILClone(aPidls[i]);
85 return TRUE;
87 return FALSE;
90 /**************************************************************************
91 * IContextMenu_CanRenameItems()
93 BOOL IContextMenu_CanRenameItems(IContextMenuImpl *This)
94 { UINT i;
95 DWORD dwAttributes;
97 TRACE(shell,"(%p)->()\n",This);
99 if(This->aPidls)
101 for(i = 0; This->aPidls[i]; i++){} /*get the number of items assigned to This object*/
102 { if(i > 1) /*you can't rename more than one item at a time*/
103 { return FALSE;
106 dwAttributes = SFGAO_CANRENAME;
107 IShellFolder_GetAttributesOf(This->pSFParent, i, (LPCITEMIDLIST*)This->aPidls, &dwAttributes);
109 return dwAttributes & SFGAO_CANRENAME;
111 return FALSE;
114 /**************************************************************************
115 * IContextMenu_Constructor()
117 IContextMenu *IContextMenu_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST *aPidls, UINT uItemCount)
118 { IContextMenuImpl* cm;
119 UINT u;
121 cm = (IContextMenuImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IContextMenuImpl));
122 cm->lpvtbl=&cmvt;
123 cm->ref = 1;
125 cm->pSFParent = pSFParent;
126 if(pSFParent)
127 IShellFolder_AddRef(pSFParent);
129 cm->aPidls = NULL;
131 IContextMenu_AllocPidlTable(cm, uItemCount);
133 if(cm->aPidls)
134 { IContextMenu_FillPidlTable(cm, aPidls, uItemCount);
137 cm->bAllValues = 1;
138 for(u = 0; u < uItemCount; u++)
139 { cm->bAllValues &= (_ILIsValue(aPidls[u]) ? 1 : 0);
141 TRACE(shell,"(%p)->()\n",cm);
142 shell32_ObjCount++;
143 return (IContextMenu*)cm;
146 /**************************************************************************
147 * IContextMenu_fnQueryInterface
149 static HRESULT WINAPI IContextMenu_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj)
151 ICOM_THIS(IContextMenuImpl, iface);
153 char xriid[50];
154 WINE_StringFromCLSID((LPCLSID)riid,xriid);
156 TRACE(shell,"(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
158 *ppvObj = NULL;
160 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
161 { *ppvObj = This;
163 else if(IsEqualIID(riid, &IID_IContextMenu)) /*IContextMenu*/
164 { *ppvObj = This;
166 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
167 { FIXME (shell,"-- LPSHELLEXTINIT pointer requested\n");
170 if(*ppvObj)
172 IContextMenu_AddRef((IContextMenu*)*ppvObj);
173 TRACE(shell,"-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
174 return S_OK;
176 TRACE(shell,"-- Interface: E_NOINTERFACE\n");
177 return E_NOINTERFACE;
180 /**************************************************************************
181 * IContextMenu_fnAddRef
183 static ULONG WINAPI IContextMenu_fnAddRef(IContextMenu *iface)
185 ICOM_THIS(IContextMenuImpl, iface);
187 TRACE(shell,"(%p)->(count=%lu)\n",This, This->ref);
189 shell32_ObjCount++;
190 return ++(This->ref);
193 /**************************************************************************
194 * IContextMenu_fnRelease
196 static ULONG WINAPI IContextMenu_fnRelease(IContextMenu *iface)
198 ICOM_THIS(IContextMenuImpl, iface);
200 TRACE(shell,"(%p)->()\n",This);
202 shell32_ObjCount--;
204 if (!--(This->ref))
205 { TRACE(shell," destroying IContextMenu(%p)\n",This);
207 if(This->pSFParent)
208 IShellFolder_Release(This->pSFParent);
210 /*make sure the pidl is freed*/
211 if(This->aPidls)
212 { IContextMenu_FreePidlTable(This);
215 HeapFree(GetProcessHeap(),0,This);
216 return 0;
218 return This->ref;
221 /**************************************************************************
222 * ICM_InsertItem()
224 void WINAPI _InsertMenuItem (
225 HMENU hmenu,
226 UINT indexMenu,
227 BOOL fByPosition,
228 UINT wID,
229 UINT fType,
230 LPSTR dwTypeData,
231 UINT fState)
233 MENUITEMINFOA mii;
235 ZeroMemory(&mii, sizeof(mii));
236 mii.cbSize = sizeof(mii);
237 if (fType == MFT_SEPARATOR)
238 { mii.fMask = MIIM_ID | MIIM_TYPE;
240 else
241 { mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
242 mii.dwTypeData = dwTypeData;
243 mii.fState = MFS_ENABLED | MFS_DEFAULT;
245 mii.wID = wID;
246 mii.fType = fType;
247 InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
249 /**************************************************************************
250 * IContextMenu_fnQueryContextMenu()
253 static HRESULT WINAPI IContextMenu_fnQueryContextMenu(
254 IContextMenu *iface,
255 HMENU hmenu,
256 UINT indexMenu,
257 UINT idCmdFirst,
258 UINT idCmdLast,
259 UINT uFlags)
261 ICOM_THIS(IContextMenuImpl, iface);
263 BOOL fExplore ;
265 TRACE(shell,"(%p)->(hmenu=%x indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
267 if(!(CMF_DEFAULTONLY & uFlags))
268 { if(!This->bAllValues)
269 { /* folder menu */
270 fExplore = uFlags & CMF_EXPLORE;
271 if(fExplore)
272 { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
273 _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
275 else
276 { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED|MFS_DEFAULT);
277 _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
280 if(uFlags & CMF_CANRENAME)
281 { _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
282 _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_RENAME, MFT_STRING, "&Rename", (IContextMenu_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED));
285 else /* file menu */
286 { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED|MFS_DEFAULT);
287 if(uFlags & CMF_CANRENAME)
288 { _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
289 _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_RENAME, MFT_STRING, "&Rename", (IContextMenu_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED));
292 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (IDM_LAST + 1));
294 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
297 /**************************************************************************
298 * IContextMenu_fnInvokeCommand()
300 static HRESULT WINAPI IContextMenu_fnInvokeCommand(
301 IContextMenu *iface,
302 LPCMINVOKECOMMANDINFO lpcmi)
304 ICOM_THIS(IContextMenuImpl, iface);
306 LPITEMIDLIST pidlTemp,pidlFQ;
307 LPSHELLBROWSER lpSB;
308 LPSHELLVIEW lpSV;
309 HWND hWndSV;
310 SHELLEXECUTEINFOA sei;
311 int i;
313 TRACE(shell,"(%p)->(invcom=%p verb=%p wnd=%x)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
315 if(HIWORD(lpcmi->lpVerb))
316 { /* get the active IShellView */
317 lpSB = (LPSHELLBROWSER)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER,0,0);
318 IShellBrowser_QueryActiveShellView(lpSB, &lpSV); /* does AddRef() on lpSV */
319 IShellView_GetWindow(lpSV, &hWndSV);
321 /* these verbs are used by the filedialogs*/
322 TRACE(shell,"%s\n",lpcmi->lpVerb);
323 if (! strcmp(lpcmi->lpVerb,CMDSTR_NEWFOLDER))
324 { FIXME(shell,"%s not implemented\n",lpcmi->lpVerb);
326 else if (! strcmp(lpcmi->lpVerb,CMDSTR_VIEWLIST))
327 { SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_LISTVIEW,0),0 );
329 else if (! strcmp(lpcmi->lpVerb,CMDSTR_VIEWDETAILS))
330 { SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_REPORTVIEW,0),0 );
332 else
333 { FIXME(shell,"please report: unknown verb %s\n",lpcmi->lpVerb);
335 IShellView_Release(lpSV);
336 return NOERROR;
339 if(LOWORD(lpcmi->lpVerb) > IDM_LAST)
340 return E_INVALIDARG;
342 switch(LOWORD(lpcmi->lpVerb))
343 { case IDM_EXPLORE:
344 case IDM_OPEN:
345 /* Find the first item in the list that is not a value. These commands
346 should never be invoked if there isn't at least one folder item in the list.*/
348 for(i = 0; This->aPidls[i]; i++)
349 { if(!_ILIsValue(This->aPidls[i]))
350 break;
353 pidlTemp = ILCombine(((IGenericSFImpl*)(This->pSFParent))->mpidl, This->aPidls[i]);
354 pidlFQ = ILCombine(((IGenericSFImpl*)(This->pSFParent))->pMyPidl, pidlTemp);
355 SHFree(pidlTemp);
357 ZeroMemory(&sei, sizeof(sei));
358 sei.cbSize = sizeof(sei);
359 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
360 sei.lpIDList = pidlFQ;
361 sei.lpClass = "folder";
362 sei.hwnd = lpcmi->hwnd;
363 sei.nShow = SW_SHOWNORMAL;
365 if(LOWORD(lpcmi->lpVerb) == IDM_EXPLORE)
366 { sei.lpVerb = "explore";
368 else
369 { sei.lpVerb = "open";
371 ShellExecuteExA(&sei);
372 SHFree(pidlFQ);
373 break;
375 case IDM_RENAME:
376 MessageBeep(MB_OK);
377 /*handle rename for the view here*/
378 break;
380 return NOERROR;
383 /**************************************************************************
384 * IContextMenu_fnGetCommandString()
386 static HRESULT WINAPI IContextMenu_fnGetCommandString(
387 IContextMenu *iface,
388 UINT idCommand,
389 UINT uFlags,
390 LPUINT lpReserved,
391 LPSTR lpszName,
392 UINT uMaxNameLen)
394 ICOM_THIS(IContextMenuImpl, iface);
396 HRESULT hr = E_INVALIDARG;
398 TRACE(shell,"(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
400 switch(uFlags)
401 { case GCS_HELPTEXT:
402 hr = E_NOTIMPL;
403 break;
405 case GCS_VERBA:
406 switch(idCommand)
407 { case IDM_RENAME:
408 strcpy((LPSTR)lpszName, "rename");
409 hr = NOERROR;
410 break;
412 break;
414 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
415 case, you need to do the lstrcpyW to the pointer passed.*/
416 case GCS_VERBW:
417 switch(idCommand)
418 { case IDM_RENAME:
419 lstrcpyAtoW((LPWSTR)lpszName, "rename");
420 hr = NOERROR;
421 break;
423 break;
425 case GCS_VALIDATE:
426 hr = NOERROR;
427 break;
429 TRACE(shell,"-- (%p)->(name=%s)\n",This, lpszName);
430 return hr;
433 /**************************************************************************
434 * IContextMenu_fnHandleMenuMsg()
435 * NOTES
436 * should be only in IContextMenu2 and IContextMenu3
437 * is nevertheless called from word95
439 static HRESULT WINAPI IContextMenu_fnHandleMenuMsg(
440 IContextMenu *iface,
441 UINT uMsg,
442 WPARAM wParam,
443 LPARAM lParam)
445 ICOM_THIS(IContextMenuImpl, iface);
447 TRACE(shell,"(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
449 return E_NOTIMPL;
452 /**************************************************************************
453 * IContextMenu VTable
456 static struct ICOM_VTABLE(IContextMenu) cmvt =
458 IContextMenu_fnQueryInterface,
459 IContextMenu_fnAddRef,
460 IContextMenu_fnRelease,
461 IContextMenu_fnQueryContextMenu,
462 IContextMenu_fnInvokeCommand,
463 IContextMenu_fnGetCommandString,
464 IContextMenu_fnHandleMenuMsg,
465 (void *) 0xdeadbabe /* just paranoia */