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

#include <vcl.h>
#pragma hdrstop

#include "GlobalUnit.h"
#include "ImportFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GeneralDialogPanel"
#pragma link "LineEdit"
#pragma link "TextButton"
#pragma link "ComboBoxComponent"
#pragma link "SkinTrackBar"
#pragma link "CapButton"
#pragma resource "*.dfm"
TImportForm *ImportForm;
//---------------------------------------------------------------------------
__fastcall TImportForm::TImportForm(TComponent* Owner, String DefaultFile,
											int TS, int Tempo, int iMode)
	: TForm(Owner)
{
	m_iMode = iMode;
    m_MediaFilePath->Text = DefaultFile;

    MediaFilePath = DefaultFile;

    DoFilePathChange(NULL);

    m_RetrunValue = 0;

	this->TS = TS;
    this->Tempo = Tempo;

	this->m_TSTrackBar->Position = TS;
	this->m_TempoTrackBar->Position = Tempo;

	if( iMode==1 )
		m_panGeneral->Caption = "插入音频片段";

	for( int i=0; i<ComponentCount; i++ ){
		if(Components[i]->ClassNameIs( TLabel::ClassName() ) )
			((TLabel*)Components[i])->ControlStyle>>csOpaque;
	}
}
//---------------------------------------------------------------------------

void __fastcall TImportForm::DoBrows(TObject *Sender)
{
	TOpenDialog * pDlg = new TOpenDialog(NULL);
	pDlg->Options << ofFileMustExist;
	pDlg->Title = L"打开";
	pDlg->FileName = ExtractFileName(m_MediaFilePath->Text);
	pDlg->InitialDir = ExtractFilePath(m_MediaFilePath->Text);

	pDlg->Filter = "mp3文件 (*.mp3)|*.MP3|Wave文件 (*.wav)|*.WAV";

	if( m_iMode==0 )
		pDlg->Filter += "|midi文件 (*.mid)|*.MID";

    if(ExtractFileExt(m_MediaFilePath->Text).CompareIC(".mid") == 0)
    {
		pDlg->FilterIndex = 3;
	}
	else if(ExtractFileExt(m_MediaFilePath->Text).CompareIC(".wav") == 0)
	{
		pDlg->FilterIndex = 2;
	}
	else
	{
		pDlg->FilterIndex = 1;
    }

    if(pDlg->Execute(Handle))
    {
        m_MediaFilePath->Text = pDlg->FileName;
        MediaFilePath = pDlg->FileName;
        //DoFilePathChange(NULL);
	}

	delete pDlg;
}
//---------------------------------------------------------------------------

void __fastcall TImportForm::DoFilePathChange(TObject *Sender)
{
	UnicodeString var_FilePath(m_MediaFilePath->Text);

    MediaFilePath = var_FilePath;

    //检查文件是否存在
    if(System::Ioutils::TFile::Exists(MediaFilePath))
        m_OKButton->Enabled = true;
    else
    	m_OKButton->Enabled = false;;

	if( m_iMode==0 && ( ExtractFileExt(var_FilePath).CompareIC(".wav") == 0
							|| ExtractFileExt(var_FilePath).CompareIC(".mp3") == 0)){
    	///////////
		m_panGeneral->Height = 206;
        Height = 206;

        m_TipsLabel->Top = 162;
        m_OKButton->Top = 155;
        m_CancelButton->Top = 155;

        m_TSTrackBar->Visible = true;
        m_TempoTrackBar->Visible = true;

        m_TSLabel->Visible = true;
        m_TempoLabel->Visible = true;

        m_TipsLabel->Visible = true;

        m_ShowTempo->Visible = true;

        ts_44->Visible = true;
        ts_34->Visible = true;
        ts_24->Visible = true;
        ts_68->Visible = true;

		m_ShowTempoMin->Visible = true;
        m_ShowTempoMax->Visible = true;
        m_TempoUpDown->Visible = true;
    }
	else{
    	m_TSTrackBar->Visible = false;
        m_TempoTrackBar->Visible = false;
        m_ShowTempo->Visible = false;

        m_TSLabel->Visible = false;
        m_TempoLabel->Visible = false;

        m_TipsLabel->Visible = false;

        ts_44->Visible = false;
        ts_34->Visible = false;
        ts_24->Visible = false;
        ts_68->Visible = false;

        m_ShowTempoMin->Visible = false;
        m_ShowTempoMax->Visible = false;
        m_TempoUpDown->Visible = false;

        ///////////
		m_panGeneral->Height = 140;
        Height = 140;

        m_TipsLabel->Top = 96;
        m_OKButton->Top = 89;
        m_CancelButton->Top = 89;
    }
}
//---------------------------------------------------------------------------

int __fastcall TImportForm::ShowModal()
{
	int var_Result = TForm::ShowModal();

    TS = m_TSTrackBar->Position;
	Tempo = m_TempoTrackBar->Position;
    MediaFilePath = m_MediaFilePath->Text;

    return var_Result;
}
//---------------------------------------------------------------------------


void __fastcall TImportForm::DoOK(TObject *Sender)
{
	m_RetrunValue = 1;
	Close();
}
//---------------------------------------------------------------------------


void __fastcall TImportForm::DoTempoTrackBar(TObject *Sender)
{
	String var_Tempo(m_TempoTrackBar->Position);
    m_ShowTempo->Text = var_Tempo;
	//m_ShowTempo->Caption = var_Tempo;
}
//---------------------------------------------------------------------------

void __fastcall TImportForm::DoTSTrackBar(TObject *Sender)
{
	TS = m_TSTrackBar->Position;
}
//---------------------------------------------------------------------------


void __fastcall TImportForm::DoCancel(TObject *Sender)
{
	Close();
}
//---------------------------------------------------------------------------
void __fastcall TImportForm::DoShowTempoChange(TObject *Sender)
{
	if(m_ShowTempo->Text == "")
    {
        return;
    }

	int var_Tempo = m_ShowTempo->Text.ToInt();

    if(var_Tempo < 50/*m_TSTrackBar->Min*/ || var_Tempo > 200/*m_TSTrackBar->Max*/)
    {
        //m_ShowTempo->Text = String(m_TSTrackBar->Position);
    }
    else
    {
        m_TempoTrackBar->Position = var_Tempo;
        m_TempoUpDown->Position = var_Tempo;
    }
}
//---------------------------------------------------------------------------

void __fastcall TImportForm::DoTempoUpDown(TObject *Sender, TUDBtnType Button)
{
	if(Button == btNext)
    {
		m_TempoUpDown->Position += 1;
        m_TempoTrackBar->Position += 1;
    }
	else
	{
		m_TempoUpDown->Position -= 1;
        m_TempoTrackBar->Position -= 1;
    }
}
//---------------------------------------------------------------------------

void __fastcall TImportForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	switch( Key ){
	case VK_RETURN:
		if(m_OKButton->Enabled)
			ModalResult = mrOk;
		break;

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

void __fastcall TImportForm::FormCreate(TObject *Sender)
{
	m_panGeneral->Font->Size = g_szFont9;
	m_BrowsButton->FontSize = g_szFont9;
	m_OKButton->FontSize = g_szFont9;
	m_CancelButton->FontSize = g_szFont9;
	m_MediaFilePath->SetTextFont(g_strTxtFontName, g_szFont9);
	m_ShowTempo->SetTextFont(g_strTxtFontName, g_szFont9);
}
//---------------------------------------------------------------------------

