Refactoring of Wine DLL import
[reactos.git] / reactos / lib / shell32 / shv_item_cmenu.c
1 /*
2 * IContextMenu for items in the shellview
3 *
4 * Copyright 1998, 2000 Juergen Schmied <juergen.schmied@debitel.net>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <string.h>
22
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
25 #include "winerror.h"
26 #include "wine/debug.h"
27
28 #include "windef.h"
29 #include "wingdi.h"
30 #include "pidl.h"
31 #include "shlguid.h"
32 #include "undocshell.h"
33 #include "shlobj.h"
34
35 #include "shell32_main.h"
36 #include "shellfolder.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(shell);
39
40 /**************************************************************************
41 * IContextMenu Implementation
42 */
43 typedef struct
44 { ICOM_VFIELD(IContextMenu2);
45 DWORD ref;
46 IShellFolder* pSFParent;
47 LPITEMIDLIST pidl; /* root pidl */
48 LPITEMIDLIST *apidl; /* array of child pidls */
49 UINT cidl;
50 BOOL bAllValues;
51 } ItemCmImpl;
52
53
54 static struct ICOM_VTABLE(IContextMenu2) cmvt;
55
56 /**************************************************************************
57 * ISvItemCm_CanRenameItems()
58 */
59 static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
60 { UINT i;
61 DWORD dwAttributes;
62
63 TRACE("(%p)->()\n",This);
64
65 if(This->apidl)
66 {
67 for(i = 0; i < This->cidl; i++){}
68 if(i > 1) return FALSE; /* can't rename more than one item at a time*/
69 dwAttributes = SFGAO_CANRENAME;
70 IShellFolder_GetAttributesOf(This->pSFParent, 1, (LPCITEMIDLIST*)This->apidl, &dwAttributes);
71 return dwAttributes & SFGAO_CANRENAME;
72 }
73 return FALSE;
74 }
75
76 /**************************************************************************
77 * ISvItemCm_Constructor()
78 */
79 IContextMenu2 *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *apidl, UINT cidl)
80 { ItemCmImpl* cm;
81 UINT u;
82
83 cm = (ItemCmImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
84 cm->lpVtbl = &cmvt;
85 cm->ref = 1;
86 cm->pidl = ILClone(pidl);
87 cm->pSFParent = pSFParent;
88
89 if(pSFParent) IShellFolder_AddRef(pSFParent);
90
91 cm->apidl = _ILCopyaPidl(apidl, cidl);
92 cm->cidl = cidl;
93
94 cm->bAllValues = 1;
95 for(u = 0; u < cidl; u++)
96 {
97 cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
98 }
99
100 TRACE("(%p)->()\n",cm);
101
102 return (IContextMenu2*)cm;
103 }
104
105 /**************************************************************************
106 * ISvItemCm_fnQueryInterface
107 */
108 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
109 {
110 ICOM_THIS(ItemCmImpl, iface);
111
112 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
113
114 *ppvObj = NULL;
115
116 if(IsEqualIID(riid, &IID_IUnknown) ||
117 IsEqualIID(riid, &IID_IContextMenu) ||
118 IsEqualIID(riid, &IID_IContextMenu2))
119 {
120 *ppvObj = This;
121 }
122 else if(IsEqualIID(riid, &IID_IShellExtInit)) /*IShellExtInit*/
123 {
124 FIXME("-- LPSHELLEXTINIT pointer requested\n");
125 }
126
127 if(*ppvObj)
128 {
129 IUnknown_AddRef((IUnknown*)*ppvObj);
130 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
131 return S_OK;
132 }
133 TRACE("-- Interface: E_NOINTERFACE\n");
134 return E_NOINTERFACE;
135 }
136
137 /**************************************************************************
138 * ISvItemCm_fnAddRef
139 */
140 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu2 *iface)
141 {
142 ICOM_THIS(ItemCmImpl, iface);
143
144 TRACE("(%p)->(count=%lu)\n",This, This->ref);
145
146 return ++(This->ref);
147 }
148
149 /**************************************************************************
150 * ISvItemCm_fnRelease
151 */
152 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu2 *iface)
153 {
154 ICOM_THIS(ItemCmImpl, iface);
155
156 TRACE("(%p)->()\n",This);
157
158 if (!--(This->ref))
159 {
160 TRACE(" destroying IContextMenu(%p)\n",This);
161
162 if(This->pSFParent)
163 IShellFolder_Release(This->pSFParent);
164
165 if(This->pidl)
166 SHFree(This->pidl);
167
168 /*make sure the pidl is freed*/
169 _ILFreeaPidl(This->apidl, This->cidl);
170
171 HeapFree(GetProcessHeap(),0,This);
172 return 0;
173 }
174 return This->ref;
175 }
176
177 /**************************************************************************
178 * ICM_InsertItem()
179 */
180 void WINAPI _InsertMenuItem (
181 HMENU hmenu,
182 UINT indexMenu,
183 BOOL fByPosition,
184 UINT wID,
185 UINT fType,
186 LPSTR dwTypeData,
187 UINT fState)
188 {
189 MENUITEMINFOA mii;
190
191 ZeroMemory(&mii, sizeof(mii));
192 mii.cbSize = sizeof(mii);
193 if (fType == MFT_SEPARATOR)
194 {
195 mii.fMask = MIIM_ID | MIIM_TYPE;
196 }
197 else
198 {
199 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
200 mii.dwTypeData = dwTypeData;
201 mii.fState = fState;
202 }
203 mii.wID = wID;
204 mii.fType = fType;
205 InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
206 }
207
208 /**************************************************************************
209 * ISvItemCm_fnQueryContextMenu()
210 * FIXME: load menu MENU_SHV_FILE out of resources instead if creating
211 * each menu item by calling _InsertMenuItem()
212 */
213 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
214 IContextMenu2 *iface,
215 HMENU hmenu,
216 UINT indexMenu,
217 UINT idCmdFirst,
218 UINT idCmdLast,
219 UINT uFlags)
220 {
221 ICOM_THIS(ItemCmImpl, iface);
222
223 TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
224
225 if (idCmdFirst != 0)
226 FIXME("We should use idCmdFirst=%d and idCmdLast=%d for command ids\n", idCmdFirst, idCmdLast);
227
228 if(!(CMF_DEFAULTONLY & uFlags) && This->cidl>0)
229 {
230 if(!(uFlags & CMF_EXPLORE))
231 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Select", MFS_ENABLED);
232
233 if(This->bAllValues)
234 {
235 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
236 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
237 }
238 else
239 {
240 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
241 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
242 }
243
244 SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
245
246 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
247 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_COPY, MFT_STRING, "&Copy", MFS_ENABLED);
248 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_CUT, MFT_STRING, "&Cut", MFS_ENABLED);
249
250 _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
251 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_DELETE, MFT_STRING, "&Delete", MFS_ENABLED);
252
253 if(uFlags & CMF_CANRENAME)
254 _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_RENAME, MFT_STRING, "&Rename", ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
255
256 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (FCIDM_SHVIEWLAST));
257 }
258 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
259 }
260
261 /**************************************************************************
262 * DoOpenExplore
263 *
264 * for folders only
265 */
266
267 static void DoOpenExplore(
268 IContextMenu2 *iface,
269 HWND hwnd,
270 LPCSTR verb)
271 {
272 ICOM_THIS(ItemCmImpl, iface);
273
274 UINT i, bFolderFound = FALSE;
275 LPITEMIDLIST pidlFQ;
276 SHELLEXECUTEINFOA sei;
277
278 /* Find the first item in the list that is not a value. These commands
279 should never be invoked if there isn't at least one folder item in the list.*/
280
281 for(i = 0; i<This->cidl; i++)
282 {
283 if(!_ILIsValue(This->apidl[i]))
284 {
285 bFolderFound = TRUE;
286 break;
287 }
288 }
289
290 if (!bFolderFound) return;
291
292 pidlFQ = ILCombine(This->pidl, This->apidl[i]);
293
294 ZeroMemory(&sei, sizeof(sei));
295 sei.cbSize = sizeof(sei);
296 sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
297 sei.lpIDList = pidlFQ;
298 sei.lpClass = "folder";
299 sei.hwnd = hwnd;
300 sei.nShow = SW_SHOWNORMAL;
301 sei.lpVerb = verb;
302 ShellExecuteExA(&sei);
303 SHFree(pidlFQ);
304 }
305
306 /**************************************************************************
307 * DoRename
308 */
309 static void DoRename(
310 IContextMenu2 *iface,
311 HWND hwnd)
312 {
313 ICOM_THIS(ItemCmImpl, iface);
314
315 LPSHELLBROWSER lpSB;
316 LPSHELLVIEW lpSV;
317
318 TRACE("(%p)->(wnd=%p)\n",This, hwnd);
319
320 /* get the active IShellView */
321 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
322 {
323 if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
324 {
325 TRACE("(sv=%p)\n",lpSV);
326 IShellView_SelectItem(lpSV, This->apidl[0],
327 SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
328 IShellView_Release(lpSV);
329 }
330 }
331 }
332
333 /**************************************************************************
334 * DoDelete
335 *
336 * deletes the currently selected items
337 */
338 static void DoDelete(IContextMenu2 *iface)
339 {
340 ICOM_THIS(ItemCmImpl, iface);
341 ISFHelper * psfhlp;
342
343 IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
344 if (psfhlp)
345 {
346 ISFHelper_DeleteItems(psfhlp, This->cidl, (LPCITEMIDLIST *)This->apidl);
347 ISFHelper_Release(psfhlp);
348 }
349 }
350
351 /**************************************************************************
352 * DoCopyOrCut
353 *
354 * copies the currently selected items into the clipboard
355 */
356 static BOOL DoCopyOrCut(
357 IContextMenu2 *iface,
358 HWND hwnd,
359 BOOL bCut)
360 {
361 ICOM_THIS(ItemCmImpl, iface);
362
363 LPSHELLBROWSER lpSB;
364 LPSHELLVIEW lpSV;
365 LPDATAOBJECT lpDo;
366
367 TRACE("(%p)->(wnd=%p,bCut=0x%08x)\n",This, hwnd, bCut);
368
369 if(GetShellOle())
370 {
371 /* get the active IShellView */
372 if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
373 {
374 if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
375 {
376 if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
377 {
378 pOleSetClipboard(lpDo);
379 IDataObject_Release(lpDo);
380 }
381 IShellView_Release(lpSV);
382 }
383 }
384 }
385 return TRUE;
386 #if 0
387 /*
388 the following code does the copy operation witout ole32.dll
389 we might need this possibility too (js)
390 */
391 BOOL bSuccess = FALSE;
392
393 TRACE("(%p)\n", iface);
394
395 if(OpenClipboard(NULL))
396 {
397 if(EmptyClipboard())
398 {
399 IPersistFolder2 * ppf2;
400 IShellFolder_QueryInterface(This->pSFParent, &IID_IPersistFolder2, (LPVOID*)&ppf2);
401 if (ppf2)
402 {
403 LPITEMIDLIST pidl;
404 IPersistFolder2_GetCurFolder(ppf2, &pidl);
405 if(pidl)
406 {
407 HGLOBAL hMem;
408
409 hMem = RenderHDROP(pidl, This->apidl, This->cidl);
410
411 if(SetClipboardData(CF_HDROP, hMem))
412 {
413 bSuccess = TRUE;
414 }
415 SHFree(pidl);
416 }
417 IPersistFolder2_Release(ppf2);
418 }
419
420 }
421 CloseClipboard();
422 }
423 return bSuccess;
424 #endif
425 }
426 /**************************************************************************
427 * ISvItemCm_fnInvokeCommand()
428 */
429 static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
430 IContextMenu2 *iface,
431 LPCMINVOKECOMMANDINFO lpcmi)
432 {
433 ICOM_THIS(ItemCmImpl, iface);
434
435 if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
436 FIXME("Is an EX structure\n");
437
438 TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);
439
440 if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
441 {
442 TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
443 return E_INVALIDARG;
444 }
445
446 if (HIWORD(lpcmi->lpVerb) == 0)
447 {
448 switch(LOWORD(lpcmi->lpVerb))
449 {
450 case FCIDM_SHVIEW_EXPLORE:
451 TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
452 DoOpenExplore(iface, lpcmi->hwnd, "explore");
453 break;
454 case FCIDM_SHVIEW_OPEN:
455 TRACE("Verb FCIDM_SHVIEW_OPEN\n");
456 DoOpenExplore(iface, lpcmi->hwnd, "open");
457 break;
458 case FCIDM_SHVIEW_RENAME:
459 TRACE("Verb FCIDM_SHVIEW_RENAME\n");
460 DoRename(iface, lpcmi->hwnd);
461 break;
462 case FCIDM_SHVIEW_DELETE:
463 TRACE("Verb FCIDM_SHVIEW_DELETE\n");
464 DoDelete(iface);
465 break;
466 case FCIDM_SHVIEW_COPY:
467 TRACE("Verb FCIDM_SHVIEW_COPY\n");
468 DoCopyOrCut(iface, lpcmi->hwnd, FALSE);
469 break;
470 case FCIDM_SHVIEW_CUT:
471 TRACE("Verb FCIDM_SHVIEW_CUT\n");
472 DoCopyOrCut(iface, lpcmi->hwnd, TRUE);
473 break;
474 default:
475 FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb));
476 }
477 }
478 else
479 {
480 TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
481 if (strcmp(lpcmi->lpVerb,"delete")==0)
482 DoDelete(iface);
483 else
484 FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
485 }
486 return NOERROR;
487 }
488
489 /**************************************************************************
490 * ISvItemCm_fnGetCommandString()
491 */
492 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
493 IContextMenu2 *iface,
494 UINT idCommand,
495 UINT uFlags,
496 UINT* lpReserved,
497 LPSTR lpszName,
498 UINT uMaxNameLen)
499 {
500 ICOM_THIS(ItemCmImpl, iface);
501
502 HRESULT hr = E_INVALIDARG;
503
504 TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
505
506 switch(uFlags)
507 {
508 case GCS_HELPTEXTA:
509 case GCS_HELPTEXTW:
510 hr = E_NOTIMPL;
511 break;
512
513 case GCS_VERBA:
514 switch(idCommand)
515 {
516 case FCIDM_SHVIEW_RENAME:
517 strcpy((LPSTR)lpszName, "rename");
518 hr = NOERROR;
519 break;
520 }
521 break;
522
523 /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
524 case, you need to do the lstrcpyW to the pointer passed.*/
525 case GCS_VERBW:
526 switch(idCommand)
527 { case FCIDM_SHVIEW_RENAME:
528 MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
529 hr = NOERROR;
530 break;
531 }
532 break;
533
534 case GCS_VALIDATEA:
535 case GCS_VALIDATEW:
536 hr = NOERROR;
537 break;
538 }
539 TRACE("-- (%p)->(name=%s)\n",This, lpszName);
540 return hr;
541 }
542
543 /**************************************************************************
544 * ISvItemCm_fnHandleMenuMsg()
545 * NOTES
546 * should be only in IContextMenu2 and IContextMenu3
547 * is nevertheless called from word95
548 */
549 static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
550 IContextMenu2 *iface,
551 UINT uMsg,
552 WPARAM wParam,
553 LPARAM lParam)
554 {
555 ICOM_THIS(ItemCmImpl, iface);
556
557 TRACE("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
558
559 return E_NOTIMPL;
560 }
561
562 static struct ICOM_VTABLE(IContextMenu2) cmvt =
563 {
564 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
565 ISvItemCm_fnQueryInterface,
566 ISvItemCm_fnAddRef,
567 ISvItemCm_fnRelease,
568 ISvItemCm_fnQueryContextMenu,
569 ISvItemCm_fnInvokeCommand,
570 ISvItemCm_fnGetCommandString,
571 ISvItemCm_fnHandleMenuMsg
572 };