// fontedoc.cpp : implementation of the CFonteditDoc class
//

#include "stdafx.h"
#include "globals.h"
#include "font.h"
#include "fontedit.h"

#include "mainfrm.h"
#include "fontedoc.h"
#include "fontevw.h"
#include "exporttextures.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

Font *theFont = NULL;
char szExportName[260];

/////////////////////////////////////////////////////////////////////////////
// CFonteditDoc

IMPLEMENT_DYNCREATE(CFonteditDoc, CDocument)

BEGIN_MESSAGE_MAP(CFonteditDoc, CDocument)
	//{{AFX_MSG_MAP(CFonteditDoc)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_IMAGE_SHORTEN, OnImageShorten)
	ON_COMMAND(ID_FILE_NEWNONANTIALIASED, OnFileNewnonantialiased)
	ON_COMMAND(ID_OPTIONS_MINIMIZETEXTURECOUNT, OnOptionsMinimizetexturecount)
	ON_COMMAND(ID_OPTIONS_MINIMIZETEXTURESPACE, OnOptionsMinimizetexturespace)
	ON_UPDATE_COMMAND_UI(ID_OPTIONS_MINIMIZETEXTURECOUNT, OnUpdateOptionsMinimizetexturecount)
	ON_COMMAND(ID_FILE_EXPORTTEXTURES, OnFileExporttextures)
	ON_COMMAND(ID_FILE_IMPORTTEXTURES, OnFileImporttextures)
	ON_UPDATE_COMMAND_UI(ID_FILE_EXPORTTEXTURES, OnUpdateFileExporttextures)
	ON_UPDATE_COMMAND_UI(ID_FILE_IMPORTTEXTURES, OnUpdateFileImporttextures)
	ON_COMMAND(ID_IMAGE_SHORTENFROMBOTTOM, OnImageShortenfrombottom)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFonteditDoc construction/destruction

CFonteditDoc::CFonteditDoc()
{
	bMinimizeTextureCount = true;
	SetModifiedFlag(FALSE);
}

CFonteditDoc::~CFonteditDoc()
{
}

BOOL CFonteditDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CFonteditDoc serialization

void CFonteditDoc::Serialize(CArchive& ar)
{
	if (!theFont)
		return;
	if (ar.IsStoring())
	{
		theFont->Save(ar.GetFile()->m_hFile);
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CFonteditDoc diagnostics

#ifdef _DEBUG
void CFonteditDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CFonteditDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFonteditDoc commands

BOOL CFonteditDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	Font *temp;

	bool bSuccess=false;
	temp = new Font(lpszPathName,bSuccess);
	if (!bSuccess)
	{
		delete temp;
		return FALSE;
	}
	if (theFont)
		Cleanup();
	
	theFont = temp;
	
	view->NewFont();
	return TRUE;
}

void CFonteditDoc::Cleanup(void)
{

	delete theFont;
}

void CFonteditDoc::OnFileNewnonantialiased() 
{
	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));
	if (theFont)
	{
		lf.lfHeight = theFont->PointSize();
		lf.lfWeight = theFont->IsBold()?700:400;
		lf.lfItalic = theFont->IsItalic();
		strcpy(lf.lfFaceName, theFont->FaceName());
	}
	CFontDialog fdlg(&lf);
	Font *temp;

	if (IsModified())
	{
		int result = view->MessageBox("This font has been modified, do you want to save it",NULL,MB_YESNOCANCEL);
		if(result == IDCANCEL)
			return;
		if (result == IDYES)
			main->SendMessage(WM_COMMAND,ID_FILE_SAVE,0);
	}
	fdlg.m_cf.Flags |= CF_TTONLY;
	if (fdlg.DoModal() != IDOK)
		return;

	temp=new Font(fdlg.m_cf.lpLogFont->lfFaceName,
								fdlg.m_cf.lpLogFont->lfHeight,
								fdlg.m_cf.lpLogFont->lfItalic!=0,
								fdlg.m_cf.lpLogFont->lfWeight,false,bMinimizeTextureCount);
	if (!temp)
		return;
	Cleanup();
	theFont = temp;

	m_strPathName.Empty();
	view->NewFont();
	view->InvalidateRect(NULL,TRUE);
	SetModifiedFlag(FALSE);
}

void CFonteditDoc::OnFileNew() 
{
	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));
	if (theFont)
	{
		lf.lfHeight = theFont->PointSize();
		lf.lfWeight = theFont->IsBold()?700:400;
		lf.lfItalic = theFont->IsItalic();
		strcpy(lf.lfFaceName, theFont->FaceName());
	}
	CFontDialog fdlg(&lf);
	Font *temp;

	if (IsModified())
	{
		int result = view->MessageBox("This font has been modified, do you want to save it",NULL,MB_YESNOCANCEL);
		if(result == IDCANCEL)
			return;
		if (result == IDYES)
			main->SendMessage(WM_COMMAND,ID_FILE_SAVE,0);
	}
	fdlg.m_cf.Flags |= CF_TTONLY;
	if (fdlg.DoModal() != IDOK)
		return;

	temp=new Font(fdlg.m_cf.lpLogFont->lfFaceName,
								fdlg.m_cf.lpLogFont->lfHeight,
								fdlg.m_cf.lpLogFont->lfItalic!=0,
								fdlg.m_cf.lpLogFont->lfWeight,true,bMinimizeTextureCount);
	if (!temp)
		return;
	Cleanup();
	theFont = temp;

	m_strPathName.Empty();
	view->NewFont();
	view->InvalidateRect(NULL,TRUE);
	SetModifiedFlag(FALSE);
}

BOOL CFonteditDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
	if (!theFont)
		return false;
	view->UpdateFont();	
	return CDocument::OnSaveDocument(lpszPathName);
}


void CFonteditDoc::OnImageShorten() 
{
	if (!theFont)
		return;
	theFont->Shorten();
	view->InvalidateRect(NULL,TRUE);
}


void CFonteditDoc::OnImageShortenfrombottom() 
{
	if (!theFont)
		return;
	theFont->ShortenFromBottom();
	view->InvalidateRect(NULL,TRUE);
}



void CFonteditDoc::OnOptionsMinimizetexturecount() 
{
	bMinimizeTextureCount = true;

	HMENU hMainMenu = ::GetMenu(AfxGetMainWnd()->m_hWnd);
	HMENU hOptionsMenu = ::GetSubMenu(hMainMenu,3);


	CheckMenuItem(hOptionsMenu,ID_OPTIONS_MINIMIZETEXTURECOUNT,MF_BYCOMMAND|MF_CHECKED);
	CheckMenuItem(hOptionsMenu,ID_OPTIONS_MINIMIZETEXTURESPACE,MF_BYCOMMAND|MF_UNCHECKED);
}

void CFonteditDoc::OnOptionsMinimizetexturespace() 
{
	bMinimizeTextureCount = false;

	HMENU hMainMenu = ::GetMenu(AfxGetMainWnd()->m_hWnd);
	HMENU hOptionsMenu = ::GetSubMenu(hMainMenu,3);


	CheckMenuItem(hOptionsMenu,ID_OPTIONS_MINIMIZETEXTURECOUNT,MF_BYCOMMAND|MF_UNCHECKED);
	CheckMenuItem(hOptionsMenu,ID_OPTIONS_MINIMIZETEXTURESPACE,MF_BYCOMMAND|MF_CHECKED);
}

void CFonteditDoc::OnUpdateOptionsMinimizetexturecount(CCmdUI* pCmdUI) 
{
	if (bMinimizeTextureCount)
		OnOptionsMinimizetexturecount();
	else
		OnOptionsMinimizetexturespace();
}

void CFonteditDoc::OnFileExporttextures() 
{
	CExportTextures dlg;
	if (dlg.DoModal()!= IDOK)
		return;

	if (!SetCurrentDirectory(dlg.m_OutputDir))
	{
		int iResult = MessageBox(NULL,"That directory does not exist, do you want to create it?","Question",MB_YESNO);
		if (iResult == IDNO)
			return;
		CreateDirectory(dlg.m_OutputDir,NULL);
	}
	theFont->ExportTGAS(dlg.m_FileTag,dlg.m_OutputDir);

}

void CFonteditDoc::OnFileImporttextures() 
{
	CFileDialog dlg(true /*open/save*/,"*.tga",NULL,OFN_FILEMUSTEXIST,"Targas (*.tga)|*.tga||",NULL);
	dlg.DoModal();

	const char *szPath = dlg.m_ofn.lpstrFile;
	if (!strstr(szPath,"00.tga") && !strstr(szPath,"00.TGA"))
	{
		MessageBox(NULL,"You must select the first file that was written out by the export command, it will be called something like 'fontname00.tga'","Bogus File Name",MB_OK);
		return;
	}
	theFont->ImportTGAS(szPath);
	view->InvalidateRect(NULL,TRUE);
}

void CFonteditDoc::OnUpdateFileExporttextures(CCmdUI* pCmdUI) 
{
	if (!theFont)	
		pCmdUI->Enable(false);
	else
		pCmdUI->Enable(true);
		
}

void CFonteditDoc::OnUpdateFileImporttextures(CCmdUI* pCmdUI) 
{
	if (!theFont)	
		pCmdUI->Enable(false);
	else
		pCmdUI->Enable(true);
		
}

