comctl32: Fix a logical operator typo.
[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     UINT                    verb_offset;
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=%u)\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=%i)\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     This->verb_offset=idCmdFirst;
162
163     hMyMenu = LoadMenuA(shell32_hInstance, "MENU_002");
164     if (uFlags & CMF_DEFAULTONLY)
165     {
166         HMENU ourMenu = GetSubMenu(hMyMenu,0);
167         UINT oldDef = GetMenuDefaultItem(hMenu,TRUE,GMDI_USEDISABLED);
168         UINT newDef = GetMenuDefaultItem(ourMenu,TRUE,GMDI_USEDISABLED);
169         if (newDef != oldDef)
170             SetMenuDefaultItem(hMenu,newDef,TRUE);
171         if (newDef!=0xFFFFFFFF)
172             hr =  MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, newDef+1);
173         else
174             hr =  MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
175     }
176     else
177     {
178         idMax = Shell_MergeMenus (hMenu, GetSubMenu(hMyMenu,0), indexMenu,
179                                   idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
180         hr =  MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, idMax-idCmdFirst);
181     }
182     DestroyMenu(hMyMenu);
183
184     TRACE("(%p)->returning 0x%x\n",This,hr);
185     return hr;
186 }
187
188 /**************************************************************************
189 * DoNewFolder
190 */
191 static void DoNewFolder(
192         IContextMenu2 *iface,
193         IShellView *psv)
194 {
195         BgCmImpl *This = (BgCmImpl *)iface;
196         ISFHelper * psfhlp;
197         WCHAR wszName[MAX_PATH];
198
199         IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
200         if (psfhlp)
201         {
202           LPITEMIDLIST pidl;
203           ISFHelper_GetUniqueName(psfhlp, wszName, MAX_PATH);
204           ISFHelper_AddFolder(psfhlp, 0, wszName, &pidl);
205
206           if(psv)
207           {
208             /* if we are in a shellview do labeledit */
209             IShellView_SelectItem(psv,
210                     pidl,(SVSI_DESELECTOTHERS | SVSI_EDIT | SVSI_ENSUREVISIBLE
211                     |SVSI_FOCUSED|SVSI_SELECT));
212           }
213           SHFree(pidl);
214
215           ISFHelper_Release(psfhlp);
216         }
217 }
218
219 /**************************************************************************
220 * DoPaste
221 */
222 static BOOL DoPaste(
223         IContextMenu2 *iface)
224 {
225         BgCmImpl *This = (BgCmImpl *)iface;
226         BOOL bSuccess = FALSE;
227         IDataObject * pda;
228
229         TRACE("\n");
230
231         if(SUCCEEDED(OleGetClipboard(&pda)))
232         {
233           STGMEDIUM medium;
234           FORMATETC formatetc;
235
236           TRACE("pda=%p\n", pda);
237
238           /* Set the FORMATETC structure*/
239           InitFormatEtc(formatetc, RegisterClipboardFormatW(CFSTR_SHELLIDLISTW), TYMED_HGLOBAL);
240
241           /* Get the pidls from IDataObject */
242           if(SUCCEEDED(IDataObject_GetData(pda,&formatetc,&medium)))
243           {
244             LPITEMIDLIST * apidl;
245             LPITEMIDLIST pidl;
246             IShellFolder *psfFrom = NULL, *psfDesktop;
247
248             LPIDA lpcida = GlobalLock(medium.u.hGlobal);
249             TRACE("cida=%p\n", lpcida);
250
251             apidl = _ILCopyCidaToaPidl(&pidl, lpcida);
252
253             /* bind to the source shellfolder */
254             SHGetDesktopFolder(&psfDesktop);
255             if(psfDesktop)
256             {
257               IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (LPVOID*)&psfFrom);
258               IShellFolder_Release(psfDesktop);
259             }
260
261             if (psfFrom)
262             {
263               /* get source and destination shellfolder */
264               ISFHelper *psfhlpdst, *psfhlpsrc;
265               IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlpdst);
266               IShellFolder_QueryInterface(psfFrom, &IID_ISFHelper, (LPVOID*)&psfhlpsrc);
267
268               /* do the copy/move */
269               if (psfhlpdst && psfhlpsrc)
270               {
271                 ISFHelper_CopyItems(psfhlpdst, psfFrom, lpcida->cidl, (LPCITEMIDLIST*)apidl);
272                 /* FIXME handle move
273                 ISFHelper_DeleteItems(psfhlpsrc, lpcida->cidl, apidl);
274                 */
275               }
276               if(psfhlpdst) ISFHelper_Release(psfhlpdst);
277               if(psfhlpsrc) ISFHelper_Release(psfhlpsrc);
278               IShellFolder_Release(psfFrom);
279             }
280
281             _ILFreeaPidl(apidl, lpcida->cidl);
282             SHFree(pidl);
283
284             /* release the medium*/
285             ReleaseStgMedium(&medium);
286           }
287           IDataObject_Release(pda);
288         }
289 #if 0
290         HGLOBAL  hMem;
291
292         OpenClipboard(NULL);
293         hMem = GetClipboardData(CF_HDROP);
294
295         if(hMem)
296         {
297           char * pDropFiles = GlobalLock(hMem);
298           if(pDropFiles)
299           {
300             int len, offset = sizeof(DROPFILESTRUCT);
301
302             while( pDropFiles[offset] != 0)
303             {
304               len = strlen(pDropFiles + offset);
305               TRACE("%s\n", pDropFiles + offset);
306               offset += len+1;
307             }
308           }
309           GlobalUnlock(hMem);
310         }
311         CloseClipboard();
312 #endif
313         return bSuccess;
314 }
315
316 /**************************************************************************
317 * ISVBgCm_fnInvokeCommand()
318 */
319 static HRESULT WINAPI ISVBgCm_fnInvokeCommand(
320         IContextMenu2 *iface,
321         LPCMINVOKECOMMANDINFO lpcmi)
322 {
323         BgCmImpl *This = (BgCmImpl *)iface;
324
325         LPSHELLBROWSER  lpSB;
326         LPSHELLVIEW lpSV = NULL;
327         HWND hWndSV = 0;
328
329         TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
330
331         /* get the active IShellView */
332         if((lpSB = (LPSHELLBROWSER)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER,0,0)))
333         {
334           if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
335           {
336             IShellView_GetWindow(lpSV, &hWndSV);
337           }
338         }
339
340           if(HIWORD(lpcmi->lpVerb))
341           {
342             TRACE("%s\n",lpcmi->lpVerb);
343
344             if (! strcmp(lpcmi->lpVerb,CMDSTR_NEWFOLDERA))
345             {
346                 DoNewFolder(iface, lpSV);
347             }
348             else if (! strcmp(lpcmi->lpVerb,CMDSTR_VIEWLISTA))
349             {
350               if(hWndSV) SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_LISTVIEW,0),0 );
351             }
352             else if (! strcmp(lpcmi->lpVerb,CMDSTR_VIEWDETAILSA))
353             {
354               if(hWndSV) SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_REPORTVIEW,0),0 );
355             }
356             else
357             {
358               FIXME("please report: unknown verb %s\n",lpcmi->lpVerb);
359             }
360           }
361           else
362           {
363             switch(LOWORD(lpcmi->lpVerb)-This->verb_offset)
364             {
365               case FCIDM_SHVIEW_REFRESH:
366                 if (lpSV) IShellView_Refresh(lpSV);
367                 break;
368
369               case FCIDM_SHVIEW_NEWFOLDER:
370                 DoNewFolder(iface, lpSV);
371                 break;
372
373               case FCIDM_SHVIEW_INSERT:
374                 DoPaste(iface);
375                 break;
376
377               case FCIDM_SHVIEW_PROPERTIES:
378                 if (This->bDesktop) {
379                     ShellExecuteA(lpcmi->hwnd, "open", "rundll32.exe shell32.dll,Control_RunDLL desk.cpl", NULL, NULL, SW_SHOWNORMAL);
380                 } else {
381                     FIXME("launch item properties dialog\n");
382                 }
383                 break;
384
385               default:
386                 /* if it's an id just pass it to the parent shv */
387                 if (hWndSV) SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(LOWORD(lpcmi->lpVerb), 0),0 );
388                 break;
389             }
390           }
391
392         if (lpSV)
393           IShellView_Release(lpSV);     /* QueryActiveShellView does AddRef */
394
395         return NOERROR;
396 }
397
398 /**************************************************************************
399  *  ISVBgCm_fnGetCommandString()
400  *
401  */
402 static HRESULT WINAPI ISVBgCm_fnGetCommandString(
403         IContextMenu2 *iface,
404         UINT_PTR idCommand,
405         UINT uFlags,
406         UINT* lpReserved,
407         LPSTR lpszName,
408         UINT uMaxNameLen)
409 {
410         BgCmImpl *This = (BgCmImpl *)iface;
411
412         TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
413
414         /* test the existence of the menu items, the file dialog enables
415            the buttons according to this */
416         if (uFlags == GCS_VALIDATEA)
417         {
418           if(HIWORD(idCommand))
419           {
420             if (!strcmp((LPSTR)idCommand, CMDSTR_VIEWLISTA) ||
421                 !strcmp((LPSTR)idCommand, CMDSTR_VIEWDETAILSA) ||
422                 !strcmp((LPSTR)idCommand, CMDSTR_NEWFOLDERA))
423             {
424               return NOERROR;
425             }
426           }
427         }
428
429         FIXME("unknown command string\n");
430         return E_FAIL;
431 }
432
433 /**************************************************************************
434 * ISVBgCm_fnHandleMenuMsg()
435 */
436 static HRESULT WINAPI ISVBgCm_fnHandleMenuMsg(
437         IContextMenu2 *iface,
438         UINT uMsg,
439         WPARAM wParam,
440         LPARAM lParam)
441 {
442         BgCmImpl *This = (BgCmImpl *)iface;
443
444         FIXME("(%p)->(msg=%x wp=%lx lp=%lx)\n",This, uMsg, wParam, lParam);
445
446         return E_NOTIMPL;
447 }
448
449 /**************************************************************************
450 * IContextMenu2 VTable
451 *
452 */
453 static const IContextMenu2Vtbl cmvt =
454 {
455         ISVBgCm_fnQueryInterface,
456         ISVBgCm_fnAddRef,
457         ISVBgCm_fnRelease,
458         ISVBgCm_fnQueryContextMenu,
459         ISVBgCm_fnInvokeCommand,
460         ISVBgCm_fnGetCommandString,
461         ISVBgCm_fnHandleMenuMsg
462 };