// BlockItem_InputProps.cpp : implementation file
//

#include "stdafx.h"
#include "xpeluc01.h"
#include "BlockItem_OutputProps.h"
#include "BlockItem_Output.h"
#include "BlockItem_Virtual.h"
#include "SchemeDoc.h"


// CBlockItem_InputProps dialog

IMPLEMENT_DYNAMIC(CBlockItem_OutputProps, CDialog)

CBlockItem_OutputProps::CBlockItem_OutputProps(CWnd* pParent /*=NULL*/, CBlockItem_Output* pOwner)
	: CDialog(CBlockItem_OutputProps::IDD, pParent)
	, m_strName(_T(""))
{
	m_pOwner = pOwner;
}

CBlockItem_OutputProps::~CBlockItem_OutputProps()
{
	if ( m_hWnd )
		DestroyWindow();
}

void CBlockItem_OutputProps::DoDataExchange(CDataExchange* pDX)
{
	if ( m_pOwner )
	{
		m_strName = m_pOwner->GetName();
	}
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_PORTNAME, m_strName);
	DoDataExchangeComboBox(pDX);
}


BEGIN_MESSAGE_MAP(CBlockItem_OutputProps, CDialog)
	ON_BN_CLICKED(ID_APPLY, &CBlockItem_OutputProps::OnBnClickedApply)
END_MESSAGE_MAP()


// CBlockItem_OutputProps message handlers

BOOL CBlockItem_OutputProps::OnInitDialog()
{
	CDialog::OnInitDialog();
	SetWindowText(L"Virtual Output Propertes...");
	CComboBox* pCombo = (CComboBox*)GetDlgItem(IDC_COMBOPORTASSIGN);

	if ( m_pOwner )
	{
		CBlockItem_Virtual* pParentBlock = m_pOwner->GetParentBlock();
		if ( !pParentBlock )
		{
			CEdit* pEdit = (CEdit*)GetDlgItem(IDC_PORTNAME);
			pEdit->SetWindowText(L"Cant be assigned...");
			pEdit->EnableWindow(FALSE);
			pCombo->EnableWindow(FALSE);
		}
	}
	

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

void CBlockItem_OutputProps::DoDataExchangeComboBox(CDataExchange *pDX)
{
	if ( !pDX->m_bSaveAndValidate )
	{
		CComboBox* pCombo = (CComboBox*)GetDlgItem(IDC_COMBOPORTASSIGN);
		pCombo->ResetContent();
		pCombo->InsertString(0, L"None");
		pCombo->SetItemData(0, -1);
		DWORD dwSel = 0;
		if ( m_pOwner )
		{
			CBlockItem_Virtual *pParent = m_pOwner->GetParentBlock();
			if ( pParent )
			{
				DWORD dwPorts = pParent->GetOutputCount();
				for ( DWORD i = 0; i < dwPorts; i++ )
				{
					CString str;
					CBlockItem_Output *pOutput;
					if ( pParent->GetOutputBlock(i, &pOutput) )
					{
						if ( pOutput == m_pOwner )
						{
							dwSel = i + 1;
							str.Format(L"*%d [%s]", i, pOutput->GetName());
						}
						else
							str.Format(L"%d [%s]", i, pOutput->GetName());
					}
					else
						str.Format(L"%d", i);
					int nItem = pCombo->InsertString(i+1, str);
					pCombo->SetItemData(nItem, i);
				}
			}
		}
		pCombo->SetCurSel(dwSel);
	}
	else
	{
		CComboBox* pCombo = (CComboBox*)GetDlgItem(IDC_COMBOPORTASSIGN);
		m_pOwner->SetName(m_strName);
		int nSel = pCombo->GetCurSel();
		m_dwPort = (DWORD)pCombo->GetItemData(nSel);
	}
}
void CBlockItem_OutputProps::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
	DestroyWindow();
}

void CBlockItem_OutputProps::OnOK()
{
	CDialog::OnOK();
	if ( m_pOwner )
	{
		if ( m_dwPort != -1 )
			m_pOwner->Assign(m_dwPort);
		else
			m_pOwner->UnAssign();
		m_pOwner->m_pDocument->UpdateAllViews(NULL);
		//UpdatePortList();
		CBlockItem_Virtual *pParent;
		if ( pParent = m_pOwner->GetParentBlock() )
		{
			pParent->RefreshOutputBlocks();
		}
	}
	DestroyWindow();
}

BOOL CBlockItem_OutputProps::UpdatePortList()
{
	CComboBox* pCombo = (CComboBox*)GetDlgItem(IDC_COMBOPORTASSIGN);
	/*int nOldSel = pCombo->GetCurSel();*/
	CDataExchange dx(this, FALSE);
	DoDataExchangeComboBox(&dx);
	if ( m_pOwner )
	{
		CBlockItem_Virtual *pParent = m_pOwner->GetParentBlock();
/*		if ( pParent )
		{
			if ( nOldSel < (int)pParent->GetInputCount() + 1 )
			{
				pCombo->SetCurSel(nOldSel);
			}
			else
			{
				pCombo->SetCurSel(0);
			}
		}*/
	}
	return TRUE;
}
void CBlockItem_OutputProps::OnBnClickedApply()
{
	CDataExchange dx(this, TRUE);
	DoDataExchange(&dx);
	if ( m_pOwner )
	{
		if ( m_dwPort != -1 )
			m_pOwner->Assign(m_dwPort);
		else
			m_pOwner->UnAssign();
		m_pOwner->m_pDocument->UpdateAllViews(NULL);
		//UpdatePortList();
		CBlockItem_Virtual *pParent;
		if ( pParent = m_pOwner->GetParentBlock() )
		{
			pParent->RefreshOutputBlocks();
		}
	}
}
