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

#include <vcl.h>
#pragma hdrstop

#include "GlobalUnit.h"
#include "VSTiFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GeneralDialogPanel"
#pragma link "TextButton"
#pragma link "ComboBoxComponent"
#pragma link "PNGButton"
#pragma link "SkinTrackBar"
#pragma resource "*.dfm"

#define TIMER_INTERVAL 50

TVSTiForm *VSTiForm;
//---------------------------------------------------------------------------
__fastcall TVSTiForm::TVSTiForm( TComponent* Owner, BYTE iVSTi, BYTE iChno, int iStart, int len )
	: TForm(Owner)
{
	m_iVSTi = iVSTi;
	m_iChno = iChno;
	m_iStartBar = iStart;
	m_lenBar = len;

	String strCaption = m_BKGPanel->Caption + L" - ";
	strCaption += g_lstVSTi[iVSTi-0x80]->strVSTName;
	m_BKGPanel->Caption = strCaption;

	m_dblStart = iStart;
	m_dblEnd = iStart + len;

	int nBeatsPerMeasure;
	switch (g_iMelodyTimeSignature) {
	case 0:
		nBeatsPerMeasure = 4;
		break;
	case 1:
		nBeatsPerMeasure = 3;
		break;
	case 2:
		nBeatsPerMeasure = 2;
		break;
	case 3:
		nBeatsPerMeasure = 3;
		break;
	}

	m_iCurrPos = 0;
	m_dblPos = 0;
	m_dblStep = TIMER_INTERVAL * (double)g_nMelodyTempo	/ nBeatsPerMeasure / 60.0 / len;
}
//---------------------------------------------------------------------------

void __fastcall TVSTiForm::FormCreate(TObject *Sender)
{
	Screen->Cursor = crHourGlass;

	m_BKGPanel->Font->Size = g_szFont9;
	m_btnCancel->FontSize = g_szFont9;
	m_btnOk->FontSize = g_szFont9;
	m_cbChannel->SetFont(g_strTxtFontName, g_szFont9);

	int nWidth(0), nHeight(0);
	if( f_MM_ShowVSTEditor( Handle, m_panShow->Handle, &nWidth, &nHeight, -1, m_iVSTi,
													g_lstVSTi[m_iVSTi-0x80]->strFileName )==S_OK ){
		if( nWidth<0 && nHeight<0 ){
			m_lblNoEditor->Visible = true;
		}
		else{
			if( nWidth==0 || nHeight==0 )
				BorderStyle = bsSizeable;

			if( nWidth==0 )
				nWidth = 300;
			if( nHeight==0 )
				nHeight = 150;

			SetBounds( Left, Top, nWidth + 50, nHeight + 134 );
		}
	}
	else{
		m_lblNoEditor->Visible = true;
	}

	for( int i=0; i<16; i++ ){
		m_cbChannel->AddItem( IntToStr(i+1) );
	}
	m_cbChannel->DefItemIndex = m_iChno;

	Screen->Cursor = crDefault;

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

void __fastcall TVSTiForm::FormClose(TObject *Sender, TCloseAction &Action)
{
	if( m_btnStop->Visible ){
		StopClick( NULL );
	}

	if( !m_lblNoEditor->Visible ){
		f_MM_ShowVSTEditor( NULL, NULL, NULL, NULL, -1, m_iVSTi, NULL );
	}
}
//---------------------------------------------------------------------------

void __fastcall TVSTiForm::FormResize(TObject *Sender)
{
	m_panShow->SetBounds( m_panShow->Left, m_panShow->Top,
					ClientWidth - 50, ClientHeight - 134 );

	m_tbPosition->Width = m_panShow->Width;
	m_tbPosition->Top = ClientHeight - 69;

	m_btnPlay->Top = ClientHeight - 43;
	m_btnStop->Top = m_btnPlay->Top;
	m_btnOk->Top = m_btnPlay->Top + 1;
	m_btnCancel->Top = m_btnOk->Top;
	m_cbChannel->Top = m_btnPlay->Top + 3;
	m_lblChannel->Top = m_btnPlay->Top + 6;

	m_btnOk->Left = ClientWidth - 148;
	m_btnCancel->Left = ClientWidth - 83;
}
//---------------------------------------------------------------------------

void __fastcall TVSTiForm::PlayClick(TObject *Sender)
{
	double dblPos = ( (double)m_tbPosition->Position / m_tbPosition->Max ) * ( m_dblEnd - m_dblStart ) + m_dblStart;

	m_btnPlay->Visible = false;
	m_btnStop->Visible = true;

	f_MM_PlayDemo( Handle, dblPos, m_iChno, -1, m_iVSTi, g_lstVSTi[m_iVSTi-0x80]->strFileName );

	SetPlayTimer( true, m_tTimer, m_tAccuracy, Handle, TIMER_INTERVAL );
}
//---------------------------------------------------------------------------

void __fastcall TVSTiForm::StopClick(TObject *Sender)
{
	m_btnStop->Visible = false;
	m_btnPlay->Visible = true;

	f_MM_Stop();
	SetPlayTimer(false, m_tTimer, m_tAccuracy);
}
//---------------------------------------------------------------------------

void __fastcall TVSTiForm::UMMediaTime(TMessage &Message)
{
	m_dblPos += m_dblStep;
	m_iCurrPos = (DWORD)( m_dblPos + 0.5 );

	if( m_iCurrPos==m_tbPosition->Position )
		return;

	if( m_iCurrPos >= m_tbPosition->Max ){
		m_dblPos = 0;
		m_iCurrPos = 0;

		StopClick( NULL );
	}

	m_tbPosition->Position = m_iCurrPos;
}
//---------------------------------------------------------------------------

void __fastcall TVSTiForm::UMEditorResize(TMessage &Message)
{
	int nWidth = Message.WParam,
		nHeight = Message.LParam;

	if( nWidth==0 )
		nWidth = 300;
	if( nHeight==0 )
		nHeight = 150;

	SetBounds( Left, Top, nWidth + 50, nHeight + 134 );
}
//---------------------------------------------------------------------------

void __fastcall TVSTiForm::PositionChange(TObject *Sender)
{
	if( m_iCurrPos!=m_tbPosition->Position ){
		m_iCurrPos = m_tbPosition->Position;
		m_dblPos = m_iCurrPos;

		if( m_btnStop->Visible ){
			SetPlayTimer(false, m_tTimer, m_tAccuracy);

			double dblPos = ( (double)m_tbPosition->Position / m_tbPosition->Max ) * ( m_dblEnd - m_dblStart ) + m_dblStart;
			f_MM_PlayDemo( Handle, dblPos, m_iChno, -1, m_iVSTi, g_lstVSTi[m_iVSTi-0x80]->strFileName );

			SetPlayTimer( true, m_tTimer, m_tAccuracy, Handle, TIMER_INTERVAL );
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TVSTiForm::ChannelChange(TComboBoxItem *Sender, int Index)
{
	m_iChno = Index;

	if( m_btnStop->Visible ){
		f_MM_ChangeDemoParam( m_iChno, -1 );
	}
}
//---------------------------------------------------------------------------

