// AnnotationDialog.cpp : implementation file
//

#include "stdafx.h"
#include "DiagramEng.h"
#include "AnnotationDialog.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAnnotationDialog dialog


CAnnotationDialog::CAnnotationDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CAnnotationDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAnnotationDialog)
	m_strAnnotation = _T("");
	m_strFont = _T("");
	m_strStyle = _T("");
	m_strSize = _T("");
	m_strColor = _T("");
	//}}AFX_DATA_INIT
}


void CAnnotationDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAnnotationDialog)
	DDX_Control(pDX, ID_ANNOTATION_DLG_CB_FONT, m_ctlFontList);
	DDX_Text(pDX, ID_ANNOTATION_DLG_EB_AN, m_strAnnotation);
	DDX_CBString(pDX, ID_ANNOTATION_DLG_CB_FONT, m_strFont);
	DDX_CBString(pDX, ID_ANNOTATION_DLG_CB_STYLE, m_strStyle);
	DDX_CBString(pDX, ID_ANNOTATION_DLG_CB_SIZE, m_strSize);
	DDX_CBString(pDX, ID_ANNOTATION_DLG_CB_COLOR, m_strColor);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAnnotationDialog, CDialog)
	//{{AFX_MSG_MAP(CAnnotationDialog)
	ON_BN_CLICKED(ID_ANNOTATION_DLG_BTN_OK, OnAnnotationDlgBtnOk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAnnotationDialog message handlers

// DiagramEng (start)
// Callback fn to get the list of fonts
int CALLBACK EnumFontFamProc(LPENUMLOGFONT lpelf, LPNEWTEXTMETRIC lpntm, DWORD nFontType, long lParam)
{
	// Create a pointer to the dlg wnd.
	CAnnotationDialog *pWnd = (CAnnotationDialog*)lParam;

	// Add the font name to the list box
	pWnd->m_ctlFontList.AddString(lpelf->elfLogFont.lfFaceName);

	return 1;
}
// DiagramEng (end)

void CAnnotationDialog::GenerateFonts()
{
	int cnt;				
	int nFonts;
	CString sNameCurrent;		
	CString sNamePrevious = "";	
	LOGFONT lf;					

	// Init. struct
	lf.lfCharSet = DEFAULT_CHARSET;
	strcpy(lf.lfFaceName, "");

	// Clear combo box
	m_ctlFontList.ResetContent();

	// Device Context
	CClientDC dc(this);	

	// Enumerate
	::EnumFontFamiliesEx((HDC) dc, &lf, (FONTENUMPROC) EnumFontFamProc, (LPARAM)this, 0);

	// Get Font Count
	nFonts = m_ctlFontList.GetCount();

	// Loop in reverse searching for and deleting duplicate entries
	for(cnt = nFonts; cnt > 0; cnt--)
	{
		// Current Font
		m_ctlFontList.GetLBText((cnt - 1), sNameCurrent);	// numbering from 0 to cnt - 1

		// Check Duplicate
		if(sNameCurrent == sNamePrevious)
		{
			m_ctlFontList.DeleteString((cnt - 1));	// delete font
		}

		// Update Font
		sNamePrevious = sNameCurrent;
	}
}

void CAnnotationDialog::OnAnnotationDlgBtnOk() 
{
	// TODO: Add your control notification handler code here
	// DiagramEng (start)

	// Check the value of all member vars
	UpdateData(TRUE);	// TRUE => dlg wnd control vals updated to vars.

	// Check validity of font size arg.
	int size = atoi(m_strSize);

	if((size >= 1) && (size <= 1638))
	{
		// Print a CString msg.
		CString sMsg;
		CString sMsgTemp;

		sMsgTemp.Format(" CAnnotationDialog::OnAnnotationDlgBtnOk()\t    \n\n");
		sMsg += sMsgTemp;
		sMsgTemp.Format(" m_strAnnotation\t = %s\n m_strFont\t = %s\n m_strStyle\t = %s\n m_strSize\t = %s\n m_strColor\t = %s\n",
						  m_strAnnotation, m_strFont, m_strStyle, m_strSize, m_strColor);
		sMsg += sMsgTemp;
		//AfxMessageBox(sMsg, MB_OK, 0);

		// Chain to base class version of the fn.
		CDialog::OnOK();
	}
	else
	{
		AfxMessageBox("\n The font size should be in the interval [1, 1638].    \n", MB_OK, 0);
		return;
	}

	// DiagramEng (end)
}

BOOL CAnnotationDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here

	// DiagramEng (start)
	// Initialization that must be performed here and that cannot be done in CAnnotation::FormatAnnotation()

	// Generate the fonts for the "m_ctlFontList" dialog control
	GenerateFonts();	
	UpdateData(FALSE);		// FALSE => var values updated to dlg wnd controls

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