#include <common/puremenu.hpp>
#include <common/purebmp.hpp>

PureMenu &PureMenu::operator=(const String &menuName)
{
  destroyMenu();
	mhPureMenu=::LoadMenu(::GetModuleHandle(0),(char*)(String&)menuName);
	mDisposition=Delete;
	return *this;
}

PureMenu &PureMenu::operator=(const PureMenu &somePureMenu)
{
	WORD itemEntries;
	UINT itemID;

	if(!somePureMenu.isOkay())return *this;
	itemEntries=somePureMenu.size();
	mMenuStyle=somePureMenu.mMenuStyle;
	createMenu();
	for(short itemIndex=0;itemIndex<itemEntries;itemIndex++)
	{
		String itemString;

		itemID=somePureMenu.menuItemID(itemIndex);
		itemString=somePureMenu.menuItemString(itemIndex);
		appendMenu(itemID,itemString);
	}
	return *this;
}

PureMenu &PureMenu::operator=(HMENU hMenu)
{
  if((HMENU)*this==hMenu)return *this;
  destroyMenu();
	mhPureMenu=hMenu;
	mDisposition=Keep;
  return *this;
}

BOOL PureMenu::getSubMenu(int menuItemPos,PureMenu &subMenu)const
{
  HMENU hSubMenu;
  if(!isOkay())return FALSE;
	hSubMenu=::GetSubMenu(mhPureMenu,menuItemPos);
	if(!hSubMenu)return FALSE;
	subMenu=hSubMenu;
	return TRUE;
}

WORD PureMenu::trackPopupMenu(HWND hParentWnd,const GDIPoint &trackPoint,DWORD trackFlags)const
{
	if(!isOkay())return FALSE;

	if(trackPoint==GDIPoint())::GetCursorPos(&((POINT&)trackPoint));
	return ::TrackPopupMenu(mhPureMenu,trackFlags,trackPoint.x(),trackPoint.y(),0,hParentWnd,0);
}

WORD PureMenu::size(void)const
{
	if(!isOkay())return FALSE;
	return ::GetMenuItemCount(mhPureMenu);
}

void PureMenu::createMenu(void)
{
	destroyMenu();
	if(PopupMenu==mMenuStyle)mhPureMenu=::CreatePopupMenu();
	else mhPureMenu=::CreateMenu();
	mDisposition=Delete;
}

void PureMenu::destroyMenu(void)
{
	if(!mhPureMenu)return;
	if(::IsMenu(mhPureMenu)&&Delete==mDisposition)::DestroyMenu(mhPureMenu);
	mhPureMenu=0;
	mDisposition=Invalid;
}

String PureMenu::menuItemString(UINT menuItem,PositionFlags positionFlags)const
{
	String menuItemString;

	if(!isOkay()||!size())return menuItemString;
	::GetMenuString(mhPureMenu,menuItem,(LPSTR)menuItemString,String::MaxString,positionFlags);
        menuItemString.removeTokens("&\t");
	return menuItemString;
}

UINT PureMenu::menuItemStringPos(const String &strMenuHeading)const
{
	UINT menuItems(size());
  String strMenuItem;

	for(int menuItem=0;menuItem<menuItems;menuItem++)
	{
	  strMenuItem=menuItemString(menuItem);
		strMenuItem.removeTokens("&");
		if(strMenuItem==strMenuHeading)return menuItem;
	}
	return 0;
}

bool PureMenu::checkMenuItem(UINT menuItem,UINT flags)const
{
  if(!isOkay()||!size())return FALSE;
	return 0xFFFFFFFF==::CheckMenuItem(mhPureMenu,menuItem,flags)?false:true;
}

UINT PureMenu::getMenuState(UINT menuItem,PositionFlags positionFlags)const
{
  if(!isOkay()||!size())return FALSE;
	return ::GetMenuState(mhPureMenu,menuItem,(UINT)positionFlags);
}

PureMenu::MenuType PureMenu::getMenuItemType(UINT menuItem,PositionFlags positionFlags)const
{
  MENUITEMINFO menuItemInfo;
	if(!isOkay())return (MenuType)0;
	::memset(&menuItemInfo,0,sizeof(MENUITEMINFO));
	menuItemInfo.cbSize=sizeof(MENUITEMINFO);
	menuItemInfo.fMask=MIIM_TYPE;
  ::GetMenuItemInfo(mhPureMenu,menuItem,positionFlags,&menuItemInfo);
	return (MenuType)menuItemInfo.fType;
}

WORD PureMenu::enableMenuItem(UINT menuItem,PositionFlags positionFlags,InsertFlags insertFlags)const
{
  if(!isOkay()||!size())return FALSE;
	return ::EnableMenuItem(mhPureMenu,menuItem,(UINT)positionFlags|(UINT)insertFlags);
}

WORD PureMenu::appendMenu(Block<MenuItem> &menuItems)const
{
	WORD itemCount(menuItems.size());

	if(!isOkay())return FALSE;
	for(short itemIndex=0;itemIndex<itemCount;itemIndex++)
		appendMenu(menuItems[itemIndex].itemID(),menuItems[itemIndex].itemString());
	return TRUE;
}

BOOL PureMenu::insertMenu(UINT position,InsertFlags insertFlags,UINT itemID,const String &itemString)const
{
  if(!isOkay())return FALSE;
	return ::InsertMenu(mhPureMenu,position,insertFlags|ItemString,itemID,itemString);
}

BOOL PureMenu::insertMenu(UINT position,const PureMenu &pureMenu,const String &itemString)const
{
  if(!isOkay()||!pureMenu.isOkay())return FALSE;
	return ::InsertMenu(mhPureMenu,position,ItemEnabled|ItemPopup|ItemString,(UINT)(HMENU)pureMenu,itemString);
}

BOOL PureMenu::insertMenu(UINT position,const PureMenu &pureMenu,PureBitmap &somePureBitmap)const
{
  if(!isOkay()||!pureMenu.isOkay())return FALSE;
	return ::InsertMenu(mhPureMenu,position,ItemEnabled|ItemBitmap|ItemPopup,(UINT)(HMENU)pureMenu,(LPCTSTR)(HBITMAP)somePureBitmap);
}

WORD PureMenu::isMenuItem(UINT itemID)const
{
	WORD itemCount(size());

	for(short itemIndex=0;itemIndex<itemCount;itemIndex++)if(itemID==menuItemID(itemIndex))return TRUE;
  return FALSE;
}


