www.pudn.com > fastplayer-0.1.zip > FastPlayerView.cpp, change:2002-01-08,size:15291b


// FastPlayerView.cpp : implementation of the CFastPlayerView class 
// 
 
#include "stdafx.h" 
#include "FastPlayer.h" 
 
#include "FastPlayerDoc.h" 
#include "FastPlayerView.h" 
 
#include "MainFrm.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CFastPlayerView 
 
IMPLEMENT_DYNCREATE(CFastPlayerView, ETSLayoutFormView) 
 
BEGIN_MESSAGE_MAP(CFastPlayerView, ETSLayoutFormView) 
	//{{AFX_MSG_MAP(CFastPlayerView) 
	ON_WM_SIZE() 
	ON_BN_CLICKED(IDC_PAUSE, OnPause) 
	ON_BN_CLICKED(IDC_STOP, OnStop) 
	ON_BN_CLICKED(IDC_PLAY, OnPlay) 
	ON_WM_TIMER() 
	ON_WM_HSCROLL() 
	ON_BN_CLICKED(IDC_MUTE, OnMute) 
	ON_BN_CLICKED(IDC_FULLSCREEN, OnFullscreen) 
	ON_BN_CLICKED(IDC_VOLUME_UP, OnVolumeUp) 
	ON_BN_CLICKED(IDC_VOLUME_DOWN, OnVolumeDown) 
	ON_BN_CLICKED(IDC_RESET_SIZE, OnResetSize) 
	ON_BN_CLICKED(IDC_FILTERS, OnFilters) 
	ON_BN_CLICKED(IDC_OPTIONS, OnOptions) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFastPlayerView construction/destruction 
 
CFastPlayerView::CFastPlayerView() 
	: ETSLayoutFormView(CFastPlayerView::IDD) 
{ 
	//{{AFX_DATA_INIT(CFastPlayerView) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	m_bAlreadySubclassed = FALSE; 
	m_bDisableVideoResize = FALSE; 
	m_bTimerActivated = FALSE; 
	m_bProgressMovement = FALSE; 
} 
 
CFastPlayerView::~CFastPlayerView() 
{ 
} 
 
void CFastPlayerView::DoDataExchange(CDataExchange* pDX) 
{ 
	ETSLayoutFormView::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CFastPlayerView) 
	DDX_Control(pDX, IDC_VOLUME, m_Volume); 
	DDX_Control(pDX, IDC_PROGRESS, m_Progress); 
	//}}AFX_DATA_MAP 
} 
 
BOOL CFastPlayerView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
	cs.style |= WS_CLIPCHILDREN; 
 
	return ETSLayoutFormView::PreCreateWindow(cs); 
} 
 
void CFastPlayerView::OnInitialUpdate() 
{ 
	HRESULT hr; 
 
	if (!m_bAlreadySubclassed) 
	{ 
		ETSLayoutFormView::OnInitialUpdate(); 
		GetParentFrame()->RecalcLayout(); 
		ResizeParentToFit(); 
 
		// Define the layout 
 
		/*  
			|-------------------------| 
			|                         | 
			|       Video Window      | 
			|                         | 
			|-------------------------| 
			| Progress Slider         | 
			|-------------------------| 
			| Play, etc.    | Volume  | 
			|-------------------------| 
 
		*/ 
 
		CreateRoot(VERTICAL) 
			<< (pane(HORIZONTAL, GREEDY) << item(IDC_FRAME, GREEDY)) 
			<< (pane(HORIZONTAL, ABSOLUTE_VERT)  
				<< item(IDC_PROGRESS, GREEDY) 
				<< item(IDC_RESET_SIZE, NORESIZE) 
				<< item(IDC_FULLSCREEN, NORESIZE | ALIGN_RIGHT)) 
			<< (pane(HORIZONTAL, ABSOLUTE_VERT)  
				<< item(IDC_PLAY, NORESIZE) 
				<< item(IDC_PAUSE, NORESIZE) 
				<< item(IDC_STOP, NORESIZE) 
				<< item(IDC_STATIC_VOLUME, GREEDY) 
				<< item(IDC_VOLUME, NORESIZE) 
				<< item(IDC_MUTE, NORESIZE) 
				<< item(IDC_DUMMY, GREEDY) 
				<< item(IDC_FILTERS, NORESIZE) 
				<< item(IDC_OPTIONS, NORESIZE)); 
 
		UpdateLayout(); 
		((CMainFrame*)GetParentFrame())->ResizeToMin(); 
 
		// Set the bitmap buttons 
		COLORREF	crBtnColor; 
		// Calculate a color effect for hilighting the button 
		crBtnColor = ::GetSysColor(COLOR_BTNFACE) + RGB(30, 30, 30); 
 
		m_btnPlay.SubclassDlgItem(IDC_PLAY, this); 
		m_btnPlay.SetIcon(IDI_PLAY); 
		m_btnPlay.SetColor(CButtonST::BTNST_COLOR_BK_IN, crBtnColor); 
		m_btnPlay.SetTooltipText("Play [P]"); 
		 
		m_btnPause.SubclassDlgItem(IDC_PAUSE, this); 
		m_btnPause.SetIcon(IDI_PAUSE); 
		m_btnPause.SetColor(CButtonST::BTNST_COLOR_BK_IN, crBtnColor); 
		m_btnPause.SetTooltipText("Pause [Space]"); 
 
		m_btnStop.SubclassDlgItem(IDC_STOP, this); 
		m_btnStop.SetIcon(IDI_STOP); 
		m_btnStop.SetColor(CButtonST::BTNST_COLOR_BK_IN, crBtnColor); 
		m_btnStop.SetTooltipText("Stop [S or ESC]"); 
 
		m_btnResetSize.SubclassDlgItem(IDC_RESET_SIZE, this); 
		m_btnResetSize.SetIcon(IDI_RESET_SIZE); 
		m_btnResetSize.SetColor(CButtonST::BTNST_COLOR_BK_IN, crBtnColor); 
		m_btnResetSize.SetTooltipText("Reset Size [R]"); 
 
		m_btnMute.SubclassDlgItem(IDC_MUTE, this); 
		m_btnMute.SetIcon(IDI_MUTE); 
		m_btnMute.SetColor(CButtonST::BTNST_COLOR_BK_IN, crBtnColor); 
		m_btnMute.SetTooltipText("Mute [M]"); 
 
		m_btnFullScreen.SubclassDlgItem(IDC_FULLSCREEN, this); 
		m_btnFullScreen.SetIcon(IDI_FULLSCREEN); 
		m_btnFullScreen.SetColor(CButtonST::BTNST_COLOR_BK_IN, crBtnColor); 
		m_btnFullScreen.SetTooltipText("FullScreen Mode [F or Alt+Enter]"); 
 
		m_btnOptions.SubclassDlgItem(IDC_OPTIONS, this); 
		m_btnOptions.SetIcon(IDI_OPTIONS); 
		m_btnOptions.SetColor(CButtonST::BTNST_COLOR_BK_IN, crBtnColor); 
		m_btnOptions.SetTooltipText("Program options"); 
 
		m_btnFilters.SubclassDlgItem(IDC_FILTERS, this); 
		m_btnFilters.SetIcon(IDI_FILTERS); 
		m_btnFilters.SetColor(CButtonST::BTNST_COLOR_BK_IN, crBtnColor); 
		m_btnFilters.SetTooltipText("Playback filters"); 
 
		// Set the sliders range 
		m_Progress.SetRange(0, 1000); 
		m_Volume.SetRange(50, 100); 
 
		m_bAlreadySubclassed = TRUE; 
	} 
 
	// If is a video file loaded Set the video window owner and size 
	CFastPlayerDoc *pDoc = GetDocument(); 
	if (pDoc->m_currentStatus != CFastPlayerDoc::statusEmpty) 
	{ 
		// Set the window owner 
		hr = pDoc->m_pVideoWindow->put_Owner((OAHWND)m_hWnd); 
		if (FAILED(hr)) 
		{ 
			TRACE0("Failed to establish video window owner\n"); 
			return; 
		} 
		ResizeToVideoSize(); 
 
		// Set the message drain 
		hr = pDoc->m_pVideoWindow->put_MessageDrain((OAHWND)m_hWnd); 
		if (FAILED(hr)) 
		{ 
			TRACE0("Failed to establish video window message drain\n"); 
			return; 
		} 
 
 
		// Verify if the video has audio 
		BOOL bAudio = (GetDocument()->m_pBasicAudio != NULL); 
		m_Volume.EnableWindow(bAudio); 
		m_btnMute.EnableWindow(bAudio); 
 
		// Enable FullScreen button 
		m_btnFullScreen.EnableWindow(); 
 
		// If the timer is not running, start it 
		SetTimer(WM_PROGRESS, 1000, NULL); 
		m_bTimerActivated = TRUE; 
		m_bProgressMovement = FALSE; 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CFastPlayerView diagnostics 
 
#ifdef _DEBUG 
void CFastPlayerView::AssertValid() const 
{ 
	ETSLayoutFormView::AssertValid(); 
} 
 
void CFastPlayerView::Dump(CDumpContext& dc) const 
{ 
	ETSLayoutFormView::Dump(dc); 
} 
 
CFastPlayerDoc* CFastPlayerView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFastPlayerDoc))); 
	return (CFastPlayerDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CFastPlayerView message handlers 
 
void CFastPlayerView::OnDraw(CDC* pDC)  
{ 
	ETSLayoutFormView::OnDraw(pDC); 
 
	// Get the document 
	CFastPlayerDoc *pDoc = GetDocument(); 
	// If no video is playing, fill the frame in black 
	if (pDoc->m_currentStatus == CFastPlayerDoc::statusEmpty) 
	{ 
		// Get the frame size 
		CRect rcFrame; 
		GetDlgItem(IDC_FRAME)->GetWindowRect(&rcFrame); 
		ScreenToClient(&rcFrame); 
		CBrush brBlack(RGB(0,0,0)); 
		pDC->FillRect(&rcFrame, &brBlack); 
 
		// Set the buttons to disabled 
		m_btnPlay.EnableWindow(FALSE); 
		m_btnPause.EnableWindow(FALSE); 
		m_btnStop.EnableWindow(FALSE); 
		m_btnMute.EnableWindow(FALSE); 
		m_btnFullScreen.EnableWindow(FALSE); 
		m_btnResetSize.EnableWindow(FALSE); 
		m_btnFilters.EnableWindow(FALSE); 
 
		// If the timer is running, stop it 
		if (m_bTimerActivated) 
		{ 
			KillTimer(WM_PROGRESS); 
			m_bTimerActivated = FALSE; 
		} 
		return; 
	} 
 
	// Update the buttons 
	if (pDoc->m_currentStatus == CFastPlayerDoc::statusRunning) 
	{ 
		m_btnPlay.EnableWindow(); 
		m_btnPlay.SetCheck(1); 
		m_btnPause.EnableWindow(); 
		m_btnPause.SetCheck(0); 
		m_btnStop.EnableWindow(); 
		m_btnStop.SetCheck(0); 
	} 
	else if (pDoc->m_currentStatus == CFastPlayerDoc::statusPaused) 
	{ 
		m_btnPlay.EnableWindow(); 
		m_btnPlay.SetCheck(0); 
		m_btnPause.EnableWindow(); 
		m_btnPause.SetCheck(1); 
		m_btnStop.EnableWindow(); 
		m_btnStop.SetCheck(0); 
	} 
	else if (pDoc->m_currentStatus == CFastPlayerDoc::statusStopped) 
	{ 
		m_btnPlay.EnableWindow(); 
		m_btnPlay.SetCheck(0); 
		m_btnPause.EnableWindow(); 
		m_btnPause.SetCheck(0); 
		m_btnStop.EnableWindow(); 
		m_btnStop.SetCheck(1); 
	} 
 
	m_btnResetSize.EnableWindow(); 
	m_btnFilters.EnableWindow(); 
 
	if (pDoc->IsInFullScreenMode()) 
		m_btnFullScreen.SetCheck(1); 
 
	// Update the audio volume 
	m_Volume.SetPos(pDoc->GetAudioVolume()); 
} 
 
void CFastPlayerView::OnSize(UINT nType, int cx, int cy)  
{ 
	HRESULT hr; 
	ETSLayoutFormView::OnSize(nType, cx, cy); 
 
	if (m_bDisableVideoResize) 
		return; 
 
	// If there is a vide file loaded, resize it accordingly 
	CFastPlayerDoc *pDoc = GetDocument(); 
	// If no video is playing, fill the frame in black 
	if (pDoc->m_currentStatus != CFastPlayerDoc::statusEmpty) 
	{ 
		// Get the frame size 
		CRect rcFrame; 
		GetDlgItem(IDC_FRAME)->GetWindowRect(&rcFrame); 
		ScreenToClient(&rcFrame); 
		hr = pDoc->m_pVideoWindow->SetWindowPosition(rcFrame.left, rcFrame.top, rcFrame.Width(), rcFrame.Height()); 
		if (FAILED(hr)) 
		{ 
			TRACE0("Failed to stablish video window size\n"); 
			return; 
		} 
	} 
	 
} 
 
void CFastPlayerView::ResizeParentToFit2(LONG lWidth, LONG lHeight) 
{ 
	// Taken from CScrollView::ResizeParentToFit 
 
	// determine current size of the client area as if no scrollbars present 
	CRect rectClient; 
	GetWindowRect(rectClient); 
	CRect rect = rectClient; 
	CalcWindowRect(rect); 
	 
	rectClient.left += rectClient.left - rect.left; 
	rectClient.top += rectClient.top - rect.top; 
	rectClient.right -= rect.right - rectClient.right; 
	rectClient.bottom -= rect.bottom - rectClient.bottom; 
	rectClient.OffsetRect(-rectClient.left, -rectClient.top); 
	ASSERT(rectClient.left == 0 && rectClient.top == 0); 
 
	// dermine and set size of frame based on desired size of view 
	CRect rectFrame; 
	CFrameWnd* pFrame = GetParentFrame(); 
	ASSERT_VALID(pFrame); 
	pFrame->GetWindowRect(rectFrame); 
	CSize size = rectFrame.Size(); 
	size.cx += lWidth - rectClient.right; 
	size.cy += lHeight - rectClient.bottom; 
	pFrame->SetWindowPos(NULL, 0, 0, size.cx, size.cy, 
		SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE); 
 
} 
 
void CFastPlayerView::ResizeToVideoSize(int nWidthFactor, int nHeightFactor) 
{ 
	HRESULT hr; 
	 
	// Get the document 
	CFastPlayerDoc *pDoc = GetDocument(); 
 
	// Get the original video size 
	long lWidth, lHeight; 
	hr = pDoc->m_pBasicVideo->GetVideoSize(&lWidth, &lHeight); 
	if (FAILED(hr)) 
	{ 
		TRACE0("Failed to retreive video size\n"); 
		return; 
	} 
 
	// Resize to the factors given 
	lWidth = (LONG)lWidth * nWidthFactor / 100; 
	lHeight = (LONG)lHeight * nHeightFactor / 100; 
 
	// Get the size of the frame and the window 
	CRect rcFrame, rcWindow; 
	GetDlgItem(IDC_FRAME)->GetClientRect(&rcFrame); 
	GetClientRect(&rcWindow); 
 
	// Calculate the final size 
	long lFinalWidth = rcWindow.Width() - rcFrame.Width() + 14 + lWidth; 
	long lFinalHeight = rcWindow.Height() - rcFrame.Height() + 4 + lHeight; 
 
	// Set the final size 
	m_bDisableVideoResize = TRUE; 
	ResizeParentToFit2(lFinalWidth, lFinalHeight); 
	m_bDisableVideoResize = FALSE; 
 
	// Get the new frame size and position 
	GetDlgItem(IDC_FRAME)->GetWindowRect(&rcFrame); 
	ScreenToClient(&rcFrame); 
	// Set the video position 
	hr = pDoc->m_pVideoWindow->SetWindowPosition(rcFrame.left, rcFrame.top, rcFrame.Width(), rcFrame.Height()); 
	if (FAILED(hr)) 
	{ 
		TRACE0("Failed to set the new video window size and position\n"); 
		return; 
	} 
	 
	TRACE("Original video size: x(%d) y(%d)\nNew video size: x(%d) y(%d)\n", lWidth, lHeight, rcFrame.Width(), rcFrame.Height()); 
} 
 
void CFastPlayerView::OnPause()  
{ 
	if (GetDocument()->m_currentStatus == CFastPlayerDoc::statusEmpty) 
		return; 
 
	if (GetDocument()->m_currentStatus != CFastPlayerDoc::statusPaused) 
	{ 
		GetDocument()->Pause(); 
		m_btnPlay.SetCheck(0); 
		m_btnPause.SetCheck(1); 
		m_btnStop.SetCheck(0); 
	} 
	else if (GetDocument()->m_currentStatus != CFastPlayerDoc::statusEmpty) 
		GetDocument()->Play(); 
} 
 
void CFastPlayerView::OnStop()  
{ 
	if (GetDocument()->m_currentStatus == CFastPlayerDoc::statusEmpty) 
		return; 
 
	if (GetDocument()->m_currentStatus != CFastPlayerDoc::statusStopped) 
	{ 
		GetDocument()->Stop(); 
		m_btnPlay.SetCheck(0); 
		m_btnPause.SetCheck(0); 
		m_btnStop.SetCheck(1); 
	} 
	else 
		m_btnStop.SetCheck(1); 
} 
 
void CFastPlayerView::OnPlay()  
{ 
	if (GetDocument()->m_currentStatus == CFastPlayerDoc::statusEmpty) 
		return; 
 
	if (GetDocument()->m_currentStatus != CFastPlayerDoc::statusRunning) 
	{ 
		GetDocument()->Pause(); 
		m_btnPlay.SetCheck(1); 
		m_btnPause.SetCheck(0); 
		m_btnStop.SetCheck(0); 
	} 
	else 
		m_btnPlay.SetCheck(1); 
} 
 
void CFastPlayerView::OnTimer(UINT nIDEvent)  
{ 
	if (nIDEvent == WM_PROGRESS) 
	{ 
		if (!m_bProgressMovement) 
		{ 
			// Update the progress slider 
			m_Progress.SetPos(GetDocument()->GetPlaybackPosition()); 
		} 
	} 
	 
	ETSLayoutFormView::OnTimer(nIDEvent); 
} 
 
void CFastPlayerView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
	if (pScrollBar->GetDlgCtrlID() == IDC_PROGRESS) 
	{ 
		if (nSBCode == TB_THUMBTRACK) 
		{ 
			// Disable progress update 
			m_bProgressMovement = TRUE; 
			return; 
		} 
		else if (nSBCode == TB_ENDTRACK) 
		{ 
			// Signal the document to change position 
			GetDocument()->ChangePlaybackPosition(m_Progress.GetPos()); 
		} 
	} 
	else if (pScrollBar->GetDlgCtrlID() == IDC_VOLUME) 
	{ 
		// Signal the document to change position 
		GetDocument()->ChangeAudioVolume(m_Volume.GetPos()); 
	} 
	 
	ETSLayoutFormView::OnHScroll(nSBCode, nPos, pScrollBar); 
} 
 
void CFastPlayerView::OnMute()  
{ 
	if (GetDocument()->IsAudioMuted()) 
	{ 
		GetDocument()->UnMute(); 
		m_Volume.EnableWindow(TRUE); 
		m_btnMute.SetCheck(0); 
	} 
	else 
	{ 
		GetDocument()->Mute(); 
		m_Volume.EnableWindow(FALSE); 
		m_btnMute.SetCheck(1); 
	} 
} 
 
void CFastPlayerView::OnFullscreen()  
{ 
	CFastPlayerDoc *pDoc = GetDocument(); 
	if (pDoc->m_currentStatus != CFastPlayerDoc::statusEmpty) 
		pDoc->SetFullScreen(!pDoc->IsInFullScreenMode()); 
} 
 
 
void CFastPlayerView::OnVolumeUp()  
{ 
	if (GetDocument()->m_currentStatus == CFastPlayerDoc::statusEmpty) 
		return; 
 
	int nPos = m_Volume.GetPos(); 
	if (nPos < 100) 
	{ 
		nPos++; 
		GetDocument()->ChangeAudioVolume(nPos); 
		m_Volume.SetPos(nPos); 
	} 
} 
 
void CFastPlayerView::OnVolumeDown()  
{ 
	if (GetDocument()->m_currentStatus == CFastPlayerDoc::statusEmpty) 
		return; 
 
	int nPos = m_Volume.GetPos(); 
	if (nPos > 50) 
	{ 
		nPos--; 
		GetDocument()->ChangeAudioVolume(nPos); 
		m_Volume.SetPos(nPos); 
	} 
} 
 
void CFastPlayerView::OnResetSize()  
{ 
	if (GetDocument()->m_currentStatus != CFastPlayerDoc::statusEmpty) 
		if (!GetDocument()->IsInFullScreenMode()) 
			ResizeToVideoSize(); 
} 
 
void CFastPlayerView::OnFilters()  
{ 
	if (GetDocument()->m_currentStatus != CFastPlayerDoc::statusEmpty) 
		GetDocument()->ShowFilters(); 
} 
 
void CFastPlayerView::OnOptions()  
{ 
	GetDocument()->ShowOptionsDialog(); 
}