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

// MySplitterWnd.cpp : implementation file
//

#include "stdafx.h"
#include "DsLavSplit.h"
#include "MainFrm.h"
#include "DsLavDoc.h"
#include "MySplitterWnd.h"

#include "DsLavTreeView.h"	// for ReplaceView()
#include "DsLavListView.h"
#include "DsLavVideoView.h"

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


/////////////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CMySplitterWnd, CSplitterWnd)

CMySplitterWnd::CMySplitterWnd()
{
	TRACE(_T("In CMySplitterWnd::CMySplitterWnd - constructing CMySplitterWnd\n"));
}
/////////////////////////////////////////////////////////////////////////////
CMySplitterWnd::~CMySplitterWnd()
{
	TRACE(_T("Destructing CMySplitterWnd\n"));	
}

/////////////////////////////////////////////////////////////////////////////
//
// 7/22 - update comments
// Called from CDsLavSplitterView::ReplaceView() for switching
// between a CDsLavListView and a CDsLavVideoView.
//
// Return true if successfull switch or if view already selected.
BOOL CMySplitterWnd::ReplaceView(CRuntimeClass* pRTClass)
{
	TRACE(_T("in CMySplitterWnd::ReplaceView\n"));

	// Get CDoc.
	CDsLavDoc* pDoc = ((CDsLavApp*)AfxGetApp())->m_pDocument;
	if(!pDoc)
	{
		TRACE(_T("in CMySplitterWnd::ReplaceView - pDoc NULL\n"));

		AfxMessageBox(_T("CMySplitterWnd ReplaceView - pDoc Null."));
		return FALSE;
	}

	// If View param is already selected, no need to 
	// destroy it and then create it again.
	// Note we return TRUE as requested View is created.
	CWnd* pWndRightPane = GetPane(0,1); 
	if(pWndRightPane->IsKindOf(pRTClass))
	{
		TRACE(_T("in CMySplitterWnd::ReplaceView - view already selected\n"));

		// 11/22-2
		if(pWndRightPane->IsKindOf(RUNTIME_CLASS(CDsLavListView)))
		{
			TRACE(_T("In CMySplitterWnd::ReplaceView - calling StateRestore.\n"));

			// We get here if an audio file or audio stream has just stopped.
			//
			// Call ListView's Restore() to scroll and highlight 
			// the media item just stopped.
			CDsLavListView * pListView = NULL;
			pListView = (CDsLavListView*)pWndRightPane;
			pListView->StateRestore();
		}
		// Must return TRUE.
		return TRUE;
	}

    // Set flag so that document will not be deleted when view is destroyed.
	// m_bAutoDelete is a non-documented member of CDocument.
	pDoc->m_bAutoDelete = FALSE;

	// Set CApp handle for existing view NULL.
	if(pWndRightPane->IsKindOf(RUNTIME_CLASS(CDsLavListView)))
		((CDsLavApp*)AfxGetApp())->m_hwndListView = NULL;
	else if(pWndRightPane->IsKindOf(RUNTIME_CLASS(CDsLavVideoView)))
		((CDsLavApp*)AfxGetApp())->m_hwndVideoView = NULL;

    // Delete existing view: 
	pWndRightPane->DestroyWindow();

	// Prepare context for new right pane view.
	CCreateContext context;
	context.m_pNewViewClass   = pRTClass;
	context.m_pCurrentDoc     = pDoc;
	context.m_pNewDocTemplate = NULL;
	context.m_pLastView       = NULL;
	context.m_pCurrentFrame   = NULL;

	// Create the new right pane view.
	CreateView(0, 1, pRTClass, CSize(0,0), &context);

	// Get ptr to new view.
	CView* pNewView = (CView*)GetPane(0,1);

	// Update CApp handles.
	//
	// Get new HWND
	HWND newHwnd = pNewView->GetSafeHwnd();
	// Get new CWnd.
	CWnd* pWndNewRightPane = GetPane(0,1);

	// Set ListView HWND.
	if(pWndNewRightPane->IsKindOf(RUNTIME_CLASS(CDsLavListView)))
		((CDsLavApp*)AfxGetApp())->m_hwndListView = newHwnd;

	// Or set VideoView HWND.
	else if(pWndNewRightPane->IsKindOf(RUNTIME_CLASS(CDsLavVideoView)))
		((CDsLavApp*)AfxGetApp())->m_hwndVideoView = newHwnd;

	// Set Current RightPane HWND.
	((CDsLavApp*)AfxGetApp())->m_hwndCurRightPane = newHwnd;

	// Update CMySplitterWnd.
	RecalcLayout();

	// Update the New View.
	TRACE(_T("CMySplitterWnd::ReplaceView - OnInitalUpdate() call\n"));
	pNewView->OnInitialUpdate();
	pNewView->SendMessage(WM_PAINT);

	// Reset flag or document data does not get destroyed and memory leaks.
	pDoc->m_bAutoDelete = TRUE; 
	
	// 8/22-2 - Fix menu
	// Set Active View - must else Frame menu items disabled in VideoView.
	CMainFrame * pFrame = pDoc->GetMainFrame();
	pFrame->SetActiveView(pNewView);

	/* Set focus to view - to enable controls bar hide left button. 
	// not working after error msg box - caller needs to set.
	pWndNewRightPane->SetFocus();
	::SetFocus(newHwnd);*/

	return TRUE;
}
