shell32: Localize file context menu.
[wine.git] / dlls / shell32 / shv_item_cmenu.c
blob1cb60c5852f880247daa4cdbecfb61ae7daf6675
1 /*
2 * IContextMenu for items in the shellview
4 * Copyright 1998, 2000 Juergen Schmied <juergen.schmied@debitel.net>
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.
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.
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
21 #include <string.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
27 #include "winerror.h"
28 #include "wine/debug.h"
30 #include "windef.h"
31 #include "wingdi.h"
32 #include "pidl.h"
33 #include "undocshell.h"
34 #include "shlobj.h"
36 #include "shell32_main.h"
37 #include "shellfolder.h"
39 #include "shresdef.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(shell);
43 /**************************************************************************
44 * IContextMenu Implementation
46 typedef struct
47 { const IContextMenu2Vtbl *lpVtbl;
48 LONG ref;
49 IShellFolder* pSFParent;
50 LPITEMIDLIST pidl; /* root pidl */
51 LPITEMIDLIST *apidl; /* array of child pidls */
52 UINT cidl;
53 BOOL bAllValues;
54 } ItemCmImpl;
57 static const IContextMenu2Vtbl cmvt;
59 /**************************************************************************
60 * ISvItemCm_CanRenameItems()
62 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
63 { UINT i;
64 DWORD dwAttributes;
66 TRACE("(%p)->()\n",This);
68 if(This->apidl)
70 for(i = 0; i < This->cidl; i++){}
71 if(i > 1) return FALSE; /* can't rename more than one item at a time*/
72 dwAttributes = SFGAO_CANRENAME;
73 IShellFolder_GetAttributesOf(This->pSFParent, 1, (LPCITEMIDLIST*)This->apidl, &dwAttributes);
74 return dwAttributes & SFGAO_CANRENAME;
76 return FALSE;
79 /**************************************************************************
80 * ISvItemCm_Constructor()
82 IContextMenu2 *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, const LPCITEMIDLIST *apidl, UINT cidl)
83 { ItemCmImpl* cm;
84 UINT u;
86 cm = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
87 cm->lpVtbl = &cmvt;
88 cm->ref = 1;
89 cm->pidl = ILClone(pidl);
90 cm->pSFParent = pSFParent;
92 if(pSFParent) IShellFolder_AddRef(pSFParent);
94 cm->apidl = _ILCopyaPidl(apidl, cidl);
95 cm->cidl = cidl;
97 cm->bAllValues = 1;
98 for(u = 0; u < cidl; u++)
100 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
103 TRACE("(%p)->()\n",cm);
105 return (IContextMenu2*)cm;
108 /**************************************************************************
109 * ISvItemCm_fnQueryInterface
111 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
113 ItemCmImpl *This = (ItemCmImpl *)iface;
115 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
117 *ppvObj = NULL;
119 if(IsEqualIID(riid, &IID_IUnknown) ||
120 IsEqualIID(riid, &IID_IContextMenu) ||
121 IsEqualIID(riid, &IID_IContextMenu2))
123 *ppvObj = This;
125 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
127 FIXME("-- LPSHELLEXTINIT pointer requested\n");
130 if(*ppvObj)
132 IUnknown_AddRef((IUnknown*)*ppvObj);
133 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
134 return S_OK;
136 TRACE("-- Interface: E_NOINTERFACE\n");
137 return E_NOINTERFACE;
140 /**************************************************************************
141 * ISvItemCm_fnAddRef
143 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu2 *iface)
145 ItemCmImpl *This = (ItemCmImpl *)iface;
146 ULONG refCount = InterlockedIncrement(&This->ref);
148 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
150 return refCount;
153 /**************************************************************************
154 * ISvItemCm_fnRelease
156 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu2 *iface)
158 ItemCmImpl *This = (ItemCmImpl *)iface;
159 ULONG refCount = InterlockedDecrement(&This->ref);
161 TRACE("(%p)->(count=%i)\n", This, refCount + 1);
163 if (!refCount)
165 TRACE(" destroying IContextMenu(%p)\n",This);
167 if(This->pSFParent)
168 IShellFolder_Release(This->pSFParent);
170 SHFree(This->pidl);
172 /*make sure the pidl is freed*/
173 _ILFreeaPidl(This->apidl, This->cidl);
175 HeapFree(GetProcessHeap(),0,This);
177 return refCount;
180 /**************************************************************************
181 * ICM_InsertItem()
183 void WINAPI _InsertMenuItem (
184 HMENU hmenu,
185 UINT indexMenu,
186 BOOL fByPosition,
187 UINT wID,
188 UINT fType,
189 LPCSTR dwTypeData,
190 UINT fState)
192 MENUITEMINFOA mii;
194 ZeroMemory(&mii, sizeof(mii));
195 mii.cbSize = sizeof(mii);
196 if (fType == MFT_SEPARATOR)
198 mii.fMask = MIIM_ID | MIIM_TYPE;
200 else
202 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
203 mii.dwTypeData = (LPSTR) dwTypeData;
204 mii.fState = fState;
206 mii.wID = wID;
207 mii.fType = fType;
208 InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
211 static void WINAPI _InsertMenuItemW (
212 HMENU hmenu,
213 UINT indexMenu,
214 BOOL fByPosition,
215 UINT wID,
216 UINT fType,
217 LPWSTR dwTypeData,
218 UINT fState)
220 MENUITEMINFOW mii;
222 mii.cbSize = sizeof(mii);
223 if (fType == MFT_SEPARATOR)
225 mii.fMask = MIIM_ID | MIIM_TYPE;
227 else
229 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
230 mii.dwTypeData = dwTypeData;
231 mii.fState = fState;
233 mii.wID = wID;
234 mii.fType = fType;
235 InsertMenuItemW( hmenu, indexMenu, fByPosition, &mii);
238 /**************************************************************************
239 * ISvItemCm_fnQueryContextMenu()
241 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
242 IContextMenu2 *iface,
243 HMENU hmenu,
244 UINT indexMenu,
245 UINT idCmdFirst,
246 UINT idCmdLast,
247 UINT uFlags)
249 ItemCmImpl *This = (ItemCmImpl *)iface;
251 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
253 if (idCmdFirst != 0)
254 FIXME("We should use idCmdFirst=%d and idCmdLast=%d for command ids\n", idCmdFirst, idCmdLast);
256 if(!(CMF_DEFAULTONLY & uFlags) && This->cidl>0)
258 HMENU hmenures = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_SHV_FILE));
260 if(uFlags & CMF_EXPLORE)
261 RemoveMenu(hmenures, FCIDM_SHVIEW_OPEN, MF_BYCOMMAND);
263 Shell_MergeMenus(hmenu, GetSubMenu(hmenures, 0), indexMenu, idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
265 DestroyMenu(hmenures);
267 if(This->bAllValues)
269 MENUITEMINFOW mi;
270 WCHAR str[255];
271 mi.cbSize = sizeof(mi);
272 mi.fMask = MIIM_ID | MIIM_STRING | MIIM_FTYPE;
273 mi.dwTypeData = str;
274 mi.cch = 255;
275 GetMenuItemInfoW(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND, &mi);
276 RemoveMenu(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND);
277 _InsertMenuItemW(hmenu, (uFlags & CMF_EXPLORE) ? 1 : 2, MF_BYPOSITION, FCIDM_SHVIEW_EXPLORE, MFT_STRING, str, MFS_ENABLED);
280 SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
282 if(uFlags & ~CMF_CANRENAME)
283 RemoveMenu(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND);
284 else
285 EnableMenuItem(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND | ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
287 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (FCIDM_SHVIEWLAST));
289 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
292 /**************************************************************************
293 * DoOpenExplore
295 * for folders only
298 static void DoOpenExplore(
299 IContextMenu2 *iface,
300 HWND hwnd,
301 LPCSTR verb)
303 ItemCmImpl *This = (ItemCmImpl *)iface;
305 UINT i, bFolderFound = FALSE;
306 LPITEMIDLIST pidlFQ;
307 SHELLEXECUTEINFOA sei;
309 /* Find the first item in the list that is not a value. These commands
310 should never be invoked if there isn't at least one folder item in the list.*/
312 for(i = 0; i<This->cidl; i++)
314 if(!_ILIsValue(This->apidl[i]))
316 bFolderFound = TRUE;
317 break;
321 if (!bFolderFound) return;
323 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
325 ZeroMemory(&sei, sizeof(sei));
326 sei.cbSize = sizeof(sei);
327 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
328 sei.lpIDList = pidlFQ;
329 sei.lpClass = "Folder";
330 sei.hwnd = hwnd;
331 sei.nShow = SW_SHOWNORMAL;
332 sei.lpVerb = verb;
333 ShellExecuteExA(&sei);
334 SHFree(pidlFQ);
337 /**************************************************************************
338 * DoRename
340 static void DoRename(
341 IContextMenu2 *iface,
342 HWND hwnd)
344 ItemCmImpl *This = (ItemCmImpl *)iface;
346 LPSHELLBROWSER lpSB;
347 LPSHELLVIEW lpSV;
349 TRACE("(%p)->(wnd=%p)\n",This, hwnd);
351 /* get the active IShellView */
352 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
354 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
356 TRACE("(sv=%p)\n",lpSV);
357 IShellView_SelectItem(lpSV, This->apidl[0],
358 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
359 IShellView_Release(lpSV);
364 /**************************************************************************
365 * DoDelete
367 * deletes the currently selected items
369 static void DoDelete(IContextMenu2 *iface)
371 ItemCmImpl *This = (ItemCmImpl *)iface;
372 ISFHelper * psfhlp;
374 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
375 if (psfhlp)
377 ISFHelper_DeleteItems(psfhlp, This->cidl, (LPCITEMIDLIST *)This->apidl);
378 ISFHelper_Release(psfhlp);
382 /**************************************************************************
383 * DoCopyOrCut
385 * copies the currently selected items into the clipboard
387 static BOOL DoCopyOrCut(
388 IContextMenu2 *iface,
389 HWND hwnd,
390 BOOL bCut)
392 ItemCmImpl *This = (ItemCmImpl *)iface;
394 LPSHELLBROWSER lpSB;
395 LPSHELLVIEW lpSV;
396 LPDATAOBJECT lpDo;
398 TRACE("(%p)->(wnd=%p,bCut=0x%08x)\n",This, hwnd, bCut);
400 /* get the active IShellView */
401 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
403 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
405 if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
407 OleSetClipboard(lpDo);
408 IDataObject_Release(lpDo);
410 IShellView_Release(lpSV);
413 return TRUE;
415 /**************************************************************************
416 * ISvItemCm_fnInvokeCommand()
418 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
419 IContextMenu2 *iface,
420 LPCMINVOKECOMMANDINFO lpcmi)
422 ItemCmImpl *This = (ItemCmImpl *)iface;
424 if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
425 FIXME("Is an EX structure\n");
427 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
429 if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
431 TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
432 return E_INVALIDARG;
435 if (HIWORD(lpcmi->lpVerb) == 0)
437 switch(LOWORD(lpcmi->lpVerb))
439 case FCIDM_SHVIEW_EXPLORE:
440 TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
441 DoOpenExplore(iface, lpcmi->hwnd, "explore");
442 break;
443 case FCIDM_SHVIEW_OPEN:
444 TRACE("Verb FCIDM_SHVIEW_OPEN\n");
445 DoOpenExplore(iface, lpcmi->hwnd, "open");
446 break;
447 case FCIDM_SHVIEW_RENAME:
448 TRACE("Verb FCIDM_SHVIEW_RENAME\n");
449 DoRename(iface, lpcmi->hwnd);
450 break;
451 case FCIDM_SHVIEW_DELETE:
452 TRACE("Verb FCIDM_SHVIEW_DELETE\n");
453 DoDelete(iface);
454 break;
455 case FCIDM_SHVIEW_COPY:
456 TRACE("Verb FCIDM_SHVIEW_COPY\n");
457 DoCopyOrCut(iface, lpcmi->hwnd, FALSE);
458 break;
459 case FCIDM_SHVIEW_CUT:
460 TRACE("Verb FCIDM_SHVIEW_CUT\n");
461 DoCopyOrCut(iface, lpcmi->hwnd, TRUE);
462 break;
463 default:
464 FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb));
467 else
469 TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
470 if (strcmp(lpcmi->lpVerb,"delete")==0)
471 DoDelete(iface);
472 else
473 FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
475 return NOERROR;
478 /**************************************************************************
479 * ISvItemCm_fnGetCommandString()
481 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
482 IContextMenu2 *iface,
483 UINT_PTR idCommand,
484 UINT uFlags,
485 UINT* lpReserved,
486 LPSTR lpszName,
487 UINT uMaxNameLen)
489 ItemCmImpl *This = (ItemCmImpl *)iface;
491 HRESULT hr = E_INVALIDARG;
493 TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
495 switch(uFlags)
497 case GCS_HELPTEXTA:
498 case GCS_HELPTEXTW:
499 hr = E_NOTIMPL;
500 break;
502 case GCS_VERBA:
503 switch(idCommand)
505 case FCIDM_SHVIEW_RENAME:
506 strcpy((LPSTR)lpszName, "rename");
507 hr = NOERROR;
508 break;
510 break;
512 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
513 case, you need to do the lstrcpyW to the pointer passed.*/
514 case GCS_VERBW:
515 switch(idCommand)
516 { case FCIDM_SHVIEW_RENAME:
517 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
518 hr = NOERROR;
519 break;
521 break;
523 case GCS_VALIDATEA:
524 case GCS_VALIDATEW:
525 hr = NOERROR;
526 break;
528 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
529 return hr;
532 /**************************************************************************
533 * ISvItemCm_fnHandleMenuMsg()
534 * NOTES
535 * should be only in IContextMenu2 and IContextMenu3
536 * is nevertheless called from word95
538 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
539 IContextMenu2 *iface,
540 UINT uMsg,
541 WPARAM wParam,
542 LPARAM lParam)
544 ItemCmImpl *This = (ItemCmImpl *)iface;
546 TRACE("(%p)->(msg=%x wp=%lx lp=%lx)\n",This, uMsg, wParam, lParam);
548 return E_NOTIMPL;
551 static const IContextMenu2Vtbl cmvt =
553 ISvItemCm_fnQueryInterface,
554 ISvItemCm_fnAddRef,
555 ISvItemCm_fnRelease,
556 ISvItemCm_fnQueryContextMenu,
557 ISvItemCm_fnInvokeCommand,
558 ISvItemCm_fnGetCommandString,
559 ISvItemCm_fnHandleMenuMsg