//---------------------------------------------------------------------------
#ifndef MenuUnitH
#define MenuUnitH
//---------------------------------------------------------------------------
#include <Vcl.Imaging.pngimage.hpp>
#include <Vcl.AppEvnts.hpp>

//---------------------------------------------------------------------------
#define CLEAR_ALL -1
//---------------------------------------------------------------------------
String __fastcall CutDirString(TCanvas *ACanvas, String strDir, int nWidth);
typedef void __fastcall (__closure *DrawItem)( TObject *Sender, TCanvas* ACanvas, const TRect &ARect );
class TMenuPanel;
class CMenuItem : public TObject
{
private:
	System::Classes::TList* FSubItems;

	DrawItem FDrawItem;
	TNotifyEvent FOnClick;
	TPngImage* FIcon;

	bool FEnabled;
	bool FSelected;
	bool FBreak;
	int FMenuID;

	String FCaption;
	String FHotkey;
	CMenuItem *Parent;

public:
	int  m_iTypeMenu;

	__fastcall CMenuItem(String strCaption, String strHotkey, TNotifyEvent aClickEvent,
							   int idMenu=-1, TPngImage *pPic=0);
	__fastcall ~CMenuItem();

	void __fastcall AddItem( CMenuItem *Item );
	void __fastcall ClearSubItems();

	CMenuItem* __fastcall AppendMenu( String strCaption, String strHotkey="",
									  TNotifyEvent aClickEvent=NULL, int idMenu=-1,
									  TPngImage *pPic=0 );
	void __fastcall CreateSubMenu( TMenuPanel* pSubMenu );

	bool __fastcall HasSubItem() { return FSubItems->Count > 0; };
	bool __fastcall IsDrawDir() { return FCaption.Pos("\\")>0; };

__published:
	__property DrawItem OnDrawItem = { read=FDrawItem, write=FDrawItem };
	__property TNotifyEvent OnClick = { read=FOnClick, write=FOnClick };
	__property TPngImage* Icon = { read=FIcon, write=FIcon };
	__property String Caption = { read=FCaption, write=FCaption };
	__property String Hotkey = { read=FHotkey, write=FHotkey };
	__property bool Enabled = { read=FEnabled, write=FEnabled };
	__property bool Selected = { read=FSelected, write=FSelected };
	__property bool Break = { read=FBreak, write=FBreak };
	__property int MenuID = { read=FMenuID, write=FMenuID };
};

//---------------------------------------------------------------------------
class TMenuPanel: public Extctrls::TPanel
{
private:
//	TApplicationEvents *m_AppEvent;          //add by LAQ

	//滚动菜单信息
	int m_iUpState,
		m_iDownState,
		m_iFirst;
//	TTimer *m_pTimer;	//自动滚屏定时器

	void __fastcall TimerProc(TObject* Sender);
	void __fastcall DoHandleMessage(MSG &Msg, bool &Handled);	//add by LAQ
	void __fastcall HideAll();
	void __fastcall ClearAll();
	void __fastcall DrawItem( TObject *Sender, TCanvas* ACanvas, const TRect &ARect );
	void __fastcall DrawUpDown( TCanvas* ACanvas, const TRect &ARect, bool bUp );

protected:
	System::Classes::TList* m_Items;
	int m_ItemHeight,
		m_iType,
		m_iSelected;

	virtual void __fastcall CreateParams(TCreateParams &Params);
	virtual void __fastcall CreateWnd();
	virtual void __fastcall Paint(void);

	virtual void __fastcall CalRect(int X, int Y);
	virtual void __fastcall ShowSubMenu(CMenuItem *pItem, int iTop);
	virtual void __fastcall CloseMenu();
	virtual bool __fastcall IsDrawImg(){ return m_iType!=0; };
	virtual bool __fastcall IsDrawDir(){ return false; };

public:
	TMenuPanel *m_SubMenu,
				  *m_SupMenu;

	__fastcall TMenuPanel( TComponent* Owner=NULL, int iType=0 );
	__fastcall ~TMenuPanel();

	static void LoadResources();
	static void FreeResources();

	DYNAMIC void __fastcall MouseDown(System::Uitypes::TMouseButton Button,
							System::Classes::TShiftState Shift, int X, int Y);
	DYNAMIC void __fastcall MouseUp(System::Uitypes::TMouseButton Button,
							System::Classes::TShiftState Shift, int X, int Y);
	MESSAGE void __fastcall WMEraseBkgnd(Winapi::Messages::TWMEraseBkgnd &Message);
	MESSAGE void __fastcall WMMouseMove(TWMMouse &Message);
	MESSAGE void __fastcall CMMouseLeave(TMessage &Message);

	BEGIN_MESSAGE_MAP
		MESSAGE_HANDLER(WM_ERASEBKGND, TWMEraseBkgnd, WMEraseBkgnd)
		MESSAGE_HANDLER(WM_MOUSEMOVE, TWMMouseMove, WMMouseMove)
		MESSAGE_HANDLER(CM_MOUSELEAVE, TMessage, CMMouseLeave)
	END_MESSAGE_MAP(Vcl::Extctrls::TPanel)

	CMenuItem*  __fastcall CreateMenu( String strCaption, String strHotkey="",
											TNotifyEvent aClickEvent=NULL, int idMenu=-1,
											TPngImage *pPic=0);
	void __fastcall SetMenuItems( System::Classes::TList* pItems ) { m_Items=pItems; };
	void __fastcall DeleteMenuItem(int index);
	void __fastcall ClearItems() { DeleteMenuItem(CLEAR_ALL); };
	void __fastcall Show( int X, int Y );

__published:
	__property int Type = { read=m_iType, write=m_iType };

};
//---------------------------------------------------------------------------
#endif

