// INKS.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "INKS.h"
#include "MainFrm.h"

#include "ChildFrm.h"
#include "INKSDoc.h"
#include "INKSView.h"
#include "RequestNameDlg.h"
#include "ElementContainer.h"
#include "CollapseDialog.h"
#include "XmlCreator.h"
#include "TreeBar.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CINKSApp

BEGIN_MESSAGE_MAP(CINKSApp, CWinApp)
	ON_COMMAND(ID_APP_ABOUT, &CINKSApp::OnAppAbout)
	// Standard file based document commands
	ON_COMMAND(ID_PROJECT_NEW, &CINKSApp::OnProjectNew)
	ON_COMMAND(ID_PROJECT_OPEN, &CINKSApp::OnProjectOpen)
	ON_COMMAND(ID_PROJECT_CLOSE,&CINKSApp::OnProjectClose)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinApp::OnFilePrintSetup)

	ON_COMMAND(ID_PARSEALL_GATES, &CINKSApp::OnParseAllGates)
	ON_COMMAND(ID_PARSEALL_BLOCKS, &CINKSApp::OnParseAllBlocks)
	
	ON_COMMAND(ID_APP_EXIT, &CINKSApp::OnAppExit)
	ON_COMMAND(ID_ADDSCHEME, &CINKSApp::AddNewScheme)
END_MESSAGE_MAP()


// CINKSApp construction

CINKSApp::CINKSApp()
	: m_pOpenedProjectDoc(NULL), m_bProjectDirty(false)
{
	m_pElementCont = new CElementContainer();
}
CINKSApp::~CINKSApp()
{
	SAFE_DELETE(m_pElementCont);
}

// The one and only CINKSApp object

CINKSApp theApp;


// CINKSApp initialization
void CINKSApp::OnParseAllGates()
{
	m_pElementCont->OpenAppSpecific(m_strAppPath, 1);
}

void CINKSApp::OnParseAllBlocks()
{
	m_pElementCont->OpenAppSpecific(m_strAppPath, 2);
}

BOOL CINKSApp::InitInstance()
{
	
	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);
	
	CWinApp::InitInstance();

	// Initialize OLE libraries
	if (!AfxOleInit())
	{
		AfxMessageBox(IDP_OLE_INIT_FAILED);
		return FALSE;
	}
	AfxEnableControlContainer();

	// initialize OLE
	HRESULT hr = CoInitialize(NULL);

	// check for errors
	if (FAILED(hr)) {
		AfxMessageBox(_T("OLE initialization error"),MB_OK);
		return -1;
	};


	// Standard initialization
	// If you are not using these features and wish to reduce the size
	// of your final executable, you should remove from the following
	// the specific initialization routines you do not need
	// Change the registry key under which our settmings are stored
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));
	LoadStdProfileSettings(4);  // Load standard INI file options (including MRU)
	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views
	//CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(IDR_INKSTYPE,
		RUNTIME_CLASS(CINKSDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CINKSView));
	if (!pDocTemplate)
		return FALSE;
	
	AddDocTemplate(pDocTemplate);

	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame();
	if (!pMainFrame){
		delete pMainFrame;
		return FALSE;
	}
	if(!pMainFrame->LoadFrame(IDR_INKSTYPE)){
		delete pMainFrame;
		return FALSE;
	}
	
	m_pMainWnd = pMainFrame;
	
	// call DragAcceptFiles only if there's a suffix
	//  In an MDI app, this should occur immediately after setting m_pMainWnd


	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line.  Will return FALSE if
	// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
	// don't open new file when starting app
	if(cmdInfo.m_nShellCommand != CCommandLineInfo::FileNew)
		if(!ProcessShellCommand(cmdInfo))
			return FALSE;

	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

	CString apppath = GetCommandLine();
	int backsl = apppath.ReverseFind(92);
	apppath = apppath.Mid(0, backsl+1);
	if(apppath.GetAt(0) == 34)
		apppath.Delete(0);
	m_strAppPath = apppath;
	pMainFrame->m_TreeBar.m_strAppPath = apppath;

	// The one and only window has been initialized, so show and update it
	m_pMainFrame = (CMainFrame*)m_pMainWnd; 
	m_pMainWnd->MoveWindow(0,0,800,600);

	// The main window has been initialized, so show and update it
	//pMainFrame->ShowWindow(m_nCmdShow);
	
	pMainFrame->ShowWindow(SW_MAXIMIZE);
	pMainFrame->UpdateWindow();


	// fill app element container with availiable content
	this->m_pElementCont->OpenAppSpecific(apppath, 0);

	m_bAppExit = false;
	pMainFrame->SetTitle(_T("INKS"));
	return TRUE;
}


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

// App command to run the dialog
void CINKSApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}
int CINKSApp::ExitInstance()
{
	m_bAppExit = true;
	OnProjectClose();
	return CWinApp::ExitInstance();
}

void CINKSApp::CloseAllDocuments(BOOL bEndSession)
{
	m_bAppExit = true;
	OnProjectClose();
	CWinApp::CloseAllDocuments(bEndSession);
}


void CINKSApp::OnProjectNew()
{
	// create new project!
	// create doc with invisible view

	// if some project already opened...so...close it
	if(m_pOpenedProjectDoc){
		if(!CloseAllDocumentsCommand()) // if 'cancel' pressed for any document
			return;
	}
	m_pOpenedProjectDoc = NULL;
	m_pMainFrame->m_TreeBar.RemoveAll();
	CreateNewElement(0);
}

bool CINKSApp::DirectoryExists(CString strDirPath)
{
	CString strPath = strDirPath;
	strPath.Replace(_T("\\"), _T("\\\\"));
	DWORD dwAttr = GetFileAttributes(strPath);
	if(dwAttr == 0xffffffff){
		DWORD dwError = GetLastError();
		if(dwError == ERROR_PATH_NOT_FOUND){
			// path not found
			return false;
		}
	}
	else if(dwAttr & FILE_ATTRIBUTE_DIRECTORY){
		return true;
	}
	return false;
}

bool CINKSApp::FileExists(CString strFile)
{
	CFile file;
	if(file.Open(strFile, CFile::readOnly)){
		file.Close();
		return true;
	}
	return false;
}

void CINKSApp::OnProjectOpen()
{
	if(m_pOpenedProjectDoc){
		if(!CloseAllDocumentsCommand()) // if 'cancel' pressed for any document
			return;
	}
	m_pMainFrame->m_TreeBar.RemoveAll();
	m_pOpenedProjectDoc = NULL;
	CWinApp::OnFileOpen();
}

void CINKSApp::OpenFile(CString strPath, CTreeItem *pItem)
{
	// open new document/view
	// fill with content of file strFilename
	CString strFile = m_pElementCont->AssociateFile(pItem->m_strOrigin, strPath);
	if(!strFile.GetLength()){
		MessageBox(AfxGetMainWnd()->m_hWnd, _T("Couldn't find specified file"), _T("Error"), MB_ICONERROR);
		return;
	}
	// open file IFF not already opened!!!!
	DocumentList documents;
	GetAllDocuments(documents);
	POSITION pos = documents.GetHeadPosition();
	CINKSDoc *pDocTemp = NULL;
	while(pos){
		pDocTemp = DYNAMIC_DOWNCAST(CINKSDoc, documents.GetNext(pos));
		if(pDocTemp->GetPathName() == strFile){
			// if already opened, bring to front
			BringToTop(pDocTemp);
			return;
		}	
	}
	
	CDocument *pNewDoc = CreateDocView(strFile, pItem->m_strOrigin, TRUE);
	CINKSDoc *pDoc = NULL;
	if(pNewDoc->IsKindOf(RUNTIME_CLASS(CINKSDoc))){
		pDoc = (CINKSDoc*)pNewDoc;
	}
	if(pDoc){
		BOOL bItemOk = pDoc->OnOpenFile(strFile, pItem->GetType(), pItem->FileInProject());
		// fill m_ProjectDoc, m_SchemeDoc | m_BlockDoc
		if(pItem->GetType() == 0){ // projectDoc
			pDoc->m_pParentDoc = NULL;
			int i=0;
		}
		else if(pItem->GetType() == 1){ // schemeDoc
			pDoc->m_pParentDoc = m_pOpenedProjectDoc;
			pDoc->m_SchemeDoc.m_strSchemeName = pItem->m_strOrigin; // always schemename as origin for scheme
		}
		else if(pItem->GetType() == 2){ // blockDoc
			pDoc->m_pParentDoc = pItem->GetParent()->m_pDoc;
			pDoc->m_BlockDoc.m_bAppBlock = !pItem->m_bFileInProject;
			pDoc->m_BlockDoc.m_strBlockName = pItem->m_strNodeName;
			pDoc->m_BlockDoc.m_strBlockOrigin = pItem->m_strOrigin;
		}
		// -------------------------------------------
		pItem->SetDoc(pDoc);
		pDoc->SetTitle(pItem->m_strNodeName);
		pDoc->m_pTreeItem = pItem;
		m_pMainFrame->m_SideBar.ShowInfo(pDoc);
		if(!bItemOk){
			theApp.m_pMainFrame->m_TreeBar.SetErrorItem(pItem);
		}
	}
}

//void CINKSApp::OpenFile(CString strPath, CString strTitle, CString strOrigin, int nType, bool bInProject)
//{
//	// open new document/view
//	// fill with content of file strFilename
//	CString strFile = m_pElementCont->AssociateFile(strOrigin, strPath);
//	if(!strFile.GetLength()){
//		MessageBox(AfxGetMainWnd()->m_hWnd, _T("Couldn't find specified file"), _T("Error"), MB_ICONERROR);
//		return;
//	}
//	// open file IFF not already opened!!!!
//	DocumentList documents;
//	GetAllDocuments(documents);
//	POSITION pos = documents.GetHeadPosition();
//	CINKSDoc *pDocTemp = NULL;
//	while(pos){
//		pDocTemp = DYNAMIC_DOWNCAST(CINKSDoc, documents.GetNext(pos));
//		if(pDocTemp->GetPathName() == strFile){
//			// if already opened, bring to front
//			BringToTop(pDocTemp);
//			return;
//		}
//			
//	}
//	
//	CDocument *pNewDoc = CreateDocView(strFile, strOrigin, TRUE);
//	CINKSDoc *pDoc = NULL;
//	if(pNewDoc->IsKindOf(RUNTIME_CLASS(CINKSDoc))){
//		pDoc = (CINKSDoc*)pNewDoc;
//	}
//	if(pDoc){
//		if(pDoc->OnOpenFile(strFile, nType, bInProject)){
//			pDoc->SetTitle(strTitle);
//			m_pMainFrame->m_SideBar.ShowInfo(pDoc);
//		}
//	}
//}

void CINKSApp::BringToTop(CDocument *pDoc)
{
	POSITION ViewPos = pDoc->GetFirstViewPosition();
	if(ViewPos){
		CView *pView =  pDoc->GetNextView(ViewPos); 
		CFrameWnd *pFrame = pView->GetParentFrame();
		if(pFrame)
			pFrame->ActivateFrame(); // activate.
	}
}

BOOL CINKSApp::CloseAllDocumentsCommand()
{
	BOOL bRet = TRUE;
	DocumentList Docs;
	GetAllDocuments(Docs);

	POSITION pos = Docs.GetHeadPosition();
	CDocument *pDoc = NULL;
	CINKSDoc* pProDoc = NULL;
	//CDocument *pFirst = Docs.GetNext(pos);
	while(pos){		  
		pDoc = Docs.GetNext(pos);
		if(pDoc->IsKindOf(RUNTIME_CLASS(CINKSDoc))){
			if(((CINKSDoc*)pDoc)->m_nDocumentType != 0){
				if(((CINKSDoc*)pDoc)->OnCloseProject()){
					//pDoc->OnCloseDocument();
					((CINKSDoc*)pDoc)->CloseDocument(true);
				}
				else{
					bRet = FALSE;
				}
			}
			else
				pProDoc = (CINKSDoc*)pDoc;
			//if(!pDoc->OnCmdMsg(ID_FILE_CLOSE, 0, 0, 0))
		}
	}
	if(pProDoc && pProDoc->IsModified()){ // always project-document (=>always save if needed)
		if(pProDoc->IsKindOf(RUNTIME_CLASS(CINKSDoc))){
			((CINKSDoc*)pProDoc)->OnSaveDocument(pProDoc->GetPathName());
			pProDoc->CloseDocument(true);
			m_pOpenedProjectDoc = NULL;
		}
	}
	return bRet;
}

void CINKSApp::GetAllDocuments(DocumentList &Docs)
{
	CDocManager *pDocMgr = m_pDocManager;
	// For all document templates
	POSITION TemplPos = pDocMgr->GetFirstDocTemplatePosition();
	while(TemplPos){
		CDocTemplate *pTmpl = pDocMgr->GetNextDocTemplate(TemplPos);
		POSITION DocPos = pTmpl->GetFirstDocPosition();
		while(DocPos){
			CDocument *pDoc = pTmpl->GetNextDoc(DocPos);
			Docs.AddTail(pDoc);
		}
	}
}

CDocument* CINKSApp::CreateDocView(CString strFilename, CString strTitle, BOOL bViewVisible)
{
	// create new doc/view when opening scheme, block
	ASSERT(strFilename.GetLength() != 0);
	CString str = strFilename;

	CDocument* pDoc = pDocTemplate->CreateNewDocument();
	// if returns NULL, the user has already been alerted

	ASSERT(pDoc != NULL);

	// creating view here

	if(pDoc != NULL){
		ASSERT_VALID(pDoc);

		BOOL bAutoDelete = pDoc->m_bAutoDelete;//m_bAutoDelete is FALSE any way
		pDoc->m_bAutoDelete = FALSE;   // don't destroy if something goes wrong
		CFrameWnd* pNewFrame = pDocTemplate->CreateNewFrame(pDoc, NULL);
		pDoc->m_bAutoDelete = bAutoDelete;
		pDoc->SetPathName(str);
		pDoc->SetTitle(strTitle);

		if(pNewFrame != NULL){
			pDocTemplate->InitialUpdateFrame(pNewFrame, pDoc, bViewVisible);
		}
		else{
			ASSERT(0);
		}
	}
	return pDoc;
}

CDocument* CINKSApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
	// open project
	CDocument *pNewDoc = CreateDocView(lpszFileName, _T(""), FALSE);
	CINKSDoc* pDoc = NULL;
	if(pNewDoc && pNewDoc->IsKindOf(RUNTIME_CLASS(CINKSDoc))){
		pDoc = (CINKSDoc*)pNewDoc;
		m_pOpenedProjectDoc = (CINKSDoc*)pDoc;
		if(pDoc->OnOpenProject(lpszFileName)){
			theApp.m_pMainFrame->m_SideBar.ShowInfo((CINKSDoc*)pDoc);
		}
		else{
		}
	}
	
	return pDoc;
}

void CINKSApp::CreateNewElement(int nType)
{
	CString strElemName;
	switch(nType){
		case 0:		// adding new project
				CreateNewProject();
			break;
		case 1:		// adding new scheme
			{
				CreateNewScheme(m_pMainFrame->m_TreeBar.m_pSelectedItem);
			}
			break;
		case 2:		// adding new block
			{
				CreateNewBlock(m_pMainFrame->m_TreeBar.m_pSelectedItem);
			}
			break;
	}
}

void CINKSApp::CreateNewProject()
{
	CRequestNameDlg dlg(_T("Please insert Project name"), _T("New Project"));
	CString strProjectName;
	if(dlg.DoModal() == IDOK){
		strProjectName = dlg.GetName();

		CString strNewProject = this->m_strAppPath;
		strNewProject.Append(strProjectName);
		if(DirectoryExists(strNewProject)){
			MessageBox(this->m_pMainFrame->m_hWnd, _T("Project with this name already exists!"), _T("Error"), MB_ICONERROR);
			return;
		}
		else{
			CreateProjectDirectories(strNewProject);
			CString strProjectPath = strNewProject;
			strNewProject.Append(_T("\\"));
			strNewProject.Append(strProjectName);
			strNewProject.Append(_T(".xml"));
			CDocument *pDoc = CreateDocView(strNewProject, strProjectName, FALSE);
			if(pDoc->IsKindOf(RUNTIME_CLASS(CINKSDoc))){
				m_pOpenedProjectDoc = (CINKSDoc*)pDoc;
				strProjectPath.Append(_T("\\"));

				ofstream outfile(strNewProject);
				if(outfile.is_open()){
					CXmlCreator creator;

					outfile << creator.WriteProject(strProjectName, NULL).str();
					outfile.close();
				}

				if(m_pOpenedProjectDoc){
					if(m_pOpenedProjectDoc->OnNewProject(strProjectPath, strProjectName)){
						m_pOpenedProjectDoc->m_pTreeItem = m_pMainFrame->m_TreeBar.AddTreeItem(m_pOpenedProjectDoc->m_ProjectDoc.m_strProjectName, m_pOpenedProjectDoc->m_ProjectDoc.m_strProjectName, NULL, 0, true, _T(""), true);
						if(m_pOpenedProjectDoc->m_pTreeItem)
							m_pOpenedProjectDoc->m_pTreeItem->SetDoc(m_pOpenedProjectDoc);
						else
							MessageBox(AfxGetMainWnd()->m_hWnd, _T("CreateNewProject failed"), _T("Error"), MB_ICONERROR);
					}
				}
			}
			else{
				MessageBox(AfxGetMainWnd()->m_hWnd, _T("CreateNewProject failed"), _T("Error"), MB_ICONERROR);
			}
		}
	}
}

void CINKSApp::CreateProjectDirectories(CString strProjectPath)
{
	
	CreateDirectory(strProjectPath, NULL);
	strProjectPath.Append(_T("\\Content"));
	CreateDirectory(strProjectPath, NULL);

	CString strPath = strProjectPath;
	strPath.Append(_T("\\Blocks"));
	CreateDirectory(strPath, NULL);
	strProjectPath.Append(_T("\\Gates"));
	CreateDirectory(strProjectPath, NULL);
}

void CINKSApp::AddNewScheme()
{
	CreateNewScheme(this->m_pMainFrame->m_TreeBar.GetProjectItem());
}

void CINKSApp::CreateNewScheme(CTreeItem *pParent)
{
	// create new doc/view
	CRequestNameDlg dlg(_T("Please insert Scheme name"), _T("New Scheme"));
	CString strElemName;
	if(dlg.DoModal() == IDOK){
		strElemName = dlg.GetName();
		// check for elem name consistency
		CString strPath = m_pOpenedProjectDoc->m_ProjectDoc.m_strProjectPath;
		strPath.Append(_T("Content\\"));
		strPath.Append(strElemName);
		strPath.Append(_T(".xml"));
		// create file
		if(FileExists(strPath)){
			MessageBox(AfxGetMainWnd()->m_hWnd, _T("Scheme with this filename already exists!"), _T("Error"), MB_ICONERROR);
			return;
		}
		ofstream outfile(strPath);
		CXmlCreator creator;
		outfile << creator.WriteScheme(strElemName, NULL).str();
		outfile.flush();
		outfile.close();


		CDocument* pDocument = CreateDocView(strPath, strElemName, TRUE);
		CINKSDoc *pDoc = NULL;
		if(pDocument->IsKindOf(RUNTIME_CLASS(CINKSDoc))){
			pDoc = (CINKSDoc*)pDocument;
			pDoc->SetModifiedFlag();
			pDoc->m_pTreeItem = m_pMainFrame->m_TreeBar.AddTreeItem(strElemName, strElemName, pParent, 1, true, _T(""), true);
			if(pDoc->m_pTreeItem){
				pDoc->m_pTreeItem->SetDoc(pDoc);
				pParent->m_slContent.AddTail(strElemName);
				m_pOpenedProjectDoc->SetModifiedFlag();
				pDoc->m_nDocumentType = 1;
				pDoc->m_SchemeDoc.m_strSchemeName = strElemName;
				pParent->SetModifiedInSession(true);
			}
		}
	}
}

bool CINKSApp::DeleteScheme()
{
	if(m_pMainFrame->m_TreeBar.m_pSelectedItem->m_pDoc)
		m_pMainFrame->m_TreeBar.m_pSelectedItem->m_pDoc->CloseDocument(true);
	return true;
}

CDocument* CINKSApp::GetActiveDocument()
{
	CMDIChildWnd *pChild = ((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();

	if(!pChild)
		return NULL;

	CDocument * pDoc = pChild->GetActiveDocument();
	return pDoc;
}

void CINKSApp::CreateNewBlock(CTreeItem *pParent)
{
	// scheme that is about to be collapsed is currently opened - just get selected document and extract info from m_COList
	CDocument *pDocument = GetActiveDocument();
	if(!pDocument || !pDocument->IsKindOf(RUNTIME_CLASS(CINKSDoc))){
		MessageBox(AfxGetMainWnd()->m_hWnd, _T("Cannot create new block from scheme"), _T("Error"), MB_ICONERROR);
		return;
	}

	CRequestNameDlg dlg(_T("Please insert Block name"), _T("New Block"));
	CString strElemName;
	CCollapseDialog collapseDlg;

	CINKSDoc *pDoc = (CINKSDoc*)pDocument;
	if(collapseDlg.InitDialog(&pDoc->m_COList)){

		if(collapseDlg.DoModal() == IDOK){
			strElemName = collapseDlg.m_strBlockName;
			// check for elem name consistency
			CString strPath = m_pOpenedProjectDoc->m_ProjectDoc.m_strProjectPath;
			strPath.Append(_T("Content\\Blocks\\"));
			strPath.Append(strElemName);
			strPath.Append(_T(".xml"));

			// create new block object
			CBlock *pBlock = new CBlock(collapseDlg.m_strlistIns, collapseDlg.m_strlistOuts, strElemName, (CStringA)strElemName, CPoint(0,0));
			// create file
			CXmlCreator creator;
			ofstream outfile(strPath);

			outfile << creator.WriteBlock(strElemName, &pDoc->m_COList, pBlock).str();
			outfile.flush();
			outfile.close();

			////CDocument* pNewDoc = CreateDocView(strPath, strElemName, TRUE);
			//pDoc->m_pTreeItem = m_pMainFrame->m_TreeBar.AddTreeItem(strElemName, strElemName, pParent, 1, true, true);
			//if(pDoc->m_pTreeItem){
			//	pDoc->m_pTreeItem->SetDoc(pDoc);
			//	m_pOpenedProjectDoc->m_pElementCont->OpenProjectSpecific(strElemName, m_pOpenedProjectDoc->m_ProjectDoc.m_strProjectPath);
			//}
			this->m_pOpenedProjectDoc->m_pElementCont->OpenProjectSpecific(strPath);
			SAFE_DELETE(pBlock);
		}
	}
	else{
		MessageBox(AfxGetMainWnd()->m_hWnd, _T("Scheme doesn't have Tags specified"), _T("Warning"), MB_ICONWARNING);
	}
}

BOOL CINKSApp::SaveAllModified()
{
	if(m_pOpenedProjectDoc){
		if(CloseAllDocumentsCommand()){
			m_pOpenedProjectDoc = NULL;
			m_pMainFrame->m_TreeBar.RemoveAll();
			return TRUE;
		}
		else
			return FALSE;
	}
	return TRUE;
}

void CINKSApp::OnProjectClose()
{
	if(m_pOpenedProjectDoc){
		if(CloseAllDocumentsCommand()){
			m_pOpenedProjectDoc = NULL;
			m_pMainFrame->m_TreeBar.RemoveAll();
		}
	}
}

// directory list
	//CDialog *pDlg = new CDialog();
	//
	//DlgDirList(pDlg->m_hWnd, _T("C:\\"), IDC_DIRLIST, 0,DDL_EXCLUSIVE | DDL_DIRECTORY);

    //BROWSEINFO bi = { 0 };
    //bi.lpszTitle = _T("Pick a Directory");
    //LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
    //if ( pidl != 0 )
    //{
    //    // get the name of the folder
    //    TCHAR path[MAX_PATH];
    //    if ( SHGetPathFromIDList ( pidl, path ) )
    //    {
    //        TRACE1( "Selected Folder: %s\n", path );
    //    }

    //    // free memory used
    //    IMalloc * imalloc = 0;
    //    if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
    //    {
    //        imalloc->Free ( pidl );
    //        imalloc->Release ( );
    //    }
//}

void CINKSApp::OnAppExit()
{
	m_bAppExit = true;
	OnProjectClose();
	CoUninitialize();
	CWinApp::OnAppExit();
	// TODO: Add your command handler code here
}
