shell32: Add missing multiply by sizeof(WCHAR)l
[wine] / dlls / shell32 / shv_bg_cmenu.c
1 /*
2  *      IContextMenu
3  *      ShellView Background Context Menu (shv_bg_cm)
4  *
5  *      Copyright 1999  Juergen Schmied <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 #include <string.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26
27 #include "wine/debug.h"
28
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "pidl.h"
32 #include "shlguid.h"
33 #include "shlobj.h"
34
35 #include "shell32_main.h"
36 #include "shellfolder.h"
37 #include "undocshell.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(shell);
40
41 /**************************************************************************
42 *  IContextMenu Implementation
43 */
44 typedef struct
45 {
46         const IContextMenu2Vtbl *lpVtbl;
47         IShellFolder*   pSFParent;
48         LONG            ref;
49         BOOL            bDesktop;
50 } BgCmImpl;
51
52
53 static const IContextMenu2Vtbl cmvt;
54
55 /**************************************************************************
56 *   ISVBgCm_Constructor()
57 */
58 IContextMenu2 *ISvBgCm_Constructor(IShellFolder* pSFParent, BOOL bDesktop)
59 {
60         BgCmImpl* cm;
61
62         cm = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(BgCmImpl));
63         cm->lpVtbl = &cmvt;
64         cm->ref = 1;
65         cm->pSFParent = pSFParent;
66         cm->bDesktop = bDesktop;
67         if(pSFParent) IShellFolder_AddRef(pSFParent);
68
69         TRACE("(%p)->()\n",cm);
70         return (IContextMenu2*)cm;
71 }
72
73 /**************************************************************************
74 *  ISVBgCm_fnQueryInterface
75 */
76 static HRESULT WINAPI ISVBgCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
77 {
78         BgCmImpl *This = (BgCmImpl *)iface;
79
80         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
81
82         *ppvObj = NULL;
83
84         if(IsEqualIID(riid, &IID_IUnknown) ||
85            IsEqualIID(riid, &IID_IContextMenu) ||
86            IsEqualIID(riid, &IID_IContextMenu2))
87         {
88           *ppvObj = This;
89         }
90         else if(IsEqualIID(riid, &IID_IShellExtInit))  /*IShellExtInit*/
91         {
92           FIXME("-- LPSHELLEXTINIT pointer requested\n");
93         }
94
95         if(*ppvObj)
96         {
97           IUnknown_AddRef((IUnknown*)*ppvObj);
98           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
99           return S_OK;
100         }
101         TRACE("-- Interface: E_NOINTERFACE\n");
102         return E_NOINTERFACE;
103 }
104
105 /**************************************************************************
106 *  ISVBgCm_fnAddRef
107 */
108 static ULONG WINAPI ISVBgCm_fnAddRef(IContextMenu2 *iface)
109 {
110         BgCmImpl *This = (BgCmImpl *)iface;
111         ULONG refCount = InterlockedIncrement(&This->ref);
112
113         TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
114
115         return refCount;
116 }
117
118 /**************************************************************************
119 *  ISVBgCm_fnRelease
120 */
121 static ULONG WINAPI ISVBgCm_fnRelease(IContextMenu2 *iface)
122 {
123         BgCmImpl *This = (BgCmImpl *)iface;
124         ULONG refCount = InterlockedDecrement(&This->ref);
125
126         TRACE("(%p)->(count=%li)\n", This, refCount + 1);
127
128         if (!refCount)
129         {
130           TRACE(" destroying IContextMenu(%p)\n",This);
131
132           if(This->pSFParent)
133             IShellFolder_Release(This->pSFParent);
134
135           HeapFree(GetProcessHeap(),0,This);
136         }
137         return refCount;
138 }
139
140 /**************************************************************************
141 * ISVBgCm_fnQueryContextMenu()
142 */
143
144 static HRESULT WINAPI ISVBgCm_fnQueryContextMenu(
145         IContextMenu2 *iface,
146         HMENU hMenu,
147         UINT indexMenu,
148         UINT idCmdFirst,
149         UINT idCmdLast,
150         UINT uFlags)
151 {
152     HMENU       hMyMenu;
153     UINT        idMax;
154     HRESULT hr;
155
156     BgCmImpl *This = (BgCmImpl *)iface;
157
158     TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",
159           This, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
160
161
162     hMyMenu = LoadMenuA(shell32_hInstance, "MENU_002");
163     if (uFlags & CMF_DEFAULTONLY)
164     {
165         HMENU ourMenu = GetSubMenu(hMyMenu,0);
166         UINT oldDef = GetMenuDefaultItem(hMenu,TRUE,GMDI_USEDISABLED);
167         UINT newDef = GetMenuDefaultItem(ourMenu,TRUE,GMDI_USEDISABLED);
168         if (newDef != oldDef)
169             SetMenuDefaultItem(hMenu,newDef,TRUE);
170         if (newDef!=0xFFFFFFFF)
171             hr =  MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, newDef+1);
172         else
173             hr =  MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
174     }
175     else
176     {
177         idMax = Shell_MergeMenus (hMenu, GetSubMenu(hMyMenu,0), indexMenu,
178                                   idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
179         hr =  MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, idMax-idCmdFirst+1);
180     }
181     DestroyMenu(hMyMenu);
182
183     TRACE("(%p)->returning 0x%lx\n",This,hr);
184     return hr;
185 }
186
187 /**************************************************************************
188 * DoNewFolder
189 */
190 static void DoNewFolder(
191         IContextMenu2 *iface,
192         IShellView *psv)
193 {
194         BgCmImpl *This = (BgCmImpl *)iface;
195         ISFHelper * psfhlp;
196         WCHAR wszName[MAX_PATH];
197
198         IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
199         if (psfhlp)
200         {
201           LPITEMIDLIST pidl;
202           ISFHelper_GetUniqueName(psfhlp, wszName, MAX_PATH);
203           ISFHelper_AddFolder(psfhlp, 0, wszName, &pidl);
204
205           if(psv)
206           {
207             /* if we are in a shellview do labeledit */
208             IShellView_SelectItem(psv,
209                     pidl,(SVSI_DESELECTOTHERS | SVSI_EDIT | SVSI_ENSUREVISIBLE
210                     |SVSI_FOCUSED|SVSI_SELECT));
211           }
212           SHFree(pidl);
213
214           ISFHelper_Release(psfhlp);
215         }
216 }
217
218 /**************************************************************************
219 * DoPaste
220 */
221 static BOOL DoPaste(
222         IContextMenu2 *iface)
223 {
224         BgCmImpl *This = (BgCmImpl *)iface;
225         BOOL bSuccess = FALSE;
226         IDataObject * pda;
227
228         TRACE("\n");
229
230         if(SUCCEEDED(OleGetClipboard(&pda)))
231         {
232           STGMEDIUM medium;
233           FORMATETC formatetc;
234
235           TRACE("pda=%p\n", pda);
236
237           /* Set the FORMATETC structure*/
238           InitFormatEtc(formatetc, RegisterClipboardFormatA(CFSTR_SHELLIDLIST), TYMED_HGLOBAL);
239
240           /* Get the pidls from IDataObject */
241           if(SUCCEEDED(IDataObject_GetData(pda,&formatetc,&medium)))
242           {
243             LPITEMIDLIST * apidl;
244             LPITEMIDLIST pidl;
245             IShellFolder *psfFrom = NULL, *psfDesktop;
246
247             LPIDA lpcida = GlobalLock(medium.u.hGlobal);
248             TRACE("cida=%p\n", lpcida);
249
250             apidl = _ILCopyCidaToaPidl(&pidl, lpcida);
251
252             /* bind to the source shellfolder */
253             SHGetDesktopFolder(&psfDesktop);
254             if(psfDesktop)
255             {
256               IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (LPVOID*)&psfFrom);
257               IShellFolder_Release(psfDesktop);
258             }
259
260             if (psfFrom)
261             {
262               /* get source and destination shellfolder */
263               ISFHelper *psfhlpdst, *psfhlpsrc;
264               IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlpdst);
265               IShellFolder_QueryInterface(psfFrom, &IID_ISFHelper, (LPVOID*)&psfhlpsrc);
266
267               /* do the copy/move */
268               if (psfhlpdst && psfhlpsrc)
269               {
270                 ISFHelper_CopyItems(psfhlpdst, psfFrom, lpcida->cidl, (LPCITEMIDLIST*)apidl);
271                 /* FIXME handle move
272                 ISFHelper_DeleteItems(psfhlpsrc, lpcida->cidl, apidl);
273                 */
274               }
275               if(psfhlpdst) ISFHelper_Release(psfhlpdst);
276               if(psfhlpsrc) ISFHelper_Release(psfhlpsrc);
277               IShellFolder_Release(psfFrom);
278             }
279
280             _ILFreeaPidl(apidl, lpcida->cidl);
281             SHFree(pidl);
282
283             /* release the medium*/
284             ReleaseStgMedium(&medium);
285           }
286           IDataObject_Release(pda);
287         }
288 #if 0
289         HGLOBAL  hMem;
290
291         OpenClipboard(NULL);
292         hMem = GetClipboardData(CF_HDROP);
293
294         if(hMem)
295         {
296           char * pDropFiles = (char *)GlobalLock(hMem);
297           if(pDropFiles)
298           {
299             int len, offset = sizeof(DROPFILESTRUCT);
300
301             while( pDropFiles[offset] != 0)
302             {
303               len = strlen(pDropFiles + offset);
304               TRACE("%s\n", pDropFiles + offset);
305               offset += len+1;
306             }
307           }
308           GlobalUnlock(hMem);
309         }
310         CloseClipboard();
311 #endif
312         return bSuccess;
313 }
314
315 /**************************************************************************
316 * ISVBgCm_fnInvokeCommand()
317 */
318 static HRESULT WINAPI ISVBgCm_fnInvokeCommand(
319         IContextMenu2 *iface,
320         LPCMINVOKECOMMANDINFO lpcmi)
321 {
322         BgCmImpl *This = (BgCmImpl *)iface;
323
324         LPSHELLBROWSER  lpSB;
325         LPSHELLVIEW lpSV = NULL;
326         HWND hWndSV = 0;
327
328         TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
329
330         /* get the active IShellView */
331         if((lpSB = (LPSHELLBROWSER)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER,0,0)))
332         {
333           if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
334           {
335             IShellView_GetWindow(lpSV, &hWndSV);
336           }
337         }
338
339           if(HIWORD(lpcmi->lpVerb))
340           {
341             TRACE("%s\n",lpcmi->lpVerb);
342
343             if (! strcmp(lpcmi->lpVerb,CMDSTR_NEWFOLDERA))
344             {
345                 DoNewFolder(iface, lpSV);
346             }
347             else if (! strcmp(lpcmi->lpVerb,CMDSTR_VIEWLISTA))
348             {
349               if(hWndSV) SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_LISTVIEW,0),0 );
350             }
351             else if (! strcmp(lpcmi->lpVerb,CMDSTR_VIEWDETAILSA))
352             {
353               if(hWndSV) SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_REPORTVIEW,0),0 );
354             }
355             else
356             {
357               FIXME("please report: unknown verb %s\n",lpcmi->lpVerb);
358             }
359           }
360           else
361           {
362             switch(LOWORD(lpcmi->lpVerb))
363             {
364               case FCIDM_SHVIEW_REFRESH:
365                 if (lpSV) IShellView_Refresh(lpSV);
366                 break;
367
368               case FCIDM_SHVIEW_NEWFOLDER:
369                 DoNewFolder(iface, lpSV);
370                 break;
371
372               case FCIDM_SHVIEW_INSERT:
373                 DoPaste(iface);
374                 break;
375
376               case FCIDM_SHVIEW_PROPERTIES:
377                 if (This->bDesktop) {
378                     ShellExecuteA(lpcmi->hwnd, "open", "rundll32.exe shell32.dll,Control_RunDLL desk.cpl", NULL, NULL, SW_SHOWNORMAL);
379                 } else {
380                     FIXME("launch item properties dialog\n");
381                 }
382                 break;
383
384               default:
385                 /* if it's an id just pass it to the parent shv */
386                 if (hWndSV) SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(LOWORD(lpcmi->lpVerb), 0),0 );
387                 break;
388             }
389           }
390
391         if (lpSV)
392           IShellView_Release(lpSV);     /* QueryActiveShellView does AddRef */
393
394         return NOERROR;
395 }
396
397 /**************************************************************************
398  *  ISVBgCm_fnGetCommandString()
399  *
400  */
401 static HRESULT WINAPI ISVBgCm_fnGetCommandString(
402         IContextMenu2 *iface,
403         UINT_PTR idCommand,
404         UINT uFlags,
405         UINT* lpReserved,
406         LPSTR lpszName,
407         UINT uMaxNameLen)
408 {
409         BgCmImpl *This = (BgCmImpl *)iface;
410
411         TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
412
413         /* test the existence of the menu items, the file dialog enables
414            the buttons according to this */
415         if (uFlags == GCS_VALIDATEA)
416         {
417           if(HIWORD(idCommand))
418           {
419             if (!strcmp((LPSTR)idCommand, CMDSTR_VIEWLISTA) ||
420                 !strcmp((LPSTR)idCommand, CMDSTR_VIEWDETAILSA) ||
421                 !strcmp((LPSTR)idCommand, CMDSTR_NEWFOLDERA))
422             {
423               return NOERROR;
424             }
425           }
426         }
427
428         FIXME("unknown command string\n");
429         return E_FAIL;
430 }
431
432 /**************************************************************************
433 * ISVBgCm_fnHandleMenuMsg()
434 */
435 static HRESULT WINAPI ISVBgCm_fnHandleMenuMsg(
436         IContextMenu2 *iface,
437         UINT uMsg,
438         WPARAM wParam,
439         LPARAM lParam)
440 {
441         BgCmImpl *This = (BgCmImpl *)iface;
442
443         FIXME("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
444
445         return E_NOTIMPL;
446 }
447
448 /**************************************************************************
449 * IContextMenu2 VTable
450 *
451 */
452 static const IContextMenu2Vtbl cmvt =
453 {
454         ISVBgCm_fnQueryInterface,
455         ISVBgCm_fnAddRef,
456         ISVBgCm_fnRelease,
457         ISVBgCm_fnQueryContextMenu,
458         ISVBgCm_fnInvokeCommand,
459         ISVBgCm_fnGetCommandString,
460         ISVBgCm_fnHandleMenuMsg
461 };