shell32: Use shared IUnknown implementation for both vtables.
[wine] / dlls / shell32 / shlview_cmenu.c
1 /*
2  *      IContextMenu for items in the shellview
3  *
4  * Copyright 1998-2000 Juergen Schmied <juergen.schmied@debitel.net>,
5  *                                     <juergen.schmied@metronet.de>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <string.h>
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
28 #include "winerror.h"
29 #include "wine/debug.h"
30
31 #include "windef.h"
32 #include "wingdi.h"
33 #include "pidl.h"
34 #include "undocshell.h"
35 #include "shlobj.h"
36 #include "winreg.h"
37 #include "prsht.h"
38
39 #include "shell32_main.h"
40 #include "shellfolder.h"
41
42 #include "shresdef.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
45
46 typedef struct
47 {
48     IContextMenu2 IContextMenu2_iface;
49     LONG ref;
50
51     IShellFolder* parent;
52     UINT verb_offset;
53
54     /* item menu data */
55     LPITEMIDLIST  pidl;  /* root pidl */
56     LPITEMIDLIST *apidl; /* array of child pidls */
57     UINT cidl;
58     BOOL allvalues;
59
60     /* background menu data */
61     BOOL desktop;
62 } ContextMenu;
63
64 static inline ContextMenu *impl_from_IContextMenu2(IContextMenu2 *iface)
65 {
66     return CONTAINING_RECORD(iface, ContextMenu, IContextMenu2_iface);
67 }
68
69 static HRESULT WINAPI ContextMenu_QueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
70 {
71     ContextMenu *This = impl_from_IContextMenu2(iface);
72
73     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObj);
74
75     *ppvObj = NULL;
76
77     if (IsEqualIID(riid, &IID_IUnknown) ||
78         IsEqualIID(riid, &IID_IContextMenu) ||
79         IsEqualIID(riid, &IID_IContextMenu2))
80     {
81         *ppvObj = This;
82     }
83     else if (IsEqualIID(riid, &IID_IShellExtInit))  /*IShellExtInit*/
84     {
85         FIXME("-- LPSHELLEXTINIT pointer requested\n");
86     }
87
88     if(*ppvObj)
89     {
90         IContextMenu2_AddRef(iface);
91         return S_OK;
92     }
93
94     TRACE("-- Interface: E_NOINTERFACE\n");
95     return E_NOINTERFACE;
96 }
97
98 static ULONG WINAPI ContextMenu_AddRef(IContextMenu2 *iface)
99 {
100     ContextMenu *This = impl_from_IContextMenu2(iface);
101     ULONG ref = InterlockedIncrement(&This->ref);
102     TRACE("(%p)->(%u)\n", This, ref);
103     return ref;
104 }
105
106 static ULONG WINAPI ContextMenu_Release(IContextMenu2 *iface)
107 {
108     ContextMenu *This = impl_from_IContextMenu2(iface);
109     ULONG ref = InterlockedDecrement(&This->ref);
110
111     TRACE("(%p)->(%u)\n", This, ref);
112
113     if (!ref)
114     {
115         if(This->parent)
116             IShellFolder_Release(This->parent);
117
118         SHFree(This->pidl);
119         _ILFreeaPidl(This->apidl, This->cidl);
120
121         HeapFree(GetProcessHeap(), 0, This);
122     }
123
124     return ref;
125 }
126
127 static HRESULT WINAPI ItemMenu_QueryContextMenu(
128         IContextMenu2 *iface,
129         HMENU hmenu,
130         UINT indexMenu,
131         UINT idCmdFirst,
132         UINT idCmdLast,
133         UINT uFlags)
134 {
135     ContextMenu *This = impl_from_IContextMenu2(iface);
136     INT uIDMax;
137
138     TRACE("(%p)->(%p %d 0x%x 0x%x 0x%x )\n", This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
139
140     This->verb_offset = idCmdFirst;
141
142     if(!(CMF_DEFAULTONLY & uFlags) && This->cidl > 0)
143     {
144         HMENU hmenures = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_SHV_FILE));
145
146         if(uFlags & CMF_EXPLORE)
147             RemoveMenu(hmenures, FCIDM_SHVIEW_OPEN, MF_BYCOMMAND);
148
149         uIDMax = Shell_MergeMenus(hmenu, GetSubMenu(hmenures, 0), indexMenu, idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
150
151         DestroyMenu(hmenures);
152
153         if(This->allvalues)
154         {
155             MENUITEMINFOW mi;
156             WCHAR str[255];
157             mi.cbSize = sizeof(mi);
158             mi.fMask = MIIM_ID | MIIM_STRING | MIIM_FTYPE;
159             mi.dwTypeData = str;
160             mi.cch = 255;
161             GetMenuItemInfoW(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND, &mi);
162             RemoveMenu(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND);
163
164             mi.cbSize = sizeof(mi);
165             mi.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
166             mi.dwTypeData = str;
167             mi.fState = MFS_ENABLED;
168             mi.wID = FCIDM_SHVIEW_EXPLORE;
169             mi.fType = MFT_STRING;
170             InsertMenuItemW(hmenu, (uFlags & CMF_EXPLORE) ? 1 : 2, MF_BYPOSITION, &mi);
171         }
172
173         SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
174
175         if(uFlags & ~CMF_CANRENAME)
176             RemoveMenu(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND);
177         else
178         {
179             UINT enable = MF_BYCOMMAND;
180
181             /* can't rename more than one item at a time*/
182             if (!This->apidl || This->cidl > 1)
183                 enable |= MFS_DISABLED;
184             else
185             {
186                 DWORD attr = SFGAO_CANRENAME;
187
188                 IShellFolder_GetAttributesOf(This->parent, 1, (LPCITEMIDLIST*)This->apidl, &attr);
189                 enable |= (attr & SFGAO_CANRENAME) ? MFS_ENABLED : MFS_DISABLED;
190             }
191
192             EnableMenuItem(hmenu, FCIDM_SHVIEW_RENAME, enable);
193         }
194
195         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, uIDMax-idCmdFirst);
196     }
197     return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
198 }
199
200 /**************************************************************************
201 * DoOpenExplore
202 *
203 *  for folders only
204 */
205
206 static void DoOpenExplore(ContextMenu *This, HWND hwnd, LPCSTR verb)
207 {
208         UINT i, bFolderFound = FALSE;
209         LPITEMIDLIST    pidlFQ;
210         SHELLEXECUTEINFOA       sei;
211
212         /* Find the first item in the list that is not a value. These commands
213             should never be invoked if there isn't at least one folder item in the list.*/
214
215         for(i = 0; i<This->cidl; i++)
216         {
217           if(!_ILIsValue(This->apidl[i]))
218           {
219             bFolderFound = TRUE;
220             break;
221           }
222         }
223
224         if (!bFolderFound) return;
225
226         pidlFQ = ILCombine(This->pidl, This->apidl[i]);
227
228         ZeroMemory(&sei, sizeof(sei));
229         sei.cbSize = sizeof(sei);
230         sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
231         sei.lpIDList = pidlFQ;
232         sei.lpClass = "Folder";
233         sei.hwnd = hwnd;
234         sei.nShow = SW_SHOWNORMAL;
235         sei.lpVerb = verb;
236         ShellExecuteExA(&sei);
237         SHFree(pidlFQ);
238 }
239
240 /**************************************************************************
241  * DoDelete
242  *
243  * deletes the currently selected items
244  */
245 static void DoDelete(ContextMenu *This)
246 {
247     ISFHelper *helper;
248
249     IShellFolder_QueryInterface(This->parent, &IID_ISFHelper, (void**)&helper);
250     if (helper)
251     {
252         ISFHelper_DeleteItems(helper, This->cidl, (LPCITEMIDLIST*)This->apidl);
253         ISFHelper_Release(helper);
254     }
255 }
256
257 /**************************************************************************
258  * DoCopyOrCut
259  *
260  * copies the currently selected items into the clipboard
261  */
262 static BOOL DoCopyOrCut(ContextMenu *This, HWND hwnd, BOOL cut)
263 {
264     IDataObject *dataobject;
265     IShellBrowser *browser;
266     IShellView *view;
267
268     TRACE("(%p)->(wnd=%p, cut=%d)\n", This, hwnd, cut);
269
270     /* get the active IShellView */
271     if ((browser = (IShellBrowser*)SendMessageA(hwnd, CWM_GETISHELLBROWSER, 0, 0)))
272     {
273         if (SUCCEEDED(IShellBrowser_QueryActiveShellView(browser, &view)))
274         {
275             if (SUCCEEDED(IShellView_GetItemObject(view, SVGIO_SELECTION, &IID_IDataObject, (void**)&dataobject)))
276             {
277                 OleSetClipboard(dataobject);
278                 IDataObject_Release(dataobject);
279             }
280             IShellView_Release(view);
281         }
282     }
283
284     return TRUE;
285 }
286
287 /**************************************************************************
288  * Properties_AddPropSheetCallback
289  *
290  * Used by DoOpenProperties through SHCreatePropSheetExtArrayEx to add
291  * propertysheet pages from shell extensions.
292  */
293 static BOOL CALLBACK Properties_AddPropSheetCallback(HPROPSHEETPAGE hpage, LPARAM lparam)
294 {
295         LPPROPSHEETHEADERW psh = (LPPROPSHEETHEADERW) lparam;
296         psh->u3.phpage[psh->nPages++] = hpage;
297
298         return TRUE;
299 }
300
301 static void DoOpenProperties(ContextMenu *This, HWND hwnd)
302 {
303         static const UINT MAX_PROP_PAGES = 99;
304         static const WCHAR wszFolder[] = {'F','o','l','d','e','r', 0};
305         static const WCHAR wszFiletypeAll[] = {'*',0};
306         LPSHELLFOLDER lpDesktopSF;
307         LPSHELLFOLDER lpSF;
308         LPDATAOBJECT lpDo;
309         WCHAR wszFiletype[MAX_PATH];
310         WCHAR wszFilename[MAX_PATH];
311         PROPSHEETHEADERW psh;
312         HPROPSHEETPAGE hpages[MAX_PROP_PAGES];
313         HPSXA hpsxa;
314         UINT ret;
315
316         TRACE("(%p)->(wnd=%p)\n", This, hwnd);
317
318         ZeroMemory(&psh, sizeof(PROPSHEETHEADERW));
319         psh.dwSize = sizeof (PROPSHEETHEADERW);
320         psh.hwndParent = hwnd;
321         psh.dwFlags = PSH_PROPTITLE;
322         psh.nPages = 0;
323         psh.u3.phpage = hpages;
324         psh.u2.nStartPage = 0;
325
326         _ILSimpleGetTextW(This->apidl[0], (LPVOID)wszFilename, MAX_PATH);
327         psh.pszCaption = (LPCWSTR)wszFilename;
328
329         /* Find out where to look for the shell extensions */
330         if (_ILIsValue(This->apidl[0]))
331         {
332             char sTemp[64];
333             sTemp[0] = 0;
334             if (_ILGetExtension(This->apidl[0], sTemp, 64))
335             {
336                 HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE);
337                 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, wszFiletype, MAX_PATH);
338             }
339             else
340             {
341                 wszFiletype[0] = 0;
342             }
343         }
344         else if (_ILIsFolder(This->apidl[0]))
345         {
346             lstrcpynW(wszFiletype, wszFolder, 64);
347         }
348         else if (_ILIsSpecialFolder(This->apidl[0]))
349         {
350             LPGUID folderGUID;
351             static const WCHAR wszclsid[] = {'C','L','S','I','D','\\', 0};
352             folderGUID = _ILGetGUIDPointer(This->apidl[0]);
353             lstrcpyW(wszFiletype, wszclsid);
354             StringFromGUID2(folderGUID, &wszFiletype[6], MAX_PATH - 6);
355         }
356         else
357         {
358             FIXME("Requested properties for unknown type.\n");
359             return;
360         }
361
362         /* Get a suitable DataObject for accessing the files */
363         SHGetDesktopFolder(&lpDesktopSF);
364         if (_ILIsPidlSimple(This->pidl))
365         {
366             ret = IShellFolder_GetUIObjectOf(lpDesktopSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
367                                              &IID_IDataObject, NULL, (LPVOID *)&lpDo);
368             IShellFolder_Release(lpDesktopSF);
369         }
370         else
371         {
372             IShellFolder_BindToObject(lpDesktopSF, This->pidl, NULL, &IID_IShellFolder, (LPVOID*) &lpSF);
373             ret = IShellFolder_GetUIObjectOf(lpSF, hwnd, This->cidl, (LPCITEMIDLIST*)This->apidl,
374                                              &IID_IDataObject, NULL, (LPVOID *)&lpDo);
375             IShellFolder_Release(lpSF);
376             IShellFolder_Release(lpDesktopSF);
377         }
378
379         if (SUCCEEDED(ret))
380         {
381             hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletype, MAX_PROP_PAGES - psh.nPages, lpDo);
382             if (hpsxa != NULL)
383             {
384                 SHAddFromPropSheetExtArray(hpsxa, Properties_AddPropSheetCallback, (LPARAM)&psh);
385                 SHDestroyPropSheetExtArray(hpsxa);
386             }
387             hpsxa = SHCreatePropSheetExtArrayEx(HKEY_CLASSES_ROOT, wszFiletypeAll, MAX_PROP_PAGES - psh.nPages, lpDo);
388             if (hpsxa != NULL)
389             {
390                 SHAddFromPropSheetExtArray(hpsxa, Properties_AddPropSheetCallback, (LPARAM)&psh);
391                 SHDestroyPropSheetExtArray(hpsxa);
392             }
393             IDataObject_Release(lpDo);
394         }
395
396         if (psh.nPages)
397             PropertySheetW(&psh);
398         else
399             FIXME("No property pages found.\n");
400 }
401
402 static HRESULT WINAPI ItemMenu_InvokeCommand(
403         IContextMenu2 *iface,
404         LPCMINVOKECOMMANDINFO lpcmi)
405 {
406     ContextMenu *This = impl_from_IContextMenu2(iface);
407
408     if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
409         FIXME("Is an EX structure\n");
410
411     TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
412
413     if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
414     {
415         TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
416         return E_INVALIDARG;
417     }
418
419     if (HIWORD(lpcmi->lpVerb) == 0)
420     {
421         switch(LOWORD(lpcmi->lpVerb - This->verb_offset))
422         {
423         case FCIDM_SHVIEW_EXPLORE:
424             TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
425             DoOpenExplore(This, lpcmi->hwnd, "explore");
426             break;
427         case FCIDM_SHVIEW_OPEN:
428             TRACE("Verb FCIDM_SHVIEW_OPEN\n");
429             DoOpenExplore(This, lpcmi->hwnd, "open");
430             break;
431         case FCIDM_SHVIEW_RENAME:
432         {
433             IShellBrowser *browser;
434
435             /* get the active IShellView */
436             browser = (IShellBrowser*)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER, 0, 0);
437             if (browser)
438             {
439                 IShellView *view;
440
441                 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(browser, &view)))
442                 {
443                     TRACE("(shellview=%p)\n", view);
444                     IShellView_SelectItem(view, This->apidl[0],
445                          SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
446                     IShellView_Release(view);
447                 }
448             }
449             break;
450         }
451         case FCIDM_SHVIEW_DELETE:
452             TRACE("Verb FCIDM_SHVIEW_DELETE\n");
453             DoDelete(This);
454             break;
455         case FCIDM_SHVIEW_COPY:
456             TRACE("Verb FCIDM_SHVIEW_COPY\n");
457             DoCopyOrCut(This, lpcmi->hwnd, FALSE);
458             break;
459         case FCIDM_SHVIEW_CUT:
460             TRACE("Verb FCIDM_SHVIEW_CUT\n");
461             DoCopyOrCut(This, lpcmi->hwnd, TRUE);
462             break;
463         case FCIDM_SHVIEW_PROPERTIES:
464             TRACE("Verb FCIDM_SHVIEW_PROPERTIES\n");
465             DoOpenProperties(This, lpcmi->hwnd);
466             break;
467         default:
468             FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb)-This->verb_offset);
469             return E_INVALIDARG;
470         }
471     }
472     else
473     {
474         TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
475         if (strcmp(lpcmi->lpVerb,"delete")==0)
476             DoDelete(This);
477         else if (strcmp(lpcmi->lpVerb,"properties")==0)
478             DoOpenProperties(This, lpcmi->hwnd);
479         else {
480             FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
481             return E_FAIL;
482         }
483     }
484     return S_OK;
485 }
486
487 static HRESULT WINAPI ItemMenu_GetCommandString(
488         IContextMenu2 *iface,
489         UINT_PTR idCommand,
490         UINT uFlags,
491         UINT* lpReserved,
492         LPSTR lpszName,
493         UINT uMaxNameLen)
494 {
495         ContextMenu *This = impl_from_IContextMenu2(iface);
496         HRESULT hr = E_INVALIDARG;
497
498         TRACE("(%p)->(%lx flags=%x %p name=%p len=%x)\n", This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
499
500         switch(uFlags)
501         {
502           case GCS_HELPTEXTA:
503           case GCS_HELPTEXTW:
504             hr = E_NOTIMPL;
505             break;
506
507           case GCS_VERBA:
508             switch(idCommand-This->verb_offset)
509             {
510             case FCIDM_SHVIEW_RENAME:
511                 strcpy(lpszName, "rename");
512                 hr = S_OK;
513                 break;
514             }
515             break;
516
517              /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
518              case, you need to do the lstrcpyW to the pointer passed.*/
519           case GCS_VERBW:
520             switch(idCommand-This->verb_offset)
521             {
522             case FCIDM_SHVIEW_RENAME:
523                 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
524                 hr = S_OK;
525                 break;
526             }
527             break;
528
529           case GCS_VALIDATEA:
530           case GCS_VALIDATEW:
531             hr = S_OK;
532             break;
533         }
534         TRACE("-- (%p)->(name=%s)\n", This, lpszName);
535         return hr;
536 }
537
538 /**************************************************************************
539 * NOTES
540 *  should be only in IContextMenu2 and IContextMenu3
541 *  is nevertheless called from word95
542 */
543 static HRESULT WINAPI ItemMenu_HandleMenuMsg(
544         IContextMenu2 *iface,
545         UINT uMsg,
546         WPARAM wParam,
547         LPARAM lParam)
548 {
549     ContextMenu *This = impl_from_IContextMenu2(iface);
550     TRACE("(%p)->(0x%x 0x%lx 0x%lx)\n", This, uMsg, wParam, lParam);
551     return E_NOTIMPL;
552 }
553
554 static const IContextMenu2Vtbl ItemContextMenuVtbl =
555 {
556     ContextMenu_QueryInterface,
557     ContextMenu_AddRef,
558     ContextMenu_Release,
559     ItemMenu_QueryContextMenu,
560     ItemMenu_InvokeCommand,
561     ItemMenu_GetCommandString,
562     ItemMenu_HandleMenuMsg
563 };
564
565 IContextMenu2 *ItemMenu_Constructor(IShellFolder *parent, LPCITEMIDLIST pidl, const LPCITEMIDLIST *apidl, UINT cidl)
566 {
567     ContextMenu* This;
568     UINT u;
569
570     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
571     This->IContextMenu2_iface.lpVtbl = &ItemContextMenuVtbl;
572     This->ref = 1;
573     This->verb_offset = 0;
574     This->parent = parent;
575     if (parent) IShellFolder_AddRef(parent);
576
577     This->pidl = ILClone(pidl);
578     This->apidl = _ILCopyaPidl(apidl, cidl);
579     This->cidl = cidl;
580     This->allvalues = TRUE;
581
582     This->desktop = FALSE;
583
584     for(u = 0; u < cidl; u++)
585        This->allvalues &= (_ILIsValue(apidl[u]) ? 1 : 0);
586
587     TRACE("(%p)\n", This);
588
589     return &This->IContextMenu2_iface;
590 }
591
592 /* Background menu implementation */
593 static HRESULT WINAPI BackgroundMenu_QueryContextMenu(
594         IContextMenu2 *iface,
595         HMENU hMenu,
596         UINT indexMenu,
597         UINT idCmdFirst,
598         UINT idCmdLast,
599         UINT uFlags)
600 {
601     ContextMenu *This = impl_from_IContextMenu2(iface);
602     HMENU hMyMenu;
603     UINT idMax;
604     HRESULT hr;
605
606     TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",
607           This, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
608
609     This->verb_offset = idCmdFirst;
610
611     hMyMenu = LoadMenuA(shell32_hInstance, "MENU_002");
612     if (uFlags & CMF_DEFAULTONLY)
613     {
614         HMENU ourMenu = GetSubMenu(hMyMenu,0);
615         UINT oldDef = GetMenuDefaultItem(hMenu,TRUE,GMDI_USEDISABLED);
616         UINT newDef = GetMenuDefaultItem(ourMenu,TRUE,GMDI_USEDISABLED);
617         if (newDef != oldDef)
618             SetMenuDefaultItem(hMenu,newDef,TRUE);
619         if (newDef!=0xFFFFFFFF)
620             hr =  MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, newDef+1);
621         else
622             hr =  MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
623     }
624     else
625     {
626         idMax = Shell_MergeMenus (hMenu, GetSubMenu(hMyMenu,0), indexMenu,
627                                   idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
628         hr =  MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, idMax-idCmdFirst);
629     }
630     DestroyMenu(hMyMenu);
631
632     TRACE("(%p)->returning 0x%x\n",This,hr);
633     return hr;
634 }
635
636 static void DoNewFolder(ContextMenu *This, IShellView *view)
637 {
638     ISFHelper *helper;
639
640     IShellFolder_QueryInterface(This->parent, &IID_ISFHelper, (void**)&helper);
641     if (helper)
642     {
643         WCHAR nameW[MAX_PATH];
644         LPITEMIDLIST pidl;
645
646         ISFHelper_GetUniqueName(helper, nameW, MAX_PATH);
647         ISFHelper_AddFolder(helper, 0, nameW, &pidl);
648
649         if (view)
650         {
651             /* if we are in a shellview do labeledit */
652             IShellView_SelectItem(view,
653                     pidl,(SVSI_DESELECTOTHERS | SVSI_EDIT | SVSI_ENSUREVISIBLE
654                     |SVSI_FOCUSED|SVSI_SELECT));
655         }
656
657         SHFree(pidl);
658         ISFHelper_Release(helper);
659     }
660 }
661
662 static BOOL DoPaste(ContextMenu *This)
663 {
664         BOOL bSuccess = FALSE;
665         IDataObject * pda;
666
667         TRACE("\n");
668
669         if(SUCCEEDED(OleGetClipboard(&pda)))
670         {
671           STGMEDIUM medium;
672           FORMATETC formatetc;
673
674           TRACE("pda=%p\n", pda);
675
676           /* Set the FORMATETC structure*/
677           InitFormatEtc(formatetc, RegisterClipboardFormatW(CFSTR_SHELLIDLISTW), TYMED_HGLOBAL);
678
679           /* Get the pidls from IDataObject */
680           if(SUCCEEDED(IDataObject_GetData(pda,&formatetc,&medium)))
681           {
682             LPITEMIDLIST * apidl;
683             LPITEMIDLIST pidl;
684             IShellFolder *psfFrom = NULL, *psfDesktop;
685
686             LPIDA lpcida = GlobalLock(medium.u.hGlobal);
687             TRACE("cida=%p\n", lpcida);
688
689             apidl = _ILCopyCidaToaPidl(&pidl, lpcida);
690
691             /* bind to the source shellfolder */
692             SHGetDesktopFolder(&psfDesktop);
693             if(psfDesktop)
694             {
695               IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (LPVOID*)&psfFrom);
696               IShellFolder_Release(psfDesktop);
697             }
698
699             if (psfFrom)
700             {
701               /* get source and destination shellfolder */
702               ISFHelper *psfhlpdst, *psfhlpsrc;
703               IShellFolder_QueryInterface(This->parent, &IID_ISFHelper, (void**)&psfhlpdst);
704               IShellFolder_QueryInterface(psfFrom, &IID_ISFHelper, (void**)&psfhlpsrc);
705
706               /* do the copy/move */
707               if (psfhlpdst && psfhlpsrc)
708               {
709                 ISFHelper_CopyItems(psfhlpdst, psfFrom, lpcida->cidl, (LPCITEMIDLIST*)apidl);
710                 /* FIXME handle move
711                 ISFHelper_DeleteItems(psfhlpsrc, lpcida->cidl, apidl);
712                 */
713               }
714               if(psfhlpdst) ISFHelper_Release(psfhlpdst);
715               if(psfhlpsrc) ISFHelper_Release(psfhlpsrc);
716               IShellFolder_Release(psfFrom);
717             }
718
719             _ILFreeaPidl(apidl, lpcida->cidl);
720             SHFree(pidl);
721
722             /* release the medium*/
723             ReleaseStgMedium(&medium);
724           }
725           IDataObject_Release(pda);
726         }
727 #if 0
728         HGLOBAL  hMem;
729
730         OpenClipboard(NULL);
731         hMem = GetClipboardData(CF_HDROP);
732
733         if(hMem)
734         {
735           char * pDropFiles = GlobalLock(hMem);
736           if(pDropFiles)
737           {
738             int len, offset = sizeof(DROPFILESTRUCT);
739
740             while( pDropFiles[offset] != 0)
741             {
742               len = strlen(pDropFiles + offset);
743               TRACE("%s\n", pDropFiles + offset);
744               offset += len+1;
745             }
746           }
747           GlobalUnlock(hMem);
748         }
749         CloseClipboard();
750 #endif
751         return bSuccess;
752 }
753
754 static HRESULT WINAPI BackgroundMenu_InvokeCommand(
755         IContextMenu2 *iface,
756         LPCMINVOKECOMMANDINFO lpcmi)
757 {
758     ContextMenu *This = impl_from_IContextMenu2(iface);
759     IShellBrowser *browser;
760     IShellView *view = NULL;
761     HWND hWnd = NULL;
762
763     TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n", This, lpcmi, lpcmi->lpVerb, lpcmi->hwnd);
764
765     /* get the active IShellView */
766     if ((browser = (IShellBrowser*)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER, 0, 0)))
767     {
768         if (SUCCEEDED(IShellBrowser_QueryActiveShellView(browser, &view)))
769             IShellView_GetWindow(view, &hWnd);
770     }
771
772     if(HIWORD(lpcmi->lpVerb))
773     {
774         TRACE("%s\n", debugstr_a(lpcmi->lpVerb));
775
776         if (!strcmp(lpcmi->lpVerb, CMDSTR_NEWFOLDERA))
777         {
778             DoNewFolder(This, view);
779         }
780         else if (!strcmp(lpcmi->lpVerb, CMDSTR_VIEWLISTA))
781         {
782             if (hWnd) SendMessageA(hWnd, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_LISTVIEW, 0), 0);
783         }
784         else if (!strcmp(lpcmi->lpVerb, CMDSTR_VIEWDETAILSA))
785         {
786             if (hWnd) SendMessageA(hWnd, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_REPORTVIEW, 0), 0);
787         }
788         else
789         {
790             FIXME("please report: unknown verb %s\n", debugstr_a(lpcmi->lpVerb));
791         }
792     }
793     else
794     {
795         switch (LOWORD(lpcmi->lpVerb) - This->verb_offset)
796         {
797             case FCIDM_SHVIEW_REFRESH:
798                 if (view) IShellView_Refresh(view);
799                 break;
800
801             case FCIDM_SHVIEW_NEWFOLDER:
802                 DoNewFolder(This, view);
803                 break;
804
805             case FCIDM_SHVIEW_INSERT:
806                 DoPaste(This);
807                 break;
808
809             case FCIDM_SHVIEW_PROPERTIES:
810                 if (This->desktop) {
811                     ShellExecuteA(lpcmi->hwnd, "open", "rundll32.exe shell32.dll,Control_RunDLL desk.cpl", NULL, NULL, SW_SHOWNORMAL);
812                 } else {
813                     FIXME("launch item properties dialog\n");
814                 }
815                 break;
816
817             default:
818                 /* if it's an id just pass it to the parent shv */
819                 if (hWnd) SendMessageA(hWnd, WM_COMMAND, MAKEWPARAM(LOWORD(lpcmi->lpVerb), 0), 0);
820                 break;
821          }
822     }
823
824     if (view)
825         IShellView_Release(view);
826
827     return S_OK;
828 }
829
830 static HRESULT WINAPI BackgroundMenu_GetCommandString(
831         IContextMenu2 *iface,
832         UINT_PTR idCommand,
833         UINT uFlags,
834         UINT* lpReserved,
835         LPSTR lpszName,
836         UINT uMaxNameLen)
837 {
838         ContextMenu *This = impl_from_IContextMenu2(iface);
839
840         TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
841
842         /* test the existence of the menu items, the file dialog enables
843            the buttons according to this */
844         if (uFlags == GCS_VALIDATEA)
845         {
846           if(HIWORD(idCommand))
847           {
848             if (!strcmp((LPSTR)idCommand, CMDSTR_VIEWLISTA) ||
849                 !strcmp((LPSTR)idCommand, CMDSTR_VIEWDETAILSA) ||
850                 !strcmp((LPSTR)idCommand, CMDSTR_NEWFOLDERA))
851             {
852               return S_OK;
853             }
854           }
855         }
856
857         FIXME("unknown command string\n");
858         return E_FAIL;
859 }
860
861 static HRESULT WINAPI BackgroundMenu_HandleMenuMsg(
862         IContextMenu2 *iface,
863         UINT uMsg,
864         WPARAM wParam,
865         LPARAM lParam)
866 {
867     ContextMenu *This = impl_from_IContextMenu2(iface);
868     FIXME("(%p)->(msg=%x wp=%lx lp=%lx)\n",This, uMsg, wParam, lParam);
869     return E_NOTIMPL;
870 }
871
872 static const IContextMenu2Vtbl BackgroundContextMenuVtbl =
873 {
874     ContextMenu_QueryInterface,
875     ContextMenu_AddRef,
876     ContextMenu_Release,
877     BackgroundMenu_QueryContextMenu,
878     BackgroundMenu_InvokeCommand,
879     BackgroundMenu_GetCommandString,
880     BackgroundMenu_HandleMenuMsg
881 };
882
883 IContextMenu2 *BackgroundMenu_Constructor(IShellFolder *parent, BOOL desktop)
884 {
885     ContextMenu *This;
886
887     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
888     This->IContextMenu2_iface.lpVtbl = &BackgroundContextMenuVtbl;
889     This->ref = 1;
890     This->parent = parent;
891     This->verb_offset = 0;
892
893     This->pidl = NULL;
894     This->apidl = NULL;
895     This->cidl = 0;
896     This->allvalues = FALSE;
897
898     This->desktop = desktop;
899     if (parent) IShellFolder_AddRef(parent);
900
901     TRACE("(%p)\n", This);
902     return &This->IContextMenu2_iface;
903 }