/*=========================================================================
* 
* ot_dLoad.cpp :  Load dialog - Implementation file
* 
*
* Version			1.0
* Creation date		18/08/97	
* Revision date
*
* Shaitan
*=======================================================================*/

#include "stdafx.h"
#define HieFriend

#include "acp_base.h"

#include "OT_inter.hpp"
#include "OT_dLoad.hpp"

#include "x:\cpa\main\inc\_editid.h"


//#################################################################################
// OT_DialogLoad dialog
//#################################################################################

/*----------------------------------------
----------------------------------------*/
OT_DialogLoad::OT_DialogLoad(ObjectTableInterface *pOwnerEditor, CWnd* pParent /*=NULL*/)
	: CDialog(OT_DialogLoad::IDD, pParent)
{
	// init editor
	m_pOwnerEditor = pOwnerEditor;
	// init icon list
	m_oIconList.Create(16, 16, ILC_MASK, 0, 6);
	m_oIconList.Add(LoadIcon(pOwnerEditor->GetDLLIdentity()->hModule, MAKEINTRESOURCE(TOT_IDI_FAMILY)));
	m_oIconList.Add(LoadIcon(pOwnerEditor->GetDLLIdentity()->hModule, MAKEINTRESOURCE(TOT_IDI_UNLOAD)));
	m_oIconList.Add(LoadIcon(pOwnerEditor->GetDLLIdentity()->hModule, MAKEINTRESOURCE(TOT_IDI_OBJTABLE)));
	m_oIconList.Add(LoadIcon(pOwnerEditor->GetDLLIdentity()->hModule, MAKEINTRESOURCE(TOT_IDI_UNLOADB)));
	m_oIconList.Add(LoadIcon(pOwnerEditor->GetDLLIdentity()->hModule, MAKEINTRESOURCE(TOT_IDI_OBJTABLEB)));
}


/*----------------------------------------
----------------------------------------*/
void OT_DialogLoad::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(OT_DialogLoad)
	DDX_Control(pDX, TOT_IDC_TREE, m_cTree);
	//}}AFX_DATA_MAP
}


/*----------------------------------------
----------------------------------------*/
BEGIN_MESSAGE_MAP(OT_DialogLoad, CDialog)
	//{{AFX_MSG_MAP(OT_DialogLoad)
	ON_NOTIFY(TVN_SELCHANGED, TOT_IDC_TREE, OnSelchangeTreeControl)
	ON_NOTIFY(NM_RCLICK, TOT_IDC_TREE, OnRightClkTreeControl)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//#################################################################################
// OT_DialogLoad message handlers
//#################################################################################

/*----------------------------------------
----------------------------------------*/
BOOL OT_DialogLoad::OnInitDialog() 
{
	CPA_BaseObjectList	*pListObjects;
	EditorObjectTable	*pObjTable;
	CPA_BaseObject		*pObject;
	HTREEITEM			hFamilyItem, hObjTableItem;
	Position			Pos;
	CString				csName;
	
	CDialog::OnInitDialog();

	m_hFirstItem = NULL;
	
	m_cTree.DeleteAllItems();
	m_cTree.SetImageList(&m_oIconList, TVSIL_NORMAL);

	// fill ListBox with invalid Lips Synchros
    pListObjects = m_pOwnerEditor->GetInvalidObjectList(C_szObjectTableTypeName);
	
	// only unloaded lips synchro must be dispayed
	for (pObject = pListObjects->GetHeadElement(Pos); pObject;
		 pObject = pListObjects->GetNextElement(Pos))
	{
		pObjTable = (EditorObjectTable *)pObject;
		if (!pObjTable->fn_bIsAvailable())
		{
			// if necessary, create family item
			hFamilyItem = GetCorrespondingItem(pObjTable->GetOwner());
			if (!hFamilyItem)
			{
				hFamilyItem = m_cTree.InsertItem(pObjTable->GetOwner()->GetName(), 0, 0, TVI_ROOT, TVI_SORT);
				m_cTree.SetItemData(hFamilyItem, (DWORD) pObjTable->GetOwner());
			}
			// add lip synchro item
			hObjTableItem = m_cTree.InsertItem(pObjTable->GetName(), 1, 1, hFamilyItem, TVI_SORT);
			m_cTree.SetItemData(hObjTableItem, (DWORD) pObjTable);
		}
	}
	
	return TRUE;  
}


/*----------------------------------------
----------------------------------------*/
void OT_DialogLoad::OnSelchangeTreeControl (NMHDR* pNMHDR, LRESULT* pResult)
{
	CPA_BaseObject	*pObject;
	HTREEITEM		hCurItem;
	POSITION		pos;

	LPNM_TREEVIEW pNMTreeView = (LPNM_TREEVIEW)pNMHDR;

	if (pNMTreeView->action)
	{
		// get selected item
		hCurItem = m_cTree.GetSelectedItem();
		if (hCurItem != NULL)
		{
			// get corresponding object
			pObject = (CPA_BaseObject *) m_cTree.GetItemData(hCurItem);
			// Obj Table => update list
			if (pObject->GetType() == C_szObjectTableTypeName)
			{
				pos = m_oObjectsToLoad.Find(pObject);
				// if object is already in the list, remove it
				if (pos != NULL)
				{
					m_oObjectsToLoad.RemoveAt(pos);
					m_cTree.SetItemImage(hCurItem, 1, 1);
				}
				// else add it
				else
				{
					m_oObjectsToLoad.AddTail(pObject);
					m_cTree.SetItemImage(hCurItem, 2, 2);
				}
				// remove selection
				m_cTree.SelectItem(NULL);
			}

		}
	}

	*pResult = 0;
}

/*----------------------------------------
----------------------------------------*/
void OT_DialogLoad::OnRightClkTreeControl(NMHDR* pNMHDR, LRESULT* pResult) 
{
	EditorObjectTable	*pObjTable = NULL;
	EditorObjectTable	*pFirstObjTable;
	CPA_BaseObject		*pObject;
	HTREEITEM			hCurItem;
	POSITION			pos;
	POINT				mousePos;
	BOOL				bAdd, bPrev;


	// get selected item
	GetCursorPos(&mousePos);
	m_cTree.ScreenToClient(&mousePos);
	hCurItem = m_cTree.HitTest(mousePos);
	if (hCurItem != NULL)
	{
		pObject = (CPA_BaseObject *) m_cTree.GetItemData(hCurItem);
		// Lip Synchro => update list
		if (pObject->GetType() == C_szObjectTableTypeName)
			pObjTable = (EditorObjectTable *) pObject;
	}
	if (!pObjTable)
		return;
	
	// first click => get first selected object
	if (!m_hFirstItem)
	{
		// register first item
		m_hFirstItem = hCurItem;
		m_cTree.SelectItem(m_hFirstItem);
		// update icon & state
		pFirstObjTable = (EditorObjectTable *) m_cTree.GetItemData(m_hFirstItem);
		if (m_oObjectsToLoad.Find(pFirstObjTable) != NULL)
			m_cTree.SetItemImage(m_hFirstItem, 3, 3);
		else
			m_cTree.SetItemImage(m_hFirstItem, 4, 4);
	}
	// next click => get last selection and update
	else
	{
		pFirstObjTable = (EditorObjectTable *) m_cTree.GetItemData(m_hFirstItem);
		if (pObjTable->GetOwner() == pFirstObjTable->GetOwner())
		{
			bAdd = (m_oObjectsToLoad.Find(pFirstObjTable) == NULL);
			bPrev = (pObjTable->GetName() > pFirstObjTable->GetName());
			// update all items
			while (pObjTable)
			{
				// update list and icon
				pos = m_oObjectsToLoad.Find(pObjTable);
				if (bAdd && !pos)
				{
					m_oObjectsToLoad.AddTail(pObjTable);
					m_cTree.SetItemImage(hCurItem, 2, 2);
				}
				else if (!bAdd && pos)
				{
					m_oObjectsToLoad.RemoveAt(pos);
					m_cTree.SetItemImage(hCurItem, 1, 1);
				}
				// get previous item
				if (pObjTable != pFirstObjTable)
				{
					if (bPrev)
						hCurItem = m_cTree.GetPrevSiblingItem(hCurItem);
					else
						hCurItem = m_cTree.GetNextSiblingItem(hCurItem);
					pObjTable = (EditorObjectTable *) m_cTree.GetItemData(hCurItem);
				}
				else
					pObjTable = NULL;
			}
			m_hFirstItem = NULL;
			m_cTree.SelectItem(m_hFirstItem);
		}
	}
//	*pResult = 0;
}



HTREEITEM OT_DialogLoad::GetCorrespondingItem (CPA_BaseObject *pObject, HTREEITEM hElem)
{
	CPA_BaseObject	*pData;
	HTREEITEM		hChild, hResult;

	// test hElem name
	if (hElem)
	{
		// get item data
		pData = (CPA_BaseObject *) m_cTree.GetItemData(hElem);
		// check item data
		if (pData == pObject)
			return hElem;
	}
	// test all childs
	hChild = (hElem) ? m_cTree.GetChildItem(hElem) : m_cTree.GetRootItem();
	while (hChild)
	{
		hResult = GetCorrespondingItem(pObject, hChild);
		if (hResult)
			return hResult;
		hChild = m_cTree.GetNextSiblingItem(hChild);
	}
	
	return NULL;
}
