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

#include "utime.hpp"

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

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

	// Create a ZafUTimeData to be shared by all ZafUTimes.
	ZafUTimeData *uTimeData = new ZafUTimeData();

	// Get a pointer to each ZafUTime and set its ZafUTimeData to be 
	// the one just created.
	editUTime = DynamicPtrCast(GetObject(ZAF_ITEXT("EnterUTimeData")), ZafUTime);
	editUTime->SetUTimeData(uTimeData);
	
	// Get a pointer to the list window.  This pointer can then be used in a
	// for loop to find all of ZafUTime objects.
	ZafScrolledWindow *listWindow = DynamicPtrCast(GetObject(ZAF_ITEXT("ListWindow")), 
		ZafScrolledWindow);
		
	// Use a for loop to find all of the ZafUtime objects.
	ZafUTime *uTime;
	for (ZafWindowObject *object = listWindow->First(); object; 
		object = object->Next())
	{
		// If the object is a ZafUtime set its ZafUTimeData to be the
		// same as all other ZafUtime objects.
	 	uTime = DynamicPtrCast(object, ZafUTime);
		if (uTime)
			uTime->SetUTimeData(uTimeData);
	}

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

	outputFormat = DynamicPtrCast(GetObject(ZAF_ITEXT("OutputFormat")), ZafString);
	outputFormat->SetUserFunction(FormatUTime);
	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 editUTime has focuse set userStatus to 1.
		bool saveFocus = editUTime->Focus();
		
		// Get the next object in the list following the editUTime so
		// that when it is added back to the window it will remain in the
		// same tab order.
		ZafWindowObject *nextObj = editUTime->Next();

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

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

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

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

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

ZafEventType FormatWindow::FormatUTime(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);
}
