//-------------------------------------------
// DrawList.cpp - sicherer@sichemsoft.nl
// Complete commented code available on line
// NOTE: only functions whose prototypes are
// shown in Figure 3 are shown here!
//-------------------------------------------
// .... #includes and typedefs omitted
//-------------------------------------------
TDrawItem::TDrawItem(double x1, double y1,
double x2, double y2, TColor lineColor,
TColor fillColor) :
X1(x1), Y1(y1), X2(x2), Y2(y2),
LineColor(lineColor),
FillColor(fillColor)
{}
//-------------------------------------------
TDrawItem::TDrawItem(ifstream &ifp)
{
Read(ifp, X1); Read(ifp, Y1);
Read(ifp, X2); Read(ifp, Y2);
Read(ifp, LineColor); Read(ifp, FillColor);
}
//-------------------------------------------
TDrawItem *TDrawItem::Load(ifstream &ifp)
// static function
{
TDrawItemType type;
Read(ifp, type);
switch (type) {
case line: return new TLineItem(ifp);
case rectangle:
return new TRectangleItem(ifp);
case roundrect:
return new TRoundRectItem(ifp);
case ellipse: return new TEllipseItem(ifp);
case invert: return new TInvertItem(ifp);
case text: return new TTextItem(ifp);
}
return NULL;
}
//-------------------------------------------
void
TDrawItem::Save(ofstream &ofp,
TDrawItemType type)
{
Write(ofp, type);
Write(ofp, X1); Write(ofp, Y1);
Write(ofp, X2); Write(ofp, Y2);
Write(ofp, LineColor);
Write(ofp, FillColor);
}
//-------------------------------------------
TLineItem::TLineItem(double x1, double y1,
double x2, double y2, TColor lineColor):
TDrawItem(x1, y1, x2, y2,
lineColor, NOCOLOR),
LX1(x1), LY1(y1), LX2(x2), LY2(y2)
{
if (X1 > X2) Swap(X1, X2);
if (Y1 > Y2) Swap(Y1, Y2);
}
//-------------------------------------------
TLineItem::TLineItem(ifstream &ifp):
TDrawItem(ifp)
{
Read(ifp, LX1); Read(ifp, LY1);
Read(ifp, LX2); Read(ifp, LY2);
}
//-------------------------------------------
void TLineItem::Save(ofstream &ofp) // virtual
{
TDrawItem::Save(ofp, line);
Write(ofp, LX1); Write(ofp, LY1);
Write(ofp, LX2); Write(ofp, LY2);
}
//-------------------------------------------
void TLineItem::Draw(TDrawBox *drawBox)// vir.
{
TColor oldPenColor;
SetPen(&oldPenColor, drawBox);
drawBox->DrawLine(LX1, LY1, LX2, LY2);
ResetPen(oldPenColor, drawBox);
}
//-------------------------------------------
void TTextItem::Draw(TDrawBox *drawBox)// vir.
{
HDC hdc = drawBox->GetCanvas()->Handle;
int oldBkMode = ::GetBkMode(hdc);
::SetBkMode(hdc,
FillColor == NOCOLOR ? TRANSPARENT :
OPAQUE);
COLORREF oldTextColor =
::GetTextColor(hdc);
::SetTextColor(hdc, ColorToRGB(LineColor));
COLORREF oldBkColor = ::GetBkColor(hdc);
::SetBkColor(hdc, ColorToRGB(FillColor));
// now draw the text normally
drawBox->DrawText(X1, Y1, String);
// restore everything we've done
::SetBkMode(hdc, oldBkMode);
::SetTextColor(hdc, oldTextColor);
::SetBkColor(hdc, oldBkColor);
}
//-------------------------------------------
TDrawList::TDrawList()
{
Current = List.begin();
}
//-------------------------------------------
TDrawList::~TDrawList()
{
Clear();
}
//-------------------------------------------
bool
TDrawList::Load(const AnsiString &filename)
{
ifstream ifp;
ifp.open(filename.c_str(),
ios::in | ios::binary);
if (ifp.fail())
return false;
Clear();
int size; Read(ifp, size);
for (int i = 0; i < size; ++i) {
TDrawItem *item = TDrawItem::Load(ifp);
List.push_back(item);
}
ifp.close();
return true;
}
//-------------------------------------------
void
TDrawList::Save(const AnsiString &filename)
{
ofstream ofp;
ofp.open(filename.c_str(),
ios::out | ios::binary);
if (ofp.fail())
return;
int count = List.size();
Write(ofp, count);
TInternalDrawList::iterator i;
for (i = List.begin();i != List.end(); ++i)
(*i)->Save(ofp);
ofp.close();
}
//-------------------------------------------
void TDrawList::Draw(TDrawBox *drawBox)
{
drawBox->Clear();
TInternalDrawList::iterator i;
for (i = List.begin();i != List.end(); ++i)
(*i)->Draw(drawBox);
}