Introduction
JesCopyRightWriter
is a simple DevStudio Add-in to demonstrate ITextDocument
and ITextSelection
Text Object Model(TOM) objects. This Add-in inserts a simple copy-right information file header in an opened file of VisualStudio 6.0 (SP5).
Background
When I decided to submit some articles to CodeProject, I thought that it would be nice if I have an Add-in of my own that will insert file headers with copy-right information. When I searched for help on the internet, I found a lot of articles written in Visual Basic, C# etc., but not many in VC++. However, I got some ideas about TOM and its interfaces (Thanks also to Derek Lakin for his article WinDiff Visual Studio Add-in). After continuous attempts, I could write some working lines of code.
Using the Tool/Code
To configure the Add-in, follow the steps below:
- Download and save Add-in in local drive.
- Open Visual Studio.
- Select Tools -> Customize menu.
- Select Add-ins and Macro Files tab.
- Browse and select Add-in DLL and click on close button.
- Click on newly appeared toolbar button. This will add file header to presently opened file.
The code-snippet for retrieving presently open document name and adding new lines to this file is shown below:
STDMETHODIMP CCommands::JesCopyRightWriterCommandMethod()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
VERIFY_OK( m_pApplication->EnableModeless( VARIANT_FALSE ));
CComPtr<IDispatch> pIActiveDocDisp;
m_pApplication->get_ActiveDocument( &pIActiveDocDisp );
CComQIPtr<ITextDocument, &IID_ITextDocument> pActiveSrc( pIActiveDocDisp );
if( pActiveSrc )
{
CComPtr<IDispatch> pITextSelDisp;
if( SUCCEEDED( pActiveSrc->get_Selection( &pITextSelDisp )))
{
SYSTEMTIME stSysTime;
GetSystemTime( &stSysTime );
const int NUM_COPY_RIGHT_INFO_LINES = 7;
CString csCopyRightInfoLines[ NUM_COPY_RIGHT_INFO_LINES ];
csCopyRightInfoLines[ 0 ] = _T( "/*\n" );
csCopyRightInfoLines[ 1 ].Format
( _T( " * Copy-Right(c) %d Jellow T. K., Refer CPOL Clauses.\n" ),
stSysTime.wYear );
csCopyRightInfoLines[ 2 ] = _T( " *\n" );
CComBSTR bstrSrcFile;
pActiveSrc->get_Name( &bstrSrcFile );
CString csSrcFileName( bstrSrcFile );
csCopyRightInfoLines[ 3 ].Format( _T( " * %s - TODO :
Add file description here.\n" ),
csSrcFileName );
csCopyRightInfoLines[ 4 ] = _T( " *\n" );
csCopyRightInfoLines[ 5 ].Format( _T( " * @version: 1.0
Date: %2d-%2d-%4d\n" ),
stSysTime.wDay, stSysTime.wMonth, stSysTime.wYear );
csCopyRightInfoLines[ 6 ] = _T( " */\n" );
CComQIPtr<ITextSelection, &IID_ITextSelection> pITextSel( pITextSelDisp );
pITextSel->StartOfDocument( CComVariant( 0 ));
pITextSel->NewLine( CComVariant( 0 ));
pITextSel->NewLine( CComVariant( 0 ));
pITextSel->StartOfDocument( CComVariant( 0 ));
for( int nIdx = 0; nIdx < NUM_COPY_RIGHT_INFO_LINES; ++nIdx )
{
CComBSTR bstrCopyRightLineInfo = csCopyRightInfoLines[ nIdx ];
pITextSel->put_Text( bstrCopyRightLineInfo );
}
}
}
else
{
AfxMessageBox( _T( "Could not find open source file..." ));
}
VERIFY_OK( m_pApplication->EnableModeless( VARIANT_TRUE ));
return S_OK;
}
Points of Interest
While I checked for help of ITextDocument
and ITextSelection
in MSDN, I found that tom.h is needed for compilation. But when I included this header file, I got multiple or redefinition compilation-error. These interface definitions were already present in \ObjModel\TextAuto.h of VC98 standard header files. But I was not able to find some of the documented methods in this header file (\ObjModel\TextAuto.h). I don't know the exact reason.
History
- Version 1.0 - 11-Mar-2009