// COPYRIGHT (C) 1990-1999.  All Rights Reserved. - TABLE5.CPP
// Zinc Software Incorporated.  Pleasant Grove, Utah  USA
// May be freely copied, used and distributed.

#include "table5.hpp"

struct ButtonData
{
	bool selected;
	bool recordSelected;
	ZafIChar btnText[64];
	ZafIChar strText[64];
} buttonTable[] =
{
	{ false, false, ZAF_ITEXT("Button 1"), ZAF_ITEXT("String 1") },
	{ false, false, ZAF_ITEXT("Button 2"), ZAF_ITEXT("String 2")  },
	{ true , false, ZAF_ITEXT("Button 3"), ZAF_ITEXT("String 3")  },
	{ false, false, ZAF_ITEXT("Button 4"), ZAF_ITEXT("String 4")  },
	{ false, false, ZAF_ITEXT("Button 5"), ZAF_ITEXT("String 5")  },
	{ false, false, ZAF_ITEXT("Button 6"), ZAF_ITEXT("String 6")  },
	{ true,  false, ZAF_ITEXT("Button 7"), ZAF_ITEXT("String 7")  },
	{ false, false, ZAF_ITEXT("Button 8"), ZAF_ITEXT("String 8")  },
	{ false, false, ZAF_ITEXT("Button 9"), ZAF_ITEXT("String 9")  },
	{ false, false, ZAF_ITEXT("Button 10"), ZAF_ITEXT("String 10")  },
	{ false, false, ZAF_ITEXT("Button 11"), ZAF_ITEXT("String 11")  },
	{ true,  false, ZAF_ITEXT("Button 12"), ZAF_ITEXT("String 12")  },
	{ false, false, ZAF_ITEXT("Button 13"), ZAF_ITEXT("String 13")  },
	{ false, false, ZAF_ITEXT("Button 14"), ZAF_ITEXT("String 14")  },
	{ false, false, ZAF_ITEXT("Button 15"), ZAF_ITEXT("String 15")  },
	{ false, false, ZAF_ITEXT(""), ZAF_ITEXT("")  }
};

ZafClassNameChar ZAF_FARDATA RowHeader::className[] = ZAF_ITEXT("RowHeader");

RowHeader::RowHeader(const ZafIChar *name, ZafObjectPersistence &persist) :
	ZafTableHeader(name, persist)
{
	ZafString *string = new ZafString(0, 0, 5, ZAF_ITEXT(""), -1);
	SetVirtualField(string);
}

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

ZafClassNameChar ZAF_FARDATA ButtonRecord::className[] = ZAF_ITEXT("ButtonRecord");

ButtonRecord::ButtonRecord(const ButtonRecord &copy) : ZafTableRecord(copy)
{
	// Setup the data field pointers for access.
	buttonField = DynamicPtrCast(GetObject("ButtonField"), ZafButton);
	stringField = DynamicPtrCast(GetObject("StringField"), ZafString);
}

ButtonRecord::ButtonRecord(const ZafIChar *name, ZafObjectPersistence &persist) :
	ZafTableRecord(name, persist)
{
	// Create the data fields.
	buttonField = new ZafButton(0, 0, 10, 1, ZAF_ITEXT(""));
	buttonField->SetStringID(ZAF_ITEXT("ButtonField"));
	buttonField->SetAutoSize(false);
	buttonField->SetButtonType(ZAF_CHECK_BOX);
	buttonField->SetHzJustify(ZAF_HZ_LEFT);
	buttonField->SetAllowToggling(true);

	stringField = new ZafString(10, 0, 25, new ZafStringData(""));
	stringField->SetStringID(ZAF_ITEXT("StringField"));
	stringField->SetBordered(false);

	Add(buttonField);
	Add(stringField);
}

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

ZafClassNameChar ZAF_FARDATA ButtonTable::className[] = ZAF_ITEXT("ButtonTable");

ButtonTable::ButtonTable(const ZafIChar *name, ZafObjectPersistence &persist) :
	ZafTable(name, persist)
{
	SetColumnText(0, ZAF_ITEXT("Simple Table"));
	SetColumnText(1, ZAF_ITEXT(""));
	// Set the data information.
	maxOffset = -1;
	while (buttonTable[(int)maxOffset+1].btnText[0])
		maxOffset++;
}

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

ZafError ButtonTable::ReadRecordData(ZafTableRecord &tRecord, ZafWindowObject *rowField)
{
	ButtonRecord &record = (ButtonRecord &)tRecord;
	// Set the text in the row header.
	ZafIChar recordNumText[16];
	sprintf(recordNumText, ZAF_ITEXT("%ld"), record.Offset());
	rowField->SetText(recordNumText);

	// Read the record.
	record.buttonField->SetSelected(buttonTable[(int)record.Offset()].selected);
	record.buttonField->SetText(buttonTable[(int)record.Offset()].btnText);
	record.stringField->SetText(buttonTable[(int)record.Offset()].strText);

	// Set the selected status on the record.
	record.SetSelected(buttonTable[(int)record.Offset()].recordSelected);
	rowField->SetSelected(buttonTable[(int)record.Offset()].recordSelected);

	// Return success.
	return (ZAF_ERROR_NONE);
}

ZafError ButtonTable::WriteRecordData(ZafTableRecord &tRecord, ZafWindowObject *rowField)
{
	ButtonRecord &record = (ButtonRecord &)tRecord;
	// Save the selected status of the button.
	buttonTable[(int)record.Offset()].selected = record.buttonField->Selected();
	// Save the selected status of the record.
	buttonTable[(int)record.Offset()].recordSelected = rowField->Selected();

	// Return success.
	return (ZAF_ERROR_NONE);
}

MainWindow::MainWindow(ZafIChar *name, ZafObjectPersistence &persist) :
	ZafWindow(name, persist)
{
	zafWindowManager->Center(this);
}
