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