c5176912bf4dc3926240b52680761a69bc15060f
[reactos.git] / reactos / lib / shell32 / shv_item_cmenu.c
1 /*
2 * IContextMenu for items in the shellview
3 *
4 * Copyright 1998, 2000 Juergen Schmied <juergen.schmied@debitel.net>
5 *
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.
10 *
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.
15 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <string.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26
27 #include "winerror.h"
28 #include "wine/debug.h"
29
30 #include "windef.h"
31 #include "wingdi.h"
32 #include "pidl.h"
33 #include "shlguid.h"
34 #include "undocshell.h"
35 #include "shlobj.h"
36
37 #include "shell32_main.h"
38 #include "shellfolder.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(shell);
41
42 /**************************************************************************
43 * IContextMenu Implementation
44 */
45 typedef struct
46 { IContextMenu2Vtbl *lpVtbl;
47 DWORD ref;
48 IShellFolder* pSFParent;
49 LPITEMIDLIST pidl; /* root pidl */
50 LPITEMIDLIST *apidl; /* array of child pidls */
51 UINT cidl;
52 BOOL bAllValues;
53 } ItemCmImpl;
54
55
56 static struct IContextMenu2Vtbl cmvt;
57
58 /**************************************************************************
59 * ISvItemCm_CanRenameItems()
60 */
61 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
62 { UINT i;
63 DWORD dwAttributes;
64
65 TRACE("(%p)->()\n",This);
66
67 if(This->apidl)
68 {
69 for(i = 0; i < This->cidl; i++){}
70 if(i > 1) return FALSE; /* can't rename more than one item at a time*/
71 dwAttributes = SFGAO_CANRENAME;
72 IShellFolder_GetAttributesOf(This->pSFParent, 1, (LPCITEMIDLIST*)This->apidl, &dwAttributes);
73 return dwAttributes & SFGAO_CANRENAME;
74 }
75 return FALSE;
76 }
77
78 /**************************************************************************
79 * ISvItemCm_Constructor()
80 */
81 IContextMenu2 *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *apidl, UINT cidl)
82 { ItemCmImpl* cm;
83 UINT u;
84
85 cm = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
86 cm->lpVtbl = &cmvt;
87 cm->ref = 1;
88 cm->pidl = ILClone(pidl);
89 cm->pSFParent = pSFParent;
90
91 if(pSFParent) IShellFolder_AddRef(pSFParent);
92
93 cm->apidl = _ILCopyaPidl(apidl, cidl);
94 cm->cidl = cidl;
95
96 cm->bAllValues = 1;
97 for(u = 0; u < cidl; u++)
98 {
99 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
100 }
101
102 TRACE("(%p)->()\n",cm);
103
104 return (IContextMenu2*)cm;
105 }
106
107 /**************************************************************************
108 * ISvItemCm_fnQueryInterface
109 */
110 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
111 {
112 ItemCmImpl *This = (ItemCmImpl *)iface;
113
114 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
115
116 *ppvObj = NULL;
117
118 if(IsEqualIID(riid, &IID_IUnknown) ||
119 IsEqualIID(riid, &IID_IContextMenu) ||
120 IsEqualIID(riid, &IID_IContextMenu2))
121 {
122 *ppvObj = This;
123 }
124 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
125 {
126 FIXME("-- LPSHELLEXTINIT pointer requested\n");
127 }
128
129 if(*ppvObj)
130 {
131 IUnknown_AddRef((IUnknown*)*ppvObj);
132 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
133 return S_OK;
134 }
135 TRACE("-- Interface: E_NOINTERFACE\n");
136 return E_NOINTERFACE;
137 }
138
139 /**************************************************************************
140 * ISvItemCm_fnAddRef
141 */
142 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu2 *iface)
143 {
144 ItemCmImpl *This = (ItemCmImpl *)iface;
145 ULONG refCount = InterlockedIncrement(&This->ref);
146
147 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
148
149 return refCount;
150 }
151
152 /**************************************************************************
153 * ISvItemCm_fnRelease
154 */
155 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu2 *iface)
156 {
157 ItemCmImpl *This = (ItemCmImpl *)iface;
158 ULONG refCount = InterlockedDecrement(&This->ref);
159
160 TRACE("(%p)->(count=%li)\n", This, refCount + 1);
161
162 if (!refCount)
163 {
164 TRACE(" destroying IContextMenu(%p)\n",This);
165
166 if(This->pSFParent)
167 IShellFolder_Release(This->pSFParent);
168
169 if(This->pidl)
170 SHFree(This->pidl);
171
172 /*make sure the pidl is freed*/
173 _ILFreeaPidl(This->apidl, This->cidl);
174
175 HeapFree(GetProcessHeap(),0,This);
176 }
177 return refCount;
178 }
179
180 /**************************************************************************
181 * ICM_InsertItem()
182 */
183 void WINAPI _InsertMenuItem (
184 HMENU hmenu,
185 UINT indexMenu,
186 BOOL fByPosition,
187 UINT wID,
188 UINT fType,
189 LPSTR dwTypeData,
190 UINT fState)
191 {
192 MENUITEMINFOA mii;
193
194 ZeroMemory(&mii, sizeof(mii));
195 mii.cbSize = sizeof(mii);
196 if (fType == MFT_SEPARATOR)
197 {
198 mii.fMask = MIIM_ID | MIIM_TYPE;
199 }
200 else
201 {
202 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
203 mii.dwTypeData = dwTypeData;
204 mii.fState = fState;
205 }
206 mii.wID = wID;
207 mii.fType = fType;
208 InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
209 }
210
211 /**************************************************************************
212 * ISvItemCm_fnQueryContextMenu()
213 * FIXME: load menu MENU_SHV_FILE out of resources instead if creating
214 * each menu item by calling _InsertMenuItem()
215 */
216 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
217 IContextMenu2 *iface,
218 HMENU hmenu,
219 UINT indexMenu,
220 UINT idCmdFirst,
221 UINT idCmdLast,
222 UINT uFlags)
223 {
224 ItemCmImpl *This = (ItemCmImpl *)iface;
225
226 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
227
228 if (idCmdFirst != 0)
229 FIXME("We should use idCmdFirst=%d and idCmdLast=%d for command ids\n", idCmdFirst, idCmdLast);
230
231 if(!(CMF_DEFAULTONLY & uFlags) && This->cidl>0)
232 {
233 if(!(uFlags & CMF_EXPLORE))
234 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Select", MFS_ENABLED);
235
236 if(This->bAllValues)
237 {
238 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
239 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
240 }
241 else
242 {
243 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
244 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
245 }
246
247 SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
248
249 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
250 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_COPY, MFT_STRING, "&Copy", MFS_ENABLED);
251 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_CUT, MFT_STRING, "&Cut", MFS_ENABLED);
252
253 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
254 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_DELETE, MFT_STRING, "&Delete", MFS_ENABLED);
255
256 if(uFlags & CMF_CANRENAME)
257 {
258 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_RENAME, MFT_STRING, "&Rename", ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
259 }
260
261 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
262 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_PROPERTIES, MFT_STRING, "&Properties::", MFS_ENABLED);
263
264
265 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (FCIDM_SHVIEWLAST));
266 }
267 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
268 }
269
270 /**************************************************************************
271 * DoOpenExplore
272 *
273 * for folders only
274 */
275
276 static void DoOpenExplore(
277 IContextMenu2 *iface,
278 HWND hwnd,
279 LPCSTR verb)
280 {
281 ItemCmImpl *This = (ItemCmImpl *)iface;
282
283 UINT i, bFolderFound = FALSE;
284 LPITEMIDLIST pidlFQ;
285 SHELLEXECUTEINFOA sei;
286
287 /* Find the first item in the list that is not a value. These commands
288 should never be invoked if there isn't at least one folder item in the list.*/
289
290 for(i = 0; i<This->cidl; i++)
291 {
292 if(!_ILIsValue(This->apidl[i]))
293 {
294 bFolderFound = TRUE;
295 break;
296 }
297 }
298
299 if (!bFolderFound) return;
300
301 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
302
303 ZeroMemory(&sei, sizeof(sei));
304 sei.cbSize = sizeof(sei);
305 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
306 sei.lpIDList = pidlFQ;
307 sei.lpClass = "Folder";
308 sei.hwnd = hwnd;
309 sei.nShow = SW_SHOWNORMAL;
310 sei.lpVerb = verb;
311 ShellExecuteExA(&sei);
312 SHFree(pidlFQ);
313 }
314
315 /**************************************************************************
316 * DoRename
317 */
318 static void DoRename(
319 IContextMenu2 *iface,
320 HWND hwnd)
321 {
322 ItemCmImpl *This = (ItemCmImpl *)iface;
323
324 LPSHELLBROWSER lpSB;
325 LPSHELLVIEW lpSV;
326
327 TRACE("(%p)->(wnd=%p)\n",This, hwnd);
328
329 /* get the active IShellView */
330 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
331 {
332 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
333 {
334 TRACE("(sv=%p)\n",lpSV);
335 IShellView_SelectItem(lpSV, This->apidl[0],
336 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
337 IShellView_Release(lpSV);
338 }
339 }
340 }
341
342 /**************************************************************************
343 * DoDelete
344 *
345 * deletes the currently selected items
346 */
347 static void DoDelete(IContextMenu2 *iface)
348 {
349 ItemCmImpl *This = (ItemCmImpl *)iface;
350 ISFHelper * psfhlp;
351
352 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
353 if (psfhlp)
354 {
355 ISFHelper_DeleteItems(psfhlp, This->cidl, (LPCITEMIDLIST *)This->apidl);
356 ISFHelper_Release(psfhlp);
357 }
358 }
359
360 /**************************************************************************
361 * DoCopyOrCut
362 *
363 * copies the currently selected items into the clipboard
364 */
365 static BOOL DoCopyOrCut(
366 IContextMenu2 *iface,
367 HWND hwnd,
368 BOOL bCut)
369 {
370 ItemCmImpl *This = (ItemCmImpl *)iface;
371
372 LPSHELLBROWSER lpSB;
373 LPSHELLVIEW lpSV;
374 LPDATAOBJECT lpDo;
375
376 TRACE("(%p)->(wnd=%p,bCut=0x%08x)\n",This, hwnd, bCut);
377
378 /* get the active IShellView */
379 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
380 {
381 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
382 {
383 if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
384 {
385 OleSetClipboard(lpDo);
386 IDataObject_Release(lpDo);
387 }
388 IShellView_Release(lpSV);
389 }
390 }
391 return TRUE;
392 }
393 /**************************************************************************
394 * ISvItemCm_fnInvokeCommand()
395 */
396 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
397 IContextMenu2 *iface,
398 LPCMINVOKECOMMANDINFO lpcmi)
399 {
400 ItemCmImpl *This = (ItemCmImpl *)iface;
401
402 if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
403 FIXME("Is an EX structure\n");
404
405 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
406
407 if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
408 {
409 TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
410 return E_INVALIDARG;
411 }
412
413 if (HIWORD(lpcmi->lpVerb) == 0)
414 {
415 switch(LOWORD(lpcmi->lpVerb))
416 {
417 case FCIDM_SHVIEW_EXPLORE:
418 TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
419 DoOpenExplore(iface, lpcmi->hwnd, "explore");
420 break;
421 case FCIDM_SHVIEW_OPEN:
422 TRACE("Verb FCIDM_SHVIEW_OPEN\n");
423 DoOpenExplore(iface, lpcmi->hwnd, "open");
424 break;
425 case FCIDM_SHVIEW_RENAME:
426 TRACE("Verb FCIDM_SHVIEW_RENAME\n");
427 DoRename(iface, lpcmi->hwnd);
428 break;
429 case FCIDM_SHVIEW_DELETE:
430 TRACE("Verb FCIDM_SHVIEW_DELETE\n");
431 DoDelete(iface);
432 break;
433 case FCIDM_SHVIEW_COPY:
434 TRACE("Verb FCIDM_SHVIEW_COPY\n");
435 DoCopyOrCut(iface, lpcmi->hwnd, FALSE);
436 break;
437 case FCIDM_SHVIEW_CUT:
438 TRACE("Verb FCIDM_SHVIEW_CUT\n");
439 DoCopyOrCut(iface, lpcmi->hwnd, TRUE);
440 break;
441 case FCIDM_SHVIEW_PROPERTIES:
442 TRACE("Verb FCIDM_SHVIEW_PROPERTIES\n");
443 /* Open the property sheet page */
444 SHObjectProperties(NULL, TEXT("SHOP_FILEPATH"), lpcmi->hwnd, NULL);
445 break;
446 default:
447 FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb));
448 }
449 }
450 else
451 {
452 TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
453 if (strcmp(lpcmi->lpVerb,"delete")==0)
454 DoDelete(iface);
455 else
456 FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
457 }
458 return NOERROR;
459 }
460
461 /**************************************************************************
462 * ISvItemCm_fnGetCommandString()
463 */
464 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
465 IContextMenu2 *iface,
466 UINT idCommand,
467 UINT uFlags,
468 UINT* lpReserved,
469 LPSTR lpszName,
470 UINT uMaxNameLen)
471 {
472 ItemCmImpl *This = (ItemCmImpl *)iface;
473
474 HRESULT hr = E_INVALIDARG;
475
476 TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
477
478 switch(uFlags)
479 {
480 case GCS_HELPTEXTA:
481 case GCS_HELPTEXTW:
482 hr = E_NOTIMPL;
483 break;
484
485 case GCS_VERBA:
486 switch(idCommand)
487 {
488 case FCIDM_SHVIEW_RENAME:
489 strcpy((LPSTR)lpszName, "rename");
490 hr = NOERROR;
491 break;
492 }
493 break;
494
495 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
496 case, you need to do the lstrcpyW to the pointer passed.*/
497 case GCS_VERBW:
498 switch(idCommand)
499 { case FCIDM_SHVIEW_RENAME:
500 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
501 hr = NOERROR;
502 break;
503 }
504 break;
505
506 case GCS_VALIDATEA:
507 case GCS_VALIDATEW:
508 hr = NOERROR;
509 break;
510 }
511 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
512 return hr;
513 }
514
515 /**************************************************************************
516 * ISvItemCm_fnHandleMenuMsg()
517 * NOTES
518 * should be only in IContextMenu2 and IContextMenu3
519 * is nevertheless called from word95
520 */
521 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
522 IContextMenu2 *iface,
523 UINT uMsg,
524 WPARAM wParam,
525 LPARAM lParam)
526 {
527 ItemCmImpl *This = (ItemCmImpl *)iface;
528
529 TRACE("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
530
531 return E_NOTIMPL;
532 }
533
534 static struct IContextMenu2Vtbl cmvt =
535 {
536 ISvItemCm_fnQueryInterface,
537 ISvItemCm_fnAddRef,
538 ISvItemCm_fnRelease,
539 ISvItemCm_fnQueryContextMenu,
540 ISvItemCm_fnInvokeCommand,
541 ISvItemCm_fnGetCommandString,
542 ISvItemCm_fnHandleMenuMsg
543 };