// Annotation.cpp: implementation of the CAnnotation class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DiagramEng.h"
#include "Annotation.h"
#include "AnnotationDialog.h"	// rqd. since FormatAnnotation() uses a CAnnotationDialog obj.
#include "Block.h"				// rqd. for decl. of L_FILE_IO_STR

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAnnotation::CAnnotation()
{
	// Member init.
	m_iSize = 10;								// default size
	m_ptPosition = CPoint(0,0);					// origin location
	m_strAnnotation = "Enter annotation here";	// initial string
	m_strColor = "red";							// color
	m_strFont = "Arial";						// font
	m_strStyle = "Regular";						// style
}

CAnnotation::~CAnnotation()
{
	// Do nothing for now.
}

int CAnnotation::GetSize() const
{
	return m_iSize;
}

void CAnnotation::SetSize(int size)
{
	m_iSize = size;
}

CPoint CAnnotation::GetPosition() const
{
	return m_ptPosition;
}

void CAnnotation::SetPosition(CPoint point)
{
	m_ptPosition = point;
}

CString CAnnotation::GetStringAnnotation() const
{
	return m_strAnnotation;
}

void CAnnotation::SetStringAnnotation(CString an)
{
	m_strAnnotation = an;
}

CString CAnnotation::GetColor() const
{	
	return m_strColor;
}

void CAnnotation::SetColor(CString color)
{
	m_strColor = color;
}

CString CAnnotation::GetStringFont() const
{
	return m_strFont;
}

void CAnnotation::SetStringFont(CString font)
{
	m_strFont = font;
}

CString CAnnotation::GetStyle() const
{
	return m_strStyle;
}

void CAnnotation::SetStyle(CString style)
{
	m_strStyle = style;
}

bool CAnnotation::FormatAnnotation(CPoint point)
{
	char strSize[20];	// local string with enough space to hold integer printed as a string

	// Create a dlg obj. of class CAnnotationDialog : public CDialog
	CAnnotationDialog oDlg;		

	// Set the dialog class vars using the CAnnotation class vars
	sprintf(strSize, "%d", m_iSize);			// convert integer "m_iSize" to string
	oDlg.m_strSize = strSize;					// size
	oDlg.m_strAnnotation = m_strAnnotation;		// annotation
	oDlg.m_strColor = m_strColor;				// color
	oDlg.m_strFont = m_strFont;					// font
	oDlg.m_strStyle = m_strStyle;				// style

	// Return val of DoModal() fn of ancestor class CDialog is checked to determine which btn was clicked.
	if(oDlg.DoModal() == IDOK) 
	{	
		// Assign CAnnotationDialog variable values to CAnnotation variable values.
		m_iSize = atoi(oDlg.m_strSize);				// convert the string to an int
		m_ptPosition = point;						// position
		m_strAnnotation = oDlg.m_strAnnotation;		// annotation
		m_strColor = oDlg.m_strColor;				// color
		m_strFont = oDlg.m_strFont;					// font
		m_strStyle = oDlg.m_strStyle;				// style
		CreateAnnotationFont(m_fAnnotationFont);	// create the font	
		return TRUE;								// "OK" has been clicked
	}
	else
	{
		return FALSE;							// "Cancel" has been clicked
	}
}

void CAnnotation::CreateAnnotationFont(CFont &fAnnotationFont) const
{
	int iHeight = 0;			// height of display area
	int iItalic = 0;			// italic font: (0) regular, (1) italic
	int iWeight = FW_REGULAR;	// weight corres. to style

	// Check if a font has been selected
	if(m_strFont != "")
	{
		// Size
		iHeight = m_iSize;

		// Style
		if(m_strStyle == "Regular")
		{
			iItalic = 0;
			iWeight = FW_REGULAR;
		}
		else if(m_strStyle == "Italic")
		{
			iItalic = 1;
			iWeight = FW_REGULAR;
		}
		else if(m_strStyle == "Bold")
		{
			iItalic = 0;
			iWeight = FW_BOLD;
		}
		else if(m_strStyle == "Bold Italic")
		{
			iItalic = 1;
			iWeight = FW_BOLD;
		}

		// Release the current font
		fAnnotationFont.Detach();

		// Create the font to be used
		fAnnotationFont.CreateFont(iHeight, 0, 0, 0, iWeight, iItalic, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
							       CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE , m_strFont);

	}
}

CFont &CAnnotation::GetAnnotationFont()
{
	return m_fAnnotationFont;
}

void CAnnotation::DrawAnnotation(CDC *pDC)
{
	// Select the new font
	CFont *pOldFont = pDC->SelectObject(&m_fAnnotationFont);

	// Set the color
	if(m_strColor == "Red")
	{
		pDC->SetTextColor(RGB(255,0,0));
	}
	else if(m_strColor == "Green")
	{
		pDC->SetTextColor(RGB(0,255,0));
	}
	else if(m_strColor == "Blue")
	{
		pDC->SetTextColor(RGB(0,0,255));
	}
	else
	{
		pDC->SetTextColor(RGB(0,0,0));	// m_strColor == "Black" or otherwise.
	}

	// Display the annotation in the current font
	pDC->TextOut(m_ptPosition.x, m_ptPosition.y, m_strAnnotation);

	// Record the size of the text
	m_sOutputTextSize = pDC->GetOutputTextExtent(m_strAnnotation);

	// Reset the old font
	pDC->SelectObject(pOldFont);
}

CSize CAnnotation::GetOutputTextSize() const
{
	return m_sOutputTextSize;
}

CAnnotation::CAnnotation(const CAnnotation &an)
{
	// Copy incoming argument values to object under construction

	m_sOutputTextSize = an.GetOutputTextSize();	
	an.CreateAnnotationFont(m_fAnnotationFont);	// pass underlying member var by ref to create new font
	m_iSize = an.GetSize();
	m_ptPosition = an.GetPosition();
	m_strAnnotation = an.GetStringAnnotation();
	m_strColor = an.GetColor();
	m_strFont = an.GetStringFont();
	m_strStyle = an.GetStyle();
}

const CFont &CAnnotation::GetAnnotationFontCopy() const
{
	return m_fAnnotationFont;
}

void CAnnotation::WriteAnnotationDataToFile(ofstream &fout)
{
	char strVar[L_FILE_IO_STR];	// char array used with fout
				
	// Write CAnnotation member data to file.
	strcpy(strVar, "annotation");
	fout << strVar << endl;

	fout << m_ptPosition.x << " " << m_ptPosition.y << endl;
	fout << m_sOutputTextSize.cx << " " << m_sOutputTextSize.cy << endl;
	fout << m_iSize << endl;

	strcpy(strVar, m_strAnnotation);
	fout << strVar << endl;

	strcpy(strVar, m_strColor);
	fout << strVar << endl;
	
	strcpy(strVar, m_strFont);
	fout << strVar << endl;

	strcpy(strVar, m_strStyle);
	fout << strVar << endl;
}

void CAnnotation::ReadAnnotationDataFromFile(ifstream &fin)
{
	char strVar[L_FILE_IO_STR];		// char array used with fin
	
	// Read CAnnotation member data from file
	fin >> strVar;					// connection string ID
	fin >> m_ptPosition.x;		
	fin >> m_ptPosition.y;
	fin >> m_sOutputTextSize.cx;
	fin >> m_sOutputTextSize.cy;
	fin >> m_iSize;

	// Annotation string
	while(fin.getline(strVar, L_FILE_IO_STR, '\n'))
	{
		// If string not empty then assume string on line is correct
		if(strcmp(strVar, "") != 0)		
		{
			m_strAnnotation = strVar;
			break;
		}
	}

	// Annotation color
	while(fin.getline(strVar, L_FILE_IO_STR, '\n'))
	{
		// If string not empty then assume string on line is correct
		if(strcmp(strVar, "") != 0)		
		{
			m_strColor = strVar;
			break;
		}
	}

	// Font string
	while(fin.getline(strVar, L_FILE_IO_STR, '\n'))
	{
		// If string not empty then assume string on line is correct
		if(strcmp(strVar, "") != 0)		
		{
			m_strFont = strVar;
			break;
		}
	}

	// Style string
	while(fin.getline(strVar, L_FILE_IO_STR, '\n'))
	{
		// If string not empty then assume string on line is correct
		if(strcmp(strVar, "") != 0)		
		{
			m_strStyle = strVar;
			break;
		}
	}

	// Create the annotation font m_fAnnotationFont
	CreateAnnotationFont(m_fAnnotationFont);
}
