dmscript: Merge the IClassFactory implementations.
[wine/testsucceed.git] / dlls / shell32 / shv_bg_cmenu.c
blob7f59b6dd3dba4ca9fe99a64c52fc99557bc9ef76
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <string.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
27 #include "wine/debug.h"
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "pidl.h"
32 #include "shlobj.h"
34 #include "shell32_main.h"
35 #include "shellfolder.h"
36 #include "undocshell.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(shell);
40 /**************************************************************************
41 * IContextMenu Implementation
43 typedef struct
45 const IContextMenu2Vtbl *lpVtbl;
46 IShellFolder* pSFParent;
47 LONG ref;
48 BOOL bDesktop;
49 } BgCmImpl;
52 static const IContextMenu2Vtbl cmvt;
54 /**************************************************************************
55 * ISVBgCm_Constructor()
57 IContextMenu2 *ISvBgCm_Constructor(IShellFolder* pSFParent, BOOL bDesktop)
59 BgCmImpl* cm;
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);
68 TRACE("(%p)->()\n",cm);
69 return (IContextMenu2*)cm;
72 /**************************************************************************
73 * ISVBgCm_fnQueryInterface
75 static HRESULT WINAPI ISVBgCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
77 BgCmImpl *This = (BgCmImpl *)iface;
79 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
81 *ppvObj = NULL;
83 if(IsEqualIID(riid, &IID_IUnknown) ||
84 IsEqualIID(riid, &IID_IContextMenu) ||
85 IsEqualIID(riid, &IID_IContextMenu2))
87 *ppvObj = This;
89 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
91 FIXME("-- LPSHELLEXTINIT pointer requested\n");
94 if(*ppvObj)
96 IUnknown_AddRef((IUnknown*)*ppvObj);
97 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
98 return S_OK;
100 TRACE("-- Interface: E_NOINTERFACE\n");
101 return E_NOINTERFACE;
104 /**************************************************************************
105 * ISVBgCm_fnAddRef
107 static ULONG WINAPI ISVBgCm_fnAddRef(IContextMenu2 *iface)
109 BgCmImpl *This = (BgCmImpl *)iface;
110 ULONG refCount = InterlockedIncrement(&This->ref);
112 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
114 return refCount;
117 /**************************************************************************
118 * ISVBgCm_fnRelease
120 static ULONG WINAPI ISVBgCm_fnRelease(IContextMenu2 *iface)
122 BgCmImpl *This = (BgCmImpl *)iface;
123 ULONG refCount = InterlockedDecrement(&This->ref);
125 TRACE("(%p)->(count=%i)\n", This, refCount + 1);
127 if (!refCount)
129 TRACE(" destroying IContextMenu(%p)\n",This);
131 if(This->pSFParent)
132 IShellFolder_Release(This->pSFParent);
134 HeapFree(GetProcessHeap(),0,This);
136 return refCount;
139 /**************************************************************************
140 * ISVBgCm_fnQueryContextMenu()
143 static HRESULT WINAPI ISVBgCm_fnQueryContextMenu(
144 IContextMenu2 *iface,
145 HMENU hMenu,
146 UINT indexMenu,
147 UINT idCmdFirst,
148 UINT idCmdLast,
149 UINT uFlags)
151 HMENU hMyMenu;
152 UINT idMax;
153 HRESULT hr;
155 BgCmImpl *This = (BgCmImpl *)iface;
157 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",
158 This, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
161 hMyMenu = LoadMenuA(shell32_hInstance, "MENU_002");
162 if (uFlags & CMF_DEFAULTONLY)
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);
174 else
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);
180 DestroyMenu(hMyMenu);
182 TRACE("(%p)->returning 0x%x\n",This,hr);
183 return hr;
186 /**************************************************************************
187 * DoNewFolder
189 static void DoNewFolder(
190 IContextMenu2 *iface,
191 IShellView *psv)
193 BgCmImpl *This = (BgCmImpl *)iface;
194 ISFHelper * psfhlp;
195 WCHAR wszName[MAX_PATH];
197 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
198 if (psfhlp)
200 LPITEMIDLIST pidl;
201 ISFHelper_GetUniqueName(psfhlp, wszName, MAX_PATH);
202 ISFHelper_AddFolder(psfhlp, 0, wszName, &pidl);
204 if(psv)
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));
211 SHFree(pidl);
213 ISFHelper_Release(psfhlp);
217 /**************************************************************************
218 * DoPaste
220 static BOOL DoPaste(
221 IContextMenu2 *iface)
223 BgCmImpl *This = (BgCmImpl *)iface;
224 BOOL bSuccess = FALSE;
225 IDataObject * pda;
227 TRACE("\n");
229 if(SUCCEEDED(OleGetClipboard(&pda)))
231 STGMEDIUM medium;
232 FORMATETC formatetc;
234 TRACE("pda=%p\n", pda);
236 /* Set the FORMATETC structure*/
237 InitFormatEtc(formatetc, RegisterClipboardFormatW(CFSTR_SHELLIDLISTW), TYMED_HGLOBAL);
239 /* Get the pidls from IDataObject */
240 if(SUCCEEDED(IDataObject_GetData(pda,&formatetc,&medium)))
242 LPITEMIDLIST * apidl;
243 LPITEMIDLIST pidl;
244 IShellFolder *psfFrom = NULL, *psfDesktop;
246 LPIDA lpcida = GlobalLock(medium.u.hGlobal);
247 TRACE("cida=%p\n", lpcida);
249 apidl = _ILCopyCidaToaPidl(&pidl, lpcida);
251 /* bind to the source shellfolder */
252 SHGetDesktopFolder(&psfDesktop);
253 if(psfDesktop)
255 IShellFolder_BindToObject(psfDesktop, pidl, NULL, &IID_IShellFolder, (LPVOID*)&psfFrom);
256 IShellFolder_Release(psfDesktop);
259 if (psfFrom)
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);
266 /* do the copy/move */
267 if (psfhlpdst && psfhlpsrc)
269 ISFHelper_CopyItems(psfhlpdst, psfFrom, lpcida->cidl, (LPCITEMIDLIST*)apidl);
270 /* FIXME handle move
271 ISFHelper_DeleteItems(psfhlpsrc, lpcida->cidl, apidl);
274 if(psfhlpdst) ISFHelper_Release(psfhlpdst);
275 if(psfhlpsrc) ISFHelper_Release(psfhlpsrc);
276 IShellFolder_Release(psfFrom);
279 _ILFreeaPidl(apidl, lpcida->cidl);
280 SHFree(pidl);
282 /* release the medium*/
283 ReleaseStgMedium(&medium);
285 IDataObject_Release(pda);
287 #if 0
288 HGLOBAL hMem;
290 OpenClipboard(NULL);
291 hMem = GetClipboardData(CF_HDROP);
293 if(hMem)
295 char * pDropFiles = GlobalLock(hMem);
296 if(pDropFiles)
298 int len, offset = sizeof(DROPFILESTRUCT);
300 while( pDropFiles[offset] != 0)
302 len = strlen(pDropFiles + offset);
303 TRACE("%s\n", pDropFiles + offset);
304 offset += len+1;
307 GlobalUnlock(hMem);
309 CloseClipboard();
310 #endif
311 return bSuccess;
314 /**************************************************************************
315 * ISVBgCm_fnInvokeCommand()
317 static HRESULT WINAPI ISVBgCm_fnInvokeCommand(
318 IContextMenu2 *iface,
319 LPCMINVOKECOMMANDINFO lpcmi)
321 BgCmImpl *This = (BgCmImpl *)iface;
323 LPSHELLBROWSER lpSB;
324 LPSHELLVIEW lpSV = NULL;
325 HWND hWndSV = 0;
327 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
329 /* get the active IShellView */
330 if((lpSB = (LPSHELLBROWSER)SendMessageA(lpcmi->hwnd, CWM_GETISHELLBROWSER,0,0)))
332 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
334 IShellView_GetWindow(lpSV, &hWndSV);
338 if(HIWORD(lpcmi->lpVerb))
340 TRACE("%s\n",lpcmi->lpVerb);
342 if (! strcmp(lpcmi->lpVerb,CMDSTR_NEWFOLDERA))
344 DoNewFolder(iface, lpSV);
346 else if (! strcmp(lpcmi->lpVerb,CMDSTR_VIEWLISTA))
348 if(hWndSV) SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_LISTVIEW,0),0 );
350 else if (! strcmp(lpcmi->lpVerb,CMDSTR_VIEWDETAILSA))
352 if(hWndSV) SendMessageA(hWndSV, WM_COMMAND, MAKEWPARAM(FCIDM_SHVIEW_REPORTVIEW,0),0 );
354 else
356 FIXME("please report: unknown verb %s\n",lpcmi->lpVerb);
359 else
361 switch(LOWORD(lpcmi->lpVerb))
363 case FCIDM_SHVIEW_REFRESH:
364 if (lpSV) IShellView_Refresh(lpSV);
365 break;
367 case FCIDM_SHVIEW_NEWFOLDER:
368 DoNewFolder(iface, lpSV);
369 break;
371 case FCIDM_SHVIEW_INSERT:
372 DoPaste(iface);
373 break;
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");
381 break;
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;
390 if (lpSV)
391 IShellView_Release(lpSV); /* QueryActiveShellView does AddRef */
393 return NOERROR;
396 /**************************************************************************
397 * ISVBgCm_fnGetCommandString()
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)
408 BgCmImpl *This = (BgCmImpl *)iface;
410 TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
412 /* test the existence of the menu items, the file dialog enables
413 the buttons according to this */
414 if (uFlags == GCS_VALIDATEA)
416 if(HIWORD(idCommand))
418 if (!strcmp((LPSTR)idCommand, CMDSTR_VIEWLISTA) ||
419 !strcmp((LPSTR)idCommand, CMDSTR_VIEWDETAILSA) ||
420 !strcmp((LPSTR)idCommand, CMDSTR_NEWFOLDERA))
422 return NOERROR;
427 FIXME("unknown command string\n");
428 return E_FAIL;
431 /**************************************************************************
432 * ISVBgCm_fnHandleMenuMsg()
434 static HRESULT WINAPI ISVBgCm_fnHandleMenuMsg(
435 IContextMenu2 *iface,
436 UINT uMsg,
437 WPARAM wParam,
438 LPARAM lParam)
440 BgCmImpl *This = (BgCmImpl *)iface;
442 FIXME("(%p)->(msg=%x wp=%lx lp=%lx)\n",This, uMsg, wParam, lParam);
444 return E_NOTIMPL;
447 /**************************************************************************
448 * IContextMenu2 VTable
451 static const IContextMenu2Vtbl cmvt =
453 ISVBgCm_fnQueryInterface,
454 ISVBgCm_fnAddRef,
455 ISVBgCm_fnRelease,
456 ISVBgCm_fnQueryContextMenu,
457 ISVBgCm_fnInvokeCommand,
458 ISVBgCm_fnGetCommandString,
459 ISVBgCm_fnHandleMenuMsg