// TreeBar.cpp : implementation file
//

#include "stdafx.h"
#include "INKS.h"
#include "TreeBar.h"
#include "ElementContainer.h"
#include "CommonObject.h"
#include "INKSDoc.h"
#include "Tag.h"
CTreeItem::CTreeItem(CString strNodeName, CString strDisplay, CString strOrigin, HTREEITEM hItem, CTreeItem *pParent, int nType)
	: m_strNodeName(strNodeName), m_strDisplayName(strDisplay), m_strOrigin(strOrigin), m_hItem(hItem), m_pParent(pParent), m_nType(nType), m_bNeedUpdate(true), m_pDoc(NULL), m_pView(NULL), m_bFileInProject(false), m_bItemOK(false)
{

}

CTreeItem::~CTreeItem()
{
	CTreeItem *pTempItem = NULL;
	POSITION pos = m_mapContent.GetStartPosition();
	CString strTemp;
	while(pos){
		m_mapContent.GetNextAssoc(pos, strTemp, pTempItem);
		SAFE_DELETE(pTempItem);
	}
}

// CTreeBar dialog

IMPLEMENT_DYNAMIC(CTreeBar, CDialogBar)

CTreeBar::CTreeBar(CWnd* pParent /*=NULL*/)
: CDialogBar() // CTreeBar::IDD, pParent
{
	m_ImgList.Create(IDB_TREEBITMAP, 16, 1, RGB(255,255,255));
}

CTreeBar::~CTreeBar()
{
	CTreeItem *pTempItem = NULL;
	POSITION pos = m_mapContent.GetStartPosition();
	CString strTemp;
	while(pos){
		m_mapContent.GetNextAssoc(pos, strTemp, pTempItem);
		SAFE_DELETE(pTempItem);
	}
}

BOOL CTreeBar::Create(CWnd *pParentWnd, UINT nIDTemplate, UINT nStyle, UINT nID)
{
	BOOL bRet = CDialogBar::Create(pParentWnd, nIDTemplate, nStyle, nID);
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	m_pSchemeTree->SetImageList(&m_ImgList, TVSIL_NORMAL);
	
	return bRet;
}


void CTreeBar::DoDataExchange(CDataExchange* pDX)
{
	CDialogBar::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CTreeBar, CDialogBar)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, &CTreeBar::OnTvnSelchangedTree1)
	ON_NOTIFY(TVN_ITEMEXPANDING, IDC_TREE1, &CTreeBar::OnTvnItemexpandingTree1)
	ON_NOTIFY(NM_DBLCLK, IDC_TREE1, &CTreeBar::OnNMDblclkTree1)
	ON_NOTIFY(NM_RCLICK, IDC_TREE1, &CTreeBar::OnNMRclickTree1)
	ON_COMMAND(ID_PROJECTTYPE_CREATENEWSCHEME, &CTreeBar::OnProjecttypeCreatenewscheme)
	ON_COMMAND(ID_SCHEMETYPE_COLLAPSEINTOBLOCK, &CTreeBar::OnSchemetypeCollapseintoblock)
	ON_COMMAND(ID_SCHEMETYPE_DELETE, &CTreeBar::OnSchemetypeDelete)
END_MESSAGE_MAP()

CTreeItem* CTreeBar::AddTreeItem(CString strName, CString strOrigin, CTreeItem *sParent, int nType, bool bFileInProject, CString strAppendDisplayName, bool bCreatedInSession)
{
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	HTREEITEM hItem = NULL;
	CTreeItem *pNewItem = NULL;
	CString strDisplay = strName;
	if(strName != strOrigin){
		strDisplay.Append(_T(" ("));
		strDisplay.Append(strOrigin);
		strDisplay.Append(_T(")"));
	}
	if(strAppendDisplayName.GetLength()){
		strDisplay.Append(strName);
	}
	if(sParent){
		hItem = m_pSchemeTree->InsertItem(strDisplay, sParent->m_hItem, TVI_LAST);
		pNewItem = new CTreeItem(strName, strDisplay, strOrigin, hItem, sParent, nType);
		sParent->m_mapContent[strName] = pNewItem;
	}
	else{
		hItem = m_pSchemeTree->InsertItem(strDisplay, TVI_ROOT);
		pNewItem = new CTreeItem(strName, strDisplay, strOrigin, hItem, sParent, nType);
		m_mapContent[strName] = pNewItem;
		pNewItem->SetNeedUpdate(true);
	}
	pNewItem->m_bFileInProject = bFileInProject;
	pNewItem->m_bItemOK = CheckItem(pNewItem);
	pNewItem->m_bModified = bCreatedInSession;
	AddDummyChild(pNewItem);
	return pNewItem;
}

bool CTreeBar::CheckItem(CTreeItem *pItem)
{
	if(pItem->m_nType == 0 || pItem->m_nType == 4){ // if this is project file || wire it surely is ok
		//pItem->SetNeedUpdate(false);
		return true;
	}
	if(theApp.m_pElementCont->CanContainElement(pItem->m_strOrigin)){
		m_pSchemeTree->SetItemImage(pItem->m_hItem, 0, 0);
		return true;
	}

	pItem->m_pParent->SetNeedUpdate(true);
	m_pSchemeTree->SetItemImage(pItem->m_hItem, 1, 1);
	CString msg;
	msg = _T("Couldn't find file origin for ");
	msg.Append(pItem->m_strNodeName);
	MessageBox(msg, _T("Warning"), 0);
	return true;
}

void CTreeBar::AddDummyChild(CTreeItem *pParent)
{
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	HTREEITEM hItem = NULL;
	CTreeItem *pNewItem = NULL;
	if(pParent){
		hItem = m_pSchemeTree->InsertItem(_T("dummy"), pParent->m_hItem, TVI_FIRST);
		pNewItem = new CTreeItem(_T("dummy"), _T("dummy"), _T("dummy"), hItem, pParent);
		pNewItem->m_bModified = false;
		pParent->m_mapContent[_T("dummy")] = pNewItem;
	}
}

CTreeItem* CTreeBar::GetTreeItem(HTREEITEM hItem)
{
	CTreeItem *pItem = NULL;
	HTREEITEM hTemp = m_pSchemeTree->GetParentItem(hItem);
	HTREEITEM hh = NULL;
	if(!hTemp){
		POSITION pos = m_mapContent.GetStartPosition();
		CTreeItem *pTempItem = NULL;
		CString strTemp;
		while(pos){
			m_mapContent.GetNextAssoc(pos, strTemp, pTempItem);
			if(pTempItem->m_hItem == hItem)
				return pTempItem;
		}
		return NULL;
	}
	pItem = GetTreeItem(hTemp);
	if(!pItem)
		return NULL;

	POSITION pos = pItem->m_mapContent.GetStartPosition();
	CTreeItem *pTempItem = NULL;
	CString strTemp;
	while(pos){
		pItem->m_mapContent.GetNextAssoc(pos, strTemp, pTempItem);
		if(pTempItem->m_hItem == hItem)
			return pTempItem;
	}
	return NULL;
}

CTreeItem* CTreeBar::GetTreeItem(CString strItem, CTreeItem *pParent)
{
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	POSITION pos;
	CTreeItem *pTempItem = NULL;
	CString strTemp;
	if(!pParent){
		pos = m_mapContent.GetStartPosition();
		if(pos){
			m_mapContent.GetNextAssoc(pos, strTemp, pTempItem);
			return pTempItem;
		}
	}
	else {
		if(pParent->m_strNodeName == strItem)
			return pParent;
	}
	pos = pParent->m_mapContent.GetStartPosition();
	CTreeItem *pRet = NULL;
	while(pos){
		pParent->m_mapContent.GetNextAssoc(pos, strTemp, pTempItem);
		if(pTempItem->m_strNodeName == strItem){
			return pTempItem;
		}
		else{
			pRet = GetTreeItem(strItem, pTempItem);
		}
		if(pRet){
			return pRet;
		}
	}
	return NULL;
}

void CTreeBar::OnTvnSelchangedTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
	*pResult = 0;
}

void CTreeBar::PopulateItem(CTreeItem *pItem)
{
	CString apppath;
	if(pItem->m_bFileInProject){
		apppath = m_strProjectPath;
	}
	else{
		apppath = m_strAppPath;
	}
	int backsl = apppath.ReverseFind(92);
	apppath = apppath.Mid(0, backsl+1);
	if(apppath.GetAt(0) == 34)
		apppath.Delete(0);
	char *pBuffer = new char[MAX_PATH];

	switch(pItem->GetType()){
		case 0:				// Project
			{
			}
			break;
		case 1:				// Scheme
			{
				if(pItem->m_bFileInProject){
					apppath.Append(_T("Content\\"));
				}
				else{
					apppath.Append(_T("Additional\\"));
				}
			}
			break;
		case 2:				// Block
			{
				if(pItem->m_bFileInProject){
					apppath.Append(_T("Content\\Blocks\\"));
				}
				else{
					apppath.Append(_T("Additional\\Blocks\\"));
				}
			}
			break;
		case 3:
			{
				if(pItem->m_bFileInProject){
					apppath.Append(_T("Content\\Gates\\"));
				}
				else{
					apppath.Append(_T("Additional\\Gates\\"));
				}
			}
	}
	CString strFile = theApp.m_pElementCont->AssociateFile(pItem->m_strOrigin, apppath);
	//apppath.Append(pItem->m_strFilename);
	wcstombs(pBuffer, strFile, MAX_PATH-1);
	TiXmlDocument project(pBuffer);
	project.LoadFile();
	TiXmlElement* root = project.FirstChildElement();
	if(root){
		bool bFileInProject = false;
		TiXmlElement* content = root->FirstChildElement("CONTENT");
		if(content){
			TiXmlElement* item = content->FirstChildElement();
			while(item){
				int *nType = new int;
				TiXmlElement* temp = item->FirstChildElement("DISPLAY");
				CString strDisplay, strOrigin;
				if(temp)
					strDisplay = (CString)temp->GetText();
				temp = item->FirstChildElement("ORIGIN");
				if(temp){
					strOrigin = (CString)temp->GetText();
					int *inProject = new int;
					temp->Attribute("inProject", inProject);
					bFileInProject = (bool)*inProject;
					SAFE_DELETE(inProject);
				}
				item->Attribute("type", nType);
				//HTREEITEM hItem = AddTreeItem(strDisplay, strOrigin, pItem, *nType, bFileInProject);
				AddTreeItem(strDisplay, strOrigin, pItem, *nType, bFileInProject, _T(""));
				item = item->NextSiblingElement();
				pItem->m_slContent.AddTail(strDisplay);
				delete nType;
			}
		}
	}
	SAFE_DELETE_ARRAY(pBuffer);
}

bool CTreeBar::FileExists(CString strFilename, bool bInProject)
{
	return false;
}

void CTreeBar::RemoveAll()
{
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	//tree must have topmost item in order to delete its children
	if(m_mapContent.GetSize()){
		CTreeItem *pItem;
		CString strTemp;
		POSITION pos = m_mapContent.GetStartPosition();
		if(pos){
			m_mapContent.GetNextAssoc(pos, strTemp, pItem);
		}
		if(pItem)
			DeleteChildren(pItem);
		SAFE_DELETE(pItem);
		m_mapContent.RemoveAll();
		m_pSchemeTree->DeleteAllItems();
	}
}

void CTreeBar::DeleteChildren(CTreeItem *pItem)
{
	// remove all items except for those modified
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	CTreeItem *pTempItem = NULL;
	CString strTemp;
	if(pItem){
		POSITION pos = pItem->m_mapContent.GetStartPosition();
		if(!pItem->ModifiedInSession()){
			while(pos){
				pItem->m_mapContent.GetNextAssoc(pos, strTemp, pTempItem);
				if(pTempItem){
					DeleteChildren(pTempItem);
					m_pSchemeTree->DeleteItem(pTempItem->m_hItem);
					pItem->m_mapContent.RemoveKey(pTempItem->m_strNodeName);
					SAFE_DELETE(pTempItem);
				}
			}
		}
		else{ // just erase dummy object
			while(pos){
				pItem->m_mapContent.GetNextAssoc(pos, strTemp, pTempItem);
				if(pTempItem && pTempItem->GetType() == -1){
					m_pSchemeTree->DeleteItem(pTempItem->m_hItem);
					pItem->m_mapContent.RemoveKey(pTempItem->m_strNodeName);
					SAFE_DELETE(pTempItem);
				}
			}
		}
	}
}
void CTreeBar::OnTvnItemexpandingTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
	if(pNMTreeView->action != TVE_EXPAND){
		*pResult = 0;
		return;
	}
	if(IsItemExpanded(pNMTreeView->itemNew.hItem)){
		*pResult = 0;
		return;
	}
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	CTreeItem* pItem = GetTreeItem(pNMTreeView->itemNew.hItem);

	if(!pItem || !pItem->m_bItemOK){
		MessageBox(_T("Cannot expand item"), _T("Error"));
		*pResult = -1;
		return;
	}

	if(pItem && pItem->NeedUpdate()){
		pItem->SetNeedUpdate(false);
		DeleteChildren(pItem);
		PopulateItem(pItem);
	}
	
	*pResult = 0;
}

bool CTreeBar::IsItemExpanded(HTREEITEM hItem)
{
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	if(hItem == NULL)
		return false;

	return (TVIS_EXPANDED | TVIS_EXPANDPARTIAL) &m_pSchemeTree->GetItemState(hItem, TVIS_EXPANDED | TVIS_EXPANDPARTIAL ) ? true : false;
}

void CTreeBar::OnNMDblclkTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
	*pResult = -1; // never expand after dblckl
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	HTREEITEM hItem = m_pSchemeTree->GetSelectedItem();
	if(!hItem){
		return;
	}
	CTreeItem *pItem = OpenElement(hItem);
	EnsureExpanded(pItem);
	if(pItem && (pItem->GetType() == 0 || pItem->GetType() == 3 || pItem->GetType() == 4)){ // dblckl on project
		*pResult = 0;	// expand
		return;
	}
}

void CTreeBar::OnNMRclickTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
	*pResult = 0;
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);

	CPoint pntScreen;
	if(GetCursorPos(&pntScreen)){
		CPoint ptClient(pntScreen);
		m_pSchemeTree->ScreenToClient(&ptClient);

		UINT flags;
		HTREEITEM hItem = m_pSchemeTree->HitTest(ptClient, &flags);
		
		if(hItem == NULL){
			// The user hasn't clicked on any item.
		}
		else{
			// show context menu depending on type of chosen item
			CTreeItem* pItem = GetTreeItem(hItem);
			if(pItem){
				m_pSelectedItem = pItem;
				int nType = pItem->GetType();
				CMenu menu;
				switch(nType){
			case 0:		// projectType
				{
					if(menu.LoadMenuW(IDR_MENU2)){
						CMenu *pMenu = menu.GetSubMenu(0);
						if(pMenu){
							pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, pntScreen.x, pntScreen.y, this);
							pMenu->DestroyMenu();
						}
					}
				}
				break;
			case 1:		// schemeType
				{
					if(menu.LoadMenuW(IDR_MENU2)){
						CMenu *pMenu = menu.GetSubMenu(1);
						if(pMenu)
							pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, pntScreen.x, pntScreen.y, this);
						pMenu->DestroyMenu();
					}
				}
				break;
			case 2:
				{
					if(menu.LoadMenuW(IDR_MENU2)){
						CMenu *pMenu = menu.GetSubMenu(2);
						if(pMenu)
							pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, pntScreen.x, pntScreen.y, AfxGetMainWnd());
					}
				}
				break;
				}
			}
		}
	}
}

CTreeItem* CTreeBar::OpenElement(HTREEITEM hItem)
{
	CTreeItem* pItem = GetTreeItem(hItem);
	OpenElement(pItem);
	return pItem;
}

void CTreeBar::OpenElement(CTreeItem *pItem)
{
	if(!pItem || !pItem->m_bItemOK){
		MessageBox(_T("Cannot open this item (file or origin not found)."), _T("Error!"));
		return;
	}

	if(pItem->GetType() == 0 || pItem->GetType() == 3 || pItem->GetType() == 4){ // open project || gate
		//MessageBox(_T("Cannot open project | gate | wire"), _T("Information!"), MB_ICONINFORMATION);
		return;
	}

	CString strPath;
	if(pItem->m_bFileInProject){
		strPath = theApp.m_pOpenedProjectDoc->m_ProjectDoc.m_strProjectPath;
		switch(pItem->GetType()){
			case 1:
				strPath.Append(_T("Content\\"));
				break;
			case 2:
				strPath.Append(_T("Content\\Blocks\\"));
				break;
		}
		//((CINKSApp*)AfxGetApp())->OpenFile(strPath, pItem->m_strNodeName, pItem->m_strOrigin, pItem->GetType(), pItem->m_bFileInProject);
		((CINKSApp*)AfxGetApp())->OpenFile(strPath, pItem);
	}
	else{
		strPath = m_strAppPath;
		switch(pItem->GetType()){
			case 1:
				strPath.Append(_T("Additional\\"));
				break;
			case 2:
				strPath.Append(_T("Additional\\Blocks\\"));
				break;
		}
		//((CINKSApp*)AfxGetApp())->OpenFile(strPath, pItem->m_strNodeName, pItem->m_strOrigin, pItem->GetType(), pItem->m_bFileInProject);
		((CINKSApp*)AfxGetApp())->OpenFile(strPath, pItem);
	}
}

void CTreeBar::PushIntoTheBlock(CString strName, CString strParent, CINKSDoc *pOwnerDoc)
{
	if(!pOwnerDoc)
		return;
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);

	CTreeItem *pItem = GetTreeItem(strName, pOwnerDoc->m_pTreeItem);

	if(pItem){	// strName->parent expanded and strName visible
		OpenElement(pItem);
		EnsureExpanded(pItem);
	}
	else{
		CTreeItem *pItemParent = pOwnerDoc->m_pTreeItem;
		if(pItemParent){
			if(EnsureExpanded(pItemParent)){
				pItem = GetTreeItem(strName, pItemParent);
				if(pItem){	// strName->parent expanded and strName visible
					OpenElement(pItem);
					EnsureExpanded(pItem);
				}
			}
		}
	}
}

bool CTreeBar::EnsureExpanded(CTreeItem *pItem)
{
	if(!pItem)
		return false;

	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	if(m_pSchemeTree->Expand(pItem->m_hItem, TVE_EXPAND)){
		return true;
	}
	return false;
}

void CTreeBar::PushOutOfTheBlock(CString strName)
{
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	CTreeItem *pItem = GetTreeItem(strName, GetProjectItem());
	if(pItem){
		OpenElement(pItem->GetParent());
	}
}

CTreeItem* CTreeBar::GetProjectItem()
{
	CTreeItem *pParent = NULL;
	CString strTemp;
	POSITION pos = m_mapContent.GetStartPosition();
	if(pos){
		m_mapContent.GetNextAssoc(pos, strTemp, pParent);
	}
	return pParent;
}

void CTreeBar::SetSelectedItem(CString strName)
{
	//m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	//CTreeItem *pItem = GetTreeItem(strName, m_arrayContent.GetAt(0));
	//if(pItem){
	//	m_pSchemeTree->Select(pItem->m_hItem, TVGN_CARET);
	//	m_pSchemeTree->SelectItem(pItem->m_hItem);
	//}
}

 // Create new scheme and add it to project
void CTreeBar::OnProjecttypeCreatenewscheme()
{
	theApp.CreateNewElement(1);
}

void CTreeBar::OnSchemetypeCollapseintoblock()
{
	// create new doc
	if(m_pSelectedItem){
		OpenElement(m_pSelectedItem);
		theApp.CreateNewElement(2);
	}
}

void CTreeBar::OnSchemetypeDelete()
{
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	if(theApp.DeleteScheme()){
		DeleteChildren(m_pSelectedItem);
		m_pSchemeTree->DeleteItem(m_pSelectedItem->m_hItem);
		m_pSelectedItem->GetParent()->m_mapContent.RemoveKey(m_pSelectedItem->m_strDisplayName);
		m_pSelectedItem->GetParent()->m_pDoc->SetModifiedFlag();
		POSITION pos = m_pSelectedItem->GetParent()->m_slContent.GetHeadPosition();
		
		CString strName;
		POSITION posPrev;
		while(pos){
			posPrev = pos;
			strName = m_pSelectedItem->GetParent()->m_slContent.GetNext(pos);
			if(strName == m_pSelectedItem->m_strDisplayName){
				m_pSelectedItem->GetParent()->m_slContent.RemoveAt(posPrev);
				pos = NULL;
			}
		}
		SAFE_DELETE(m_pSelectedItem);
	}
}



//<Project display="MyProject1" filename="Project1.xml">
//  <CONTENT>
//    <ELEM type="1">
//      <DISPLAY>myScheme1</DISPLAY>
//      <ORIGIN inProject="1">myScheme1</ORIGIN>
//      <ORIGINF>Scheme1.xml</ORIGINF>
//    </ELEM>
//
//    <ELEM type="1">
//      <DISPLAY>myScheme2</DISPLAY>
//      <ORIGIN inProject="1">myScheme1</ORIGIN>
//      <ORIGINF>Scheme2.xml</ORIGINF>
//    </ELEM>
//  </CONTENT>
//</Project>

void CTreeBar::SetModified(CTreeItem *pItem, bool bModified)
{
	pItem->m_bModified = bModified;
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	if(bModified){ // must notify treeitem that this item is modified and not saved
		m_pSchemeTree->SetItemImage(pItem->m_hItem, 2, 2);
	}
	else{
		m_pSchemeTree->SetItemImage(pItem->m_hItem, 0, 0);
	}
}

void CTreeBar::SetErrorItem(CTreeItem *pItem)
{
	if(!pItem)
		return;
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	m_pSchemeTree->SetItemImage(pItem->m_hItem, 1, 1);
	pItem->SetNeedUpdate(true);
}

CTreeItem* CTreeBar::RegisterNewCommonObject(CCommonObject *pObj, CINKSDoc *pDoc)
{
	if(!pDoc) // owner
		return NULL;

	CTreeItem *pItem = GetTreeItem(pDoc->GetTitle(), pDoc->m_pTreeItem);
	CTreeItem *pRet = NULL;
	CString strAppend;
	if(pItem){
		pRet = AddTreeItem(pObj->m_strUniqueName, (CString)pObj->m_strOrigin, pItem, pObj->m_nObjType, pObj->m_bFileInProject, _T(""), true);
		pDoc->m_pTreeItem->m_slContent.AddTail(pObj->m_strUniqueName);
		pRet->SetDoc(pDoc);
		SetModified(pItem, true);
		EnsureExpanded(pItem);

	}
	return pRet;
}

CTreeItem* CTreeBar::RegisterNewCommonObject(CString strName, CString strOrigin, CString strParent, int nType, bool bFileInProject, CINKSDoc *pDoc)
{
	if(!pDoc)
		return NULL;
	//CTreeItem *pItem = GetTreeItem(strParent, GetProjectItem());
	CTreeItem *pItem = GetTreeItem(strParent, pDoc->m_pTreeItem);
	CTreeItem *pRet = NULL;
	if(pItem){
		pRet = AddTreeItem(strName, strOrigin, pItem, nType, bFileInProject, _T(""), true);
		pDoc->m_pTreeItem->m_slContent.AddTail(strName);
		pRet->SetDoc(pDoc);
		SetModified(pItem, true);
		EnsureExpanded(pItem);

	}
	return pRet;
}

bool CTreeBar::CanDeregisterCommonObject(CString strName, CINKSDoc *pOwnerDoc)
{
	return true;
	if(!pOwnerDoc)
		return false;
	//CTreeItem *pItem = GetTreeItem(strName, GetProjectItem());
	CTreeItem *pItem = GetTreeItem(strName, pOwnerDoc->m_pTreeItem);
	if(!pItem)
		return true;
	return !pItem->ModifiedInSession();
}

void CTreeBar::DeRegisterCommonObject(CString strName, CINKSDoc *pOwnerDoc)
{
	if(!pOwnerDoc)
		return;
	m_pSchemeTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1);
	//CTreeItem *pItem = GetTreeItem(strName, GetProjectItem());
	CTreeItem *pItem = GetTreeItem(strName, pOwnerDoc->m_pTreeItem);
	if(pItem){
		DeleteChildren(pItem);
		m_pSchemeTree->DeleteItem(pItem->m_hItem);
		pItem->GetParent()->m_mapContent.RemoveKey(pItem->m_strNodeName);
		SAFE_DELETE(pItem);
	}
}