#ifndef __MwlmfcPopupMenu_H__
#define __MwlmfcPopupMenu_H__

//	クラス宣言
class CMwlmfcPopupMenu
{
private:
	CMenu m_popupMenu;
	CMenu *m_pPopup;
	UINT m_nMenuId;
	CWnd *m_pCallingDialog;

	void CMwlmfcPopupMenu_Base(){
		Release();
		m_pPopup=NULL; m_nMenuId=0xffffffff; m_pCallingDialog=NULL;
	}
	void LoadPopup( int nMenuId, int nSubMenuIndex=0 ){
		Release();
		VERIFY( m_popupMenu.LoadMenu(nMenuId) );
		m_pPopup = m_popupMenu.GetSubMenu(nSubMenuIndex);
	}

public:
	//	コンストラクタ・デストラクタ
	CMwlmfcPopupMenu(){ CMwlmfcPopupMenu_Base(); }
	CMwlmfcPopupMenu( UINT nMenuId, CWnd *pCallingDialog ){ Setup( nMenuId, pCallingDialog ); }
	CMwlmfcPopupMenu( UINT nMenuId, int nSubMenuIndex, CWnd *pCallingDialog ){ Setup( nMenuId, pCallingDialog, nSubMenuIndex ); }
	virtual ~CMwlmfcPopupMenu(){}

	void Setup( UINT nMenuId, CWnd *pCallingDialog, int nSubMenuIndex=0 )
	{
		CMwlmfcPopupMenu_Base();
		m_nMenuId=nMenuId;
		m_pCallingDialog=pCallingDialog;
		LoadPopup( nMenuId, nSubMenuIndex );
	}
	void Release(){
		m_popupMenu.DestroyMenu();
		/*
		//	2008/06/29 - 前回のデータを破棄する処理を追加
		if ( m_pPopup != NULL ){
			if ( m_popupMenu.DestroyMenu() != TRUE ){
				EzErrBox("CMwlmfcPopupMenu: m_popupMenu.DestroyMenu() に失敗。");
				return;
			}
		}*/
	}

	//	エラー時NULL復帰
	/*BOOL Popup(){ return Popup(m_nMenuId,m_pCallingDialog); }
	BOOL Popup( UINT nMenuId, CWnd *pCallingDialog ){
		return Popup( nMenuId, 0, pCallingDialog ); }
	BOOL Popup( UINT nMenuId, int nSubMenuIndex, CWnd *pCallingDialog )*/
	//	2008/07/03  復帰値は選択された項目のID。エラー時0
	//	  -> なんかメッセージ飛ばなくなるくさくて駄目ぽ・・ -> SendMessageにて回避！
	UINT Popup(){ return Popup(m_nMenuId,m_pCallingDialog); }
	UINT Popup( UINT nMenuId, CWnd *pCallingDialog ){
		return Popup( nMenuId, 0, pCallingDialog ); }
	UINT Popup( UINT nMenuId, int nSubMenuIndex, CWnd *pCallingDialog )
	{
		if ( nMenuId == 0xffffffff || pCallingDialog == NULL )
			return 0;

		// Shift + F10 に備えてカーソルポジションを取得
		POINT point;
		GetCursorPos(&point);

		if ( m_pPopup == NULL )
			LoadPopup(nMenuId, nSubMenuIndex);

		ASSERT(m_pPopup != NULL);
		pCallingDialog->SetForegroundWindow();

		//	ポップアップメニュー表示
		UINT cmdId = m_pPopup->TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
			//point.x, point.y, AfxGetMainWnd());
			point.x, point.y, pCallingDialog);	//	pCallingDialogでないと駄目くない・・・？

		//	2008/07/03  TPM_RETURNCMDを指定するとメッセージが飛ばなくなる臭いのでSendMessageで飛ばす…
		SendMessage(pCallingDialog->m_hWnd, WM_COMMAND, cmdId, 0);	//	最後0でいいのかな・・・

		return cmdId;
	}

	//	EnableMenuItem()系
	BOOL EnableMenuItem( UINT uItemID, UINT uEnable ){
		if ( m_pPopup == NULL )
			return FALSE;
		return m_pPopup->EnableMenuItem( uItemID, uEnable );
	}
	BOOL GrayByCmd( UINT uItemID ){
		if ( m_pPopup == NULL )
			return FALSE;
		return m_pPopup->EnableMenuItem( uItemID, MF_GRAYED|MF_BYCOMMAND );
	}
	BOOL GrayByPos( UINT uItemID ){
		if ( m_pPopup == NULL )
			return FALSE;
		return m_pPopup->EnableMenuItem( uItemID, MF_GRAYED|MF_BYPOSITION );
	}
	BOOL GrayPopup( UINT uChildItemID );

	CMenu* GetMenuPtr(){ return m_pPopup; }
};

namespace mwlmfc{
	typedef CMwlmfcPopupMenu CPopupMenu;
}//namespace mwlmfc

#endif//__MwlmfcPopupMenu_H__
