/*=========================================================================
*
* DlgMain1.cpp	-	implementation file for main window class
*					Dialog functions
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include <io.h>
#include "stdafx.h"

#include "DlgMain.h"

#include "print.h"
#include "main.h"
#include "system.h"

#include "Help.h"
#include "IniData.h"
#include "resource.hm"

char g_szAnims[255];
char g_szBanks[255];
char g_szClasses[255];
char g_szFamilies[255];
char g_szLevels[255];
char g_szTextures[255];

extern "C"
{
BOOL g_bStandardConfig = TRUE;
}


//==================================================//
//				HELP								//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on IDC_BUTTON_HELP button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckHelp() 
{
	CButton *p_oCheck = ((CButton *) GetDlgItem (IDC_BUTTON_HELP));
	RECT	stRect;

	CMenu oMenu;
	oMenu.CreatePopupMenu();
		
	oMenu.AppendMenu( MF_STRING | MF_ENABLED, IDM_ABOUTBOX, "About ...");
	oMenu.AppendMenu( MF_STRING | MF_ENABLED, IDM_OPENHELP, "Help");

	p_oCheck->GetWindowRect(&stRect);
	oMenu.TrackPopupMenu( TPM_LEFTBUTTON, stRect.left, stRect.bottom, this  );
	oMenu.DestroyMenu();
}

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on IDM_OPENHELP entry
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnHelp() 
{
	xString sDirectory;
	int		nPanel;
	
	// set directory
	GetCurrentDirectory(256, sDirectory);
	SetCurrentDirectory(g_sInitialDir);

	// get the current panel 
	nPanel = m_oMainPanel.GetCurSel();

	switch(nPanel)
	{
		// the "Options" panel
		case 0:
			::WinHelp(this->GetSafeHwnd(), C_szMainHelpFile, HELP_CONTEXT, ID_USAGE_PATHS );	
			break;
		// the "Selection" panel
		case 1: 
			::WinHelp(this->GetSafeHwnd(), C_szMainHelpFile, HELP_CONTEXT, ID_USAGE_SELECTION );	
			break;
		// the "Result" panel
		case 2:
			::WinHelp(this->GetSafeHwnd(), C_szMainHelpFile, HELP_CONTEXT, ID_USAGE_CONVERSION );	
			break;
	}
	SetCurrentDirectory(sDirectory);
}

/****************************************************************************
*		Description:	Handle function for message WM_HELPINFO
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
BOOL CDlgMain::OnHelpInfo(HELPINFO* pHelpInfo) 
{
//	AfxGetApp()->WinHelp(pHelpInfo->dwContextId, HELP_CONTEXTPOPUP);	
	OnHelp();
	return TRUE;
}


//==================================================//
//				CHANGE PANEL						//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message TCN_SELCHANGE on main panel
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnSelchangePanel(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// get current panel
	int  nPanel = m_oMainPanel.GetCurSel();
	
	// check data validity before changing
	BOOL bResult = m_fn_vTestIniDatas();

	// get the current panel and for it show only its controls 
	switch(nPanel)
	{
	// "Options" panel
	case 0:
		m_fn_TabC_vShowControlsPanelDir() ;
		break;
	// "Selection" panel
	case 1: 
		if (bResult)
		{	
			m_fn_TabC_vShowControlsPanelDir() ;
			m_oMainPanel.SetCurSel(0);
		}
		else
			m_fn_TabC_vShowControlsPanelSel() ;
		break;
	// "Result" panel
	case 2:
		if (bResult)
		{	
			m_fn_TabC_vShowControlsPanelDir() ;
			m_oMainPanel.SetCurSel(0);
		}
		else
			m_fn_TabC_vShowControlsPanelRes() ;
		break;
	}
	*pResult = 0;
}

/****************************************************************************
*		Description:	Handle function for message TCN_SELCHANGE on config panel
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnSelchangeTab(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// get current panel
	int  nPanel = m_oConfigPanel.GetCurSel();
	
	// get the current panel and for it show only its controls 
	switch(nPanel)
	{
		// "Current" panel
		case 0:
			if (m_bConversion)
			{	
				m_fn_vShowControlsPanelCustom() ;
				g_bStandardConfig = FALSE;
				m_oConfigPanel.SetCurSel(1);
			}
			else
			{	
				m_fn_vShowControlsPanelCurrent();
				g_bStandardConfig = TRUE;
				m_fn_vResetSelection();
				m_fn_vUpdateForSelectConfig();
			}
			break;
		// "Custom" panel
		case 1: 
			if (m_bConversion)
			{	
				m_fn_vShowControlsPanelCurrent() ;
				g_bStandardConfig = TRUE;
				m_oConfigPanel.SetCurSel(0);
			}
			else
			{	
				m_fn_vShowControlsPanelCustom();
				g_bStandardConfig = FALSE;
				m_fn_vResetSelection();
				m_fn_vUpdateForSelectConfig();
			}
			break;
	}
	*pResult = 0;
}



//==================================================//
//				RENAME DIRECTORIES					//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message EN_KILLFOCUS on edit box Game Specific1 
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusGameSpecific1()
{
	char	szName[15];
	
	m_oEditGameSp1.GetWindowText(szName, 14);
	if (strcmp(szName, ""))
	{
		fn_IniData_vSetGameSp1(szName);
		strcpy(g_stInitialData.szGameSp1, szName);
	}
}



/****************************************************************************
*		Description:	Handle function for message EN_KILLFOCUS on edit box Game Specific2
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusGameSpecific2()
{
	char	szName[15];

	m_oEditGameSp2.GetWindowText(szName, 14);
	if (strcmp(szName, ""))
	{
		fn_IniData_vSetGameSp2(szName);
		strcpy(g_stInitialData.szGameSp2, szName);
	}
}


/****************************************************************************
*		Description:	Handle function for message EN_KILLFOCUS on edit box Raw Specific1
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusRawSpecific1()
{
	char	szName[15];

	m_oEditRawSp1.GetWindowText(szName, 14);
	if (strcmp(szName, ""))
	{
		fn_IniData_vSetRawSp1(szName);
		strcpy(g_stInitialData.szRawSp1, szName);
	}
}


/****************************************************************************
*		Description:	Handle function for message EN_KILLFOCUS on edit box Raw Specific2
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusRawSpecific2()
{
	char	szName[15];
	
	m_oEditRawSp2.GetWindowText(szName, 14);
	if (strcmp(szName, ""))
	{
		fn_IniData_vSetRawSp2(szName);
		strcpy(g_stInitialData.szRawSp2, szName);
	}
}



//==================================================//
//				CHOOSE DIRECTORIES					//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on GameData radio button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckGameData()
{
	if (m_oCheckGameData.GetCheck())
	{
		m_oCheckGameData.SetCheck(1);
		m_oCheckGameSP1.SetCheck(0);
		m_oCheckGameSP2.SetCheck(0);
		g_iData = 0;
		m_fn_vResetSelection() ;
		fn_ComRes_vDeleteLines( &m_oRichEditCom);				
		fn_ComRes_vDeleteLines( &m_oRichEditRes);				

		if (!g_iData && !g_bAskFamily) 
			m_oCheckCommon.EnableWindow(TRUE);
		if (g_iData && !g_bAskFamily) 
			m_oCheckSpecific.EnableWindow(TRUE);

		m_fn_vUpdateForSelectConfig();
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on GameSpecific1 radio button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckGameSpecific1()
{
	if (m_oCheckGameSP1.GetCheck())
	{
		if (strcmp(g_stInitialData.szGameSpecific1, ""))
		{	
			m_oCheckGameSP1.SetCheck(1);
			m_oCheckGameData.SetCheck(0);
			m_oCheckGameSP2.SetCheck(0);
			g_iData=1;
		}
		else  
		{
			m_oCheckGameSP1.SetCheck(0);
			m_oCheckGameSP2.SetCheck(0);
			m_oCheckGameData.SetCheck(1);
			g_iData=0;
		}
	}
	m_fn_vResetSelection() ;
    fn_ComRes_vDeleteLines( &m_oRichEditCom);				
	fn_ComRes_vDeleteLines( &m_oRichEditRes);				

	if (!g_iData && !g_bAskFamily) 
		m_oCheckCommon.EnableWindow(TRUE);
	if (g_iData && !g_bAskFamily) 
		m_oCheckSpecific.EnableWindow(TRUE);

	m_fn_vUpdateForSelectConfig();
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on GameSpecific2 radio button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckGameSpecific2()
{
	if (m_oCheckGameSP2.GetCheck())
	{
		if (strcmp(g_stInitialData.szGameSpecific2, ""))
		{	
			m_oCheckGameSP2.SetCheck(1);
			m_oCheckGameData.SetCheck(0);
			m_oCheckGameSP1.SetCheck(0);
			g_iData=2;
		}
		else  
		{
			m_oCheckGameSP1.SetCheck(0);
			m_oCheckGameSP2.SetCheck(0);
			m_oCheckGameData.SetCheck(1);
			g_iData=0;
		}
	}
	m_fn_vResetSelection() ;
    fn_ComRes_vDeleteLines( &m_oRichEditCom);				
	fn_ComRes_vDeleteLines( &m_oRichEditRes);				

	if (!g_iData && !g_bAskFamily) 
		m_oCheckCommon.EnableWindow(TRUE);
	if (g_iData && !g_bAskFamily) 
		m_oCheckSpecific.EnableWindow(TRUE);

	m_fn_vUpdateForSelectConfig();
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on RawData radio button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckRawData()
{
	if (m_oCheckRawData.GetCheck())
	{
		m_oCheckRawData.SetCheck(1);
		m_oCheckRawSP1.SetCheck(0);
		m_oCheckRawSP2.SetCheck(0);
		g_iRaw = 0;
		m_fn_vFreeMemory();

		m_p_oTypeAnims = NULL;
		m_p_oTypeBanks = NULL;
		m_p_oTypeClasses = NULL;
		m_p_oTypeFamilies = NULL;
		m_p_oTypeLevels = NULL;
		m_p_oTypeTextures = NULL;
		
		//reset the contains of the list boxes
		m_oListType->ResetContent();
		m_oListObject->ResetContent();

		m_fn_vUpdateAll();
		//reset the total number of selections
		m_iCountOfSelections = 0;

		if (!g_iData && !g_bAskFamily) 
			m_oCheckCommon.EnableWindow(TRUE);
		if (g_iData && !g_bAskFamily) 
			m_oCheckSpecific.EnableWindow(TRUE);

		m_fn_vUpdateForSelectConfig();
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on RawSpecific1 radio button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckRawSpecific1()
{
	if (m_oCheckRawSP1.GetCheck())
	{
		m_fn_vFreeMemory();

		m_p_oTypeAnims = NULL;
		m_p_oTypeBanks = NULL;
		m_p_oTypeClasses = NULL;
		m_p_oTypeFamilies = NULL;
		m_p_oTypeLevels = NULL;
		m_p_oTypeTextures = NULL;
		
		//reset the contains of the list boxes
		m_oListType->ResetContent();
		m_oListObject->ResetContent();

		if (strcmp(g_stInitialData.szRawSpecific1, ""))
		{	m_oCheckRawSP1.SetCheck(1);
			m_oCheckRawData.SetCheck(0);
			m_oCheckRawSP2.SetCheck(0);
			g_iRaw=1;
		}
		else  
		{	m_oCheckRawSP1.SetCheck(0);
			m_oCheckRawSP2.SetCheck(0);
			m_oCheckRawData.SetCheck(1);
			g_iRaw=0;
		}
		m_fn_vUpdateAll();

		//reset the total number of selections
		m_iCountOfSelections = 0;

		if (!g_iData && !g_bAskFamily) 
			m_oCheckCommon.EnableWindow(TRUE);
		if (g_iData && !g_bAskFamily) 
			m_oCheckSpecific.EnableWindow(TRUE);

		m_fn_vUpdateForSelectConfig();
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on RawSpecific2 radio button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckRawSpecific2()
{
	if (m_oCheckRawSP2.GetCheck())
	{
		m_fn_vFreeMemory();

		m_p_oTypeAnims = NULL;
		m_p_oTypeBanks = NULL;
		m_p_oTypeClasses = NULL;
		m_p_oTypeFamilies = NULL;
		m_p_oTypeLevels = NULL;
		m_p_oTypeTextures = NULL;
		
		//reset the contains of the list boxes
		m_oListType->ResetContent();
		m_oListObject->ResetContent();

		if (strcmp(g_stInitialData.szRawSpecific2, ""))
		{	
			m_oCheckRawSP2.SetCheck(1);
			m_oCheckRawData.SetCheck(0);
			m_oCheckRawSP1.SetCheck(0);
			g_iRaw=2;
		}
		else  
		{
			m_oCheckRawSP2.SetCheck(0);
			m_oCheckRawSP1.SetCheck(0);
			m_oCheckRawData.SetCheck(1);
			g_iRaw=0;
		}
		m_fn_vUpdateAll();
		//reset the total number of selections
		m_iCountOfSelections = 0;

		if (!g_iData && !g_bAskFamily) 
			m_oCheckCommon.EnableWindow(TRUE);
		if (g_iData && !g_bAskFamily) 
			m_oCheckSpecific.EnableWindow(TRUE);

		m_fn_vUpdateForSelectConfig();
	}
}





//==================================================//
//				BROWSE DIRECTORIES					//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on GameData Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseGameData() 
{
	char szPath[ MAX_PATH ];
	int i;

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);

	if (i)
	{	
		// if the new directory is different to the initial one
		if (strcmp(szPath, g_stInitialData.szGameData))
		{	
			char szPathAux1[ MAX_PATH ];
			char szPathAux2[ MAX_PATH ];
			char _szMessage[50];
			
			strcpy(szPathAux1, szPath);
			strcpy(szPathAux2, szPath);
			
			// in the directory GameData there must be sub-directories
			if (!m_fn_bExistDirectories(szPath))
			{	
				sprintf( _szMessage,"Error : You have to select another GameData directory, because it does not contain any directories (It should contain the Texture and Default sub-directories)!");
				AfxMessageBox( _szMessage, MB_OK | MB_ICONSTOP );
				strcpy (szPath, "");
			}
			// see if in the new path exist the last sub-directories 
			if (!(szPathAux1[strlen(szPathAux1)-1]=='\\'))
				strcat(szPathAux1, "\\");
			strcat(szPathAux1, g_stInitialData.szTextures);
			if (!(szPathAux2[strlen(szPathAux2)-1]=='\\'))
				strcat(szPathAux2, "\\");
			strcat(szPathAux2, g_stInitialData.szDefault);
		}
		// set that path for the Game Data directory
		fn_IniData_vSetGameData( szPath );
		strcpy(g_stInitialData.szGameData, szPath);
		GetDlgItem( IDC_EDIT_GAMEDATA )->SetWindowText( szPath );
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on GameSpecific1 Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseGameSpecific1() 
{
	char szPath[ MAX_PATH ];
	int i;

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);

	if (i)
	{	
		// set that path for the Game Specific1 directory
		fn_IniData_vSetGameSpecific1( szPath );
		strcpy(g_stInitialData.szGameSpecific1, szPath);
		GetDlgItem( IDC_EDIT_GAMESPECIFIC1 )->SetWindowText( szPath );
		if (strcmp(g_stInitialData.szGameSpecific1, ""))
			m_oCheckGameSP1.EnableWindow(TRUE);
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on GameSpecific2 Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseGameSpecific2() 
{
	char szPath[ MAX_PATH ];
	int i;

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);

	if (i)
	{	
		// set that path for the Game Specific2 directory
		fn_IniData_vSetGameSpecific2( szPath );
		strcpy(g_stInitialData.szGameSpecific2, szPath);
		GetDlgItem( IDC_EDIT_GAMESPECIFIC2 )->SetWindowText( szPath );
		if (strcmp(g_stInitialData.szGameSpecific2, ""))
			m_oCheckGameSP2.EnableWindow(TRUE);
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on RawData Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseRawData() 
{
	char szPath[ MAX_PATH ];
	int i;
	
	if (m_iCountOfSelections)
	{
		char _szMessage[50];
		sprintf( _szMessage, "In the RawData directory there are some selections made. Do you want to loose this selections by changing the directory?\n");
		if (AfxMessageBox( _szMessage, MB_YESNO | MB_ICONQUESTION) == IDNO)
			return;
	}

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);
	if (i)
	{	
		// set that path for the Raw Data directory
		fn_IniData_vSetRawData( szPath );
		strcpy(g_stInitialData.szRawData, szPath);
		GetDlgItem( IDC_EDIT_RAWDATA )->SetWindowText( szPath );

		//remove all the elements in the lists with objects
		m_fn_vFreeMemory();

		m_p_oTypeAnims = NULL;
		m_p_oTypeBanks = NULL;
		m_p_oTypeClasses = NULL;
		m_p_oTypeFamilies = NULL;
		m_p_oTypeLevels = NULL;
		m_p_oTypeTextures = NULL;
		
		//reset the contains of the list boxes
		m_oListType->ResetContent();
		m_oListObject->ResetContent();

		m_fn_vUpdateAll();
		//reset the total number of selections
		m_iCountOfSelections = 0;

		m_fn_vUpdateForSelectConfig();
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on RawSpecific1 Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseRawSpecific1() 
{
	char szPath[ MAX_PATH ];
	int i;

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);

	if (i)
	{	
		// set that path for the Game Specific1 directory
		fn_IniData_vSetRawSpecific1( szPath );
		strcpy(g_stInitialData.szRawSpecific1, szPath);
		GetDlgItem( IDC_EDIT_RAWSPECIFIC1 )->SetWindowText( szPath );
		if (strcmp(g_stInitialData.szRawSpecific1, ""))
			m_oCheckRawSP1.EnableWindow(TRUE);
		//remove all the elements in the lists with objects
		m_fn_vFreeMemory();

		m_p_oTypeAnims = NULL;
		m_p_oTypeBanks = NULL;
		m_p_oTypeClasses = NULL;
		m_p_oTypeFamilies = NULL;
		m_p_oTypeLevels = NULL;
		m_p_oTypeTextures = NULL;
		
		//reset the contains of the list boxes
		m_oListType->ResetContent();
		m_oListObject->ResetContent();

		m_fn_vUpdateAll();
		//reset the total number of selections
		m_iCountOfSelections = 0;

		m_fn_vUpdateForSelectConfig();
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on RawSpecific2 Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseRawSpecific2() 
{
	char szPath[ MAX_PATH ];
	int i;

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);

	if (i)
	{	
		// set that path for the Game Specific2 directory
		fn_IniData_vSetRawSpecific2( szPath );
		strcpy(g_stInitialData.szRawSpecific2, szPath);
		GetDlgItem( IDC_EDIT_RAWSPECIFIC2 )->SetWindowText( szPath );
		if (strcmp(g_stInitialData.szRawSpecific2, ""))
			m_oCheckRawSP2.EnableWindow(TRUE);
		//remove all the elements in the lists with objects
		m_fn_vFreeMemory();

		m_p_oTypeAnims = NULL;
		m_p_oTypeBanks = NULL;
		m_p_oTypeClasses = NULL;
		m_p_oTypeFamilies = NULL;
		m_p_oTypeLevels = NULL;
		m_p_oTypeTextures = NULL;
		
		//reset the contains of the list boxes
		m_oListType->ResetContent();
		m_oListObject->ResetContent();

		m_fn_vUpdateAll();
		//reset the total number of selections
		m_iCountOfSelections = 0;

		m_fn_vUpdateForSelectConfig();
	}
}



//==================================================//
//				DIRECTORY CONFIGURATION				//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on button Create for DirConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCreateConfigDir() 
{
	if (g_bChangeComboDir && g_iCurrentIndexDir)
	{	
		int iResult = AfxMessageBox( "The current configuration was changed : if you do not save it before creating a new one, the changes will be lost!\n Do you want to save the current configuration ?", MB_YESNOCANCEL | MB_ICONEXCLAMATION );
		switch (iResult)
		{
			case IDYES:
				OnSaveConfigDir();
				break;

			case IDNO:
				g_bChangeComboDir = FALSE;
				break;

			case IDCANCEL:
				m_oComboChooseDir.SetCurSel(g_iCurrentIndexDir);
				return;	
		}
	}
	m_oButtonCreateDir.ShowWindow(FALSE);
	m_oEditCreateDir.ShowWindow(TRUE);
	m_oEditCreateDir.SetFocus();
}

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on button Register for DirConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnRegisterConfigDir() 
{
	m_oButtonRegisterDir.ShowWindow(FALSE);
	m_oEditRegisterDir.ShowWindow(TRUE);
	m_oEditRegisterDir.SetFocus();
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on button Save for DirConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnSaveConfigDir() 
{
	int i = m_oComboChooseDir.GetCurSel();
	xString szDir1;

	if (i!=-1)
	{	//save the directories for the new config
		GetCurrentDirectory( MAX_PATH, szDir1 );
		SetCurrentDirectory( g_szInitialDirectory );
		GetCurrentDirectory( MAX_PATH, g_szIniConfDirFile );
		if (g_szIniConfDirFile[strlen(g_szIniConfDirFile) - 1] != '\\')
			strcat( g_szIniConfDirFile, "\\");
		strcat( g_szIniConfDirFile, C_szIniConfDirFile );
		SetCurrentDirectory( szDir1 );
		
		g_bChangeComboDir = FALSE;

		m_fn_vRegisterAllPaths(i);

		xString szPath;
		strcpy(szPath, "");

		for (int i=1; i<g_iNbConfDirs-1; i++)
		{	
			strcat(szPath, g_szConfDirs[i]);
			strcat(szPath, ",");
		}
		if (g_iNbConfDirs) //!!!
			strcat(szPath, g_szConfDirs[g_iNbConfDirs-1]);
		fn_IniData_vSetConfigDirNames( szPath );
		strcpy(g_stInitialData.szConfDirs, szPath);
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on button Delete for DirConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnDeleteConfigDir() 
{
	xString szPath;
	FILE	*fileR, *fileW;
	xString szFind, szOld, sRead;
	int j;

	strcpy(szPath, "");
	int i = m_oComboChooseDir.GetCurSel();
	if (i != -1)
	{	
		m_oComboChooseDir.GetLBText( i, szOld );
		m_oComboChooseDir.DeleteString(i);
	}

	g_iNbConfDirs--;
	for (j=i; j<g_iNbConfDirs; j++)
		strcpy(g_szConfDirs[j], g_szConfDirs[j+1]);

	strcpy(szPath, "");
	for (j=1; j<g_iNbConfDirs-1; j++)
	{	
		strcat(szPath, g_szConfDirs[j]);
		strcat(szPath, ",");
	}
	if (g_iNbConfDirs) 
		strcat(szPath, g_szConfDirs[g_iNbConfDirs-1]);
	fn_IniData_vSetConfigDirNames( szPath );

	// if necessary, init config file
	if (strlen(g_szIniConfDirFile) == 0)
	{
		GetCurrentDirectory(MAX_PATH, g_szIniConfDirFile);
		if (g_szIniConfDirFile[strlen(g_szIniConfDirFile) - 1] != '\\')
			strcat( g_szIniConfDirFile, "\\");
		strcat( g_szIniConfDirFile, C_szIniConfDirFile );
	}

	//delete the section with the previous name from the Configs.ini file
	fileR = fopen (g_szIniConfDirFile, "r");
	if (fileR)
	{
		fileW = fopen ("New.ini", "w");
			
		strcpy(szFind, "[");
		strcat(szFind, szOld);
		strcat(szFind, "]\n");
		while (fgets(sRead, 256, fileR)!=NULL)
			if (!stricmp(sRead, szFind))
				break;
			else
				fputs( sRead, fileW);
		while ((fgets(sRead, 256, fileR) != NULL) && !strstr(sRead, "["));
		if (!feof(fileR)) fputs( sRead, fileW);
		while ((fgets(sRead, 256, fileR) != NULL))
			fputs( sRead, fileW);
		fclose(fileR);
		fclose(fileW);
		remove(g_szIniConfDirFile);
		MoveFile("New.ini", g_szIniConfDirFile);
	}

	m_oComboChooseDir.SetCurSel(0);
	g_iCurrentIndexDir = 0;

	m_fn_vSetDefaultDirConfig();
	m_oButtonDeleteDir.EnableWindow(g_iCurrentIndexDir);
	m_oButtonSaveDir.EnableWindow(g_iCurrentIndexDir);

	g_bChangeComboDir = FALSE;
}


/****************************************************************************
*		Description:	Handle function for message CBN_SELCHANGE on ComboBox for DirConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnChooseConfigDir() 
{
	char szDir1[_MAX_PATH];
	
	if (g_bChangeComboDir && g_iCurrentIndexDir)
	{	
		int iResult = AfxMessageBox( "The current configuration was changed : if you do not save it before choosing another one, the changes will be lost!\n Do you want to save the current configuration ?", MB_YESNOCANCEL | MB_ICONEXCLAMATION );
		switch (iResult)
		{
			case IDYES:
				OnSaveConfigDir();
				break;

			case IDNO:
				g_bChangeComboDir = FALSE;
				break;

			case IDCANCEL:
				m_oComboChooseDir.SetCurSel(g_iCurrentIndexDir);
				return;	
		}
	}

	g_iCurrentIndexDir = m_oComboChooseDir.GetCurSel();

	if (g_iCurrentIndexDir)
	{
		if (strcmp(g_stInitialData.szConfDirs, ""))
		{	
			GetCurrentDirectory( MAX_PATH, szDir1 );
			SetCurrentDirectory( g_szInitialDirectory );
			//read the Configs.ini file
			fn_IniData_vGetDatasFromConfigDirsFile( TRUE, g_szConfDirs[g_iCurrentIndexDir] );

			m_fn_vUpdateDirectories() ;
			SetCurrentDirectory( szDir1 );
			m_fn_vResetSelection();
		}
	}
	else
		m_fn_vSetDefaultDirConfig();

	m_oButtonDeleteDir.EnableWindow(g_iCurrentIndexDir);
	m_oButtonSaveDir.EnableWindow(g_iCurrentIndexDir);
}


/****************************************************************************
*		Description:	Handle function for message EN_KILLFOCUS on Register EditBox for DirConfig 
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusEditCreateConfigDir() 
{
	char	szName[15];
	int i;
	
	strcpy(szName, "");

	m_oEditCreateDir.GetWindowText(szName, 14);
	m_oEditCreateDir.ShowWindow(FALSE);
	if (m_oConfigPanel.GetCurSel() && !m_oMainPanel.GetCurSel())
		m_oButtonCreateDir.ShowWindow(TRUE);
	i = m_oComboChooseDir.GetCount();
	if (strcmp(szName, ""))  
	{	//search if the name already exist in the combo-box
		//if yes inform the user!!!

		int j = m_oComboChooseDir.FindStringExact( -1, szName);
		if (j != CB_ERR)
		{	
			AfxMessageBox( "The configuration already exists!", MB_OK | MB_ICONSTOP );
			return;	//if the string exists do nothing
		}

		m_oComboChooseDir.AddString(szName);
		m_oComboChooseDir.SetCurSel(i);
		g_iCurrentIndexDir = i;

		OnChooseConfigDir();
		g_bChangeComboDir = TRUE;

		g_iNbConfDirs++;
		strcpy(g_szConfDirs[g_iNbConfDirs-1], szName);
	}
	m_oButtonSaveDir.EnableWindow(g_iCurrentIndexDir);
	m_oButtonDeleteDir.EnableWindow(g_iCurrentIndexDir);
}

/****************************************************************************
*		Description:	Handle function for message EN_KILLFOCUS on Register EditBox for DirConfig 
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusEditRegisterConfigDir() 
{
	char	szName[15];
	int i;
	
	strcpy(szName, "");

	m_oEditRegisterDir.GetWindowText(szName, 14);
	m_oEditRegisterDir.ShowWindow(FALSE);
	if (m_oConfigPanel.GetCurSel() && !m_oMainPanel.GetCurSel())
		m_oButtonRegisterDir.ShowWindow(TRUE);
	i = m_oComboChooseDir.GetCount();
	if (strcmp(szName, ""))  
	{	//search if the name already exist in the combo-box
		//if yes inform the user!!!

		int j = m_oComboChooseDir.FindStringExact( -1, szName);
		if (j != CB_ERR)
		{	
			AfxMessageBox( "The configuration already exists!", MB_OK | MB_ICONSTOP );
			return;	//if the string exists do nothing
		}

		m_oComboChooseDir.AddString(szName);
		m_oComboChooseDir.SetCurSel(i);
		g_iCurrentIndexDir = i;

		g_bChangeComboDir = TRUE;

		g_iNbConfDirs++;
		strcpy(g_szConfDirs[g_iNbConfDirs-1], szName);
	}

	OnSaveConfigDir();
	m_oButtonSaveDir.EnableWindow(g_iCurrentIndexDir);
	m_oButtonDeleteDir.EnableWindow(g_iCurrentIndexDir);
}

/****************************************************************************
*		Description:	Handle function for message CBN_KILLFOCUS on ComboBox for DirConfig 
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusComboChooseConfigDir() 
{	
	char	szName[15];
	FILE	*fileR, *fileW;
	xString szFind, sRead, szOld;
	
	strcpy(szName, "");
	strcpy(szOld, "");
	m_oComboChooseDir.GetLBText( g_iCurrentIndexDir, szOld );
	m_oComboChooseDir.GetWindowText( szName, 25 );
	if (!strcmp(szName, szOld))
		return;
	
	if (strcmp(szName, ""))
	{	
		xString szPath;
		int j1 = m_oComboChooseDir.FindStringExact( -1, szName);
		if (j1 != CB_ERR)
		{	
			AfxMessageBox( "The configuration already exists or you gave the same name for it. Please select another name!", MB_OK | MB_ICONSTOP );
			m_oComboChooseDir.SetCurSel(g_iCurrentIndexDir);
			return;	//if the string exists do nothing
		}

		if (g_iCurrentIndexDir)
		{
			if (g_iCurrentIndexDir != -1)
				m_oComboChooseDir.DeleteString(g_iCurrentIndexDir);

			m_oComboChooseDir.InsertString(g_iCurrentIndexDir, szName);
			m_oComboChooseDir.SetCurSel(g_iCurrentIndexDir);

			strcpy(g_szConfDirs[g_iCurrentIndexDir], szName);

			//delete the section with the previous name from the Configs.ini file
			fileR = fopen (g_szIniConfDirFile, "r");
			fileW = fopen ("New.ini", "w");
			
			strcpy(szFind, "[");
			strcat(szFind, szOld);
			strcat(szFind, "]\n");
			while (fgets(sRead, 256, fileR)!=NULL) 
			{
				if (!stricmp(sRead, szFind))
					break;
				else
					fputs( sRead, fileW);
			}

			fputs("[", fileW);
			fputs(szName, fileW);
			fputs("]\n", fileW);


			while (!feof(fileR))
			{	fgets(sRead, 256, fileR);
				if (!feof(fileR)) fputs( sRead, fileW);

			}

			fclose(fileR);
			fclose(fileW);
			remove(g_szIniConfDirFile);
			MoveFile("New.ini", g_szIniConfDirFile);
		}
		else
		{
			m_oComboChooseDir.AddString(szName);
			m_oComboChooseDir.SetCurSel(m_oComboChooseDir.GetCount() - 1);
			g_iCurrentIndexDir = m_oComboChooseDir.GetCount() - 1;
			g_iNbConfDirs++;
			strcpy(g_szConfDirs[g_iNbConfDirs-1], szName);
			m_oButtonSaveDir.EnableWindow(g_iCurrentIndexDir);
			m_oButtonDeleteDir.EnableWindow(g_iCurrentIndexDir);
		}
		strcpy(szPath, "");

		for (int j=1; j<g_iNbConfDirs-1; j++)
		{	
			strcat(szPath, g_szConfDirs[j]);
			strcat(szPath, ",");
		}
		if (g_iNbConfDirs) //!!!
			strcat(szPath, g_szConfDirs[g_iNbConfDirs-1]);
		fn_IniData_vSetConfigDirNames( szPath );

		//add the new name section in the .ini file
		m_fn_vRegisterAllPaths(g_iCurrentIndexDir);
	}
}




//==================================================//
//				BROWSE CUSTOM DIRECTORIES			//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Anims Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseAnims() 
{
	char szPath[ MAX_PATH ];
	int i;

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);

	if (i)
	{	
		GetDlgItem( IDC_EDIT_ANIMS )->SetWindowText( szPath );
		strcpy(g_szAnims, szPath);
		m_fn_vResetSelection();
		m_fn_vUpdateForSelectConfig();
		g_bChangeComboDir = (g_iCurrentIndexDir != 0);
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Banks Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseBanks() 
{
	char szPath[ MAX_PATH ];
	int i;

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);

	if (i)
	{	
		GetDlgItem( IDC_EDIT_BANKS )->SetWindowText( szPath );
		strcpy(g_szBanks, szPath);
		m_fn_vResetSelection();
		m_fn_vUpdateForSelectConfig();
		g_bChangeComboDir = (g_iCurrentIndexDir != 0);
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Classes Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseClasses() 
{
	char szPath[ MAX_PATH ];
	int i;

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);

	if (i)
	{	
		GetDlgItem( IDC_EDIT_CLASSES )->SetWindowText( szPath );
		strcpy(g_szClasses, szPath);
		m_fn_vResetSelection();
		m_fn_vUpdateForSelectConfig();
		g_bChangeComboDir = (g_iCurrentIndexDir != 0);
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Families Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseFamilies() 
{
	char szPath[ MAX_PATH ];
	int i;

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);

	if (i)
	{	
		GetDlgItem( IDC_EDIT_FAMILIES )->SetWindowText( szPath );
		strcpy(g_szFamilies, szPath);
		m_fn_vResetSelection();
		m_fn_vUpdateForSelectConfig();
		g_bChangeComboDir = (g_iCurrentIndexDir != 0);
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Levels Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseLevels() 
{
	char szPath[ MAX_PATH ];
	int i;

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);

	if (i)
	{	
		GetDlgItem( IDC_EDIT_LEVELS )->SetWindowText( szPath );
		strcpy(g_szLevels, szPath);
		m_fn_vResetSelection();
		m_fn_vUpdateForSelectConfig();
		g_bChangeComboDir = (g_iCurrentIndexDir != 0);
	}
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Textures Browse button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnButtonBrowseTextures() 
{
	char szPath[ MAX_PATH ];
	int i;

	// prepare the structure for the browsing
	m_fn_vBrowseDirectory(szPath, &i, NULL);

	if (i)
	{	
		GetDlgItem( IDC_EDIT_TEXTURES )->SetWindowText( szPath );
		strcpy(g_szTextures, szPath);
		m_fn_vResetSelection();
		m_fn_vUpdateForSelectConfig();
		g_bChangeComboDir = (g_iCurrentIndexDir != 0);
	}
}





//==================================================//
//				MAIN OPTIONS						//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Rayman2 option
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnRayman2()
{
	g_bRaymanII = m_oCheckRayman2.GetCheck();
	m_oCheckSctMax.EnableWindow(g_bRaymanII);
	m_oCheckSctEds.EnableWindow(g_bRaymanII);
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on ModifLst option
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnModifLst()
{
	g_bModifLst = m_oCheckModifLst.GetCheck();
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on EraseMdf option
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnEraseMdf()
{
	g_bEraseMdf = m_oCheckEraseMdf.GetCheck();
}



//==================================================//
//				LIST BOX FUNCTIONS					//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message LBN_SELCHANGE in Type listbox
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnSelchangeListType() 
{
	GI_Type *p_oType;
	char	szPath[ MAX_PATH ];
	char	szName[20];
	int		iIndex;

	//reset the contents of the list box with objects
	m_oListObject->ResetContent();
	iIndex = m_oListType->GetCurSel();
	if (iIndex != LB_ERR)
	{
		m_oListType->GetText(iIndex, szPath);
		p_oType = (GI_Type *)m_oListType->GetItemData(iIndex);
	}
	else
		p_oType = NULL;
	
	// update the list of objects corresponding to the type selected
	if (m_p_oTypeAnims) 
		m_fn_vUpdateListBox(&m_p_oTypeAnims->m_oList, szPath, "Anims");
	if (m_p_oTypeBanks) 
		m_fn_vUpdateListBox(&m_p_oTypeBanks->m_oList, szPath, "Banks");
	if (m_p_oTypeClasses) 
		m_fn_vUpdateListBox(&m_p_oTypeClasses->m_oList, szPath, "Classes");
	if (m_p_oTypeFamilies) 
		m_fn_vUpdateListBox(&m_p_oTypeFamilies->m_oList, szPath, "Families");
	if (m_p_oTypeLevels) 
		m_fn_vUpdateListBox(&m_p_oTypeLevels->m_oList, szPath, "Levels");
	if (m_p_oTypeTextures) 
		m_fn_vUpdateListBox(&m_p_oTypeTextures->m_oList, szPath, "Textures");
	
	// enable/disable the selection buttons corresponding with the current selections
	if(!m_bConversion)
	{	
		if (p_oType && p_oType->m_iSelected==m_oListObject->GetCount())
			m_oSelectList.EnableWindow(FALSE);
		else
			m_oSelectList.EnableWindow(TRUE);
		if (p_oType && p_oType->m_iSelected>0)
			m_oClearList.EnableWindow(TRUE);
		else
			m_oClearList.EnableWindow(FALSE);
	}

	iIndex = m_oListType->GetCurSel();
	if (iIndex!=LB_ERR)
	{
		p_oType = (GI_Type *)m_oListType->GetItemData( iIndex );
		strcpy(szName, p_oType -> m_fn_p_oGetName());
	}
}



int gs_iRefIndex = -1;

/****************************************************************************
*		Description:	Handle function for message WM_LBUTTONDOWN in Object listbox
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
BOOL CDlgMain::OnLButtonDownListBoxObject(MSG *pMsg)
{
    CListBox    *pLB = m_oListObject;
    POINT       xPos;
    CRect       oRect;
    BOOL        bOutside;
    int         iIndex;
    BOOL        bResult = TRUE;

	if( !m_bConversion)
	{
		// get the x and y coordinates
		xPos.x = LOWORD(pMsg->lParam);
		xPos.y = HIWORD(pMsg->lParam);

		iIndex = pLB -> ItemFromPoint ( xPos, bOutside ) ;

		if (!(pMsg->wParam & MK_SHIFT))
			gs_iRefIndex = iIndex;

		pLB -> GetItemRect ( iIndex , &oRect ) ;
		// determine the object under the cursor in front of which the user clicked
		GI_Item *p_oItem = (GI_Item*) pLB->GetItemData( iIndex );
		ASSERT( p_oItem ) ;
		
		// and if the user click in front change the activation (validate/invalidate the activation).
		if ((pMsg->wParam & MK_SHIFT) && (gs_iRefIndex != iIndex) && (gs_iRefIndex != -1))
		{
			GI_Item *p_oRefItem = (GI_Item*) pLB->GetItemData(gs_iRefIndex);
			int		iMidIndex;

			if (gs_iRefIndex < iIndex)
			{
				for (iMidIndex = gs_iRefIndex + 1; iMidIndex <= iIndex; iMidIndex++)
				{
					GI_Item *p_oMidItem = (GI_Item*) pLB->GetItemData(iMidIndex);
					
					if (p_oMidItem->m_bActived != p_oRefItem->m_bActived)
						m_fn_bOnChangeActivation(p_oMidItem, iMidIndex);
				}
			}
			else 
			{
				for (iMidIndex = iIndex; iMidIndex < gs_iRefIndex; iMidIndex++)
				{
					GI_Item *p_oMidItem = (GI_Item*) pLB->GetItemData(iMidIndex);

					if (p_oMidItem->m_bActived != p_oRefItem->m_bActived)
						m_fn_bOnChangeActivation(p_oMidItem, iMidIndex);
				}
			}
			bResult = TRUE;
		}
		else if (m_fn_bOnChangeActivation(p_oItem, iIndex ))  
			bResult = TRUE;
		else
			bResult = CDialog::PreTranslateMessage(pMsg);
		return bResult;
	}
	else
		return bResult;
}


/****************************************************************************
*		Description:	Handle function for message WM_DRAWITEM in listbox
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDIS)
{
	// Object listbox
	if ((nIDCtl == IDC_LISTBOX_OBJECTS) && (m_oListObject->IsWindowEnabled()) && (lpDIS->itemID != LB_ERR))
        m_fn_vOnDrawItemListBoxObject ( lpDIS ) ;
	// Type listbox
    else if ((nIDCtl == IDC_LISTBOX_TYPES) && (m_oListType->IsWindowEnabled()) && (lpDIS->itemID != LB_ERR))
		m_fn_vOnDrawItemListBoxType ( lpDIS ) ;
	// default handling
	else	
		CDialog::OnDrawItem(nIDCtl, lpDIS);
}


/****************************************************************************
*		Description:	Handle function for message WM_MEASUREITEM in listbox
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	CDialog::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}




//==================================================//
//				SELECTION BUTTONS					//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Select All" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnSelectAll() 
{
	// select all the objects in all the lists associated with the types
	m_fn_vSelectAll(TRUE); 
	m_oListType->Invalidate(TRUE);
	// enable (Clear, Clear All, Convert) / disable (All, AllInList)
	m_fn_vEnableButtons(TRUE);
	g_bChangeComboSelect = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Select List" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnSelectList() 
{
	//change all the objects in active and enable/disable the buttons for selection
	m_fn_vSelectAllForType(TRUE);
	m_fn_vEnableButtonAll();
	m_oSelectList.EnableWindow(FALSE);
	m_oClearList.EnableWindow(TRUE);
	m_oClearAll.EnableWindow(TRUE);
	m_oConvert.EnableWindow(TRUE);
	if (!m_bConversion && m_iCountOfSelections)
		m_oCheckGenerateRLI.EnableWindow(TRUE);
	else
		m_oCheckGenerateRLI.EnableWindow(FALSE);
	if (m_p_oTypeFamilies && m_p_oTypeFamilies->m_iSelected) 
	{	m_oCheckCopyAnims.EnableWindow(TRUE);
		m_oCheckAskFamily.EnableWindow(TRUE);
		if (!g_iData && !g_bAskFamily) m_oCheckCommon.EnableWindow(TRUE);
		if (g_iData && !g_bAskFamily) m_oCheckSpecific.EnableWindow(TRUE);
	}

	if ((m_p_oTypeBanks && m_p_oTypeBanks->m_iSelected) || (m_p_oTypeClasses && m_p_oTypeClasses->m_iSelected) || (m_p_oTypeLevels && m_p_oTypeLevels->m_iSelected)) 
		m_oCheckCopyTextures.EnableWindow(TRUE);
	if (m_p_oTypeLevels && m_p_oTypeLevels->m_iSelected) 
	{	
		m_oCheckSpoMax.EnableWindow(TRUE);
		m_oCheckSpoEds.EnableWindow(TRUE);
		m_oCheckUpdateGameDsc.EnableWindow(TRUE);
		m_oCheckEraseDup.EnableWindow(TRUE);
		m_oCheckEraseUnused.EnableWindow(TRUE);	
		if (g_bRaymanII)
		{
			m_oCheckSctMax.EnableWindow(TRUE);
			m_oCheckSctEds.EnableWindow(TRUE);
		}
	}
	if ((m_p_oTypeLevels && m_p_oTypeLevels->m_iSelected) ||
		(m_p_oTypeBanks && m_p_oTypeBanks->m_iSelected) ||
		(m_p_oTypeClasses && m_p_oTypeClasses->m_iSelected)) 
	{		
		m_oCheckGmtMax.EnableWindow(TRUE);
		m_oCheckGmtEds.EnableWindow(TRUE);
		m_oCheckVmtMax.EnableWindow(TRUE);
		m_oCheckVmtEds.EnableWindow(TRUE);
	}
	if ((m_p_oTypeLevels && m_p_oTypeLevels->m_iSelected) ||
		(m_p_oTypeFamilies && m_p_oTypeFamilies->m_iSelected) ||
		(m_p_oTypeClasses && m_p_oTypeClasses->m_iSelected)) 
	{		
		m_oCheckZooMax.EnableWindow(TRUE);
		m_oCheckZooEds.EnableWindow(TRUE);
	}
	if (m_p_oTypeBanks && m_p_oTypeBanks->m_iSelected)
		m_oCheckCreateFamily.EnableWindow(TRUE);
	
	g_bChangeComboSelect = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Clear List" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnClearList() 
{
	//change all the objects in the current list to inactive and enable/disable the buttons
	m_fn_vSelectAllForType(FALSE);
	m_fn_vEnableButtonConvert();
	m_fn_vEnableButtonAll();
	m_oClearList.EnableWindow(FALSE);
	m_oSelectList.EnableWindow(TRUE);

	g_bChangeComboSelect = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Clear All" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnClearAll() 
{
	// change the state of all the objects to inactive and enable/disable selections buttons
	m_fn_vSelectAll(FALSE); 
	m_oListType->Invalidate(TRUE);
	m_fn_vEnableButtons(FALSE);

	g_bChangeComboSelect = TRUE;
}


//==================================================//
//				FILE LIST BUTTONS					//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Create button for SelectConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCreateConfigSelect() 
{
	if (g_bChangeComboSelect && g_iCurrentIndexSelect)
	{	
		int iResult = AfxMessageBox( "The current configuration was changed : if you do not save it before creating a new one, the changes will be lost!\n Do you want to save the current configuration ?", MB_YESNOCANCEL | MB_ICONEXCLAMATION );
		switch (iResult)
		{
			case IDYES:
				OnSaveConfigSelect();
				break;

			case IDNO:
				g_bChangeComboSelect = FALSE;
				break;

			case IDCANCEL:
				m_oComboChooseSelect.SetCurSel(g_iCurrentIndexSelect);
				return;	
		}
	}
	m_oButtonCreateSelect.ShowWindow(FALSE);
	m_oEditCreateSelect.ShowWindow(TRUE);
	m_oEditCreateSelect.SetFocus();
}

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Register button for SelectConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnRegisterConfigSelect() 
{
	m_oButtonRegisterSelect.ShowWindow(FALSE);
	m_oEditRegisterSelect.ShowWindow(TRUE);
	m_oEditRegisterSelect.SetFocus();
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Save button for SelectConfig 
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnSaveConfigSelect() 
{
	int i = m_oComboChooseSelect.GetCurSel();
	xString szDir1;

	if (i!=-1)
	{	//save the directories for the new config
		GetCurrentDirectory( MAX_PATH, szDir1 );
		SetCurrentDirectory( g_szInitialDirectory );
		GetCurrentDirectory( MAX_PATH, g_szIniConfSelectFile );
		if (g_szIniConfSelectFile[strlen(g_szIniConfSelectFile) - 1] != '\\')
			strcat( g_szIniConfSelectFile, "\\");
		strcat( g_szIniConfSelectFile, C_szIniConfSelectFile );
		g_bChangeComboSelect = FALSE;
		SetCurrentDirectory( szDir1 );
		
		m_fn_vRegisterSelections(i) ;
		xString szPath;
		strcpy(szPath, "");

		for (int i=1; i<g_iNbConfSelects-1; i++)
		{	
			strcat(szPath, g_szConfSelects[i]);
			strcat(szPath, ",");
		}
		if (g_iNbConfSelects) //!!!
			strcat(szPath, g_szConfSelects[g_iNbConfSelects-1]);
		fn_IniData_vSetSelectConfigNames( szPath );
		strcpy(g_stInitialData.szConfSelects, szPath);
	}
}



/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Delete button for SelectConfig 
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnDeleteConfigSelect() 
{
	xString szPath;
	FILE	*fileR, *fileW;
	xString szFind, szOld, sRead;
	int j;

	strcpy(szPath, "");
	int i = m_oComboChooseSelect.GetCurSel();
	if (i != -1)
	{	
		m_oComboChooseSelect.GetLBText( i, szOld );
		m_oComboChooseSelect.DeleteString(i);
	}

	g_iNbConfSelects--;
	for (j=i; j<g_iNbConfSelects; j++)
		strcpy(g_szConfSelects[j], g_szConfSelects[j+1]);

	strcpy(szPath, "");
	for (j=1; j<g_iNbConfSelects-1; j++)
	{	
		strcat(szPath, g_szConfSelects[j]);
		strcat(szPath, ",");
	}
	if (g_iNbConfSelects) 
		strcat(szPath, g_szConfSelects[g_iNbConfSelects-1]);
	fn_IniData_vSetSelectConfigNames( szPath );

	//delete the section with the previous name from the Configs.ini file
	fileR = fopen (g_szIniConfSelectFile, "r");
	if (fileR)
	{
		fileW = fopen ("New.ini", "w");
			
		strcpy(szFind, "[");
		strcat(szFind, szOld);
		strcat(szFind, "]\n");
		while (fgets(sRead, 256, fileR)!=NULL)
			if (!stricmp(sRead, szFind))
				break;
			else
				fputs( sRead, fileW);
		while ((fgets(sRead, 256, fileR) != NULL) && !strstr(sRead, "["));
		if (!feof(fileR)) fputs( sRead, fileW);
		while ((fgets(sRead, 256, fileR) != NULL))
			if (!feof(fileR)) fputs( sRead, fileW);
		fclose(fileR);
		fclose(fileW);
		remove(g_szIniConfSelectFile);
		MoveFile("New.ini", g_szIniConfSelectFile);
	}

	m_oComboChooseSelect.SetCurSel(0);
	g_iCurrentIndexSelect = 0;

	//reset the last selections!!!!
	m_fn_vUpdateForSelectConfig();
	m_oButtonDeleteSelect.EnableWindow(g_iCurrentIndexSelect);
	m_oButtonSaveSelect.EnableWindow(g_iCurrentIndexSelect);

	g_bChangeComboSelect = FALSE;
}


/****************************************************************************
*		Description:	Handle function for message CBN_SELCHANGE on ConboBox for SelectConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnChooseConfigSelect() 
{
	if (g_bChangeComboSelect && g_iCurrentIndexSelect)
	{	
		int iResult = AfxMessageBox( "The current configuration was changed : if you do not save it before choosing another one, the changes will be lost!\n Do you want to save the current configuration ?", MB_YESNOCANCEL | MB_ICONEXCLAMATION );
		switch (iResult)
		{
			case IDYES:
				OnSaveConfigSelect();
				break;

			case IDNO:
				g_bChangeComboSelect = FALSE;
				break;

			case IDCANCEL:
				m_oComboChooseSelect.SetCurSel(g_iCurrentIndexSelect);
				return;	
		}
	}
	//reset the last selections!!!!
	m_fn_vUpdateForSelectConfig();
	m_oButtonDeleteSelect.EnableWindow(g_iCurrentIndexSelect);
	m_oButtonSaveSelect.EnableWindow(g_iCurrentIndexSelect);
}

/****************************************************************************
*		Description:	Handle function for message EN_KILLFOCUS on Create editbox for SelectConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusEditCreateConfigSelect() 
{
	char	szName[15];
	int i;
	
	strcpy(szName, "");

	m_oEditCreateSelect.GetWindowText(szName, 14);
	m_oEditCreateSelect.ShowWindow(FALSE);
	m_oButtonCreateSelect.ShowWindow(TRUE);

	i = m_oComboChooseSelect.GetCount();
	if (strcmp(szName, ""))  
	{	//search if the name already exist in the combo-box
		//if yes inform the user!!!

		int j = m_oComboChooseSelect.FindStringExact( -1, szName);
		if (j != CB_ERR)
		{	
			AfxMessageBox( "The configuration already exists!", MB_OK | MB_ICONSTOP );
			return;	//if the string exists do nothing
		}

		m_oComboChooseSelect.AddString(szName);

		m_oComboChooseSelect.SetCurSel(i);
		g_iCurrentIndexSelect = i;

		OnChooseConfigSelect();
		g_bChangeComboSelect = TRUE;

		g_iNbConfSelects++;
		strcpy(g_szConfSelects[g_iNbConfSelects-1], szName);
	}
	m_oButtonSaveSelect.EnableWindow(g_iCurrentIndexSelect);
	m_oButtonDeleteSelect.EnableWindow(g_iCurrentIndexSelect);
}

/****************************************************************************
*		Description:	Handle function for message EN_KILLFOCUS on Register editbox for SelectConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusEditRegisterConfigSelect() 
{
	char	szName[15];
	int i;
	
	strcpy(szName, "");

	m_oEditRegisterSelect.GetWindowText(szName, 14);
	m_oEditRegisterSelect.ShowWindow(FALSE);
	m_oButtonRegisterSelect.ShowWindow(TRUE);

	i = m_oComboChooseSelect.GetCount();
	if (strcmp(szName, ""))  
	{	//search if the name already exist in the combo-box
		//if yes inform the user!!!

		int j = m_oComboChooseSelect.FindStringExact( -1, szName);
		if (j != CB_ERR)
		{	
			AfxMessageBox( "The configuration already exists!", MB_OK | MB_ICONSTOP );
			return;	//if the string exists do nothing
		}

		m_oComboChooseSelect.AddString(szName);

		m_oComboChooseSelect.SetCurSel(i);
		g_iCurrentIndexSelect = i;

		g_bChangeComboSelect = TRUE;

		g_iNbConfSelects++;
		strcpy(g_szConfSelects[g_iNbConfSelects-1], szName);
	}
	OnSaveConfigSelect();
	m_oButtonSaveSelect.EnableWindow(g_iCurrentIndexSelect);
	m_oButtonDeleteSelect.EnableWindow(g_iCurrentIndexSelect);
}



/****************************************************************************
*		Description:	Handle function for message CBN_KILLFOCUS on combobox for config select
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusComboChooseConfigSelect() 
{	
	char	szName[15];
	FILE	*fileR, *fileW;
	xString szFind, sRead, szOld;
	int		i1;
	
	strcpy(szName, "");
	strcpy(szOld, "");
	m_oComboChooseSelect.GetLBText( g_iCurrentIndexSelect, szOld );
	m_oComboChooseSelect.GetWindowText( szName, 25 );
	if (!strcmp(szName, szOld))
		return;

	if (strcmp(szName, ""))
	{	
		xString szPath;
		int j1 = m_oComboChooseSelect.FindStringExact( -1, szName);
		if (j1 != CB_ERR)
		{	
			AfxMessageBox( "The configuration already exists or you gave the same name for it. Please select another name!", MB_OK | MB_ICONSTOP );
			return;	//if the string exists do nothing
		}
		i1 = g_iCurrentIndexSelect;
		
		
		if (i1)
		{
			if (i1 != -1)
				m_oComboChooseSelect.DeleteString(i1);

			m_oComboChooseSelect.InsertString(i1, szName);
			m_oComboChooseSelect.SetCurSel(i1);

			strcpy(g_szConfSelects[i1], szName);

			//delete the section with the previous name from the Configs.ini file
			fileR = fopen (g_szIniConfSelectFile, "r");
			fileW = fopen ("New.ini", "w");
			
			strcpy(szFind, "[");
			strcat(szFind, szOld);
			strcat(szFind, "]\n");
			while (fgets(sRead, 256, fileR)!=NULL) //
				if (!stricmp(sRead, szFind))
					break;
				else
					fputs( sRead, fileW);



			fputs("[", fileW);
			fputs(szName, fileW);
			fputs("]\n", fileW);

			while (!feof(fileR))
			{	fgets(sRead, 256, fileR);
				if (!feof(fileR)) fputs( sRead, fileW);
			}

			fclose(fileR);
			fclose(fileW);
			remove(g_szIniConfSelectFile);
			MoveFile("New.ini", g_szIniConfSelectFile);
		}
		else
		{
			m_oComboChooseSelect.AddString(szName);
			m_oComboChooseSelect.SetCurSel(m_oComboChooseSelect.GetCount() - 1);
			g_iCurrentIndexSelect = m_oComboChooseSelect.GetCount() - 1;
			g_iNbConfSelects++;
			strcpy(g_szConfSelects[g_iNbConfSelects-1], szName);
			m_oButtonSaveSelect.EnableWindow(g_iCurrentIndexSelect);
			m_oButtonDeleteSelect.EnableWindow(g_iCurrentIndexSelect);
		}

		strcpy(szPath, "");

		for (int j=1; j<g_iNbConfSelects-1; j++)
		{	
			strcat(szPath, g_szConfSelects[j]);
			strcat(szPath, ",");
		}
		if (g_iNbConfSelects) //!!!
			strcat(szPath, g_szConfSelects[g_iNbConfSelects-1]);
		fn_IniData_vSetSelectConfigNames( szPath );


		//add the new name section in the .ini file
		m_fn_vRegisterSelections(i1);
	}
}





//==================================================//
//				CONVERT BUTTON						//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Convert" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnConvert() 
{
  GetDlgItem( IDC_STATIC_PROGRESS)->ShowWindow( TRUE);
  m_oProgress.SetRange( 0, 0 );
  m_oProgress.SetPos( 0 );
  GetDlgItem( IDC_STATIC_PROGRESS)->ShowWindow( TRUE);

  RedrawWindow( NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_UPDATENOW );
  AfxBeginThread( ms_fn_vConvertData, this, THREAD_PRIORITY_LOWEST);
}



//==================================================//
//				IGNORE OPTIONS						//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Ignore Zoo" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckZooMax()
{
	g_bIgnoreZoo = m_oCheckZooMax.GetCheck();

	m_oCheckZooMax.SetCheck(g_bIgnoreZoo);
	m_oCheckZooEds.SetCheck(!g_bIgnoreZoo);

	g_bChangeComboOption = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Ignore Zoo" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckZooEds()
{
	g_bIgnoreZoo = !m_oCheckZooEds.GetCheck();

	m_oCheckZooEds.SetCheck(!g_bIgnoreZoo);
	m_oCheckZooMax.SetCheck(g_bIgnoreZoo);

	g_bChangeComboOption = TRUE;
}



/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Ignore Sct" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckSctMax()
{
	g_bIgnoreSct = m_oCheckSctMax.GetCheck();

	m_oCheckSctMax.SetCheck(g_bIgnoreSct);
	m_oCheckSctEds.SetCheck(!g_bIgnoreSct);

	g_bChangeComboOption = TRUE;
}

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Ignore Sct" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckSctEds()
{
	g_bIgnoreSct = !m_oCheckSctEds.GetCheck();

	m_oCheckSctEds.SetCheck(!g_bIgnoreSct);
	m_oCheckSctMax.SetCheck(g_bIgnoreSct);

	g_bChangeComboOption = TRUE;
}



/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Ignore Spo" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckSpoMax()
{
	g_bIgnoreSpo = m_oCheckSpoMax.GetCheck();

	m_oCheckSpoMax.SetCheck(g_bIgnoreSpo);
	m_oCheckSpoEds.SetCheck(!g_bIgnoreSpo);

	g_bChangeComboOption = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Ignore Spo" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckSpoEds()
{
	g_bIgnoreSpo = !m_oCheckSpoEds.GetCheck();

	m_oCheckSpoEds.SetCheck(!g_bIgnoreSpo);
	m_oCheckSpoMax.SetCheck(g_bIgnoreSpo);

	g_bChangeComboOption = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Ignore Spo" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckGmtMax()
{
	g_bIgnoreGmt = m_oCheckGmtMax.GetCheck();

	m_oCheckGmtMax.SetCheck(g_bIgnoreGmt);
	m_oCheckGmtEds.SetCheck(!g_bIgnoreGmt);

	g_bChangeComboOption = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Ignore Spo" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckGmtEds()
{
	g_bIgnoreGmt = !m_oCheckGmtEds.GetCheck();

	m_oCheckGmtEds.SetCheck(!g_bIgnoreGmt);
	m_oCheckGmtMax.SetCheck(g_bIgnoreGmt);

	g_bChangeComboOption = TRUE;
}



/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Ignore Spo" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckVmtMax()
{
	g_bIgnoreTex = m_oCheckVmtMax.GetCheck();
	
	m_oCheckVmtMax.SetCheck(g_bIgnoreTex);
	m_oCheckVmtEds.SetCheck(!g_bIgnoreTex);

	g_bChangeComboOption = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Ignore Spo" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCheckVmtEds()
{
	g_bIgnoreTex = !m_oCheckVmtEds.GetCheck();

	m_oCheckVmtEds.SetCheck(!g_bIgnoreTex);
	m_oCheckVmtMax.SetCheck(g_bIgnoreTex);

	g_bChangeComboOption = TRUE;
}



//==================================================//
//				CONVERSION OPTIONS					//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Copy Anims" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCopyAnims()
{
	g_bCopyAnims = m_oCheckCopyAnims.GetCheck();

	g_bChangeComboOption = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Copy Textures" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCopyTextures()
{
	g_bCopyTextures = m_oCheckCopyTextures.GetCheck();

	g_bChangeComboOption = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Generate RLI" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnGenerateRLI()
{
	g_bGenerateRLI = m_oCheckGenerateRLI.GetCheck();

	g_bChangeComboOption = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Update Game.DSC" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnUpdateGameDSC()
{
	g_bUpdateGameDSC = m_oCheckUpdateGameDsc.GetCheck();

	g_bChangeComboOption = TRUE;
}


//==================================================//
//				ERASE OPTIONS						//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Erase Duplicated Points" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnEraseDuplicated()
{
	g_bEraseDuplicated = m_oCheckEraseDup.GetCheck();

	g_bChangeComboOption = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Erase Unused Points" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnEraseUnused()
{
	g_bEraseUnused = m_oCheckEraseUnused.GetCheck();

	g_bChangeComboOption = TRUE;
}


//==================================================//
//				FAMILY OPTIONS						//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Force Common" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnForceCommon()
{
	g_bCommonFamily = m_oCheckCommon.GetCheck();

	g_bChangeComboOption = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Force Specific" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnForceSpecific()
{
	g_bSpecificFamily = m_oCheckSpecific.GetCheck();

	g_bChangeComboOption = TRUE;
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Ask for Family" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnAskFamily()
{
	g_bAskFamily = m_oCheckAskFamily.GetCheck();
	m_oCheckCommon.EnableWindow(!g_bAskFamily);
	m_oCheckSpecific.EnableWindow(!g_bAskFamily);

	g_bChangeComboOption = TRUE;
}


//==================================================//
//				BANK OPTIONS						//
//==================================================//

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on "Create Basic Family" button
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCreateBasicFamily()
{
	g_bCreateBasicFamily = m_oCheckCreateFamily.GetCheck();

	g_bChangeComboOption = TRUE;
}




//==================================================//
//				OPTION CONFIGURATIONS				//
//==================================================//



/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Create button for OptionConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnCreateConfigOption() 
{
	if (g_bChangeComboOption && g_iCurrentIndexOption)
	{	
		int iResult = AfxMessageBox( "The current configuration was changed : if you do not save it before creating a new one, the changes will be lost!\n Do you want to save the current configuration ?", MB_YESNOCANCEL | MB_ICONEXCLAMATION );
		switch (iResult)
		{
			case IDYES:
				OnSaveConfigOption();
				break;

			case IDNO:
				g_bChangeComboOption = FALSE;
				break;

			case IDCANCEL:
				m_oComboChooseOption.SetCurSel(g_iCurrentIndexOption);
				return;	
		}
	}
	m_oButtonCreateOption.ShowWindow(FALSE);
	m_oEditCreateOption.ShowWindow(TRUE);
	m_oEditCreateOption.SetFocus();
}

/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Register button for OptionConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnRegisterConfigOption() 
{
	m_oButtonRegisterOption.ShowWindow(FALSE);
	m_oEditRegisterOption.ShowWindow(TRUE);
	m_oEditRegisterOption.SetFocus();
}


/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Save button for OptionConfig 
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnSaveConfigOption() 
{
	int i = m_oComboChooseOption.GetCurSel();
	xString szDir1;

	if (i!=-1)
	{	//save the directories for the new config
		GetCurrentDirectory( MAX_PATH, szDir1 );
		SetCurrentDirectory( g_szInitialDirectory );
		GetCurrentDirectory( MAX_PATH, g_szIniConfOptionFile );
		if (g_szIniConfOptionFile[strlen(g_szIniConfOptionFile) - 1] != '\\')
			strcat( g_szIniConfOptionFile, "\\");
		strcat( g_szIniConfOptionFile, C_szIniConfOptionFile );
		g_bChangeComboOption = FALSE;
		SetCurrentDirectory( szDir1 );
		
		m_fn_vRegisterAllOptions(i) ;
		xString szPath;
		strcpy(szPath, "");

		for (int i=1; i<g_iNbConfOptions-1; i++)
		{	
			strcat(szPath, g_szConfOptions[i]);
			strcat(szPath, ",");
		}
		if (g_iNbConfOptions) //!!!
			strcat(szPath, g_szConfOptions[g_iNbConfOptions-1]);
		fn_IniData_vSetOptionConfigNames( szPath );
		strcpy(g_stInitialData.szConfOptions, szPath);
	}
}



/****************************************************************************
*		Description:	Handle function for message BN_CLICKED on Delete button for OptionConfig 
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnDeleteConfigOption() 
{
	xString szPath;
	FILE	*fileR, *fileW;
	xString szFind, szOld, sRead;
	int j;

	strcpy(szPath, "");
	int i = m_oComboChooseOption.GetCurSel();
	if (i != -1)
	{	
		m_oComboChooseOption.GetLBText( i, szOld );
		m_oComboChooseOption.DeleteString(i);
	}

	g_iNbConfOptions--;
	for (j=i; j<g_iNbConfOptions; j++)
		strcpy(g_szConfOptions[j], g_szConfOptions[j+1]);

	strcpy(szPath, "");
	for (j=1; j<g_iNbConfOptions-1; j++)
	{	
		strcat(szPath, g_szConfOptions[j]);
		strcat(szPath, ",");
	}
	if (g_iNbConfOptions) 
		strcat(szPath, g_szConfOptions[g_iNbConfOptions-1]);
	fn_IniData_vSetOptionConfigNames( szPath );

	//delete the section with the previous name from the Configs.ini file
	fileR = fopen (g_szIniConfOptionFile, "r");
	if (fileR)
	{
		fileW = fopen ("New.ini", "w");
			
		strcpy(szFind, "[");
		strcat(szFind, szOld);
		strcat(szFind, "]\n");
		while (fgets(sRead, 256, fileR)!=NULL)
			if (!stricmp(sRead, szFind))
				break;
			else
				fputs( sRead, fileW);
		while ((fgets(sRead, 256, fileR) != NULL) && !strstr(sRead, "["));
		if (!feof(fileR)) fputs( sRead, fileW);
		while ((fgets(sRead, 256, fileR) != NULL))
			if (!feof(fileR)) fputs( sRead, fileW);
		fclose(fileR);
		fclose(fileW);
		remove(g_szIniConfOptionFile);
		MoveFile("New.ini", g_szIniConfOptionFile);
	}

	m_oComboChooseOption.SetCurSel(0);
	g_iCurrentIndexOption = 0;

	//reset the last selections!!!!
	m_fn_vUpdateForOptionConfig();
	m_oButtonDeleteOption.EnableWindow(g_iCurrentIndexOption);
	m_oButtonSaveOption.EnableWindow(g_iCurrentIndexOption);

	g_bChangeComboOption = FALSE;
}


/****************************************************************************
*		Description:	Handle function for message CBN_SELCHANGE on ConboBox for OptionConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnChooseConfigOption() 
{
	if (g_bChangeComboOption && g_iCurrentIndexOption)
	{	
		int iResult = AfxMessageBox( "The current configuration was changed : if you do not save it before choosing another one, the changes will be lost!\n Do you want to save the current configuration ?", MB_YESNOCANCEL | MB_ICONEXCLAMATION );
		switch (iResult)
		{
			case IDYES:
				OnSaveConfigOption();
				break;

			case IDNO:
				g_bChangeComboOption = FALSE;
				break;

			case IDCANCEL:
				m_oComboChooseOption.SetCurSel(g_iCurrentIndexOption);
				return;	
		}
	}
	//reset the last selections!!!!
	m_fn_vUpdateForOptionConfig();
	m_oButtonDeleteOption.EnableWindow(g_iCurrentIndexOption);
	m_oButtonSaveOption.EnableWindow(g_iCurrentIndexOption);
}

/****************************************************************************
*		Description:	Handle function for message EN_KILLFOCUS on Create editbox for OptionConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusEditCreateConfigOption() 
{
	char	szName[15];
	int i;
	
	strcpy(szName, "");

	m_oEditCreateOption.GetWindowText(szName, 14);
	m_oEditCreateOption.ShowWindow(FALSE);
	m_oButtonCreateOption.ShowWindow(TRUE);

	i = m_oComboChooseOption.GetCount();
	if (strcmp(szName, ""))  
	{	//search if the name already exist in the combo-box
		//if yes inform the user!!!

		int j = m_oComboChooseOption.FindStringExact( -1, szName);
		if (j != CB_ERR)
		{	
			AfxMessageBox( "The configuration already exists!", MB_OK | MB_ICONSTOP );
			return;	//if the string exists do nothing
		}

		m_oComboChooseOption.AddString(szName);

		m_oComboChooseOption.SetCurSel(i);
		g_iCurrentIndexOption = i;

		OnChooseConfigOption();
		g_bChangeComboOption = TRUE;

		g_iNbConfOptions++;
		strcpy(g_szConfOptions[g_iNbConfOptions-1], szName);
	}
	m_oButtonSaveOption.EnableWindow(g_iCurrentIndexOption);
	m_oButtonDeleteOption.EnableWindow(g_iCurrentIndexOption);
}

/****************************************************************************
*		Description:	Handle function for message EN_KILLFOCUS on Register editbox for OptionConfig
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusEditRegisterConfigOption() 
{
	char	szName[15];
	int i;
	
	strcpy(szName, "");

	m_oEditRegisterOption.GetWindowText(szName, 14);
	m_oEditRegisterOption.ShowWindow(FALSE);
	m_oButtonRegisterOption.ShowWindow(TRUE);

	i = m_oComboChooseOption.GetCount();
	if (strcmp(szName, ""))  
	{	//search if the name already exist in the combo-box
		//if yes inform the user!!!

		int j = m_oComboChooseOption.FindStringExact( -1, szName);
		if (j != CB_ERR)
		{	
			AfxMessageBox( "The configuration already exists!", MB_OK | MB_ICONSTOP );
			return;	//if the string exists do nothing
		}

		m_oComboChooseOption.AddString(szName);

		m_oComboChooseOption.SetCurSel(i);
		g_iCurrentIndexOption = i;

		g_bChangeComboOption = TRUE;

		g_iNbConfOptions++;
		strcpy(g_szConfOptions[g_iNbConfOptions-1], szName);
	}
	OnSaveConfigOption();
	m_oButtonSaveOption.EnableWindow(g_iCurrentIndexOption);
	m_oButtonDeleteOption.EnableWindow(g_iCurrentIndexOption);
}



/****************************************************************************
*		Description:	Handle function for message CBN_KILLFOCUS on combobox ofr config option
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void CDlgMain::OnKillFocusComboChooseConfigOption() 
{	
	char	szName[15];
	FILE	*fileR, *fileW;
	xString szFind, sRead, szOld;
	int		i1;
	
	strcpy(szName, "");
	strcpy(szOld, "");
	m_oComboChooseOption.GetLBText( g_iCurrentIndexOption, szOld );
	m_oComboChooseOption.GetWindowText( szName, 25 );
	if (!strcmp(szName, szOld))
		return;

	if (strcmp(szName, ""))
	{	
		xString szPath;
		int j1 = m_oComboChooseOption.FindStringExact( -1, szName);
		if (j1 != CB_ERR)
		{	
			AfxMessageBox( "The configuration already exists or you gave the same name for it. Please select another name!", MB_OK | MB_ICONSTOP );
			return;	//if the string exists do nothing
		}
		i1 = g_iCurrentIndexOption;

		if (i1)
		{
			if (i1 != -1)
				m_oComboChooseOption.DeleteString(i1);

			m_oComboChooseOption.InsertString(i1, szName);
			m_oComboChooseOption.SetCurSel(i1);

			strcpy(g_szConfOptions[i1], szName);
			//delete the section with the previous name from the Configs.ini file
			fileR = fopen (g_szIniConfOptionFile, "r");
			fileW = fopen ("New.ini", "w");
			
			strcpy(szFind, "[");
			strcat(szFind, szOld);
			strcat(szFind, "]\n");
			while (fgets(sRead, 256, fileR)!=NULL) //
				if (!stricmp(sRead, szFind))
					break;
				else
					fputs( sRead, fileW);

			fputs("[", fileW);
			fputs(szName, fileW);
			fputs("]\n", fileW);

			while (!feof(fileR))
			{	fgets(sRead, 256, fileR);
				if (!feof(fileR)) fputs( sRead, fileW);
			}

			fclose(fileR);
			fclose(fileW);
			remove(g_szIniConfOptionFile);
			MoveFile("New.ini", g_szIniConfOptionFile);
		}
		else
		{
			m_oComboChooseOption.AddString(szName);
			m_oComboChooseOption.SetCurSel(m_oComboChooseOption.GetCount() - 1);
			g_iCurrentIndexOption = m_oComboChooseOption.GetCount() - 1;
			g_iNbConfOptions++;
			strcpy(g_szConfOptions[g_iNbConfOptions-1], szName);
			m_oButtonSaveOption.EnableWindow(g_iCurrentIndexOption);
			m_oButtonDeleteOption.EnableWindow(g_iCurrentIndexOption);
		}

		strcpy(szPath, "");

		for (int j=1; j<g_iNbConfOptions-1; j++)
		{	
			strcat(szPath, g_szConfOptions[j]);
			strcat(szPath, ",");
		}
		if (g_iNbConfOptions) //!!!
			strcat(szPath, g_szConfOptions[g_iNbConfOptions-1]);
		fn_IniData_vSetOptionConfigNames( szPath );

		//add the new name section in the .ini file
		m_fn_vRegisterAllOptions(i1);
	}
}




