Click here to Skip to main content
15,893,668 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 184.3K   4.9K   122  
A set of source code and project browsers to compliment Visual Studio.
/**
/*   Copyright (c) 2003by  Marco Welti
/*
/*   This document is  bound by the  QT Public License
/*   (http://www.trolltech.com/licenses/qpl.html).
/*   See License.txt for more information.
/*
/*
/*
/*   ALL RIGHTS RESERVED.  
/* 
/*   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
/*   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
/*   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
/*   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
/*   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
/*   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
/*   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
/*   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
/*   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
/*   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
/*   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/* 
/***********************************************************************/

#include "stdafx.h"
#include "Resource.h"

#include "ESBServer.h"
#include "VSWorkspaceLoader.h"

#include <Model/Simulators/HierarchyLoaderSimulator.h>
#include <Model/CTags/CTagsHierarchyLoader.h>
#include <Model/CTags/CTagsOutlineLoader.h>
#include <Renderer/MFC/Browsers/OutlineBrowserRenderer.h>

using namespace std;

void ESBServer::EditorHandler::selectionChanged(const ScopedNameBrowserItem &item)
{
  
  if(dynamic_cast<const TagbrowserItem*>(&item)) {
    const TagbrowserItem *metaObject = dynamic_cast<const TagbrowserItem*>(&item);
    if(!metaObject->getTag()->getDeclaration().getFile().empty()) {
      gotoFile(metaObject->getTag()->getDeclaration());

    } else if(!metaObject->getTag()->getDefinition().getFile().empty()) {
      gotoFile(metaObject->getTag()->getDefinition());
    }

  } else if(dynamic_cast<const WorkspaceBrowserItem*>(&item)) {    
    const WorkspaceBrowserItem *file = dynamic_cast<const WorkspaceBrowserItem*>(&item);
    gotoFile(TagLocation(file->getFile()));
  }
}

void ESBServer::EditorHandler::gotoFile(const TagLocation &location) const
{
  mOuter->getEditorBroker().getEditor(location.getFile())->goToLine(location.getLine());
}


ESBServer::ESBServer(const std::string &name)
: mName(name), mRenderer(this), mMethodBrowser("Method"), mTypeBrowser("Type") 
{  
  getFileOutlineBrowser().setRenderer(make_smart_ptr(new OutlineBrowserDialog()));
  getFileOutlineBrowser().setESBServer(this);
  getTypeOutlineBrowser().setRenderer(make_smart_ptr(new OutlineBrowserDialog()));
  getTypeOutlineBrowser().setESBServer(this);
  
  smart_ptr<EditorHandler> listener = make_smart_ptr(new EditorHandler(this));
  getFileOutlineBrowser().addSelectionListener(listener);
  getTypeOutlineBrowser().addSelectionListener(listener);
  getHierarchyBrowser().addSelectionListener(listener);

  getTypeBrowser().addSelectionListener(listener);
  getMethodBrowser().addSelectionListener(listener);
  getWorkspaceBrowser().addSelectionListener(listener);
}

ESBServer::~ESBServer()
{
  try {
    CTags::DatabaseManager::instance().removeDatabase(getWorkspace());
  } catch(std::exception &e) {
    message_box(e.what());
  }
}

void ESBServer::run() 
{ 
  mRenderer.DoModal(); 
}

void ESBServer::showTypes() 
{
 	smart_ptr<TypeBrowserModel> model(new TypeBrowserModel(getTypeInfoLoader()->load()));

  getTypeBrowser().setModel(model);
	getTypeBrowser().show();	
}

void ESBServer::showMethods()
{
 	smart_ptr<MethodBrowserModel> model(new MethodBrowserModel(getMethodLoader()->load()));

  getMethodBrowser().setModel(model);
	getMethodBrowser().show();	
}


void ESBServer::showWorkspace()
{
  smart_ptr<ScopedNameBrowserModel> model(new WorkspaceBrowserModel(*getWorkspace()));

  getWorkspaceBrowser().setModel(model);
  getWorkspaceBrowser().show();
}

void ESBServer::showFileOutline(const std::string &file, bool enforceModelReload)
{
  if(!file.empty()) {
    string x = file_path_is::pathof(file) + file_name_is::nameof(file);
    string y = file_path_is::pathof(getFileOutlineBrowser().getModelName()) + file_name_is::nameof(getFileOutlineBrowser().getModelName());
    
    bool reload = enforceModelReload || x != y;

    if(reload) {
      getFileOutlineBrowser().setModel(getFileOutlineLoader()->load(file), file);
      getFileOutlineBrowser().show();
    }  
  }
}

void ESBServer::showHierarchy(const Inheritable &type)
{
  MetaObjectCloner cloner;
  const_cast<Inheritable&>(type).acceptVisitor(&cloner);

  smart_ptr<Inheritable> clone = dynamic__cast<Inheritable>(cloner.getClone());
  
  getHierarchyBrowser().setModel(clone);
  getHierarchyBrowser().show();
}

void ESBServer::reparseFile(const std::string &file)
{
  try {
    CTags::DatabaseManager::instance().getDatabase(getWorkspace())->refreshTagsReferencingFile(file);
    showFileOutline(file, true);

  } catch(std::exception &e) {
    message_box(e.what());
  }
}

void ESBServer::openWorkspace(const Workspace& w)
{
  try {
    VisualStudioWorkspaceLoader workspaceLoader(w.getName());
    setWorkspace(workspaceLoader.load(w.getPackages()));
    
    LoaderBroker::instance(getWorkspace())->setOutlineLoader(make_smart_ptr(new CTags::OutlineLoader(getWorkspace())));
    LoaderBroker::instance(getWorkspace())->setHierarchyLoader(make_smart_ptr(new CTags::HierarchyLoader(getWorkspace())));
    
  } catch(std::exception &e) {
    message_box(e.what());
  }
}

void ESBServer::reparseWorkspace()
{
  openWorkspace(*getWorkspace());
}

smart_ptr<CppType> ESBServer::getType(const std::string &type_name)
{
  TypeInfoLoader::TypeList tl = CTags::TypeInfoLoader(getWorkspace()).load(type_name);
  smart_ptr<MetaObject> item;
  
  smart_ptr<TypeBrowserModel> model;
  if(tl.size() != 1) {
    if(tl.empty()) {
 	    model = smart_ptr<TypeBrowserModel>(new TypeBrowserModel(getTypeInfoLoader()->load()));

    } else if(tl.size() > 1) {
      model = smart_ptr<TypeBrowserModel>(new TypeBrowserModel(tl));
    }
  
    TagBrowser typeBrowser("Type");
    typeBrowser.closeOnSelection(true);
    typeBrowser.setModel(model);
    //typeBrowser.setFilter("*" + type_name + "*");

    if(!typeBrowser.show(true)) {
      return smart_ptr<CppType>();
    }
    item = dynamic_cast<TagbrowserItem*>(typeBrowser.getSelection())->getTag();

  } else {
    item = *tl.begin();
  }

  return dynamic__cast<CppType>(item);
}

void ESBServer::showOutline(const std::string &type_name)
{
 if(type_name.empty()) {
    getFileOutlineBrowser().show();
    getFileOutlineBrowser().moveToForeground();

  } else {
    smart_ptr<MetaObject> type = getType(type_name);
    if(!type.isNull()) {
      list<smart_ptr<MetaObject> > outline;
      outline.push_back(type);
    
      getTypeOutlineBrowser().setModel(outline, type_name);     
      getTypeOutlineBrowser().show();
      getTypeOutlineBrowser().moveToForeground();
    }
  }
}

void ESBServer::showHierarchy(const std::string &type_name)
{
  if(getHierarchyBrowser().getSelection() == NULL || !type_name.empty()) {
    smart_ptr<Inheritable> type = dynamic__cast<Inheritable>(getType(type_name.empty() ? string("*") : type_name));
    if(!type.isNull()) {
      showHierarchy(*type);
    }
   
  } else {
    getHierarchyBrowser().show();
    getHierarchyBrowser().getRenderer().moveToForeground();
  }
}

void ESBServer::showBrowser(const std::string &browser, const std::string &what)
{
  if(browser == "outline") {
    showOutline(what);

  } else if(browser == "hierarchy") {
    showHierarchy(what);

  } else if(browser == "types") {
    showTypes();
    if(!what.empty()) {
      getTypeBrowser().setFilter(what);
    }
    getTypeBrowser().moveToForeground();

  } else if(browser == "methods") {
    showMethods();
    if(!what.empty()) {
      getMethodBrowser().setFilter(what);
    }
    getMethodBrowser().moveToForeground();

  } else if(browser == "workspace") {
    showWorkspace();
    getWorkspaceBrowser().moveToForeground();

  } else {
    message_box("ESBServer::showBrowser():unknown browser type " + browser);
  }
}

#include "Model/Simulators/TypeInfoLoaderSimulator.h"
smart_ptr<::TypeInfoLoader> ESBServer::getTypeInfoLoader()
{
  //return smart_ptr<TypeInfoLoaderSimulator>(new TypeInfoLoaderSimulator());
  return smart_ptr<::TypeInfoLoader>(new CTags::TypeInfoLoader(getWorkspace())); 
}

smart_ptr<::MethodLoader> ESBServer::getMethodLoader()
{
  return smart_ptr<::MethodLoader>(new CTags::MethodLoader(getWorkspace()));
}

smart_ptr<::FileOutlineLoader> ESBServer::getFileOutlineLoader()
{
  //return smart_ptr<FileOutlineLoaderSimulator>(new FileOutlineLoaderSimulator());
  return smart_ptr<::FileOutlineLoader>(new CTags::FileOutlineLoader(getWorkspace()));
}

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