// CheckTreeCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "CheckTreeCtrl.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCheckTreeCtrl

CCheckTreeCtrl::CCheckTreeCtrl()
{

}

CCheckTreeCtrl::~CCheckTreeCtrl()
{
}


BEGIN_MESSAGE_MAP(CCheckTreeCtrl, CTreeCtrl)
	//{{AFX_MSG_MAP(CCheckTreeCtrl)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCheckTreeCtrl message handlers

void CCheckTreeCtrl::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	UINT uFlags;
	HTREEITEM hitem=HitTest(point,&uFlags);
	if(((hitem!=0)&&(uFlags & TVHT_ONITEMSTATEICON))) return;

	CTreeCtrl::OnLButtonDblClk(nFlags, point);
}


void CCheckTreeCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
//	CTreeCtrl::OnLButtonDown(nFlags, point);

/*	UINT uFlags1;
	HTREEITEM hitem1=HitTest(point,&uFlags1);
	SetCheck1(hitem1,0);
	int j=GetCheck(hitem1);
	TRACE("\n%i",j);
	return;*/

	UINT uFlags;
	HTREEITEM hitem=HitTest(point,&uFlags);
	if(!((hitem!=0)&&(uFlags & TVHT_ONITEMSTATEICON)))
	{
		CTreeCtrl::OnLButtonDown(nFlags, point);
		return;
	}

	/*static i=-2;
	i++;
	if(i>3) i=0;
	int j=GetCheck(hitem);
	TRACE("\n%i / %i",i,j);
	SetCheck(hitem,i);
	*/
	BOOL check=GetCheck(hitem);
	if(check) check=FALSE; else check=TRUE;
	SetCheck(hitem,check);
	SelectItem(hitem);
	Select(hitem,TVGN_DROPHILITE);

	SetRecursiveChildCheck(hitem,check);

	SetRecursiveParentCheck(hitem,check);

//	if(ItemHasChildren(hitem))
//	{
//		// Установка детей
//		HTREEITEM hChildItem = GetChildItem(hitem);
//
//		while (hChildItem != NULL)
//		{
//			SetCheck(hChildItem,check);
//			hChildItem= GetNextItem(hChildItem, TVGN_NEXT);
//		}
//	}
//	else
//	{
//		// Установка родителя
//		HTREEITEM par=GetParentItem(hitem);
//		if(par!=0)
//		{
//			HTREEITEM hChildItem = GetChildItem(par);
//			BOOL check=GetCheck(hChildItem);
//
//			while (1)
//			{
//				hChildItem= GetNextItem(hChildItem, TVGN_NEXT);
//				if(hChildItem==0) break;
//				BOOL check1=GetCheck(hChildItem);
//				if(check1!=check)
//				{
//					check=2;
//					break;
//				}
//			}
//			SetCheck1(par,check);
//		}
//	}
}
BOOL CCheckTreeCtrl::SetRecursiveParentCheck(HTREEITEM hItem, BOOL check)
{
	// Установка родителя
	HTREEITEM par=GetParentItem(hItem);
	if(par!=0)
	{
		HTREEITEM hChildItem = GetChildItem(par);
		BOOL check=GetCheck(hChildItem);
		
		while (1)
		{
			hChildItem= GetNextItem(hChildItem, TVGN_NEXT);
			if(hChildItem==0) break;
			BOOL check1=GetCheck(hChildItem);
			if(check1!=check)
			{
				check=2;
				break;
			}
		}
		SetCheck1(par,check);
		SetRecursiveParentCheck(par, check);
	}

	return TRUE;
}

BOOL CCheckTreeCtrl::SetRecursiveChildCheck(HTREEITEM hItem, BOOL check)
{
	SetCheck(hItem,check);
	if(ItemHasChildren(hItem))
	{
		// Установка детей
		HTREEITEM hChildItem = GetChildItem(hItem);
		
		while (hChildItem != NULL)
		{
			SetRecursiveChildCheck(hChildItem,check);
			hChildItem= GetNextItem(hChildItem, TVGN_NEXT);
		}
	}

	return TRUE;
}

BOOL CCheckTreeCtrl::SetCheck1(HTREEITEM hItem, BOOL fCheck)
{
	ASSERT(::IsWindow(m_hWnd));
	TVITEM item;
	item.mask = TVIF_HANDLE | TVIF_STATE;
	item.hItem = hItem;
	item.stateMask = TVIS_STATEIMAGEMASK;

	/*
	Since state images are one-based, 1 in this macro turns the check off, and
	2 turns it on.
	*/
/*	item.state = INDEXTOSTATEIMAGEMASK(fCheck);
	item.state =4096;
	item.state =8192; // 1
	item.state =4096; // 0
	item.state =2048;
	item.state =16384;
	item.state =4096+8192;*/

	switch(fCheck)
	{
	case 0: item.state =4096; break;
	case 1: item.state =8192; break;
	case 2: item.state =4096+8192; break;
	default: item.state =0; break;
	}
	return (BOOL)::SendMessage(m_hWnd, TVM_SETITEM, 0, (LPARAM)&item);
}

