Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Customized Visual Studio .NET package: your fully integrated document window inside the IDE

, 20 Dec 2003
Create a fully integrated document window inside the Visual Studio IDE.
#include "StdAfx.h"
#include "OleCommandTarget.h"
#include "Dbg.h"

BOOL COleCommandTargetSpy::Attach( void* pObj )
{
	if ( pObj == (void*)m_pObj )
	{
		return TRUE;
	}
	if ( m_pObj == NULL )
	{
		m_pObj = (IOleCommandTarget*)pObj;
		return TRUE;
	}
	return FALSE;
}

STDMETHODIMP COleCommandTargetSpy::QueryInterface(REFIID riid, void** ppvObject )
{
	if ( m_pObj )
	{
		//CDbg::Trace( "       IOleCommandTarget::QueryInterface, REFIID=" );
		//CDbg::TraceGuid( riid );
		return m_pObj->QueryInterface( riid, ppvObject );
	}
	return E_INVALIDARG;
}

STDMETHODIMP_(ULONG) COleCommandTargetSpy::AddRef()
{
	if ( m_pObj )
	{
		return m_pObj->AddRef();
	}
	return 0;
}
STDMETHODIMP_(ULONG) COleCommandTargetSpy::Release()
{
	if ( m_pObj )
	{
		ULONG uRes = m_pObj->Release();
		if ( uRes == 0 )
		{
			delete this;
		}
		return uRes;
	}
	return 0;
}

static GUID guidCommonActionsGrp = {0x5EFC7975,0x14BC,0x11CF,0x9B,0x2B,0x00,0xAA,0x00,0x57,0x38,0x19};
#define COMMAND_ID_CUT	0x10
#define COMMAND_ID_PST	0x1A /*paste*/
#define COMMAND_ID_CPY	0xF
#define COMMAND_ID_UDO	0x2C /*undo*/
#define COMMAND_ID_RDO	0x1E /*redo*/
#define COMMAND_ID_DEL	0x11 
#define COMMAND_ID_SALL	0x1F /*select all*/
#define COMMAND_ID_SPLT 0x10D/*called when new vertical group window is about to be created*/
#define COMMAND_ID_SPLT1 0x10C/*called when new vertical group window is about to be created*/
#define COMMAND_ID_EXIT	0xE5 /*called on close: revoke drop target*/

static HRESULT FilterOleCommand( ULONG cCmds, OLECMD *prgCmds )
{
	for( ULONG i=0; i < cCmds; i ++ )
	{
		if ( prgCmds[i].cmdID == COMMAND_ID_CUT )
		{
			prgCmds[i].cmdf = OLECMDF_SUPPORTED|OLECMDF_ENABLED;
			return S_OK;
		}
		if ( prgCmds[i].cmdID == COMMAND_ID_EXIT||
			prgCmds[i].cmdID == COMMAND_ID_SPLT1 ||
			prgCmds[i].cmdID == COMMAND_ID_SPLT  )
		{
			return 0x80040100;
		}
	}
	return E_UNEXPECTED;
}

STDMETHODIMP COleCommandTargetSpy::QueryStatus(
		const GUID *pguidCmdGroup,
		ULONG cCmds, // Number of commands in prgCmds array
		OLECMD *prgCmds, // Array of commands
		OLECMDTEXT *pCmdText // Pointer to name or status of command
		)
{
	if ( m_pObj )
	{
/*		if ( *pguidCmdGroup == guidCommonActionsGrp && prgCmds[0].cmdID == COMMAND_ID_SPLT1)
		{
			HRESULT hr = m_pObj->QueryStatus( pguidCmdGroup, cCmds, prgCmds, pCmdText );
			return hr;
		}
*/		
		if ( *pguidCmdGroup == guidCommonActionsGrp )
		{
			return FilterOleCommand(cCmds,prgCmds);//later enable some commands
		}
/*		HRESULT hr = m_pObj->QueryStatus( pguidCmdGroup, cCmds, prgCmds, pCmdText );
		/ *if ( hr == S_OK ) called frequently
		{
			CDbg::Trace( " QueryStatus: " );
			CDbg::TraceGuid( *pguidCmdGroup, FALSE );
			CDbg::Trace( " cmd=%X\n", prgCmds[0].cmdID );
		}* /
		return hr;
*/	}
	return E_INVALIDARG;
}

STDMETHODIMP COleCommandTargetSpy::Exec(
		const GUID *pguidCmdGroup, // Pointer to command group
		DWORD nCmdID, // Identifier of command to execute
		DWORD nCmdExecOpt, // Options for executing the command
		VARIANTARG *pvaIn, // Pointer to input arguments
		VARIANTARG *pvaOut // Pointer to command output
		)
{ 
	if ( m_pObj )
	{
/*	this is useful for testing all commands
		HRESULT hr = m_pObj->Exec( pguidCmdGroup, nCmdID, nCmdExecOpt, pvaIn, pvaOut );
		if ( hr == S_OK )
		{
			CDbg::TraceGuid( *pguidCmdGroup, FALSE );
			CDbg::Trace( "      - cmd=%X\n", nCmdID );
		}
		return hr;
*/
/*	this code is useful for specific command results test
		if ( *pguidCmdGroup == guidCommonActionsGrp && nCmdID == COMMAND_ID_SPLT1)
		{
			HRESULT hr = m_pObj->Exec( pguidCmdGroup, nCmdID, nCmdExecOpt, pvaIn, pvaOut );
			return hr;
		}
*/
	if ( *pguidCmdGroup == guidCommonActionsGrp )
	{
		CDbg::Trace("       IOleCommandTarget::Exec(cmd=%X )\n", nCmdID );
		if ( nCmdID == 0x2AC || nCmdID == 0x151 )
		{
			return S_OK;
		}
		if ( nCmdID == COMMAND_ID_EXIT ||
			nCmdID == COMMAND_ID_SPLT1 ||
			nCmdID == COMMAND_ID_SPLT )
		{
			return 0x80040100;
		}
	}

	}
	return E_INVALIDARG;
}

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

Share

About the Author

Michael Sheinin
Web Developer
United States United States
For the last 7 years I have developed software in real-time/embedded and MS-Windows environments for military and civil markets. I hold B.Sc. degree in computer engineering. Living in Ontario, Canada, I like hiking and traveling in general. Currently, I'm looking for employment opportunity inside the GTA.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 21 Dec 2003
Article Copyright 2003 by Michael Sheinin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid