Click here to Skip to main content
15,886,798 members
Articles / Desktop Programming / MFC

Be Sweet - a set of visual source code browsers

Rate me:
Please Sign up or sign in to vote.
4.85/5 (35 votes)
1 Jul 20038 min read 183.8K   4.9K   122  
A set of source code and project browsers to compliment Visual Studio.
// ESBDSAddinImpl.cpp : Implementation of ESBDSAddinImpl
#include "stdafx.h"
#include "IESBDSAddin.h"
#include "DSAddin.h"

#include "CommandHandler.h"
#include "EventHandler.h"

#include "ComHelper.h"


#include <string>
using namespace std;

#include "ESBServer/src/ESBServer_i.c"

/////////////////////////////////////////////////////////////////////////////
// ESBDSAddinImpl

ESBDSAddinImpl::ESBDSAddinImpl() 
: mApplication(NULL), mCommandHandler(NULL), mApplicationEventHandler(NULL), 
  mCookie(0), mClassRegistrationCookie(0), mIsEnabled(false)
{
    DebugBox("ESBDSAddinImpl::ESBDSAddinImpl()");
}

HRESULT ESBDSAddinImpl::OnConnection(IApplication* application, VARIANT_BOOL isFirstTime, long dwCookie, VARIANT_BOOL* pbOnConnection)
{
  const static string function = "ESBDSAddinImpl::OnConnection(): ";
  DebugBox(function);
 
  try {
    *pbOnConnection = VARIANT_FALSE;

    setupComStuff();
    mApplication = application;
    mCookie = dwCookie;
  
    setupAddIn(isFirstTime == VARIANT_TRUE);
    
    *pbOnConnection = VARIANT_TRUE;
    return S_OK;

  } catch(std::exception &e) {
		DebugBox(function + e.what());

	} catch(_com_error &e) {
		DebugBox(function + asString(e));
		
	} catch(...) {
    DebugBox(function + " unknown error");
  }
  
  return E_FAIL;
}

HRESULT ESBDSAddinImpl::OnDisconnection(VARIANT_BOOL bLastTime)
{
  DebugBox("ESBDSAddinImpl::OnDisconnection()");
  CoRevokeClassObject(mClassRegistrationCookie);
  mApplication = NULL;
  mCommandHandler = NULL;
  mApplicationEventHandler = NULL;
  return TRUE;
}

HRESULT ESBDSAddinImpl::gotoLine(BSTR filename, long line)
{
  const static string function = "ESBDSAddinImpl::gotoLine(): ";
  DebugBox("ESBDSAddinImpl::gotoLine(): ");

  try {
    CComPtr< IDispatch> tmp = 0;

    cex_ = getApplication()->get_Documents(&tmp.p);
    CComQIPtr< IDocuments> documents = tmp;
    
    tmp = 0;
    cex_ = documents->Open( filename, CComVariant(), CComVariant(), &tmp.p);
    CComQIPtr< ITextDocument> document = tmp;
    
    tmp = 0;
    cex_ = document->get_Selection( &tmp.p);
    CComQIPtr< ITextSelection> selection = tmp;

    cex_ = selection->GoToLine(line, CComVariant(1));

  } catch(std::exception &e) {
		DebugBox(function + e.what());

	} catch(_com_error &e) {
		DebugBox(function + asString(e));
		
	} catch(...) {
    DebugBox(function + " unknown error");
  }

  return E_FAIL;
}

void ESBDSAddinImpl::setupComStuff()
{
  CComPtr<IUnknown> pIUnk = 0;
  cex_ = _Module.GetClassObject( GetObjectCLSID(), IID_IUnknown, reinterpret_cast<void**>(&pIUnk));
  cex_ = CoRegisterClassObject(GetObjectCLSID(), pIUnk, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &mClassRegistrationCookie);
}

void ESBDSAddinImpl::setupAddIn(bool isFirstTime)
{
  DebugBox("ESBDSAddinImpl::setupAddIn()");

  mCommandHandler = new_instance<CommandHandler>();
  mCommandHandler->setAddIn(this);
  
  mApplicationEventHandler = new_instance<ApplicationEventHandler>();
  mApplicationEventHandler->setAddIn(this);
  enable(false);

  //tell vc about us....
  cex_ = getApplication()->SetAddInInfo((long)_Module.GetModuleInstance(), mCommandHandler, IDR_TOOLBAR_MEDIUM, IDR_TOOLBAR_LARGE, mCookie);

	// register our commands with VC
	VARIANT_BOOL bRet;

  const size_t commandCnt = 6;
  string commands[6][3] = { 
                          {"ESBProperties", "\n\n\nEnable/Disable","setProperties"},
                          {"ESBShowOutlineBrowser", "\n\n\nShow Outline", "showOutlineBrowser"}, 
                          {"ESBShowHierarchyBrowser", "\n\n\nShow Hierarchy", "showHierarchyBrowser"}, 
                          {"ESBShowTypeBrowser", "\n\n\nShow Types", "showTypeBrowser"}, 
                          {"ESBShowMethodBrowser", "\n\n\nShow Methods", "showMethodBrowser"}, 
                          {"ESBShowWorkspaceBrowser", "\n\n\nShow Workspace", "showWorkspaceBrowser"}, 
                         };

  for(int i = 0; i < commandCnt; i++) {
    cex_ = getApplication()->AddCommand(CComBSTR((commands[i][0]+ commands[i][1]).c_str()), CComBSTR(commands[i][2].c_str()), i, mCookie, &bRet);
	  if(isFirstTime)
	  {
		  cex_ = getApplication()->AddCommandBarButton(dsGlyph, CComBSTR(commands[i][0].c_str()), mCookie);
	  }
  }

  DebugBox("... completed ESBDSAddinImpl::setupAddIn()");
}

CComPtr<IESBServer> ESBDSAddinImpl::getESBServer()
{
  CComPtr<IESBServer> esbServer;
  cex_ = esbServer.CoCreateInstance(CLSID_ESBServerCoClass);  
  return esbServer;
}

void ESBDSAddinImpl::enable(bool enable)
{
  mIsEnabled = enable;
  if(isEnabled()) {
    mApplicationEventHandler->Connect(getApplication());

  } else {
    mApplicationEventHandler->Disconnect(getApplication());
  }
}

void ESBDSAddinImpl::workspaceLoaded(const std::string &workspace, const std::string &options)
{
  getESBServer()->loadWorkspace(CComBSTR(workspace.c_str()), CComBSTR(options.c_str()));
}

void ESBDSAddinImpl::documentChanged(const std::string &filename)
{
  getESBServer()->fileContentChanged(CComBSTR(filename.c_str()));
}

void ESBDSAddinImpl::documentActivated(const std::string &filename)
{
  getESBServer()->showFileOutline(CComBSTR(filename.c_str()));
}

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

Comments and Discussions