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

#include "CustomerSettingPanelUnit.h"
#include "GlobalUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
#define SCOLLBAR_WIDTH    	  (14)
#define TOP_BUTTOM	      	  (10)
#define LEFT_RIGHT	      	  (SCOLLBAR_WIDTH + 20)

const static COLORREF
		CLR_TXT_HOT 			    = RGB(171, 186, 207),
//		CLR_BKG_INTERNAL 	        = RGB(65,  70,  77),
		CLR_BKG_INTERNAL 	        = RGB(47, 50, 56),
		CLR_BKG_INTERNAL_LIGHT 	    = RGB(73, 77, 86),
		CLR_CONTENT_DB_LINE_DARK    = RGB(32, 35, 39),
		CLR_CONTENT_DB_LINE_LIGHT   = RGB(76, 81, 87);

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

__fastcall TCustmerSettingPanel::TCustmerSettingPanel( TComponent* Owner)
		: Vcl::Extctrls::TPanel( Owner )
{
    Color = CLR_BKG_INTERNAL;

	m_ScrollBar = NULL;
	m_iTopLine  =0;
	m_iShowLine = 1;
}
//---------------------------------------------------------------------------

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

	m_ckInitProject   = new TCKBox(this);
	m_ckDownloadStyle = new TCKBox(this);
	m_ckPreBeat		  = new TCKBox(this);
	m_ckSegColor	  = new TCKBox(this);
	m_cbChordMode     = new TComboBoxComponent(this);
	m_cbPreStage      = new TComboBoxComponent(this);

	m_ckInitProject->Parent   = this;
	m_ckDownloadStyle->Parent = this;
	m_ckPreBeat->Parent 	  = this;
	m_ckSegColor->Parent 	  = this;
	m_cbChordMode->Parent     = this;
	m_cbPreStage->Parent      = this;

	m_ckInitProject->Font->Size = g_szFont9;
	m_ckDownloadStyle->Font->Size = g_szFont9;
	m_ckPreBeat->Font->Size = g_szFont9;
	m_ckSegColor->Font->Size = g_szFont9;
	m_cbChordMode->SetFontSize(g_szFont9);
	m_cbPreStage->SetFontSize(g_szFont9);

	m_ckInitProject->Font->Color = CLR_TXT_HOT;
	m_ckDownloadStyle->Font->Color = CLR_TXT_HOT;
	m_ckPreBeat->Font->Color = CLR_TXT_HOT;
	m_ckSegColor->Font->Color = CLR_TXT_HOT;

	Canvas->Font->Assign(m_ckPreBeat->Font);

	m_ckInitProject->Caption   = L"启动时自动打开上次工程";
	m_ckDownloadStyle->Caption = L"自动下载风格";
	m_ckPreBeat->Caption = L"录音时使用预设风格节拍提示";
	m_ckSegColor->Caption = L"按乐器数显示段落颜色";

	m_ckInitProject->Width    = 180;
	m_ckDownloadStyle->Width  = 120;
	m_ckPreBeat->Width 		  = 180;
	m_ckSegColor->Width    = 180;

	m_cbChordMode->AddItem(L"级数");
	m_cbChordMode->AddItem(L"英文");
	m_cbChordMode->Height = 24;

	m_cbPreStage->AddItem(L"M");
	m_cbPreStage->AddItem(L"IME");
	m_cbPreStage->AddItem(L"IMIME");
	m_cbPreStage->Height = 24;

	m_ckInitProject->OnChange   = CKBoxChange;
	m_ckDownloadStyle->OnChange = CKBoxChange;
	m_ckPreBeat->OnChange 		= CKBoxChange;
	m_ckSegColor->OnChange 		= CKBoxChange;
	m_cbChordMode->OnChange     = ChordModeChange;
	m_cbPreStage->OnChange      = PreStageChange;

	if( SOFTWARE_ID==e_Pro_Lite )
        m_ckDownloadStyle->Enabled = false;

	SetShowLine();
}
//---------------------------------------------------------------------------

__fastcall TCustmerSettingPanel::~TCustmerSettingPanel()
{
	if( m_ScrollBar ){
		delete m_ScrollBar;
		m_ScrollBar = NULL;
	}

	if( m_ckInitProject ){
		delete m_ckInitProject;
		m_ckInitProject = NULL;
	}

	if( m_ckDownloadStyle ){
		delete m_ckDownloadStyle;
		m_ckDownloadStyle = NULL;
	}

	if( m_ckPreBeat ){
		delete m_ckPreBeat;
		m_ckPreBeat = NULL;
	}

	if( m_ckSegColor ){
		delete m_ckSegColor;
		m_ckSegColor = NULL;
	}

	if( m_cbChordMode ){
		delete m_cbChordMode;
		m_cbChordMode = NULL;
	}

	if( m_cbPreStage ){
		delete m_cbPreStage;
		m_cbPreStage = NULL;
	}
}
//---------------------------------------------------------------------------

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

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

	tempCanvas->Brush->Color = CLR_BKG_INTERNAL;		//背景颜色
	tempCanvas->FillRect( rect );
	tempCanvas->Brush->Color = CLR_CONTENT_DB_LINE_DARK;
	tempCanvas->FrameRect( rect );

	delete tempCanvas;
	tempCanvas = NULL;

//	DefaultHandler( &Message );
}
//---------------------------------------------------------------------------

void __fastcall TCustmerSettingPanel::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( m_iTopLine != curpos ){
		m_iTopLine = curpos;
		Invalidate();
	}

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

void __fastcall TCustmerSettingPanel::WMMouseWheel(TWMMouseWheel &Message)
{
	if( m_ScrollBar && 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 TCustmerSettingPanel::SetShowLine()
{
	int totalLine = LINE_NUM;
	m_iShowLine = Height/LINE_HEIGHT;

	if( totalLine>m_iShowLine  ){
		m_ScrollBar = new TSkinScrollBar(this);
		m_ScrollBar->m_pWndCtrl = this;
		m_ScrollBar->Parent = this;
		m_ScrollBar->Height = Height-2;
		m_ScrollBar->Left = Width - m_ScrollBar->Width - 1;
		m_ScrollBar->Top = 1;
	}
}
//---------------------------------------------------------------------------

void __fastcall TCustmerSettingPanel::SetContent()
{
/*	m_ckInitProject->Width    = 155;
	m_ckDownloadStyle->Width  = 95;
	m_ckPreBeat->Width 		  = 180;
*/
	m_bAutoOpenProject    = g_bAutoOpenProject;
	m_bDownloadStyle      = g_bAutoDownloadStyle;
	m_bPreBeat			  = g_bPrebeat;
	m_bAlphaKey 		  = g_bAlphaKey;
	m_bSegColor			  = g_iSegColorMode==1;
	m_cPreStage  		  = g_iPreStageDefault;

	m_ckInitProject->Checked    = m_bAutoOpenProject;
	m_ckDownloadStyle->Checked  = m_bDownloadStyle;
	m_ckPreBeat->Checked 		= m_bPreBeat;
	m_ckSegColor->Checked 		= m_bSegColor;
	m_cbChordMode->DefItemIndex = g_bAlphaKey;
	m_cbPreStage->DefItemIndex  = g_iPreStageDefault;

	if(m_ScrollBar) {
		m_ScrollBar->SetRange( 0,LINE_NUM-1 );
		m_ScrollBar->SetPageSize(m_iShowLine);
		m_ScrollBar->SetPosition( m_iTopLine );
		m_ScrollBar->Visible = true;
	}
}
//---------------------------------------------------------------------------

void __fastcall TCustmerSettingPanel::Paint(void)
{
	int curX		  = 0;
	int curY		  = 0;
	int contentHeight = LINE_HEIGHT;
	int contentStart  = -m_iTopLine*LINE_HEIGHT;
	TSize sizeFont;
	String strText;

	Canvas->Font->Color  = CLR_TXT_HOT;
	Canvas->Brush->Style = bsClear;

	/////////////////////////////////////////////////
	curX = LEFT_RIGHT;
	curY = contentStart;
	m_ckInitProject->Left = curX;
	m_ckInitProject->Top  = curY + (contentHeight - m_ckInitProject->Height)/2 + 2;

	/////////////////////////////////////////////////
	contentStart += LINE_HEIGHT;
	curY = contentStart;

	m_ckDownloadStyle->Left = curX;
	m_ckDownloadStyle->Top  = curY + (contentHeight - m_ckDownloadStyle->Height)/2;

	/////////////////////////////////////////////////
	contentStart += LINE_HEIGHT;
	curY = contentStart;

	m_ckPreBeat->Left = curX;
	m_ckPreBeat->Top  = curY + (contentHeight - m_ckPreBeat->Height)/2;

	/////////////////////////////////////////////////
	contentStart += LINE_HEIGHT;
	curY = contentStart;

	m_ckSegColor->Left = curX;
	m_ckSegColor->Top  = curY + (contentHeight - m_ckSegColor->Height)/2;

	/////////////////////////////////////////////////
	contentStart += LINE_HEIGHT;
	curX = LEFT_RIGHT + 5;

	strText = L"和弦显示方式：";
	sizeFont = Canvas->TextExtent(strText);
	curY = contentStart + (contentHeight - sizeFont.Height)/2;
	Canvas->TextOutW(curX, curY, strText);

	curX += sizeFont.Width + 10;
	curY = contentStart;
	m_cbChordMode->Left = curX;
	m_cbChordMode->Top  = curY + (contentHeight - m_cbChordMode->Height)/2;

	strText = L"  *    示例：";
	sizeFont = Canvas->TextExtent(strText);
	curX += m_cbChordMode->Width + 10;
	curY = contentStart + (contentHeight - sizeFont.Height)/2;
	Canvas->TextOutW(curX, curY, strText);

	curX += sizeFont.Width;

	TFont *fntOld = new TFont;
	fntOld->Assign( Canvas->Font );

	Canvas->Font->Name = "Times New Roman";
	Canvas->Font->Quality = TFontQuality::fqClearTypeNatural;
	Canvas->Font->Color = CLR_TXT_HOT;
	Canvas->Font->Style = TFontStyles()<<fsItalic<<fsBold;
	Canvas->Font->Size = g_szFont16;


	strText = m_bAlphaKey ? L"A" : L"VI";
	sizeFont = Canvas->TextExtent(strText);
	curY = contentStart + (contentHeight - sizeFont.Height)/2;
	Canvas->TextOutW(curX, curY, strText);

	curX += sizeFont.Width;
	curY += 7;
	strText = L"m";
	Canvas->Font->Size = g_szFont9;
	Canvas->TextOutW(curX, curY, strText);

	Canvas->Font->Assign( fntOld );
	delete fntOld;

	/////////////////////////////////////////////////
	contentStart += LINE_HEIGHT;
	curX = LEFT_RIGHT + 5;

	strText = L"预设段落结构：";
	sizeFont = Canvas->TextExtent(strText);
	curY = contentStart + (contentHeight - sizeFont.Height)/2;
	Canvas->TextOutW(curX, curY, strText);

	curX += sizeFont.Width + 10;
	curY = contentStart;
	m_cbPreStage->Left = curX;
	m_cbPreStage->Top  = curY + (contentHeight - m_cbPreStage->Height)/2;

	strText = L"  *    I-前奏、M-旋律、E-尾奏";
	sizeFont = Canvas->TextExtent(strText);
	curX += m_cbPreStage->Width + 10;
	curY = contentStart + (contentHeight - sizeFont.Height)/2;
	Canvas->TextOutW(curX, curY, strText);
}
//---------------------------------------------------------------------------
void __fastcall TCustmerSettingPanel::CKBoxChange(TObject* Sender)
{
	TCKBox *tmpCK = (TCKBox *)Sender;

	if( tmpCK == m_ckInitProject )
		m_bAutoOpenProject = tmpCK->Checked;
	else if( tmpCK == m_ckDownloadStyle )
		m_bDownloadStyle = m_ckDownloadStyle->Checked;
	else if( tmpCK == m_ckPreBeat )
		m_bPreBeat = m_ckPreBeat->Checked;
	else if( tmpCK == m_ckSegColor ){
		m_bSegColor = m_ckSegColor->Checked;
		Application->MainForm->Perform( UM_PREFERCHANGED, 0, m_bSegColor ? 1 : 0 );
	}
}
//---------------------------------------------------------------------------

void __fastcall TCustmerSettingPanel::ChordModeChange(TComboBoxItem* Sender, int Index)
{
	  m_bAlphaKey = Index!=0;
	  this->Invalidate();

	  Application->MainForm->Perform( UM_PREFERCHANGED, 1, m_bAlphaKey );
}
//---------------------------------------------------------------------------

void __fastcall TCustmerSettingPanel::PreStageChange(TComboBoxItem* Sender, int Index)
{
	  m_cPreStage = Index;
}
//---------------------------------------------------------------------------

