/*
*******************************************************************************
* Copyright (c) 1996-97 Digi International Inc., All Rights Reserved          *
*                                                                             *
* This  software  contains  proprietary  and  confidential   information   of *
* Digi International  Inc.   By  accepting  transfer  of this copy, Recipient *
* agrees to retain this software in  confidence,  to  prevent  disclosure  to *
* others,  and  to make no  use of this software other than that for which it *
* was delivered.  This is an unpublished copyright work  Digi   International *
* Inc.    Except   as   permitted   by  federal  law,  17 USC 117, copying is *
* strictly prohibited.                                                        *
*                                                                             *
* Digi  International  Inc.  CONFIDENTIAL  -   (Digi    International    Inc. *
* Confidential  when   combined   with   the   aggregated  modules  for  this *
* product) OBJECT CODE ONLY SOURCE MATERIALS                                  *
*                                                                             *
* (C) COPYRIGHT Digi International Inc. 1996-97                               *
*                                                                             *
* All Rights Reserved                                                         *
*                                                                             *
* US   Government   Users   Restricted   Rights   -   Use,   duplication   or *
* disclosure  restricted by GSA ADP Schedule Contract with Digi International *
* Inc.                                                                        *
*                                                                             *
*******************************************************************************
*/
// PacketChildFrame.cpp : implementation file
//
/*
	This frame window is used for all Trace capture windows. It creates the CPacketListView,
	CPakcetTreeView, and CPacketByteView classes into a splitter.

	the frame remembers the sizes of this windows and it's panes and creates new
	windows with thoses sizes

Revision History:

   $History: PacketChildFrame.cpp $
 * 
 * *****************  Version 17  *****************
 * User: Mikez        Date: 7/11/97    Time: 5:02p
 * Branched in $/Components/Windows/NT/Sync/Build/Diag
 * 
 * *****************  Version 16  *****************
 * User: Rodgerb      Date: 2/25/97    Time: 1:23p
 * Updated in $/Components/Windows/NT/Sync/rev2diag
 * 
 * *****************  Version 15  *****************
 * User: Rodgerb      Date: 2/14/97    Time: 9:52a
 * Updated in $/Components/Windows/NT/Sync/rev2diag
 * 1) added refresh thread 
 * 2) remember window positions of summay and capture windows
 * 
 * *****************  Version 14  *****************
 * User: Rodgerb      Date: 1/27/97    Time: 1:18p
 * Updated in $/Components/Windows/NT/Sync/rev2diag
 * 1) documentation
 * 2) added some new params to Physical layer stats
*/

#include "stdafx.h"
#include "Diagnostic.h"
#include "PacketChildFrame.h"

#include "PacketListView.h"
#include "PacketByteView.h"
#include "PacketTreeView.h"

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

void CMySplitterWnd::StopTracking(BOOL bAccept)
{
	CSplitterWnd::StopTracking(bAccept);

	if (bAccept)
	{
		CPacketChildFrame *wnd = (CPacketChildFrame *)GetParent();

		wnd->SetUserSizes();
	}
}

/////////////////////////////////////////////////////////////////////////////
// CPacketChildFrame

IMPLEMENT_DYNCREATE(CPacketChildFrame, CMDIChildWnd)

CPacketChildFrame::CPacketChildFrame()
{
	m_bInSplitterCreate = TRUE;
}

CPacketChildFrame::~CPacketChildFrame()
{
}


BEGIN_MESSAGE_MAP(CPacketChildFrame, CMDIChildWnd)
	//{{AFX_MSG_MAP(CPacketChildFrame)
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_RBUTTONUP()
	ON_COMMAND(ID_CAPTURE_FILTER, OnCaptureFilter)
	ON_UPDATE_COMMAND_UI(IDW_DataSource, OnUpdateDataSourceBox)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPacketChildFrame message handlers

void CPacketChildFrame::OnUpdateDataSourceBox(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(FALSE);
}

BOOL CPacketChildFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	tagWINDOWPLACEMENT defWndPlacement = 
	{ 
		sizeof(WINDOWPLACEMENT),
		0,
		SW_HIDE,
		{ UINT_MAX, UINT_MAX },
		{ UINT_MAX, UINT_MAX },
		{ lpcs->x, lpcs->y, lpcs->cx, lpcs->cy }
	};

	CWindowPlacement wp;
	
	wp = defWndPlacement;
	wp.Restore(c_pszKeyTraceWndPlacement, this);

	if ((lpcs->style & WS_MAXIMIZE) == 0 && (lpcs->style & WS_MINIMIZE) == 0)
		SetWindowPos(NULL, lpcs->x, lpcs->y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);

	RecalcLayout();

	m_bInSplitterCreate = TRUE;

	if (!m_wndSplitter.CreateStatic(this, 3, 1))
		return FALSE;

	m_bInSplitterCreate = FALSE;

	int defpaneheight = (lpcs->cy - GetSystemMetrics(SM_CYCAPTION) - 20) / 3;

	int SummaryHeight = AfxGetApp()->GetProfileInt(
		c_pszKeySettings,
		c_pszValueListPaneHeight,
		defpaneheight);
	int DetailHeight = AfxGetApp()->GetProfileInt(
		c_pszKeySettings,
		c_pszValueTreePaneHeight,
		defpaneheight);
	int HexHeight = AfxGetApp()->GetProfileInt(
		c_pszKeySettings,
		c_pszValueBytePaneHeight,
		defpaneheight);

	m_wndSplitter.CreateView(
		2, 0, 
		RUNTIME_CLASS(CPacketByteView),
		CSize(lpcs->cx, SummaryHeight),
		pContext);
	m_wndSplitter.CreateView(
		1, 0,
		RUNTIME_CLASS(CPacketTreeView),
		CSize(lpcs->cx, DetailHeight),
		pContext);
	m_wndSplitter.CreateView(
		0, 0,
		RUNTIME_CLASS(CPacketListView),
		CSize(lpcs->cx, HexHeight),
		pContext);
		
	m_wndSplitter.SetRowInfo(0, SummaryHeight, 10);
	m_wndSplitter.SetRowInfo(1, DetailHeight, 10);
	m_wndSplitter.RecalcLayout();

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CChildFrame diagnostics

#ifdef _DEBUG
void CPacketChildFrame::AssertValid() const
{
	CMDIChildWnd::AssertValid();
}

void CPacketChildFrame::Dump(CDumpContext& dc) const
{
	CMDIChildWnd::Dump(dc);
}

#endif //_DEBUG

BOOL CPacketChildFrame::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	return CMDIChildWnd::PreCreateWindow(cs);
}

// When using the old common controls destroying this frame window and specifically
// the CPacketListView window can take a long long time so I display a wait cursor
// until it is destroyed, i.e. PostNcDestroy()
void CPacketChildFrame::OnDestroy() 
{
	ShowWindow(FALSE);

	CWnd *wnd = AfxGetMainWnd();
	if (wnd)
		wnd->UpdateWindow();

	BeginWaitCursor();
	CMDIChildWnd::OnDestroy();
	
	// TODO: Add your message handler code here
}

void CPacketChildFrame::PostNcDestroy() 
{
	// TODO: Add your specialized code here and/or call the base class
	CMDIChildWnd::PostNcDestroy();
	EndWaitCursor();
}

void CPacketChildFrame::OnSize(UINT nType, int cx, int cy) 
{
	CMDIChildWnd::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if (!m_bInSplitterCreate)
		SetUserSizes();
}

void CPacketChildFrame::SetUserSizes()
{	
	int dummy, height;
	
	m_wndSplitter.GetRowInfo(0, height, dummy);
	AfxGetApp()->WriteProfileInt(
		c_pszKeySettings,
		c_pszValueListPaneHeight,
		height);

	m_wndSplitter.GetRowInfo(1, height, dummy);
	AfxGetApp()->WriteProfileInt(
		c_pszKeySettings,
		c_pszValueTreePaneHeight,
		height);

	m_wndSplitter.GetRowInfo(2, height, dummy);
	AfxGetApp()->WriteProfileInt(
		c_pszKeySettings,
		c_pszValueBytePaneHeight,
		height);

	CWindowPlacement wp;

	wp.Save(c_pszKeyTraceWndPlacement, this);
	wp.showCmd = SW_HIDE;
	wp.WriteProfileWP(c_pszKeyTraceWndPlacement);
}

void CPacketChildFrame::OnRButtonUp(UINT, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMenu menu;
	CMenu *submenu;
	// Load the menu.
	menu.LoadMenu(IDR_DIAGTYPE);
	// Get the pop-up menu.
	submenu = menu.GetSubMenu(1);
	// Post the menu.
	submenu->TrackPopupMenu(
					TPM_LEFTALIGN|TPM_RIGHTBUTTON,
					point.x, point.y,
					AfxGetApp()->m_pMainWnd,NULL);
}

// based on which panes has focus the title of the frame will change to name the
// pane which has the focus
void CPacketChildFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
{
	GetMDIFrame()->OnUpdateFrameTitle(bAddToTitle);

	if ((GetStyle() & FWS_ADDTOTITLE) == 0)
		return;     // leave child window alone!

	CDocument* pDocument = GetActiveDocument();
	if (bAddToTitle && pDocument != NULL)
	{
		TCHAR szText[256+_MAX_PATH];
		lstrcpy(szText, pDocument->GetTitle());
		if (m_nWindow > 0)
			wsprintf(szText + lstrlen(szText), _T(":%d"), m_nWindow);

		CView *pView = GetActiveView();
		LPCTSTR ViewType = NULL;

		if (pView->IsKindOf(RUNTIME_CLASS(CPacketByteView)))
			ViewType = " (Hex)";
		if (pView->IsKindOf(RUNTIME_CLASS(CPacketListView)))
			ViewType = " (Summary)";
		if (pView->IsKindOf(RUNTIME_CLASS(CPacketTreeView)))
			ViewType = " (Detail)";
		strcat(szText, ViewType);
		
		// set title if changed, but don't remove completely
		SetWindowText(szText);
	}
}

void CPacketChildFrame::OnCaptureFilter() 
{
	// TODO: Add your command handler code here
	CPacketListView *pView = (CPacketListView *)m_wndSplitter.GetPane(0, 0);

	if (pView->OnCaptureFilter())
		m_wndSplitter.RecalcLayout();
}
