// DQHTabWnd.cpp : implementation file
//

#include "stdafx.h"
#include "DQHTabWnd.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDQHTabWnd

CDQHTabWnd::CDQHTabWnd()
{
	m_SelPage = -1;
}

CDQHTabWnd::~CDQHTabWnd()
{
}


BEGIN_MESSAGE_MAP(CDQHTabWnd, CTabCtrl)
	//{{AFX_MSG_MAP(CDQHTabWnd)
	ON_WM_SIZE()
	ON_NOTIFY_REFLECT(TCN_SELCHANGE, OnSelchange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDQHTabWnd message handlers

void CDQHTabWnd::OnSize(UINT nType, int cx, int cy) 
{
	CTabCtrl::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	int idx;
	CString name;
	CWnd *pWnd;
	int iCur = GetSelIdx();
	RECT rect;
	rect = GetAvailableRect();
	idx = 0;
	while( GetItem( idx , name , pWnd ) )
	{
		if( !pWnd ){
			idx ++;
			continue;
		}
		pWnd->SetWindowPos( NULL , rect.left, rect.top, rect.right - rect.left , rect.bottom - rect.top , 0 );
		if( idx == iCur )
		{
			pWnd->ShowWindow( SW_SHOW );
		}
		else
		{
			pWnd->ShowWindow( SW_HIDE );
		}
		idx ++;
	}
}

BOOL CDQHTabWnd::Create(DWORD dwStyle, const RECT &rect, CWnd *pParentWnd, UINT nID)
{
	m_TabStyle = dwStyle;
	return CTabCtrl::Create(dwStyle,rect,pParentWnd,nID);
}

void CDQHTabWnd::AddPage(CString txt, CWnd *pWnd)
{
	_DQHTabItem item;
	RECT rect;
	item.m_Title = txt;
	item.m_pWnd = pWnd;
	m_WndList.AddTail(item);
	InsertItem( GetItemCount() , txt );
	
	rect = GetAvailableRect();
	if( item.m_pWnd )
	{
		item.m_pWnd->SetWindowPos( NULL , rect.left , rect.top , rect.right - rect.left , rect.bottom - rect.top , 0);
		if( m_WndList.GetCount() == 1)
		{
			item.m_pWnd->ShowWindow( SW_SHOW );
			m_SelPage = 0;
		}
		else
		{
			item.m_pWnd->ShowWindow( SW_HIDE );
		}
	}
}

BOOL CDQHTabWnd::GetItem(int idx, CString &name, CWnd *&pWnd)
{
	POSITION pos;
	_DQHTabItem item;
	int iCount;
	iCount = m_WndList.GetCount();
	if( idx < 0 || idx >= iCount )return FALSE;
	pos = m_WndList.FindIndex( idx );
	item = m_WndList.GetAt( pos );
	name = item.m_Title;
	pWnd = item.m_pWnd;
	return TRUE;
}

void CDQHTabWnd::OnSelchange(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int idx = GetCurSel();
	if( idx >= 0)
	{
		m_SelPage = idx;
		for( int i = 0 ; i < m_WndList.GetCount() ; i++)
		{
			CString name;
			CWnd *pWnd;
			if( GetItem( i , name , pWnd ) )
			{
				if( pWnd )
				{
					if( i == idx)
					{
						pWnd->ShowWindow( SW_SHOW );
					}
					else
					{
						pWnd->ShowWindow( SW_HIDE );
					}
				}
			}
		}
	}
	*pResult = 0;
}

RECT CDQHTabWnd::GetAvailableRect()
{
	RECT r;
	int iCount = GetItemCount();
	GetClientRect( &r );
	if( iCount == 0 )
	{
		return r;
	}
	else
	{
		RECT rect;
		rect =GetTabRect();
		r.left += 5;
		r.right -= 5;
		if( m_TabStyle & TCS_BOTTOM && !(m_TabStyle & TCS_VERTICAL))
		{
			r.top += 5;
			r.bottom = rect.top - 5;
		}
		else if( m_TabStyle & TCS_VERTICAL && !(m_TabStyle & TCS_BOTTOM))
		{
			r.left = rect.right + 5;
			r.top += 5;
			r.bottom -= 5;
		}
		else if( m_TabStyle & ( TCS_VERTICAL | TCS_BOTTOM))
		{
			r.right = rect.left - 5;
			r.top += 5;
			r.bottom -= 5;
		}
		else
		{
			r.top = rect.bottom + 5;
			r.bottom -= 5;
		}
	}
	return r;
}

int CDQHTabWnd::GetSelIdx()
{
	return m_SelPage;
}

RECT CDQHTabWnd::GetTabRect()
{
	RECT r;
	ZeroMemory( &r , sizeof(r) );
	for( int i = 0 ; i < GetItemCount() ; i++)
	{
		RECT rect;
		GetItemRect( i , &rect );
		if( i == 0 )
		{
			r = rect;
		}
		else 
		{
			if( r.left > rect.left ) r.left = rect.left;
			if( r.right < rect.right ) r.right = rect.right;
			if( r.top > rect.top ) r.top = rect.top ;
			if( r.bottom < rect.bottom ) r.bottom = rect.bottom ;
		}
	}
	return r;
}
