// CPACDgMI.cpp : implementation file
/////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "ACP_Base.h"
#include "ITF.h"
/*
#include "TAc.h"
#include "TFa.h"
#include "IncMEC.h"
#define D_State_Define
#include "IncGAM.h"
#undef D_State_Define
#include "GLI.h"
*/
#include "EDACDgMi.hpp"
/*
#include "EDACDgIf.hpp"
#include "EDACDgMg.hpp"
#include "EDACModl.hpp"
#include "EDACStrg.hpp"
#include "EDACGlob.hpp"
*/
#include "_Ainterf.hpp"
#include "EdAcDoc.hpp"

//External Modules
#include "ErO.h"
#include "IncTUT.h"
//End of External Modules

#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
    #include "_Actors.hpp"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT
/*

#define EdActors_C_COLOR_FOR_LOADED_FAMILIES							(RGB(255,0,0))
#define EdActors_C_INVERSE_COLOR_FOR_LOADED_FAMILIES			(RGB(255,150,150))
#define EdActors_C_COLOR_FOR_SELECTED_LOADED_FAMILIES			(RGB(150,100,100))

#define EdActors_C_COLOR_FOR_UNLOADED_FAMILIES						(RGB(96,96,96))
#define EdActors_C_INVERSE_COLOR_FOR_UNLOADED_FAMILIES		(RGB(255,255,255))
#define EdActors_C_COLOR_FOR_SELECTED_UNLOADED_FAMILIES		(RGB(96,96,255))

#define EdActors_C_szGenDoorsDescriptor		"[GenDoor] "
#define EdActors_C_szNormalDescriptor			"[Normal]  "
*/

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

/////////////////////////////////////////////////////////////////////////////
// EdActors_ModelIconDialog dialog
BEGIN_MESSAGE_MAP(EdActors_ModelIconDialog, CDialog)
	//{{AFX_MSG_MAP(EdActors_ModelIconDialog)
	ON_LBN_SELCHANGE(IDC_BITMAP_LIST, OnSelchangeListBitmaps)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//**************************************************************************
EdActors_ModelIconDialog::EdActors_ModelIconDialog(CWnd* pParent, CPA_SuperObject *pModel)
		: CDialog(EdActors_ModelIconDialog::IDD, &g_oBaseFrame)
{
	hOldInstance = AfxGetResourceHandle();
	AfxSetResourceHandle(g_stActorEditorIdentity.hModule);

	//{{AFX_DATA_INIT(EdActors_ModelIconDialog)
	//}}AFX_DATA_INIT

	m_pModel = pModel;
	m_csBitmapName = "";

}

//**************************************************************************
EdActors_ModelIconDialog::~EdActors_ModelIconDialog()
{
	// deletes all bitmap descriptions
	POSITION pos = m_clListOfBitmapsForActors.GetHeadPosition();
	while ( pos != NULL )
		delete (m_clListOfBitmapsForActors.GetNext(pos));

	AfxSetResourceHandle(hOldInstance);
}

//**************************************************************************
void EdActors_ModelIconDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(EdActors_ModelIconDialog)
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// EdActors_ModelIconDialog message handlers

//**************************************************************************
BOOL EdActors_ModelIconDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// get the list of all bitmaps
	struct m_stBitmapDescription	*p_stBMDesc;
	WIN32_FIND_DATA					FindData;
	CString							csBitmapsNames;
	CString							csFileName;
	HANDLE							hFileHandle;
	
	// get parameters
	CPA_Actor *pclEdtModel = (CPA_Actor *)m_pModel->GetObject();
	if (pclEdtModel->m_fn_bIsAGenDoor())
		csBitmapsNames = g_pclInterface->m_clDocument.m_csModelsBitmapsCompletePathForGenDoors + "*.bmp";
	else
		csBitmapsNames = g_pclInterface->m_clDocument.m_csModelsBitmapsCompletePath + "*.bmp";

	hFileHandle = FindFirstFile(LPCTSTR(csBitmapsNames), &FindData);

	if (hFileHandle != INVALID_HANDLE_VALUE)
	{
		do
		{
			csFileName = FindData.cFileName;
			
			//Adds to internal list
			p_stBMDesc = new struct m_stBitmapDescription;
			p_stBMDesc->csFileName = csFileName;
			p_stBMDesc->hBitmap = NULL;
			p_stBMDesc->bLoaded = FALSE;

			m_clListOfBitmapsForActors.AddTail(p_stBMDesc);
		
		}
		while (FindNextFile(hFileHandle, &FindData));
	}

	FindClose(hFileHandle);

	m_fn_vFillListBoxWithBitmapNames(&m_clListOfBitmapsForActors);

	return TRUE;  
}

//**************************************************************************
void EdActors_ModelIconDialog::OnDestroy() 
{
	CDialog::OnDestroy();
}

//**************************************************************************
void EdActors_ModelIconDialog::OnOK() 
{
	// get selection
	CListBox *pclLB = (CListBox *)GetDlgItem(IDC_BITMAP_LIST);
	if (pclLB->GetCurSel() != -1)
	{
		char szBitmap[256];
		pclLB->GetText(pclLB->GetCurSel(), szBitmap);
		m_csBitmapName = szBitmap;
	}
	CDialog::OnOK();
}

//**************************************************************************
CString EdActors_ModelIconDialog::m_fn_csGetBitmapName()
{
	return m_csBitmapName;
}

//**************************************************************************
void EdActors_ModelIconDialog::m_fn_vFillListBoxWithBitmapNames(CPA_List<struct m_stBitmapDescription> *_pclListOfBitmaps)
{
	struct m_stBitmapDescription	*pstCurrentBDesc;
	CListBox						*pclLB;
	POSITION						pos;
	short							wIndex;
	
	// reset list box
	pclLB = (CListBox *)GetDlgItem(IDC_BITMAP_LIST);
	pclLB->ResetContent();

	// fill list with bitmaps
	pos = _pclListOfBitmaps->GetHeadPosition();
	while (pos)
	{
		pstCurrentBDesc = _pclListOfBitmaps->GetNext(pos);
 		wIndex = pclLB->AddString(pstCurrentBDesc->csFileName);
		pclLB->SetItemDataPtr(wIndex, (void *)pstCurrentBDesc);
	}

	//Selects first bitmap
	if (pclLB->GetCount() > 0)
	{
		pclLB->SetCurSel(0);
		OnSelchangeListBitmaps();
	}
}

//**************************************************************************
void EdActors_ModelIconDialog::OnSelchangeListBitmaps() 
{
	CListBox *pclLB = (CListBox *)GetDlgItem(IDC_BITMAP_LIST);
	short wIndex = pclLB->GetCurSel();

	if ( wIndex != LB_ERR )
	{
		pclLB->GetText(wIndex, m_csBitmapName);

		CWnd *pclWnd = GetDlgItem(IDC_SHOW_BITMAP);
		CRect crPictureRect;
		pclWnd->GetWindowRect(crPictureRect);
	
		struct m_stBitmapDescription *p_stBMDesc;
		p_stBMDesc = (struct m_stBitmapDescription *)pclLB->GetItemDataPtr(wIndex);

		if (!p_stBMDesc->bLoaded)
		{
			CString csBitmapNameAndPath;
			CPA_Actor *pclEdtModel = (CPA_Actor *)m_pModel->GetObject();
			if (pclEdtModel->m_fn_bIsAGenDoor())
				csBitmapNameAndPath = g_pclInterface->m_clDocument.m_csModelsBitmapsCompletePathForGenDoors + m_csBitmapName;
			else
 				csBitmapNameAndPath = g_pclInterface->m_clDocument.m_csModelsBitmapsCompletePath + m_csBitmapName;

			//Gets size
			m_fn_bGetBitmapSize(csBitmapNameAndPath, &p_stBMDesc->csSize);
			int xSize, ySize;
			double dfRatio;
			if ( p_stBMDesc->csSize.cx > p_stBMDesc->csSize.cy )
			{
				xSize = crPictureRect.Width();
 				dfRatio = (double)crPictureRect.Width() / (double)p_stBMDesc->csSize.cx;
				ySize = (int)(p_stBMDesc->csSize.cy * dfRatio);
			}
			else
			{
				ySize = crPictureRect.Height();
 				dfRatio = (double)crPictureRect.Height() / (double)p_stBMDesc->csSize.cy;
				xSize = (int)(p_stBMDesc->csSize.cx * dfRatio);
			}

			//Loads Bitmap
			p_stBMDesc->hBitmap = (HBITMAP)LoadImage(NULL, csBitmapNameAndPath, IMAGE_BITMAP, xSize, ySize, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
			
			p_stBMDesc->bLoaded = TRUE;
		}

		// Displays bitmap
		CStatic *pclStatic = (CStatic *)GetDlgItem(IDC_SHOW_BITMAP);
		pclStatic->SetBitmap(p_stBMDesc->hBitmap);
	}
}

//**************************************************************************
BOOL EdActors_ModelIconDialog::m_fn_bGetBitmapSize(CString csFileName, CSize *sz)
{
	FILE *hFile;
	BITMAPFILEHEADER bf;
	BITMAPINFOHEADER bi;

	if ( (hFile = fopen( csFileName, "rb")) == NULL)
		return FALSE;

	if (sizeof(bf) == fread( &bf, 1, sizeof(bf), hFile ))
	{
		if (bf.bfType == 0x4d42 /*BM*/)
		{
			if ( sizeof(bi) == fread( &bi, 1, sizeof(bi), hFile) )
			{
				sz->cx = bi.biWidth;
				sz->cy = bi.biHeight;
				fclose(hFile);
				return TRUE;
			}
		}
	}
	fclose(hFile);
	return FALSE;
}

