// imp_configwindow.cpp : implementation file
//

#include "stdafx.h"
#include "bsavefile.h"
#include "imp_configwindow.h"
#include "imp_block.h"


// CImpBlockConfigWindow dialog

IMPLEMENT_DYNAMIC(CImpBlockConfigWindow, CDialog)

CImpBlockConfigWindow::CImpBlockConfigWindow(CWnd* pParent /*=NULL*/, IUnknown* pUnkOuter)
	: CDialog(CImpBlockConfigWindow::IDD, pParent)
	, m_strName(_T(""))
	, m_bCheckFile(FALSE)
	, m_bSkipEmptyLayers(FALSE)
	, m_bDisableInput(FALSE)
{
	m_pImpBlock = (CImpBlock*)pUnkOuter;
}

CImpBlockConfigWindow::~CImpBlockConfigWindow()
{
}

void CImpBlockConfigWindow::DoDataExchange(CDataExchange* pDX)
{
	EnterCriticalSection(&m_pImpBlock->m_cs);

	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDITPATH, m_pImpBlock->m_strPath);
	DDX_Text(pDX, IDC_EDITNAME, m_pImpBlock->m_strName);
	DDX_Check(pDX, IDC_CHECKOVERWRITE, m_pImpBlock->m_bCheckOverwrite);
	DDX_Check(pDX, IDC_CHECKDONOTHING, m_pImpBlock->m_bDoNothing);

	LeaveCriticalSection(&m_pImpBlock->m_cs);

	if ( pDX->m_bSaveAndValidate )
		m_pImpBlock->SetModifiedFlag(TRUE);
}


BEGIN_MESSAGE_MAP(CImpBlockConfigWindow, CDialog)
	ON_BN_CLICKED(IDC_APPLY, &CImpBlockConfigWindow::OnBnClickedApply)
	ON_BN_CLICKED(IDC_BROWSE, &CImpBlockConfigWindow::OnBnClickedBrowse)
	ON_MESSAGE(WM_UPDATESTATUS, &CImpBlockConfigWindow::OnUpdateStatus)
	ON_BN_CLICKED(IDC_BUTTON1, &CImpBlockConfigWindow::OnBnClickedButton1)
END_MESSAGE_MAP()


// CImpBlockConfigWindow message handlers

void CImpBlockConfigWindow::OnCancel()
{
	CDialog::OnCancel();
	DestroyWindow();
}

void CImpBlockConfigWindow::OnOK()
{
	CDialog::OnOK();
	DestroyWindow();
}

void CImpBlockConfigWindow::OnBnClickedApply()
{
	CDataExchange dx(this, TRUE);
	DoDataExchange(&dx);
}

void CImpBlockConfigWindow::OnBnClickedBrowse()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState( ))
	BROWSEINFO bi;
	ZeroMemory(&bi, sizeof(bi));
	bi.hwndOwner = this->m_hWnd;
	bi.lpszTitle = L"Select folder";
	bi.ulFlags = BIF_NEWDIALOGSTYLE;
	bi.lpfn = browseProc;
	bi.lParam = (LPARAM)this;
	LPITEMIDLIST pIdList = SHBrowseForFolder(&bi);
	if ( pIdList )
	{
		LPWSTR pPath;
		pPath = new WCHAR[MAX_PATH];
		SHGetPathFromIDList(pIdList, pPath);
		CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDITPATH);
		pEdit->SetWindowText(pPath);
		delete(pPath);
	}
}

int CALLBACK CImpBlockConfigWindow::browseProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	switch ( uMsg )
	{
	case BFFM_INITIALIZED:
		{
			CEdit* pEdit = (CEdit*) ((CImpBlockConfigWindow*)lpData)->GetDlgItem(IDC_EDITPATH);
			CString str;
			pEdit->GetWindowText(str);
			::SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)str.GetString());
		}
		break;
	}
	return 0;
}

LRESULT CImpBlockConfigWindow::OnUpdateStatus(WPARAM wParam, LPARAM lParam)
{
	CStatic* pStatic = (CStatic*)GetDlgItem(IDC_STATICID);
	CString str;

	DWORD dwId;
	EnterCriticalSection(&m_pImpBlock->m_cs);
	dwId = m_pImpBlock->m_dwId;
	LeaveCriticalSection(&m_pImpBlock->m_cs);

	str.Format(L"%08x", m_pImpBlock->m_dwId);
	pStatic->SetWindowText(str);
	return 0;
}

BOOL CImpBlockConfigWindow::OnInitDialog()
{
	CDialog::OnInitDialog();

	OnUpdateStatus(0, 0);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CImpBlockConfigWindow::OnBnClickedButton1()
{
	EnterCriticalSection(&m_pImpBlock->m_cs);
	m_pImpBlock->m_dwId = 0;
	LeaveCriticalSection(&m_pImpBlock->m_cs);
	OnUpdateStatus(0, 0);
}
