Click here to Skip to main content
15,881,172 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.5K   4.9K   122  
A set of source code and project browsers to compliment Visual Studio.
// ScopedNameBrowserRenderer.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"

#include "ScopedNameBrowserRenderer.h"
#include "ScopedNameBrowser.h"

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

#include <algorithm>
#include <sstream>
#include <exception>

using namespace std;


/////////////////////////////////////////////////////////////////////////////
// ScopedNameBrowserRenderer dialog



ScopedNameBrowserRenderer::ScopedNameBrowserRenderer(ScopedNameBrowser *ctrl, CWnd* pParent /*=NULL*/)
	: super(ScopedNameBrowserRenderer::IDD, pParent), mController(ctrl)
{
	//{{AFX_DATA_INIT(ScopedNameBrowserRenderer)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
  mImages = &ESBImageList::instance();
}


void ScopedNameBrowserRenderer::DoDataExchange(CDataExchange* pDX)
{
	super::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(ScopedNameBrowserRenderer)
  DDX_Control(pDX, IDC_FILTER, mFilter);
	DDX_Control(pDX, IDC_TYPES, mNameListRenderer);
	DDX_Control(pDX, IDC_QUALIFIERS, mScopeListRenderer);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(ScopedNameBrowserRenderer, super)
	//{{AFX_MSG_MAP(ScopedNameBrowserRenderer)
	ON_NOTIFY(NM_CLICK, IDC_TYPES, onNameSelectionChanged)
	ON_NOTIFY(NM_DBLCLK, IDC_TYPES, onNameSelectionChanged)
	ON_NOTIFY(NM_CLICK, IDC_QUALIFIERS, onScopeSelectionChanged)
	ON_NOTIFY(NM_DBLCLK, IDC_QUALIFIERS, onScopeSelectionChanged)
	ON_EN_CHANGE(IDC_FILTER, onFilterChanged)
	ON_BN_CLICKED(IDC_CLOSE, onClose)
  ON_WM_SHOWWINDOW()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// ScopedNameBrowserRenderer message handlers

BOOL ScopedNameBrowserRenderer::OnInitDialog() 
{
	super::OnInitDialog();


  AllowSizing(sizeResize,sizeResize);
  AddControl(IDC_CHOOSE,sizeResize,sizeNone);
  AddControl(IDC_TYPEFILTER,sizeResize,sizeNone);
	AddControl(IDC_TYPES,sizeResize,sizeResize);
  AddControl(IDC_QUALIFIER_LABEL,sizeNone,sizeRepos);
  AddControl(IDC_QUALIFIERS,sizeResize,sizeRepos);
  AddControl(IDC_CLOSE,sizeRepos,sizeRepos);
  AddControl(IDCANCEL,sizeRepos,sizeRepos);
  HideSizeIcon();

  getNameListRenderer().SetImageList(getImages(), LVSIL_SMALL);
  getNameListRenderer().InsertColumn(0, "dummy text", LVCFMT_LEFT, 300);

  getScopeListRenderer().SetImageList(getImages(), LVSIL_SMALL);
  getScopeListRenderer().InsertColumn(0, "dummy text", LVCFMT_LEFT, 300);
  getScopeListRenderer().SetColumnWidth(0, LVSCW_AUTOSIZE); 
  
  SetDlgItemText(IDC_CHOOSE, mFilterDescription.c_str());
  
  /*mSplitter.BindWithControl(this, IDC_QUALIFIER_LABEL);
  mSplitter.AttachAsAbovePane(IDC_TYPES);
  mSplitter.AttachAsBelowPane(IDC_QUALIFIERS);
  mSplitter.RecalcLayout();	*/

  setFocusableWindow(GetDlgItem(IDC_FILTER));

  return TRUE;
}

void ScopedNameBrowserRenderer::onNameSelectionChanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
  *pResult = 0;
  try {
    if(isSelected(pNMHDR)) {
      ScopedNameBrowserItem *item = getData(getNameListRenderer(), pNMHDR);
      switch(pNMHDR->code) {
        case NM_CLICK: getController().typeSelectionChanged(item); break;
        case NM_DBLCLK : getController().typeSelected(item); break;
      }
	  }
    *pResult = 0;

  } catch(std::out_of_range&) {
    //ignore..user clicked somewhere but no on the item label or iten icon
  } catch(std::exception &e) {
    AfxMessageBox((string("ScopedNameBrowserRenderer::onNameSelectionChanged():") + e.what()).c_str());

  } catch(...) {
    AfxMessageBox("ScopedNameBrowserRenderer::onNameSelectionChanged() unknown exception");
  }
}

void ScopedNameBrowserRenderer::onScopeSelectionChanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
  *pResult = 1;
  try {
    if(isSelected(pNMHDR)) {
      ScopedNameBrowserItem *item = getData(getScopeListRenderer(), pNMHDR);
      switch(pNMHDR->code) {
        case NM_CLICK: getController().qualifierSelectionChanged(item); break;
        case NM_DBLCLK : getController().typeSelected(item); break;
      }
	  }
    *pResult = 0;

  } catch(std::out_of_range&) {
    //ignore..user clicked somewhere but no on the item label or iten icon
  } catch(std::exception &e) {
    AfxMessageBox((string("ScopedNameBrowserRenderer::onScopeSelectionChanged():") + e.what()).c_str());

  } catch(...) {
    AfxMessageBox("ScopedNameBrowserRenderer::onScopeSelectionChanged() unknown exception");
  }
}

void ScopedNameBrowserRenderer::onFilterChanged() 
{
  CString text;
  GetDlgItemText(IDC_TYPEFILTER, text);

  getController().filterChanged(text.GetBuffer(0));
}

bool ScopedNameBrowserRenderer::isSelected(NMHDR *pNMHDR)
{
//	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
//
//  if ((pNMListView->uChanged & LVIF_STATE) != LVIF_STATE)
//	     false; // state has not changed
//
//  // Has the selected state changed?
//  if (!(pNMListView->uNewState & LVIS_SELECTED) || (pNMListView->uOldState & LVIS_SELECTED))
//    return false;

	return true;
}

void ScopedNameBrowserRenderer::append(CListCtrl &ctrl, const smart_ptr<ScopedNameBrowserItem> &scopedItem, bool useScope)
{
  string text = useScope ? scopedItem->getScopeLabel() : scopedItem->getNameLabel();
  int icon = useScope ? scopedItem->getScopeIcon() : scopedItem->getNameIcon();

  LV_ITEM item;
	item.mask= icon > 0 ? LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM : LVIF_PARAM | LVIF_TEXT;
	item.pszText=(char*)text.c_str();
	item.cchTextMax=1000;
	item.iSubItem=0;
  item.iItem = ctrl.GetItemCount();
	item.iImage = icon;
  item.lParam = (LPARAM)scopedItem.get();

  ctrl.InsertItem(&item);
}

ScopedNameBrowserItem* ScopedNameBrowserRenderer::getData(const CListCtrl &ctrl, NMHDR *pNMHDR)
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;	
  if(pNMListView->iItem < 0) {
    throw out_of_range("item index < 0");
  }
  return reinterpret_cast<ScopedNameBrowserItem*>(ctrl.GetItemData(pNMListView->iItem));
}

ScopedNameBrowserItem* ScopedNameBrowserRenderer::getSelectedItem()
{
	for( int i = 0; i < getNameListRenderer().GetItemCount(); i++ ) {
		if( getNameListRenderer().GetItemState( i, LVIS_SELECTED )  ) {
			return reinterpret_cast<ScopedNameBrowserItem*>(getNameListRenderer().GetItemData(i));
		}
	}
	return NULL;
}

void ScopedNameBrowserRenderer::onClose() 
{
	getController().typeSelected(getSelectedItem());
	CDialog::OnOK(); 
}

void ScopedNameBrowserRenderer::OnOK() 
{ 
	getController().typeSelected(getSelectedItem());
}

void ScopedNameBrowserRenderer::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	super::OnShowWindow(bShow, nStatus);
	onFilterChanged();
}

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