
/*	Copyright (C) G. Jespersen
	See Licence.txt.*/

// NewTopicDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dslavsplit.h"
#include "NewTopicDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNewTopicDlg dialog


CNewTopicDlg::CNewTopicDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNewTopicDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNewTopicDlg)
	m_topicName = _T("");
	//}}AFX_DATA_INIT

	// Members - set by caller.
	m_pDoc = NULL;
	m_type = NONE;
	m_dlgTitle = _T("New Topic");
	m_topicName = _T("");
}


void CNewTopicDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNewTopicDlg)
	DDX_Control(pDX, IDC_COMBO_TYPES, m_ctrlComboTypes);
	DDX_Control(pDX, IDC_TOPIC_NODES_COMBO, m_ctrlComboNodes);
	DDX_CBString(pDX, IDC_TOPIC_NODES_COMBO, m_topicName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNewTopicDlg, CDialog)
	//{{AFX_MSG_MAP(CNewTopicDlg)
	//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNewTopicDlg message handlers

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

	// Set Title per caller string.
	SetWindowText(m_dlgTitle);

	// If editing topic.
	BOOL isEditing = FALSE;
	if(m_dlgTitle == TOPIC_EDIT_STR)
		isEditing = TRUE;

	
	// Caller must have m_pDoc.
	if(m_pDoc)
	{
		// Set Combo extended UI.
		//	- allows arrow keys.
		//	- disables mouse wheel.
		//	- good handling for invalid mouse clicks.
		m_ctrlComboNodes.SetExtendedUI(TRUE);
		m_ctrlComboTypes.SetExtendedUI(TRUE);

		// Set media type combo.
		InitTypesCombo();
		SetComboTypesSel(isEditing);

		// Set Nodes Combo.
		InitNodesCombo();

		// Set topicName if editing existing topic.
		if(isEditing)
		{
		}

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

// Used by caller when editing a Topic.
void CNewTopicDlg::SetTopicName(CString topicName)
{
	// Select predefined topic, or set edit to custom.
	if(m_ctrlComboNodes.SelectString(-1, topicName) == CB_ERR)
	{
		m_ctrlComboNodes.SetWindowText(topicName);
	}
}

//////////////////////////////////////////////////////////
// Media Types Combo:
void CNewTopicDlg::InitTypesCombo()
{
	// todo - need code in CMediaItem or elswhere for user type strings.
	m_ctrlComboTypes.AddString(_T("Unknown Stream"));
	m_ctrlComboTypes.AddString(_T("Audio Stream"));
	m_ctrlComboTypes.AddString(_T("Video Stream"));
	m_ctrlComboTypes.AddString(_T("Video File"));
	m_ctrlComboTypes.AddString(_T("Audio File"));

	// Set width.
	//
	// Find the longest string in the combo box.
	CString str;
	CSize   sz;
	int     dx=0;
	CDC*    pDC = m_ctrlComboTypes.GetDC();
	int nTypes = m_ctrlComboTypes.GetCount();
	for(int i = 0; i < nTypes; i++)
	{
		m_ctrlComboTypes.GetLBText(i, str);
		sz = pDC->GetTextExtent(str);

		if(sz.cx > dx)
			dx = sz.cx;
	}
	m_ctrlComboTypes.ReleaseDC(pDC);

	// Adjust the width for the vertical scroll bar and the left and right border.
	dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2 * ::GetSystemMetrics(SM_CXEDGE);

	// Set the width of the list box so that every item is completely visible.
	m_ctrlComboTypes.SetDroppedWidth(dx);
}

void CNewTopicDlg::SetComboTypesSel(BOOL isEditing)
{
	// A new Root topic - user specifies type.
	if(m_type == NONE)
	{
		CString str = _T("Unknown Stream");
		m_ctrlComboTypes.SelectString(-1, str);
	}
	// Type per input from caller - fixed 
	// if not editing the Topic. 
	else
	{
		// Need a temp CMediaItem for type string functions.
		CMediaItem item;
		item.m_mediaType = m_type;
		CString str = item.GetStreamTypeString();
		m_ctrlComboTypes.SelectString(-1, str);
		// Type per parent topic - disable window if not editing.
		if(!isEditing)
			m_ctrlComboTypes.EnableWindow(FALSE);
	}
}

// Get string as type.
MEDIA_TYPE CNewTopicDlg::GetComboTypesSelMediaType()
{
	// todo - need code in CMediaItem for user strings.
	MEDIA_TYPE type = NONE;
	CString str;

	int index = m_ctrlComboTypes.GetCurSel();
	m_ctrlComboTypes.GetLBText(index, str);
	// m_topicName

	if(str == _T("Unknown Stream"))
		type = NONE;
	else if(str == _T("Audio Stream"))
		type = AUDU;
	else if(str == _T("Video Stream"))
		type = VIDU;
	else if(str == _T("Video File"))
		type = VIDF;
	else if(str == _T("Audio File"))
		type = AUDF;
	return type;
}


//////////////////////////////////////////////////////////
// Topic Nodes Combo:
void CNewTopicDlg::InitNodesCombo()
{
	// Default topics are part of the TOPIC_STR_ALIAS array.
	// Note Favorites is not included.
	int nAlises = m_pDoc->m_aliasAr.GetSize();
	for(int i = 0; i < nAlises; i++)
	{
		// Add str.
		m_ctrlComboNodes.AddString(m_pDoc->m_aliasAr[i].str);
	}

	// Set width.
	//
	// Find the longest string in the combo box.
	CString str;
	CSize   sz;
	int     dx=0;
	CDC*    pDC = m_ctrlComboNodes.GetDC();
	int nNodes = m_ctrlComboNodes.GetCount();
	for(int i = 0; i < nNodes; i++)
	{
		m_ctrlComboNodes.GetLBText(i, str);
		sz = pDC->GetTextExtent(str);

		if(sz.cx > dx)
			dx = sz.cx;
	}
	m_ctrlComboNodes.ReleaseDC(pDC);

	// Adjust the width for the vertical scroll bar and the left and right border.
	dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2 * ::GetSystemMetrics(SM_CXEDGE);

	// Set the width of the list box so that every item is completely visible.
	m_ctrlComboNodes.SetDroppedWidth(dx);
}

////////////////////////////////////////////////////////////
// OK

// User pressed OK.
void CNewTopicDlg::OnOK() 
{
	// Get input.
	UpdateData();

	// Validate.
	//
	// Type,
	m_type = GetComboTypesSelMediaType();
	if(m_type == NONE)
	{
		AfxMessageBox(_T("Please enter a valid Media Type."));
		return;
	}
	// Name.
	if(m_topicName == _T(""))
	{
		AfxMessageBox(_T("Please enter a Topic Name."));
		return;
	}
	// OK.
	CDialog::OnOK();
}

