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

VS File Finder

Rate me:
Please Sign up or sign in to vote.
4.90/5 (39 votes)
8 May 20053 min read 222.6K   3.3K   37  
A Visual Studio add-in to help navigate around large projects.
#include "stdafx.h"
#include "AddIn.h"
#include "Connect.h"

extern CAddInModule _AtlModule;


CConnect::CConnect()
	: m_strCmdName(L"VSFileFinder.Connect.ShowVSFileFinder")
    , m_strShortCmdName(L"ShowVSFileFinder")
{
}

STDMETHODIMP CConnect::OnConnection(IDispatch *pApplication, AddInDesignerObjects::ext_ConnectMode /*ConnectMode*/, IDispatch *pAddInInst, SAFEARRAY ** /*custom*/ )
{
	pApplication->QueryInterface(__uuidof(EnvDTE::_DTE), (LPVOID*)&m_pDTE);
	pAddInInst->QueryInterface(__uuidof(EnvDTE::AddIn), (LPVOID*)&m_pAddInInstance);

	if (!m_pDTE || !m_pAddInInstance)
		return E_FAIL;

	AddMenuItem();
	CreateToolWindow();

	return S_OK;
}

STDMETHODIMP CConnect::OnDisconnection(AddInDesignerObjects::ext_DisconnectMode /*RemoveMode*/, SAFEARRAY ** /*custom*/ )
{
	if (m_pDTE && m_pControl)
		SetControlInterface(NULL);

	m_pDTE = NULL;

	m_pControl = NULL;
	m_pWindow = NULL;
	
	return S_OK;
}

STDMETHODIMP CConnect::OnAddInsUpdate (SAFEARRAY ** /*custom*/ )
{
	return S_OK;
}

STDMETHODIMP CConnect::OnStartupComplete (SAFEARRAY ** /*custom*/ )
{
	m_pWindow->put_Visible(VARIANT_TRUE);
	return S_OK;
}

STDMETHODIMP CConnect::OnBeginShutdown (SAFEARRAY ** /*custom*/ )
{
	return S_OK;
}

void CConnect::CreateToolWindow()
{
	CComPtr<EnvDTE::Windows> pWindows;
	if (FAILED(m_pDTE->get_Windows(&pWindows))) return;

	CComBSTR control(_T("VSFileFinder.VSFileFinderCtrl.1"));
	CComBSTR caption(_T("VSFileFinder"));
	CComBSTR random(_T("{BAB96FF1-C0BB-42de-9299-868DA1BC5C76}"));

	if (FAILED(pWindows->CreateToolWindow(m_pAddInInstance, control, caption, random, &m_pControl, &m_pWindow)) || !m_pWindow) return;

	m_pWindow->put_Visible(VARIANT_TRUE);

	CComPtr<IDispatch> pDispatch;
	m_pDTE->QueryInterface(&pDispatch);
	SetControlInterface(pDispatch);
}

void CConnect::SetControlInterface(CComPtr<IDispatch> pDispatch)
{
	if (!m_pControl)
		return;

	DISPID id = 0;
	if (FAILED(m_pControl->GetIDsOfNames(IID_NULL, &CComBSTR(_T("SetInterface")), 1, LOCALE_SYSTEM_DEFAULT, &id))) return;

	CComVariant vt;
	vt.pdispVal = pDispatch;
	vt.vt = VT_DISPATCH;

	DISPPARAMS params;
	params.rgvarg = &vt;
	params.rgdispidNamedArgs = NULL;
	params.cArgs = 1;
	params.cNamedArgs = 0;

	CComVariant result;
	m_pControl->Invoke(id, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &params, &result, NULL, NULL);
}

STDMETHODIMP CConnect::QueryStatus(BSTR bstrCmdName, EnvDTE::vsCommandStatusTextWanted NeededText, EnvDTE::vsCommandStatus* pStatusOption, VARIANT* /*pvarCommandText*/)
{
	if(NeededText == EnvDTE::vsCommandStatusTextWantedNone)
	{
		if(_wcsicmp(bstrCmdName, m_strCmdName) == 0)
		{
			*pStatusOption = (EnvDTE::vsCommandStatus)(EnvDTE::vsCommandStatusEnabled|EnvDTE::vsCommandStatusSupported);
		}
	}
	return S_OK;
}

STDMETHODIMP CConnect::Exec(BSTR bstrCmdName, EnvDTE::vsCommandExecOption ExecuteOption, VARIANT * /*pvarVariantIn*/, VARIANT * /*pvarVariantOut*/, VARIANT_BOOL *pvbHandled)
{
	*pvbHandled = VARIANT_FALSE;
	if(ExecuteOption == EnvDTE::vsCommandExecOptionDoDefault)
	{
		if(!_wcsicmp(bstrCmdName, m_strCmdName))
		{
			*pvbHandled = VARIANT_TRUE;

			if (m_pWindow)
				m_pWindow->put_Visible(VARIANT_TRUE);
			return S_OK;
		}
	}
	return S_OK;
}

void CConnect::AddMenuItem()
{
	RemoveMenuItem();

	CComPtr<EnvDTE::Commands> pCommands;
	if (FAILED(m_pDTE->get_Commands(&pCommands)) || !pCommands) return;

	CComPtr<EnvDTE::Command> pOurCommand;
	HRESULT hr = pCommands->AddNamedCommand(
		m_pAddInInstance,
		CComBSTR(m_strShortCmdName),
		CComBSTR(L"VSFileFinder"),
		CComBSTR(L"Show File Finder window"),
		VARIANT_TRUE,
		59,
		NULL,
		EnvDTE::vsCommandStatusSupported|EnvDTE::vsCommandStatusEnabled,
		&pOurCommand
	);
	if (FAILED(hr) || !pOurCommand) return;

	CComPtr<Office::_CommandBars> pCommandBars;
	if (FAILED(m_pDTE->get_CommandBars(&pCommandBars)) || !pCommandBars) return;

	CComPtr<Office::CommandBar> pMenuBar;
	if (FAILED(pCommandBars->get_Item(CComVariant(L"MenuBar"), &pMenuBar)) || !pMenuBar) return;

	CComPtr<Office::CommandBarControls> pMenuBarControls;
	if (FAILED(pMenuBar->get_Controls(&pMenuBarControls)) || !pMenuBarControls) return;

	CComPtr<Office::CommandBarControl> pViewMenuControl;
	if (FAILED(pMenuBarControls->get_Item(CComVariant(L"View"), &pViewMenuControl)) || !pViewMenuControl) return;

	CComPtr<Office::CommandBarPopup> pViewMenuCommandBarPopup;
	if (FAILED(pViewMenuControl->QueryInterface(&pViewMenuCommandBarPopup)) || !pViewMenuCommandBarPopup) return;

	CComPtr<Office::CommandBar> pViewMenuCommandBar;
	if (FAILED(pViewMenuCommandBarPopup->get_CommandBar(&pViewMenuCommandBar)) || !pViewMenuCommandBar) return;

	const int i = GetMenuIndex(pViewMenuCommandBar);

	CComPtr<Office::CommandBarControl> pCommandBarControl;
	if (FAILED(pOurCommand->AddControl(pViewMenuCommandBar, i, &pCommandBarControl)) || !pCommandBarControl) return;
}

void CConnect::RemoveMenuItem()
{
	CComPtr<EnvDTE::Commands> pCommands;
	if (FAILED(m_pDTE->get_Commands(&pCommands))) return;

	CComPtr<EnvDTE::Command> pOurCommand;
	CComVariant name(m_strCmdName);
	if (FAILED(pCommands->Item(name, 0, &pOurCommand)) || !pOurCommand) return;
	pOurCommand->Delete();
}

int CConnect::GetMenuIndex(CComPtr<Office::CommandBar> pMenu)
{
	CComPtr<Office::CommandBarControls> pControls;
	if (FAILED(pMenu->get_Controls(&pControls)) || !pControls) return 1;

	CComPtr<Office::CommandBarControl> pClassView;
	if (FAILED(pControls->get_Item(CComVariant(L"Cl&ass View"), &pClassView)) || !pClassView) return 1;

	int index = 0;
	if (FAILED(pClassView->get_Index(&index))) return 1;

	return index;
}

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

Comments and Discussions