/*
*******************************************************************************
* 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.                                                                        *
*                                                                             *
*******************************************************************************

// MainFrm.cpp : implementation of the CMainFrame class
//

Revision History:

   $History: MainFrm.cpp $
 * 
 * *****************  Version 11  *****************
 * User: Mikez        Date: 7/11/97    Time: 5:02p
 * Branched in $/Components/Windows/NT/Sync/Build/Diag
 * 
 * *****************  Version 10  *****************
 * User: Rodgerb      Date: 2/14/97    Time: 9:51a
 * Updated in $/Components/Windows/NT/Sync/rev2diag
 * 1) added refresh thread 
 * 2) remember window positions of summay and capture windows
 * 
 * *****************  Version 9  *****************
 * 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 "MainFrm.h"
#include "DataSource.h"
#include "Splash.h"
#if defined NT5
#include <htmlhelp.h>
#endif // #if defined NT5

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

extern CDiagApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code !
	ON_WM_CREATE()
	ON_MESSAGE(c_RefreshMessage, OnRefreshMessage)
	//}}AFX_MSG_MAP
	// Global help commands
	ON_COMMAND(ID_HELP_FINDER, CMDIFrameWnd::OnHelpFinder)
	ON_COMMAND(ID_HELP, CMDIFrameWnd::OnHelpFinder)
	ON_COMMAND(ID_CONTEXT_HELP, CMDIFrameWnd::OnContextHelp)
	ON_COMMAND(ID_DEFAULT_HELP, CMDIFrameWnd::OnHelpFinder)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

LRESULT CMainFrame::OnRefreshMessage(WPARAM, LPARAM)
{
	theApp.OnRefreshMessage();

	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	const int nDropHeight = 100;

	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
// create toolbar
	if (!m_wndToolBar.Create(this) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

// Design guide advises 12 pixel gap between combos and buttons
	m_wndToolBar.SetButtonInfo(2, ID_SEPARATOR, TBBS_SEPARATOR, 12);

// set the combo box in toolbar
	m_wndToolBar.SetButtonInfo(3, IDW_COMBO, TBBS_SEPARATOR, 80);

// Design guide advises 12 pixel gap between combos and buttons
	m_wndToolBar.SetButtonInfo(4, ID_SEPARATOR, TBBS_SEPARATOR, 12);

	CRect rect;

	m_wndToolBar.GetItemRect(3, &rect);
        //rect.top = 3;         !removed 050300 [jjl] to align combobox to buttons
        rect.top = 0;
	rect.bottom = rect.top + nDropHeight;

// create combo box
	if (!m_ComboBox.Create(
			CBS_DROPDOWNLIST|WS_VISIBLE|WS_TABSTOP,
			rect, &m_wndToolBar, IDW_COMBO))
	{
		TRACE0("Failed to create combo-box\n");
		return FALSE;
	}

	//  Fill the combo box
	DWORD Data[5] = { c_ConstantRefresh, 1, 2, 5, c_ManualRefresh };
	LPCTSTR Text[5] = { "Constant", "1 Second", "2 Seconds", "5 Seconds", "Manual" };

	for (short i = 0; i < 5; i++)
		m_ComboBox.SetItemData(m_ComboBox.AddString(Text[i]), Data[i]);

	// set the combo box in toolbar
        m_wndToolBar.SetButtonInfo(5, IDW_DataSource, TBBS_SEPARATOR, 240);     //!050300 [jjl] increased width to fix Vantive 3924

	m_wndToolBar.SetButtonInfo(6, ID_SEPARATOR, TBBS_SEPARATOR, 12);

	m_wndToolBar.GetItemRect(5, &rect);
        //rect.top = 3;         !removed 050300 [jjl] to align combobox to buttons
        rect.top = 0;
        rect.bottom = rect.top + nDropHeight;

// create Data Source combo box
	if (!m_DataSourceBox.Create(
			CBS_DROPDOWNLIST|WS_VISIBLE|WS_TABSTOP|WS_VSCROLL,
			rect, &m_wndToolBar, IDW_DataSource))
	{
		TRACE0("Failed to create combo-box\n");
		return FALSE;
	}
	
	// create and set font
	CDC *pDC = GetDC();
	CString DefaultFont;

	DefaultFont.LoadString(IDS_DEFAULT_FONT);
	m_Font.CreatePointFont(8, DefaultFont, pDC);
	ReleaseDC(pDC);

	m_ComboBox.SetFont(&m_Font);
	m_DataSourceBox.SetFont(&m_Font);

// create status bar
	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Remove this if you don't want tool tips or a resizeable toolbar
	m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
		CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	CSplashWnd::ShowSplashScreen(this);

// LoadConfig is called here because I wanted the splash screen to show first
// LoadConfig is relatively time consuming
// The combo box is filled with all the CDataSource class created
// a CDataSourceControl class is created to help manage the menu/toolbar states
// associated with each CDataSource object 
	if (theApp.LoadConfig())
	{
// we remember which data source was selected when the app quits
		CString LastDataSource = theApp.GetProfileString(
			c_pszKeySettings,
			c_pszValueLastDataSource);

		for (i = 0; i < theApp.GetNumAdapters(); i++)
			for (short j = 0; j < theApp.GetAdapter(i)->GetNumDataSource(); j++)
			{
// get next data source
				CDataSource *pDS = theApp.GetAdapter(i)->GetDataSource(j);
// add to combo box				
				int index = m_DataSourceBox.AddString(pDS->GetName());
// create control class
				CDataSourceControl *pControl = new CDataSourceControl(pDS);
// set control class as item data
				m_DataSourceBox.SetItemDataPtr(index, pControl);

				if (LastDataSource == pDS->GetName())
					m_DataSourceBox.SetCurSel(index);
			}
		
		if (m_DataSourceBox.GetCurSel() == CB_ERR)
			m_DataSourceBox.SetCurSel(0);
	}

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CMDIFrameWnd::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

// write out to the registry the last CDataSource selected in the IDW_DataSource
// combo box
BOOL CMainFrame::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	theApp.KillRefreshThread();

	int nSel = m_DataSourceBox.GetCurSel();

	if (nSel > CB_ERR)
	{
		CDataSourceControl *pControl = (CDataSourceControl *)m_DataSourceBox.GetItemDataPtr(nSel);

// we remember which data source was selected when the app quits
		if (pControl != NULL)
			AfxGetApp()->WriteProfileString(
				c_pszKeySettings,
				c_pszValueLastDataSource,
				pControl->GetDataSource()->GetName());

// delete data source control objects
		for (short i = 0; i < m_DataSourceBox.GetCount(); i++)
			delete (CDataSourceControl *)m_DataSourceBox.GetItemDataPtr(i);

		m_DataSourceBox.ResetContent();
	}

	return CMDIFrameWnd::DestroyWindow();
}

#define DIGIHELPFILE "Digiview.chm"
void CMainFrame::WinHelp(DWORD dwData, UINT nCmd) 
{
#ifdef NT5
	
	switch(nCmd)
	{
	case (HELP_CONTEXT):
	{
		HWND hwndHtmlHelp = HtmlHelp(NULL, DIGIHELPFILE, HH_HELP_CONTEXT, dwData);
		if (hwndHtmlHelp == NULL)
		{
			TRACE0("Unable to start HTML Help for HELP_INDEX: / HH_HELP_CONTEXT\n");
		}
		break;
	}
	default:
	{
		HWND hwndHtmlHelp = HtmlHelp(NULL, DIGIHELPFILE, HH_DISPLAY_TOPIC, NULL); 
		if (hwndHtmlHelp == NULL)
		{
			TRACE0("Unable to start HTML Help for default / HH_DISPLAY_TOPIC\n");
		}
	    break;
	}
	}
#else
	CMDIFrameWnd::WinHelp(dwData, nCmd);	
#endif
}

