// ex30aDoc.cpp : implementation of the CEx30aDoc class
//

#include "stdafx.h"
#include "ex30a.h"

#include "ex30aDoc.h"
#include "PictureSet.h"
#include "AddPictureDlg.h"

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

extern CDaoDatabase g_library;

/////////////////////////////////////////////////////////////////////////////
// CEx30aDoc

IMPLEMENT_DYNCREATE(CEx30aDoc, CDocument)

BEGIN_MESSAGE_MAP(CEx30aDoc, CDocument)
	//{{AFX_MSG_MAP(CEx30aDoc)
	ON_COMMAND(ID_ITB_ADD_PICTURE, OnItbAddPicture)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx30aDoc construction/destruction

CEx30aDoc::CEx30aDoc()
{
	// TODO: add one-time construction code here

}

CEx30aDoc::~CEx30aDoc()
{
}

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

	((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);

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

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CEx30aDoc serialization

void CEx30aDoc::Serialize(CArchive& ar)
{
	// CEditView contains an edit control which handles all serialization
	((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CEx30aDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CEx30aDoc commands

BOOL CEx30aDoc::SaveModified() 
{
	return TRUE; // eliminates "save doc" message on exit
}

void CEx30aDoc::OnItbAddPicture() 
{
	CAddPictureDlg dlg;

	if (dlg.DoModal() == IDCANCEL ||
		dlg.m_strPath.IsEmpty())
		return;

	CFile file(dlg.m_strPath,CFile::modeRead);
	CDib dib;

	dib.Read(&file);

	try {
		if (!g_library.IsOpen())
			g_library.Open("./ITBLibrary.mdb");
		
		CPictureSet set(&g_library);
		
		if (dlg.m_nAsSmallPicture == 0)
		{
			UINT nLastPicture = 0;

			set.m_strFilter.Format("[BookID] = %d AND [PageNumber] = %d",
				dlg.m_uBook, dlg.m_nPage);
			
			set.Open(IMG_ALL);

			if (!set.IsEOF()) {
				set.MoveLast();
				nLastPicture = set.GetRecordCount();
			}
			set.AddNew();

			set.m_lBookID = dlg.m_uBook;
			set.m_lPageNumber = dlg.m_nPage;
			set.m_lPictureNumber = nLastPicture + 1;
			set.m_strName = dlg.m_strName;
			set.m_byType = 1;
			set.m_lLargeSize = 0;
			set.m_lLargeWidth = 0;
			set.m_lLargeHeight = 0;

			CSize sz = dib.GetDimensions();
			set.m_lSmallSize = dib.GetSizeImage() + 
				dib.GetSizeHeader() + sizeof(BITMAPFILEHEADER);
			set.m_lSmallWidth = sz.cx;
			set.m_lSmallHeight = sz.cy;
			
			dib.Write(&set.m_smallPicture);

			set.SetFieldDirty(&set.m_smallPicture);
			set.SetFieldNull(&set.m_smallPicture, FALSE);
		}
		else
		{
			set.m_strFilter.Format("[BookID] = %d AND [PageNumber] = %d AND [Name] = \"%s\"",
				dlg.m_uBook, dlg.m_nPage, dlg.m_strName);

			set.Open(IMG_KEY | IMG_LARGEPICTURE | IMG_LARGEINFO);

			if (set.IsEOF()) {
				AfxMessageBox("You should first add a small version");
				return;
			}

			set.Edit();

			CSize sz = dib.GetDimensions();
			set.m_lLargeSize = dib.GetSizeImage() + 
				dib.GetSizeHeader() + sizeof(BITMAPFILEHEADER);
			set.m_lLargeWidth = sz.cx;
			set.m_lLargeHeight = sz.cy;

			dib.Write(&set.m_largePicture);

			set.SetFieldDirty(&set.m_largePicture);
			set.SetFieldNull(&set.m_largePicture, FALSE);
		}

		set.Update();
		set.Close();
	}
	catch (CDaoException* e) {
		AfxMessageBox(e->m_pErrorInfo->m_strDescription);
		e->Delete();
	}
	catch (CMemoryException* e) {
		AfxMessageBox("Out Of Memory");
		e->Delete();
	}
}
