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

#include <z_min.hpp>
#include "m_utils.hpp"

extern "C"
{
#include _MwmUtil_
}

const int ZAF_MDI_BUTTON_SIZE = 22;

// ----- ZafMinimizeButton --------------------------------------------------

#if defined(ZAF_DEFAULT_DRAW)
ZafEventType ZafMinimizeButton::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 border.
	DrawShadow(drawRegion, Depressed() ? -depth : depth, ccode);

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

	const int ZAF_MDI_BOX_SIZE = 6;
	drawRegion.left += (drawRegion.Width() - ZAF_MDI_BOX_SIZE) / 2;
	drawRegion.top += (drawRegion.Height() - ZAF_MDI_BOX_SIZE) / 2;
	drawRegion.right = drawRegion.left + ZAF_MDI_BOX_SIZE - 1;
	drawRegion.bottom = drawRegion.top + ZAF_MDI_BOX_SIZE - 1;
	DrawShadow(drawRegion, DynamicPtrCast(parent, ZafWindow)->Minimized() ? -1 : 1, ccode);

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

ZafEventType ZafMinimizeButton::Event(const ZafEventStruct &event)
{
	// Check for special sub-window code.
	ZafEventType ccode = event.type;
	if (SystemObject())
	{
		switch (ccode)
		{
		case S_INITIALIZE:
			ccode = ZafButton::Event(event);
			ZafButton::SetSendMessageWhenSelected(true);
			ZafButton::SetValue(S_MDI_MINIMIZE);
			break;

		case S_COMPUTE_SIZE:
			// This widget will be in the motif frame, not in the client area.
			ZafWindowObject::Event(event);
			oldRegion.Assign(-1, -1, -1, -1, ZAF_PIXEL);
			zafRegion.bottom = zafRegion.top + ZAF_MDI_BUTTON_SIZE - 1;
			if (DynamicPtrCast(parent, ZafWindow)->Maximized())
				zafRegion.left = zafRegion.right = -1;
			else
				zafRegion.left = zafRegion.right - ZAF_MDI_BUTTON_SIZE + 1;
			depth = 1;
			break;

		case L_BEGIN_SELECT:
			if (!parent->Focus())
				parent->SetFocus(true);
			ccode = ZafButton::Event(event);
			break;

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

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

	// Check for zinc events.
	switch (ccode)
	{
	case S_INITIALIZE:
		// Set the parent decorations.
		if (parent && !parent->parent)
			DynamicPtrCast(parent, ZafWindow)->SetDecoration(MWM_DECOR_MINIMIZE);
		break;

	case S_DEINITIALIZE:
		// Clear the parent decorations.
		if (parent && !parent->parent)
			DynamicPtrCast(parent, ZafWindow)->ClearDecoration(MWM_DECOR_MINIMIZE);
		break;

	case S_COMPUTE_SIZE:
		// This object is a decoration, and will not be in the client area.
		zafRegion.Assign(-1, -1, -1, -1, ZAF_PIXEL);
		break;

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

	default:
		ccode = S_UNKNOWN;
		break;
	}

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

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

void ZafMinimizeButton::OSRegisterObject(void)
{
	// Only MDI minimize button is not registered.
	if (SystemObject())
	{
		ZafButton::OSRegisterObject();
		XtVaSetValues(screenID, XmNhighlightThickness, 0, 0);
	}
}

void ZafMinimizeButton::OSSize(void)
{
	// Only MDI minimize button needs to be sized.
	if (SystemObject())
		ZafButton::OSSize();
}

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