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

#include "atr_main.hpp"
#include <cf_api/bz_zcht.hpp>
#include <cff3_zaf.h>

const ZafEventType TST_SET_AXIS_GREEN       = 14002;
const ZafEventType TST_SET_AXIS_YELLOW      = 14003;
const ZafEventType TST_SET_AXIS_BLUE        = 14004;
const ZafEventType TST_SET_AXIS_DEFAULT     = 14005;
const ZafEventType TST_SET_TITLE_TEXT     	= 14006;
const ZafEventType TST_SET_XAXIS_TEXT     	= 14007;
const ZafEventType TST_SET_YAXIS_TEXT     	= 14008;

const ZafEventType TST_SET_TYPE_BASE		= 15000 + 0;
const ZafEventType TST_SET_TYPE_PIE			= 15000 + ZAF_PIE_2D;
const ZafEventType TST_SET_TYPE_BAR			= 15000 + ZAF_BAR_2D;
const ZafEventType TST_SET_TYPE_COLUMN		= 15000 + ZAF_COL_2D;
const ZafEventType TST_SET_TYPE_STACK		= 15000 + ZAF_STACK_2D;
const ZafEventType TST_SET_TYPE_POINTS		= 15000 + ZAF_POINTS_2D;
const ZafEventType TST_SET_TYPE_LINE		= 15000 + ZAF_LINE_2D;
const ZafEventType TST_SET_TYPE_AREA		= 15000 + ZAF_AREA_2D;
const ZafEventType TST_SET_INDEX_NAT		= 16000 + ZAF_RANGE_NATURAL;
const ZafEventType TST_SET_INDEX_INT		= 16000 + ZAF_RANGE_INTEGER;

ListEntry ZAF_FARDATA GenericTestWin::AttributeTable[] =
{
	// ZafChart
	{ ZAF_ITEXT("X-Axis Marks"),      0, TST_TOGGLE_XAXIS_MARKS },
	{ ZAF_ITEXT("Y-Axis Marks"),      0, TST_TOGGLE_YAXIS_MARKS },

	// ZafWindowObject
	{ ZAF_ITEXT("Bordered"),          0, TST_TOGGLE_BORDERED },
	{ ZAF_ITEXT("Visible *"),         0, TST_TOGGLE_VISIBLE },
//	{ ZAF_ITEXT("Accept drop *"),     0, TST_TOGGLE_ACCEPT_DROP },
	{ ZAF_ITEXT("Auto update *"),     0, TST_TOGGLE_AUTOMATIC_UPDATE },
//	{ ZAF_ITEXT("Changed *"),         0, TST_TOGGLE_CHANGED },
//	{ ZAF_ITEXT("Copy Draggable *"),  0, TST_TOGGLE_COPY_DRAGGABLE },
//	{ ZAF_ITEXT("Link Draggable *"),  0, TST_TOGGLE_LINK_DRAGGABLE },
//	{ ZAF_ITEXT("Move Draggable *"),  0, TST_TOGGLE_MOVE_DRAGGABLE },
//	{ ZAF_ITEXT("Focus *"),           0, TST_TOGGLE_FOCUS },
	{ ZAF_ITEXT("Noncurrent *"),      0, TST_TOGGLE_NONCURRENT },
	{ ZAF_ITEXT("Disabled *"),        0, TST_TOGGLE_DISABLED },
	{ ZAF_ITEXT("OSdraw"),            0, TST_TOGGLE_OSDRAW },
	{ ZAF_ITEXT("Parentdraw border"), 0, TST_TOGGLE_PARENTDRAW_BORDER },
	{ ZAF_ITEXT("Parent palette"),    0, TST_TOGGLE_PARENTPALETTE },
	{ ZAF_ITEXT("Parentdraw focus"),  0, TST_TOGGLE_PARENTDRAW_FOCUS },
//	{ ZAF_ITEXT("Selected *"),        0, TST_TOGGLE_SELECTED },
	{ ZAF_ITEXT("Edit mode"),         0, TST_TOGGLE_EDIT_MODE },
	{ ZAF_NULLP(ZafIChar),            0, 0 }
};

// ---- Derived test object -------------------------------------------------

// Derive a test object so can log which events it gets.
class TestObj : public ZafChart
{
public:
  	TestObj();
    void         SendTestData();
	ZafEventType Event(const ZafEventStruct &event);
};

TestObj::TestObj() : ZafChart(50, 0, 39, 10, CF_TYPE_PSER, CF_INDEX_NAT_N)
{
	// !!! MUST BE "TEST_OBJECT" so can initialize testObject pointer !!!
	SetStringID(ZAF_ITEXT("TEST_OBJECT"));
    SendTestData();  // as per immediately below...
}

void  TestObj::SendTestData()
{
	const int NUM_POINTS = 10;

	double SET1[ NUM_POINTS ] = { 20, 10, 20, 10, 20, 10, 20, 10, 20, 10 };
	double SET2[ NUM_POINTS ] = { 20, 18, 14, 2, 14, 37, 35, 30, 24, 23 };
	double SET3[ NUM_POINTS ] = { 33, 30, 27, 24, 21, 18, 15, 12, 9, 6 };
	double SET4[ NUM_POINTS ] = { 20, 15, 10, 12, 14, 20, 24, 28, 30, 32 };
	double SET5[ NUM_POINTS ] = { 16, 15, 23, 17, 24, 20, 18, 13, 28, 16 };

	int i,j;
	double *p_dTargetArray = &SET1[ 0 ];

	for (i = 1; i <= 5; i++)
	{
		// Iterate through the target data array/pointer addresses:
		if (i == 2)
			p_dTargetArray = &SET2[ 0 ];
		if (i == 3)
			p_dTargetArray = &SET3[ 0 ];
		if (i == 4)
			p_dTargetArray = &SET4[ 0 ];
		if (i == 5)
			p_dTargetArray = &SET5[ 0 ];

		// Activate the appropriate data group:
		AddDataGroup(i);

		// Add data points into the active data group:
		for (j = 0; j < NUM_POINTS; j++)
			AddDataPoint(i, *p_dTargetArray++);
	}
}

ZafEventType TestObj::Event(const ZafEventStruct &event)
{
	// Log the event and then pass to base class to be handled.
	ZafEventType ccode = LogicalEvent(event);
	GenericTestWin *testWin = DynamicPtrCast(parent, GenericTestWin);
	testWin->LogEvent(ccode);

	ccode = ZafChart::Event(event);

	if (ccode == S_CURRENT || ccode == S_NON_CURRENT)
	{
		ZafVtList *attrList= DynamicPtrCast(testWin->GetObject(ZAF_ITEXT("ATTR_LIST")), ZafVtList);
		attrList->GetObject((ZafNumberID)TST_TOGGLE_FOCUS)->SetSelected(Focus());
	}

	return (ccode);
}

// ---- Derived test window -------------------------------------------------

ObjectTestWin::ObjectTestWin() : GenericTestWin(8, 1, 90, 15)
{
	// The test object can place settings combo boxes, etc. in the area
	// 1, 8 (left, top) 47, 9 (right, bottom).

	// Test objects can be placed in the area to the right of the divider
	// line.  So an area of 50, 1 (left, top) to 78, 9 (right, bottom) can
	// be used.  This area can be bigger or smaller simply by adjusting
	// the size passed to the GenericTestWin constructor above.

	// Set the title text.
	SetText(ZAF_ITEXT("ZafChart"));

	// Add settings objects.
	ZafPrompt *prompt = new ZafPrompt(1, 8, 10, new ZafStringData("Type:"));
	prompt->SetHzJustify(ZAF_HZ_RIGHT);
	Add(prompt);
	ZafComboBox *typeCombo = new ZafComboBox(12, 8, 14, 3);
	typeCombo->SetViewOnly(true);
	typeCombo->SetStringID(ZAF_ITEXT("TYPE_COMBO"));
	typeCombo->Add(new ZafScrollBar(0, 0, 0, 0, ZAF_NULLP(ZafScrollData)));
	ZafButton *attr = new ZafButton(0, 0, 15, 1, new ZafStringData("Base"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_TYPE_BASE);
	attr->SetNumberID((ZafNumberID)TST_SET_TYPE_BASE);
	typeCombo->Add(attr);
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("2D Pie"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_TYPE_PIE);
	attr->SetNumberID((ZafNumberID)TST_SET_TYPE_PIE);
	typeCombo->Add(attr);
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("2D Bar"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_TYPE_BAR);
	attr->SetNumberID((ZafNumberID)TST_SET_TYPE_BAR);
	typeCombo->Add(attr);
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("2D Column"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_TYPE_COLUMN);
	attr->SetNumberID((ZafNumberID)TST_SET_TYPE_COLUMN);
	typeCombo->Add(attr);
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("2D Stack"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_TYPE_STACK);
	attr->SetNumberID((ZafNumberID)TST_SET_TYPE_STACK);
	typeCombo->Add(attr);
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("2D Points"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_TYPE_POINTS);
	attr->SetNumberID((ZafNumberID)TST_SET_TYPE_POINTS);
	typeCombo->Add(attr);
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("2D Line"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_TYPE_LINE);
	attr->SetNumberID((ZafNumberID)TST_SET_TYPE_LINE);
	typeCombo->Add(attr);
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("2D Area"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_TYPE_AREA);
	attr->SetNumberID((ZafNumberID)TST_SET_TYPE_AREA);
	typeCombo->Add(attr);
	Add(typeCombo);

	// Add settings objects.
	prompt = new ZafPrompt(1, 9, 10, new ZafStringData("Index Method:"));
	prompt->SetHzJustify(ZAF_HZ_RIGHT);
	Add(prompt);
	ZafComboBox *methodCombo = new ZafComboBox(12, 9, 14, 3);
	methodCombo->SetViewOnly(true);
	methodCombo->SetStringID(ZAF_ITEXT("METHOD_COMBO"));
	methodCombo->Add(new ZafScrollBar(0, 0, 0, 0, ZAF_NULLP(ZafScrollData)));
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("Natural Numbers"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_INDEX_NAT);
	attr->SetNumberID((ZafNumberID)TST_SET_INDEX_NAT);
	methodCombo->Add(attr);
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("Integer Indices"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_INDEX_INT);
	attr->SetNumberID((ZafNumberID)TST_SET_INDEX_INT);
	methodCombo->Add(attr);
	Add(methodCombo);

	// Create axis color combo box.
	Add(new ZafPrompt(27, 5, 9, new ZafStringData("Axis:**")));
	ZafComboBox *axisColorCombo = new ZafComboBox(37, 5, 10, 3);
	axisColorCombo->SetViewOnly(true);
	axisColorCombo->SetStringID(ZAF_ITEXT("AXIS_COLOR_COMBO"));
	axisColorCombo->Add(new ZafScrollBar(0, 0, 0, 0, ZAF_NULLP(ZafScrollData)));
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("Default"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_AXIS_DEFAULT);
	attr->SetSelected(true);
	axisColorCombo->Add(attr);
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("Green"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_AXIS_GREEN);
	axisColorCombo->Add(attr);
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("Blue"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_AXIS_BLUE);
	axisColorCombo->Add(attr);
	attr = new ZafButton(0, 0, 15, 1, new ZafStringData("Yellow"));
	attr->SetButtonType(ZAF_FLAT_BUTTON);
	attr->SetHzJustify(ZAF_HZ_LEFT);
	attr->SetSendMessageWhenSelected(true);
	attr->SetValue(TST_SET_AXIS_YELLOW);
	axisColorCombo->Add(attr);

	Add(axisColorCombo);

	prompt = new ZafPrompt(50, 12, 6, new ZafStringData("Title:"));
	prompt->SetHzJustify(ZAF_HZ_RIGHT);
	Add(prompt);
	ZafString *string = new ZafString(57, 12, 17, "", -1);
	string->SetNumberID((ZafNumberID)TST_SET_TITLE_TEXT);
	Add(string);
	ZafButton *button = new ZafButton(75, 12, 6, 1, "Set");
	button->SetSendMessageWhenSelected(true);
	button->SetValue(TST_SET_TITLE_TEXT);
	button->SetAutoSize(false);
	Add(button);

	prompt = new ZafPrompt(50, 13, 6, new ZafStringData("X-Axis:"));
	prompt->SetHzJustify(ZAF_HZ_RIGHT);
	Add(prompt);
	string = new ZafString(57, 13, 17, "", -1);
	string->SetNumberID((ZafNumberID)TST_SET_XAXIS_TEXT);
	Add(string);
	button = new ZafButton(75, 13, 6, 1, "Set");
	button->SetSendMessageWhenSelected(true);
	button->SetValue(TST_SET_XAXIS_TEXT);
	button->SetAutoSize(false);
	Add(button);

	prompt = new ZafPrompt(50, 14, 6, new ZafStringData("Y-Axis:"));
	prompt->SetHzJustify(ZAF_HZ_RIGHT);
	Add(prompt);
	string = new ZafString(57, 14, 17, "", -1);
	string->SetNumberID((ZafNumberID)TST_SET_YAXIS_TEXT);
	Add(string);
	button = new ZafButton(75, 14, 6, 1, "Set");
	button->SetSendMessageWhenSelected(true);
	button->SetValue(TST_SET_YAXIS_TEXT);
	button->SetAutoSize(false);
	Add(button);

	// Add the test object.
	testObject = new TestObj;
	Add(testObject);
}

ZafEventType ObjectTestWin::Event(const ZafEventStruct &event)
{
    TestObj  *p_testObject = DynamicPtrCast(testObject, TestObj);
	ZafChart *chart = DynamicPtrCast(testObject, ZafChart);

	ZafEventType ccode = event.type;
	switch (ccode)
	{
	case TST_SET_TYPE_BASE:
	case TST_SET_TYPE_PIE:
	case TST_SET_TYPE_BAR:
	case TST_SET_TYPE_COLUMN:
	case TST_SET_TYPE_STACK:
	case TST_SET_TYPE_POINTS:
	case TST_SET_TYPE_LINE:
	case TST_SET_TYPE_AREA:
		{
            int iCurrIndexMode = chart->IndexMethod();
            chart->ClearDataAll();
            p_testObject->SendTestData();
	    	chart->SetChartType( ccode - 15000 );
		    chart->SetIndexMethod( iCurrIndexMode );

		    int type = chart->ChartType() + 15000;
		    switch (type)
		    {
		        case TST_SET_TYPE_BASE:
		        case TST_SET_TYPE_PIE:
		        case TST_SET_TYPE_BAR:
		        case TST_SET_TYPE_COLUMN:
		        case TST_SET_TYPE_STACK:
		        case TST_SET_TYPE_POINTS:
		        case TST_SET_TYPE_LINE:
		        case TST_SET_TYPE_AREA:
			    {
			        ZafComboBox *typeCombo = DynamicPtrCast(GetObject(ZAF_ITEXT("TYPE_COMBO")), ZafComboBox);
			    typeCombo->GetObject((ZafNumberID)type)->SetSelected(true);
			    }
			break;
		    }
		}
		break;

	case TST_SET_INDEX_NAT:
	case TST_SET_INDEX_INT:
        {
            int iCurrChartType = chart->ChartType();
            chart->ClearDataAll();
            p_testObject->SendTestData();
	    	chart->SetChartType( iCurrChartType );
		    chart->SetIndexMethod( ccode - 16000 );

		    int indexMethod = chart->IndexMethod() + 16000;
		    switch (indexMethod)
		    {
		        case TST_SET_INDEX_NAT:
		        case TST_SET_INDEX_INT:
			    {
			        ZafComboBox *methodCombo = DynamicPtrCast(GetObject(ZAF_ITEXT("METHOD_COMBO")), ZafComboBox);
			        methodCombo->GetObject((ZafNumberID)indexMethod)->SetSelected(true);
			    }
			    break;
		    }
		}
		break;

	case TST_SET_AXIS_DEFAULT:
		chart->SetAxisColor(ZAF_CLR_NULL);
		break;

	case TST_SET_AXIS_GREEN:
		chart->SetAxisColor(ZAF_CLR_LIGHTGREEN);
		break;

	case TST_SET_AXIS_YELLOW:
		chart->SetAxisColor(ZAF_CLR_YELLOW);
		break;

	case TST_SET_AXIS_BLUE:
		chart->SetAxisColor(ZAF_CLR_LIGHTBLUE);
		break;

	case TST_TOGGLE_XAXIS_MARKS:
		{
		ZafWindowObject *attr = attrList->GetObject((ZafNumberID)TST_TOGGLE_XAXIS_MARKS);
		chart->SetMarksXAxis(attr->Selected());
		attr->SetSelected(chart->MarksXAxis());

		// Make sure counts get updated.
		GenericTestWin::Event(event);
		}
		break;

	case TST_TOGGLE_YAXIS_MARKS:
		{
		ZafWindowObject *attr = attrList->GetObject((ZafNumberID)TST_TOGGLE_YAXIS_MARKS);
		chart->SetMarksYAxis(attr->Selected());
		attr->SetSelected(chart->MarksYAxis());

		// Make sure counts get updated.
		GenericTestWin::Event(event);
		}
		break;

	case TST_SET_TITLE_TEXT:
		chart->SetTextTitle(GetObject(ccode)->Text());
		break;

	case TST_SET_XAXIS_TEXT:
		chart->SetTextXAxis(GetObject(ccode)->Text());
		break;

	case TST_SET_YAXIS_TEXT:
		chart->SetTextYAxis(GetObject(ccode)->Text());
		break;

	default:
		ccode = GenericTestWin::Event(event);
		break;
	}

	return (ccode);
}

void ObjectTestWin::SynchronizeAttributes()
{
	ZafChart *chart = DynamicPtrCast(testObject, ZafChart);

	// Preset attributes according to test object's settings.
	attrList->GetObject((ZafNumberID)TST_TOGGLE_XAXIS_MARKS)->SetSelected(chart->MarksXAxis());
	attrList->GetObject((ZafNumberID)TST_TOGGLE_YAXIS_MARKS)->SetSelected(chart->MarksYAxis());

	GenericTestWin::SynchronizeAttributes();

	// Set settings fields disabled.
	attrList->GetObject((ZafNumberID)TST_TOGGLE_NONCURRENT)->SetDisabled(true);
	GetObject(ZAF_ITEXT("HELP_CONTEXT_COMBO"))->SetDisabled(true);
}
