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

#include <z_stdlib.hpp>
#include <z_title.hpp>
#include "m_utils.hpp"

extern "C"
{
#include _MwmUtil_
}

const int ZAF_MDI_BUTTON_SIZE = 22;

// ----- ZafTitle -----------------------------------------------------------

#if defined(ZAF_DEFAULT_DRAW)
ZafEventType ZafTitle::Draw(const ZafEventStruct &, ZafEventType ccode)
{
	// Check for a valid sub-window.
	if (!screenID || !SystemObject())
		return (S_ERROR);

	// Compute the actual draw region.
	ZafPaletteState state = PaletteState();
	ZafRegionStruct drawRegion = BeginDraw();

	// Draw button shadow and fill region.
	DrawShadow(drawRegion, depth, ccode);

	display->SetPalette(LogicalPalette(ZAF_PM_BACKGROUND, state));
	display->Rectangle(drawRegion, 0, true);

	// Draw the title information.
	display->SetPalette(LogicalPalette(ZAF_PM_TEXT, state));
	display->Text(drawRegion, stringData->Text(), -1, hzJustify, vtJustify, hotKeyIndex);

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

ZafEventType ZafTitle::Event(const ZafEventStruct &event)
{
	// Check for special sub-window code.
	ZafEventType ccode = event.type;
	if (SystemObject())
	{
		switch (ccode)
		{
		case S_COMPUTE_SIZE:
			// This widget will be in the motif frame, not in the client area.
			ZafWindowObject::Event(event);
			zafRegion.bottom = zafRegion.top + ZAF_MDI_BUTTON_SIZE - 1;
			depth = 1;
			break;

		// --- Movement ---
		case L_BEGIN_SELECT:
			if (!parent->Focus())
				parent->SetFocus(true);
			parent->Modify(event);
			break;

		case L_CONTINUE_SELECT:
		case L_END_SELECT:
			break;

		// ----- Unknown or error messages -----
		default:
			ccode = ZafButton::Event(event);
			break;
		}

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

	// Check for zinc events.
	switch (ccode)
	{
	// ----- Create messages -----
	case S_INITIALIZE:
		// Set the parent decorations.
		SetSystemObject(false);
		DynamicPtrCast(parent, ZafWindow)->SetDecoration(MWM_DECOR_TITLE);
		break;

	case S_DEINITIALIZE:
		// Clear the parent decorations.
		DynamicPtrCast(parent, ZafWindow)->SetDecoration(MWM_DECOR_TITLE);
		break;

	case S_COMPUTE_SIZE:
		// This widget will be in the motif frame, not in the client area.
		zafRegion.Assign(-1, -1, -1, -1, ZAF_PIXEL);
		break;

	case S_REDISPLAY:
	case S_REDISPLAY_DATA:
	case S_REGISTER_OBJECT:
		// Set the parent's title.
		OSSetText();
		break;

	// ----- Motif or unknown messages -----
	case E_MOTIF:
		ccode = ZafWindowObject::Event(event);
		break;

	default:
		return (S_UNKNOWN);
	}

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

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

ZafError ZafTitle::OSSetText(void)
{
	// Only reset the title when it is a main-window title.
	if (SystemObject() || (!parent || parent->parent))
		return (ZAF_ERROR_INVALID);

	// Set the parent title.
	Widget shell = parent ? parent->OSScreenID(ZAF_SHELLID) : 0;
	if (shell && stringData)
	{
		char *osText = stringData->DynamicOSText();
#		if defined(ZIL_UNICODE)
		XtVaSetValues(shell,
			XtNtitle, osText,
			0);
#		else
		XtVaSetValues(shell,
			XtNtitleEncoding, XA_STRING,
			XtNtitle, osText,
			0);
#		endif
		delete []osText;
	}

	return (ZAF_ERROR_NONE);
}

void ZafTitle::OSRegisterObject(void)
{
	// Only sub-window titles needs to be registered.
	if (SystemObject())
	{
		ZafButton::OSRegisterObject();
		XtVaSetValues(screenID, XmNhighlightThickness, 0, 0);
	}
}

void ZafTitle::OSSize(void)
{
	// Only sub-window titles needs to be sized.
	if (SystemObject())
		ZafButton::OSSize();
}

ZafError ZafTitle::OSUpdatePalettes(ZafPaletteType zafTypes, ZafPaletteType osTypes)
{
	// Skip the button level.
	return (ZafWindowObject::OSUpdatePalettes(zafTypes, osTypes));
}

