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

#include <zaf.hpp>

// ----- MovieData ----------------------------------------------------------

struct MovieData
{
	ZafIChar movie[64];
	ZafIChar director[64];
	ZafIChar producer[64];
	long copyright;
	ZafIChar stars[256];
} movieTable[] =
{
	{ ZAF_ITEXT("The Alamo"), ZAF_ITEXT("John Wayne"), ZAF_ITEXT("John Wayne"), 1960, ZAF_ITEXT("John Wayne, Richard Widmark, Laurence Harvey, Richard Boone") },
	{ ZAF_ITEXT("Angel and the Badman"), ZAF_ITEXT("James Edward Grant"), ZAF_ITEXT("John Wayne"), 1946, ZAF_ITEXT("John Wayne, Gail Russell") },
	{ ZAF_ITEXT("Hondo"), ZAF_ITEXT("John Farrow"), ZAF_ITEXT( "Robert Fellows"), 1953, ZAF_ITEXT("John Wayne, Ward Bond, Michael Pate, James Arness") },
	{ ZAF_ITEXT("Back To Bataan"), ZAF_ITEXT("Edward Dmytryk"), ZAF_ITEXT("Robert Fellows"), 1945, ZAF_ITEXT("John Wayne, Anthony Quinn") },
	{ ZAF_ITEXT("The Cowboys"), ZAF_ITEXT("Mark Rydell"), ZAF_ITEXT("Mark Rydell"), 1972, ZAF_ITEXT("John Wayne") },
	{ 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") },
	{ ZAF_ITEXT(""), ZAF_ITEXT(""), ZAF_ITEXT(""), 0, ZAF_ITEXT("") }
};

// ----- MovieWindow --------------------------------------------------------

const ZafEventType NEXT_RECORD 		= 10000;
const ZafEventType PREVIOUS_RECORD 	= 10001;

class MovieWindow : public ZafWindow
{
public:
	MovieWindow(void);
	ZafEventType Event(const ZafEventStruct &event);

protected:
	ZafStringData *movieData;
	ZafStringData *directorData;
	ZafStringData *producerData;
	ZafIntegerData *copyrightData;
	ZafStringData *starsData;

	int curOffset, minOffset, maxOffset;
	ZafError ReadRecord(int offset);
	void ReadNextRecord(void) { ReadRecord(curOffset+1); }
	void ReadPreviousRecord(void) { ReadRecord(curOffset-1); }
};

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

	// Create the data fields.
	movieData = new ZafStringData;
	directorData = new ZafStringData;
	producerData = new ZafStringData;
	copyrightData = new ZafIntegerData;
	starsData = new ZafStringData;

	// Add the data fields.
	Add(new ZafPrompt(2, 1, 10, ZAF_ITEXT("Movie:")));
	Add(new ZafString(12, 1, 35, movieData));

	Add(new ZafPrompt(2, 2, 10, ZAF_ITEXT("Director:")));
	Add(new ZafString(12, 2, 35, directorData));

	Add(new ZafPrompt(2, 3, 10, ZAF_ITEXT("Producer:")));
	Add(new ZafString(12, 3, 35, producerData));

	Add(new ZafPrompt(2, 4, 10, ZAF_ITEXT("Copyright:")));
	Add(new ZafInteger(12, 4, 35, copyrightData));

	Add(new ZafPrompt(2, 5, 10, ZAF_ITEXT("Stars:")));
	Add(new ZafString(12, 5, 35, starsData));

	// Add movement buttons.
	ZafButton *previousButton = new ZafButton(15, 7, 10, 1, ZAF_ITEXT("Previous"));
	previousButton->SetSendMessageWhenSelected(true);
	previousButton->SetValue(PREVIOUS_RECORD);
	previousButton->SetHotKey('P', 0);
	Add(previousButton);

	ZafButton *nextButton = new ZafButton(35, 7, 10, 1, ZAF_ITEXT("Next"));
	nextButton->SetSendMessageWhenSelected(true);
	nextButton->SetValue(NEXT_RECORD);
	nextButton->SetHotKey('N', 0);
	Add(nextButton);

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

ZafEventType MovieWindow::Event(const ZafEventStruct &event)
{
	// Switch on the event type.
	ZafEventType ccode = event.type;
	switch (ccode)
	{
	case NEXT_RECORD:
		ReadNextRecord();
		break;

	case PREVIOUS_RECORD:
		ReadPreviousRecord();
		break;

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

	// Return the control code.
	return (ccode);
}

ZafError MovieWindow::ReadRecord(int offset)
{
	// Check for out-of-range.
	if (offset < minOffset || offset > maxOffset)
	{
		ZafErrorStub::Beep();
		return (ZAF_ERROR_OUT_OF_RANGE);
	}

	// Load the record.
	curOffset = offset;
	movieData->SetText(movieTable[offset].movie);
	directorData->SetText(movieTable[offset].director);
	producerData->SetText(movieTable[offset].producer);
	copyrightData->SetInteger(movieTable[offset].copyright);
	starsData->SetText(movieTable[offset].stars);

	// Return success.
	return (ZAF_ERROR_NONE);
}

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

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

	zafWindowManager->Add(new MovieWindow);

	Control();

	return (0);
}

