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

#include "stdafx.h"
#include "INKS.h"

#include "MainFrm.h"
#include "CommonObject.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	ON_WM_CREATE()
	ON_WM_CLOSE()

	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)

	ON_BN_CLICKED(IDC_SIDEBUTTON1, &CMainFrame::OnBnClickedSidebutton1)
	ON_BN_CLICKED(IDC_SIDEBUTTON2, &CMainFrame::OnBnClickedSidebutton2)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SIDEROTSPIN1, &CMainFrame::OnDeltaposSiderotspin1)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SIDEROTSPIN2, &CMainFrame::OnDeltaposSiderotspin2)
	ON_EN_KILLFOCUS(IDC_SIDEROTEDIT1, &CMainFrame::OnEnKillfocusSiderotedit1)
	ON_EN_KILLFOCUS(IDC_SIDEROTEDIT2, &CMainFrame::OnEnKillfocusSiderotedit2)

	ON_COMMAND(ID_VIEW_TOOLBAR, &CMainFrame::OnViewToolbar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_TOOLBAR, &CMainFrame::OnUpdateViewToolbar)
	ON_COMMAND(ID_VIEW_ZOOMBAR, &CMainFrame::OnViewZoombar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_ZOOMBAR, &CMainFrame::OnUpdateViewZoombar)
	ON_COMMAND(ID_VIEW_PUSHBAR, &CMainFrame::OnViewPushbar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_PUSHBAR, &CMainFrame::OnUpdateViewPushbar)
	ON_COMMAND(ID_VIEW_STATUSBAR, &CMainFrame::OnViewStatusbar)
	ON_UPDATE_COMMAND_UI(ID_VIEW_STATUSBAR, &CMainFrame::OnUpdateViewStatusbar)

	ON_UPDATE_COMMAND_UI(ID_ADDSCHEME, &CMainFrame::OnUpdateAddScheme)

	ON_UPDATE_COMMAND_UI(ID_ZOOM_TO_FIT, &CMainFrame::DisableItem)
	//ON_UPDATE_COMMAND_UI(ID_ZOOM_HAND, &CMainFrame::DisableItem)

END_MESSAGE_MAP()

HCURSOR CMainFrame::g_hCursor[10] = {0};

static void	MakeToolbarImageList(UINT inBitmapID, CImageList& outImageList)
{
	CBitmap bm;
	VERIFY (bm.Attach (::LoadImage (::AfxFindResourceHandle(MAKEINTRESOURCE (inBitmapID), RT_BITMAP), MAKEINTRESOURCE (inBitmapID), IMAGE_BITMAP, 0, 0, (LR_DEFAULTSIZE | LR_CREATEDIBSECTION))));
	
	VERIFY(outImageList.Create(16, 15, ILC_COLOR24, 4, 0));
	VERIFY(outImageList.Add (&bm, RGB (0, 0, 0)) != -1);
}

// CMainFrame construction/destruction

CMainFrame::CMainFrame() : m_staticIns(NULL), m_staticOuts(NULL), m_nIns(0), m_nOuts(0), m_pFont(NULL), m_nMouseTrackerMode(CMouseTracker::ModeNone), m_nMouseTrackerCreate(CMouseTracker::NoCreate)
//, m_ColorsDialogBar((CINKSDoc*)this->GetActiveDocument())
{
	EnableActiveAccessibility();
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
	SAFE_DELETE(m_pFont);
	m_TreeBar.DestroyWindow();
	m_wndDrawToolBar.DestroyWindow();
	m_wndZoomBar.DestroyWindow();
	m_wndStatusBar.DestroyWindow();
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	int cx = GetSystemMetrics(SM_CXSCREEN);
	int cy = GetSystemMetrics(SM_CYSCREEN);
	
	////////////////////////////////////////////////////
	//Draw ToolBar
	////////////////////////////////////////////////////
	if(!m_wndDrawToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))
	{
		TRACE0("Failed to create draw toolbar\n");
		return -1;      // fail to create
	}
	if(!m_wndDrawToolBar.SetButtons(nButtonIDs, 4))
		TRACE(_T("Failed to set drawbar buttons!"));
	m_wndDrawToolBar.LoadBitmap(IDB_DRAWTOOLBAR);
	m_wndDrawToolBar.SetButtonInfo(0, ID_SELECT, TBBS_BUTTON, 0);
	m_wndDrawToolBar.SetButtonInfo(1, ID_DRAWGATE, TBBS_BUTTON, 1);
	m_wndDrawToolBar.SetButtonInfo(2, ID_DRAWBLOCK, TBBS_BUTTON, 2);
	m_wndDrawToolBar.SetButtonInfo(3, ID_DRAWWIRE, TBBS_BUTTON, 3);
/*	m_wndDrawToolBar.SetButtonInfo(4, ID_DRAWTAG, TBBS_BUTTON, 4);
	m_wndDrawToolBar.SetButtonInfo(5, ID_DRAWINV, TBBS_BUTTON, 5);*/

	m_wndDrawToolBar.EnableDocking(CBRS_ALIGN_ANY);

	////////////////////////////////////////////////////
	//Draw ToolBar
	////////////////////////////////////////////////////
	if(!m_wndPushToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))
	{
		TRACE0("Failed to create draw toolbar\n");
		return -1;      // fail to create
	}

	::MakeToolbarImageList(IDB_PUSHTOOLBAR, m_PushToolImageList);
	::MakeToolbarImageList(IDB_PUSHTOOLBARDIS, m_PushToolImageListDisabled);
	CToolBarCtrl *pPushToolBar = &m_wndPushToolBar.GetToolBarCtrl();
	pPushToolBar->SetImageList(&m_PushToolImageList);
	pPushToolBar->SetDisabledImageList(&m_PushToolImageListDisabled);

	if(!m_wndPushToolBar.SetButtons(nButtonIDs, 3))
		TRACE(_T("Failed to set drawbar buttons!"));
	m_wndPushToolBar.SetButtonInfo(0, ID_PUSHIN, TBBS_BUTTON, 0);
	m_wndPushToolBar.SetButtonInfo(1, ID_PUSHOUT, TBBS_BUTTON, 1);
	m_wndPushToolBar.SetButtonInfo(2, ID_ADDSCHEME, TBBS_DISABLED | TBBS_BUTTON, 2);

	m_wndPushToolBar.EnableDocking(CBRS_ALIGN_ANY);


	////////////////////////////////////////////////////
	// Zoom ToolBar
	////////////////////////////////////////////////////
	if(!m_wndZoomBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))
	{
		TRACE0("Failed to create zoom toolbar\n");
		return -1;      // fail to create
	}
	::MakeToolbarImageList(IDB_ZOOMTOOLBAR, m_ZoomToolImageList);
	::MakeToolbarImageList(IDB_ZOOMTOOLBARDIS, m_ZoomToolImageListDisabled);
	CToolBarCtrl *pZoomToolBar = &m_wndZoomBar.GetToolBarCtrl();
	pZoomToolBar->SetImageList(&m_ZoomToolImageList);
	pZoomToolBar->SetDisabledImageList(&m_ZoomToolImageListDisabled);

	if(!m_wndZoomBar.SetButtons(nZoomButtons, 4))
		TRACE(_T("Failed to set zoombar buttons!"));

	m_wndZoomBar.SetButtonInfo(0, ID_ZOOM_IN, TBBS_BUTTON, 0);
	m_wndZoomBar.SetButtonInfo(1, ID_ZOOM_OUT, TBBS_BUTTON, 1);
	m_wndZoomBar.SetButtonInfo(2, ID_ZOOM_TO_FIT, TBBS_BUTTON, 2);
	m_wndZoomBar.SetButtonInfo(3, ID_ZOOM_HAND, TBBS_BUTTON, 3);
	
	m_wndZoomBar.EnableDocking(CBRS_ALIGN_ANY);

	////////////////////////////////////////////////////
	// StatusBar
	////////////////////////////////////////////////////
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndDrawToolBar);
	DockControlBar(&m_wndPushToolBar);
	DockControlBar(&m_wndZoomBar);


	if(!m_wndStatusBar.Create(this)){
		TRACE0("Failed to create status bar\n");
		return -1;
	}

	if(m_wndStatusBar.SetIndicators(Mindicators, sizeof(Mindicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}
	m_wndStatusBar.SetPaneInfo(0, ID_XYMOUSEPOS, SBPS_NORMAL, 80);
	m_wndStatusBar.SetPaneInfo(1, ID_XYMOUSEPOS, SBPS_NORMAL, 80);
	m_wndStatusBar.SetPaneInfo(2, ID_XYMOUSEPOSSNAPPED, SBPS_NORMAL, 80);


	if(!m_SideBar.Create(this, IDD_SIDEBAR, CBRS_LEFT | CBRS_GRIPPER | CBRS_TOOLTIPS | WS_VISIBLE, 0)){
                        TRACE0("Failed to create side bar m_wndDlgBar\n");
                        return -1;              // fail to create
	}
	m_SideBar.Initialize();
	m_SideBar.SelNoSubdlg();
	m_SideBar.HideInfo();

	if(!m_TreeBar.Create(this, IDD_TREEBAR, WS_CHILD | WS_VISIBLE | CBRS_RIGHT | CBRS_GRIPPER | CBRS_TOOLTIPS, 0)){
                        TRACE0("Failed to create side bar m_wndDlgBar\n");
                        return -1;              // fail to create
	}
	
	if(!LoadCursors()){
		TRACE("Failed to load cursors\n");
		return -1;
	}
	return 0;
}

bool CMainFrame::LoadCursors()
{
	UINT ourCursorIDs[] ={
		IDC_TRACK4WAY,
		IDC_COPY4WAY,
		IDC_CREATEGATE,
		IDC_DRAWWIRE,
		IDC_DRAWBLOCK,
		IDC_DRAWTAG,
		IDC_CURSOR_ZOOMIN,
		IDC_CURSOR_ZOOMOUT
	};

	bool bRet = true;
	for (int i = 0; i < sizeof(ourCursorIDs)/sizeof(UINT); i++){
		HCURSOR h(0);
		h = AfxGetApp()->LoadCursorW(ourCursorIDs[i]);
		g_hCursor[i] = h;
		if(!h)
			return false;
	}
	return true;
}

BOOL CMainFrame::SetCursor(int iHandle)
{
	HCURSOR h = g_hCursor[iHandle];
	if (h) ::SetCursor(h);

	return h != 0;
}

BOOL CMainFrame::GetToolText(UINT nID, CString& strTipText )
{
	switch(nID) {
	case ID_SELECT:
		strTipText = _T("Select");
		return TRUE;
	case ID_DRAWGATE:
		strTipText = _T("Add new gate");
		return TRUE;
	case ID_DRAWBLOCK:
		strTipText = _T("Add new block");
		return TRUE;
	case ID_DRAWWIRE:
		strTipText = _T("Add wire");
		return TRUE;
	case ID_DRAWTAG:
		strTipText = _T("Add tag");
		return TRUE;
	case ID_DRAWINV:
		strTipText = _T("Add invertor");
		return TRUE;
	case ID_ZOOM_IN:
		strTipText = _T("Zoom in");
		return TRUE;
	case ID_ZOOM_OUT:
		strTipText = _T("Zoom out");
		return TRUE;
	case ID_ZOOM_TO_FIT:
		strTipText = _T("Zoom to fit");
		return TRUE;
	case ID_ZOOM_HAND:
		strTipText = _T("Zoom hand");
		return TRUE;
	case ID_PUSHIN:
		strTipText = _T("Push into the block");
		return TRUE;
	case ID_PUSHOUT:
		strTipText = _T("Push out of the block");
		return TRUE;
	case ID_ADDSCHEME:
		strTipText = _T("Add scheme to project");
		return TRUE;

	}
	return TRUE;
}

BOOL CMainFrame::OnToolTipText(UINT nID, NMHDR* pNMHDR, LRESULT*pResult)
{
	ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);

	TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
	TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;

	CString strTipText;
	if(GetToolText((UINT)pNMHDR->idFrom, strTipText)){
#ifndef _UNICODE
		if (pNMHDR->code == TTN_NEEDTEXTA)
			lstrcpyn(pTTTA->szText, strTipText, _countof(pTTTA->szText));
		else
			_mbstowcsz(pTTTW->szText, strTipText, _countof(pTTTW->szText));
#else
		if (pNMHDR->code == TTN_NEEDTEXTA)
			_wcstombsz(pTTTA->szText, strTipText, _countof(pTTTA->szText));
		else
			lstrcpyn(pTTTW->szText, strTipText, _countof(pTTTW->szText));
#endif
		return TRUE;
	}

	return CMainFrame::OnToolTipText(nID, pNMHDR, pResult);
}

void CMainFrame::UpdateMousePos(CPoint p1, CPoint p2, CPoint p3)
{
	CString coord1, coord2, coord3;
	coord1.Format(_T("X:%d Y:%d"), p1.x, p1.y);
	coord2.Format(_T("X:%d Y:%d"), p2.x, p2.y);
	coord3.Format(_T("X:%d Y:%d"), p3.x, p3.y);

	m_wndStatusBar.SetPaneText(0, coord1);
	m_wndStatusBar.SetPaneText(0, coord2);
	//m_ObjectInfoDialogBar.m_StatusBar.SetText(coord1, 0, 0);
	//m_ObjectInfoDialogBar.m_StatusBar.SetText(coord2, 1, 0);
	//m_ObjectInfoDialogBar.m_StatusBar.SetText(coord3, 2, 0);
}

void CMainFrame::UpdateTreeBar(CString strSelectedView)
{
	m_TreeBar.SetSelectedItem(strSelectedView);
}

void CMainFrame::UpdateCreateBar(CMouseTracker::Mode m, CMouseTracker::CreateMode cm)
{
	m_nMouseTrackerMode = m;
	m_nMouseTrackerCreate = cm;
	switch(m){
		case CMouseTracker::ModeNone:
		case CMouseTracker::ModeSelect:
			{
			m_SideBar.SelNoSubdlg();
			CDocument *pDoc = theApp.GetActiveDocument();
			if(pDoc->IsKindOf(RUNTIME_CLASS(CINKSDoc))){
				m_SideBar.ShowInfo((CINKSDoc*)pDoc);
			}
			break;
			}
		case CMouseTracker::ModeCreate:
			switch(cm){
				case CMouseTracker::CreateGate:
					m_SideBar.SelGateSubdlg();
					break;
				case CMouseTracker::CreateBlock:
					m_SideBar.SelBlockSubdlg();
					break;
				default:
					m_SideBar.SelNoSubdlg();
			}
			break;
		default:
			m_SideBar.SelNoSubdlg();
	}
	
}

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


void CMainFrame::OnBnClickedSidebutton1()
{
	m_SideBar.InsertObjectHit(0);
	m_SideBar.m_nSelectedSource = 1;
	
}

void CMainFrame::OnBnClickedSidebutton2()
{
	m_SideBar.InsertObjectHit(1);
	m_SideBar.m_nSelectedSource = 2;
	
}

void CMainFrame::OnDeltaposSiderotspin1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
	*pResult = -1;
	m_SideBar.UpdateSpin1(pNMUpDown->iDelta);
}

void CMainFrame::OnDeltaposSiderotspin2(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
	*pResult = -1;
	m_SideBar.UpdateSpin2(pNMUpDown->iDelta);
}

void CMainFrame::OnEnKillfocusSiderotedit1()
{
	m_SideBar.UpdateSpin1(0);
}

void CMainFrame::OnEnKillfocusSiderotedit2()
{
	m_SideBar.UpdateSpin2(0);
}

void CMainFrame::DisableItem(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(FALSE);
}

void CMainFrame::OnUpdateAddScheme(CCmdUI *pCmdUI)
{
	if(theApp.m_pOpenedProjectDoc)
		pCmdUI->Enable(TRUE);
	else
		pCmdUI->Enable(FALSE);
}

void CMainFrame::OnViewToolbar()
{
	ShowControlBar(&m_wndDrawToolBar, (m_wndDrawToolBar.GetStyle() & WS_VISIBLE) == 0, FALSE);
}

void CMainFrame::OnUpdateViewToolbar(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck((m_wndDrawToolBar.GetStyle() & WS_VISIBLE) != 0);
}

void CMainFrame::OnViewZoombar()
{
	ShowControlBar(&m_wndZoomBar, (m_wndZoomBar.GetStyle() & WS_VISIBLE) == 0, FALSE);
}

void CMainFrame::OnUpdateViewZoombar(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck((m_wndZoomBar.GetStyle() & WS_VISIBLE) != 0);
}

void CMainFrame::OnViewPushbar()
{
	ShowControlBar(&m_wndPushToolBar, (m_wndPushToolBar.GetStyle() & WS_VISIBLE) == 0, FALSE);
}

void CMainFrame::OnUpdateViewPushbar(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck((m_wndPushToolBar.GetStyle() & WS_VISIBLE) != 0);
}

void CMainFrame::OnViewStatusbar()
{
	ShowControlBar(&m_wndStatusBar, (m_wndStatusBar.GetStyle() & WS_VISIBLE) == 0, FALSE);
}

void CMainFrame::OnUpdateViewStatusbar(CCmdUI *pCmdUI)
{
	
	pCmdUI->SetCheck((m_wndStatusBar.GetStyle() & WS_VISIBLE) != 0);
}

//void CMainFrame::OnClose()
//{
//	theApp.m_bAppExit = true;
//	CMDIFrameWnd::OnClose();
//}