// CPACMNDg.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 "EDACDgMC.hpp"

#include "EDACDgIf.hpp"
#include "EDACDgMg.hpp"
#include "EDACModl.hpp"
#include "EDACStrg.hpp"
#include "EDACGlob.hpp"
#include "_Ainterf.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_ModelCreationDialog dialog
BEGIN_MESSAGE_MAP(EdActors_ModelCreationDialog, CDialog)
	//{{AFX_MSG_MAP(EdActors_ModelCreationDialog)
	ON_LBN_DBLCLK(IDC_LIST_FAMILIES, OnDblclkListFamilies)
	ON_LBN_SELCHANGE(IDC_LIST_FAMILIES, OnSelchangeListFamilies)
	ON_LBN_SELCHANGE(IDC_LIST_BITMAPS, OnSelchangeListBitmaps)
	ON_BN_CLICKED(IDC_RADIO_ALL_FAMILIES, OnRadioAllFamilies)
	ON_BN_CLICKED(IDC_RADIO_LOADED_FAMILIES, OnRadioLoadedFamilies)
	ON_BN_CLICKED(IDC_RADIO_UNLOADED_FAMILIES, OnRadioUnloadedFamilies)
	ON_WM_DRAWITEM()
	ON_BN_CLICKED(IDC_CHECK_GEN_DOOR, OnCheckGenDoor)
	ON_EN_CHANGE(IDC_EDIT_CONTAINING, OnChangeEditContaining)
	ON_EN_CHANGE(IDC_EDIT_START_WITH, OnChangeEditStartWith)
	ON_BN_CLICKED(IDC_CHECK_ASPECT, OnCheckAspect)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


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

	//{{AFX_DATA_INIT(EdActors_ModelCreationDialog)
	m_bQuickView = TRUE;
	m_iFilterChoice = 0;
	m_bIsAGenDoor = FALSE;
	m_csStartWith = _T("");
	m_csContaining = _T("");
	//}}AFX_DATA_INIT

	//Gets the current Family List
	m_pclFamilyList = g_pclInterface->GetMainWorld()->fn_p_oGetOriginalObjectList(C_szFamilyTypeName);

	m_hBitmap = NULL;
	m_hNoBitmap = NULL;

	m_pclCreatedModel = NULL;
	m_pclSelectedFamily = NULL;
}

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

	pos = m_clListOfFamilies.GetHeadPosition();
	while ( pos != NULL )
		delete (m_clListOfFamilies.GetNext(pos));

	AfxSetResourceHandle(hOldInstance);
}

//**************************************************************************
void EdActors_ModelCreationDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(EdActors_ModelCreationDialog)
	DDX_Text(pDX, IDC_EDIT_FOR_NAME, m_csNewModelName);
	DDX_Check(pDX, IDC_CHECK_ASPECT, m_bQuickView);
	DDX_Radio(pDX, IDC_RADIO_ALL_FAMILIES, m_iFilterChoice);
	DDX_Check(pDX, IDC_CHECK_GEN_DOOR, m_bIsAGenDoor);
	DDX_Text(pDX, IDC_EDIT_START_WITH, m_csStartWith);
	DDX_Text(pDX, IDC_EDIT_CONTAINING, m_csContaining);
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// EdActors_ModelCreationDialog message handlers

//**************************************************************************
BOOL EdActors_ModelCreationDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//Computes BackGround Color
	m_colrefBackGroundColor = ::GetSysColor(COLOR_MENU);

	//Builds the internal list of Families
	CPA_Family *pclCurrentFamily;
	struct m_stFamilyDescription *p_stDescriptor;
	Position pos = m_pclFamilyList->GetHeadPosition();
	while ( pos != 0 )
	{
		pclCurrentFamily = (CPA_Family *)m_pclFamilyList->GetNext(pos);

		//Adds a descriptor
		p_stDescriptor = new struct m_stFamilyDescription;
		p_stDescriptor->pclFamily = pclCurrentFamily;
		p_stDescriptor->bIsAGenDoorFamily = m_pri_fn_bIsAGenDoorFamily(pclCurrentFamily);

		m_clListOfFamilies.AddTail(p_stDescriptor);
	}

	//Fills the list box with Families
	m_fn_vDisplayFilteredFamilies();

	//Fills the second ListBox with Bitmaps Names
	CString csBitmapsNames = g_pclInterface->m_clDocument.m_csModelsBitmapsCompletePath
														+ "*.bmp";
	CString csFileName;
	HANDLE hFileHandle;
	WIN32_FIND_DATA FindData;
  hFileHandle = FindFirstFile(LPCTSTR(csBitmapsNames), &FindData);
	BOOL bMustGoOn = ( hFileHandle != INVALID_HANDLE_VALUE );
	struct m_stBitmapDescription *p_stBMDesc;

	while ( bMustGoOn )
	{
		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);
		
		bMustGoOn = FindNextFile(hFileHandle, &FindData);
	}

	FindClose(hFileHandle);

	//Gets the Bitmaps for GenDoors
	csBitmapsNames = g_pclInterface->m_clDocument.m_csModelsBitmapsCompletePathForGenDoors
														+ "*.bmp";
  BOOL bModFound;
	CString csModFileName;
	CFile cfModFile;
	hFileHandle = FindFirstFile(LPCTSTR(csBitmapsNames), &FindData);
	bMustGoOn = ( hFileHandle != INVALID_HANDLE_VALUE );

	while ( bMustGoOn )
	{
		csFileName = FindData.cFileName;

		//Searches for the corresponding ".MOD"
		csModFileName = csFileName.Left(csFileName.GetLength() - 3) + "mod";
		csModFileName = g_pclInterface->m_clDocument.m_csModelsBitmapsCompletePathForGenDoors + csModFileName;
		bModFound = cfModFile.Open(csModFileName, CFile::modeRead);

		if ( bModFound )
		{
			cfModFile.Close();

			//Adds to internal list
			p_stBMDesc = new struct m_stBitmapDescription;
			p_stBMDesc->csFileName = csFileName;
			p_stBMDesc->hBitmap = NULL;
			p_stBMDesc->bLoaded = FALSE;

			m_clListOfBitmapsForGenDoors.AddTail(p_stBMDesc);
		}
		
		bMustGoOn = FindNextFile(hFileHandle, &FindData);
	}

	FindClose(hFileHandle);

	//Initial setting is "Actors mode"
	CButton *pclGenDoorButton = (CButton *)GetDlgItem(IDC_CHECK_GEN_DOOR);
	pclGenDoorButton->SetCheck(TRUE);	//Init
	m_fn_vFillListBoxWithBitmapNames(&m_clListOfBitmapsForActors);

	//////////////
	//////////////
	//Registers for TUT Module
	TUT_M_vGetTutDll();
	
	TUT_M_vRegisterControlID(IDC_LIST_FAMILIES, "OAc_ModelCreationDialog_FamiliesListBox", TUT_e_ListBox);
	TUT_M_vRegisterControlID(IDC_LIST_BITMAPS, "OAc_ModelCreationDialog_BitmpasListBox", TUT_e_ListBox);
	TUT_M_vRegisterControlID(IDC_EDIT_FOR_NAME, "OAc_ModelCreationDialog_ModelNameEdit", TUT_e_TextEdit);
	TUT_M_vRegisterControlID(IDC_RADIO_ALL_FAMILIES, "OAc_ModelCreationDialog_AllFamiliesButton", TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_RADIO_LOADED_FAMILIES, "OAc_ModelCreationDialog_LoadedFamiliesButton", TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_RADIO_UNLOADED_FAMILIES, "OAc_ModelCreationDialog_UnloadedFamiliesButton", TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_EDIT_START_WITH, "OAc_ModelCreationDialog_StartWithEdit", TUT_e_TextEdit);
	TUT_M_vRegisterControlID(IDC_EDIT_CONTAINING, "OAc_ModelCreationDialog_ContainingEdit", TUT_e_TextEdit);
	TUT_M_vRegisterControlID(IDC_CHECK_ASPECT, "OAc_ModelCreationDialog_QuickViewButton", TUT_e_Button);
	TUT_M_vRegisterControlID(IDOK, "OAc_ModelCreationDialog_OKButton", TUT_e_Button);
	TUT_M_vRegisterControlID(IDCANCEL, "OAc_ModelCreationDialog_CancelButton", TUT_e_Button);

	//End of Registers for TUT Module
	//////////////

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//**************************************************************************
void EdActors_ModelCreationDialog::OnDestroy() 
{
	//////////////
	//////////////
	//UnRegisters for TUT Module
	TUT_M_vGetTutDll();
	
	TUT_M_vUnregisterControlID(IDC_LIST_FAMILIES);
	TUT_M_vUnregisterControlID(IDC_LIST_BITMAPS);
	TUT_M_vUnregisterControlID(IDC_EDIT_FOR_NAME);
	TUT_M_vUnregisterControlID(IDC_RADIO_ALL_FAMILIES);
	TUT_M_vUnregisterControlID(IDC_RADIO_LOADED_FAMILIES);
	TUT_M_vUnregisterControlID(IDC_RADIO_UNLOADED_FAMILIES);
	TUT_M_vUnregisterControlID(IDC_EDIT_START_WITH);
	TUT_M_vUnregisterControlID(IDC_EDIT_CONTAINING);
	TUT_M_vUnregisterControlID(IDC_CHECK_ASPECT);
	TUT_M_vUnregisterControlID(IDOK);
	TUT_M_vUnregisterControlID(IDCANCEL);
	
	//End of UnRegisters for TUT Module
	//////////////

	CDialog::OnDestroy();
}

//**************************************************************************
void EdActors_ModelCreationDialog::OnOK() 
{
	AfxGetApp()->DoWaitCursor(1);

	UpdateData(TRUE);

	BOOL bMustContinue = TRUE;

	//Checks if Family has an animation
	if ( m_pclSelectedFamily == NULL )
	{
		CString csMessage = "You must choose a family for the Model\nor 'Cancel' !!";
		EdActors_InformationDialog dial(&g_oBaseFrame, csMessage);
		dial.DoModal();
	
		bMustContinue = FALSE;
	}
 
	//Checks Name
	if ( bMustContinue )
	{
		if ( m_csNewModelName.IsEmpty() )
		{
			if ( MessageBox("You did not give any name to your new Model !\nIf you chose 'OK', a default name will be given.",
										  "Model Creation - WARNING !",
										  MB_OKCANCEL | MB_ICONEXCLAMATION) == IDCANCEL )
				bMustContinue = FALSE;
		}
		else
			fn_vCheckAndChangeName(m_csNewModelName);
	}

	if ( bMustContinue )
	{
		//Loads Family if necessary
		if ( !m_pclSelectedFamily->fn_bIsAvailable() )
		{
			CPA_DLLBase *p_clFamilyDll = g_pclInterface->GetMainWorld()->GetDLLWithTypeName(C_szDLLFamilyName);

			ERROR_ASSERT( p_clFamilyDll != NULL );

			//Displays message window
			CString csMessageString = "Loading Family '" + m_pclSelectedFamily->GetName() + "'... Please wait !";
			EdActors_Dialog_Message MessageDial(csMessageString);
			MessageDial.m_pub_fn_bCreate();

			ERROR_VERIFY( p_clFamilyDll->fn_bLoadBaseObject(m_pclSelectedFamily) );
		}
	
		//Creates all needed objects
 		
		///////////////////////////////
		//Creates Editor's Actor
		CPA_Actor *pclNewEditorActor = new CPA_Actor(	NULL,
																									NULL,
																									g_c_csActorModelType,
																									NULL,
																									m_pclSelectedFamily,
																									NULL,
																									m_csNewModelName);

		pclNewEditorActor->m_fn_vMakeGenDoor(m_bIsAGenDoor);

		////////////////////////////////////////////////////////
		//Creates a SuperObject to place in the hierarchy
		m_pclCreatedModel = g_pclInterface->GetInterface()->GetNewSuperObject(E_ss_NoSave,
																																					C_Protected,
																																					"",
																																					"");

		pclNewEditorActor->m_fn_vMakeGenDoor(m_bIsAGenDoor);
		pclNewEditorActor->m_pub_fn_vSetSuperObject(m_pclCreatedModel);
		pclNewEditorActor->m_fn_vSetModelDefaultValues();
		
		m_pclCreatedModel->SetModelName(pclNewEditorActor->GetName());
		m_pclCreatedModel->SetObject(pclNewEditorActor);
		//PROVISOIRE...
		m_pclCreatedModel->GetStruct()->hLocalMatrix = (GEO_tdxHandleToMatrix)malloc(sizeof(POS_tdstCompletePosition));
		POS_fn_vSetIdentityMatrix(m_pclCreatedModel->GetStruct()->hLocalMatrix);

		EndDialog(IDOK);
	}

	AfxGetApp()->DoWaitCursor(-1);
}

//**************************************************************************
void EdActors_ModelCreationDialog::OnSelchangeListFamilies() 
{
  CListBox *pclLB = (CListBox *)GetDlgItem(IDC_LIST_FAMILIES);
	short wIndex = pclLB->GetCurSel();
	if ( wIndex != LB_ERR )
	{
 		m_pclSelectedFamily = ((struct m_stFamilyDescription *)(pclLB->GetItemDataPtr(wIndex)))->pclFamily;

		if ( ((struct m_stFamilyDescription *)(pclLB->GetItemDataPtr(wIndex)))->bIsAGenDoorFamily )
		{
			m_bIsAGenDoor = TRUE;
			m_fn_vFillListBoxWithBitmapNames(&m_clListOfBitmapsForGenDoors);
		}
		else
		{
			m_bIsAGenDoor = FALSE;
			m_fn_vFillListBoxWithBitmapNames(&m_clListOfBitmapsForActors);
		}
	}
}

//**************************************************************************
void EdActors_ModelCreationDialog::OnDblclkListFamilies() 
{
//	OnOK();
}

//**************************************************************************
void EdActors_ModelCreationDialog::OnRadioAllFamilies() 
{
	UpdateData(TRUE);
	m_fn_vDisplayFilteredFamilies();
}

//**************************************************************************
void EdActors_ModelCreationDialog::OnRadioLoadedFamilies() 
{
	UpdateData(TRUE);
	m_fn_vDisplayFilteredFamilies();
}

//**************************************************************************
void EdActors_ModelCreationDialog::OnRadioUnloadedFamilies() 
{
	UpdateData(TRUE);
	m_fn_vDisplayFilteredFamilies();
}

//**************************************************************************
void EdActors_ModelCreationDialog::OnChangeEditContaining() 
{
	UpdateData(TRUE);
	m_fn_vDisplayFilteredFamilies();
}

//**************************************************************************
void EdActors_ModelCreationDialog::OnChangeEditStartWith() 
{
	UpdateData(TRUE);
	m_fn_vDisplayFilteredFamilies();
}

//**************************************************************************
//Fills the list box with Families
void EdActors_ModelCreationDialog::m_fn_vDisplayFilteredFamilies() 
{
	//Fills the list box with Families
	CListBox *pclLB = (CListBox *)GetDlgItem(IDC_LIST_FAMILIES);

	pclLB->ResetContent();

	BOOL bMustDisplayCurrentFamily;
	short wIndex;
	struct m_stFamilyDescription *p_stCurrentFamilyDescriptor;
	CPA_Family *pclCurrentFamily;
	POSITION pos = m_clListOfFamilies.GetHeadPosition();
	while ( pos != 0 )
	{		
		p_stCurrentFamilyDescriptor = m_clListOfFamilies.GetNext(pos);
		pclCurrentFamily = p_stCurrentFamilyDescriptor->pclFamily;

		switch ( m_iFilterChoice )
		{
		//All families
		case 0:
			bMustDisplayCurrentFamily = TRUE;
			break;
		//Loaded families
		case 1:
	 		bMustDisplayCurrentFamily = ( pclCurrentFamily->fn_bIsAvailable() );
			break;
		//Unloaded families
		case 2:
	 		bMustDisplayCurrentFamily = ( !(pclCurrentFamily->fn_bIsAvailable()) );
			break;
		default:
			bMustDisplayCurrentFamily = FALSE;
			break;
		}

		if ( bMustDisplayCurrentFamily )
		{
			CString csFamilyName = pclCurrentFamily->GetName();
			csFamilyName.MakeLower();

			//Looks for strings
			if ( !m_csStartWith.IsEmpty() )
			{
				m_csStartWith.MakeLower();
				bMustDisplayCurrentFamily = ( csFamilyName.Left(m_csStartWith.GetLength()).CompareNoCase(m_csStartWith) == 0 );
			}

			if ( bMustDisplayCurrentFamily )
				if ( !m_csContaining.IsEmpty() )
				{
					m_csContaining.MakeLower();
					bMustDisplayCurrentFamily = ( csFamilyName.Find(m_csContaining) != -1 );
				}

			if ( bMustDisplayCurrentFamily )
			{
				wIndex = pclLB->AddString(pclCurrentFamily->GetName());
				pclLB->SetItemDataPtr(wIndex, (void *)p_stCurrentFamilyDescriptor);
			}
		}
	}

	m_pclSelectedFamily = NULL;
}

//**************************************************************************
CPA_SuperObject *EdActors_ModelCreationDialog::m_fn_pclGetNewModel()
{
	return m_pclCreatedModel;
}

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

//**************************************************************************
void EdActors_ModelCreationDialog::OnCheckAspect() 
{
	UpdateData(TRUE);

	if ( !m_bQuickView )
	{
		if ( m_hNoBitmap == NULL )
			if ( m_clBitmapNoImage.LoadBitmap(IDB_BITMAP_NO_IMAGE) )
				m_hNoBitmap = HBITMAP(m_clBitmapNoImage);

		CStatic *pclStatic = (CStatic *)GetDlgItem(IDC_PICTURES);
		pclStatic->SetBitmap(m_hNoBitmap);
	}
	else
		OnSelchangeListBitmaps();
}

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

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

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

		if ( (m_bQuickView) && (!p_stBMDesc->bLoaded) )
		{
			CString csBitmapNameAndPath;
			if ( m_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;
		}

		m_hBitmap = p_stBMDesc->hBitmap;
		m_csBitmapSize = p_stBMDesc->csSize;

		if ( m_bQuickView )
		{
			//Sets size Text
			CWnd *pclStaticWnd = GetDlgItem(IDC_PICTURE_SIZE);
			CString csSizeText;
			csSizeText.Format("%i x %i", m_csBitmapSize.cx, m_csBitmapSize.cy);
			pclStaticWnd->SetWindowText(csSizeText);

			//Displays it
			CStatic *pclStatic = (CStatic *)GetDlgItem(IDC_PICTURES);
			pclStatic->SetBitmap(m_hBitmap);
		}
	}
}

//**************************************************************************
BOOL EdActors_ModelCreationDialog::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;
}

//**************************************************************************
void EdActors_ModelCreationDialog::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	if ( nIDCtl == IDC_LIST_FAMILIES )
	{
		ERROR_ASSERT( lpDrawItemStruct->CtlType == ODT_LISTBOX );
			
		CDC *pclDC;
		pclDC = pclDC->FromHandle(lpDrawItemStruct->hDC);
		CRect crRect(lpDrawItemStruct->rcItem);

		CFont *pclFont = GetFont();
		pclDC->SelectObject(pclFont);

		struct m_stFamilyDescription *p_stDescriptor = (struct m_stFamilyDescription *)lpDrawItemStruct->itemData;
		CPA_Family *pclFamily = p_stDescriptor->pclFamily;
		
		COLORREF colRef;
		COLORREF colRefForBackground;
		if ( (lpDrawItemStruct->itemState	& ODS_SELECTED) != 0 )
		{
			colRef = pclFamily->fn_bIsAvailable()	? EdActors_C_INVERSE_COLOR_FOR_LOADED_FAMILIES
																						: EdActors_C_INVERSE_COLOR_FOR_UNLOADED_FAMILIES;
			colRefForBackground = pclFamily->fn_bIsAvailable()	? EdActors_C_COLOR_FOR_SELECTED_LOADED_FAMILIES
																													: EdActors_C_COLOR_FOR_SELECTED_UNLOADED_FAMILIES;
		}
		else
		{
			colRef = pclFamily->fn_bIsAvailable()	? EdActors_C_COLOR_FOR_LOADED_FAMILIES
																						: EdActors_C_COLOR_FOR_UNLOADED_FAMILIES;
			colRefForBackground = m_colrefBackGroundColor;
		}

 		pclDC->SetTextColor(colRef);
 		pclDC->SetBkMode(TRANSPARENT);

		pclDC->FillSolidRect(&(lpDrawItemStruct->rcItem), colRefForBackground);

		CString csCurrentText = p_stDescriptor->bIsAGenDoorFamily ? EdActors_C_szGenDoorsDescriptor : EdActors_C_szNormalDescriptor;
		csCurrentText += pclFamily->GetName();
 		pclDC->TextOut(crRect.left + 1, crRect.top + 1, csCurrentText);
	}
}

//**************************************************************************
void EdActors_ModelCreationDialog::OnCheckGenDoor() 
{
	UpdateData(TRUE);
	
	if ( m_bIsAGenDoor )
		m_fn_vFillListBoxWithBitmapNames(&m_clListOfBitmapsForGenDoors);
	else
		m_fn_vFillListBoxWithBitmapNames(&m_clListOfBitmapsForActors);
}

//**************************************************************************
void EdActors_ModelCreationDialog::m_fn_vFillListBoxWithBitmapNames(CPA_List<struct m_stBitmapDescription> *_pclListOfBitmaps)
{
	BOOL bMustSwap = FALSE;

	//Gets current list type
	CButton *pclGenDoorButton = (CButton *)GetDlgItem(IDC_CHECK_GEN_DOOR);

	if ( pclGenDoorButton->GetCheck() )
	{
		//Must swap modes
		if ( !m_bIsAGenDoor )
			bMustSwap = TRUE;
	}
	else
	{
		//Must swap modes
		if ( m_bIsAGenDoor )
			bMustSwap = TRUE;
	}

	if ( bMustSwap )
	{
		pclGenDoorButton->SetCheck( !pclGenDoorButton->GetCheck() );

		CListBox *pclLB = (CListBox *)GetDlgItem(IDC_LIST_BITMAPS);
		pclLB->ResetContent();

		short wIndex;
		struct m_stBitmapDescription *pstCurrentBDesc;
		POSITION pos = _pclListOfBitmaps->GetHeadPosition();
		while (pos != NULL )
		{
			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();
		}
	}
}

//**************************************************************************
BOOL EdActors_ModelCreationDialog::m_pri_fn_bIsAGenDoorFamily(CPA_Family *_pclFamily)
{
	BOOL bReturn = FALSE;

	//Gets the Family Subfolder
	CString csFolderName = _pclFamily->GetCompleteSectionName();
	short wIndex = csFolderName.ReverseFind('\\');
	if ( wIndex != -1 )
		csFolderName = csFolderName.Left(++wIndex);

	//Looks in Family subfolder for the CHL or TBL file
	HANDLE hFileHandle;
	WIN32_FIND_DATA FindData;
	
	// check if CHL exists
	CString csFileName = csFolderName + "*.chl";

	hFileHandle = FindFirstFile(LPCTSTR(csFileName), &FindData);
	bReturn = (hFileHandle == INVALID_HANDLE_VALUE);

	FindClose(hFileHandle);

	if (!bReturn)
		return FALSE;

	// checks if TBL exist
	csFileName = csFolderName + "*.tbl";

	hFileHandle = FindFirstFile(LPCTSTR(csFileName), &FindData);
	bReturn = ( hFileHandle == INVALID_HANDLE_VALUE );

	FindClose(hFileHandle);

	if (!bReturn)
		return FALSE;

	// Look in Specific directories
	char szVersion[255];
	FILE *hIni;

	// get info from ini file
	hIni = fopen("version.ini", "rt");
	if(hIni == NULL)
		hIni = fopen("x:\\cpa\\exe\\main\\version.ini", "rt");
	if(hIni)
	{
		fgets(szVersion, 49, hIni);
		while(!isalnum(szVersion[strlen(szVersion) - 1]))
			szVersion[strlen(szVersion) - 1] = '\0';
		fclose(hIni);
	}
	// if no specific version, no more search
	else
		return bReturn;

	char szVersion1[255];
	char szVersion2[255];
	char *szCut;
	int	 iNumVersion, iNbVersions = 1;

	strcpy(szVersion1, szVersion);
	szCut = strchr(szVersion1, ',');
	if (szCut)
	{
		strcpy(szVersion2, szCut+1 );
		strcpy(szCut, "\0");
		iNbVersions = 2;
	}

	for (iNumVersion = 0; iNumVersion < iNbVersions; iNumVersion++)
	{
		// build specific path
		wIndex = csFolderName.Find('\\');
		if (wIndex != -1)
			csFolderName = (iNumVersion ? szVersion2 : szVersion1) + csFolderName.Mid(wIndex);

		// check if CHL exists
		csFileName = csFolderName + "*.chl";

		hFileHandle = FindFirstFile(LPCTSTR(csFileName), &FindData);
		bReturn = (hFileHandle == INVALID_HANDLE_VALUE);

		FindClose(hFileHandle);

		if (!bReturn)
			return FALSE;

		// checks if TBL exist
		csFileName = csFolderName + "*.tbl";

		hFileHandle = FindFirstFile(LPCTSTR(csFileName), &FindData);
		bReturn = (hFileHandle == INVALID_HANDLE_VALUE);

		FindClose(hFileHandle);

		if (!bReturn)
			return FALSE;
	}

	return bReturn;
}

