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

#include <vcl.h>
#pragma hdrstop
#include "GlobalUnit.h"
#include "UpdateFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "PNGButton"
#pragma link "GeneralDialogPanel"
#pragma resource "*.dfm"
TUpdateForm *UpdateForm;

vector< String > ItemStr;
//---------------------------------------------------------------------------
__fastcall TUpdateForm::TUpdateForm(TComponent* Owner)
	: TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TUpdateForm::FormCreate(TObject *Sender)
{
//	Left = Screen->Width / 2 - Width / 2;
//	Top  = Screen->Height / 2 - Height / 2;
//	HRGN hRgn = CreateRoundRectRgn(0, 0, Width + 1, Height + 1, 20, 20);
//	SetWindowRgn(Handle, hRgn, TRUE);
//	DeleteObject(hRgn);

	//**************测试*****************
//	String strTemp = L"TPanel位于Standard组件面板上，也是常用的一种容器控件。TPanel位于Standard组件面板上，也是常用的一种容器控件。TPanel位于Standard组件面板上，也是常用的一种容器控件。TPanel位于Standard组件面板上，也是常用的一种容器控件。";
//	ItemStr.push_back( strTemp );
//
//	strTemp = L"不管用户怎样设置窗口尺寸";
//	ItemStr.push_back( strTemp );

   //*****************************

	m_panBk->Font->Size = g_szFont9;

	ShowMSG();

	m_plUpdateMsg = new TUpdateContenPanel(this);
	m_plUpdateMsg->Parent = this;
	m_plUpdateMsg->Top = 105;
	m_plUpdateMsg->Left = 11;
	m_plUpdateMsg->Width = 400;
	m_plUpdateMsg->Height = 280;

	SetTransparent( this );
}
//---------------------------------------------------------------------------

void __fastcall TUpdateForm::WMNCHitTest(TWMNCHitTest &Msg) {
	TForm::Dispatch(&Msg);

	if (Msg.Result == HTCLIENT && Msg.XPos > Left + 10 && Msg.XPos <
		Left + Width - 20 && Msg.YPos < Top + 24)
		Msg.Result = HTCAPTION;
}
//---------------------------------------------------------------------------

void TUpdateForm::ShowMSG()
{
	TRect rtDownload;
	String strNew;
	strNew.sprintf( L"最新版本：%d.%d.%d.%d",
						   g_infoSoftwareUpdate.version.majorVersion,
						   g_infoSoftwareUpdate.version.minorVersion,
						   g_infoSoftwareUpdate.version.modifyVersion,
						   g_infoSoftwareUpdate.version.buildVersion );

	if( g_infoSoftwareUpdate.version.majorVersion==0 )
		strNew += L" 测试版";
	else
		strNew += L" 正式版";

	m_lbNewVer->Caption =  strNew.c_str();

	int wMaj, wMin, wMod, wBld;
	sscanf( g_strCurrentVersion, "%d.%d.%d.%d", &wMaj, &wMin, &wMod, &wBld );
	string strCurrent = "当前版本：";
	strCurrent += g_strCurrentVersion;

	if( wMaj==0 )
		strCurrent += " 测试版";
	else
		strCurrent += " 正式版";

	m_lbCurrentVer->Caption = strCurrent.c_str();

	string sDescribe = UTF8ToMBCS( g_infoSoftwareUpdate.strDescribe );
	String strResource = sDescribe.c_str();
	String strTemp;
	int ibg = 1;
	int i = 1;
	for( i = 1; i < strResource.Length(); i++){
		if(strResource[i]=='\n'){
			if(i!=ibg){
				strTemp = strResource.SubString( ibg,i-ibg );
				ItemStr.push_back( strTemp );
			}else{
				strTemp = "\n" ;
				ItemStr.push_back( strTemp );
			}
			ibg = i;
		}
	}

	if(i > ibg){
		strTemp = strResource.SubString( ibg,i-ibg );
		ItemStr.push_back( strTemp );
	}

	if( g_bUpdateMandatory ){
		m_lbDownloadMsg->Font->Color = RGB( 207,15,15 );//RGB( 126,177,205 );//clRed;
		m_lbDownloadMsg->Caption = "当前版本下，用户将无法进行联网操作\n建议升级到最新版本。" ;
	}
	else{
		m_lbDownloadMsg->Font->Color = RGB( 184,189,188 );//RGB( 160,183,183 );//clBlack;
		m_lbDownloadMsg->Top += 8;
		m_lbDownloadMsg->Caption = "新的版本已发布,建议立即更新。" ;
	}
}
//---------------------------------------------------------------------------


void __fastcall TUpdateForm::m_lbDownloadMouseEnter(TObject *Sender)
{
   Screen->Cursor = crHandPoint;
   m_lbDownload->Font->Color = RGB( 70,150,236 );
}
//---------------------------------------------------------------------------

void __fastcall TUpdateForm::m_lbDownloadMouseLeave(TObject *Sender)
{
	Screen->Cursor = crDefault;
	m_lbDownload->Font->Color = RGB( 67,117,233 );
}
//---------------------------------------------------------------------------

void __fastcall TUpdateForm::m_lbDownloadClick(TObject *Sender)
{
	char strURL[256];
	f_CS_GetWebPageURL( URL_SOFTWARE, strURL );
    strcat( strURL, "?id=3&os=pc" );

	ShellExecute(Handle,NULL, ((String)strURL).c_str(), NULL,NULL,SW_SHOWNORMAL);
	Close();
}
//---------------------------------------------------------------------------

void __fastcall TUpdateForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	switch( Key ){
	case VK_RETURN:
		return m_lbDownloadClick(NULL);
		break;

	case VK_ESCAPE:
		ModalResult = mrCancel;
		break;
	}
}
//---------------------------------------------------------------------------

void __fastcall TUpdateForm::PaintBoxPaint(TObject *Sender)
{
	((TPaintBox*)Sender)->Canvas->Brush->Color = RGB( 43,47,50 );      //框颜色
    ((TPaintBox*)Sender)->Canvas->FrameRect( ((TPaintBox*)Sender)->ClientRect );
}
//---------------------------------------------------------------------------


#define TOP_BUTTON	10
#define LEFT_RIGHT	10
#define STR_H_D		20				//20
#define STR_H_H		20
vector< String > LineStr;
//---------------------------------------------------------------------------
__fastcall TUpdateContenPanel::TUpdateContenPanel( TComponent* Owner )
		: Vcl::Extctrls::TPanel( Owner )
{
	m_ScrollBar = NULL;
	TopLine =0;
	ShowLine = 1;
}
//---------------------------------------------------------------------------

void __fastcall TUpdateContenPanel::CreateWnd()
{
	Vcl::Extctrls::TPanel::CreateWnd();
	Canvas->Font->Name = g_strTxtFontName;
    Canvas->Font->Size = g_szFont9;

	for( int i=0;ItemStr.size()>0 && i<ItemStr.size(); i++ ){
		//获取行 加载到 LineStr中
		if( Canvas->TextWidth(ItemStr[i]) >= Width-2*LEFT_RIGHT ){
			int ibg=1;
			int j=1;
			int Leng = ItemStr[i].Length();
			String strr1 = "";

			for(  j=1;j<Leng;j++ ){

				strr1 =  ItemStr[i].SubString( ibg,j-ibg );

				if( Canvas->TextWidth( strr1 ) >= (Width - 2*LEFT_RIGHT)-5  ){
					LineStr.push_back( strr1 );
					OutputDebugString((strr1 + "-111--------------").c_str());
					ibg = j;
				}
			}

			strr1 =  ItemStr[i].SubString( ibg,j-ibg+1 );
			LineStr.push_back( strr1 );
			OutputDebugString((strr1 + "--222-------------").c_str());
		}
		else{
			String strr1 = ItemStr[i];
			LineStr.push_back( ItemStr[i] );
			OutputDebugString((ItemStr[i] + "-333--------------").c_str());
		}
	}

	ShowLine = (Height - 2*TOP_BUTTON)/STR_H_D;

	if( LineStr.size()>ShowLine  ){
		m_ScrollBar = new TSkinScrollBar(this);
		m_ScrollBar->m_pWndCtrl = this;
		m_ScrollBar->Parent = this;
		m_ScrollBar->Left = Width - m_ScrollBar->Width - 1;
		m_ScrollBar->Top = 1;
		m_ScrollBar->Height = Height-2;
		m_ScrollBar->SetRange( 0,LineStr.size()-1 );
		m_ScrollBar->SetPageSize(ShowLine);
		m_ScrollBar->SetPosition( TopLine );
		m_ScrollBar->Visible = true;
	}
}
//---------------------------------------------------------------------------

__fastcall TUpdateContenPanel::~TUpdateContenPanel()
{
	if( m_ScrollBar ){
		delete m_ScrollBar;
		m_ScrollBar = NULL;
	}
}
//---------------------------------------------------------------------------

void __fastcall TUpdateContenPanel::WMEraseBkgnd(TWMEraseBkgnd &Message)
{
	TRect rect = GetClientRect();

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

	tempCanvas->Brush->Color = RGB( 58,63,69 );		//背景颜色
	tempCanvas->FillRect( rect );

	tempCanvas->Brush->Color = RGB( 43,47,50 );       //边框
	tempCanvas->FrameRect( rect );

	delete tempCanvas;
	tempCanvas = NULL;
}

void __fastcall TUpdateContenPanel::WMVScroll(Winapi::Messages::TWMScroll &Message)
{
	int minpos, maxpos, page, curpos;
	m_ScrollBar->GetScrollRange(&minpos, &maxpos);
	page = m_ScrollBar->GetPageSize();
	// Get the current position of scroll box.
	curpos = m_ScrollBar->GetScrollPos();

	// Determine the new position of scroll box.
	switch (Message.ScrollCode)
	{
	case SB_TOP:      // Scroll to far top.
		curpos = minpos;
		break;

	case SB_BOTTOM:      // Scroll to far bottom.
		curpos = maxpos-page+1;
		break;

	case SB_ENDSCROLL:   // End scroll.
		break;

	case SB_LINEUP:      // Scroll left.
		if (curpos > minpos)
			curpos--;
		break;

	case SB_LINEDOWN:   // Scroll right.
		if (curpos < maxpos-page+1)
			curpos++;
		break;

	case SB_PAGEUP:    // Scroll one page left.
		if (curpos > minpos)
			curpos = (minpos > curpos-page) ? minpos : curpos-page;
			//max(minpos, curpos-page);
		break;

	case SB_PAGEDOWN:      // Scroll one page right.
		if( curpos<maxpos )
			curpos = (maxpos-page+1 < curpos+page) ? maxpos-page+1 : curpos+page;
			//min(maxpos-page+1, curpos+page);
		break;

	case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
		curpos = Message.Pos;      // of the scroll box at the end of the drag operation.
		break;

	case SB_THUMBTRACK:   // Drag scroll box to specified position. nPos is the
		curpos = Message.Pos;     // position that the scroll box has been dragged to.
		break;
	}

	// Set the new position of the thumb (scroll box).
	if( Message.ScrollBar!=m_ScrollBar->Handle ){
		m_ScrollBar->SetPosition(curpos);
//		Paint();
	}
	if( TopLine != curpos ){
		TopLine = curpos;
		Invalidate();
	}

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

void __fastcall TUpdateContenPanel::WMMouseWheel(TWMMouseWheel &Message)
{
	if( m_ScrollBar->Visible ){
		UINT nSBCode;

		if( Message.WheelDelta>0 ) nSBCode = SB_LINEUP;
		else nSBCode = SB_LINEDOWN;

		Perform( WM_VSCROLL, MAKELONG(nSBCode,0), NULL );
	}
}
//---------------------------------------------------------------------------

void __fastcall TUpdateContenPanel::Paint(void)
{
	Canvas->Brush->Style = bsClear;
	Canvas->Font->Color = RGB( 184,189,188 );             //Content字体颜色
	int ContentHeight=0;
//	int iShowLine = 0;
	for( int i=TopLine;LineStr.size()>0 && i<LineStr.size() && i<=TopLine + ShowLine-1; i++ ){
		Canvas->TextOutW( LEFT_RIGHT,TOP_BUTTON + ContentHeight, LineStr[i] );
		ContentHeight +=STR_H_H;
		//截取字符串换行
		/*
		if( Canvas->TextWidth(ItemStr[i]) >= Width-2*LEFT_RIGHT ){
			int ibg=1;
			int j=1;
			int Leng = ItemStr[i].Length();
			String strr1 = "";

			for(  j=1;j<Leng;j++ ){

				strr1 =  ItemStr[i].SubString( ibg,j-ibg );

				if( Canvas->TextWidth( strr1 ) >= (Width - 2*LEFT_RIGHT)-5  ){

					if( iShowLine<ShowLine ){
						Canvas->TextOutW( LEFT_RIGHT,TOP_BUTTON + ContentHeight, strr1 );

						ContentHeight +=STR_H_H;
						iShowLine++;
						ibg = j;
					}
				}
			}
			if( iShowLine<ShowLine ){
				strr1 =  ItemStr[i].SubString( ibg,j-ibg+1 );
				Canvas->TextOutW( LEFT_RIGHT,TOP_BUTTON + ContentHeight, strr1 );

				ContentHeight +=STR_H_D;
				iShowLine++;
			}
		}
		else{
			if( iShowLine<ShowLine ){
				Canvas->TextOutW( LEFT_RIGHT,TOP_BUTTON + ContentHeight, ItemStr[i] );
				ContentHeight += STR_H_D;
				iShowLine++;
			}
		}  */
	}

}
//---------------------------------------------------------------------------



