shv_item_cmenu.c 13.4 KB
Newer Older
1 2 3
/*
 *	IContextMenu for items in the shellview
 *
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright 1998, 2000 Juergen Schmied <juergen.schmied@debitel.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
 */
20

21 22
#include <string.h>

23
#define COBJMACROS
24 25
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
26

27
#include "winerror.h"
28
#include "wine/debug.h"
29

30 31
#include "windef.h"
#include "wingdi.h"
32
#include "pidl.h"
33
#include "undocshell.h"
34
#include "shlobj.h"
35 36 37 38

#include "shell32_main.h"
#include "shellfolder.h"

39 40
#include "shresdef.h"

41
WINE_DEFAULT_DEBUG_CHANNEL(shell);
42 43 44 45

/**************************************************************************
*  IContextMenu Implementation
*/
46
typedef struct
47
{	const IContextMenu2Vtbl *lpVtbl;
Mike McCormack's avatar
Mike McCormack committed
48
	LONG		ref;
49 50 51 52 53 54 55 56
	IShellFolder*	pSFParent;
	LPITEMIDLIST	pidl;		/* root pidl */
	LPITEMIDLIST	*apidl;		/* array of child pidls */
	UINT		cidl;
	BOOL		bAllValues;
} ItemCmImpl;


57
static const IContextMenu2Vtbl cmvt;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

/**************************************************************************
* ISvItemCm_CanRenameItems()
*/
static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
{	UINT  i;
	DWORD dwAttributes;

	TRACE("(%p)->()\n",This);

	if(This->apidl)
	{
	  for(i = 0; i < This->cidl; i++){}
	  if(i > 1) return FALSE;		/* can't rename more than one item at a time*/
	  dwAttributes = SFGAO_CANRENAME;
73
	  IShellFolder_GetAttributesOf(This->pSFParent, 1, (LPCITEMIDLIST*)This->apidl, &dwAttributes);
74 75 76 77 78 79 80 81
	  return dwAttributes & SFGAO_CANRENAME;
	}
	return FALSE;
}

/**************************************************************************
*   ISvItemCm_Constructor()
*/
82
IContextMenu2 *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, const LPCITEMIDLIST *apidl, UINT cidl)
83 84 85
{	ItemCmImpl* cm;
	UINT  u;

86
	cm = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
87
	cm->lpVtbl = &cmvt;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	cm->ref = 1;
	cm->pidl = ILClone(pidl);
	cm->pSFParent = pSFParent;

	if(pSFParent) IShellFolder_AddRef(pSFParent);

	cm->apidl = _ILCopyaPidl(apidl, cidl);
	cm->cidl = cidl;

	cm->bAllValues = 1;
	for(u = 0; u < cidl; u++)
	{
	  cm->bAllValues &= (_ILIsValue(apidl[u]) ? 1 : 0);
	}

	TRACE("(%p)->()\n",cm);

105
	return (IContextMenu2*)cm;
106 107 108 109 110
}

/**************************************************************************
*  ISvItemCm_fnQueryInterface
*/
111
static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
112
{
113
	ItemCmImpl *This = (ItemCmImpl *)iface;
114 115 116 117 118

	TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);

	*ppvObj = NULL;

119 120 121
        if(IsEqualIID(riid, &IID_IUnknown) ||
           IsEqualIID(riid, &IID_IContextMenu) ||
           IsEqualIID(riid, &IID_IContextMenu2))
122 123
	{
	  *ppvObj = This;
124
	}
125 126 127 128 129 130
	else if(IsEqualIID(riid, &IID_IShellExtInit))  /*IShellExtInit*/
	{
	  FIXME("-- LPSHELLEXTINIT pointer requested\n");
	}

	if(*ppvObj)
131 132
	{
	  IUnknown_AddRef((IUnknown*)*ppvObj);
133 134 135 136 137 138 139 140 141 142
	  TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
	  return S_OK;
	}
	TRACE("-- Interface: E_NOINTERFACE\n");
	return E_NOINTERFACE;
}

/**************************************************************************
*  ISvItemCm_fnAddRef
*/
143
static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu2 *iface)
144
{
145
	ItemCmImpl *This = (ItemCmImpl *)iface;
146
	ULONG refCount = InterlockedIncrement(&This->ref);
147

148
	TRACE("(%p)->(count=%u)\n", This, refCount - 1);
149

150
	return refCount;
151 152 153 154 155
}

/**************************************************************************
*  ISvItemCm_fnRelease
*/
156
static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu2 *iface)
157
{
158
	ItemCmImpl *This = (ItemCmImpl *)iface;
159
	ULONG refCount = InterlockedDecrement(&This->ref);
160

161
	TRACE("(%p)->(count=%i)\n", This, refCount + 1);
162

163
	if (!refCount)
164 165 166 167 168 169
	{
	  TRACE(" destroying IContextMenu(%p)\n",This);

	  if(This->pSFParent)
	    IShellFolder_Release(This->pSFParent);

170
	  SHFree(This->pidl);
171

172 173 174 175 176
	  /*make sure the pidl is freed*/
	  _ILFreeaPidl(This->apidl, This->cidl);

	  HeapFree(GetProcessHeap(),0,This);
	}
177
	return refCount;
178 179
}

180
static void _InsertMenuItemW (
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	HMENU hmenu,
	UINT indexMenu,
	BOOL fByPosition,
	UINT wID,
	UINT fType,
	LPWSTR dwTypeData,
	UINT fState)
{
	MENUITEMINFOW	mii;

	mii.cbSize = sizeof(mii);
	if (fType == MFT_SEPARATOR)
	{
	  mii.fMask = MIIM_ID | MIIM_TYPE;
	}
	else
	{
	  mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
	  mii.dwTypeData = dwTypeData;
	  mii.fState = fState;
	}
	mii.wID = wID;
	mii.fType = fType;
	InsertMenuItemW( hmenu, indexMenu, fByPosition, &mii);
}

207 208 209 210
/**************************************************************************
* ISvItemCm_fnQueryContextMenu()
*/
static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
211
	IContextMenu2 *iface,
212 213 214 215 216 217
	HMENU hmenu,
	UINT indexMenu,
	UINT idCmdFirst,
	UINT idCmdLast,
	UINT uFlags)
{
218
	ItemCmImpl *This = (ItemCmImpl *)iface;
219

220
	TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
221

222 223 224 225
	if (idCmdFirst != 0)
	  FIXME("We should use idCmdFirst=%d and idCmdLast=%d for command ids\n", idCmdFirst, idCmdLast);

	if(!(CMF_DEFAULTONLY & uFlags) && This->cidl>0)
226
	{
227 228 229 230 231 232 233 234
          HMENU hmenures = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_SHV_FILE));

	  if(uFlags & CMF_EXPLORE)
            RemoveMenu(hmenures, FCIDM_SHVIEW_OPEN, MF_BYCOMMAND);

          Shell_MergeMenus(hmenu, GetSubMenu(hmenures, 0), indexMenu, idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);

          DestroyMenu(hmenures);
235 236

	  if(This->bAllValues)
237
	  {
238 239 240 241 242 243 244 245 246
            MENUITEMINFOW mi;
            WCHAR str[255];
            mi.cbSize = sizeof(mi);
            mi.fMask = MIIM_ID | MIIM_STRING | MIIM_FTYPE;
            mi.dwTypeData = str;
            mi.cch = 255;
            GetMenuItemInfoW(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND, &mi);
            RemoveMenu(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND);
            _InsertMenuItemW(hmenu, (uFlags & CMF_EXPLORE) ? 1 : 2, MF_BYPOSITION, FCIDM_SHVIEW_EXPLORE, MFT_STRING, str, MFS_ENABLED);
247
	  }
248 249 250

	  SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);

251 252 253 254
	  if(uFlags & ~CMF_CANRENAME)
            RemoveMenu(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND);
          else
            EnableMenuItem(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND | ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
255 256 257 258 259 260 261 262 263 264 265 266 267

	  return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (FCIDM_SHVIEWLAST));
	}
	return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
}

/**************************************************************************
* DoOpenExplore
*
*  for folders only
*/

static void DoOpenExplore(
268
	IContextMenu2 *iface,
269 270 271
	HWND hwnd,
	LPCSTR verb)
{
272
	ItemCmImpl *This = (ItemCmImpl *)iface;
273

274
	UINT i, bFolderFound = FALSE;
275 276 277
	LPITEMIDLIST	pidlFQ;
	SHELLEXECUTEINFOA	sei;

278
	/* Find the first item in the list that is not a value. These commands
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	    should never be invoked if there isn't at least one folder item in the list.*/

	for(i = 0; i<This->cidl; i++)
	{
	  if(!_ILIsValue(This->apidl[i]))
	  {
	    bFolderFound = TRUE;
	    break;
	  }
	}

	if (!bFolderFound) return;

	pidlFQ = ILCombine(This->pidl, This->apidl[i]);

	ZeroMemory(&sei, sizeof(sei));
	sei.cbSize = sizeof(sei);
	sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
	sei.lpIDList = pidlFQ;
298
	sei.lpClass = "Folder";
299 300 301 302 303 304 305 306 307 308 309
	sei.hwnd = hwnd;
	sei.nShow = SW_SHOWNORMAL;
	sei.lpVerb = verb;
	ShellExecuteExA(&sei);
	SHFree(pidlFQ);
}

/**************************************************************************
* DoRename
*/
static void DoRename(
310
	IContextMenu2 *iface,
311 312
	HWND hwnd)
{
313
	ItemCmImpl *This = (ItemCmImpl *)iface;
314 315 316 317

	LPSHELLBROWSER	lpSB;
	LPSHELLVIEW	lpSV;

318
	TRACE("(%p)->(wnd=%p)\n",This, hwnd);
319 320 321 322 323 324

	/* get the active IShellView */
	if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
	{
	  if(SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
	  {
325
	    TRACE("(sv=%p)\n",lpSV);
326 327
	    IShellView_SelectItem(lpSV, This->apidl[0],
              SVSI_DESELECTOTHERS|SVSI_EDIT|SVSI_ENSUREVISIBLE|SVSI_FOCUSED|SVSI_SELECT);
328 329 330 331 332 333 334 335 336 337
	    IShellView_Release(lpSV);
	  }
	}
}

/**************************************************************************
 * DoDelete
 *
 * deletes the currently selected items
 */
338
static void DoDelete(IContextMenu2 *iface)
339
{
340
	ItemCmImpl *This = (ItemCmImpl *)iface;
341
	ISFHelper * psfhlp;
342

343 344 345
	IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
	if (psfhlp)
	{
346
	  ISFHelper_DeleteItems(psfhlp, This->cidl, (LPCITEMIDLIST *)This->apidl);
347 348 349 350 351 352 353
	  ISFHelper_Release(psfhlp);
	}
}

/**************************************************************************
 * DoCopyOrCut
 *
354
 * copies the currently selected items into the clipboard
355 356
 */
static BOOL DoCopyOrCut(
357
	IContextMenu2 *iface,
358 359 360
	HWND hwnd,
	BOOL bCut)
{
361
	ItemCmImpl *This = (ItemCmImpl *)iface;
362 363 364 365

	LPSHELLBROWSER	lpSB;
	LPSHELLVIEW	lpSV;
	LPDATAOBJECT    lpDo;
366

367
	TRACE("(%p)->(wnd=%p,bCut=0x%08x)\n",This, hwnd, bCut);
368

369 370
	/* get the active IShellView */
	if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
371
	{
372
	  if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
373
	  {
374
	    if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
375
	    {
376 377
	      OleSetClipboard(lpDo);
	      IDataObject_Release(lpDo);
378
	    }
379
	    IShellView_Release(lpSV);
380 381 382 383 384 385 386 387
	  }
	}
	return TRUE;
}
/**************************************************************************
* ISvItemCm_fnInvokeCommand()
*/
static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
388
	IContextMenu2 *iface,
389 390
	LPCMINVOKECOMMANDINFO lpcmi)
{
391
    ItemCmImpl *This = (ItemCmImpl *)iface;
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

    if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
        FIXME("Is an EX structure\n");

    TRACE("(%p)->(invcom=%p verb=%p wnd=%p)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);

    if( HIWORD(lpcmi->lpVerb)==0 && LOWORD(lpcmi->lpVerb) > FCIDM_SHVIEWLAST)
    {
        TRACE("Invalid Verb %x\n",LOWORD(lpcmi->lpVerb));
        return E_INVALIDARG;
    }

    if (HIWORD(lpcmi->lpVerb) == 0)
    {
        switch(LOWORD(lpcmi->lpVerb))
        {
        case FCIDM_SHVIEW_EXPLORE:
            TRACE("Verb FCIDM_SHVIEW_EXPLORE\n");
            DoOpenExplore(iface, lpcmi->hwnd, "explore");
            break;
        case FCIDM_SHVIEW_OPEN:
            TRACE("Verb FCIDM_SHVIEW_OPEN\n");
            DoOpenExplore(iface, lpcmi->hwnd, "open");
            break;
        case FCIDM_SHVIEW_RENAME:
            TRACE("Verb FCIDM_SHVIEW_RENAME\n");
            DoRename(iface, lpcmi->hwnd);
            break;
        case FCIDM_SHVIEW_DELETE:
            TRACE("Verb FCIDM_SHVIEW_DELETE\n");
            DoDelete(iface);
            break;
        case FCIDM_SHVIEW_COPY:
            TRACE("Verb FCIDM_SHVIEW_COPY\n");
            DoCopyOrCut(iface, lpcmi->hwnd, FALSE);
            break;
        case FCIDM_SHVIEW_CUT:
            TRACE("Verb FCIDM_SHVIEW_CUT\n");
            DoCopyOrCut(iface, lpcmi->hwnd, TRUE);
            break;
        default:
            FIXME("Unhandled Verb %xl\n",LOWORD(lpcmi->lpVerb));
        }
    }
    else
    {
        TRACE("Verb is %s\n",debugstr_a(lpcmi->lpVerb));
        if (strcmp(lpcmi->lpVerb,"delete")==0)
            DoDelete(iface);
        else
            FIXME("Unhandled string verb %s\n",debugstr_a(lpcmi->lpVerb));
    }
    return NOERROR;
445 446 447 448 449 450
}

/**************************************************************************
*  ISvItemCm_fnGetCommandString()
*/
static HRESULT WINAPI ISvItemCm_fnGetCommandString(
451
	IContextMenu2 *iface,
Kevin Koltzau's avatar
Kevin Koltzau committed
452
	UINT_PTR idCommand,
453
	UINT uFlags,
454
	UINT* lpReserved,
455 456
	LPSTR lpszName,
	UINT uMaxNameLen)
457
{
458
	ItemCmImpl *This = (ItemCmImpl *)iface;
459 460 461

	HRESULT  hr = E_INVALIDARG;

462
	TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
463 464 465

	switch(uFlags)
	{
466 467
	  case GCS_HELPTEXTA:
	  case GCS_HELPTEXTW:
468 469 470 471 472 473 474
	    hr = E_NOTIMPL;
	    break;

	  case GCS_VERBA:
	    switch(idCommand)
	    {
	      case FCIDM_SHVIEW_RENAME:
475
	        strcpy(lpszName, "rename");
476 477 478 479 480
	        hr = NOERROR;
	        break;
	    }
	    break;

481
	     /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This
482 483 484 485
	     case, you need to do the lstrcpyW to the pointer passed.*/
	  case GCS_VERBW:
	    switch(idCommand)
	    { case FCIDM_SHVIEW_RENAME:
486
                MultiByteToWideChar( CP_ACP, 0, "rename", -1, (LPWSTR)lpszName, uMaxNameLen );
487 488 489 490 491
	        hr = NOERROR;
	        break;
	    }
	    break;

492 493
	  case GCS_VALIDATEA:
	  case GCS_VALIDATEW:
494 495 496 497 498 499 500 501 502 503 504 505 506 507
	    hr = NOERROR;
	    break;
	}
	TRACE("-- (%p)->(name=%s)\n",This, lpszName);
	return hr;
}

/**************************************************************************
* ISvItemCm_fnHandleMenuMsg()
* NOTES
*  should be only in IContextMenu2 and IContextMenu3
*  is nevertheless called from word95
*/
static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
508
	IContextMenu2 *iface,
509 510 511 512
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam)
{
513
	ItemCmImpl *This = (ItemCmImpl *)iface;
514

515
	TRACE("(%p)->(msg=%x wp=%lx lp=%lx)\n",This, uMsg, wParam, lParam);
516 517 518 519

	return E_NOTIMPL;
}

520
static const IContextMenu2Vtbl cmvt =
521
{
522 523 524 525 526 527
	ISvItemCm_fnQueryInterface,
	ISvItemCm_fnAddRef,
	ISvItemCm_fnRelease,
	ISvItemCm_fnQueryContextMenu,
	ISvItemCm_fnInvokeCommand,
	ISvItemCm_fnGetCommandString,
528
	ISvItemCm_fnHandleMenuMsg
529
};