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

#include <z_string.hpp>
#include "m_win.hpp"

// ----- MtfWindowFrame widget definition -----------------------------------

XmOffsetPtr MtfWindowFrame::offsets;

XtActionsRec MtfWindowFrame::action[] =
{
	{ "ZafHelp", 	(XtActionProc)help }
};

char MtfWindowFrame::translations[] =
	"<Key>F1:		ZafHelp()\n\
	 <Key>osfHelp:	ZafHelp()";

MtfWindowFrameClassRec MtfWindowFrame::classRecord =
{
	// --- CoreClassPart ---
	{
		(WidgetClass)&xmBulletinBoardClassRec,	// superclass
		"ZafWindowFrame",						// class_name
		sizeof(MtfWindowFramePart), 			// widget_size (data part)
		MtfWindowFrame::classInitialize,		// class_initialize
		0,										// class_part_initialize
		false,									// class inited
		(XtInitProc)initialize,					// initialize
		0,										// initialize_hook
		XtInheritRealize,						// realize
		action,									// actions
		XtNumber(action),						// num_actions
		0, 										// resources
		0, 										// num_resources
		NULLQUARK,								// xrm
		true,									// compress_motion
		XtExposeCompressMultiple,				// compress_exposure
		true,									// compress_enterleave
		false,									// visible_interest
		0,										// destroy
		(XtWidgetProc)resize,					// resize
		(XtExposeProc)expose,					// expose
		0,										// set_values
		0, 										// set_values_hook
		XtInheritSetValuesAlmost,				// set_values_almost
		0, 										// get_values_hook
		XtInheritAcceptFocus,					// accept_focus
		XtVersionDontCheck,					// version
		0, 										// callback_private
		XtInheritTranslations,					// tm_table
		XtInheritQueryGeometry, 				// query_geometry
		XtInheritDisplayAccelerator,			// display_accelerator
		0 										// extension
	},
	// --- CompositeClassPart ---
	{
		XtInheritGeometryManager,				// geometry_manager
		XtInheritChangeManaged,					// change_managed
		XtInheritInsertChild,					// insert_child
		XtInheritDeleteChild,					// delete_child
		0										// extension
	},
	// --- ConstraintClassPart ---
	{
		0,										// resources
		0,										// num_resources
		0,										// constraint_size
		0,										// initialize
		0,										// destroy
		0,										// set_values
		0										// extension
	},
	// --- XmManagerClassPart ---
	{
		XtInheritTranslations,					// translations
		0,										// syn_resources
		0,										// num_syn_resources
		0,										// syn_constraint_resources
		0,										// num_syn_constraint_resources
		XmInheritParentProcess,					// parent_process
		0										// extension
	},
	// --- XmBulletinBoardClassPart ---
	{
		false,									// always_install_accelerators
		0,										// geo_matrix_create
		XmInheritFocusMovedProc,				// focus_moved_proc
		0										// extension
	},
	// --- MtfWindowFrameClassPart ---
	{
		0									// extension
	}
};

void MtfWindowFrame::classInitialize()
{
	XmResolvePartOffsets((WidgetClass) &(MtfWindowFrame::classRecord),
	 &(MtfWindowFrame::offsets));
}

void MtfWindowFrame::expose(MtfWindowFrame *mtfObject, XEvent *xEvent, Region xRegion)
{
	// Draw the shadow.
	ZafWindow *zafObject = GetZincObject(mtfObject);
	ZafRegionStruct drawRegion = zafObject->clientRegion;
	drawRegion += mtfObject->manager.shadow_thickness;
	if (zafObject->Bordered())
		_XmDrawShadows(XtDisplay(mtfObject), XtWindow(mtfObject),
			mtfObject->manager.top_shadow_GC, mtfObject->manager.bottom_shadow_GC,
			drawRegion.left, drawRegion.top, drawRegion.Width(), drawRegion.Height(),
//			mtfObject->manager.shadow_thickness, mtfObject->shadow_type);
			mtfObject->manager.shadow_thickness, Shadow_Type((Widget)mtfObject));

	// Redraw any system support objects that have the same screenID
	// as the frame.
	ZafEventStruct event(E_MOTIF, xEvent);
	XRectangle xRect;
	XClipBox(xRegion, &xRect);
  	event.region.Assign(xRect.x, xRect.y, xRect.width, xRect.height, ZAF_PIXEL);
	for (ZafWindowObject *object = zafObject->SupportFirst(); object; object = object->Next())
		if (object->screenID == (Widget)mtfObject && object->SystemObject())
			object->Draw(event, E_MOTIF);
}

void MtfWindowFrame::initialize(MtfWindowFrame *, MtfWindowFrame *init, ArgList , Cardinal *)
{
	// Fix initialization problems.
	init->manager.has_focus = false;
}

void MtfWindowFrame::resize(MtfWindowFrame *mtfObject)
{
	// Compute the support region sizes.
	ZafWindow *window = GetZincObject(mtfObject);
	for (ZafWindowObject *object = window->SupportFirst(); object; object = object->Next())
		if (object->SystemObject())
		{
			// Compute and set the desired size.
			object->Event(S_COMPUTE_SIZE);
			object->OSSize();
		}
	window->clientRegion = window->MaxRegion(window);

	// Determine the shadow.
	if (window->Bordered())
		window->clientRegion -= mtfObject->manager.shadow_thickness;

	// Move the client area.
	Widget frameID = window->OSScreenID(ZAF_FRAMEID);
	Widget clientID = window->OSScreenID(ZAF_CLIENTID);
	if (clientID == (Widget)mtfObject)
		return;
	else if (XtParent(clientID) != frameID) // special scrolled window.
		clientID = XtParent(clientID);

	if (window->clientRegion.Width() != clientID->core.width ||
		window->clientRegion.Height() != clientID->core.height)
		XtConfigureWidget(clientID, window->clientRegion.left,
			window->clientRegion.top, window->clientRegion.Width(),
			window->clientRegion.Height(), 0);
	else if (window->clientRegion.left != clientID->core.x ||
		window->clientRegion.top != clientID->core.y)
		XtMoveWidget(clientID, window->clientRegion.left,
			window->clientRegion.top);
}

void MtfWindowFrame::OverrideTranslations(void)
{
	XtTranslations translationTable = XtParseTranslationTable(translations);
	XtOverrideTranslations((Widget)this, translationTable);
}

