#include "StdAfx.h"

#include "EMECDoc.hpp"
#include "EMECDgIf.hpp"
#include "_Minterf.hpp"

	/////////////////////////////////////
	//  	Ini file implementation	   //
	/////////////////////////////////////

//***************************************************************************
BOOL CPA_Meca_MyDocument::m_fn_bReadIniFile()
{
	CFile file;
	if ( !file.Open(m_csIniFileName, CFile::modeRead) )
	{
		CString csMessage = "The Ini file ""EdMeca.ini"" is not present in ";
		csMessage += m_csIniFileName.Left(m_csIniFileName.ReverseFind('\\'));
		csMessage += "\nParameters are default ones, but they will be saved when you exit this application.";
		CPA_Meca_InformationDialog dial(NULL, csMessage);
		dial.DoModal();

		return FALSE;
	}
	else
	{
		file.Close();
		
		BOOL bReturn = TRUE;

		return bReturn;
	}

}

//***************************************************************************
BOOL CPA_Meca_MyDocument::m_fn_bWriteIniFile()
{
	BOOL bReturn = TRUE;

	return bReturn;
}

//Preferences
////////////////

#define C_EdMeca_szIniSection_Dimensions		"Dimensions"
	#define C_EdMeca_szIniEntry_ListHeight			"ListHeight"
	#define C_EdMeca_szIniEntry_ControlHeight		"ControlHeight"
	#define C_EdMeca_szIniEntry_EditorWidth			"EditorWidth"

//#define C_EdMeca_szIniSection_	""

//***************************************************************************
BOOL CPA_Meca_MyDocument::m_pub_fn_bSavePreferences()
{
	BOOL bReturn = TRUE;

	m_pri_fn_vGetEditorCurrentSettings();

	CString csStringToWrite;

	if ( bReturn )
	{
		csStringToWrite.Format("%ld", m_ulListViewHeight);
		bReturn = WritePrivateProfileString(C_EdMeca_szIniSection_Dimensions,	
																				C_EdMeca_szIniEntry_ListHeight,
																				LPCTSTR(csStringToWrite),
																				LPCTSTR(m_csIniFileName));
	}

	if ( bReturn )
	{
		csStringToWrite.Format("%ld", m_ulControlViewHeight);
		bReturn = WritePrivateProfileString(C_EdMeca_szIniSection_Dimensions,	
																				C_EdMeca_szIniEntry_ControlHeight,
																				LPCTSTR(csStringToWrite),
																				LPCTSTR(m_csIniFileName));
	}

	if ( bReturn )
	{
		csStringToWrite.Format("%ld", m_ulEditorWidth);
		bReturn = WritePrivateProfileString(C_EdMeca_szIniSection_Dimensions,	
																				C_EdMeca_szIniEntry_EditorWidth,
																				LPCTSTR(csStringToWrite),
																				LPCTSTR(m_csIniFileName));
	}

	return bReturn;
}

//***************************************************************************
BOOL CPA_Meca_MyDocument::m_pub_fn_bLoadPreferences()
{
	BOOL bReturn = TRUE;

	CFile file;
	if ( !file.Open(m_csIniFileName, CFile::modeRead) )
		bReturn = FALSE;
	else
	{
		file.Close();

		m_ulListViewHeight = GetPrivateProfileInt(C_EdMeca_szIniSection_Dimensions,	
																							C_EdMeca_szIniEntry_ListHeight,
																							m_ulListViewHeight,
																							LPCTSTR(m_csIniFileName));

		m_ulControlViewHeight = GetPrivateProfileInt(	C_EdMeca_szIniSection_Dimensions,	
																									C_EdMeca_szIniEntry_ControlHeight,
																									m_ulControlViewHeight,
																									LPCTSTR(m_csIniFileName));

		m_ulEditorWidth = GetPrivateProfileInt(	C_EdMeca_szIniSection_Dimensions,	
																						C_EdMeca_szIniEntry_EditorWidth,
																						m_ulEditorWidth,
																						LPCTSTR(m_csIniFileName));

		bReturn = TRUE;
	}

	return bReturn;
}

//***************************************************************************
void CPA_Meca_MyDocument::m_pri_fn_vGetEditorCurrentSettings()
{
	//Gets views heights
	m_ulListViewHeight = g_pclInterface->m_pclMecaMainFrame->GetPaneSize(0);

	m_ulControlViewHeight = g_pclInterface->m_pclMecaMainFrame->GetPaneSize(1);

	//Gets Charac frame width
	CRect crFrameRect;
	g_pclInterface->m_pclMecaMainFrame->GetClientRect(crFrameRect);
	m_ulEditorWidth = crFrameRect.Width() - 1;	//"-1" because FRMBase adds 1 to initial Width
}