// TreeComboBox.cpp : implementation file
//

#include "pch.h"
#include "TreeComboBox.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTreeComboBox

CTreeComboBox::CTreeComboBox()
	:m_bTree(FALSE)
	, m_bAlertBkg(FALSE)
	, m_bAlertText(FALSE)
	, m_droppedHeight(250)
	, m_droppedWidth(100)
{
	m_hBrushAlert = CreateSolidBrush(COLOR_ALERT);
}

CTreeComboBox::~CTreeComboBox()
{
	DeleteObject(m_hBrushAlert);
}


BEGIN_MESSAGE_MAP(CTreeComboBox, CComboBox)
	//{{AFX_MSG_MAP(CTreeComboBox)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_CTLCOLOR_REFLECT()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WMU_CLOSE_CONTROL, OnCloseControl)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTreeComboBox message handlers
void CTreeComboBox::PreSubclassWindow()
{
	// TODO: Add your specialized code here and/or call the base class
	CComboBox::PreSubclassWindow();
	CRect rect(0, 0, 0, 0);

	DWORD dwStyle = WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT | WS_POPUPWINDOW;
	((CWnd&)m_Tree).CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), NULL);
	m_Tree.Init(this);

	GetClientRect(rect);
	SetDroppedWidth(rect.Width());
	SetDroppedHeight(m_droppedHeight);
}

BOOL CTreeComboBox::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN)
	{
		DisplayTree();
		return TRUE;
	}

	return CComboBox::PreTranslateMessage(pMsg);
}

void CTreeComboBox::OnLButtonDown(UINT nFlags, CPoint point)
{
	m_bTree = !m_bTree;
	if (m_bTree)
		DisplayTree();
}

void CTreeComboBox::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	OnLButtonDown(nFlags, point);
}

void CTreeComboBox::TreeCtrlDone()
{
	CWnd* pParent = GetParent();
	if (pParent != NULL)
	{
		CString str; GetWindowText(str);
		if (m_pretext != str)
		{
			m_pretext = str;
			WPARAM wParam = MAKEWPARAM(GetDlgCtrlID(), CBN_CLOSEUP);
			pParent->SendMessage(WM_COMMAND, wParam, (LPARAM)m_hWnd);
		}
	}
}

LRESULT CTreeComboBox::OnCloseControl(WPARAM wParam, LPARAM lParam)
{
	m_Tree.ShowWindow(SW_HIDE);
	m_bTree = FALSE;
	HTREEITEM hItemChanged = (HTREEITEM)lParam;
	if (hItemChanged)
	{
		SetTitle(m_Tree.GetItemText(hItemChanged));
	}
	AlertBkg(FALSE);
	TreeCtrlDone();
	return 1;
}

HBRUSH CTreeComboBox::CtlColor(CDC* pDC, UINT nCtlColor)
{
	HBRUSH hbr = NULL;
	if (nCtlColor == CTLCOLOR_EDIT)
	{
		if (!m_bAlertText)pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
		else pDC->SetTextColor(COLOR_RED);
		if (!m_bAlertBkg)pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
		else pDC->SetBkColor(COLOR_ALERT);
		if (m_bAlertText || m_bAlertBkg)hbr = m_hBrushAlert;
		pDC->SetBkMode(TRANSPARENT);
	}

	return hbr;
}

void CTreeComboBox::DisplayTree()
{
	CRect rc;
	GetWindowRect(rc);
	rc.top = rc.bottom + 1;
	rc.right = GetDroppedWidth();
	rc.bottom = GetDroppedHeight();

	m_Tree.Display(rc);
}

void CTreeComboBox::SetTitle(CString sTitle)
{
	ResetContent();
	AddString(sTitle);
	SetCurSel(0);
}

void CTreeComboBox::RefDroppedWidth()
{
	CRect rect;
	GetClientRect(rect);
	SetDroppedWidth(rect.Width());
}

/************************************************************************/
/*  ������InsertMyItem[12/7/2016 IT];
/*  ������;
/*  ������;
/*  	[IN] ��;
/*  	[IN] ��;
/*  ���أ�void;
/*  ע�⣺ֻ�ʺ϶���Ŀ¼;
/*  ʾ����;
/*
/*  �޸ģ�;
/*  ���ڣ�;
/*  ���ݣ�;
/************************************************************************/
void CTreeComboBox::InsertMyItem(IN CString strRoot, IN CString strItem)
{
	BOOL bExist = FALSE;
	HTREEITEM hRoot = m_Tree.GetRootItem();
	if (hRoot == NULL)
	{
		hRoot = m_Tree.InsertItem(strRoot, NULL);
		m_Tree.InsertItem(strItem, hRoot);
		return;
	}

	CString strName = m_Tree.GetItemText(hRoot);
	if (strName == strRoot)
	{
		HTREEITEM hSubItem = m_Tree.GetChildItem(hRoot);
		// ������������;
		while (hSubItem)
		{
			// ��ǰ�����Ƿ���Ҫ���ҵ�����;
			if (strItem == m_Tree.GetItemText(hSubItem))
			{
				bExist = TRUE;
				break;
			}

			// ��δ����������һ���ֵ���;
			hSubItem = m_Tree.GetNextSiblingItem(hSubItem);
		}

		if (!bExist)
		{
			// ��������;
			m_Tree.InsertItem(strItem, hRoot);
		}
	}
	else
	{
		hRoot = m_Tree.GetNextSiblingItem(hRoot);
		while (hRoot)
		{
			// ��ǰ�ֵ����Ƿ���Ҫ���ҵ�����;
			if (strRoot == m_Tree.GetItemText(hRoot))
			{
				HTREEITEM hSubItem = m_Tree.GetChildItem(hRoot);
				// ������������;
				while (hSubItem)
				{
					// ��ǰ�����Ƿ���Ҫ���ҵ�����;
					if (strItem == m_Tree.GetItemText(hSubItem))
					{
						bExist = TRUE;
						break;
					}

					// ��δ����������һ���ֵ���;
					hSubItem = m_Tree.GetNextSiblingItem(hSubItem);
				}

				if (!bExist)
				{
					// ��������;
					bExist = TRUE;
					m_Tree.InsertItem(strItem, hRoot);
				}

				break;
			}

			// ��δ����������һ���ֵ���;
			hRoot = m_Tree.GetNextSiblingItem(hRoot);
		}

		if (!bExist)
		{
			// ��������;
			hRoot = m_Tree.InsertItem(strRoot, NULL);
			m_Tree.InsertItem(strItem, hRoot);
		}
	}
}