Click here to Skip to main content
15,881,803 members
Articles / Desktop Programming / ATL

The Mini Shell Extension Framework – Part III

Rate me:
Please Sign up or sign in to vote.
4.96/5 (11 votes)
18 Sep 200516 min read 139.6K   1.4K   46  
Discussion of a small C++ framework to create Windows shell extensions (IShellFolderImpl).
//
// (C) Copyright by Victor Derks <vba64@xs4all.nl>
//
// See README.TXT for detailed details of the software licence.
//
#include "stdafx.h"
#include "vvvitem.h"
#include "columns.h"
#include "../include/shellitemid.h"
#include "../include/macros.h"
#include "../include/strutil.h"
#include "../include/util.h"
#include "../include/imagelistindex.h"
#include "resource.h"


CVVVItem::CVVVItem(const SHITEMID& shitemid)
{
	// Shell item IDs can be passed from external sources, validate
	RaiseExceptionIf(shitemid.cb < sizeof(int) + sizeof(int) + sizeof(wchar_t));

	CShellItemIterator it(shitemid);

	_bFolder = it.GetBool();
	_nID     = it.GetUnsignedInt();
	_nSize   = it.GetUnsignedInt();
	_strName = it.GetString();
}


// Note: 'switch' is used in this example to show all the possible options.
CString CVVVItem::GetDisplayName(SHGDNF shgdnf) const
{
	switch (shgdnf)
	{
	case SHGDN_NORMAL:
	case SHGDN_INFOLDER:
		break;

	case SHGDN_INFOLDER | SHGDN_FOREDITING:
		break; // override if item has a special edit name

	case SHGDN_INFOLDER | SHGDN_FORADDRESSBAR:
		break; // override if item has a special name for the address bar

	case SHGDN_INFOLDER | SHGDN_FORPARSING:
	case SHGDN_FORPARSING: // note parent should append folder name before item name.
		return ToString(GetID()); // return unique string (vvv items are unique by ID)

	default:
		ATLTRACE2(atlTraceCOM, 0, _T("CVVVItem::GetDisplayName (shgdnf=%d)\n"), shgdnf);
		break;
	}

	return _strName;
}


void CVVVItem::SetDisplayName(CString strNewName, SHGDNF shgndf)
{
	ATLASSERT(shgndf == SHGDN_NORMAL || shgndf == SHGDN_INFOLDER);
	(shgndf); // not used in release.

	_strName = strNewName;
}


SFGAOF CVVVItem::GetAttributeOf(bool bSingleSelect, bool bReadOnly) const
{
	if (IsFolder())
	{
		return SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
	}
	else
	{
		SFGAOF sfgaof = SFGAO_CANCOPY;

		// Tip: to debug error handling, disable the readonly check and make the .vvv readonly
		if (!bReadOnly)
		{
			sfgaof |= SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_CANMOVE;

			// Note: standard windows UI allows the rename option for multiple items.
		}

		if (bSingleSelect)
		{
			sfgaof |= SFGAO_HASPROPSHEET; 
		}

		return sfgaof;
	}
}


SHITEMID* CVVVItem::CreateShellItemId(bool bAddEmptyId) const
{
	CStringW strName = CT2W(_strName);

	CShellItemIdMember members[] =
	{
		CShellItemIdMember(&_bFolder),
		CShellItemIdMember(&_nID),
		CShellItemIdMember(&_nSize),
		CShellItemIdMember(strName)
	};

	CShellItemId shellitemid(members, MSF_ARRAY_SIZE(members), bAddEmptyId);

	return shellitemid.Detach();
}


int CVVVItem::Compare(const CVVVItem& item, int nCompareBy, bool /*bCanonicalOnly*/) const
{
	switch (nCompareBy)
	{
	case COLUMN_NAME:
		return CompareByName(item);

	case COLUMN_SIZE:
		return UIntCmp(_nSize, item._nSize);

	default:
		ATLASSERT(!"Illegal nCompare option detected");
		RaiseException();
	}
}


CString CVVVItem::GetItemDetailsOf(UINT iColumn) const
{
	switch (iColumn)
	{
	case COLUMN_NAME:
		return GetDisplayName(SHGDN_NORMAL);

	case COLUMN_SIZE:
		if (IsFolder())
			return CString();
		else
			return ToString(GetSize());

	default:
		ATLASSERT(false);
		RaiseException();
	}
}


CString CVVVItem::GetInfoTipText() const
{
	CString strText = LoadString(IDS_SHELLEXT_NAME) + _T(": ") + GetDisplayName() + _T("\n");
	strText += LoadString(IDS_SHELLEXT_SIZE) + _T(": ") + ToString(GetSize());

	return strText;
}


int CVVVItem::GetIconOf(UINT flags) const
{
	if (IsFolder())
		return IsBitSet(flags, GIL_OPENICON) ?
			STD_IMAGELIST_INDEX_FOLDER_OPEN :
			STD_IMAGELIST_INDEX_FOLDER_PLAIN;

	return STD_IMAGELIST_INDEX_DOCUMENT_FILLED;
}


// Note: can only return 0 if same items are compared.
int CVVVItem::CompareByName(const CVVVItem& item) const
{
	// Compare first by folder, to make sure folders are listed before files.
	if (IsFolder())
	{
		if (!item.IsFolder())
			return -1;
	}
	else
	{
		if (item.IsFolder())
			return 1;
	}

	int nResult = StrCmp(GetName(), item.GetName());
	if (nResult != 0)
		return nResult; // different by name

	// VVV items can be equal by name, but are always different by ID.
	return UIntCmp(_nID, item._nID);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Hitachi High-Tech Analytical Science
Netherlands Netherlands
Victor lives in Nijmegen, the oldest city in The Netherlands.
He studied Applied Physics in Delft and works Hitachi High-Tech Analytical Science.

Comments and Discussions