// Library.cpp : implementation file
//

#include "stdafx.h"
#include "ulaPag.h"
#include "Library.h"


// CLibrary dialog

IMPLEMENT_DYNCREATE(CLibrary, CDHtmlDialog)

CLibrary::CLibrary(CWnd* pParent /*=NULL*/)
	: CDHtmlDialog(CLibrary::IDD, CLibrary::IDH, pParent)
{
}

CLibrary::~CLibrary()
{
}

void CLibrary::DoDataExchange(CDataExchange* pDX)
{
	CDHtmlDialog::DoDataExchange(pDX);
}

//[e:00.00]03.16.06
#define DIRECTORY	0
#define OBJFILE		1
static char LibraryDir[] = "ObjLibrary";

int ListDirectoryTree(const char * directoryName, HTREEITEM htree, CTreeCtrl* pCtrl)
  {
	int _filetype = DIRECTORY;
	const int MAX = 1000;
	CFileFind finder;
	CString _fileName;
	char currentbuffer[MAX];
	char changedbuffer[MAX];

	_getcwd(currentbuffer,MAX);
	_getcwd(changedbuffer,MAX);
	
	strcat(changedbuffer, "\\");
	strcat(changedbuffer , directoryName);
	if(chdir(changedbuffer) <0 ) return -1;
	
	
   BOOL bWorking = finder.FindFile("*.*");
   while (bWorking)
   {
      bWorking = finder.FindNextFile();
      _fileName = finder.GetFileName();
	  if(_fileName.Compare(".") && _fileName.Compare(".."))
	  { 
		//cout <<"file name :" << _fileName << endl;
		if(finder.IsDirectory()) _filetype = DIRECTORY;
		else _filetype = OBJFILE;
		HTREEITEM hPA = pCtrl->InsertItem(_fileName, _filetype, _filetype,  htree, TVI_LAST);
		if(finder.IsDirectory()) ListDirectoryTree((LPCTSTR)_fileName ,hPA , pCtrl);
	  }
   }

	if(chdir(currentbuffer) < 0 ) return -1;

	return 0;

}


 void CLibrary::InitialDialog()
 {
	pmyImageList = new CImageList();
	pmyImageList->Detach();	// For MDI
	pmyImageList->Create(IDB_BITMAP1, 16, 2, RGB(255, 255, 255));	

	CTreeCtrl* pCtrl = (CTreeCtrl*) this->GetDlgItem(IDC_TREE1);
	ASSERT(pCtrl != NULL);
		
	pCtrl->SetImageList(pmyImageList, TVSIL_NORMAL);
		 
	// Insert a root item using the structure. We must
	// initialize a TVINSERTSTRUCT structure and pass its
	// address to the call. 
	//	pCtrl->SetImageList(pmyImageList,TVSIL_STATE);

	HTREEITEM hRoot = pCtrl->InsertItem(LibraryDir, 0 ,0);

    ListDirectoryTree(LibraryDir, hRoot, pCtrl);
 }

BOOL CLibrary::OnInitDialog()
{
	CDHtmlDialog::OnInitDialog();

	pmyImageList = new CImageList();
	pmyImageList->Detach();	// For MDI
	pmyImageList->Create(IDB_BITMAP1, 16, 2, RGB(255, 255, 255));	

	CTreeCtrl* pCtrl = (CTreeCtrl*) this->GetDlgItem(IDC_TREE1);
	ASSERT(pCtrl != NULL);
		
	pCtrl->SetImageList(pmyImageList, TVSIL_NORMAL);
		 
	// Insert a root item using the structure. We must
	// initialize a TVINSERTSTRUCT structure and pass its
	// address to the call. 
	//	pCtrl->SetImageList(pmyImageList,TVSIL_STATE);

	HTREEITEM hRoot = pCtrl->InsertItem(LibraryDir, 0 ,0);

    ListDirectoryTree(LibraryDir, hRoot, pCtrl);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

BEGIN_MESSAGE_MAP(CLibrary, CDHtmlDialog)
END_MESSAGE_MAP()

BEGIN_DHTML_EVENT_MAP(CLibrary)
	DHTML_EVENT_ONCLICK(_T("ButtonOK"), OnButtonOK)
	DHTML_EVENT_ONCLICK(_T("ButtonCancel"), OnButtonCancel)
END_DHTML_EVENT_MAP()



// CLibrary message handlers

HRESULT CLibrary::OnButtonOK(IHTMLElement* /*pElement*/)
{
	OnOK();
	return S_OK;  // return TRUE  unless you set the focus to a control
}

HRESULT CLibrary::OnButtonCancel(IHTMLElement* /*pElement*/)
{
	OnCancel();
	return S_OK;  // return TRUE  unless you set the focus to a control
}
