//	Zinc Application Framework - Z_CHART.CPP
//	COPYRIGHT (C) 1990-1999.  All Rights Reserved.
//	Zinc Software Incorporated.  Pleasant Grove, Utah  USA

#include <z_chart.hpp>
#define ZAF_CHART_INFO
#include "gbl_def.cpp"
#include <z_string.hpp>
            
ZafChartStub::ZafChartStub(int left, int top, int width, int height,
	int iChartTypeID, int iIndexMethodID) :
	ZafWindowObject(left, top, width, height),
	axisColor(ZAF_CLR_BLACK), monoAxisColor(ZAF_MONO_BLACK), marksXAxis(true),
	marksYAxis(true),
	textTitle(ZAF_NULLP(ZafIChar)), textXAxis(ZAF_NULLP(ZafIChar)),
	textYAxis(ZAF_NULLP(ZafIChar))
{
	SetNoncurrent(true);
	SetOSDraw(false);
	SetChartType(iChartTypeID);
	SetIndexMethod(iIndexMethodID);
}

ZafChartStub::ZafChartStub(const ZafChartStub &copy) :
	ZafWindowObject(copy),
	textTitle(ZAF_NULLP(ZafIChar)), textXAxis(ZAF_NULLP(ZafIChar)),
	textYAxis(ZAF_NULLP(ZafIChar))
{
	SetAxisColor(copy.axisColor, copy.monoAxisColor);
	SetChartType(copy.chartType);
	SetIndexMethod(copy.indexMethod);
	SetMarksXAxis(copy.marksXAxis);
	SetMarksYAxis(copy.marksYAxis);
	if (copy.textTitle)
		SetTextTitle(copy.textTitle);
	if (copy.textXAxis)
		SetTextXAxis(copy.textXAxis);
	if (copy.textYAxis)
		SetTextYAxis(copy.textYAxis);
}

ZafChartStub::~ZafChartStub(void)
{
	if (textTitle)
		delete (ZafIChar *)textTitle;
	if (textXAxis)
		delete (ZafIChar *)textXAxis;
	if (textYAxis)
		delete (ZafIChar *)textYAxis;
}

bool ZafChartStub::IsA(ZafClassID compareID) const
{
	return ((compareID == classID) ? true : ZafWindowObject::IsA(compareID));
}

bool ZafChartStub::IsA(ZafClassName compareName) const
{
	return (!strcmp(compareName, className) ? true : ZafWindowObject::IsA(compareName));
}

// --- Attributes ---

ZafLogicalColor ZafChartStub::AxisColor(ZafLogicalColor *mono)
{
	if (mono)
		*mono = monoAxisColor;
	return (axisColor);
}

int ZafChartStub::ChartType(void) const
{
	return (chartType);
}

int ZafChartStub::IndexMethod(void) const
{
	return (indexMethod);
}

bool ZafChartStub::MarksXAxis(void) const
{
	return (marksXAxis);
}

bool ZafChartStub::MarksYAxis(void) const
{
	return (marksYAxis);
}

const ZafIChar *ZafChartStub::TextTitle(void)
{
	return (textTitle);
}

const ZafIChar *ZafChartStub::TextXAxis(void)
{
	return (textXAxis);
}

const ZafIChar *ZafChartStub::TextYAxis(void)
{
	return (textYAxis);
}

ZafLogicalColor ZafChartStub::SetAxisColor(ZafLogicalColor color, ZafLogicalColor mono)
{
	axisColor = color;
	monoAxisColor = mono;
	return (axisColor);
}

int ZafChartStub::SetChartType(int chartTypeID)
{
	chartType = chartTypeID;
	return (chartType);
}

int ZafChartStub::SetIndexMethod(int indexMethodID)
{
	indexMethod = indexMethodID;
	return (indexMethod);
}

bool ZafChartStub::SetMarksXAxis(bool xAxisMarks)
{
	marksXAxis = xAxisMarks;
	return (marksXAxis);
}

bool ZafChartStub::SetMarksYAxis(bool yAxisMarks)
{
	marksYAxis = yAxisMarks;
	return (marksYAxis);
}

const ZafIChar *ZafChartStub::SetTextTitle(const ZafIChar *text)
{
	if (textTitle == text)
		return (textTitle);

	if (textTitle)
		delete (ZafIChar *)textTitle;

	if (text)
		textTitle = strdup(text);
	else
		textTitle = ZAF_NULLP(ZafIChar);

	return (textTitle);
}

const ZafIChar *ZafChartStub::SetTextXAxis(const ZafIChar *text)
{
	if (textXAxis == text)
		return (textXAxis);

	if (textXAxis)
		delete (ZafIChar *)textXAxis;

	if (text)
		textXAxis = strdup(text);
	else
		textXAxis = ZAF_NULLP(ZafIChar);

	return (textXAxis);
}

const ZafIChar *ZafChartStub::SetTextYAxis(const ZafIChar *text)
{
	if (textYAxis == text)
		return (textYAxis);

	if (textYAxis)
		delete (ZafIChar *)textYAxis;

	if (text)
		textYAxis = strdup(text);
	else
		textYAxis = ZAF_NULLP(ZafIChar);

	return (textYAxis);
}

// --- Window object members ---

ZafEventType ZafChartStub::Draw(const ZafEventStruct& event, ZafEventType ccode)
{
	// First render the window object portion.
	ZafRegionStruct drawRegion = BeginDraw();
	DrawBackground(drawRegion, ccode);

	display->SetLineStyle(ZAF_LINE_DOTTED);
	display->SetForeground(axisColor);
	display->Rectangle(drawRegion);

	display->SetFont(ZAF_FNT_DIALOG);
	display->Text(drawRegion, className, -1, ZAF_HZ_CENTER);

	EndDraw();
	return (event.type);
}

ZafWindowObject *ZafChartStub::Duplicate(void)
{
	return (new ZafChartStub(*this));
}

// --- Blocking Functions ---

bool ZafChartStub::SetAcceptDrop(bool)
{
	// acceptDrop is false for this class.
	acceptDrop = false;
	return (acceptDrop);
}

bool ZafChartStub::SetChanged(bool)
{
	// changed is false for this class.
	changed = false;
	return (changed);
}

bool ZafChartStub::SetCopyDraggable(bool)
{
	// copyDraggable is false for this class.
	copyDraggable = false;
	return (copyDraggable);
}

bool ZafChartStub::SetDisabled(bool)
{
	// disabled is false for this class.
	disabled = false;
	return (disabled);
}

bool ZafChartStub::SetFocus(bool)
{
	// focus is false for this class.
	focus = false;
	return (focus);
}

const ZafIChar *ZafChartStub::SetHelpContext(const ZafIChar *)
{
	// helpContext is empty for this class.
	return (ZAF_NULLP(ZafIChar));
}

bool ZafChartStub::SetLinkDraggable(bool)
{
	// linkDraggable is false for this class.
	linkDraggable = false;
	return (linkDraggable);
}

bool ZafChartStub::SetMoveDraggable(bool)
{
	// moveDraggable is false for this class.
	moveDraggable = false;
	return (moveDraggable);
}

bool ZafChartStub::SetNoncurrent(bool)
{
	// noncurrent is true for this class.
	noncurrent = true;
	return (noncurrent);
}

// --- Persistence ---
#if defined(USE_ZAF_PERSISTENCE)
ZafChartStub::ZafChartStub(const ZafIChar *name, ZafObjectPersistence &persist) :
	ZafWindowObject(name, persist.PushLevel(className, classID, ZAF_PERSIST_DIRECTORY)),
	textTitle(ZAF_NULLP(ZafIChar)), textXAxis(ZAF_NULLP(ZafIChar)),
	textYAxis(ZAF_NULLP(ZafIChar))
{
	// Read the data.
	if (persist.Error() == ZAF_ERROR_NONE)
		ZafChartStub::ReadData(persist);
	persist.PopLevel();
	if (persist.Error() != ZAF_ERROR_NONE)
		SetError(persist.Error());
}

ZafElement *ZafChartStub::Read(const ZafIChar *name, ZafObjectPersistence &persist)
{
	return (new ZafChartStub(name, persist));
}

void ZafChartStub::ReadData(ZafObjectPersistence &persist)
{
	ZafFile *file = persist.CurrentFile();

	// Read the data.
	int tChartType;
	int tIndexMethod;
	ZafUInt16 flags;
	ZafIChar stringBuffer[ZAF_MAXNAMELEN];
	ZafInt8 axis, mono;

	*file >> axis >> mono;
	axisColor = axis;
	monoAxisColor = mono;

	*file >> tChartType >> tIndexMethod >> flags;
	SetChartType(tChartType);
	SetIndexMethod(tIndexMethod);
	SetMarksXAxis((flags & 0x0001) ? true : false);
	SetMarksYAxis((flags & 0x0002) ? true : false);

	*file >> stringBuffer;
	SetTextTitle(stringBuffer);

	*file >> stringBuffer;
	SetTextXAxis(stringBuffer);

	*file >> stringBuffer;
	SetTextYAxis(stringBuffer);
}

void ZafChartStub::Write(ZafObjectPersistence &persist)
{
	ZafWindowObject::Write(persist.PushLevel(className, classID, ZAF_PERSIST_DIRECTORY));
	if (persist.Error() == ZAF_ERROR_NONE)
		ZafChartStub::WriteData(persist);
	persist.PopLevel();
	if (persist.Error() != ZAF_ERROR_NONE)
		SetError(persist.Error());
}

void ZafChartStub::WriteData(ZafObjectPersistence &persist)
{
	// Write the data.
	ZafUInt16 flags = 0;
	flags |= (ZafUInt16)(marksXAxis ? 0x0001 : 0);
	flags |= (ZafUInt16)(marksYAxis ? 0x0002 : 0);

	ZafInt8 axis, mono;
	axis = axisColor;
	mono = monoAxisColor;

	ZafFile *file = persist.CurrentFile();
	*file << axis << mono << chartType << indexMethod << flags
		<< textTitle << textXAxis << textYAxis;
}
#endif
