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