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

#include <z_stdlib.hpp>
#include <z_string.hpp>
#include "m_plldn.hpp"

// ----- MtfPullDownItem widget definition ----------------------------------

static void activate(Widget , ZafPullDownItem *object, XmAnyCallbackStruct *callback)
{
	ZafEventStruct event(L_SELECT);
	event.osEvent = *callback->event;
	object->Event(event);
}

// ----- ZafPullDownItem ----------------------------------------------------

#if defined(ZAF_DEFAULT_DRAW)
ZafEventType ZafPullDownItem::Draw(const ZafEventStruct &, ZafEventType ccode)
{
	// Return the control code.
	return (ccode);
}
#endif

ZafEventType ZafPullDownItem::Event(const ZafEventStruct &event)
{
	// Check for zinc events.
	ZafEventType ccode = event.type;
	switch (ccode)
	{
	// ----- Create messages -----
	case S_INITIALIZE:
		// This widget will be in the motif frame, not in the client area.
		ccode = ZafButton::Event(event);
		menu.Event(event);
		regionType = ZAF_AVAILABLE_REGION;
		zafRegion.left = zafRegion.top = zafRegion.right = zafRegion.bottom = -1;
		break;

	case S_DEINITIALIZE:
		ccode = ZafButton::Event(event);
		menu.Event(event);
		break;

	case S_REGISTER_OBJECT:
		// Create the support menu.
		RegisterObject();
		if (menu.First())
			menu.Event(event);
		break;

	// ----- Actions -----
	case L_SELECT:
		// Toggle the button.
		if (AllowToggling())
			ToggleSelected();

		// Call the associated user function.
		ccode = (this->*memberUserFunction)(event, ccode);
		break;

	case S_ADD_OBJECT:
		Add(event.windowObject);
		break;

	case S_SUBTRACT_OBJECT:
		Subtract(event.windowObject);
		break;

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

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

bool ZafPullDownItem::SetDisabled(bool setDisabled)
{
	// Defer to the base class.
	return (ZafWindowObject::SetDisabled(setDisabled));
}

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

ZafEventType ZafPullDownItem::OSEvent(const ZafEventStruct &)
{
	return (S_ERROR);
}

void ZafPullDownItem::OSMapPalette(ZafPaletteStruct &palette, ZafPaletteType type, ZafPaletteState state)
{
	// Defer to the top-most base class.
	ZafWindowObject::OSMapPalette(palette, type, state);
}

void ZafPullDownItem::OSRegisterObject(void)
{
	// Prepare the label.
	char *osText = stringData->DynamicOSText();
#	if defined(ZAF_UNICODE)
	XmString label = XmStringCreateLocalized(osText);
#	else
	XmString label = XmStringCreate(osText, XmFONTLIST_DEFAULT_TAG);
#	endif

	// Create the Motif widget.
	char *name = zafCodeSet->ConvertToOSString(stringID, 0, false);
	screenID = XtVaCreateManagedWidget(name,
		xmCascadeButtonWidgetClass, parent->OSScreenID(ZAF_CLIENTID),
		// user information
		XmNuserData, this,
		// miscellaneous
		XmNlabelString, label,
		XmNsensitive, disabled ? false : true,
		0);

	// Determine the hot key.
	if (hotKeyIndex >= 0)
	{
		KeySym keySym = ZafKeySym(stringData->Char(hotKeyIndex));
		if (keySym != NoSymbol)
			XtVaSetValues(screenID, XmNmnemonic, keySym, 0);
	}

	// Free the label memory.
	XmStringFree(label);
	delete []osText;

	// Override the action callbacks.
	XtAddCallback(screenID, XmNactivateCallback,
		(XtCallbackProc)activate, (XtPointer)this);
}

ZafError ZafPullDownItem::OSSetText(void)
{
	// Reset the system value.
	if (screenID && SystemObject())
	{
		// Set the label.
		char *osText = stringData->DynamicOSText();
#		if defined(ZAF_UNICODE)
		XmString label = XmStringCreateLocalized(osText);
#		else
		XmString label = XmStringCreate(osText, XmFONTLIST_DEFAULT_TAG);
#		endif
		XtVaSetValues(screenID, XmNlabelString, label, 0);
		XmStringFree(label);
		delete []osText;

		// Determine the hot key.
		if (hotKeyIndex >= 0)
		{
			KeySym keySym = ZafKeySym(stringData->Char(hotKeyIndex));
			if (keySym != NoSymbol)
				XtVaSetValues(screenID, XmNmnemonic, keySym, 0);
		}
	}
	else if (screenID)
		RedisplayData();
	return (ZAF_ERROR_NONE);
}

void ZafPullDownItem::OSSize(void)
{
	// Menu item does not need to be sized.
}

ZafError ZafPullDownItem::OSUpdatePalettes(ZafPaletteType zafTypes, 
 ZafPaletteType osTypes)
{
	// Defer to the top-most base class.
	ZafError tError = ZafWindowObject::OSUpdatePalettes(zafTypes, osTypes);
	return (tError);
}

