// ---------------------------------------------------------------------------

#ifndef ListPanelH
#define ListPanelH

#include <vcl.h>
#include "SkinScrollBarUnit.h"
// ---------------------------------------------------------------------------

class TListPanel;
class TListPanelItem;

/* 定义下拉列表项点击事件 */
typedef void __fastcall(__closure * TListPanelItemClick)(TListPanel *Sender, int Index);

class TListPanelItem : public TObject {

private:
	int FTag;
	String FCaption;
	TListPanelItemClick FOnItemClick;

	void __fastcall SetCaption(String Caption);

public:

	__fastcall TListPanelItem(String Caption, TListPanelItemClick OnItemClick);

	void __fastcall DrawItem(TCanvas *Canvas, TRect Rect, int flag);

__published:
	__property String Caption = {read = FCaption, write = FCaption};
	__property int Tag = {read = FTag, write = FTag};
	__property TListPanelItemClick OnItemClicked = {read = FOnItemClick, write = FOnItemClick};
};

class TListPanel : public TPanel {
private:
	int FItemHeight,
		FSelectedItemIndex;

	bool m_IsLButtonDown;

	TRect m_ItemArea;

	TList //*m_ItemRectList,
		  *m_ItemList;

	TSkinScrollBar *m_ScrollBar;

	int m_BeginIndex,
		m_EndIndex,
		m_CurrentItemIndex,
		m_ShowCount,
		m_RealShowCount,
		m_MouseX,
		m_MouseY,
		m_ItemWidth;

	void __fastcall DoKeyUpDown(int Index);
	void __fastcall DoShowItemsChange();

	void __fastcall SetItemStatus(int Index, int flag);
	void __fastcall SetSelectedItemIndex(int Index);

	int __fastcall GetItemsCount();
//	TRect *__fastcall GetItemRect(int Index);

	MESSAGE void __fastcall WMSize(Winapi::Messages::TWMSize &Message);
	MESSAGE void __fastcall WMEraseBkgnd(TWMEraseBkgnd &Message);
	MESSAGE void __fastcall WMMouseWheel(TWMMouseWheel &Message);
	MESSAGE void __fastcall CMMouseLeave(TMessage &Message);
	MESSAGE void __fastcall WMVScroll(TWMScroll &Message);

	BEGIN_MESSAGE_MAP
		MESSAGE_HANDLER(WM_SIZE, TWMSize, WMSize)
		MESSAGE_HANDLER(WM_ERASEBKGND, TWMEraseBkgnd, WMEraseBkgnd)
		MESSAGE_HANDLER(WM_MOUSEWHEEL, TWMMouseWheel, WMMouseWheel)
		MESSAGE_HANDLER(CM_MOUSELEAVE, TMessage, CMMouseLeave)
		MESSAGE_HANDLER(WM_VSCROLL, TWMScroll, WMVScroll)
	END_MESSAGE_MAP(TPanel)

protected:
	virtual void __fastcall CreateWnd();
	virtual void __fastcall Paint();

	DYNAMIC void __fastcall MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y);
	DYNAMIC void __fastcall MouseMove(TShiftState Shift, int X, int Y);
	DYNAMIC void __fastcall MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y);

public:
	__fastcall TListPanel(TComponent *Owner);
	__fastcall ~TListPanel();
	void __fastcall AddItem(TListPanelItem* Item);
	void __fastcall Clear();
	TListPanelItem *__fastcall GetItem(int Index);
	void __fastcall ScrollPos(int Pos);
	int __fastcall TextWidth(String text);
	void __fastcall ResetScroll();
//	void __fastcall SetTextFontSize(int sz);
	void __fastcall SetTextFont(String name, int size);

__published:
	__property int ItemHeight = {write = FItemHeight, read = FItemHeight, default = 16};
	__property int SelectedItemIndex = {read = FSelectedItemIndex, write = SetSelectedItemIndex};
	__property int ItemsCount = {read = GetItemsCount};
};

#endif
