//	Zinc Application Framework - D_TABLE2.CPP
//	COPYRIGHT (C) 1990-1999.  All Rights Reserved.
//	Zinc Software Incorporated.  Pleasant Grove, Utah  USA

#include <z_table.hpp>

// ----- OS Specific Functions ----------------------------------------------

ZafEventType ZafTableRecord::DispatchMouseEvent(const ZafEventStruct &event)
{
	ZafEventType ccode = LogicalEvent(event);

	if (modifyMode)
		return (Modify(event));

	if (windowManager->MouseEventRoute() == this)
	{
		ccode = Event(event);
		return (ccode);
	}

	// Convert mouse position to the coordinates of this object.
	bool overClient = false;
	ZafPositionStruct mousePosition = event.position;
	ZafRegionStruct windowClient = ClientRegion();
	if (windowClient.Overlap(mousePosition))
	{
		// If the mouse is over the client region, convert to the client
		// region's coordinates.
		overClient = true;
		mousePosition.column -= windowClient.left;
		mousePosition.line -= windowClient.top;
		// Cast away the constness so we can have event.position relative
		// to the client region.
		((ZafEventStruct &)event).position = mousePosition;
	}

	if (ccode == L_BEGIN_SELECT && overClient)
	{
		// Try to find an overlapping object.
		ZafWindowObject *object;
		for (object = First(); object; object = object->Next())
			if (object->zafRegion.Overlap(mousePosition))
			{
				if (object->Disabled() || object->Noncurrent())
					object = ZAF_NULLP(ZafWindowObject);
				else
				{
					// Activate the object and let it process the event.
					if (!object->SystemObject())
						ActivateObject(object);
					object->SetFocus(true);
					ccode = object->Event(event);
					Table()->ScrollEvent(S_HSCROLL_CHECK);
				}
				break;
			}

		// See if the record gets focus.
		if (!object && !Focus() && !Disabled() && !Noncurrent())
			SetFocus(true);
	}
	else
		ccode = ZafWindow::DispatchMouseEvent(event);

	return (ccode);
}

ZafWindowObject *ZafTableRecord::FindMouseObject(const ZafEventStruct &event)
{
	return (ZafWindowObject::FindMouseObject(event));
}
