Release 20030408.
[wine/gsoc-2012-control.git] / dlls / shell32 / shv_bg_cmenu.c
blob2c590d619abee7d931a08691ce43df982a8bc343
1 /*
2 * IContextMenu
3 * ShellView Background Context Menu (shv_bg_cm)
5 * Copyright 1999 Juergen Schmied <juergen.schmied@metronet.de>
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <string.h>
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
25 #include "wine/debug.h"
27 #include "pidl.h"
28 #include "shlguid.h"
29 #include "shlobj.h"
31 #include "shell32_main.h"
32 #include "shellfolder.h"
33 #include "undocshell.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(shell);
37 /**************************************************************************
38 * IContextMenu Implementation
40 typedef struct
42 ICOM_VFIELD(IContextMenu);
43 IShellFolder* pSFParent;
44 DWORD ref;
45 } BgCmImpl;
48 static struct ICOM_VTABLE(IContextMenu) cmvt;
50 /**************************************************************************
51 * ISVBgCm_Constructor()
53 IContextMenu *ISvBgCm_Constructor(IShellFolder* pSFParent)
55 BgCmImpl* cm;
57 cm = (BgCmImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(BgCmImpl));
58 ICOM_VTBL(cm)=&cmvt;
59 cm->ref = 1;
60 cm->pSFParent = pSFParent;
61 if(pSFParent) IShellFolder_AddRef(pSFParent);
63 TRACE("(%p)->()\n",cm);
64 return (IContextMenu*)cm;
67 /**************************************************************************
68 * ISVBgCm_fnQueryInterface
70 static HRESULT WINAPI ISVBgCm_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj)
72 ICOM_THIS(BgCmImpl, iface);
74 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
76 *ppvObj = NULL;
78 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
80 *ppvObj = This;
82 else if(IsEqualIID(riid, &IID_IContextMenu)) /*IContextMenu*/
84 *ppvObj = This;
86 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
88 FIXME("-- LPSHELLEXTINIT pointer requested\n");
91 if(*ppvObj)
93 IUnknown_AddRef((IUnknown*)*ppvObj);
94 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
95 return S_OK;
97 TRACE("-- Interface: E_NOINTERFACE\n");
98 return E_NOINTERFACE;
101 /**************************************************************************
102 * ISVBgCm_fnAddRef
104 static ULONG WINAPI ISVBgCm_fnAddRef(IContextMenu *iface)
106 ICOM_THIS(BgCmImpl, iface);
108 TRACE("(%p)->(count=%lu)\n",This, This->ref);
110 return ++(This->ref);
113 /**************************************************************************
114 * ISVBgCm_fnRelease
116 static ULONG WINAPI ISVBgCm_fnRelease(IContextMenu *iface)
118 ICOM_THIS(BgCmImpl, iface);
120 TRACE("(%p)->()\n",This);
122 if (!--(This->ref))
124 TRACE(" destroying IContextMenu(%p)\n",This);
126 if(This->pSFParent)
127 IShellFolder_Release(This->pSFParent);
129 HeapFree(GetProcessHeap(),0,This);
130 return 0;
133 return This->ref;
136 /**************************************************************************
137 * ISVBgCm_fnQueryContextMenu()
140 static HRESULT WINAPI ISVBgCm_fnQueryContextMenu(
141 IContextMenu *iface,
142 HMENU hMenu,
143 UINT indexMenu,
144 UINT idCmdFirst,
145 UINT idCmdLast,
146 UINT uFlags)
148 HMENU hMyMenu;
149 UINT idMax;
150 HRESULT hr;
152 ICOM_THIS(BgCmImpl, iface);
154 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",
155 This, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
158 hMyMenu = LoadMenuA(shell32_hInstance, "MENU_002");
159 if (uFlags & CMF_DEFAULTONLY)
161 HMENU ourMenu = GetSubMenu(hMyMenu,0);
162 UINT oldDef = GetMenuDefaultItem(hMenu,TRUE,GMDI_USEDISABLED);
163 UINT newDef = GetMenuDefaultItem(ourMenu,TRUE,GMDI_USEDISABLED);
164 if (newDef != oldDef)
165 SetMenuDefaultItem(hMenu,newDef,TRUE);
166 if (newDef!=0xFFFFFFFF)
167 hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, newDef+1);
168 else
169 hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
171 else
173 idMax = Shell_MergeMenus (hMenu, GetSubMenu(hMyMenu,0), indexMenu,
174 idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
175 hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, idMax-idCmdFirst+1);
177 DestroyMenu(hMyMenu);
179 TRACE("(%p)->returning 0x%lx\n",This,hr);
180 return hr;
183 /**************************************************************************
184 * DoNewFolder
186 static void DoNewFolder(
187 IContextMenu *iface,
188 IShellView *psv)
190 ICOM_THIS(BgCmImpl, iface);
191 ISFHelper * psfhlp;
192 char szName[MAX_PATH];
194 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
195 if (psfhlp)
197 LPITEMIDLIST pidl;
198 ISFHelper_GetUniqueName(psfhlp, szName, MAX_PATH);
199 ISFHelper_AddFolder(psfhlp, 0, szName, &pidl);
201 if(psv)
203 /* if we are in a shellview do labeledit */
204 IShellView_SelectItem(psv,
205 pidl,(SVSI_DESELECTOTHERS | SVSI_EDIT | SVSI_ENSUREVISIBLE
206 |SVSI_FOCUSED|SVSI_SELECT));
208 SHFree(pidl);
210 ISFHelper_Release(psfhlp);
214 /**************************************************************************
215 * DoPaste
217 static BOOL DoPaste(
218 IContextMenu *iface)
220 ICOM_THIS(BgCmImpl, iface);
221 BOOL bSuccess = FALSE;
222 IDataObject * pda;
224 TRACE("\n");
226 if(SUCCEEDED(pOleGetClipboard(&pda)))
228 STGMEDIUM medium;
229 FORMATETC formatetc;
231 TRACE("pda=%p\n", pda);
233 /* Set the FORMATETC structure*/
234 InitFormatEtc(formatetc, RegisterClipboardFormatA(CFSTR_SHELLIDLIST), TYMED_HGLOBAL);
236 /* Get the pidls from IDataObject */
237 if(SUCCEEDED(IDataObject_GetData(pda,&formatetc,&medium)))
239 LPITEMIDLIST * apidl;
240 LPITEMIDLIST pidl;
241 IShellFolder *psfFrom = NULL, *psfDesktop;
243 LPIDA lpcida = GlobalLock(medium.u.hGlobal);
244 TRACE("cida=%p\n", lpcida);
246 apidl = _ILCopyCidaToaPidl(&pidl, lpcida);
248 /* bind to the source shellfolder */
249 SHGetDesktopFolder(&psfDesktop);
250 if(psfDesktop)
252 IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (LPVOID*)&psfFrom);
253 IShellFolder_Release(psfDesktop);
256 if (psfFrom)
258 /* get source and destination shellfolder */
259 ISFHelper *psfhlpdst, *psfhlpsrc;
260 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlpdst);
261 IShellFolder_QueryInterface(psfFrom, &IID_ISFHelper, (LPVOID*)&psfhlpsrc);
263 /* do the copy/move */
264 if (psfhlpdst && psfhlpsrc)
266 ISFHelper_CopyItems(psfhlpdst, psfFrom, lpcida->cidl, apidl);
267 /* FIXME handle move
268 ISFHelper_DeleteItems(psfhlpsrc, lpcida->cidl, apidl);
271 if(psfhlpdst) ISFHelper_Release(psfhlpdst);
272 if(psfhlpsrc) ISFHelper_Release(psfhlpsrc);
273 IShellFolder_Release(psfFrom);
276 _ILFreeaPidl(apidl, lpcida->cidl);
277 SHFree(pidl);
279 /* release the medium*/
280 pReleaseStgMedium(&medium);
282 IDataObject_Release(pda);
284 #if 0
285 HGLOBAL hMem;
287 OpenClipboard(NULL);
288 hMem = GetClipboardData(CF_HDROP);
290 if(hMem)
292 char * pDropFiles = (char *)GlobalLock(hMem);
293 if(pDropFiles)
295 int len, offset = sizeof(DROPFILESTRUCT);
297 while( pDropFiles[offset] != 0)
299 len = strlen(pDropFiles + offset);
300 TRACE("%s\n", pDropFiles + offset);
301 offset += len+1;
304 GlobalUnlock(hMem);
306 CloseClipboard();
307 #endif
308 return bSuccess;
310 /**************************************************************************
311 * ISVBgCm_fnInvokeCommand()
313 static HRESULT WINAPI ISVBgCm_fnInvokeCommand(
314 IContextMenu *iface,
315 LPCMINVOKECOMMANDINFO lpcmi)
317 ICOM_THIS(BgCmImpl, iface);
319 LPSHELLBROWSER lpSB;
320 LPSHELLVIEW lpSV;
321 HWND hWndSV;
323 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
325 /* get the active IShellView */
326 if((lpSB = (LPSHELLBROWSER)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER,0,0)))
328 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
330 IShellView_GetWindow(lpSV, &hWndSV);
334 if(lpSV)
336 if(HIWORD(lpcmi->lpVerb))
338 TRACE("%s\n",lpcmi->lpVerb);
340 if (! strcmp(lpcmi->lpVerb,CMDSTR_NEWFOLDERA))
342 if(lpSV) DoNewFolder(iface, lpSV);
344 else if (! strcmp(lpcmi->lpVerb,CMDSTR_VIEWLISTA))
346 if(hWndSV) SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_LISTVIEW,0),0 );
348 else if (! strcmp(lpcmi->lpVerb,CMDSTR_VIEWDETAILSA))
350 if(hWndSV) SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_REPORTVIEW,0),0 );
352 else
354 FIXME("please report: unknown verb %s\n",lpcmi->lpVerb);
357 else
359 switch(LOWORD(lpcmi->lpVerb))
361 case FCIDM_SHVIEW_NEWFOLDER:
362 DoNewFolder(iface, lpSV);
363 break;
364 case FCIDM_SHVIEW_INSERT:
365 DoPaste(iface);
366 break;
367 default:
368 /* if it's a id just pass it to the parent shv */
369 SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(LOWORD(lpcmi->lpVerb), 0),0 );
370 break;
374 IShellView_Release(lpSV); /* QueryActiveShellView does AddRef*/
376 return NOERROR;
379 /**************************************************************************
380 * ISVBgCm_fnGetCommandString()
383 static HRESULT WINAPI ISVBgCm_fnGetCommandString(
384 IContextMenu *iface,
385 UINT idCommand,
386 UINT uFlags,
387 UINT* lpReserved,
388 LPSTR lpszName,
389 UINT uMaxNameLen)
391 ICOM_THIS(BgCmImpl, iface);
393 TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
395 /* test the existence of the menu items, the file dialog enables
396 the buttons according to this */
397 if (uFlags == GCS_VALIDATEA)
399 if(HIWORD(idCommand))
401 if (!strcmp((LPSTR)idCommand, CMDSTR_VIEWLISTA) ||
402 !strcmp((LPSTR)idCommand, CMDSTR_VIEWDETAILSA) ||
403 !strcmp((LPSTR)idCommand, CMDSTR_NEWFOLDERA))
405 return NOERROR;
410 FIXME("unknown command string\n");
411 return E_FAIL;
414 /**************************************************************************
415 * ISVBgCm_fnHandleMenuMsg()
417 static HRESULT WINAPI ISVBgCm_fnHandleMenuMsg(
418 IContextMenu *iface,
419 UINT uMsg,
420 WPARAM wParam,
421 LPARAM lParam)
423 ICOM_THIS(BgCmImpl, iface);
425 FIXME("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
427 return E_NOTIMPL;
430 /**************************************************************************
431 * IContextMenu VTable
434 static struct ICOM_VTABLE(IContextMenu) cmvt =
436 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
437 ISVBgCm_fnQueryInterface,
438 ISVBgCm_fnAddRef,
439 ISVBgCm_fnRelease,
440 ISVBgCm_fnQueryContextMenu,
441 ISVBgCm_fnInvokeCommand,
442 ISVBgCm_fnGetCommandString,
443 ISVBgCm_fnHandleMenuMsg,
444 (void *) 0xdeadbabe /* just paranoia (IContextMenu3) */