// sxbmedoc.cpp : implementation of the CSxbmemDoc class
//

#include "stdafx.h"
#include "sxbmem.h"

#include "sxbmedoc.h"
#include "sxbmevw.h"
#include "memview.h"

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

extern CSxbmemApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CSxbmemDoc

IMPLEMENT_DYNCREATE(CSxbmemDoc, CDocument)

BEGIN_MESSAGE_MAP(CSxbmemDoc, CDocument)
	ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)
	ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
	//{{AFX_MSG_MAP(CSxbmemDoc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSxbmemDoc construction/destruction

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

CSxbmemDoc::~CSxbmemDoc()
{
}

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

	m_MemAddr = theApp.GetProfileString("Settings", "Addr", "0");
	const CSymbol *symb = theApp.FindSymbol(m_MemAddr);
	m_MemAddress = symb ? symb->m_Address : strtoul(m_MemAddr, NULL, 16);

	m_Size = theApp.GetProfileInt("Settings", "Size", 1024);
	memset(m_Buffer, 0xCD, m_Size);
	m_Format = fmtDword;
	m_DefName = theApp.GetProfileString("Settings", "Def", "Raw bytes");

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

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CSxbmemDoc serialization

void CSxbmemDoc::Serialize(CArchive& ar)
{
	if (ar.IsLoading()) {
		ar >> m_Size;
		ar.Read(m_Buffer, m_Size);
		ar >> m_MemAddr >> m_Format;
		const CSymbol *symb = theApp.FindSymbol(m_MemAddr);
		m_MemAddress = symb ? symb->m_Address : strtoul(m_MemAddr, NULL, 16);
		ar >> m_DefName;
		UpdateAllViews(NULL, uavDisplayMemory);
		}
	else {
		ar << m_Size;
		ar.Write(m_Buffer, m_Size);
		ar << m_MemAddr << m_Format;
		ar << m_DefName;
		}
}

/////////////////////////////////////////////////////////////////////////////
// CSxbmemDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CSxbmemDoc commands
