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

#include "date.hpp"

// ----- FormatWindow -------------------------------------------------------

FormatWindow::FormatWindow(ZafIChar *name) :
	ZafWindow(name, *zafObjectPersistence)
{
	// Check for read errors.
	if (Error())
		return;

	// Create a ZafDateData to be shared by all ZafDates.
	ZafDateData *dateData = new ZafDateData();

	// Get a pointer to each ZafDate and set its ZafDateData to be 
	// the one just created.
	editDate = DynamicPtrCast(GetObject(ZAF_ITEXT("EnterDate")), ZafDate);
	editDate->SetDateData(dateData);
	
	// Get a pointer to the list window.  This pointer can then be used in a
	// for loop to find all of ZafDate objects.
	ZafScrolledWindow *listWindow = DynamicPtrCast(GetObject(ZAF_ITEXT("ListWindow")), 
		ZafScrolledWindow);
	
	// Use a for loop to find all of the ZafDate objects.
	// Create a ZafDate pointer.
	ZafDate *date;
	for (ZafWindowObject *object = listWindow->First(); object; object = object->Next())
	{
		// If the object is a ZafDate set its ZafDateData to be the
		// same as all other ZafUtime objects.
	 	date = DynamicPtrCast(object, ZafDate);
		if (date)
			date->SetDateData(dateData);
	}

	inputFormat = DynamicPtrCast(GetObject(ZAF_ITEXT("InputFormat")), ZafString);
	inputFormat->SetUserFunction(FormatDate);

	outputFormat = DynamicPtrCast(GetObject(ZAF_ITEXT("OutputFormat")), ZafString);
	outputFormat->SetUserFunction(FormatDate);
	windowManager->Center(this);
}

ZafEventType FormatWindow::Event(const ZafEventStruct &event)
{
	ZafEventType ccode = event.type;
	switch(ccode)
	{
	case CHANGE_INPUT_FORMAT:
	case CHANGE_OUTPUT_FORMAT:
		{
		if (ccode == CHANGE_OUTPUT_FORMAT && strlen(outputFormat->Text()) < 2)
			break;
		// If editDate has focuse set userStatus to 1.
		bool saveFocus = editDate->Focus();
		
		// Get the next object in the list following the editDate so
		// that when it is added back to the window it will remain in the
		// same tab order.
		ZafWindowObject *nextObj = editDate->Next();

		// Subtract the editDate from the window so that its input or 
		// output format can be changed.
		Subtract(editDate);
		if (ccode == CHANGE_OUTPUT_FORMAT)
		{
			// Clear the Changed attribute, get the new format and set it.
			outputFormat->SetChanged(false);
			editDate->SetOutputFormat(outputFormat->Text());
			editDate->SetInputFormat(inputFormat->Text());
		}
		else
		{
			// Clear the Changed attribute, get the new format and set it.
			inputFormat->SetChanged(false);
			editDate->SetInputFormat(inputFormat->Text());
		}

		// Add the editDate back to the window specifying its position.
		Add(editDate, nextObj);

		// If the editDate had focus give it focus and clear userStatus.
		if (saveFocus)
			editDate->SetFocus(true);
		}
		break;

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

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

ZafEventType FormatWindow::FormatDate(ZafWindowObject *object,
	ZafEventStruct &, ZafEventType ccode)
{
	// If a format string has been selected or is going non currect and
	// its contents has changed, determine which format string it is and
	// place the correct message on the queue.
	if ((ccode == N_NON_CURRENT || ccode == L_SELECT) && object->Changed())
	{
		if (!strcmp(object->StringID(), ZAF_ITEXT("OutputFormat")))
			object->eventManager->Put(CHANGE_OUTPUT_FORMAT);
		else
			object->eventManager->Put(CHANGE_INPUT_FORMAT);
	}

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