Click here to Skip to main content
15,896,118 members
Articles / Programming Languages / C++

Useful Managers

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Aug 2001 80.6K   1.2K   16  
An add-in for Devstudio that provides tag indexing and search, window, bookmarks, session and other managers
// DlgWinMgr.cpp : implementation file
// COPYRIGHT (C) 1999-2000 by Mike Melnikov zmike@andnow.ru
//

#include "stdafx.h"

#include "Commands.h"
#include "BookMarksMgr.h"
#include "Utils.h"
#include "Registry.h"
#include "tags\read.h"

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


class BRegValueIterator : public RegValueIterator 
{
public:
  BRegValueIterator() : 
      m_key(AfxGetApp()->GetAppRegistryKey(),RegBookMarks+CCommands::CurrentWorkSpace),
      RegValueIterator(m_key){}
private:
  RegKey m_key;
};

struct BookMark
{
  BookMark(const RegValue& val);
  operator bool()    {return m_enable;}
  operator int()     {return m_idx;}
  int line()         {return m_line;}
  operator CTime()   {return m_time;}
  operator CString() {return m_file;}
  operator char*()   {return m_str.get();}
 private:
   bool m_enable;
   int m_idx;
   int m_line;
   CTime m_time;
   CString m_file;
   auto_ptr<char> m_str;
};

BookMark::BookMark(const RegValue& val)
{
  m_idx = atoi(val.GetName());
  if((m_enable = val.GetDataSize()>0))
  {
    unsigned char* buffer = const_cast<unsigned char*>(val.GetData());
    m_line = *(WORD*)(buffer+2);
    m_time = *((CTime*)(buffer+4));
    buffer[10+buffer[8]] = 0;
    m_file = ((char*)(buffer+10));
    if(fileOpen(m_file))
    {
      m_str = auto_ptr<char>(readLine(m_line));
      fileClose();
    }
  }
}

/////////////////////////////////////////////////////////////////////////////
// CDlgBookMgr dialog
CDlgBookMgr::CDlgBookMgr()
{
  avalibleCommands = AllCommnads2;
}            


void CDlgBookMgr::OnInit()
{
  m_ctrl.makeColumns("ID","Time","File","Line","Source",0);
  m_ctrl.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE,0,(LPARAM)LVS_EX_FULLROWSELECT);
}

void CDlgBookMgr::FillList()
{
	m_ctrl.DeleteAllItems();
  // format
  // 0-1  - ver
  // 2-3  - line  
  // 4-7  - time
  // 8-9   - size of file
  // 10-.. file 

  int max = FindMax();
  int count = max(10,max);
  if(GetCount()==FindEmpty())
    count++;

  for(int i=0;i<count;i++)
    m_ctrl.InsertItem(i,itoa(i),i);

  for(BRegValueIterator it;it;it++)
  {
    RegValue val(it);
    BookMark bookMark(val);
    if(bookMark)
    {
     	m_ctrl.SetItemText(bookMark,COL_LINE, itoa(bookMark.line()));
    	m_ctrl.SetItemText(bookMark,COL_DATA, CTime(bookMark).Format(_T("%H:%M:%S %d")));
     	m_ctrl.SetItemText(bookMark,COL_FILE, CString(bookMark));
      m_ctrl.SetItemText(bookMark,COL_CODE, (char*)bookMark);
    }
  }
  m_ctrl.SetWidths();
}

int CDlgBookMgr::Sort(LPARAM lParam1, LPARAM lParam2)
{
	int iColumn = abs(m_ctrl.m_iSortedCol)-1; // iColumn here is "1 based" so we don't get -0
	int iComp = 0;
  RegKey rootKey(AfxGetApp()->GetAppRegistryKey(),RegBookMarks+CCommands::CurrentWorkSpace);
  RegValue val1(rootKey,itoa((int)lParam1));
  BookMark bookMark1(val1);
  RegValue val2(rootKey,itoa((int)lParam2));
  BookMark bookMark2(val2);
  if(iColumn==COL_ID)
	{
		iComp = (int)bookMark1 - (int)bookMark2;
    if(m_ctrl.m_iSortedCol < 0)
		  iComp = -iComp; // reverse sorting sense.
  }
  else 
  {
    if(!bool(bookMark1) && !bool(bookMark2))
      iComp = 0;
    else if(!bool(bookMark1))
      iComp = 1;
    else if(!bool(bookMark2))
      iComp = -1;
    else
    {
      if(iColumn==COL_DATA)
      {
		    iComp = CTime(bookMark1) > CTime(bookMark2) ? 1 : ( CTime(bookMark1) == CTime(bookMark2) ? 0 : -1);
      }
      else if(iColumn==COL_FILE)
      {
		    iComp = CString(bookMark1).CompareNoCase(CString(bookMark2));
      }
      else if(iColumn==COL_LINE)
      {
		    iComp = bookMark1.line() - bookMark2.line();
	    }
      else if(iColumn==COL_CODE)
      {
		    iComp = CString((char*)bookMark1).CompareNoCase((char*)bookMark2);
	    }
    	if(m_ctrl.m_iSortedCol < 0)
		    iComp = -iComp; // reverse sorting sense.
    }
  }
	return iComp;
}

void CDlgBookMgr::OnOpenFile(const RegValue& val)
{
  BookMark bookMark(val);
  if(bookMark) // open
  {
	  CCommands::Get()->SimpleOpenDoc(CString(bookMark),bookMark.line());
  }
  else // close
  {
    push(bookMark);
    CCommands::Get()->RefreshBookMarks();
  }
}

void CDlgBookMgr::RefreshFile(CString f)
{
  for(BRegValueIterator it;it;it++)
  {
    RegValue val(it);
    BookMark bookMark(val);
    if(bool(bookMark) && CString(bookMark)==f)
    {
      CCommands::Get()->SetBookmark(bookMark.line());
    }
  }
}

void CDlgBookMgr::RemoveAllFromFile(CString f)
{
  for(BRegValueIterator it;it;it++)
  {
    RegValue val(it);
    BookMark bookMark(val);
    if(bool(bookMark) && CString(bookMark)==f)
    {
      val.Delete();
    }
  }
}


void CDlgBookMgr::push(int idx)
{
  CCommands::Get()->SetBookmark();

  string activeName = CCommands::Get()->GetCurrentFile();
  if(activeName.size()==0)
    return;

  unsigned char* buffer = new unsigned char[1000];
  buffer[0] = 0;
  *((WORD*)(buffer+2)) = CCommands::Get()->GetCurrentLine();
  *((CTime*)(buffer+4)) = CTime::GetCurrentTime();
  *((WORD*)(buffer+8)) = activeName.size();
  memcpy(buffer+10,activeName.c_str(),activeName.size());

  RegKey rootKey(AfxGetApp()->GetAppRegistryKey(),RegBookMarks+CCommands::CurrentWorkSpace);
  RegValue val(rootKey,itoa(idx));
  val.Set(REG_BINARY,buffer,1000);

  delete buffer;
}

void CDlgBookMgr::Push()
{
  push(FindEmpty());
}

void CDlgBookMgr::DoDel(int idx,bool openFile)
{
  RegKey rootKey(AfxGetApp()->GetAppRegistryKey(),RegBookMarks+CCommands::CurrentWorkSpace);
  RegValue val(rootKey,itoa(idx));
  BookMark bookMark(val);
  if(openFile)
    OnOpenFile(val);

  CCommands::Get()->ClearBookmark(CString(bookMark),bookMark.line());

  val.Delete();
}

void CDlgBookMgr::Pop()
{
  if(GetCount()==0)
    return;
  DoDel(FindLast(),true);
  CCommands::Get()->RefreshBookMarks();
}

void CDlgBookMgr::OnDel()
{
	for( CListSelIterator iter(m_ctrl);iter;)
	{
    DoDel(m_ctrl.GetItemData(iter),false);
  }
  OnRefresh();
  Update();
}

void CDlgBookMgr::OnInsert()
{
  Push();
  OnRefresh();
  Update();
}

void CDlgBookMgr::OnSelect(CMListCtrl& ctrl)
{
	for( CListSelIterator iter(ctrl);iter;)
	{
    RegKey rootKey(AfxGetApp()->GetAppRegistryKey(),RegBookMarks+CCommands::CurrentWorkSpace);
    RegValue val(rootKey,itoa(ctrl.GetItemData(iter)));
    OnOpenFile(val);
	}
  CDlgMgr::OnSelect(ctrl);
}

int CDlgBookMgr::FindMax()
{
  int count = -1;
  for(BRegValueIterator it;it;it++)
  {
    RegValue val(it);
    count = max(count,atoi(val.GetName()));
  }
  return count+1;
}

int CDlgBookMgr::GetCount()
{
  int count = 0;
  for(BRegValueIterator it;it;it++)
    count++;
  return count;
}

int CDlgBookMgr::FindLast()
{
  int max = 0;
  CTime timeMax;
  for(BRegValueIterator it;it;it++)
  {
    RegValue val(it);
    BookMark bookMark(val);
    if(max==0 || (bool(bookMark) && CTime(bookMark) > timeMax))
    {
      timeMax = bookMark;
      max = bookMark;
    }
  }
  return max;
}
                   
int CDlgBookMgr::FindEmpty()
{
  for(BRegValueIterator it;it;it++)
  {
    RegValue val(it);
    BookMark bookMark(val);
    if(!bool(val))
      return bookMark;
  }
  return FindMax();
}

//    CMemFile file(const_cast<unsigned char *>(val.GetData()),val.GetDataSize());
//    CArchive archive(&file,CArchive::load);

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
Russian Federation Russian Federation
Mike has been programming in C/C++ for 11 years and Visual C++/MFC for 4 years. His background includes pure and applied mathematics, engineering and physics, and he is currently based in Moscow, Russia.

Comments and Discussions