//---------------------------------------------------------------------------
#include <vcl.h>
#include <Vcl.Imaging.pngimage.hpp>
#pragma hdrstop

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

#define MIDI_EVENT_PROGRAM	2
#define MIDI_EVENT_VOLUME	12

#define INSERT_HEIGHT		10

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

extern COLORREF clrTextStyle, //Style文字
			clrTextHot,	//高亮文字
			clrTextDisable,	//未下载文字
			clrBackground, //面板背景
			clrFocback, //焦点背景
			clrSelback; //选中背景

extern TPngImage *u_imgFold,
		*u_imgUnfold,
		*u_imgTrackOn,
		*u_imgTrackOff,
		*u_imgDelete,
		*u_imgDownload,
		*u_imgDownloading,
		*u_imgProg,
		*u_imgTexture,
		*u_imgVolume,
		*u_imgBrush,
		*u_imgCollect,
		*u_imgUnCollect,
		*u_imgScore,
		*u_imgSave,
		*u_imgImport,
		*u_imgUpload,
		*u_imgCharge;

const BYTE u_arrChordPreset[2][4][4] = { //预设和弦
			{{0,37,9,30},{0,37,20,28},{0,9,20,28},{0,17,37,30}},
			{{37,9,28,16},{37,17,20,28},{37,0,9,17},{37,28,20,16}} };

/////////////////////////////////////////////////////////////////////////////
// TStylePanel
void TStylePanel::LoadImages()
{
	if( u_imgFold )
		delete u_imgFold;
	if( u_imgUnfold )
		delete u_imgUnfold;
	if( u_imgTrackOn )
		delete u_imgTrackOn;
	if( u_imgTrackOff )
		delete u_imgTrackOff;
	if( u_imgDelete )
		delete u_imgDelete;
	if( u_imgBrush )
		delete u_imgBrush;
	if( u_imgVolume )
		delete u_imgVolume;
	if( u_imgDownload )
		delete u_imgDownload;
	if( u_imgProg )
		delete u_imgProg;
	if( u_imgTexture )
		delete u_imgTexture;
	if( u_imgCollect )
		delete u_imgCollect;
	if( u_imgUnCollect )
		delete u_imgUnCollect;
	if( u_imgScore )
		delete u_imgScore;
	if( u_imgSave )
		delete u_imgSave;
	if( u_imgImport )
		delete u_imgImport;
	if( u_imgUpload )
		delete u_imgUpload;
	if( u_imgCharge )
		delete u_imgCharge;

	u_imgFold = new TPngImage();
	u_imgFold->LoadFromResourceName(int(HInstance), L"imgFold");

	u_imgUnfold = new TPngImage();
	u_imgUnfold->LoadFromResourceName(int(HInstance), L"imgUnfold");

	u_imgTrackOn = new TPngImage();
	u_imgTrackOn->LoadFromResourceName(int(HInstance), L"imgTrackOn");

	u_imgTrackOff = new TPngImage();
	u_imgTrackOff->LoadFromResourceName(int(HInstance), L"imgTrackOff");

	u_imgDelete = new TPngImage();
	u_imgDelete->LoadFromResourceName(int(HInstance), L"imgDelete");

	u_imgBrush = new TPngImage();
	u_imgBrush->LoadFromResourceName(int(HInstance), L"imgBrush");

	u_imgDownload = new TPngImage();
	u_imgDownload->LoadFromResourceName(int(HInstance), L"imgDownload");

	u_imgProg = new TPngImage();
	u_imgProg->LoadFromResourceName(int(HInstance), L"imgProg");

	u_imgTexture = new TPngImage();
	u_imgTexture->LoadFromResourceName(int(HInstance), L"imgTexture");

	u_imgVolume = new TPngImage();
	u_imgVolume->LoadFromResourceName(int(HInstance), L"imgVolume");

	u_imgCollect = new TPngImage();
	u_imgCollect->LoadFromResourceName(int(HInstance), L"imgCollect");

	u_imgUnCollect = new TPngImage();
	u_imgUnCollect->LoadFromResourceName(int(HInstance), L"imgUnCollect");

	u_imgScore = new TPngImage();
	u_imgScore->LoadFromResourceName(int(HInstance), L"imgStar");

	u_imgSave = new TPngImage();
	u_imgSave->LoadFromResourceName(int(HInstance), L"imgStyleSave");

	u_imgImport = new TPngImage();
	u_imgImport->LoadFromResourceName(int(HInstance), L"imgAddSegment");

	u_imgUpload = new TPngImage();
	u_imgUpload->LoadFromResourceName(int(HInstance), L"imgStyleUpload");

	u_imgCharge = new TPngImage();
	u_imgCharge->LoadFromResourceName(int(HInstance), L"imgStyleCharge");
}

void TStylePanel::FreeImages()
{
	if( u_imgFold ){
		delete u_imgFold;
		u_imgFold = NULL;
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

__fastcall TStylePanel::TStylePanel(TComponent* AOwner)
	: TToolpalPanel(AOwner)
{
	m_iType = TPTYPE_STYLE;

	m_aItems = new System::Classes::TList();

	m_pTimer = NULL;
	m_nItemHeight = OPTION_HEIGHT;

	m_pTopItem = m_pFocItem = m_pSelItem = m_pPlayItem = NULL;
	m_pDragItem = m_pDropItem = m_pHoverItem = NULL;
	m_iFocLine = m_iSelLine = m_iHoverLine = -1;
	m_nTotalLine = m_iTopLine = 0;
	m_iFocCol = 0;
	m_bPlaying = false;

	m_VolTrackBar = NULL;
	m_editName = NULL;

//	m_Hint = NULL;
//	m_HintTimer = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TStylePanel::CreateWnd(void)
{
	TToolpalPanel::CreateWnd();

	OnDblClick = DoubleClick;
	OnMouseLeave = MouseLeave;

	m_sbVert = new TSkinScrollBar(NULL);
	m_sbVert->Parent = this;
	m_sbVert->m_pWndCtrl = this;
	m_sbVert->Visible = false;
	m_sbVert->Left = ClientWidth - m_sbVert->Width - 1;
	m_sbVert->Top = m_nTitleHeight + 1;
	m_sbVert->Height = ClientHeight - m_nTitleHeight - 2;

	Canvas->Font->Name = g_strTxtFontName;//"Times New Roman";
	Canvas->Font->Quality = TFontQuality::fqClearTypeNatural;
//	if( !strcmp( g_strTxtFontName, "微软雅黑" ) ){
//		Canvas->Font->Size = g_szFont9;
//	}
//	else{
//		Canvas->Font->Size = g_szFont8;
//	}
	Canvas->Font->Size = g_szFont9;

	CalcViewArea();

	InitItems();

//	m_PopupMenu = NULL;
}
//---------------------------------------------------------------------------

__fastcall TStylePanel::~TStylePanel()
{
//	if( m_PopupMenu )
//		delete m_PopupMenu;

	ClearAllItems();

	delete m_aItems;

	if( m_pTimer )
		delete m_pTimer;

//	if( m_Hint )
//		delete m_Hint;
//
//	if( m_HintTimer )
//		delete m_HintTimer;

	if( m_editName )
		delete m_editName;

	if( m_VolTrackBar )
		delete m_VolTrackBar;
}
//---------------------------------------------------------------------------

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

	if( ( Msg.message==WM_LBUTTONDOWN || Msg.message==WM_RBUTTONDOWN )
					&& Msg.hwnd!=Handle
					&& !( Msg.hwnd==m_PopMenu->Handle
						|| m_editName && Msg.hwnd==m_editName->Handle
						|| m_VolTrackBar && Msg.hwnd==m_VolTrackBar->Handle )){
		SetFocusedItem( NULL, -1, 0 );
	}

	TToolpalPanel::DoHandleMessage( Msg, Handled );
}
//---------------------------------------------------------------------------

void TStylePanel::ClearAllItems()
{
	Stop( STOP_SELF );

	m_pTopItem = m_pFocItem = m_pSelItem = NULL;
	m_pDragItem = m_pHoverItem = NULL;
	m_iFocLine = m_iSelLine = m_iHoverLine = -1;
	m_nTotalLine = m_iTopLine = 0;
	m_iFocCol = 0;

	for( int i=0; i<m_aItems->Count; i++ ){
		delete m_aItems->Items[i];
	}
	m_aItems->Clear();

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

	if( m_editName && m_editName->Visible ){
		m_editName->Hide();
	}
}
//---------------------------------------------------------------------------

void TStylePanel::CalcViewArea()
{
	m_nItemWidth = ClientWidth - 2;
	m_nItemShow = (ClientHeight - m_nTitleHeight - 1) / m_nItemHeight;
	if( ((ClientHeight - m_nTitleHeight - 1) % m_nItemHeight ) > 3 )
		m_nItemShow++;
}
//---------------------------------------------------------------------------

void TStylePanel::ScrollView( int nPos, bool bRedraw )
{
	if( m_iTopLine!=nPos ){
		if( m_iTopLine<nPos ){
			for( int i=m_iTopLine; i<nPos; i++ ){
				if( m_pTopItem ){
					m_pTopItem = GetNextItem( m_pTopItem );
				}
				else{
					break;
				}
			}
		}
		else{
			for( int i=nPos; i<m_iTopLine; i++ ){
				if( m_pTopItem ){
					m_pTopItem = GetPrevItem( m_pTopItem );
				}
				else{
					break;
                }
			}
		}

		if( m_pSelItem )
			m_iSelLine += m_iTopLine - nPos;

		if( m_pHoverItem )
			m_iHoverLine += m_iTopLine - nPos;

		SetFocusedItem( NULL, -1, 0 );

		m_iTopLine = nPos;

		Paint();
	}

	if( bRedraw ){
		m_sbVert->SetPosition( nPos );
	}
}
//---------------------------------------------------------------------------

TStyleItem* __fastcall TStylePanel::AddStyle( TPROSTYLE* info, DWORD opts )
{
	TStyleItem* pItem = new TStyleItem( info, opts );

	if( m_aItems->Count>0 ){
		TCommonItem *pLast = (TCommonItem*)m_aItems->Last();

		pLast->next = pItem;
		pItem->prev = pLast;
	}
	else{
		m_pTopItem = pItem;
	}

	m_aItems->Add( pItem );

	m_nTotalLine++;

	return pItem;
}
//---------------------------------------------------------------------------

TCommonItem* TStylePanel::GetItem( int iSty, int iSeg, int iTrk )
{
	TCommonItem* pItem = NULL;
	if( iSty<m_aItems->Count ){
		TStyleItem* pStyItem = (TStyleItem*)m_aItems->Items[iSty];
		if( iSeg<0 ){
			return pStyItem;
		}
		else if( iSeg < pStyItem->aItems->Count ){
			TSegmentItem* pSeg = (TSegmentItem*)pStyItem->aItems->Items[iSeg];
			if( iTrk<0 ){
				return pSeg;
			}
			else if( iTrk < pSeg->aItems->Count ){
				return (TTrackItem*)pSeg->aItems->Items[iTrk];
			}
		}
	}

	return NULL;
}
//---------------------------------------------------------------------------

TCommonItem* TStylePanel::GetNextItem( TCommonItem* pItem )
{
	TCommonItem* pNextItem = NULL;

	if( (pItem->dwState & STATE_UNFOLD) && pItem->aItems->Count>0 ){
		pNextItem = (TCommonItem*)pItem->aItems->First();
	}
	else if( pItem->next ){
		pNextItem = pItem->next;
	}
	else if( pItem->up && pItem->up->next ){
		pNextItem = pItem->up->next;
	}
	else if( pItem->up && pItem->up->up && pItem->up->up->next ){
		pNextItem = pItem->up->up->next;
	}

	return pNextItem;
}
//---------------------------------------------------------------------------

TCommonItem* TStylePanel::GetPrevItem( TCommonItem *pItem )
{
	TCommonItem* pPrevItem = pItem->prev;

	if( pPrevItem ){
		while( pPrevItem->aItems->Count>0 && (pPrevItem->dwState & STATE_UNFOLD) ){
			pPrevItem = (TCommonItem*)pPrevItem->aItems->Last();
		}
	}
	else{
		pPrevItem = pItem->up;
	}

	return pPrevItem;
}
//---------------------------------------------------------------------------

TCommonItem* TStylePanel::HitTest( int X, int Y, WORD* iCol )
{
	if( Y<0 || Y>Height-1 )
		return NULL;

	TCommonItem* pItem = m_pTopItem,
				*pHitItem = NULL;
	int h = m_nItemHeight;

	for( int i=0; i<m_nItemShow && pItem; i++ ){
		if( Y<h ){
			pHitItem = pItem;
			break;
		}

		pItem = GetNextItem( pItem );
		h += m_nItemHeight;
	}

	if( pHitItem ){
		int leftFold = LEFT_BOUND,
			rightFold = leftFold + OPTION_WIDTH,
			leftText = leftFold + OPTION_WIDTH,
			rightText = Width - RIGHT_BOUND - OPTION_WIDTH * pHitItem->aOptions->Count,
			offset = 0;

		if( pHitItem->iType!=SEGTYPE_STYLE ){
			offset += OPTION_WIDTH;

			if( pHitItem->iType==SEGTYPE_TRACK ){
			   offset += OPTION_WIDTH;
			}
		}

		leftFold += offset;
		rightFold += offset;
		leftText += offset;
//		rightText += offset;

		WORD col = 0;
		if( X<rightFold && X>leftFold ){
			col = COL_FOLD;
		}
		else if( X>leftText && X<rightText ){
			col = COL_TEXT;
		}
		else if( X>rightText && X<Width-RIGHT_BOUND ){
			int l = rightText,
				r = l + OPTION_WIDTH;
			for( int i=0; i<OPT_COL_NUM; i++ ){
				if( X>l && X<=r ){
					col = (COL_OPT<<(pHitItem->aOptions->Count-i-1));
					break;
				}

				l = r;
				r += OPTION_WIDTH;
			}
		}
		else{
			pHitItem = NULL;
		}

		if( iCol )
			*iCol = col;
	}

	return pHitItem;
}
//---------------------------------------------------------------------------

void TStylePanel::InitItems()
{
	//清空内容
	ClearAllItems();

	Init();

	ResetScrollBar();
	Paint();
}
//---------------------------------------------------------------------------

void __fastcall TStylePanel::Paint()
{
	TCommonItem* pItem = m_pTopItem;

	for( int i=0; i<m_nItemShow; i++ ){
		DrawItem( pItem, i, -1 );

		if( pItem )
			pItem = GetNextItem( pItem );
	}

	TToolpalPanel::Paint();
}
//---------------------------------------------------------------------------

void TStylePanel::ResetScrollBar()
{
	int page = m_nItemShow;
	if( ((ClientHeight - m_nTitleHeight - 1) % m_nItemHeight ) > 3 )
		page--;

	if( m_nTotalLine>page ){ //显示滚动条
		int range = m_nTotalLine;

		m_sbVert->SetRange( 0, range-1 );
		m_sbVert->SetPageSize( page );
		m_sbVert->SetPosition( 0 );

		int iRowScroll = 0, iTopLine = m_iTopLine;
		if( iTopLine>0 ){
			iRowScroll = min( iTopLine, range-page );
			if( iRowScroll<0 ){
				iRowScroll = 0;
			}
		}

		ScrollView( iRowScroll );
	}
	else{
		m_sbVert->SetRange( 0, 0, false );
		m_sbVert->SetPageSize( 0, false );
		m_sbVert->SetPosition( 0, false );

		if( m_iTopLine!=0 ){
			m_iTopLine = 0;
			m_pTopItem = m_aItems->Count>0 ? (TCommonItem*)m_aItems->Items[0] : NULL;
			Paint();
		}
	}
}
//---------------------------------------------------------------------------

void TStylePanel::ShowSelItem()
{
	bool bPaint = true;
	if( m_pSelItem ){
		if( m_iSelLine >= m_nItemShow ){ //需要向下滚动
			ScrollView( m_iSelLine + m_iTopLine - m_nItemShow + 1, true );
			bPaint = false;
		}
		else if( m_iSelLine<0 ){ //需要向上滚动
			ScrollView( m_iSelLine + m_iTopLine, true );
			bPaint = false;
		}
	}

	if( bPaint ){
		Paint();
	}
}
//---------------------------------------------------------------------------

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

	CalcViewArea();

	if( m_sbVert ){
		m_sbVert->Left = Width - m_sbVert->Width - 1;
		m_sbVert->Top = m_nTitleHeight + 1;
		m_sbVert->Height = Height - m_nTitleHeight - 2;

		ResetScrollBar();
	}
}
//---------------------------------------------------------------------------

void __fastcall TStylePanel::WMVScroll(Winapi::Messages::TWMScroll &Message)
{
	if( !m_sbVert->IsOperable() )
		return;

	int minpos, maxpos, page, curpos;
	m_sbVert->GetScrollRange(&minpos, &maxpos);
	page = m_sbVert->GetPageSize();
	// Get the current position of scroll box.
	curpos = m_sbVert->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 = max(minpos, curpos-page);
		break;

	case SB_PAGEDOWN:      // Scroll one page right.
		if( curpos<maxpos-page+1 )
			curpos = 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_sbVert->Handle )
		m_sbVert->SetPosition(curpos);

	ScrollView(curpos, FALSE);

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

void TStylePanel::SetSelect( TCommonItem *pItem, int iLine )
{
	if( pItem!=m_pSelItem ){
		if( m_bPlaying )
			Stop();

		if( m_pSelItem ){
			m_pSelItem->Select( false );

			if( m_iSelLine>=0 && m_iSelLine<m_nItemShow ){ //需要重绘选中项
				DrawItem( m_pSelItem, m_iSelLine, -1 );
			}
		}

		if( pItem ){
			pItem->Select( true );

			if( iLine>=0 && iLine<m_nItemShow ){
				DrawItem( pItem, iLine, -1 );
            }
		}

		m_pSelItem = pItem;
		m_iSelLine = iLine;
	}
}
//---------------------------------------------------------------------------

void TStylePanel::SetCollect(TCommonItem* pStyle)
{
	int idLoc = ((TStyleItem*)pStyle)->pStyle->idLoc,
		idStyle = ((TStyleItem*)pStyle)->pStyle->idStyle;

	int index=-1;
	bool bCollected = false;

	for( int i=0; i<g_aCollectStyles.size(); i++ ){
		if( g_aCollectStyles[i]->idLoc==idLoc && g_aCollectStyles[i]->idStyle==idStyle ){
			bCollected = true;
			index = i;
			break;
		}
	}

//	pStyle->SetCollect( !bCollected );

	if( bCollected ){
		//删除Collect 容器内容
		g_aCollectStyles.erase( g_aCollectStyles.begin() + index );
		f_CM_SetCollectionStyle( false, idLoc, idStyle );
	}
	else{
		//添加进容器
		g_aCollectStyles.push_back( ((TStyleItem*)pStyle)->pStyle );
		f_CM_SetCollectionStyle( true, idLoc, idStyle );
	}
}

void __fastcall TStylePanel::WMMouseWheel(TWMMouseWheel &Message)
{
	if( m_editName && m_editName->Visible )
		return;

	if( m_VolTrackBar ){
//		if( m_Hint )
//			m_Hint->ReleaseHandle();    //删除hint

		m_VolTrackBar->Perform( WM_MOUSEWHEEL, MAKELONG(0,Message.WheelDelta), NULL );
	}
	else if( m_sbVert->IsOperable() ){
//		if( m_Hint )
//			m_Hint->ReleaseHandle();	//删除hint

		UINT nSBCode;

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

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

void __fastcall TStylePanel::UMMouseMove(TWMMouseMove &Message)
{
	TShiftState Shift(Message.Keys);

	if( m_pDragItem ){
		if( abs(Message.XPos-ptDown.X)>=4
					|| abs(Message.YPos-ptDown.Y)>=4 ){ //开始拖动
			DrawDragImg( m_pDragItem );

			int left = LEFT_BOUND,
				iLine = (ptDown.Y - m_nTitleHeight)/m_nItemHeight,
				top = iLine * m_nItemHeight + m_nTitleHeight;

			if( m_pDragItem->iType==SEGTYPE_TRACK )
				left += OPTION_WIDTH * 2;
			else if( m_pDragItem->iType!=SEGTYPE_STYLE )
				left += OPTION_WIDTH;

			TPoint pt( left, top );
			pt = ClientToScreen( pt );

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

			w_xOffDrag = ptDown.X - left;
			w_yOffDrag = ptDown.Y - top;
		}
	}
	else if( Shift.Empty() ){
		if( !Focused())
			SetFocus();

		WORD iCol = 0;
		int iHitLine = (Message.YPos - m_nTitleHeight)/m_nItemHeight;
		TCommonItem* pItem = NULL;

		if( !m_sbVert->Visible || Message.XPos < m_sbVert->Left )
			pItem = HitTest( Message.XPos, Message.YPos-m_nTitleHeight, &iCol );

//		pMove.X = Message.XPos;
//		pMove.Y = (iHitLine+1)*m_nItemHeight +m_nTitleHeight ;//Message.YPos;

		if( pItem!=m_pFocItem || iCol!=m_iFocCol ){
//			Hint = "";
//			Hint = "";
			Application->CancelHint();
//			Application->ShowHint = false;

			if( pItem ){
				//创建hint
				if( iCol==COL_TEXT ){
					Hint = pItem->HintText();
				}
				else if( iCol==COL_FOLD ){
					if( pItem->iType==SEGTYPE_TRACK ){
							Hint = "静音";
					}
					else{
						if( pItem->IsUnfold() )
							Hint = "收起";
						else
							Hint = "展开";
					}
				}
				else{
					DWORD opt = pItem->GetOption( iCol );
					if( opt==OPT_DEL  ){
						Hint = "删除";
					}
					else if( opt==OPT_DOWNLOAD  ){
						Hint = "下载";
					}
					else if( opt==OPT_VOL ){
						int vol=0;
						if( pItem->iType==SEGTYPE_TRACK ){
							TTrackItem* pTrk = (TTrackItem*)pItem;
							vol = pTrk->pTrack->nVol;
						}
						else{
							TSegmentItem* pSeg = (TSegmentItem*)pItem;
							vol = pSeg->volume;
						}

						Hint = "音量:"+IntToStr(vol);
					}
					else if( opt==OPT_BRUSH){
						if( pItem->iType==SEGTYPE_TRACK )
							Hint = "乐器刷";
						else
							Hint = "风格刷";
					}
					else if( opt==OPT_TEXTURE ){
						Hint = "更换织体";
					}
					else if( opt==OPT_FONTPROG ){
						if(pItem->iType == SEGTYPE_STYLE)
							Hint = "批量更换音色/乐器";
						else
							Hint = "更换音色/乐器";
					}
					else if( opt==OPT_COLLECT ){
						if( pItem->iType==SEGTYPE_STYLE ){
							if( pItem->IsCollected() )
								Hint = "取消风格收藏";
							else
								Hint = "收藏风格";
						}
						else if( pItem->iType==SEGTYPE_TRACK ){
							if( pItem->IsCollected() )
								Hint = "取消织体收藏";
							else
								Hint = "收藏织体";
						}
					}
					else if( opt==OPT_SCORE ){
						TStyleItem * pStyItem = (TStyleItem*)pItem;
						if( pStyItem->pStyle ){
							char buf[8];
							int score10;
							if( pStyItem->pStyle->idLoc==0 )
								score10 =  (int)(pStyItem->pStyle->pOrigin->score/10.0 + 0.5);
							else
								score10 =  (int)(pStyItem->pStyle->pShare->score/10.0 + 0.5);
							sprintf( buf, "%.1f", score10 / 10.0 );

							Hint = "当前评分：" + String(buf) + "\n   (点击评分)";
						}
					}
					else if( opt==OPT_SAVE ){
						Hint = "保存为本地风格";
					}
					else if( opt==OPT_IMPORT ){
						Hint = "导入MIDI片段";
					}
					else if( opt==OPT_UPLOAD ){
						Hint = "发布：原创风格\n分享：重组风格";
					}
					else if( opt==OPT_CHARGE ){
						if( pItem->iType==SEGTYPE_STYLE )
							Hint = ChargeTip( (TStyleItem*)pItem );
					}
				}
			}

//			Hint = HintStr;
//			Application->ShowHint = true;
		}

		SetFocusedItem( pItem, iHitLine, iCol );
	}
}
//---------------------------------------------------------------------------

void __fastcall TStylePanel::WMMouseDown(TWMMouse &Message)
{
	TToolpalPanel::WMMouseDown(Message);

	if( Message.Result==1 )
		return;

//	if( m_HintTimer && m_HintTimer->Enabled )
//		m_HintTimer->Enabled = false;

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

	int iHitLine = (Message.YPos - m_nTitleHeight)/m_nItemHeight;
	unsigned short iCol = 0;
	TCommonItem* pItem = HitTest( Message.XPos, Message.YPos-m_nTitleHeight, &iCol );

	SetFocusedItem( pItem, iHitLine, iCol );

	if( !m_pFocItem ){
		SetSelect( NULL, -1 );
	}
	else if( Message.Keys & MK_RBUTTON ){
		CMenuItem* pMenuItem = NULL;
		m_PopMenu->ClearItems();

		int index = -1;
		if( m_pFocItem->aItems->Count>0 ){
			if( m_pFocItem->IsUnfold() )
				m_PopMenu->CreateMenu( "收起", "", DoFold );
			else
				m_PopMenu->CreateMenu( "展开", "", DoFold );
		}
		else if( m_pFocItem->iType==SEGTYPE_TRACK ){
			if( m_pFocItem->IsMute() )
				m_PopMenu->CreateMenu( "取消静音", "", DoMute );
			else
				m_PopMenu->CreateMenu( "静音", "", DoMute );
		}

		if( index!=-1 )
			m_PopMenu->CreateMenu( "-" );

		int iOnline = m_pFocItem->GetOnlineState();
		if( iOnline==e_Downloaded ){
			m_PopMenu->CreateMenu( "试听播放", "", DoPlay );
			pMenuItem = m_PopMenu->CreateMenu( "停止试听", "", DoStop );
			pMenuItem->Enabled = m_bPlaying;

			bool bBreak = true;
			for( int i=0; i<OPT_COL_NUM; i++ ){
				int opt = m_pFocItem->GetOption( COL_OPT<<i );

				if( opt>0 && opt!=OPT_CHARGE ){
					if( bBreak ){
						m_PopMenu->CreateMenu( "-" );
						//m_PopupMenu->AddMenuItem( "-" );
						bBreak = false;
					}

					if( opt==OPT_DEL ){
						m_PopMenu->CreateMenu( "删除", "", DoDelete );
						if( m_pFocItem->iType==SEGTYPE_STYLE ){
							m_PopMenu->CreateMenu( "重命名", "", DoRename );
						}
					}
					else if( opt==OPT_VOL ){
						m_PopMenu->CreateMenu( "调节音量", "", DoAdjustVolume );
					}
					else if( opt==OPT_BRUSH ){
						if( m_pFocItem->iType==SEGTYPE_TRACK )
							m_PopMenu->CreateMenu( "乐器刷", "", DoUseBrush );
						else
							m_PopMenu->CreateMenu( "风格刷", "", DoUseBrush );
					}
					else if( opt==OPT_TEXTURE ){
						m_PopMenu->CreateMenu( "更换织体", "", DoChangeTexture );
					}
					else if( opt==OPT_FONTPROG ){
						if(m_pFocItem->iType == SEGTYPE_STYLE){
							m_PopMenu->CreateMenu( "批量更换音色/乐器", "", DoChangeFontProg );

							if( !g_lstOpusStages.empty() && ((TStyleItem*)m_pFocItem)->nBars==0 ){
								m_PopMenu->CreateMenu( "-" );
								m_PopMenu->CreateMenu( "替换使用风格", "", DoApplyUseStyle );
							}
						}
						else
							m_PopMenu->CreateMenu( "更换音色库或音色", "", DoChangeFontProg );
					}
					else if( opt==OPT_COLLECT ){
						String strMenu;
						if( m_pFocItem->iType==SEGTYPE_STYLE ){
							if( m_pFocItem->IsCollected() )
								strMenu = "取消风格收藏";
							else
								strMenu = "收藏风格";
						}
						else if( m_pFocItem->iType==SEGTYPE_TRACK ){
							if( m_pFocItem->IsCollected() )
								strMenu = "取消织体收藏";
							else
								strMenu = "收藏织体";
						}
						if( !strMenu.IsEmpty() )
							m_PopMenu->CreateMenu( strMenu, "", DoCollect );
					}
					else if( opt==OPT_SAVE ){
						m_PopMenu->CreateMenu( "-" );
						m_PopMenu->CreateMenu( "保存为本地风格...", "", DoSaveLocalStyle );
					}
					else if( opt==OPT_IMPORT ){
						m_PopMenu->CreateMenu( "导入MIDI片段...", "", DoImportSegment );
					}
				}
			}

			if( m_pFocItem->iType==SEGTYPE_STYLE ){
				if( m_pFocItem!=g_pUseStyle ){
					m_PopMenu->CreateMenu(  "-" );

					pMenuItem = m_PopMenu->CreateMenu( "替换使用风格", "", DoApplyUseStyle );
					pMenuItem->Enabled = !g_lstOpusStages.empty();

					if( m_pFocItem!=g_pPreStyle ){
						m_PopMenu->CreateMenu( "替换预编风格", "", DoApplyPreStyle );

						m_PopMenu->CreateMenu(  "-" );
						pMenuItem = m_PopMenu->CreateMenu( "重新下载风格", "", DoDownload );
						pMenuItem->Enabled = !g_bUpdateMandatory && SOFTWARE_ID!=e_Pro_Lite;
					}
				}
			}
		}
		else if( iOnline==e_UnDownload ){
			pMenuItem = m_PopMenu->CreateMenu( "下载风格", "", DoDownload );
			pMenuItem->Enabled = !g_bUpdateMandatory && SOFTWARE_ID!=e_Pro_Lite;
			pMenuItem = m_PopMenu->CreateMenu( "全部下载", "", DoDownloadAll );
			pMenuItem->Enabled = !g_bUpdateMandatory && SOFTWARE_ID!=e_Pro_Lite;
		}
		else{
			m_PopMenu->CreateMenu( "取消下载", "", DoCancelDownload );
		}

		m_PopMenu->Parent = this;
		m_PopMenu->Show(Message.XPos,Message.YPos);
	}
	else if( Message.Keys & MK_LBUTTON ){
		if( m_iFocCol==COL_TEXT ){ //左键点中文字
			if( StartDrag() ){ //符合条件准备拖动
				if( m_pFocItem==m_pSelItem && m_pFocItem->iType==SEGTYPE_STYLE ){
					RenameTiming( 1 );
				}

				SetSelect( m_pFocItem, m_iFocLine );

				m_pDragItem = m_pFocItem;

				ptDown.X = Message.XPos;
				ptDown.Y = Message.YPos;

				SetCapture( Handle );
			}
		}
		else{
			DWORD opt = m_pFocItem->GetOption( m_iFocCol );

			if( opt==OPT_VOL ){  //左键点击音量
				SetSelect( m_pFocItem, m_iFocLine );

				DoAdjustVolume( NULL );
			}
			else if( opt==OPT_FONTPROG ){  //替换音色/乐器
				SetSelect( m_pFocItem, m_iFocLine );
				DoChangeFontProg(NULL);
			}
			else if( opt==OPT_TEXTURE ){
				SetSelect( m_pFocItem, m_iFocLine );
				DoChangeTexture(NULL);
			}
			else if( opt==OPT_SAVE ){
				DoSaveLocalStyle( NULL );
			}
			else if( opt==OPT_IMPORT ){
				DoImportSegment( NULL );
			}
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TStylePanel::UMMouseUp(TWMMouse &Message)
{
	TShiftState Shift(Message.Keys);
	TCommonItem* pLastItem = NULL;

	if( m_pDragItem ){
		pLastItem = m_pDragItem;

		ReleaseCapture();
		m_pDragItem = NULL;
	}

	if( Shift.Contains( ssLeft ) && m_pFocItem && m_iFocCol ){
		WORD iCol = 0;
		int iHitLine = (Message.YPos - m_nTitleHeight)/m_nItemHeight;
		TCommonItem* pItem = HitTest( Message.XPos, Message.YPos-m_nTitleHeight, &iCol );

		if( pItem==m_pFocItem && iCol==m_iFocCol ){
			if( m_iFocCol==COL_TEXT ){
				SetSelect( m_pFocItem, m_iFocLine );
			}
			else if( m_iFocCol==COL_FOLD ){
				if( m_pFocItem->iType==SEGTYPE_TRACK ){
					DoMute(NULL);
				}
				else{
					SetFold( m_pFocItem, m_iFocLine );
				}
			}
			else if( m_pFocItem->GetOption( m_iFocCol )==OPT_DEL ){
				if( m_pSelItem && ( m_pSelItem==m_pFocItem || m_pSelItem==m_pFocItem->up
									|| m_pFocItem->up && m_pSelItem==m_pFocItem->up->up )){
					Stop();
				}

				DeleteItem( m_pFocItem, m_iFocLine );

				//再次选择焦点项
				pItem = HitTest( Message.XPos, Message.YPos-m_nTitleHeight, &iCol );
				SetFocusedItem( pItem, iHitLine, iCol );
			}
			else if( m_pFocItem->GetOption( m_iFocCol )==OPT_DOWNLOAD ){
				DoDownload( NULL );
			}
			else if( m_pFocItem->GetOption( m_iFocCol )==OPT_BRUSH ){
				if( m_pFocItem->GetOnlineState()==e_Downloaded ){
					SetSelect( m_pFocItem, m_iFocLine );
					PostMessage( Parent->Handle, UM_SHOWBRUSH, 0, LPARAM(m_pFocItem) );
				}
			}
			else if( m_pFocItem->GetOption( m_iFocCol )==OPT_COLLECT ){     //点击收藏
				DoCollect(NULL);
				//SetCollect(m_pFocItem);
				//DrawItem( m_pFocItem,iHitLine,iCol  );
			}
			else if( m_pFocItem->GetOption( m_iFocCol )==OPT_SCORE ){       //评分
				if( m_pFocItem->GetOnlineState()!=e_Downloaded ){
					DoDownload( NULL );
				}
				else{
					SetSelect( m_pFocItem, m_iFocLine );

					int iTop = Top + m_nTitleHeight + ( m_iFocLine + 1 ) * m_nItemHeight,
						iRight = Left + Width - m_sbVert->Width - 2;
					PostMessage( Application->MainFormHandle, UM_STYLESCORE,
											MAKELONG( iTop, iRight ),
											(LPARAM)((TStyleItem*)m_pFocItem)->pStyle );
				}
			}
			else if( m_pFocItem->GetOption( m_iFocCol )==OPT_UPLOAD ){       //发布
				SetSelect( m_pFocItem, m_iFocLine );
				PostMessage( Parent->Handle, UM_UPLOADSTYLE, 0, LPARAM(m_pFocItem) );
			}
		}
	}
}
//---------------------------------------------------------------------------

void TStylePanel::DrawItem( TCommonItem *pItem, int iLine, WORD iCol )
{
	TRect rect;
	rect.Left = LEFT_BOUND;
	rect.Right = Width - RIGHT_BOUND;
	rect.Top = iLine * m_nItemHeight + m_nTitleHeight;
	rect.Bottom = min( Height-1, (int)(rect.Top + m_nItemHeight) );

	if( pItem ){
		pItem->DrawItem( Canvas, rect, iCol );
	}
	else{
		Canvas->Brush->Color = clrBackground;
		Canvas->FillRect( rect );
    }
}
//---------------------------------------------------------------------------

void TStylePanel::DrawDragImg( TCommonItem *pDragItem )
{
	if( pDragItem ){
		w_imgDrag = new TImage(this);
		w_imgDrag->Width = Width - LEFT_BOUND - RIGHT_BOUND - OPTION_WIDTH*3;
		w_imgDrag->Height = m_nItemHeight-2;

		if( pDragItem->iType==SEGTYPE_TRACK )
			w_imgDrag->Width -= OPTION_WIDTH * 2;
		else if( pDragItem->iType!=SEGTYPE_STYLE )
			w_imgDrag->Width -= OPTION_WIDTH;

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

		//绘制底色
		w_imgDrag->Canvas->Brush->Color = clrSelback;
		w_imgDrag->Canvas->FillRect( rcDraw );

		//绘制列表项文字
		w_imgDrag->Canvas->Font->Assign( Canvas->Font );

		if( m_pDragItem->iType==SEGTYPE_STYLE )
			w_imgDrag->Canvas->Font->Color = clrTextStyle;
		else{
			w_imgDrag->Canvas->Font->Color = pDragItem->TextColor();
		}

		String strCaption = pDragItem->GetCaption();
		int Y = rcDraw.Top + ( OPTION_HEIGHT - Canvas->TextHeight(strCaption) )/2;
		rcDraw.Left += LEFT_BOUND + OPTION_WIDTH;

		w_imgDrag->Canvas->Brush->Style = bsSolid;
		w_imgDrag->Canvas->TextRect( rcDraw, rcDraw.Left,
											Y, strCaption );

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

bool TStylePanel::ShowDragImg( bool bShow, int iState )
{
	bool bCanDrop = false;
	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_ADJUSTHEIGHT ){
		rtInvalid.left = 1;
		rtInvalid.right = Width-1;
		rtInvalid.top = Height;
		rtInvalid.bottom = 0;

		if( ptOld.Y>0 && ptOld.Y<Height
					|| ptOld.Y+h>0 && ptOld.Y+h<Height ){
			rtInvalid.top = min( ptOld.Y, rtInvalid.top );
			rtInvalid.bottom = max( ptOld.Y+h, rtInvalid.bottom );
		}

		if( bShow && ( ptNew.Y>0 && ptNew.Y<Height
					|| ptNew.Y+h>0 && ptNew.Y+h<Height )){
			rtInvalid.top = min( ptNew.Y, rtInvalid.top );
			rtInvalid.bottom = max( ptNew.Y+h, rtInvalid.bottom );
		}

		if( rtInvalid.top<0 ) rtInvalid.top = 0;
		if( rtInvalid.bottom>Height ) rtInvalid.bottom = Height;

		if( rtInvalid.bottom>rtInvalid.top ){
			InvalidateRect( Handle, &rtInvalid, false );
		}
	}
	else if( w_iDragMode==DM_ADJUSTSTYLE ){
		TPoint ptMouse( ptNew.X+w_xOffDrag, ptNew.Y+w_yOffDrag );

		rtInvalid.left = 0;
		rtInvalid.right = Width;
		rtInvalid.top = Height;
		rtInvalid.bottom = 0;

		if( ptOld.Y>0 && ptOld.Y<Height
					|| ptOld.Y+h>0 && ptOld.Y+h<Height ){
			rtInvalid.top = min( ptOld.Y, rtInvalid.top );
			rtInvalid.bottom = max( ptOld.Y+h, rtInvalid.bottom );
		}
		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 ){
			if( ptNew.Y>0 && ptNew.Y<Height
						  || ptNew.Y+h>0 && ptNew.Y+h<Height ){
				rtInvalid.top = min( ptNew.Y, rtInvalid.top );
				rtInvalid.bottom = max( ptNew.Y+h, rtInvalid.bottom );
			}

			if( 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 );
			}

			RenameTiming( 0 );

			if( ptMouse.X>LEFT_BOUND && ptMouse.X<Width-RIGHT_BOUND
					&& ptMouse.Y>m_nTitleHeight && ptMouse.Y<Height )
				bCanDrop = HandleHoverEvent( ptMouse );
			else
				SetHover( NULL, -1 );
		}
		else{
			if( m_pHoverItem || ptMouse.X>LEFT_BOUND && ptMouse.X<Width-RIGHT_BOUND
							&& ptMouse.Y>m_nTitleHeight && ptMouse.Y<Height ){ //处理Drop事件
				HandleDropEvent( iState );
			}

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

			m_pDragItem = m_pDropItem = NULL;

			SetHover( NULL, -1 );
		}

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

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

		if( bShow && m_pDropItem ){
			//SetScrollTimer( ptMouse );
			SetScrollTimer( ptNew );
		}
	}
	else
		return TToolpalPanel::ShowDragImg( bShow );

	return bCanDrop;
}
//---------------------------------------------------------------------------

void TStylePanel::SetScrollTimer( TPoint ptMouse )
{
	int X = ptMouse.X,
		Y = ptMouse.Y;

	//判断是否滚动
	if( m_sbVert->Visible && X>LEFT_BOUND && X<Width-RIGHT_BOUND ){
		int iDirect = 0;
		if( Y>Height-m_nItemHeight && Y<Height
					&& m_iTopLine<m_nTotalLine-m_nItemShow )
			iDirect = 1;
		else if( Y>0 && Y<m_nTitleHeight && m_iTopLine>0 )
			iDirect = -1;

		if( iDirect!=0 ){
			if( !m_pTimer ){
				m_pTimer = new TTimer(this);
				m_pTimer->OnTimer = TimerProc;
//				m_pTimer->Tag = iDirect;
//				m_pTimer->Interval = 500;
//				m_pTimer->Enabled = true;
			}
			else if( m_pTimer->Tag!=iDirect ){
				m_pTimer->Enabled = false;
			}

			m_pTimer->Tag = iDirect;
			m_pTimer->Interval = 500;
			m_pTimer->Enabled = true;
		}
		else if( m_pTimer && ( m_pTimer->Tag==1
							   || m_pTimer->Tag==-1 ) ){
			delete m_pTimer;
			m_pTimer = NULL;
		}
	}
}
//---------------------------------------------------------------------------

void TStylePanel::SetUnfoldTimer(TCommonItem *pItem, TCommonItem *pHoverItem)
{
	if( pItem && pItem->aItems->Count>0 && !pItem->IsUnfold() && pItem==pHoverItem){
		if( !m_pTimer ){
			m_pTimer = new TTimer(this);
			m_pTimer->OnTimer = TimerProc;
			m_pTimer->Enabled = false;
		}

		if( pHoverItem!=m_pHoverItem && m_pTimer->Enabled )
			m_pTimer->Enabled = false;

		m_pTimer->Tag = 0;
		m_pTimer->Interval = 1500;
		m_pTimer->Enabled = true;
	}
	else{
		if( m_pTimer && m_pTimer->Tag==0 ){
			delete m_pTimer;
			m_pTimer = NULL;
		}
	}
}
//---------------------------------------------------------------------------

void TStylePanel::SetFold( TCommonItem *pFoldItem, int iFoldLine )
{
	if( !pFoldItem )
		return;

	bool bUnfold = !pFoldItem->IsUnfold();
	pFoldItem->SetUnfold( bUnfold );

	if( bUnfold && pFoldItem==m_pHoverItem && pFoldItem->aItems->Count>0 ){
		TCommonItem* pSubItem = (TCommonItem*)m_pHoverItem->aItems->First();
		while( pSubItem!=m_pHoverItem->next ){
			pSubItem->SetHover( true );
			pSubItem = GetNextItem( pSubItem );
		}
	}

	RECT rt;
	rt.left = LEFT_BOUND;
	rt.top = (iFoldLine>0 ? iFoldLine : 0) * m_nItemHeight + m_nTitleHeight,
	rt.right = Width - RIGHT_BOUND;
	rt.bottom = Height-1;
	InvalidateRect( Handle, &rt, false );

	//重新计算选中行号
	if( m_pSelItem && m_iSelLine>iFoldLine ){
		if( m_pSelItem->up && ( m_pSelItem->up==pFoldItem ||
								m_pSelItem->up->up==pFoldItem )){
			m_pSelItem->Select( false );
			m_pSelItem = NULL;
			m_iSelLine = -1;
			Stop();
		}
		else{
			int iSelLine = iFoldLine;
			TCommonItem* pItem = pFoldItem;

			while( pItem ){
				if( pItem==m_pSelItem ){
					m_iSelLine = iSelLine;
					break;
				}
				else{
					iSelLine++;
				}

				pItem = GetNextItem( pItem );
			}
		}
	}

	if( pFoldItem->aItems->Count>0 ){
		//计算总行数
		TCommonItem* pSubItem = (TCommonItem*)pFoldItem->aItems->First();
		int count = 0;
		while( pSubItem!=pFoldItem->next ){ //所有显示的子孙项
			count++;
			pSubItem = GetNextItem( pSubItem );
		}

		if( bUnfold )
			m_nTotalLine += count;
		else
			m_nTotalLine -= count;

		ResetScrollBar();
	}
}
//---------------------------------------------------------------------------

void TStylePanel::DeleteItem( TCommonItem *pItem, int iLine )
{
	if( !pItem )
		return;

	if( m_pFocItem==pItem || m_pFocItem && m_pFocItem->up==pItem
							|| m_pFocItem && m_pFocItem->up
								&& m_pFocItem->up->up==pItem ){
		m_pFocItem = NULL;
		m_iFocLine = -1;
		m_iFocCol = 0;
	}

	if( m_pSelItem==pItem || m_pSelItem && m_pSelItem->up==pItem
							|| m_pSelItem && m_pSelItem->up
								&& m_pSelItem->up->up==pItem ){
		m_pSelItem = NULL;
		m_iSelLine = -1;
		Stop();
	}

	if( pItem->prev )
		pItem->prev->next = pItem->next;
	if( pItem->next )
		pItem->next->prev = pItem->prev;

	//重新计算选中行号
	if( iLine>=0 && m_pSelItem && m_iSelLine>iLine ){
		if( m_pSelItem->up && m_pSelItem->up==pItem ){
			m_pSelItem->Select( false );
			m_pSelItem = NULL;
			m_iSelLine = -1;
			Stop();
		}
		else{
			int iSelLine = iLine;
			TCommonItem* pTmpItem = GetNextItem(pItem);

			while( pTmpItem ){
				if( pTmpItem==m_pSelItem ){
					m_iSelLine = iSelLine;
					break;
				}
				else{
					iSelLine++;
				}

				pTmpItem = GetNextItem( pTmpItem );
			}
		}
	}

	if( m_pTopItem==pItem ){
		if( pItem->iType==SEGTYPE_TRACK ){
			m_pTopItem = GetNextItem( pItem );
			if( !m_pTopItem ){
				m_pTopItem = GetPrevItem( pItem );
				m_iTopLine--;
			}
		}
		else{
			m_pTopItem = pItem->next ? pItem->next : pItem->prev;
        }
	}

	if( pItem->iType==SEGTYPE_STYLE ){
		if( pItem->IsUnfold() ){
			m_nTotalLine -= pItem->aItems->Count;
			for( int i=0; i<pItem->aItems->Count; i++ ){
				TSegmentItem* pSeg = (TSegmentItem*)pItem->aItems->Items[i];
				if( pSeg->IsUnfold() ){
					m_nTotalLine -= pSeg->aItems->Count;
				}
			}
		}

		delete pItem;
		m_aItems->Remove( pItem );

		m_nTotalLine--;
	}
	else{
		TStyleItem * pStyItem = NULL;
		if( pItem->iType==SEGTYPE_TRACK  ){
			TCommonItem * pUp = pItem->up;

			if( pUp->up->IsUnfold() && pUp->IsUnfold() )
				m_nTotalLine--;

			((TSegmentItem*)pUp)->DeleteTrack( pItem );

//			pStyItem = (TStyleItem*)pUp->up;
		}
		else{
			pStyItem = (TStyleItem*)pItem->up;

			if( pStyItem->IsUnfold() ){
				m_nTotalLine--;

				if( pItem->IsUnfold() )
					m_nTotalLine -= pItem->aItems->Count;
			}

			pStyItem->DeleteSegment( pItem );
		}
	}

	ResetScrollBar();
	Paint();
}
//---------------------------------------------------------------------------

void TStylePanel::SetHover( TCommonItem *pHoverItem, int iHoverLine )
{
	if( m_pHoverItem!=pHoverItem ){
		//清除原Hover标志
		if( m_pHoverItem ){
			m_pHoverItem->SetHover( false );
			if( m_iHoverLine>=0 && m_iHoverLine<m_nItemShow )
				DrawItem( m_pHoverItem, m_iHoverLine, -1 );
			if( m_pHoverItem->IsUnfold() && m_pHoverItem->aItems->Count>0 ){
				TCommonItem* pSubItem = (TCommonItem*)m_pHoverItem->aItems->First();
				int iLine = m_iHoverLine+1;
				while( pSubItem!=m_pHoverItem->next ){ //所有显示的子孙项
					pSubItem->SetHover( false );

					if( iLine>=0 && iLine<m_nItemShow )
						DrawItem( pSubItem, iLine, -1 );

					pSubItem = GetNextItem( pSubItem );
					iLine++;
				}
			}
		}

		if( pHoverItem ){
			pHoverItem->SetHover( true );
			if( iHoverLine>=0 && iHoverLine<m_nItemShow )
				DrawItem( pHoverItem, iHoverLine, -1 );

			if( pHoverItem->IsUnfold() && pHoverItem->aItems->Count>0 ){
				TCommonItem* pSubItem = (TCommonItem*)pHoverItem->aItems->First();
				int iLine = iHoverLine+1;
				while( pSubItem!=pHoverItem->next ){ //所有显示的子孙项
					pSubItem->SetHover( true );

					if( iLine>=0 && iLine<m_nItemShow )
						DrawItem( pSubItem, iLine, -1 );

					pSubItem = GetNextItem( pSubItem );
					iLine++;
				}
			}
		}

		m_pHoverItem = pHoverItem;
		if( pHoverItem )
			m_iHoverLine = iHoverLine;
		else
			m_iHoverLine = -1;
	}
}
//---------------------------------------------------------------------------

void __fastcall TStylePanel::TimerProc(TObject* Sender)
{
	if( w_bDraging ){
		DebugPrint( "On timer." );

		if( m_pTimer->Tag!=0 ){
			ScrollView( m_iTopLine + m_pTimer->Tag );

			if( m_iTopLine<=0 || m_iTopLine>=m_nTotalLine-m_nItemShow ){
				delete m_pTimer;
				m_pTimer = NULL;
			}
			else if( m_pTimer->Interval>200 ){
				m_pTimer->Interval = 200;
			}
		}
		else{
			SetFold( m_pHoverItem, m_iHoverLine );
			delete m_pTimer;
			m_pTimer = NULL;
		}
	}
}
//---------------------------------------------------------------------------

void TStylePanel::Stop( DWORD dwSpec )
{
	if( dwSpec==STOP_SELF ){
		if( m_bPlaying )
			f_MM_Stop();
	}
	else{
		Parent->Perform( UM_STOP, dwSpec, 0 );
	}

	m_pPlayItem = NULL;
	m_bPlaying = false;
}
//---------------------------------------------------------------------------

void __fastcall TStylePanel::MouseLeave(TObject* Sender)
{
	TPoint pt;
	GetCursorPos(&pt);
	pt = this->ScreenToClient(pt);
    //如果离开当前的面板
	if( pt.X<=0||pt.X>=this->Width - RIGHT_BOUND  || pt.Y<=0 || pt.Y>=this->Height )
	{
		if( m_VolTrackBar ){
			delete m_VolTrackBar;
			m_VolTrackBar=NULL;
		}

		SetFocusedItem( NULL, -1, 0 );
	}
	else if( m_pFocItem ){
		if( !m_VolTrackBar && !m_PopMenu->Visible ){
			SetFocusedItem( NULL, -1, 0 );
        }
	}
//
//	if( m_Hint ){
//		m_Hint->ReleaseHandle();
//		delete m_Hint;
//		m_Hint = NULL;
//		HintStr = "";
//	}
//
//	if( m_HintTimer ){
//		m_HintTimer->Enabled = false;
//		delete m_HintTimer;
//		m_HintTimer = NULL;
//	}
}

TCommonItem* TStylePanel::GetTrackHoverItem( TCommonItem* pItem, int& iHoverLine )
{
	TCommonItem* pHoverItem = NULL;

	int stDrop = m_pDropItem->GetSegType(),
		stHover = pItem->GetSegType();
//		snDrop = m_pDropItem->GetSegNo(),
//		snHover = pItem->GetSegNo();

	if( stDrop!=SEGTYPE_INTRO && stDrop!=SEGTYPE_ENDING && stDrop==stHover ){
		TSegmentItem *pSeg = NULL;
		if( pItem->iType!=SEGTYPE_TRACK )
			pSeg = (TSegmentItem*)pItem;
		else
			pSeg = (TSegmentItem*)pItem->up;

		char chan = ((TTrackItem*)m_pDropItem)->pTrack->iChanNo;

		if( !pSeg->bAutoSpec || pSeg->aItems->Count<16 || chan==9 && pSeg->IsChanUsed(9) ){
			pHoverItem = pItem;
			if( pItem->iType==SEGTYPE_TRACK ){
				do{
					iHoverLine--;
					pHoverItem = GetPrevItem( pHoverItem );
				}
				while( pHoverItem!=pSeg );
			}
		}
	}

	return pHoverItem;
}
//---------------------------------------------------------------------------

TTrackItem* TStylePanel::AddTrack( TSegmentItem* pSeg, TTrackItem* pSrcTrk, bool bOwn )
{
	TTrackItem* pDestTrk = NULL;

	if( pSeg && pSrcTrk ){
		int nTrksBefore = pSeg->aItems->Count;
		DWORD opts = OPT_DEL|OPT_VOL;
		if( pSeg->iSegType<SEGTYPE_INTRO )
			opts |= OPT_TEXTURE;

		TTrack* pNewTrack = new TTrack( pSrcTrk->pTrack );

		pDestTrk = pSeg->AddTrack( pSrcTrk->pStyle, pNewTrack, opts, bOwn );

		int nTrksAfter = pSeg->aItems->Count;

		if( !pSeg->IsUnfold() ){
			SetFold( pSeg, m_iHoverLine );
		}
		else if( nTrksAfter!=nTrksBefore ){
			m_nTotalLine += nTrksAfter-nTrksBefore;
		}

		if( m_pSelItem ){
			m_pSelItem->Select( false );
		}

		pDestTrk->Select( true );
		m_pSelItem = pDestTrk;

		m_iSelLine = m_iHoverLine;
		if( m_pHoverItem->iType!=SEGTYPE_TRACK ){
			m_iSelLine++;
			TCommonItem* pItem = GetNextItem( m_pHoverItem );

			while( pItem!=pDestTrk ){
				m_iSelLine++;
				pItem = GetNextItem( pItem );
			}
		}
	}

	return pDestTrk;
}
//---------------------------------------------------------------------------

void __fastcall TStylePanel::DoubleClick(TObject* sender)
{
	if( m_pSelItem && m_iFocCol==COL_TEXT ){
		PlayDemo( m_pSelItem, g_nMelodyTempo );
	}
}
//---------------------------------------------------------------------------

void TStylePanel::PlayDemo( TCommonItem *pItem, int tempo )
{
	if( !pItem )
		return;

	Stop( STOP_ALL );

	int seed = time(NULL);

	int key = g_iMelodyKey;
	if( key<0 || key>11 )
		key = seed % 12;

	char cType = (seed>>1) % 2;

	TStage* aStages = NULL;
	int nStageNum = 0;

	if( pItem->iType==SEGTYPE_STYLE ){ //组合试听
		TStyleItem* pStyle = (TStyleItem*)pItem;
		if( tempo<0 ){
			if( pStyle->pStyle->idLoc==0 )
				tempo = pStyle->pStyle->pOrigin->detail.iTempo;
			else if( pStyle->pStyle->idLoc==1 )
				tempo = pStyle->pStyle->pShare->iTempo;
			else
				tempo = pStyle->pStyle->pLocal->iTempo;
		}

		int segCount = pStyle->nSegNum[SEGTYPE_VAR];
		int fillNum = pStyle->nSegNum[SEGTYPE_FILL];
		int barNum = fillNum>0 ? 3 : 4;
		int repeat = fillNum>0 ? 2 : 1;

		vector<TSegmentItem*> aSegments[2];
		for( int i=0; i<pStyle->aItems->Count; i++ ){
			TSegmentItem* pSeg = (TSegmentItem*)pStyle->aItems->Items[i];
			int stype = pSeg->GetSegType();

			if( stype==SEGTYPE_VAR )
				aSegments[0].push_back( pSeg );
			else if( stype==SEGTYPE_FILL)
				aSegments[1].push_back( pSeg );
		}

		nStageNum = segCount * repeat;
		aStages = new TStage[nStageNum];

		for( int s=0; s<segCount; s++ ){
			int iPreset = (seed>>(s+2)) % 4;

			for( int k=0; k<repeat; k++ ){
				int i = s * repeat + k;
				int sc = (k==0) ? s : (s % fillNum);

				TSegmentItem* pSeg = aSegments[k][sc];
				aStages[i].iSegType = pSeg->iSegType;
				aStages[i].iSegKey = key;

				//chords
				aStages[i].nBarNum = (k==0) ? barNum : 1;
				aStages[i].aChords = new TBarChords[aStages[i].nBarNum];
				for( int j=0; j<aStages[i].nBarNum; j++ ){
					int c = (k==0) ? j : 3;

					char chordType = u_arrChordPreset[cType][iPreset][c] % 4;
					char chordKey = u_arrChordPreset[cType][iPreset][c] / 4;

					aStages[i].aChords[j].iKey[0]
							= aStages[i].aChords[j].iKey[1]
							= aStages[i].aChords[j].iKey[2]
							= aStages[i].aChords[j].iKey[3] = chordKey;
					aStages[i].aChords[j].iType[0]
							= aStages[i].aChords[j].iType[1]
							= aStages[i].aChords[j].iType[2]
							= aStages[i].aChords[j].iType[3] = chordType;
				}

				//tracks
				aStages[i].nTrkNum = pSeg->aItems->Count;
				aStages[i].aTracks = new TComposeTrack[aStages[i].nTrkNum];
				for( int j=0; j<aStages[i].nTrkNum; j++ ){
					//TTrackItem* pTrk = (TTrackItem*)pSeg->aItems->Items[j];
					TTrack* pTrack = ((TTrackItem*)pSeg->aItems->Items[j])->pTrack;
					TComposeTrack& track = aStages[i].aTracks[j];

					track.iStyleLoc = pTrack->iStyleLoc;// pTrk->pStyle->idLoc;
					track.iStyleId = pTrack->iStyleId;// pTrk->pStyle->idStyle;
					track.iSegNo = pTrack->iSegNo;
					track.iChanNo = pTrack->iChanNo;
					track.iChanSpec = pTrack->iChanSpec;
					track.iProg = pTrack->iProg;// >progCurr;
					track.nVol = pTrack->nVol<0 ? 0 : pTrack->nVol * pSeg->volume / 100.0;
				}
			}
		}
	}
	else{
		if( tempo<0 ){
			TStyleItem* pStyItem = pItem->GetStyleItem();
			if( pStyItem->pStyle->idLoc==0 )
				tempo = pStyItem->pStyle->pOrigin->detail.iTempo;
			else if( pStyItem->pStyle->idLoc==1 )
				tempo = pStyItem->pStyle->pShare->iTempo;
			else
				tempo = pStyItem->pStyle->pLocal->iTempo;
		}

		int stype = pItem->GetSegType();
//			sno = pItem->GetSegNo();

		int barNum = 4;
		if( stype>=SEGTYPE_INTRO ){
			TSegmentItem* pSeg = pItem->iType==SEGTYPE_TRACK
									? (TSegmentItem*)pItem->up
									: (TSegmentItem*)pItem;
			barNum = pSeg->nBars;
		}

		nStageNum = 1;
		aStages = new TStage[1];
		aStages->iSegType = stype;
		aStages->iSegKey = key;
		aStages->nBarNum = barNum;

		int trkNum = 1;
		TTrack * pTrk = NULL;
		char nSegVol = 100;
		if( pItem->iType==SEGTYPE_TRACK ){
			pTrk = ((TTrackItem*)pItem)->pTrack;
			nSegVol = ((TSegmentItem*)pItem->up)->volume;
		}
		else{
			trkNum = pItem->aItems->Count;
			nSegVol = ((TSegmentItem*)pItem)->volume;
		}

		aStages->nTrkNum = trkNum;
		aStages->aTracks = new TComposeTrack[trkNum];
		for( int i=0; i<trkNum; i++ ){
			if( pItem->iType!=SEGTYPE_TRACK )
				pTrk = ((TTrackItem*)pItem->aItems->Items[i])->pTrack;

			aStages->aTracks[i].iStyleId = pTrk->iStyleId;
			aStages->aTracks[i].iStyleLoc = pTrk->iStyleLoc;

			if( stype>=SEGTYPE_INTRO ){
				cType = (pTrk->iSegNo / 4 + 1) << 4;
				int sno = pTrk->iSegNo % 4;
				aStages->aTracks[i].iSegNo = sno + cType;
			}
			else
				aStages->aTracks[i].iSegNo = pTrk->iSegNo;

			aStages->aTracks[i].iChanNo = pTrk->iChanNo;
			aStages->aTracks[i].iChanSpec = pTrk->iChanSpec;
			aStages->aTracks[i].iProg = pTrk->iProg;// >progCurr;
			aStages->aTracks[i].nVol = pTrk->nVol<0 ? 0 : pTrk->nVol * nSegVol / 100.0;
		}

		if( stype<=SEGTYPE_FILL ){
			int iPreset = (seed>>2) % 4;

			aStages->aChords = new TBarChords[barNum];
			for( int j=0; j<barNum; j++ ){
				char chordType = u_arrChordPreset[cType][iPreset][j] % 4;
				char chordKey = u_arrChordPreset[cType][iPreset][j] / 4;

				aStages->aChords[j].iKey[0]
						= aStages->aChords[j].iKey[1]
						= aStages->aChords[j].iKey[2]
						= aStages->aChords[j].iKey[3] = chordKey;
				aStages->aChords[j].iType[0]
						= aStages->aChords[j].iType[1]
						= aStages->aChords[j].iType[2]
						= aStages->aChords[j].iType[3] = chordType;
			}
		}
	}

	int len = 1000000;
	BYTE* data = new BYTE[len];
	int ret = f_CM_Compose( &len, data, g_iMelodyTimeSignature, tempo,
									nStageNum, aStages, false, NULL, NULL );

	if( ret==0 ){
		f_MM_LoadDemo( data, len );
		f_MM_PlayDemo( Handle, -1, -1, -1, -1, NULL );
		m_pPlayItem = pItem;
		m_bPlaying = true;
	}
	else{
		DebugPrint( "Compose error: %d", ret );
	}

	delete[] data;

	SAFE_DELETE_ARRAY(aStages);
}
//---------------------------------------------------------------------------

void TStylePanel::VolumeChanged( TCommonItem *pItem, char volNew )
{
	char &refVol = pItem->iType==SEGTYPE_TRACK ? ((TTrackItem*)pItem)->pTrack->nVol
							: ((TSegmentItem*)pItem)->volume,
		volOld = refVol;

	if( volNew==(char)0x80 ){
		pItem->SetMute( !pItem->IsMute() );
		volNew = refVol;
	}
	else{
		if( volOld<0 )
			volNew |= 0x80;
		refVol = volNew;
	}

	if( m_bPlaying && ( pItem==m_pSelItem
						|| pItem->up && ( pItem->up==m_pSelItem
						|| pItem->up->up && pItem->up->up==m_pSelItem ))){
		int iStartBar, iEndBar, vol, segCount, fillNum, repeat = 1;

		TSegmentItem* pSeg = pItem->iType==SEGTYPE_TRACK
									? (TSegmentItem*)pItem->up
									: (TSegmentItem*)pItem;

		if( m_pSelItem->iType==SEGTYPE_STYLE ){
			if( pSeg->iSegType<=SEGTYPE_FILL ){
				segCount = ((TStyleItem*)m_pSelItem)->nSegNum[SEGTYPE_VAR];
				fillNum = ((TStyleItem*)m_pSelItem)->nSegNum[SEGTYPE_FILL];
				int barNum = 1;

				if( pSeg->iSegType==SEGTYPE_VAR ){
					iStartBar = pSeg->iInd * 4;
					barNum = fillNum>0 ? 3 : 4;
				}
				else{
					iStartBar = pSeg->iInd * 4 + 3;
					repeat = segCount / fillNum;
				}

				iEndBar = iStartBar + barNum;
			}
			else
				repeat = 0;
		}
		else{
			iStartBar = 0;

			if( m_pSelItem->GetSegType()>=SEGTYPE_INTRO ){
				iEndBar = pSeg->nBars;
			}
			else
				iEndBar = 4;
		}

		for( int i=0; i<repeat; i++ ){
			float fSegVol = pSeg->volume / 100.0;

			if( pItem->iType==SEGTYPE_TRACK ){
				TTrack* pTrk = ((TTrackItem*)pItem)->pTrack;

				vol = pTrk->nVol<0 ? 0 : pTrk->nVol * fSegVol;
				f_MM_SetTrackEvent( MIDI_EVENT_VOLUME, iStartBar, iEndBar,
														pTrk->iChanSpec, vol, -1 );
			}
			else{
				for( int i=0; i<pItem->aItems->Count; i++ ){
					TTrack* pTrk = ((TTrackItem*)pItem->aItems->Items[i])->pTrack;

					vol = pTrk->nVol<0 ? 0 : pTrk->nVol * fSegVol;
					f_MM_SetTrackEvent( MIDI_EVENT_VOLUME, iStartBar, iEndBar,
															pTrk->iChanSpec, vol, -1 );
				}
			}

			iStartBar += fillNum * 4;
			iEndBar = iStartBar + 1;
		}
	}
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::SetStyleItem( TStyleItem* pStyItem )
{
	TStyleItem* pOldSty = m_aItems->Count > 0 ? (TStyleItem*)m_aItems->First() : NULL;
	if( pOldSty==pStyItem )
		return;

	ClearAllItems();

	if( pStyItem ){
		m_aItems->Add( pStyItem );
		m_nTotalLine++;

		if( pStyItem->aItems->Count==0 ){
			RestoreItems();
		}

		m_pTopItem = pStyItem;
		SetFold( pStyItem, 0 );
	}

	ResetScrollBar();
	Paint();
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::TrackBarChange(System::TObject* Sender)
{
	TCommonItem *pItem = (TCommonItem*)m_VolTrackBar->Tag;

	if( pItem ){
		VolumeChanged( pItem, m_VolTrackBar->Position );
	}
}
// ---------------------------------------------------------------------------

//void __fastcall TStylePanel::HintTimerProc(System::TObject* Sender)
//{
//	if( m_HintTimer )
//		m_HintTimer->Enabled = false;
//
//	if( m_Hint && HintStr.Length()>0 ){
//		m_Hint->ShowHintWindow( ClientToScreen(pMove), HintStr );
//        Application->Hint = HintStr;
//		Hint = HintStr;
//		Application->ActivateHint(ClientToScreen(pMove));
//	}
//}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoDownload(TObject* Sender)
{
	if( m_pFocItem /*&& m_pFocItem->GetOnlineState()==e_UnDownload*/ ){
		TStyleItem* pStyItem = m_pFocItem->GetStyleItem();

		HRESULT ret = 0;
		if( pStyItem->pStyle->idLoc==0 )
			ret = f_CS_DownloadStyle( pStyItem->pStyle->idStyle, Handle );
		else if( pStyItem->pStyle->idLoc==1 ){
			for( int i=0; i<pStyItem->aItems->Count; i++ ){
				TSegmentItem* pSeg = (TSegmentItem*)pStyItem->aItems->Items[i];
				for( int j=0; j<pSeg->aItems->Count; j++ ){
					TTrackItem* pTrk = (TTrackItem*)pSeg->aItems->Items[j];
					if( pTrk->pStyle->idLoc==0 && pTrk->pStyle->pOrigin->state==e_UnDownload ){
						ret = f_CS_DownloadStyle( pTrk->pStyle->idStyle, Handle );
						if( ret==0 )
							pTrk->pStyle->pOrigin->state = e_Downloading;
					}
				}
			}
		}

		if( ret==0 ){
			m_pFocItem->SetOnlineState( e_Downloading );
		}
		else{
			DebugPrint( "DownloadStyle error : %d\n", ret );
		}
	}
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoDownloadAll(TObject* Sender)
{
	for( int i=0; i<m_aItems->Count; i++ ){
		TStyleItem* pItem = (TStyleItem*)m_aItems->Items[i];

		if( pItem->pStyle && pItem->pStyle->idLoc==0
						&& pItem->pStyle->pOrigin->state==e_UnDownload ){
			HRESULT ret = f_CS_DownloadStyle( pItem->pStyle->idStyle, Handle );

			if( ret==0 )
				pItem->pStyle->pOrigin->state = e_Downloading;
			else
				DebugPrint( "DownloadStyle error : %d\n", ret );
		}
	}

	Paint();
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoCancelDownload(TObject* Sender)
{
	f_CS_CancelOperation();

	for( int i=0; i<m_aItems->Count; i++ ){
		TStyleItem* pItem = (TStyleItem*)m_aItems->Items[i];

		if( pItem->pStyle && pItem->pStyle->idLoc==0
							&& pItem->pStyle->pOrigin->state==e_Downloading )
			pItem->pStyle->pOrigin->state = e_UnDownload;
	}

	Paint();
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoPlay(TObject* Sender)
{
	SetSelect( m_pFocItem, m_iFocLine );
	PlayDemo( m_pSelItem, g_nMelodyTempo );
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoStop(TObject* Sender)
{
	Stop();
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoFold(TObject* Sender)
{
	SetFold( m_pFocItem, m_iFocLine );
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoMute(TObject* Sender)
{
//	m_pFocItem->SetMute( !m_pFocItem->IsMute() );

	VolumeChanged( m_pFocItem, 0x80 );
	DrawItem( m_pFocItem, m_iFocLine, COL_FOLD );
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoAdjustVolume(TObject* Sender)
{
	if( Sender )
		SetSelect( m_pFocItem, m_iFocLine );

	if( m_pSelItem ){
		if( !m_VolTrackBar ){
			m_VolTrackBar = new TSkinTrackBar(this);
			m_VolTrackBar->Parent = this;
			m_VolTrackBar->ParentBackground = false;
			m_VolTrackBar->Color = RGB(55,56,62);//RGB(69,74,84);
			m_VolTrackBar->Orientation = true;
			m_VolTrackBar->PageSize = 10;

			m_VolTrackBar->Height = 16;
			m_VolTrackBar->Width = 63;
			m_VolTrackBar->Min = 0;
		}

		m_VolTrackBar->Top = m_iSelLine*m_nItemHeight+m_nTitleHeight + 1;
		m_VolTrackBar->Left = Width - RIGHT_BOUND - 3*OPTION_WIDTH;

		m_VolTrackBar->Tag = (int)m_pSelItem;

		char vol = m_pSelItem->iType==SEGTYPE_TRACK ? ((TTrackItem*)m_pSelItem)->pTrack->nVol
								: ((TSegmentItem*)m_pSelItem)->volume;

		m_VolTrackBar->Max = m_pSelItem->iType>=SEGTYPE_TRACK ? 127 : 100;
		m_VolTrackBar->Position = vol & 0x7F;

		if( vol < 0 )
			m_VolTrackBar->Enabled = false;

		m_VolTrackBar->OnChange = TrackBarChange;

		m_VolTrackBar->SetFocus();
	}
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoUseBrush(TObject* Sender)
{
	if( Sender )
		SetSelect( m_pFocItem, m_iFocLine );

	if( m_pSelItem && m_pSelItem->GetOnlineState()==e_Downloaded ){
		PostMessage( Parent->Handle, UM_SHOWBRUSH, 0, LPARAM(m_pFocItem) );
	}
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoDelete(TObject* Sender)
{
	if( m_pSelItem && ( m_pSelItem==m_pFocItem || m_pSelItem==m_pFocItem->up
						|| m_pFocItem->up && m_pSelItem==m_pFocItem->up->up )){
		Stop();
	}

	DeleteItem( m_pFocItem, m_iFocLine );
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::SetFocusedItem( TCommonItem* pItem, int iLine, WORD iCol )
{
//	if( pItem!=m_pFocItem || iCol!=m_iFocCol ){
//		if( m_Hint ){
//			m_Hint->ReleaseHandle();
//			delete m_Hint;
//			m_Hint = NULL;
//		}
//
//		if( m_HintTimer ){
//			m_HintTimer->Enabled = false;
//			delete m_HintTimer;
//			m_HintTimer = NULL;
//		}
//		Hint = "";
//	}

//	if( !HintStr.IsEmpty() ){
//        Hint = HintStr;
//		if( !m_Hint ){
//			m_Hint = new TSkinHintWindow(this);
//			m_HintTimer = new TTimer(this);
//			m_HintTimer->Interval = 800;
//			m_HintTimer->OnTimer = HintTimerProc;
//		}
//
//		if( m_HintTimer ){
//			m_HintTimer->Enabled = false;
//			m_HintTimer->Enabled = true;
//        }
//	}

	if( pItem!=m_pFocItem ){
		if( m_pFocItem ){ //取消前一焦点
			m_pFocItem->SetFocus( 0 );

			if( m_iFocLine>=0 ){
				DrawItem( m_pFocItem, m_iFocLine, m_iFocCol );
			}
		}

		if( pItem ){ //显示当前焦点
			pItem->SetFocus( iCol );
			DrawItem( pItem, iLine, iCol );
		}
		else{
//			HintStr = "";
			Hint = "";
        }

		m_pFocItem = pItem;
		m_iFocLine = iLine;
		m_iFocCol = iCol;
	}
	else{
		if( pItem && iCol!=m_iFocCol ){
			pItem->SetFocus( iCol );
			DrawItem( pItem, iLine, m_iFocCol ); //取消前一焦点列
			DrawItem( pItem, iLine, iCol );	//显示当前焦点列
		}

		m_iFocLine = iLine;
		m_iFocCol = iCol;
	}
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoApplyPreStyle(TObject* Sender)
{
	if( m_pFocItem && m_pFocItem->GetOnlineState()==e_Downloaded ){
		PostMessage( Parent->Handle, UM_APPLYSTYLE, 0, LPARAM(m_pFocItem) );
	}
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoApplyUseStyle(TObject* Sender)
{
	if( m_pFocItem && m_pFocItem->GetOnlineState()==e_Downloaded ){
		PostMessage( Parent->Handle, UM_APPLYSTYLE, 1, LPARAM(m_pFocItem) );
	}
}
// ---------------------------------------------------------------------------

void __fastcall TStylePanel::DoSaveLocalStyle(TObject* Sender)
{
	if( m_pFocItem ){
		PostMessage( Parent->Handle, UM_SAVELOCALSTYLE, 0, 0 );
	}
}
// ---------------------------------------------------------------------------


