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

#include <w_app.hpp>
#include <z_htips.hpp>
#include <z_popup.hpp>

// ----- ZafPopUpItem ----------------------------------------------------

#if defined(ZAF_DEFAULT_DRAW)
ZafEventType ZafPopUpItem::Draw(const ZafEventStruct &, ZafEventType )
{
	// Windows automatically displays text.
	return (FALSE);
}
#endif

ZafEventType ZafPopUpItem::Event(const ZafEventStruct &event)
{
	ZafEventType ccode = event.type;

	if (ccode == E_OSEVENT)
		return ZafPopUpItem::OSEvent(event);

	switch (ccode)
	{
	case S_INITIALIZE:
	case S_DEINITIALIZE:
	case S_REGISTER_OBJECT:
		menu.Event(event);
		ccode = ZafWindowObject::Event(event);
		break;

	case S_CREATE:
		Event(S_INITIALIZE);
		Event(S_REGISTER_OBJECT);
		DrawMenuBar(RootObject()->OSScreenID(ZAF_FRAMEID));
		break;

	case S_DESTROY:
		{
		RemoveMenu((HMENU)screenID, menu.screenID ? (UINT)menu.screenID : (UINT)numberID, MF_BYCOMMAND);
		if (menu.screenID)
			DestroyMenu((HMENU)menu.screenID);
		Event(ZafEventStruct(S_DEINITIALIZE));
		}
		break;

	case S_CURRENT:
		ccode = ZafWindowObject::Event(event);
		Event(N_CURRENT);
		break;

	case S_NON_CURRENT:
		// NotifyFocus() is not used by menus... propegate the message.
		if (menu.Focus())
			menu.Event(event);
		Event(N_NON_CURRENT);
		ccode = ZafWindowObject::Event(event);
		break;

	case N_CURRENT:
		// Update the helpTips help object.
		if (HelpObjectTip() && RootObject()->Focus())
		{
			ZafEventStruct tEvent = event;
			tEvent.type = DH_UPDATE_HELP_OBJECT;
			tEvent.windowObject = this;
			eventManager->Event(tEvent, E_HELPTIPS);
		}
		break;

	case N_NON_CURRENT:
		// Update the helpTips help object.
		if (HelpObjectTip())
		{
			ZafEventStruct tEvent = event;
			tEvent.type = DH_UPDATE_HELP_OBJECT;
			tEvent.windowObject = ZAF_NULLP(ZafWindowObject);
			eventManager->Event(tEvent, E_HELPTIPS);
		}
		break;

	case L_SELECT:
		ccode = ZafButton::Event(event);
		break;

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

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

	default:
		ccode = ZafWindowObject::Event(event);
		break;
	}

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

bool ZafPopUpItem::SetDisabled(bool setDisabled)
{
	if (setDisabled != disabled)
	{
		// Set the attribute.
		disabled = setDisabled;

		// Set the selectabilty with the OS.
		if (screenID)
			EnableMenuItem((HMENU)screenID, ListIndex(), MF_BYPOSITION | (disabled ? MF_GRAYED : MF_ENABLED));
	}

	return disabled;
}

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

ZafEventType ZafPopUpItem::OSEvent(const ZafEventStruct &event)
{
	ZafEventType ccode = event.type;

	switch (event.osEvent.message)
	{
	case WM_COMMAND:
	case WM_SYSCOMMAND:
		// Item was selected.
		Event(ZafEventStruct(L_SELECT));
		break;
	
	default:
		ccode = ZafWindowObject::Event(event);
	}

	return (ccode);
}

void ZafPopUpItem::OSRegisterObject(void)
{
	screenID = parent->screenID;

	UINT flags = MF_STRING;
	if (ItemType() == ZAF_SEPARATOR)
		flags |= MF_SEPARATOR;
	if (menu.First())
		flags |= MF_POPUP;
	if (disabled)
		flags |= MF_GRAYED | MF_DISABLED;
	if (selected)
		flags |= MF_CHECKED;

	ZafIChar *hotKeyText = ZafMSWindowsApp::HotKeyText(stringData ? stringData->Text() : ZAF_NULLP(ZafIChar), hotKeyIndex);

	UINT identifier = (UINT)numberID;

	ZafWindowObject *parentPtr = parent;
	while (parentPtr && !parentPtr->IsA(ID_ZAF_SYSTEM_BUTTON))
		parentPtr = parentPtr->parent;

	// If parentPtr is ! NULL, it means this item is in a system menu.
	// If so, check to see if a special item type needs to be set.  These
	// item types should not be processed here if the item is in a pull-down
	// menu, since it has already had its userFunction and value set in 
	// SetItemType().
	if (parentPtr)
	{
		if (itemType == ZAF_CLOSE_OPTION)
		{
			memberUserFunction = (MemberUserFunction)&ZafWindowObject::DefaultUserFunction;
			identifier = SC_CLOSE;
		}
		else if (itemType == ZAF_MAXIMIZE_OPTION)
		{
			memberUserFunction = (MemberUserFunction)&ZafWindowObject::DefaultUserFunction;
			identifier = SC_MAXIMIZE;
		}
		else if (itemType == ZAF_MINIMIZE_OPTION)
		{
			memberUserFunction = (MemberUserFunction)&ZafWindowObject::DefaultUserFunction;
			identifier = SC_MINIMIZE;
		}
		else if (itemType == ZAF_MOVE_OPTION)
		{
			memberUserFunction = (MemberUserFunction)&ZafWindowObject::DefaultUserFunction;
			identifier = SC_MOVE;
		}
		else if (itemType == ZAF_RESTORE_OPTION)
		{
			memberUserFunction = (MemberUserFunction)&ZafWindowObject::DefaultUserFunction;
			identifier = SC_RESTORE;
		}
		else if (itemType == ZAF_SIZE_OPTION)
		{
			memberUserFunction = (MemberUserFunction)&ZafWindowObject::DefaultUserFunction;
			identifier = SC_SIZE;
		}
	}

	if (ZafMSWindowsApp::convertText)
	{
		char *osText = zafCodeSet->ConvertToOSString(hotKeyText, ZAF_NULLP(char), false);
#if defined(ZAF_WIN32) && defined(ZAF_UNICODE)
		InsertMenuA((HMENU)screenID, ListIndex(), MF_BYPOSITION | flags, menu.screenID ? (UINT)menu.screenID : identifier, osText);
#else
		InsertMenu((HMENU)screenID, ListIndex(), MF_BYPOSITION | flags, menu.screenID ? (UINT)menu.screenID : identifier, osText);
#endif
	}
#if defined(ZAF_WIN32) || !defined(ZAF_UNICODE)
	else
		InsertMenu((HMENU)screenID, ListIndex(), MF_BYPOSITION | flags, menu.screenID ? (UINT)menu.screenID : identifier, hotKeyText);
#endif

	delete []hotKeyText;
}

bool ZafPopUpItem::SetSelected(bool setSelected)
{
	bool oldSelected = selected;

	ZafButton::SetSelected(setSelected);

	if (selected != oldSelected && screenID)
		CheckMenuItem((HMENU)screenID, ListIndex(), MF_BYPOSITION | (selected ? MF_CHECKED : MF_UNCHECKED));

	return (selected);
}

ZafError ZafPopUpItem::OSSetText(void)
{
	if (screenID)
	{
		ZafIChar *hotKeyText = ZafMSWindowsApp::HotKeyText(stringData ? stringData->Text() : ZAF_NULLP(ZafIChar), hotKeyIndex);

		if (ZafMSWindowsApp::convertText)
		{
			char *osText =	zafCodeSet->ConvertToOSString(hotKeyText, ZAF_NULLP(char), false);
#if defined(ZAF_WIN32) && defined(ZAF_UNICODE)
			ModifyMenuA((HMENU)screenID, ListIndex(),
			 MF_BYPOSITION | MF_STRING | (selected ? 
			 MF_CHECKED : MF_UNCHECKED) | (disabled ? MF_GRAYED : MF_ENABLED),
			 menu.screenID ? (UINT)menu.screenID : (UINT)numberID, osText);
#else
			ModifyMenu((HMENU)screenID, ListIndex(),
			 MF_BYPOSITION | MF_STRING | (selected ? MF_CHECKED : MF_UNCHECKED) |
			 (disabled ? MF_GRAYED : MF_ENABLED),
			 menu.screenID ? (UINT)menu.screenID : (UINT)numberID, osText);
#endif
		}
#if defined(ZAF_WIN32) || !defined(ZAF_UNICODE)
		else
			ModifyMenu((HMENU)screenID, ListIndex(),
			 MF_BYPOSITION | MF_STRING | (selected ? MF_CHECKED : MF_UNCHECKED) |
			 (disabled ? MF_GRAYED : MF_ENABLED),
			 menu.screenID ? (UINT)menu.screenID : (UINT)numberID, hotKeyText);
#endif
		delete []hotKeyText;
	}

	return ZAF_ERROR_NONE;
}

ZafError ZafPopUpItem::OSUpdatePalettes(ZafPaletteType zafTypes, 
 ZafPaletteType osTypes)
{
	// Defer to the base class.
	ZafError tError = ZafButton::OSUpdatePalettes(zafTypes, osTypes);
	return (tError);
}

// ----- Stubs --------------------------------------------------------------

void ZafPopUpItem::OSMapPalette(ZafPaletteStruct &, ZafPaletteType, ZafPaletteState)
{
}

void ZafPopUpItem::OSSize(void)
{
}

