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

#include <w_app.hpp>
#include <z_plldn.hpp>

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

ZafEventType ZafPullDownItem::Event(const ZafEventStruct &event)
{
	// Switch on the event type.
	ZafEventType ccode = event.type;
	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 S_ADD_OBJECT:
		Add(event.windowObject);
		break;

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

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

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

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

		// Set the selectabilty with the OS.
		if (screenID)
		{
			ZafWindow *rootWindow = DynamicPtrCast(RootObject(), ZafWindow);  
			int extraItem = 0;

			if (rootWindow && rootWindow->IsA(ID_ZAF_MDI_WINDOW))
			{
				OSWindowID MDIclientHwnd = rootWindow->OSScreenID(ZAF_SCREENID);
				BOOL childMax;
#if defined(ZAF_WIN32)
				::SendMessage(MDIclientHwnd, WM_MDIGETACTIVE, (WPARAM)NULL, (LPARAM)&childMax);
#else
				childMax = (BOOL)HIWORD(::SendMessage(MDIclientHwnd, WM_MDIGETACTIVE, (WPARAM)NULL, (LPARAM)NULL));
#endif

				if (childMax)
					extraItem = 1;
			}

			EnableMenuItem((HMENU)screenID, ListIndex() + extraItem, MF_BYPOSITION | (disabled ? MF_GRAYED : MF_ENABLED));
			DrawMenuBar(RootObject()->OSScreenID(ZAF_FRAMEID));
		}
	}

	return disabled;
}

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

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

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

	return (ccode);
}

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

	UINT flags = MF_STRING;
	if (menu.First())
		flags |= MF_POPUP;
	if (disabled)
		flags |= MF_GRAYED;

	ZafIChar *hotKeyText = ZafMSWindowsApp::HotKeyText(*stringData, hotKeyIndex);

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

	delete []hotKeyText;
}

ZafError ZafPullDownItem::OSSetText(void)
{
	if (screenID)
	{
		ZafWindow *rootWindow = DynamicPtrCast(RootObject(), ZafWindow);  
		int extraItem = 0;

		if (RootObject()->IsA(ID_ZAF_MDI_WINDOW))
		{
			OSWindowID MDIclientHwnd = rootWindow->OSScreenID(ZAF_SCREENID);
			BOOL childMax = 0;
#if defined(ZAF_WIN32)
			::SendMessage(MDIclientHwnd, WM_MDIGETACTIVE, (WPARAM)NULL, (LPARAM)&childMax);
#else
			childMax = (BOOL)HIWORD(::SendMessage(MDIclientHwnd, WM_MDIGETACTIVE, (WPARAM)NULL, (LPARAM)NULL));
#endif

			if (childMax)
				extraItem = 1;
		}
		ZafIChar *hotKeyText = ZafMSWindowsApp::HotKeyText(*stringData, hotKeyIndex);

//??? Modifying a menu item destroys its menu?
		if (ZafMSWindowsApp::convertText)
		{
			char *osText = zafCodeSet->ConvertToOSString(hotKeyText, ZAF_NULLP(char), false);

#if defined(ZAF_WIN32) && defined(ZAF_UNICODE)
			ModifyMenuA((HMENU)screenID, ListIndex() + extraItem, MF_STRING | MF_BYPOSITION, menu.screenID ? (UINT)menu.screenID : (UINT)numberID, osText);
#else
			ModifyMenu((HMENU)screenID, ListIndex() + extraItem, MF_STRING | MF_BYPOSITION, menu.screenID ? (UINT)menu.screenID : (UINT)numberID, osText);
#endif
		}
#if defined(ZAF_WIN32) || !defined(ZAF_UNICODE)
		else
			ModifyMenu((HMENU)screenID, ListIndex() + extraItem, MF_STRING | MF_BYPOSITION, menu.screenID ? (UINT)menu.screenID : (UINT)numberID, hotKeyText);
#endif

		delete []hotKeyText;

		EnableMenuItem((HMENU)screenID, ListIndex() + extraItem,	MF_BYPOSITION | (disabled ? MF_GRAYED : MF_ENABLED));

		// Changing the menu item destroys the link with the popup list
		// under Windows 16 bit (Windows 3.1 only).
		if (ZafMSWindowsApp::windowsPlatform == ZAF_WIN16)
		{
			parent->Event(S_DESTROY);
			parent->Event(S_CREATE);
		}

		DrawMenuBar(parent->parent->OSScreenID(ZAF_FRAMEID));
	}
	return (ZAF_ERROR_NONE);
}

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

#if defined(ZAF_DEFAULT_DRAW)
ZafEventType ZafPullDownItem::Draw(const ZafEventStruct &, ZafEventType ccode)
{
	return (ccode);
}
#endif

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

void ZafPullDownItem::OSSize(void)
{
}

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