// GosEventIdPane.cpp : implementation file
//

#include "stdafx.h"
#include "gosview.h"
#include "GosEventIdPane.h"
#include "GosViewDoc.h"
#include "TimeFrame.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGosEventIdPane

IMPLEMENT_DYNCREATE(CGosEventIdPane, CView)

CGosEventIdPane::CGosEventIdPane()
{
}

CGosEventIdPane::~CGosEventIdPane()
{
}


BEGIN_MESSAGE_MAP(CGosEventIdPane, CView)
	//{{AFX_MSG_MAP(CGosEventIdPane)
	ON_WM_ERASEBKGND()
	ON_WM_VSCROLL()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGosEventIdPane drawing

void CGosEventIdPane::OnDraw(CDC* pDC)
{
	CGosViewDoc *pDoc = (CGosViewDoc *)GetDocument();
	CTimeFrame *pFrame = m_pParentFrame;
	CTimeFrame::TagNode *pNode;

	SCROLLINFO si;
	GetScrollBarCtrl(SB_VERT)->GetScrollInfo( &si, SIF_ALL );
	int topRow = si.nPos;
	int numRows = si.nPage;

	if( pDoc->m_pFont )
		pDC->SelectObject( pDoc->m_pFont );
	else
		pDC->SelectStockObject( ANSI_FIXED_FONT );

	TEXTMETRIC tm;
	pDC->GetTextMetrics(&tm);

	pDC->SelectStockObject( BLACK_PEN );
	pDC->SelectStockObject( NULL_BRUSH );
	pDC->SetBkMode( TRANSPARENT );

	for( DWORD id = pFrame->m_FirstID; id; id = pNode->idNextEnabled )
	{
		pNode = pFrame->AnyIDToTag(id);

		if( !pNode->independent )
			continue;

		if( pNode->onScreen )
		{
			char *pName = pFrame->AnyIDToName(pNode->id);
			if( m_pParentFrame->m_fFullNames )
			{
				pDC->TextOut( pNode->indent * 10 + 15, pNode->screenY - ( tm.tmHeight >> 1 ), pName );
			}
			else
			{
				char *pNameStart = pName;
				for( char *ptr = pName; *ptr; ptr++ )
				{
					if( *ptr == '\\' )
						pNameStart = ptr;
					if( ( ptr[0] == ':' ) && ( ptr[1] == ':' ) )
						pNameStart = ptr;
				}
				pDC->TextOut( pNode->indent * 10 + 15, pNode->screenY - ( tm.tmHeight >> 1 ), pNameStart );

			}
			if( pNode->idChild )
			{
				int x = pNode->indent * 10 + 8;
				int y = pNode->screenY;

				// draw a box
				pDC->Rectangle( x-5, y-5, x+6, y+6 );

				if( pNode->expand )
				{
					// draw a '-' symbol
					pDC->MoveTo( x-3, y );
					pDC->LineTo( x+4, y );
				}
				else
				{
					// draw a '+' symbol
					pDC->MoveTo( x-3, y );
					pDC->LineTo( x+4, y );
					pDC->MoveTo( x, y-3 );
					pDC->LineTo( x, y+4 );
				}
			}

		}
	}

	pDC->SelectStockObject( ANSI_FIXED_FONT );


}


/////////////////////////////////////////////////////////////////////////////
// CGosEventIdPane diagnostics

#ifdef _DEBUG
void CGosEventIdPane::AssertValid() const
{
	CView::AssertValid();
}

void CGosEventIdPane::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGosEventIdPane message handlers

BOOL CGosEventIdPane::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	
	return CGosEventPane::OnEraseBkgnd(pDC);
}

void CGosEventIdPane::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	
	CGosEventPane::OnVScroll(nSBCode, nPos, pScrollBar);
//	Invalidate();
}


void CGosEventIdPane::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	Invalidate(true);	
}

void CGosEventIdPane::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// just see if we clicked in/near the '+/-' expand box

	CGosViewDoc *pDoc = (CGosViewDoc *)GetDocument();
	CTimeFrame *pFrame = m_pParentFrame;
	CTimeFrame::TagNode *pNode;

	for( DWORD id = pFrame->m_FirstID; id; id = pNode->idNextEnabled )
	{
		pNode = pFrame->AnyIDToTag(id);
		if( !pNode->idChild || !pNode->onScreen )
			continue;

		if( ( point.y < pNode->screenY - 6 ) ||
			( point.y > pNode->screenY + 6 ) ||
			( point.x < pNode->indent * 10 + 8 - 6 ) ||
			( point.x > pNode->indent * 10 + 8 + 6 ) )
			continue;

		pNode->expand = !pNode->expand;
		break;
	}

	if( id )
	{
		// we expanded/contracted something
		pFrame->AnalyzeTagNodes();
		pDoc->UpdateAllViews(NULL,0,NULL);
	}
}
