Скачиваний:
11
Добавлен:
01.05.2014
Размер:
6.73 Кб
Скачать
// SketcherDoc.cpp : implementation of the CSketcherDoc class
//

#include "stdafx.h"
#include "Sketcher.h"

#include "Elements.h"
#include "SketcherDoc.h"

#include "Text.h"
#include "Triangle.h"
#include "Hash.h"
#include "utils.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSketcherDoc

IMPLEMENT_DYNCREATE(CSketcherDoc, CDocument)

BEGIN_MESSAGE_MAP(CSketcherDoc, CDocument)
	//{{AFX_MSG_MAP(CSketcherDoc)
	ON_COMMAND(ID_COLOR_BLACK, OnColorBlack)
	ON_COMMAND(ID_COLOR_BLUE, OnColorBlue)
	ON_COMMAND(ID_COLOR_GREEN, OnColorGreen)
	ON_COMMAND(ID_ELEMENT_LINE, OnElementLine)
	ON_COMMAND(ID_ELEMENT_RECTANGLE, OnElementRectangle)
	ON_COMMAND(ID_ELEMENT_TEXT, OnElementText)
	ON_COMMAND(ID_ELEMENT_TEXTINTRIANGLE, OnElementTextintriangle)
	ON_COMMAND(ID_ELEMENT_TRIANGLE, OnElementTriangle)
	ON_UPDATE_COMMAND_UI(ID_COLOR_BLACK, OnUpdateColorBlack)
	ON_UPDATE_COMMAND_UI(ID_COLOR_BLUE, OnUpdateColorBlue)
	ON_UPDATE_COMMAND_UI(ID_COLOR_GREEN, OnUpdateColorGreen)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_LINE, OnUpdateElementLine)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_RECTANGLE, OnUpdateElementRectangle)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_TEXT, OnUpdateElementText)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_TEXTINTRIANGLE, OnUpdateElementTextintriangle)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_TRIANGLE, OnUpdateElementTriangle)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSketcherDoc construction/destruction

CSketcherDoc::CSketcherDoc()
{
	// TODO: add one-time construction code here
   m_Element = LINE;   // Set initial element type
   m_Color = BLACK;    // Set initial drawing color
   m_DocSize = CSize(3000,3000);  // Set initial document size 30x30 inches
}

CSketcherDoc::~CSketcherDoc()
{
}

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

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

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CSketcherDoc serialization

// save/load in CArchive
CArchive& operator>>(CArchive& os, Serializable* o){
	return o->load(os);
}

CArchive& operator>>(CArchive& os, Serializable& o){
	return o.load(os);
}

CArchive& operator<<(CArchive& os, Serializable* o){
	return o->save(os);
}

CArchive& operator<<(CArchive& os, Serializable& o){
	return o.save(os);
}


void CSketcherDoc::Serialize(CArchive& ar) {
	m_Hash.Serialize(ar);    // Serialize hash

	if (ar.IsStoring()){
		ar	<< m_Color                // Store the current color
			<< m_Element              // the current element type,
			<< m_DocSize;             // and the current document size
	}
	else {
		ar	>> m_Color                // Retrieve the current color
			>> m_Element              // the current element type,
			>> m_DocSize;             // and the current document size
	}

	int i = 0;
}

/////////////////////////////////////////////////////////////////////////////
// CSketcherDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CSketcherDoc commands

void CSketcherDoc::OnColorBlack() {
	m_Color = BLACK;        // Set the drawing color to black
}

void CSketcherDoc::OnColorGreen() {
	m_Color = GREEN;          // Set the drawing color to green	
}

void CSketcherDoc::OnColorBlue() {
	m_Color = BLUE;          // Set the drawing color to blue	
}

void CSketcherDoc::OnElementLine() {
	m_Element = LINE;       // Set element type as a line	
}

void CSketcherDoc::OnElementRectangle() {
	m_Element = RECTANGLE;  // Set element type as a rectangle	
}

void CSketcherDoc::OnElementTriangle() {
	m_Element = TRIANGLE;  // Set element type as a triangle
}

void CSketcherDoc::OnElementText() {
	m_Element = TEXT;  // Set element type as a text
}

void CSketcherDoc::OnElementTextintriangle() {
	m_Element = TEXTINTRIANGLE;  // Set element type as a text in triangle
}

void CSketcherDoc::OnUpdateColorBlack(CCmdUI* pCmdUI) {
	// Set menu item Checked if the current color is black
	pCmdUI->SetCheck(m_Color==BLACK);
}

void CSketcherDoc::OnUpdateColorGreen(CCmdUI* pCmdUI) {
	// Set menu item Checked if the current color is green
	pCmdUI->SetCheck(m_Color==GREEN);
}

void CSketcherDoc::OnUpdateColorBlue(CCmdUI* pCmdUI) {
	// Set menu item Checked if the current color is blue
	pCmdUI->SetCheck(m_Color==BLUE);
}

void CSketcherDoc::OnUpdateElementLine(CCmdUI* pCmdUI) {
	// Set Checked if the current element is a line
	pCmdUI->SetCheck(m_Element==LINE);
}

void CSketcherDoc::OnUpdateElementRectangle(CCmdUI* pCmdUI) {
	// Set Checked if the current element is a rectangle
	pCmdUI->SetCheck(m_Element==RECTANGLE);
}

void CSketcherDoc::OnUpdateElementTriangle(CCmdUI* pCmdUI) {
	// Set Checked if the current element is a triangle
	pCmdUI->SetCheck(m_Element==TRIANGLE);
}

void CSketcherDoc::OnUpdateElementText(CCmdUI* pCmdUI) {
	// Set Checked if the current element is a text
	pCmdUI->SetCheck(m_Element==TEXT);	
}

void CSketcherDoc::OnUpdateElementTextintriangle(CCmdUI* pCmdUI) {
	// Set Checked if the current element is a text in triangle
	pCmdUI->SetCheck(m_Element==TEXTINTRIANGLE);
}


void CSketcherDoc::DeleteElement(string key) {
	if(key != ""){
		pair<pair<stringKey, CElement*>, bool> res = m_Hash.remove(stringKey(key));
		if (res.second == true){
			cout << "removed" << endl;
			cout << "now delete object it holds:" << endl;
			delete res.first.second; // el.first is HashPair, HashPair.second is Value
		}
		else{
			cout << "not found" << endl;
		}
	}
}

void CSketcherDoc::SendToBack(CElement* pElement){
}

// Get the rectangle enclosing the entire document
CRect CSketcherDoc::GetDocExtent(){
	CRect DocExtent(0,0,1,1);    // Initial document extent
	CRect ElementBound(0,0,0,0); // Space for element bounding rectangle

	Hash<stringKey, CElement*>::iterator it = m_Hash.begin();

	while(it != m_Hash.end()){
		pair<stringKey, CElement*> he = it++;

		// Get the bounding rectangle for the element
		ElementBound = (he.second)->GetBoundRect();

		// Make coordinates of document extent the outer limits
		DocExtent.UnionRect(DocExtent, ElementBound);
	}

	DocExtent.NormalizeRect();
	return DocExtent;
}

Соседние файлы в папке Sketcher05