//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "GlobalUnit.h"
#include "MessageDef.h"
#include "ToolpalPanel.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

extern TImage * w_imgDrag;  //拖动图片
extern bool w_bDraging;
extern int w_iDragMode,  //拖动模式
			w_xImgDrag, //透明图片位置
			w_yImgDrag,
			w_xOldDrag, //透明图片上次位置
			w_yOldDrag,
			w_xOffDrag, //图片左上角与鼠标距离
			w_yOffDrag;

const COLORREF clrBackground = RGB(47, 50, 57), //面板背景
		clrBackboard = RGB( 29,31,35 ), //面板边框
		clrHeaderback = RGB( 58, 62, 69 ); //标题栏背景

TMenuPanel* TToolpalPanel::m_PopMenu = NULL;
__fastcall TToolpalPanel::TToolpalPanel(TComponent* AOwner)
	: Vcl::Extctrls::TPanel(AOwner)
{
	Visible = false;
	ParentFont = true;
	ShowHint = true;
	DoubleBuffered = true;

	m_nTitleHeight = 25;
	m_iType = 0;
	Color = clrBackground;

	m_sbVert = NULL;
	m_ApplicationEvents = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TToolpalPanel::CreateWnd(void)
{
	Vcl::Extctrls::TPanel::CreateWnd();

	if( m_ApplicationEvents==NULL ){
		m_ApplicationEvents = new TApplicationEvents(NULL);
		m_ApplicationEvents->OnMessage = DoHandleMessage;
	}
}
//---------------------------------------------------------------------------

__fastcall TToolpalPanel::~TToolpalPanel()
{
	if( m_ApplicationEvents )
		delete m_ApplicationEvents;

	if( m_sbVert )
		delete m_sbVert;

	while( m_lstBtnTools.size()>0 ){
		delete m_lstBtnTools.back();
		m_lstBtnTools.pop_back();
	}
}
//---------------------------------------------------------------------------
void __fastcall TToolpalPanel::Paint()
{
	if( w_bDraging ){
		//绘制半透明图片
		TBlendFunction Blend;
		Blend.BlendOp = AC_SRC_OVER;
		Blend.BlendFlags = 0;
		Blend.AlphaFormat = 0;
		Blend.SourceConstantAlpha =135;

		TPoint pt(w_xImgDrag, w_yImgDrag);
		pt = ScreenToClient( pt );

		::AlphaBlend (Canvas->Handle,
					  pt.X,
					  pt.Y,
					  w_imgDrag->Width,
					  w_imgDrag->Height,
					  w_imgDrag->Canvas->Handle,
					  0,
					  0,
					  w_imgDrag->Width,
					  w_imgDrag->Height,
					  Blend
					 );
	}
}
//---------------------------------------------------------------------------

void __fastcall TToolpalPanel::WMEraseBkgnd(Winapi::Messages::TWMEraseBkgnd &Message)
{
	TRect rect = GetClientRect();

	TCanvas* tempCanvas = new TCanvas;
	tempCanvas->Handle = Message.DC;

	//绘制外边框
	tempCanvas->Brush->Color = clrBackboard;
	tempCanvas->FrameRect(rect);

	//绘制标题栏
	tempCanvas->Pen->Color = clrBackboard;
	tempCanvas->MoveTo( 1, m_nTitleHeight );
	tempCanvas->LineTo( Width-1, m_nTitleHeight );

	TRect rtTitle(rect);
	rtTitle.Left++;
	rtTitle.Right--;
	rtTitle.Top++;
	rtTitle.Bottom = m_nTitleHeight;

	tempCanvas->Brush->Color = clrHeaderback;
	tempCanvas->FillRect(rtTitle);

	tempCanvas->Font->Name = g_strTxtFontName;
    tempCanvas->Font->Size = g_szFont9;
	tempCanvas->Font->Color = RGB(156, 170, 189);

	TSize size = tempCanvas->TextExtent( m_strTitle );
	int x = 12,//(Width - size.Width) / 2,
		y = (25 - size.Height) / 2 + 1;

	tempCanvas->TextOut( x, y, m_strTitle );

	//绘制底色
	rect.Top += m_nTitleHeight;
	rect.Left++;
	rect.Right--;
	rect.Bottom--;
	tempCanvas->Brush->Color = clrBackground;
	tempCanvas->FillRect( rect );

	delete tempCanvas;

	Message.Result = true;
}
//---------------------------------------------------------------------------

void __fastcall TToolpalPanel::WMSize(Winapi::Messages::TWMSize &Message)
{
	DefaultHandler( &Message );

	LayoutBtns();
}
//---------------------------------------------------------------------------

void __fastcall TToolpalPanel::AddToolBtn( int stateCount, String resName,
									TNotifyEvent aClickEvent, String strHint,
									int index )
{
	if( resName.Length()>0 ){
		TPngImage* img = new TPngImage();
		img->LoadFromResourceName( int(HInstance), resName );

		TPNGButton* pBtn = new TPNGButton(NULL);
		pBtn->SetStateCount( stateCount );
		pBtn->SetPngImg( img );

		delete img;

		pBtn->Parent = this;
//		pBtn->Left = Width - pBtn->Width - 4;
		pBtn->Top = ( m_nTitleHeight - pBtn->Height );
		pBtn->OnClick = aClickEvent;
		pBtn->ShowHint = true;
		pBtn->Hint = strHint;
		pBtn->OnMouseEnter = BtnToolMouseEnter;

		if( index==-1 )
			m_lstBtnTools.push_back( pBtn );
		else
			m_lstBtnTools.insert( m_lstBtnTools.begin()+index, pBtn );

		LayoutBtns();
	}
}
//---------------------------------------------------------------------------

void __fastcall TToolpalPanel::DoHandleMessage(MSG &Msg, bool &Handled)
{
	if( !Visible )
		return;


	if( Msg.message==WM_MOUSEMOVE && Msg.wParam==0 ){
		TPoint pt( Msg.pt.x, Msg.pt.y );
		pt = ScreenToClient( pt );

		if( m_sbVert && m_sbVert->Visible && Msg.hwnd!=m_sbVert->Handle ) //滚动条自动隐藏
			m_sbVert->Visible = false;

		if( pt.x>=0 && pt.x<Width && pt.y>=0 && pt.y<Height && Msg.hwnd==Handle ){
			if( pt.x <= LEFT_BOUND ){ //拖动调整宽度
				if( g_iToolPanelShow==0 ){
					Screen->Cursor = crHSplit;
					m_ApplicationEvents->CancelDispatch();
				}
			}
			else{
				if( Screen->Cursor==crHSplit )
					Screen->Cursor = crDefault;

				if( CanVSplit() && pt.y <= LEFT_BOUND ){ //拖动调整高度
					Screen->Cursor = crVSplit;
					m_ApplicationEvents->CancelDispatch();
				}
				else if( Screen->Cursor==crVSplit )
					Screen->Cursor = crDefault;
			}

			if( m_sbVert && m_sbVert->IsOperable() && pt.x >= m_sbVert->Left
													&& pt.y >= m_sbVert->Top  ){ //滚动条自动显示
				m_sbVert->Visible = true;
				m_ApplicationEvents->CancelDispatch();
			}
		}
		else if( Screen->Cursor==crHSplit && g_iToolPanelShow==1
											&& ( pt.x<0 || pt.x>=Width )
					|| Screen->Cursor==crVSplit	&& (pt.y<0 || pt.y>=Height))
			Screen->Cursor = crDefault;
	}
}
//---------------------------------------------------------------------------

void __fastcall TToolpalPanel::WMMouseDown(TWMMouse &Message)
{
	if( ( Message.Keys & MK_LBUTTON ) && ( Screen->Cursor==crVSplit //调整高度
										|| Screen->Cursor==crHSplit )){ //调整宽度
		TPoint pt;

		if( Screen->Cursor==crVSplit ){
			DrawDragImg( DM_ADJUSTHEIGHT );
			pt.x = 1;
			pt.y = 0;
		}
		else{
			DrawDragImg( DM_ADJUSTWIDTH );

			pt.x = 0;
			pt.y = PANEL_TOP - Top;
		}

		pt = ClientToScreen( pt );

		w_xOldDrag = w_xImgDrag = pt.X;
		w_yOldDrag = w_yImgDrag = pt.Y;

		w_xOffDrag = Message.XPos - 1;
		w_yOffDrag = Message.YPos;

		SetCapture( Handle );

		Message.Result = 1;
	}
}
//---------------------------------------------------------------------------

void TToolpalPanel::DrawDragImg( int mode )
{
	w_imgDrag = new TImage(this);

	if( mode==DM_ADJUSTWIDTH ){
		COLORREF clrPixel[2] = { clWhite, clBlack };

		w_imgDrag->Width = 4;
		w_imgDrag->Height = Application->MainForm->Height - PANEL_TOP - STATE_HEIGHT;

		TRect rcDraw( 0, 0, w_imgDrag->Width, w_imgDrag->Height );

		for( int i=0; i<w_imgDrag->Width; i++ ){
			for( int j=0; j<w_imgDrag->Height; j++ ){
				w_imgDrag->Canvas->Pixels[i][j] = clrPixel[(i+j)%2];
			}
		}
	}
	else if( mode==DM_ADJUSTHEIGHT ){
		COLORREF clrPixel[2] = { clWhite, clBlack };

		w_imgDrag->Width = Width-2;
		w_imgDrag->Height = 4;

		TRect rcDraw( 0, 0, w_imgDrag->Width, w_imgDrag->Height );

		for( int i=0; i<w_imgDrag->Width; i++ ){
			for( int j=0; j<w_imgDrag->Height; j++ ){
				w_imgDrag->Canvas->Pixels[i][j] = clrPixel[(i+j)%2];
			}
		}
	}

	w_bDraging = true;
	w_iDragMode = mode;
}
//---------------------------------------------------------------------------

bool TToolpalPanel::ShowDragImg( bool bShow, int iState )
{
	int w = w_imgDrag->Width,
		h = w_imgDrag->Height;
	TPoint ptOld( w_xOldDrag, w_yOldDrag ),
		   ptNew( w_xImgDrag, w_yImgDrag );
	RECT rtInvalid;

	ptOld = ScreenToClient( ptOld );
	ptNew = ScreenToClient( ptNew );

	if( w_iDragMode==DM_ADJUSTWIDTH ){
		rtInvalid.left = Width;
		rtInvalid.right = 0;
		rtInvalid.top = 1;
		rtInvalid.bottom = Height-1;

		if( ptOld.X>0 && ptOld.X<Width
					|| ptOld.X+w>0 && ptOld.X+w<Width ){
			rtInvalid.left = min( ptOld.X, rtInvalid.left );
			rtInvalid.right = max( ptOld.X+w, rtInvalid.right );
		}

		if( bShow && ( ptNew.X>0 && ptNew.X<Width
					|| ptNew.X+w>0 && ptNew.X+w<Width )){
			rtInvalid.left = min( ptNew.X, rtInvalid.left );
			rtInvalid.right = max( ptNew.X+w, rtInvalid.right );
		}

		if( rtInvalid.left<0 ) rtInvalid.left = 0;
		if( rtInvalid.right>Width ) rtInvalid.right = Width;

		if( rtInvalid.right>rtInvalid.left ){
			InvalidateRect( Handle, &rtInvalid, false );
		}
	}

	return false;
}
//---------------------------------------------------------------------------

void TToolpalPanel::DeleteToolBtn( int index )
{
	if( index<m_lstBtnTools.size() ){
		delete m_lstBtnTools[index];
		m_lstBtnTools.erase( m_lstBtnTools.begin() + index );

		LayoutBtns();
	}
}
//---------------------------------------------------------------------------

void TToolpalPanel::LayoutBtns()
{
	int r = Width;
	for( int i=0; i<m_lstBtnTools.size(); i++ ){
		TPNGButton* pBtn = m_lstBtnTools[i];
		pBtn->Left = r - pBtn->Width;
		if( i==0 ) pBtn->Left -= 4;

		r = pBtn->Left;
	}
}
//---------------------------------------------------------------------------

void __fastcall TToolpalPanel::BtnToolMouseEnter(TObject* Sender)
{
	if( Screen->Cursor!=crDefault )
		Screen->Cursor = crDefault;
}
//---------------------------------------------------------------------------
