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

#include "stdafx.h"
#include "DiagramEng.h"

#include "MainFrm.h"

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

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

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_UPDATE_COMMAND_UI(ID_VIEW_TB_COMMON_OPS, OnUpdateViewTbCommonOps)
	ON_COMMAND(ID_VIEW_TB_COMMON_OPS, OnViewTbCommonOps)
	ON_UPDATE_COMMAND_UI(ID_VIEW_TB_COMMON_BLOCKS, OnUpdateViewTbCommonBlocks)
	ON_COMMAND(ID_VIEW_TB_COMMON_BLOCKS, OnViewTbCommonBlocks)
	ON_COMMAND(ID_VIEW_BLOCK_DIR, OnViewBlockDirectory)
	ON_UPDATE_COMMAND_UI(ID_VIEW_BLOCK_DIR, OnUpdateViewBlockDirectory)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

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

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	// DiagramEng (start)
	m_pctlEditBoxTime = NULL;	
	// DiagramEng (end)
}

CMainFrame::~CMainFrame()
{
	// DiagramEng (start)
	if(m_pctlEditBoxTime != NULL)
	{
		delete m_pctlEditBoxTime;

		m_pctlEditBoxTime = NULL;
	}
	// DiagramEng (end)
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	// DiagramEng (start) 

	// -- CREATE THE CommonOps TOOLBAR ////////////////////////////////////////////////////////////
	// NOTE: 'this' is the ptr to the parent frame wnd to which the toolbar will be attached.
	// CreateEx creates the toolbar
	// LoadToolBar loads the toolbar specified by the ID
	if(!m_wndTBCommonOps.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS |
		CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndTBCommonOps.LoadToolBar(IDR_TB_COMMON_OPS))
	{
		// Failed to create msg.
		TRACE0("Failed to create toolbar\n");
		return -1;	
	}

	// -- PLACING AN EDIT BOX ON THE CommonOps TOOLBAR 
	// Properties of Edit box Control to be placed on CommonOps Toolbar
	int nEditBoxWidth = 200;	// with of the Edit box control
	int nIndex = 10;			// index of button or separator whose info is to be set: btns (0-8), separator (9), separator placeholder (10)

	// Set Properties of Placeholder Separator for Edit box Control
	m_wndTBCommonOps.SetButtonInfo(nIndex, IDC_EB_COTB_TIME, TBBS_SEPARATOR, nEditBoxWidth);

	// Get the rectangle coords of the placeholder where the Edit box will be created.
	CRect rect;
	m_wndTBCommonOps.GetItemRect(nIndex, &rect);		// nIndex is index of button or separator whose rectangle cords are rqd.

	// Construct the CEdit object.
	m_pctlEditBoxTime = new CEdit;

	// Create the Edit box
	m_pctlEditBoxTime->Create(ES_LEFT|ES_READONLY|WS_BORDER|WS_CHILD|WS_VISIBLE, rect, &m_wndTBCommonOps, IDC_EB_COTB_TIME);
	// -- END OF PLACING AN EDIT BOX ON THE CommonOps TOOLBAR

	// -- END CREATION OF CommonOps TOOLBAR

	// -- CREATE THE CommonBlocks TOOLBAR /////////////////////////////////////////////////////////
	// NOTE: 'this' is the ptr to the parent frame wnd to which the toolbar will be attached.
	// CreateEx creates the toolbar
	// LoadToolBar loads the toolbar specified by the ID
	if(!m_wndTBCommonBlocks.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS |
		CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndTBCommonBlocks.LoadToolBar(IDR_TB_COMMON_BLOCKS))
	{
		// Failed to create msg.
		TRACE0("Failed to create toolbar\n");
		return -1;	
	}

	// -- END CREATION OF CommonBlocks TOOLBAR 

	// DiagramEng (end) 

	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
	}

	// -- STEP 8: CONVERSION of CDialog to CDialogBar
	if(!m_TreeDlgBar.Create(this, IDD_TREE_DLG, CBRS_LEFT, IDD_TREE_DLG))
	{
		TRACE0("Failed to create dialog bar\n");
		return -1;	// failed to create
	}

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

	// DiagramEng (start)
	// -- ENABLE DOCKING OF TOOLBARS
	// Enable docking for the Common Ops toolbar (IDR_TB_COMMON_OPS) 
	// NOTE: enables the toolbar for docking with the frame wnd.
	m_wndTBCommonOps.EnableDocking(CBRS_ALIGN_ANY);

	// Enable docking for the Common Blocks toolbar (IDR_TB_COMMON_BLOCKS) 
	// NOTE: enables the toolbar for docking with the frame wnd.
	m_wndTBCommonBlocks.EnableDocking(CBRS_ALIGN_ANY);

	// DiagramEng (end)

	EnableDocking(CBRS_ALIGN_ANY);	// called for the frame wnd.
	DockControlBar(&m_wndToolBar);	// frame wnd fn passed & of toolbar var. physically docks the toolbar to the frame wnd.

	// DiagramEng (start)
	// -- DOCK TOOLBARS 
	// Dock the Common Ops toolbar.
	DockControlBar(&m_wndTBCommonOps);

	// Dock the Common Blocks toolbar.
	DockControlBar(&m_wndTBCommonBlocks);
	
	// DiagramEng (end)

	// -- STEP 9: CONVERSION of CDialog to CDialogBar
	//m_TreeDlgBar.SetBarStyle(m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
	//m_TreeDlgBar.EnableDocking(CBRS_ALIGN_ANY);
	//DockControlBar(&m_TreeDlgBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

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

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

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

#endif //_DEBUG

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


void CMainFrame::OnUpdateViewTbCommonOps(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here

	// DiagramEng (start)

	// Check the status of the Common Ops. Toolbar (IDR_TB_COMMON_OPS)
	// NOTE: get style of toolbar and mask out for the WS_VISIBLE style flag.
	// If the flag is in the current toolbar style, then the toolbar is visible.
	// The SetCheck() allows checking and unchecking the toolbar MENU entry (in the View menu here).
	pCmdUI->SetCheck(((m_wndTBCommonOps.GetStyle() & WS_VISIBLE) != 0));

	// DiagramEng (end)
}

void CMainFrame::OnViewTbCommonOps() 
{
	// TODO: Add your command handler code here
	
	// DiagramEng (start)
	BOOL bShow;

	// Check state of Common Ops. toolbar
	bShow = ((m_wndTBCommonOps.GetStyle() & WS_VISIBLE) != 0);

	// Switch state
	// NOTE: & of toolbar, bool show/hide toolbar, delay showing toolbar (FALSE => no delay)
	ShowControlBar(&m_wndTBCommonOps, !bShow, FALSE);

	// Recalculate Layout
	RecalcLayout();

	// DiagramEng (end)
}

void CMainFrame::OnUpdateViewTbCommonBlocks(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
	// DiagramEng (start)

	// Check the status of the Common Blocks Toolbar (IDR_TB_COMMON_BLOCKS)
	// NOTE: get style of toolbar and mask out for the WS_VISIBLE style flag.
	// If the flag is in the current toolbar style, then the toolbar is visible.
	// The SetCheck() allows checking and unchecking the toolbar MENU entry (in the View menu here).
	pCmdUI->SetCheck(((m_wndTBCommonBlocks.GetStyle() & WS_VISIBLE) != 0));

	// DiagramEng (end)
}

void CMainFrame::OnViewTbCommonBlocks() 
{
	// TODO: Add your command handler code here
	
	// DiagramEng (start)
	BOOL bShow;

	// Check state of Common Ops. toolbar
	bShow = ((m_wndTBCommonBlocks.GetStyle() & WS_VISIBLE) != 0);

	// Switch state
	// NOTE: & of toolbar, bool show/hide toolbar, delay showing toolbar (FALSE => no delay)
	ShowControlBar(&m_wndTBCommonBlocks, !bShow, FALSE);

	// Recalculate Layout
	RecalcLayout();

	// DiagramEng (end)
}

void CMainFrame::OnViewBlockDirectory() 
{
	// TODO: Add your command handler code here
	
	// DiagramEng (start)
	BOOL bShow;

	// Check state of block library toolbar
	bShow = ((m_TreeDlgBar.GetStyle() & WS_VISIBLE) != 0);

	// Switch state
	// NOTE: & of toolbar, bool show/hide toolbar, delay showing toolbar (FALSE => no delay)
	ShowControlBar(&m_TreeDlgBar, !bShow, FALSE);

	// Recalculate Layout
	RecalcLayout();

	// DiagramEng (end)
}

void CMainFrame::OnUpdateViewBlockDirectory(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
		
	// DiagramEng (start)

	// Check the status of the CDialogBar (m_TreeDlgBar)
	// NOTE: get style of toolbar and mask out for the WS_VISIBLE style flag.
	// If the flag is in the current toolbar style, then the toolbar is visible.
	// The SetCheck() allows checking and unchecking the toolbar MENU entry (in the View menu here).
	pCmdUI->SetCheck(((m_TreeDlgBar.GetStyle() & WS_VISIBLE) != 0));

	// DiagramEng (end)
}

void CMainFrame::UpdateEditBoxTimeOnCommonOpsToolbar(CString string)
{
	CString str_empty = "";

	// Update the Edit box on the CommonOps Toolbar with the string
	// Note: SetWindowText() replaces all the text in the Edit control, here with an empty string: the msg. used is WM_SETTEXT.
	// ReplaceSel() replaces the selected text with the incoming string: the msg. used is EM_REPLACESEL.	
	// If there is no selection, the replacement text is inserted at the current cursor location.
	m_pctlEditBoxTime->SetWindowText(str_empty);		// CWnd::SetWindowText() replaces the text with an empty str.
	m_pctlEditBoxTime->ReplaceSel(string, FALSE);	// No selection, hence string placed at curr. cursor loc.
}

