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