Скачиваний:
8
Добавлен:
01.05.2014
Размер:
9.97 Кб
Скачать
// OLEAppDoc.cpp : implementation of the COLEAppDoc class
//

#include "stdafx.h"
#include "OLEApp.h"

#include "OLEAppDoc.h"
#include "OLEAppView.h"
#include "CntrItem.h"
#include "SrvrItem.h"

#include "Elements.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

/////////////////////////////////////////////////////////////////////////////
// COLEAppDoc

IMPLEMENT_DYNCREATE(COLEAppDoc, COleServerDoc)

BEGIN_MESSAGE_MAP(COLEAppDoc, COleServerDoc)
	//{{AFX_MSG_MAP(COLEAppDoc)
	ON_COMMAND(ID_ELEMENT_LINE, OnElementLine)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_LINE, OnUpdateElementLine)
	ON_COMMAND(ID_ELEMENT_TRIANGLE, OnElementTriangle)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_TRIANGLE, OnUpdateElementTriangle)
	ON_COMMAND(ID_ELEMENT_TEXTINTRIANGLE, OnElementTextintriangle)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_TEXTINTRIANGLE, OnUpdateElementTextintriangle)
	ON_COMMAND(ID_ELEMENT_TEXT, OnElementText)
	ON_UPDATE_COMMAND_UI(ID_ELEMENT_TEXT, OnUpdateElementText)
	ON_COMMAND(ID_COLOR_BLACK, OnColorBlack)
	ON_UPDATE_COMMAND_UI(ID_COLOR_BLACK, OnUpdateColorBlack)
	ON_COMMAND(ID_COLOR_BLUE, OnColorBlue)
	ON_UPDATE_COMMAND_UI(ID_COLOR_BLUE, OnUpdateColorBlue)
	ON_COMMAND(ID_COLOR_GREEN, OnColorGreen)
	ON_UPDATE_COMMAND_UI(ID_COLOR_GREEN, OnUpdateColorGreen)
	//}}AFX_MSG_MAP
	// Enable default OLE container implementation
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, COleServerDoc::OnUpdatePasteMenu)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK, COleServerDoc::OnUpdatePasteLinkMenu)
	ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_CONVERT, COleServerDoc::OnUpdateObjectVerbMenu)
	ON_COMMAND(ID_OLE_EDIT_CONVERT, COleServerDoc::OnEditConvert)
	ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, COleServerDoc::OnUpdateEditLinksMenu)
	ON_COMMAND(ID_OLE_EDIT_LINKS, COleServerDoc::OnEditLinks)
	ON_UPDATE_COMMAND_UI_RANGE(ID_OLE_VERB_FIRST, ID_OLE_VERB_LAST, COleServerDoc::OnUpdateObjectVerbMenu)
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(COLEAppDoc, COleServerDoc)
	//{{AFX_DISPATCH_MAP(COLEAppDoc)
	DISP_FUNCTION(COLEAppDoc, "deleteElement", deleteElement, VT_BOOL, VTS_BSTR)
	DISP_FUNCTION(COLEAppDoc, "showWindow", showWindow, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(COLEAppDoc, "DrawLine", DrawLine, VT_EMPTY, VTS_R4 VTS_R4 VTS_R4 VTS_R4 VTS_BSTR)
	DISP_FUNCTION(COLEAppDoc, "getHashSize", getHashSize, VT_I2, VTS_NONE)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IOLEApp to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {AD5EFB2D-6225-448A-A71C-28B2D3BC4466}
static const IID IID_IOLEApp =
{ 0xad5efb2d, 0x6225, 0x448a, { 0xa7, 0x1c, 0x28, 0xb2, 0xd3, 0xbc, 0x44, 0x66 } };

BEGIN_INTERFACE_MAP(COLEAppDoc, COleServerDoc)
	INTERFACE_PART(COLEAppDoc, IID_IOLEApp, Dispatch)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COLEAppDoc construction/destruction

COLEAppDoc::COLEAppDoc()
{
	// Use OLE compound files
	EnableCompoundFile();

	// TODO: add one-time construction code here

	EnableAutomation();

	AfxOleLockApp();

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

COLEAppDoc::~COLEAppDoc()
{
	AfxOleUnlockApp();
}

BOOL COLEAppDoc::OnNewDocument()
{
	if (!COleServerDoc::OnNewDocument())
		return FALSE;

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

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// COLEAppDoc server implementation

// 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);
}

COleServerItem* COLEAppDoc::OnGetEmbeddedItem()
{
	// OnGetEmbeddedItem is called by the framework to get the COleServerItem
	//  that is associated with the document.  It is only called when necessary.

	COLEAppSrvrItem* pItem = new COLEAppSrvrItem(this);
	ASSERT_VALID(pItem);
	return pItem;
}



/////////////////////////////////////////////////////////////////////////////
// COLEAppDoc serialization

void COLEAppDoc::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;

	// Calling the base class COleServerDoc enables serialization
	//  of the container document's COleClientItem objects.
	COleServerDoc::Serialize(ar);
}

/////////////////////////////////////////////////////////////////////////////
// COLEAppDoc diagnostics

#ifdef _DEBUG
void COLEAppDoc::AssertValid() const
{
	COleServerDoc::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// COLEAppDoc commands

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

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

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

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

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

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

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

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

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

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

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

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

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

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


void COLEAppDoc::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;
			AfxMessageBox("Element with given key doesn't exist");
		}
	}
}

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

// Get the rectangle enclosing the entire document
CRect COLEAppDoc::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;
}

BOOL COLEAppDoc::OnUpdateDocument() 
{
	// TODO: Add your specialized code here and/or call the base class
	return COleServerDoc::OnUpdateDocument();
}

BOOL COLEAppDoc::deleteElement(LPCTSTR key) 
{
	// TODO: Add your dispatch handler code here
	DeleteElement(key);
	UpdateAllViews(NULL);
	SetModifiedFlag();
	return TRUE;
}

void COLEAppDoc::showWindow() 
{
	POSITION pos = GetFirstViewPosition();
	CView* pView = GetNextView(pos);
	if (pView != NULL)
	{
		CFrameWnd* pFrameWnd = pView->GetParentFrame();
		pFrameWnd->ActivateFrame(SW_SHOW);
		pFrameWnd = pFrameWnd->GetParentFrame();
		if (pFrameWnd != NULL)
			pFrameWnd->ActivateFrame(SW_SHOW);
	}
}

void COLEAppDoc::DrawLine(float x1, float y1, float x2, float y2, LPCTSTR key) 
{
	CElement* line = new Line(CPoint(x1, y1), CPoint(x2, y2), GetElementColor());
	AddElement(line, key);
	UpdateAllViews(NULL);
	SetModifiedFlag();
}

short COLEAppDoc::getHashSize() 
{
	return m_Hash.getSize();
}

void COLEAppDoc::OnSetItemRects(LPCRECT lpPosRect, LPCRECT lpClipRect) 
{
	COleServerDoc::OnSetItemRects(lpPosRect, lpClipRect);

	// notify first view that scroll info should change
	POSITION pos = GetFirstViewPosition();
	COLEAppView* v = (COLEAppView*)GetNextView(pos);
	v->ResetScrollSizes();
}
Соседние файлы в папке OLEApp