Click here to Skip to main content
15,891,529 members
Articles / Programming Languages / C++

AutoText Add-in for Visual Studio 6.0

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
3 Mar 2001 115.8K   1.5K   39  
Adds the auto text feature from MS Word (tm) to the Developer Studio (tm)
// Commands.cpp : implementation file
//

#include "stdafx.h"
#include "DeveloperStudioAutoText.h"
#include "Commands.h"
#include "DSAddIn.h"
#include "SettingsDialog.h"
#include "CtlTools.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCommands

CCommands::CCommands()
{
	m_pApplication = NULL;
	m_pApplicationEventsObj = NULL;

   m_pAddIn = NULL;

   m_bEnableSystem = TRUE;

   ReadIniSection( "Substitutions", "DSAutoText.CFG", m_strmapAutoText );
}

CCommands::~CCommands()
{
	ASSERT (m_pApplication != NULL);
	m_pApplication->Release();
}

void CCommands::SetApplicationObject(IApplication* pApplication)
{
	// This function assumes pApplication has already been AddRef'd
	//  for us, which CDSAddIn did in its QueryInterface call
	//  just before it called us.
	m_pApplication = pApplication;

	// Create Application event handlers
	XApplicationEventsObj::CreateInstance(&m_pApplicationEventsObj);
	m_pApplicationEventsObj->AddRef();
	m_pApplicationEventsObj->Connect(m_pApplication);
	m_pApplicationEventsObj->m_pCommands = this;

   //Fake window activate, so that after initalisation
   //the active window will be subclassed
   m_pApplicationEventsObj->WindowActivate( NULL );
}

void CCommands::UnadviseFromEvents()
{
	ASSERT (m_pApplicationEventsObj != NULL);
	m_pApplicationEventsObj->Disconnect(m_pApplication);
	m_pApplicationEventsObj->Release();
	m_pApplicationEventsObj = NULL;
}

void CCommands::SetAddIn( CDSAddIn* pAddIn )
{
   m_pAddIn = pAddIn;
}


/////////////////////////////////////////////////////////////////////////////
// Event handlers

// TODO: Fill out the implementation for those events you wish handle
//  Use m_pCommands->GetApplicationObject() to access the Developer
//  Studio Application object

// Application events

HRESULT CCommands::XApplicationEvents::BeforeBuildStart()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::BuildFinish(long nNumErrors, long nNumWarnings)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::BeforeApplicationShutDown()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::DocumentOpen(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::BeforeDocumentClose(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::DocumentSave(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::NewDocument(IDispatch* theDocument)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WindowActivate(IDispatch* theWindow)
{
   IDispatch*  pIDocument;
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

   if( m_pCommands->GetAddIn() )
   {
      m_pCommands->GetApplicationObject()->get_ActiveDocument( &pIDocument );
      m_pCommands->GetAddIn()->SetMDIWindowActive( theWindow, pIDocument, TRUE );
      if( pIDocument )
      {
         pIDocument->Release();
      }
   }

	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WindowDeactivate(IDispatch* theWindow)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WorkspaceOpen()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WorkspaceClose()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::NewWorkspace()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return S_OK;
}


/////////////////////////////////////////////////////////////////////////////
// CCommands methods

STDMETHODIMP CCommands::DeveloperStudioAutoTextCommandMethod() 
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

   SettingsDialog Dlg;

	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));

   Dlg.m_bEnableAutoReplace = m_bEnableSystem;
   Dlg.m_strNewAutoText = m_strLastTypedWord;

   if( Dlg.DoModal() == IDOK )
   {
   	m_bEnableSystem = Dlg.m_bEnableAutoReplace;
      m_strLastTypedWord.Empty();
   }

   //neu lesen
   ReadIniSection( "Substitutions", "DSAutoText.CFG", m_strmapAutoText );

	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));

	return S_OK;
}

BOOL FileExists( const CString& strFileName )
{
   CFileStatus FileStatus;
   BOOL        bErg = FALSE;

   if( CFile::GetStatus( strFileName, FileStatus ) )
   {
      bErg = TRUE;
   }

   return bErg;
}

#ifndef RELEASE
#define RELEASE( x ) \
   if( x ) \
      x->Release(); \
      x = NULL;
#endif

STDMETHODIMP CCommands::DeveloperStudioOpenHeaderCommandMethod() 
{
   IDispatch*                    pDisp = NULL;;
   CComQIPtr<IDocuments>         spIDocuments;
   CString                       strHeaderName;
   IDispatch*                    pActiveDocument = NULL;;
   CComQIPtr<IGenericDocument>   spActiveDocument;
   CComBSTR                      bstrFullName;
   CComVariant                   varEmpty( DISP_E_PARAMNOTFOUND,VT_ERROR );
   CComVariant                   varFALSE( 0, VT_I4 );
   CString                       strExt;
   CComQIPtr<ITextDocument>      spTextDocument;
   IDispatch*                    pdispSelection = NULL;
   IDispatch*                    pINewDoc = NULL;
   CComQIPtr<ITextSelection>     spSelection;
   long lStartZeile;
   long lStartSpalte;

	AFX_MANAGE_STATE(AfxGetStaticModuleState());
   
   m_pApplication->get_ActiveDocument( &pActiveDocument );
   spActiveDocument = pActiveDocument;
   RELEASE( pActiveDocument );

   spTextDocument = spActiveDocument;

   spTextDocument->get_Selection( &pdispSelection );
   spSelection = pdispSelection;
   RELEASE( pdispSelection );

   spSelection->get_CurrentLine( &lStartZeile );
   spSelection->get_CurrentColumn( &lStartSpalte );
   spSelection->MoveTo( 1, 1, CComVariant( "dsSelect") );

   spActiveDocument->get_FullName( &bstrFullName );
   strHeaderName = bstrFullName;

   if( strHeaderName.Right(2).CompareNoCase( ".H" )==0 )
   {
      strHeaderName = strHeaderName.Left( strHeaderName.GetLength()-1 ) + "C";
      if( !FileExists( strHeaderName ) )
      {
         strHeaderName = strHeaderName+"PP";
      }
   }
   else if( strHeaderName.Right(4).CompareNoCase( ".CPP" )==0 )
   {
      strHeaderName = strHeaderName.Left( strHeaderName.GetLength()-3 ) + "H";
   }
   else if( strHeaderName.Right(2).CompareNoCase( ".C" )==0)
   {
      strHeaderName = strHeaderName.Left( strHeaderName.GetLength()-1 ) + "H";
   }

   if( !FileExists( strHeaderName ) )
   {
      strHeaderName = "";
   }

   if( !strHeaderName.IsEmpty() )
   {
      m_pApplication->get_Documents( &pDisp );
      spIDocuments = pDisp;
      RELEASE( pDisp );
      spIDocuments->Open( CComBSTR(strHeaderName), varEmpty, varFALSE, &pINewDoc );
      RELEASE( pINewDoc );
   }

   return S_OK;
}

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
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions