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

#include <zaf.hpp>

// ----- AMovieData ---------------------------------------------------------

struct AMovieData
{
	bool selected;
	ZafIChar movie[64];
	ZafIChar director[64];
	ZafIChar producer[64];
	long copyright;
	ZafIChar stars[256];
} movieTable[] =
{
	{ false, ZAF_ITEXT("The Alamo"), ZAF_ITEXT("John Wayne"), ZAF_ITEXT("John Wayne"), 1960, ZAF_ITEXT("John Wayne, Richard Widmark, Laurence Harvey, Richard Boone") },
	{ false, ZAF_ITEXT("Angel and the Badman"), ZAF_ITEXT("James Edward Grant"), ZAF_ITEXT("John Wayne"), 1946, ZAF_ITEXT("John Wayne, Gail Russell" )},
	{ false, ZAF_ITEXT("Back To Bataan"), ZAF_ITEXT("Edward Dmytryk"), ZAF_ITEXT("Robert Fellows"), 1945, ZAF_ITEXT("John Wayne, Anthony Quinn") },
	{ false, ZAF_ITEXT("The Cowboys"), ZAF_ITEXT("Mark Rydell"), ZAF_ITEXT("Mark Rydell"), 1972, ZAF_ITEXT("John Wayne") },
	{ false, ZAF_ITEXT("Dakota"), ZAF_ITEXT("Joseph Kane"), ZAF_ITEXT("Joseph Kane"), 1945, ZAF_ITEXT("John Wayne, Vera Ralston, Walter Brennan, Ward Bond") },
	{ false, ZAF_ITEXT("Fort Apache"), ZAF_ITEXT("John Ford"), ZAF_ITEXT("John Ford & Merian C. Cooper"), 1948, ZAF_ITEXT("John Wayne, Henry Fonda, Shirley Temple, Pedro Armendariz") },
	{ false, ZAF_ITEXT("Hondo"), ZAF_ITEXT("John Farrow"), ZAF_ITEXT("Robert Fellows"), 1953, ZAF_ITEXT("John Wayne, Wrad Bond, Michael Pate, James Arness") },
	{ false, ZAF_ITEXT("The Man Who Shot Liberty Valance"), ZAF_ITEXT("John Ford"), ZAF_ITEXT("Willis Goldbeck"), 1962, ZAF_ITEXT("James Stewart, John Wayne") },
	{ false, ZAF_ITEXT("McLintock!"), ZAF_ITEXT("Andrew V. McLaglen"), ZAF_ITEXT("Michael Wayne"), 1963, ZAF_ITEXT("John Wayne, Maureen O'Hara") },
	{ false, ZAF_ITEXT("Reap the Wild Wind"), ZAF_ITEXT("Cecil B. DeMille"), ZAF_ITEXT("Cecil B. DeMille"), 1962, ZAF_ITEXT("John Wayne, Susan Hayward, Ray Milland, Paulette Goddard") },
	{ false, ZAF_ITEXT("Red River"), ZAF_ITEXT("Howard Hanks"), ZAF_ITEXT("Howard Hanks"), 1948, ZAF_ITEXT("John Wayne, Montgomery Clift, Walter Brennan, Joanne Dru") },
	{ false, ZAF_ITEXT("Rio Bravo"), ZAF_ITEXT("Howard Hanks"), ZAF_ITEXT("Howard Hanks"), 1958, ZAF_ITEXT("John Wayne, Dean Martin, Ricky Nelson") },
	{ false, ZAF_ITEXT("Sands of Iwo Jima"), ZAF_ITEXT("Allan Dwan"), ZAF_ITEXT("Merian C. Cooper"), 1949, ZAF_ITEXT("John Wayne, John Agar, Forrest Tucker") },
	{ false, ZAF_ITEXT("The Shootist"), ZAF_ITEXT("Don Siegel"), ZAF_ITEXT("M. J. Frankovich"), 1976, ZAF_ITEXT("John Wayne, Lauren Bacall") },
	{ false, ZAF_ITEXT("Stagecoach"), ZAF_ITEXT("John Ford"), ZAF_ITEXT("Walter Wanger"), 1939, ZAF_ITEXT("Claire Trevor, John Wayne") },
	{ false, ZAF_ITEXT(""), ZAF_ITEXT(""), ZAF_ITEXT(""), 0, ZAF_ITEXT("") }
};

// ----- AMovieRecord -------------------------------------------------------

class AMovieRecord : public ZafTableRecord
{
public:
	AMovieRecord(void);
	virtual ZafWindowObject *Duplicate(void) { return (new AMovieRecord); }

protected:
	friend class AMovieTable;

	ZafStringData *movieData;
	ZafStringData *directorData;
	ZafStringData *producerData;
	ZafIntegerData *copyrightData;
	ZafStringData *starsData;
};

AMovieRecord::AMovieRecord(void) :
	ZafTableRecord(100, 1)
{
	// Create the data fields.
	movieData = new ZafStringData;
	ZafString *movieField = new ZafString(0, 0, 25, movieData);
	movieField->SetBordered(false);
	movieField->SetViewOnly(true);
	Add(movieField);

	directorData = new ZafStringData;
	ZafString *directorField = new ZafString(25, 0, 20, directorData);
	directorField->SetBordered(false);
	directorField->SetViewOnly(true);
	Add(directorField);

	producerData = new ZafStringData;
	ZafString *producerField = new ZafString(45, 0, 15, producerData);
	producerField->SetBordered(false);
	producerField->SetViewOnly(true);
	Add(producerField);

	copyrightData = new ZafIntegerData;
	ZafInteger *copyrightField = new ZafInteger(60, 0, 10, copyrightData);
	copyrightField->SetHzJustify(ZAF_HZ_CENTER);
	copyrightField->SetBordered(false);
	copyrightField->SetViewOnly(true);
	Add(copyrightField);

	starsData = new ZafStringData;
	ZafString *starsField = new ZafString(70, 5, 50, starsData);
	starsField->SetBordered(false);
	starsField->SetViewOnly(true);
	Add(starsField);
}

// ----- AMovieTable --------------------------------------------------------

class AMovieTable : public ZafTable
{
public:
	AMovieTable(void);
	ZafError ReadRecordData(ZafTableRecord &record, ZafWindowObject *row);
	ZafError WriteRecordData(ZafTableRecord &record, ZafWindowObject *row);
};

AMovieTable::AMovieTable(void) :
	ZafTable(0, 0, 0, 0, 5, 1, 1)
{
	// Reset some table properties and variables.
	SetRegionType(ZAF_AVAILABLE_REGION);
	SetVirtualRecord(new AMovieRecord);

	// Add the scroll-bars.
	Add(new ZafScrollBar(ZAF_CORNER_SCROLL));
	Add(new ZafScrollBar(ZAF_VERTICAL_SCROLL));
	Add(new ZafScrollBar(ZAF_HORIZONTAL_SCROLL));

	// Set the header titles.
	SetColumnText(0, ZAF_ITEXT("Movie"));
	SetColumnText(1, ZAF_ITEXT("Director"));
	SetColumnText(2, ZAF_ITEXT("Producer"));
	SetColumnText(3, ZAF_ITEXT("Copyright"));
	SetColumnText(4, ZAF_ITEXT("Stars"));

	// Set the data information.
	maxOffset = -1;
	while (movieTable[(int)maxOffset+1].movie[0])
		maxOffset++;
}

ZafError AMovieTable::ReadRecordData(ZafTableRecord &tRecord, ZafWindowObject *rowField)
{
	// Read the record.
	AMovieRecord &record = (AMovieRecord &)tRecord;
	record.movieData->SetText(movieTable[(int)record.Offset()].movie);
	record.directorData->SetText(movieTable[(int)record.Offset()].director);
	record.producerData->SetText(movieTable[(int)record.Offset()].producer);
	record.copyrightData->SetInteger(movieTable[(int)record.Offset()].copyright);
	record.starsData->SetText(movieTable[(int)record.Offset()].stars);

	rowField->SetSelected(movieTable[(int)record.Offset()].selected);
	record.SetSelected(movieTable[(int)record.Offset()].selected);

	// Return success.
	return (ZAF_ERROR_NONE);
}

ZafError AMovieTable::WriteRecordData(ZafTableRecord &tRecord, ZafWindowObject *rowField)
{
	// Only check the selection status.
	AMovieRecord &record = (AMovieRecord &)tRecord;
	movieTable[(int)record.Offset()].selected = rowField->Selected();

	// Return success.
	return (ZAF_ERROR_NONE);
}

// ----- AMovieWindow -------------------------------------------------------

class AMovieWindow : public ZafWindow
{
public:
	AMovieWindow(void);
};

AMovieWindow::AMovieWindow(void) :
	ZafWindow(0, 0, 50, 9)
{
	// Center the window and add generic objects.
	zafWindowManager->Center(this);
	AddGenericObjects(new ZafStringData("Data Window"));

	// Add the movie table.
	Add(new AMovieTable);
}

// ----- ZafApplication -----------------------------------------------------

int ZafApplication::Main(void)
{
	LinkMain();

	zafWindowManager->Add(new AMovieWindow);

	Control();

	return (0);
}

