Скачиваний:
7
Добавлен:
01.05.2014
Размер:
3.52 Кб
Скачать
#include "stdafx.h"
#include "Text.h"

Text::Text(string text, CPoint point, COLORREF aColor): id(++total){
	count++;
	cout << "create Text#" << id << endl;
	this->text = text;

	this->point1 = this->point2 = point;
	this->m_Color = aColor;
	this->m_Pen = 1;

	buildEnclosingRectangle();
}

Text::Text(const Text* o): id(++total){
	count++;
	cout << "create Text#" << id << " via cc" << endl;

	this->text = o->text;
	this->point1 = o->point1;
	this->point2 = o->point2;
	this->m_Color = o->m_Color;
	this->m_Pen = o->m_Pen;

	buildEnclosingRectangle();
}

Text::Text(const Text& o): id(++total){
	count++;
	cout << "create Text#" << id << " via cc" << endl;

	this->text = o.text;
	this->point1 = o.point1;
	this->point2 = o.point2;
	this->m_Color = o.m_Color;
	this->m_Pen = o.m_Pen;

	buildEnclosingRectangle();
}

void Text::buildEnclosingRectangle(){
	// Define the enclosing rectangle
	CPoint p1 = CPoint(point1.x - 3, point1.y + 3);
	m_EnclosingRect = CRect(p1, this->point2);
	m_EnclosingRect.NormalizeRect();
}

Text::~Text(){
	count--;
	cout << "destroy Text#" << id << endl;
}

void Text::setText(const string& s){
	text = s;
}

string Text::getText(){
	return text;
}

ostream& Text::print(ostream& os) const{
	os << "Text#" << id << ": " << toString();
	return os;
}

Text& Text::operator= (const Text& o){ 
	if(this == &o) 
		return *this;

	this->text = o.text;
	this->point1 = o.point1;
	this->point2 = o.point2;
	this->m_Color = o.m_Color;
	this->m_Pen = o.m_Pen;
	buildEnclosingRectangle();

	return *this;
}

void Text::Draw(CDC* pDC, CElement* pElement, string key){
   
	
	COLORREF aColor1 = (this == pElement)? SELECT_COLOR: WHITE;

	CPen aPen1;
	aPen1.CreatePen(PS_SOLID, m_Pen, aColor1);
	CPen* pOldPen1 = pDC->SelectObject(&aPen1);  // Select the pen

	CBrush* pOldBrush1 = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);  // Select the brush

	if (this == pElement){
		pDC->Rectangle(m_EnclosingRect);
	}

	//CPoint p = CPoint(p1.x, p2.y);
	if (key != "")
		m_keyRectangle = drawKey(pDC, key);
	else
		m_keyRectangle = CRect(0,0,0,0);

	pDC->SelectObject(pOldPen1);   
	pDC->SelectObject(pOldBrush1);              // Restore the old brush


	// now draw text
	pDC->SetTextColor(m_Color);

	CFont l_font; 
	l_font.CreatePointFont(140,"Arial"); 
	CFont* l_old_font = pDC->SelectObject(&l_font);
	pDC->TextOut(point1.x, point1.y, text.c_str(), text.length());

	CSize size = pDC->GetTextExtent(text.c_str());

	pDC->SelectObject(l_old_font); 
	// Delete the font object. 
	l_font.DeleteObject(); 

	this->point2 = CPoint(point1.x + size.cx + 5, point1.y - size.cy - 3);

	buildEnclosingRectangle();
}

string Text::toString() const {
	return text;
}

int Text::operator==(const Text& o) const {
	return (text == o.text);
};

void Text::Move(CSize& aSize)
{         
   point1 += aSize;           // Move points
   point2 += aSize;        
   m_EnclosingRect += aSize;    // Move the enclosing rectangle
}


// serialization
CArchive& Text::load(CArchive& ar) {
	CElement::load(ar);

	CString s;
	ar	>> s 
		>> point1 
		>> point2;

	text = string(s);
	buildEnclosingRectangle();

	return ar;
}

CArchive& Text::save(CArchive& ar) { 
	CElement::save(ar);

	CString s(text.c_str());
	ar	<< s 
		<< point1 
		<< point2;
	return ar;
}

// initialize static fields 
unsigned long int Text::count = 0;
unsigned long int Text::total = 0;
Соседние файлы в папке OLEApp