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