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

#include <vcl.h>
#pragma hdrstop

#include "VSTPathFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GeneralDialogPanel"
#pragma link "LineEdit"
#pragma link "TextButton"
#pragma link "CKBox"
#pragma resource "*.dfm"
TVSTPathForm *VSTPathForm;

#include <shlobj.h>
#include <System.IOUtils.hpp>
#include "GlobalUnit.h"
TCHAR u_lpSelectDir[MAX_PATH];  //当前初始化目录路径
//---------------------------------------------------------------------------

int CALLBACK BrowserCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	switch( uMsg )
	{
		case BFFM_INITIALIZED:
			::SendMessage(hWnd, BFFM_SETSELECTION, 1, (long)(u_lpSelectDir) );
			break;
		default:
			break;
	}
	return   0;
}
//---------------------------------------------------------------------------

__fastcall TVSTPathForm::TVSTPathForm(TComponent* Owner, bool bShowCheck)
	: TForm(Owner)
{
	m_ckHide->Visible = bShowCheck;
	m_lblMessage->Visible = !bShowCheck;
}
//---------------------------------------------------------------------------

void __fastcall TVSTPathForm::FormCreate(TObject *Sender)
{
	m_BKGPanel->Font->Size = g_szFont9;
	m_BtnCancel->FontSize = g_szFont9;
	m_BtnSure->FontSize = g_szFont9;
	m_btnVSTPath->FontSize = g_szFont9;
	m_btnVSTiPath->FontSize = g_szFont9;
	m_editVST->SetTextFont(g_strTxtFontName, g_szFont9);
	m_editVSTi->SetTextFont(g_strTxtFontName, g_szFont9);
	m_ckHide->Font->Name = g_strTxtFontName;
	m_ckHide->Font->Size = g_szFont9;

	String strVSTPath, strVSTiPath;

	if( g_strVSTPath.empty() ){
		strVSTPath = g_strExecutePath.c_str();
		strVSTPath += VST_DIRNAME;
	}
	else
		strVSTPath = g_strVSTPath.c_str();

	if( g_strVSTiPath.empty() ){
		strVSTiPath = g_strExecutePath.c_str();
		strVSTiPath += VST_DIRNAME;
	}
	else
		strVSTiPath = g_strVSTiPath.c_str();

	m_editVST->Text = strVSTPath;
	m_editVSTi->Text = strVSTiPath;

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

void __fastcall TVSTPathForm::VSTPathClick(TObject *Sender)
{
	TTextButton* pBtn = (TTextButton*)Sender;
	TLineEdit* pEdit = pBtn==m_btnVSTPath ? m_editVST : m_editVSTi;

	wcscpy( u_lpSelectDir,  (LPTSTR)(pBtn->Text.c_str()) );
	if( CrnBrowseDir(Handle, (LPTSTR)( str2wstr("请选择文件路径").c_str() ), u_lpSelectDir ) ) {
		pEdit->Text = wstr2str(u_lpSelectDir).c_str();
	}
}
//---------------------------------------------------------------------------

void __fastcall TVSTPathForm::SureClick(TObject *Sender)
{
	wcscpy( m_lpSelectVSTDir,  (LPTSTR)(m_editVST->Text.c_str()) );
	wcscpy( m_lpSelectVSTiDir, (LPTSTR)(m_editVSTi->Text.c_str()) );
}
//---------------------------------------------------------------------------

bool TVSTPathForm::CrnBrowseDir(HWND hOwner, LPTSTR lpTitle, LPTSTR lpSelectDir)
{
	bool bReslut = false;
	LPMALLOC pShellMalloc;
	if (SHGetMalloc(&pShellMalloc) == NO_ERROR){

		BROWSEINFO bi;
		WCHAR wszDisplayName[MAX_PATH];
		memset(&bi, 0x00, sizeof(bi));
		bi.hwndOwner = hOwner;
		bi.pidlRoot = 0;
		bi.pszDisplayName = wszDisplayName;
		bi.lpszTitle = lpTitle;
		bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
		bi.lpfn = BrowserCallbackProc;
		bi.lParam=LPARAM(lpSelectDir);

		LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
		if (pidl){
			bReslut= SHGetPathFromIDList(pidl, lpSelectDir);
			pShellMalloc->Free(pidl);
		}

		pShellMalloc->Release();
	}

	return bReslut;
}
//---------------------------------------------------------------------------


