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

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

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

void ZafTable::OSMapPalette(ZafPaletteStruct &palette, ZafPaletteType type, ZafPaletteState state)
{
	// Check for a non-system object.
	if (!screenID)
		return;

	// Determine the proper palette.
	XGCValues values;
	XtGCMask mask = GCForeground | GCBackground;
	XmPrimitiveRec *widget = (XmPrimitiveRec *)screenID;
	if (type == ZAF_PM_FOCUS || type == ZAF_PM_OUTLINE ||
		type == ZAF_PM_LIGHT_SHADOW || type == ZAF_PM_DARK_SHADOW)
	{
		ZafWindowObject::OSMapPalette(palette, type, state);
		return;
	}
	else if (type == ZAF_PM_TEXT || type == ZAF_PM_HOT_KEY)
	{
		mask |= GCFillStyle | GCFont;
		// colors
		values.background = widget->core.background_pixel;
		values.foreground = widget->primitive.foreground;
		// pattern
		ZafWindowObject *object = Current() ? Current()->Current() : ZAF_NULLP(ZafWindowObject);
		if (state & ZAF_PM_DISABLED)
		{
			mask |= GCTile;
			values.fill_style = FillTiled;
			values.tile = XmGetPixmapByDepth(XtScreen(screenID), "50_foreground",
				values.foreground, values.background, widget->core.depth);
		}
		else if ((state & ZAF_PM_CURRENT) && object && !object->SystemObject())
		{
			values.foreground = values.background;
			values.fill_style = FillSolid;
		}
		else
		{
			values.background = values.foreground;
			values.fill_style = FillSolid;
		}
		// font
		XmFontList mtfFont = 0;
		if (palette.font >= 0)
			mtfFont = ((ZafScreenDisplay *)display)->fontTable[palette.font];
		else
			XtVaGetValues(screenID, "fontList", &mtfFont, 0);
		if (!mtfFont)
			mtfFont = ((ZafScreenDisplay *)display)->fontTable[ZAF_FNT_DIALOG];
		values.font = ((ZafScreenDisplay *)display)->MtfGetFont(mtfFont)->fid;
	}
	else
	{
		// colors
		ZafWindowObject *object = Current() ? Current()->Current() : ZAF_NULLP(ZafWindowObject);
		if ((state & ZAF_PM_CURRENT) && object && !object->SystemObject() && getChildPalette)
		{
			values.foreground = widget->primitive.foreground;
			values.background = values.foreground;
		}
		else
		{
			values.background = widget->core.background_pixel;
			values.foreground = values.background;
		}
		getChildPalette = false;
		if (widget->core.background_pixmap != XmUNSPECIFIED_PIXMAP)
		{
   			mask |= GCFillStyle | GCTile;
   			values.fill_style = FillTiled;
   			values.tile = widget->core.background_pixmap;
		}
	}
	palette.osPalette = XtGetGC(screenID, mask, &values);
}

void ZafTable::OSRegisterObject(void)
{
	// Defer to the base window class.
	ZafWindow::OSRegisterObject();

	// Set additional default values.
	MtfWindowFrame *mtfFrame = MtfWindowFrame::GetMotifWidget(XtParent(screenID));
	mtfFrame->manager.shadow_thickness = 1;

	// Level 3 - Allows us to obtain input focus.
	char *name = zafCodeSet->ConvertToOSString(stringID, 0, false);
	screenID = XtVaCreateManagedWidget(name,
		(WidgetClass)&MtfWindowObject::classRecord, screenID,
		// user information
		XmNuserData, this,
		// miscellaneous
		XmNx, 0,
		XmNy, 0,
		XmNsensitive, Disabled(false) ? false : true,
		XmNtraversalOn, Noncurrent() ? false : true,
		0);

	// Override translations.
	XtTranslations translationTable = XtParseTranslationTable(MtfWindowObject::translations);
	XtOverrideTranslations(screenID, translationTable);
}

OSWindowID ZafTable::OSScreenID(ZafScreenIDType type) const
{
	// Determine the proper widget.
	if (!screenID)
		return (screenID);
	else if (type == ZAF_CLIENTID)
		return (XtParent(screenID));
	else if (type == ZAF_FRAMEID)
		return (XtParent(XtParent(screenID)));
	return (ZafWindow::OSScreenID(type));
}

