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

#include "GlobalUnit.h"
#include "MessageFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "PNGButton"
#pragma link "CKBox"
#pragma link "GeneralDialogPanel"
#pragma link "TextButton"
#pragma resource "*.dfm"
TMessageForm *MessageForm;

//---------------------------------------------------------------------------
__fastcall TMessageForm::TMessageForm(TComponent* Owner)
	: TForm(Owner)
{
	m_iCheck = -1;

	Font->Color = RGB(60,43,23);
}

//---------------------------------------------------------------------------
__fastcall TMessageForm::~TMessageForm()
{
}

//---------------------------------------------------------------------------
void __fastcall TMessageForm::WMNCHitTest(TWMNCHitTest &Msg)
{
	TForm::Dispatch(&Msg);

	if( Msg.Result==HTCLIENT && Msg.YPos<Top+24 )
		Msg.Result = HTCAPTION;
}

//---------------------------------------------------------------------------
int __fastcall TMessageForm::ShowMessage( const String AMessage, int AType, const String ATitle )
{
	if( Visible )
		return -1;

	m_iconInformation->Visible = false;
	m_iconError->Visible = false;
	m_iconQuestion->Visible = false;

	m_btnYes->Visible = false;
	m_btnNo->Visible = false;
	m_btnOk->Visible = false;
	m_btnCancel->Visible = false;

	m_pbCheck->Visible = false;

	int iIconType = AType & 0x0F0;
	if( iIconType==MB_ICONERROR ){
		m_iconError->Visible = true;
	}
	else if( iIconType==MB_ICONQUESTION ) {
		m_iconQuestion->Visible = true;
	}
	else if( iIconType==MB_ICONINFORMATION ) {
		m_iconInformation->Visible = true;
	}

	int iButtonType = AType & 0x0F;
	if( iButtonType==MB_OK ){
		m_btnOk->Visible = true;
		m_btnOk->Left = (Width - m_btnOk->Width)/2 + 6;
	}
	else if( iButtonType==MB_OKCANCEL ){
		m_btnOk->Visible = true;
		m_btnCancel->Visible = true;
		m_btnOk->Left = (Width - (m_btnOk->Width+6)*2 )/2 + 6;
		m_btnCancel->Left = m_btnOk->Left + m_btnOk->Width + 6;
	}
	else if( iButtonType==MB_YESNO ){
		m_btnYes->Visible = true;
		m_btnNo->Visible = true;
		m_btnYes->Left = (Width - (m_btnYes->Width+6)*2 )/2 + 6;
		m_btnNo->Left = m_btnYes->Left + m_btnYes->Width + 6;
	}
	else if( iButtonType==MB_YESNOCANCEL ){
		m_btnYes->Visible = true;
		m_btnNo->Visible = true;
		m_btnCancel->Visible = true;
		m_btnYes->Left = (Width - (m_btnYes->Width+6)*3 )/2 + 6;
		m_btnNo->Left = m_btnYes->Left + m_btnYes->Width + 6;
		m_btnCancel->Left = m_btnNo->Left + m_btnNo->Width + 6;
//		m_pbCheck->Width = m_btnYes->Left - m_pbCheck->Left - 4 ;          //add
	}

	int iCheckType = AType & 0xF00;
	if( iCheckType ){
		if( iCheckType==MB_CHECKED ){
			m_pbCheck->Checked = true;
		}
		else if( iCheckType==MB_UNCHECKED ){
			m_pbCheck->Checked = false;
		}
		m_pbCheck->Show();
	}
	else{
		m_pbCheck->Hide();
	}

	m_lblMessage->Caption = AMessage;

	if( ATitle.Length()>0 )
		m_panGeneral->Caption = ATitle;
	else
        m_panGeneral->Caption = "提示";
	//圆角
//	HRGN hRgn = CreateRoundRectRgn( 0, 0, Width+1, Height+2, 20, 20 );
//	SetWindowRgn( Handle, hRgn, false );
//	DeleteObject( hRgn );

	return ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMessageForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	switch( Key ){
	case VK_RETURN:
	case ' ':
		if( m_btnYes->Visible )
			ModalResult = mrYes;
		else if( m_btnOk->Visible )
			ModalResult = mrOk;
		break;

	case VK_ESCAPE:
		if( m_btnCancel->Visible )
			ModalResult = mrCancel;
		break;

	case 'Y':
		if( m_btnYes->Visible )
			ModalResult = mrYes;
		break;

	case 'N':
		if( m_btnNo->Visible )
			ModalResult = mrNo;
		break;
	}
}
//---------------------------------------------------------------------------

void _fastcall TMessageForm::SetCheckMessage( String strMessage )
{
	if( strMessage.IsEmpty() ){
		m_pbCheck->Caption = L"不再提示";
	}
	else{
		m_pbCheck->Caption = strMessage;
	}
}
//---------------------------------------------------------------------------

bool TMessageForm::GetCheckState()
{
	return  m_pbCheck->Checked;
}
//---------------------------------------------------------------------------

void __fastcall TMessageForm::FormCreate(TObject *Sender)
{
	m_lblMessage->ControlStyle>>csOpaque;

	m_panGeneral->Font->Size = g_szFont9;
	m_pbCheck->Font->Size = g_szFont9;
	m_btnYes->FontSize = g_szFont9;
	m_btnOk->FontSize = g_szFont9;
	m_btnNo->FontSize = g_szFont9;
	m_btnCancel->FontSize = g_szFont9;
}
//---------------------------------------------------------------------------

