Click here to Skip to main content
15,881,666 members
Articles / Programming Languages / C++

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

Rate me:
Please Sign up or sign in to vote.
4.72/5 (20 votes)
20 Dec 200313 min read 63.7K   1.1K   39  
Create a fully integrated document window inside the Visual Studio IDE.
#include "StdAfx.h"
#include "Docdata.h"
#include "DocView.h"

#include "Mediator.h"
#include "Histogram.h"
#include "FileInfo.h"
#include "CommandIDs.h"


static const GUID IID_IVsPersistDocData = {0xD5D49C61,0x1C0B,0x4EA1,0x9A,0xDB,0xA7,0x9F,0xB1,0xDB,0xC7,0xB5};
static const GUID IID_IPersistFileFormat = {0x3AFAE242,0xB530,0x11D0,0x81,0x99,0x00,0xA0,0xC9,0x1B,0xBE,0xE3};


CDocumentData::CDocumentData()
{
	m_uRefCnt = 0;
	m_pHist = NULL;
	m_pInfo = NULL;
	m_dwCmdState = 0;
	m_pMdtr = NULL;
	m_szFileName = NULL;
}

CDocumentData::~CDocumentData()
{
	if ( m_pMdtr )
	{
		m_pMdtr->SetData( NULL );
	}
	if ( m_pHist )
	{
		delete m_pHist;
	}
	if ( m_pInfo )
	{
		delete m_pInfo;
	}
	CDbg::Trace( " CDocumentData:: destructor\n" );
}

void CDocumentData::Attach( CMediator* pMdtr )
{
	if ( m_pMdtr )
	{
		m_pMdtr->SetData( NULL );
	}
	m_pMdtr = pMdtr;
	m_pMdtr->SetData( this );
}

//**************************************************************************
//	IUnknown
//**************************************************************************

STDMETHODIMP CDocumentData::QueryInterface( REFIID rid, void** ppvObject )
{
	if ( (rid == IID_IUnknown)||(rid == IID_IVsPersistDocData) )
	{
		*ppvObject = (IVsPersistDocData*)this;
		AddRef();
		return S_OK;
	}
	if ( rid == IID_IPersistFileFormat )
	{
		*ppvObject = (IPersistFileFormat*)this;
		AddRef();
		CDbg::Trace("       DocData::QI: IPersistFileFormat\n" );
		return S_OK;
	}
	if ( rid == IID_IDispatch )
	{
		CDbg::Trace("       DocData::QI: IDispatch ####\n" );
		//*ppvObject = (IDispatch*)this;
		//return S_OK;
	}
	*ppvObject = NULL;
	return E_NOTIMPL;
}

STDMETHODIMP_(ULONG) CDocumentData::AddRef()
{
	++m_uRefCnt;
	return m_uRefCnt;
}

STDMETHODIMP_(ULONG) CDocumentData::Release()
{
	m_uRefCnt --;
	if ( m_uRefCnt == 0 )
	{
		delete this;
		return 0;
	}
	return m_uRefCnt;
}

//**************************************************************************
//	IVsPersistDocData
//**************************************************************************

STDMETHODIMP CDocumentData::PD_Stub3( void* p1 )
{
	CDbg::Trace( "       IVsPersistDocData::Stub3 = S_OK\n" );
	return S_OK;
}

STDMETHODIMP CDocumentData::PD_Stub4( BOOL* pbDocChanged )
{
	if ( m_pHist && m_pInfo )
	{
		*pbDocChanged = (m_pHist->IsNotSaved() | m_pInfo->IsNotSaved());
	}
	return S_OK;
}

STDMETHODIMP CDocumentData::PD_Stub5( void* p1 )
{
	CDbg::Trace( "       IVsPersistDocData::Stub5 = S_NOTIMPL\n" );
	return E_NOTIMPL;
}

STDMETHODIMP CDocumentData::PD_LoadDocData( LPCOLESTR wszFileName )
{
	CDbg::Trace( "       IVsPersistDocData::PD_LoadDocData=S_OK\n" );

#ifndef _BASIC_WINDOW_
	HANDLE hFile = CreateFileW( wszFileName, GENERIC_READ, FILE_SHARE_READ,
							NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL );
	
	if ( hFile == INVALID_HANDLE_VALUE )
	{
		return E_FAIL;
	}

	BYTE* pucBuff = new BYTE[1028];
	if ( pucBuff == NULL )
	{
		CloseHandle( hFile );
		return E_FAIL;
	}
	m_pHist = new CHistogram();
	m_pHist->Init( 30, 30, 400, 200 );
	m_pInfo = new CFileInfo();
	m_pInfo->Init( 30, 230, 330, 200 );
	DWORD dwRead = 0;
	DWORD dwTotalRead = 0;

	while( ReadFile( hFile, pucBuff, 1028, &dwRead, NULL ) )
	{
		if ( dwRead == 0 )
		{
			break;
		}
		dwTotalRead += dwRead;
		m_pHist->AddData( pucBuff, dwRead );
	}
	if ( dwTotalRead == 0 )
	{
		delete m_pInfo;
		delete m_pHist;
		delete pucBuff;
		m_pHist = NULL;
		m_pInfo = NULL;
		CloseHandle( hFile );
		return E_FAIL;
	}
	else
	{
		m_pInfo->SetLength( int(dwTotalRead) );
		m_pInfo->SetName( wszFileName );
		m_pInfo->SetSignature( L"File content quantized histogram." );
		m_pInfo->SetSaved();
		m_pHist->SetSaved();
	}
	delete pucBuff;
	m_dwCmdState = (m_pHist->GetCmdState() | m_pInfo->GetCmdState());
	
	if ( m_pMdtr )
	{
		m_pMdtr->AddGraphicObject( m_pHist );
		m_pMdtr->AddGraphicObject( m_pInfo );
	}
#endif //_BASIC_WINDOW_

	return S_OK;
}

STDMETHODIMP CDocumentData::PD_SaveDocData( DWORD dwFlg, LPWSTR* pbstr, BOOL *pbCn )
{
	CDbg::Trace( "       IVsPersistDocData::SaveDocData( Flg=%x, [out]pbstr=%S (0x%p), [out]*pb=%d )\n",
					dwFlg, *pbstr, pbstr, *pbCn );

	if ( m_pHist && m_pInfo )
	{
		m_pHist->SetSaved();
		m_pInfo->SetSaved();
	}
	return S_OK;
}

STDMETHODIMP CDocumentData::PD_Stub8( )
{
	CDbg::Trace( "       IVsPersistDocData::Stub8( ) = S_OK\n" );
	return S_OK;
}

STDMETHODIMP CDocumentData::PD_Stub9( void* p1, void* p2, void* p3 )
{
	CDbg::Trace( "       IVsPersistDocData::Stub9( ) = S_OK\n" );
	return S_OK;
}


STDMETHODIMP CDocumentData::PD_Stub10( void* p1, void* p2, void* p3, void* p4 )
{
	CDbg::Trace( "       IVsPersistDocData::Stub10( ) = S_OK\n" );
	return S_OK;
}

STDMETHODIMP CDocumentData::PD_SetDocChanged( BOOL* pChanged )
{
	CDbg::Trace( "       IVsPersistDocData::SetChanged( ) = S_OK\n" );
	//*pChanged = TRUE;
	return S_OK;
}

STDMETHODIMP CDocumentData::PD_Stub12( void* p1 )
{
	CDbg::Trace( "       IVsPersistDocData::Stub12( p1=0x%p ) = S_OK\n", p1 );
	return S_OK;
}

//**************************************************************************
//	IPersistFileFormat
//**************************************************************************

STDMETHODIMP CDocumentData::PF_Stub3( void* p1 )
{
	CDbg::Trace( "       IPersistFileFormat::Stub3() = E_NOTIMPL\n" );
	return E_NOTIMPL;
}

STDMETHODIMP CDocumentData::PF_Stub4( void* p1 )
{
	CDbg::Trace( "       IPersistFileFormat::Stub4() = E_NOTIMPL\n" );
	return E_NOTIMPL;
}

STDMETHODIMP CDocumentData::PF_Stub5( void* p1 )
{
	CDbg::Trace( "       IPersistFileFormat::Stub5() = E_NOTIMPL\n" );
	return E_NOTIMPL;
}

STDMETHODIMP CDocumentData::PF_Stub6( void* p1, void* p2, void* p3 )
{
	CDbg::Trace( "       IPersistFileFormat::Stub6() = E_NOTIMPL\n" );
	return E_NOTIMPL;
}

STDMETHODIMP CDocumentData::PF_Save( LPCWSTR szFName, BOOL bRem, DWORD nFormIndx )
{
	CDbg::Trace( "       IPersistFileFormat::Save()\n" );
	return S_OK;
}

STDMETHODIMP CDocumentData::PF_Stub8( void* p1 )
{
	CDbg::Trace( "       IPersistFileFormat::Stub8() = S_OK\n" );
	return S_OK;
}

STDMETHODIMP CDocumentData::PF_Stub9( LPCWSTR* p1, LPCWSTR* p2 )
{
	CDbg::Trace( "       IPersistFileFormat::Stub9()\n" );
	//1st time set p2, second p1 to file path+name.
	return S_OK;
}

STDMETHODIMP CDocumentData::PF_Stub10( LPCWSTR* pszSaveFormat )
{
	CDbg::Trace( "       IPersistFileFormat::Stub10( )\n" );
	*pszSaveFormat=L"Misha Files (*.*)\r*.*";
	return S_OK;
}

HRESULT CDocumentData::ExecCommand(ULONG uCmdID )
{
	if ( m_pMdtr && m_pHist && m_pInfo )
	{
		switch( uCmdID )
		{
		case COMMAND_ID_CUT:
		case COMMAND_ID_COPY:
		case COMMAND_ID_PASTE:
		case COMMAND_ID_UNDO:
		case COMMAND_ID_REDO:
		case COMMAND_ID_DELETE:
			break;
		case COMMAND_ID_SAVE:
		case COMMAND_ID_SAVEALL:
			{
				void* pTmp = NULL; 
				return PD_SaveDocData( 0, (LPWSTR*)&pTmp, (BOOL*)&pTmp );
			}
		default:
			CDbg::Trace( "Doc::Exec unexpected command = 0x%X\n", uCmdID );
		};
		m_pHist->UserAction( ~uCmdID, NULL );
		m_pInfo->UserAction( ~uCmdID, NULL );
		m_pMdtr->UpdateView();
		m_dwCmdState = (m_pHist->GetCmdState() | m_pInfo->GetCmdState() );
		return S_OK;
	}
	return OLECMDERR_E_DISABLED;
}

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
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.

Comments and Discussions