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

// DvdNavDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dslavsplit.h"
#include "DvdNavDlg.h"

// Player
#include "Player.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDvdNavDlg dialog


CDvdNavDlg::CDvdNavDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDvdNavDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDvdNavDlg)
	m_strStaticChapter = _T("");
	m_strStaticDomain = _T("");
	m_strStaticTitle = _T("");
	m_strStaticDuration = _T("");
	//}}AFX_DATA_INIT
	m_pPlayer = NULL;
	m_curSelTitleIndex = 0;
	m_curSelChapterIndex = 0;

}


void CDvdNavDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDvdNavDlg)
	DDX_Control(pDX, IDC_COMBO_TITLE, m_ctrlComboTitle);
	DDX_Control(pDX, IDC_COMBO_CHAPTER, m_ctrlComboChapter);
	DDX_Text(pDX, IDC_STATIC_CHAPTER, m_strStaticChapter);
	DDX_Text(pDX, IDC_STATIC_DOMAIN, m_strStaticDomain);
	DDX_Text(pDX, IDC_STATIC_TITLE, m_strStaticTitle);
	DDX_Text(pDX, IDC_STATIC_TIME, m_strStaticDuration);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDvdNavDlg, CDialog)
	//{{AFX_MSG_MAP(CDvdNavDlg)
	ON_BN_CLICKED(IDC_BUTTON_GOTO_TC, OnButtonGotoTitleChapter)
	ON_CBN_CLOSEUP(IDC_COMBO_TITLE, OnCloseupComboTitle)
	ON_BN_CLICKED(IDC_BUTTON_TITLE, OnButtonTitleMenu)
	ON_BN_CLICKED(IDC_BUTTON_ROOT, OnButtonRootMenu)
	//}}AFX_MSG_MAP

	// From CPlayer.
	ON_MESSAGE(WM_USER_DVD_NAV_UPDATE,  OnPlayerUpdate)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CDvdNavDlg message handlers

BOOL CDvdNavDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// Must have player set by caller.
	if(!m_pPlayer)
		return TRUE;

	// Set app hwnd. Used by CPlayer::PrepareSeekingDvd().
	HWND hWnd = GetSafeHwnd();
	if(hWnd)
		((CDsLavApp*)AfxGetApp())->m_hwndDvdNavDlg = hWnd;

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

void CDvdNavDlg::SetDvdData()
{
	// Ensure.
	if(!m_pPlayer || !m_pPlayer->IsRunningDvd())
		return;

	CString str;
	// Static.
	m_strStaticTitle.Format(_T("Current Title: %u"),	 m_pPlayer->DvdGetCurTitle());
	m_strStaticChapter.Format(_T("Current Chapter: %u"), m_pPlayer->DvdGetCurChapter());
	m_strStaticDuration.Format(_T("Title Duration: %s"), m_pPlayer->DvdGetTotalTimeStr());
	m_strStaticDomain.Format(_T("Current Domain: %s"),	 m_pPlayer->DvdGetCurDomainStr());

	// Title Combo. Note combos are not sorted.
	m_ctrlComboTitle.ResetContent();
	int sz = (int)m_pPlayer->DvdGetNumTitles();
	for(int i = 0; i < sz; i ++)
	{
		str.Format(_T("%d"), i + 1);
		m_ctrlComboTitle.AddString(str);
	}
	str.Format(_T("%d"), (int)m_pPlayer->DvdGetCurTitle());
	m_curSelTitleIndex = m_ctrlComboTitle.SelectString(-1, str);


	// Chapter Combo.
	m_ctrlComboChapter.ResetContent();
	sz = (int)m_pPlayer->DvdGetNumChapters();
	for(int i = 0; i < sz; i ++)
	{
		str.Format(_T("%d"), i + 1);
		m_ctrlComboChapter.AddString(str);
	}
	str.Format(_T("%d"), (int)m_pPlayer->DvdGetCurChapter());
	m_curSelChapterIndex = m_ctrlComboChapter.SelectString(-1, str);

	UpdateData(FALSE);
}

// Called from CPlayer::PrepareSeekingDvd() when 
// a title or chapter changes.
LRESULT CDvdNavDlg::OnPlayerUpdate(WPARAM wParam, LPARAM lParam)	// WM_USER_DVD_NAV_UPDATE
{
	TRACE(_T("In CDvdNavDlg::OnPlayerUpdate.\n"));

	// Update the dlg.
	SetDvdData();

	return 0;
}

// Goto pressed.
void CDvdNavDlg::OnButtonGotoTitleChapter() 
{
	// Get cuttent settings.
	UpdateData(TRUE);

	// Ensure.
	if(!m_pPlayer || !m_pPlayer->IsRunningDvd())
		return;

	// Get title & chapter numbers - returns zero based index.
	// See SetDvdData();
	UINT title = (UINT)m_ctrlComboTitle.GetCurSel() + 1;
	UINT chapter = (UINT)m_ctrlComboChapter.GetCurSel() + 1;

	// Set new Title and Chapter.
	m_pPlayer->DvdTitleChapterPlay(title, chapter);
}

void CDvdNavDlg::OnButtonTitleMenu() 
{
	// Ensure.
	if(!m_pPlayer || !m_pPlayer->IsRunningDvd())
		return;
	// Go to Title Menu.
	m_pPlayer->DvdMenuCall(DVD_MENU_Title);	
}

void CDvdNavDlg::OnButtonRootMenu() 
{
	// Ensure.
	if(!m_pPlayer || !m_pPlayer->IsRunningDvd())
		return;
	// Go to Root Menu.
	m_pPlayer->DvdMenuCall(DVD_MENU_Root);	
}

// User has chosen a new title - get the 
// number of chapters for it.
void CDvdNavDlg::OnCloseupComboTitle() 
{
	// Get current sel index
	int titleIndex = m_ctrlComboTitle.GetCurSel();

	// If no change, bail.
	if(titleIndex == m_curSelTitleIndex) // all zero based
		return;

	// Update title index to user's selection,
	m_curSelTitleIndex = titleIndex; 

	CString str;

	// Set the chapter combo to a single chapter for now.
	// If we fail to get the actual number of chapters 
	// below, the single chapter (#1) should work.
	m_ctrlComboChapter.ResetContent();
	str.Format(_T("%d"), 1);
	m_ctrlComboChapter.AddString(str);
	m_curSelChapterIndex = m_ctrlComboChapter.SelectString(-1, str);

	// Get chapters.
	//
	// Ensure.
	if(!m_pPlayer || !m_pPlayer->IsRunningDvd())
		return;
	int nChapters = m_pPlayer->DvdGetNumChaptersInTitle(titleIndex + 1);
	if(!nChapters)
		return;

	// Update the chapters Combo.
	m_ctrlComboChapter.ResetContent();
	for(int i = 0; i < nChapters; i ++)
	{
		str.Format(_T("%d"), i + 1);
		m_ctrlComboChapter.AddString(str);
	}
	str.Format(_T("%d"), 1); // first
	m_curSelChapterIndex = m_ctrlComboChapter.SelectString(-1, str);	
}

// Close button pressed.
void CDvdNavDlg::OnOK() 
{
	// Set hwnd used by CPlayer::PrepareSeekingDvd() NULL.
	((CDsLavApp*)AfxGetApp())->m_hwndDvdNavDlg = NULL;
	
	CDialog::OnOK();
}


