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