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

// DsLavVideoView.cpp : implementation file
//

#include "stdafx.h"
#include "dslavsplit.h"
#include "DsLavSplitterView.h"
#include "DsLavVideoView.h"
//#include "DsLavListView.h"	// For OnVideoEvent switch views 8/22-2 out

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

/////////////////////////////////////////////////////////////////////////////
// CDsLavVideoView

IMPLEMENT_DYNCREATE(CDsLavVideoView, CView)

CDsLavVideoView::CDsLavVideoView()
{
	m_redrawVid = FALSE;
}

CDsLavVideoView::~CDsLavVideoView()
{
	TRACE(_T("Destructing CDsLavVideoView\n"));
}


BEGIN_MESSAGE_MAP(CDsLavVideoView, CView)
	//{{AFX_MSG_MAP(CDsLavVideoView)
	ON_WM_MOUSEWHEEL()
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	ON_WM_KEYDOWN()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP

	// moved to CMainFrame Video callback
//	ON_MESSAGE(WM_USER_VIDEOEVENT, OnVideoEvent)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDsLavVideoView drawing

void CDsLavVideoView::OnDraw(CDC* pDC)
{
	//CDocument* pDoc = GetDocument();
	// TODO: add draw code here

	TRACE(_T("In CDsLavVideoView::OnDraw\n"));

	CDsLavDoc * pLavDoc = GetDocument();
	if(!pLavDoc)
		return;

	CMainFrame * pFrame = pLavDoc->GetMainFrame();

	// Background repainting and Video size changing.
	//
	// m_redrawVid flag set in OnSize() and OnEraseBkgnd().
	if(m_redrawVid && pFrame && pFrame->m_player.IsRunningOrPaused())
	{
		TRACE(_T("In CDsLavVideoView::OnDraw player running - m_redrawVid\n"));

		// Hide video.
		pFrame->m_player.PutVisible(TRUE);	

		// Repaint bkgnd.
		CRect r;
		GetClientRect(&r);
		pDC->FillSolidRect(0, 0, 
						   r.Width(), r.Height(), 
						   pDC->GetBkColor());
		// Show video.
		pFrame->m_player.PutVisible(FALSE);

		// Reset.
		m_redrawVid = FALSE;
	}
}

/////////////////////////////////////////////////////////////////////////////
// CDsLavVideoView message handlers

BOOL CDsLavVideoView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
{
	CDsLavDoc * pDoc = GetDocument();
	if(!pDoc)
		return CView::OnMouseWheel(nFlags, zDelta, pt);

	CMainFrame * pFrame = pDoc->GetMainFrame();		// could make class func
	if(!pFrame)
		return CView::OnMouseWheel(nFlags, zDelta, pt);

	HWND hwndBar = pFrame->m_controlsBar.GetSafeHwnd();
	if(!hwndBar)
		return CView::OnMouseWheel(nFlags, zDelta, pt);

	BOOL stepUp = zDelta > 0;
	LONG newVol = 0;

	// If Video is playing.
	if(pFrame->m_player.IsRunningOrPaused())
	{
		if(pFrame->m_player.IsRunning() &&
		   pFrame->m_player.StepVolume(stepUp, newVol))
		{
			// Set bar vol.
			::PostMessage(hwndBar, WM_USER_BAR_VOL, 0, (LPARAM)newVol);
		}
	}

	// Else nothing to do as View draws nada.
	else if(GetFocus() == this)
	{
		int g = 0;
	}
	// Base class.
	return CView::OnMouseWheel(nFlags, zDelta, pt);
}

// DVD menu??
void CDsLavVideoView::OnMouseMove(UINT nFlags, CPoint point) 
{
	CView::OnMouseMove(nFlags, point);

	CDsLavDoc * pDoc = GetDocument();
	if(!pDoc)
		return;

	CMainFrame * pFrame = pDoc->GetMainFrame();		// could make class func
	if(!pFrame)
		return;

	// If DVD is playing, we send client coords to 
	// the player for menu selection in domain
	// video set title menu.
	if(pFrame->m_player.IsRunningOrPaused() && 
	   pFrame->m_player.IsRunningDvd()		)
	{
		CPoint screenPt = point;
		ClientToScreen(&screenPt);

	//	TRACE(_T("In CDsLavVideoView::OnMouseMove calling DvdMouseSelect\n"));
		pFrame->m_player.DvdMouseSelect(point, screenPt);
	}
	
}

// 8/22-2 comments
// DVD menu - not used - see OnLButtonUp.
void CDsLavVideoView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	TRACE(_T("In CDsLavVideoView::OnLButtonDown nFlags=%u, ptx=%d, pty=%d\n"),
				nFlags, point.x, point.y); // 8/22-2
	// Base
	CView::OnLButtonDown(nFlags, point);
	return;

	TRACE(_T("In CDsLavVideoView::OnLButtonDown\n"));
	// Base
	CView::OnLButtonDown(nFlags, point);

	CDsLavDoc * pDoc = GetDocument();
	if(!pDoc)
		return;

	CMainFrame * pFrame = pDoc->GetMainFrame();		// could make class func
	if(!pFrame)
		return;

	// If DVD is playing, we send client coords to 
	// the player for menu selection in domain
	// video set title menu.
	if(pFrame->m_player.IsRunningOrPaused() && 
	   pFrame->m_player.IsRunningDvd()		)
	{
		CPoint screenPt = point;
		ClientToScreen(&screenPt);


		TRACE(_T("In CDsLavVideoView::OnLButtonDown calling DvdMouseActivate\n"));
		pFrame->m_player.DvdMouseActivate(point);
	//	pFrame->m_player.DvdMouseSelect(point, screenPt);
	}	
}

// 8/22-2 comments
// 10/30/22 - error fix
// User selected a DVD menu item.
void CDsLavVideoView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// error 10/30/22 - must have made the errors thansfering code for version 5.0
//	return CView::OnLButtonUp(nFlags, point); NO

	// Base - correct 10/30/22
	CView::OnLButtonUp(nFlags, point);

	CDsLavDoc * pDoc = GetDocument();
	if(!pDoc)
		return;

	CMainFrame * pFrame = pDoc->GetMainFrame();		// could make class func
	if(!pFrame)
		return;

	// If DVD is playing, we send client coords to 
	// the player for menu selection in domain
	// video set title menu.
	if(pFrame->m_player.IsRunningOrPaused() && 
	   pFrame->m_player.IsRunningDvd()		)
	{

		// error 10/30/2022 - all commented out ??
		//	TRACE(_T("In CDsLavVideoView::OnLButtonUp calling DvdMouseActivate\n"));
		//	pFrame->m_player.DvdMouseSelect(point);
		//	pFrame->m_player.DvdMouseActivate(point);

		// Fix 10/30/2022 - correct
		TRACE(_T("In CDsLavVideoView::OnLButtonUp calling DvdMouseActivate\n"));
		pFrame->m_player.DvdMouseActivate(point);
	}	
}


BOOL CDsLavVideoView::OnEraseBkgnd(CDC* pDC) 
{
	// Get player ptr from CMainFrame.
	CPlayer * pPlayer = GetPlayer();

	// Redraw the video and bkgnd once.
	// See OnDraw().
	if(pPlayer && pPlayer->IsRunningOrPaused())
	{
		TRACE(_T("In CDsLavVideoView::OnEraseBkgnd player running\n"));

		m_redrawVid = TRUE;
		return 1;			// 1 = handled - must - see msdn "IVideoWindow Interface"
	}
	// Base
	return CView::OnEraseBkgnd(pDC);
}

void CDsLavVideoView::OnSize(UINT nType, int cx, int cy) 
{
	// Base first
	CView::OnSize(nType, cx, cy);

	// Avoid start up call.
	if(!IsWindowVisible())
		return;

	// Ensure something to size.
	if(!cx && !cy)
		return;

	TRACE(_T("In CDsLavVideoView::OnSize - nType = %d\n"), nType);

	// Update Video size.
	CPlayer * pPlayer = GetPlayer();	
	if(pPlayer && pPlayer->IsRunningOrPaused())
	{
		TRACE(_T("In CDsLavVideoView::OnSize - player\n"));
		// Get ListView client size.
		CRect rect;
		GetClientRect(&rect);

		// Set the size of the video window.
		if(pPlayer->SetVideoRect(rect))
		{
			// Set flag to redraw new video
			// size and View's background.
			m_redrawVid = TRUE;
		}
	}
	// Else nothing to do
	else
	{
	}	
}

// Play/Pause with Space Bar.
void CDsLavVideoView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	TRACE(_T("In CDsLavVideoView::OnKeyDown\n"));

	// Space bar down.
	//
	// Pause / depause a playing video.  
	if(nChar == VK_SPACE && ((nFlags & KF_REPEAT) == 0) )  
	{
		CPlayer * pPlayer = GetPlayer();	
		if(pPlayer && pPlayer->IsRunningOrPaused())
		{
			// Toggle playback.
			pPlayer->TogglePlaybackPause();

			// Update Controls Bar.
			//
			// Paused.
			if(pPlayer->IsPaused())
			{
				// Set bar text to "Play" (depause).
				HWND hwndBar = ((CDsLavApp*)AfxGetApp())->m_hwndControlBar;
				if(hwndBar)
					::PostMessage(hwndBar, WM_USER_BAR_PAUSE, 0, 1); //m_hwndControlBar
			}
			else
			{
				// Set bar text to "Pause".
				HWND hwndBar = ((CDsLavApp*)AfxGetApp())->m_hwndControlBar;
				if(hwndBar)
					::PostMessage(hwndBar, WM_USER_BAR_PAUSE, 0, 0);
			}
			// Insure focus here.
			SetFocus();
		}
	}
	// Tab to Controls Bar. 7/22
	else if(nChar == VK_TAB)
	{
		// Tab from VideoView into ControlsBar. 
		//  Note we should only get here if a video is playing.
		//
		HWND hWndControlsBar = ((CDsLavApp*)AfxGetApp())->m_hwndControlBar;
		HWND hWndVideoView = ((CDsLavApp*)AfxGetApp())->m_hwndVideoView;

		// Ensure in VideoView with focus.
		if(hWndVideoView == NULL)
		{
			TRACE(_T("CDsLavVideoView::OnKeyDown hWndVideoView == NULL\n"));
			CView::OnKeyDown(nChar, nRepCnt, nFlags);
			return;
		}
		HWND hWndCurrent = NULL;
		CWnd * pWndCurFocus = GetFocus();
		if(pWndCurFocus)
			hWndCurrent = pWndCurFocus->GetSafeHwnd();
		if(hWndCurrent != hWndVideoView)
		{
			TRACE(_T("CDsLavVideoView::OnKeyDown hWndCurrent != hWndVideoView\n"));
			CView::OnKeyDown(nChar, nRepCnt, nFlags);
			return;
		}

		// Ensure video playing.
		BOOL bPlaying = FALSE;
		CDsLavDoc * pDoc = GetDocument();
		CMainFrame * pFrame = pDoc->GetMainFrame();
		if(pFrame && !pFrame->m_player.IsRunningOrPaused())
		{
			TRACE(_T("CDsLavVideoView::OnKeyDown video not playing\n"));
			CView::OnKeyDown(nChar, nRepCnt, nFlags);
			return;
		}

		// Tab to ControlsBar.
		if(hWndControlsBar)
			::SetFocus(hWndControlsBar);
	}
	// Base.
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}


//////////////////////////////////////////////////////////////////
// CPlayer Video related

// Get CMainFrame CPlayer ptr.
CPlayer * CDsLavVideoView::GetPlayer()
{
	TRACE(_T("In CDsLavVideoView::GetPlayer\n"));

	CDsLavDoc * pDoc = GetDocument();
	if(!pDoc)
		return 0;

	CMainFrame * pFrame = pDoc->GetMainFrame();		// could make class func
	if(!pFrame)
		return 0;

	// Return adress of CMainFrame CPlayer.
	return &pFrame->m_player;
}

/////////////////////////////////////////////////////////////////////////////
// CDsLavVideoView diagnostics
#ifdef _DEBUG
void CDsLavVideoView::AssertValid() const
{
	CView::AssertValid();
}
void CDsLavVideoView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
// See DsLavSplitter
CDsLavDoc* CDsLavVideoView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDsLavDoc)));
	return (CDsLavDoc*)m_pDocument;
}
#endif //_DEBUG










