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

// SplitterView.cpp : implementation file
//

#include "stdafx.h"
#include "DsLavSplit.h"
#include "DsLavSplitterView.h"

#include "MainFrm.h"
#include "DsLavTreeView.h"
#include "DsLavListView.h"
#include "DsLavVideoView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDsLavSplitterView

IMPLEMENT_DYNCREATE(CDsLavSplitterView, CView)

CDsLavSplitterView::CDsLavSplitterView()
{
	TRACE(_T("constructing CDsLavSplitterView\n"));

	// Left pane width initial.
	m_leftPaneWidth = 400;

	// Hide show left pane - restore width.
	m_prevLeftPaneWidth = 0;

}

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


BEGIN_MESSAGE_MAP(CDsLavSplitterView, CView)
	//{{AFX_MSG_MAP(CDsLavSplitterView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP

	ON_MESSAGE(WM_USER_HIDE_LEFT_PANE, OnHideLeftPane)


END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDsLavSplitterView drawing

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

	// Get document 
	CDsLavDoc * pDoc = GetDocument();
	if(!pDoc)
		return;

}

/////////////////////////////////////////////////////////////////////////////
// CDsLavSplitterView diagnostics

#ifdef _DEBUG
void CDsLavSplitterView::AssertValid() const
{
	CView::AssertValid();
}
void CDsLavSplitterView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
// Added as original DsLavView files replaced with DsLavSplitter
CDsLavDoc* CDsLavSplitterView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDsLavDoc)));
	return (CDsLavDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDsLavSplitterView message handlers

int CDsLavSplitterView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	TRACE(_T("in CDsLavSplitterView::OnCreate()\n"));

	// Base first.
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// 8/22-2 update comments
	// Create the main splitter view. There are two panes (2 columns, 1 row).
	// The left pane is a Tree view, the right pane a ListView.
	//  Note right pane Video View is created by m_wndSplitter.
	m_wndSplitter.CreateStatic(this, 1, 2);
	CCreateContext * pContext = (CCreateContext *)lpCreateStruct->lpCreateParams;
	lpCreateStruct->style |= WS_OVERLAPPED;

	// Create left TreeView pane.
	m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CDsLavTreeView), CSize(0,0), pContext);
	// Create right ListView pane.
	m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CDsLavListView), CSize(0,0), pContext);

	// Set CApp handles.
	((CDsLavApp*)AfxGetApp())->m_hwndSplitterView = GetSafeHwnd();
	((CDsLavApp*)AfxGetApp())->m_hwndTreeView = m_wndSplitter.GetPane(0, 0)->GetSafeHwnd();
	((CDsLavApp*)AfxGetApp())->m_hwndListView = m_wndSplitter.GetPane(0, 1)->GetSafeHwnd();

	// Current left pane is ListView
	((CDsLavApp*)AfxGetApp())->m_hwndCurRightPane = m_wndSplitter.GetPane(0, 1)->GetSafeHwnd();

	return 0; // OnCreate must return 0 to continue the creation of the CWnd object	
}

// Initial or new size.
void CDsLavSplitterView::OnSize(UINT nType, int cx, int cy) 
{
	TRACE(_T("in CDsLavSplitterView::OnSize()\n"));

	// Base class first
	CView::OnSize(nType, cx, cy);
	int minWidth = 1;

	// Update m_leftPaneWidth member.
	if(cx && cy)
	{
		int width = 0;
		m_wndSplitter.GetColumnInfo(0, width, minWidth);

		// If hidden, left pane will be one pix. See OnHideLeftPane().
		if(width)
			m_leftPaneWidth = width;
	}	
	// Must call to establish window.
	m_wndSplitter.MoveWindow(0, 0, cx, cy);
	m_wndSplitter.SetColumnInfo(0, m_leftPaneWidth, minWidth);
	m_wndSplitter.RecalcLayout(); 
}

// Start up - init panes.
void CDsLavSplitterView::OnInitialUpdate() 
{
	TRACE(_T("in CDsLavSplitterView::OnInitialUpdate()\n"));
	CRect rect;
	GetClientRect(&rect);

	// Left pane width initial.
	m_leftPaneWidth = rect.Width() / 4;
	int minWidth = 50;
	m_wndSplitter.SetColumnInfo(0, m_leftPaneWidth, minWidth);

	// Recalc for new size.
	m_wndSplitter.RecalcLayout();

	// Init Tree and List Views.
	for(int i = 0; i < 2; i++) // left and right panes - 0 is TreeView. 
	{
		CView* pView = (CView*)m_wndSplitter.GetPane(0, i);
		pView->OnInitialUpdate();
	}
	// Set App ptr to SplitterView.
	((CDsLavApp*)AfxGetApp())->m_pSplitterView = this;
}

// 7/22 - update comments
// Replace right CMySplitterWnd splitter View. 
// Called from: CMainFrame::StartVideoPlayback
//				CMainFrame::StartDvdPlayback
//				CMainFrame::StartCdPlayback
//				CMainFrame::ContinueCdPlayback
//				CMainFrame::SetVideoWindow
//				CMainFrame::StopVideoPlayback
//	
BOOL CDsLavSplitterView::ReplaceView(CRuntimeClass* pRTClass)
{
	return m_wndSplitter.ReplaceView(pRTClass);
}


// Typically, splitter panes are hidden by destroying 
// and creating windows. Here we cheat and just 
// slide it over. Note user can still grab the hidden
// pane.
// May change this later.
//  8/22 revised for enter key
LRESULT CDsLavSplitterView::OnHideLeftPane(WPARAM wParam, LPARAM lParam)	// WM_USER_HIDE_LEFT_PANE
{
	// Note: m_prevLeftPaneWidth is ONLY set and used here
	// and in OnInitialUpdate() above.

	// Enter key pressed in ControlsBar.
	BOOL enterKeyPressed = wParam;

	// Hide.
	if(lParam)
	{
		// Save current width to member.
		int i;
		m_wndSplitter.GetColumnInfo(0, m_prevLeftPaneWidth, i);

		// Set left pane to 1 pix wide.
		m_wndSplitter.SetColumnInfo(0, 1, 1);

		// Recalc for new size.
		m_wndSplitter.RecalcLayout();	
	}
	else
	{
		// Get previous width.
		int width = m_prevLeftPaneWidth;
		if(width < 150)
			width = 150;

		// Set the left pane to previous.
		m_wndSplitter.SetColumnInfo(0, width, 0);

		// Recalc for new size.
		m_wndSplitter.RecalcLayout();
	}
	// If enter key pressed, leave focus as is.
	if(enterKeyPressed)
		return 0;

	// Else set focus to right pane listView/videoview to 
	// enable Mouse Wheel and Space Bar handling.
	CView* pView = (CView*)m_wndSplitter.GetPane(0, 1);
	pView->SetFocus();

	return 0;
}

// 8/22-5
// If left pane hidden.
BOOL CDsLavSplitterView::LeftPaneIsHidden()
{
	// Get current left pane width.
	int width = 0, i;
	m_wndSplitter.GetColumnInfo(0, width, i);
	if(width < 3)
		return TRUE;

	// Visible
	return FALSE;
}


