// ComboBoxListCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "ComboListCtrl.h"
#include <windowsx.h>
#include "ylgl.h"
#include "ChooseReason.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define IDC_ComboBox  0X01
#define PROPERTY_ITEM 0x02
#define PROPERTY_SUB  0x03
/////////////////////////////////////////////////////////////////////////////
// CComboBoxListCtrl
extern  CFont g_listctrlfont;
CComboBoxListCtrl::CComboBoxListCtrl()
{
	m_ComboBox.m_hWnd = NULL;
	m_bNeedSel=0;
}

CComboBoxListCtrl::~CComboBoxListCtrl()
{

}


BEGIN_MESSAGE_MAP(CComboBoxListCtrl, CListCtrl)
	//{{AFX_MSG_MAP(CComboBoxListCtrl)
	ON_WM_LBUTTONDBLCLK()
	ON_WM_PARENTNOTIFY()
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomdrawList)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_USER_ComboBox_END,OnComboBoxEnd)
END_MESSAGE_MAP()

void CComboBoxListCtrl::OnCustomdrawList ( NMHDR* pNMHDR, LRESULT* pResult )
{
	NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
	// Take the default processing unless we set this to something else below.
	*pResult = 0;

	// First thing - check the draw stage. If it's the control's prepaint
	// stage, then tell Windows we want messages for every item.
	if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
	}
	else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
	{
		// This is the prepaint stage for an item. Here's where we set the
		// item's text color. Our return value will tell Windows to draw the
		// item itself, but it will use the new color we set here.
		// We'll cycle the colors through red, green, and light blue.
		COLORREF crText;
		int pos=pLVCD->nmcd.dwItemSpec;
		if(pos%2)
		{
			pLVCD->clrTextBk = g_gridcol1;
		}
		else
		{
			pLVCD->clrTextBk = g_gridcol2;
		}
		// Tell Windows to paint the control itself.
		*pResult = CDRF_DODEFAULT;
	}
}

void CComboBoxListCtrl::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	CRect rcCtrl;
	LVHITTESTINFO lvhti;
	lvhti.pt = point;
	int nItem = CListCtrl::SubItemHitTest(&lvhti);
	if(nItem == -1)
		return;
	int nSubItem = lvhti.iSubItem;
	CListCtrl::GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rcCtrl); 

	rcCtrl.top -=4;
	rcCtrl.bottom +=200;

	if(nSubItem==4||nSubItem==6||nSubItem==8||nSubItem==10||nSubItem==12||nSubItem==14)
		ShowComboBox(TRUE,nItem,nSubItem,rcCtrl);	// Jeff.���ﴦ�������б�;
	CListCtrl::OnLButtonDblClk(nFlags, point);
}

void CComboBoxListCtrl::ShowComboBox(BOOL bShow,int nItem,int nIndex,CRect rcCtrl)
{
	if(m_ComboBox.m_hWnd == NULL)
	{
		//m_ComboBox.Create(WS_VSCROLL|WS_CHILD|CBS_DROPDOWNLIST,CRect(0,0,0,0),this,IDC_ComboBox);
		m_ComboBox.Create(CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP,CRect(0,0,0,0),this,IDC_ComboBox);
		m_ComboBox.ShowWindow(SW_HIDE);
	}
	if(bShow == TRUE)
	{
		if(nIndex==6 && GetItemText(nItem,4).IsEmpty ())
			return;
		CString strItem = CListCtrl::GetItemText(nItem,nIndex);
		m_ComboBox.MoveWindow(rcCtrl);
		m_ComboBox.ShowWindow(SW_SHOW);
		::SetFocus(m_ComboBox.GetSafeHwnd());
		m_ComboBox.ResetContent ();
		if(nIndex==4)
		{
			m_ComboBox.AddString ("");
			for(int i=0; i<g_List1array.GetSize (); i++)
				m_ComboBox.AddString ("�ѷ�"+g_List1array.ElementAt (i).ElementAt (1));
		}
		else if(nIndex==6)
		{
			m_ComboBox.AddString ("����");
			m_ComboBox.AddString ("");	
			m_ComboBox.AddString (strItem);
			m_bNeedSel=1;
		}
		else if(nIndex==8)
		{
			m_ComboBox.AddString ("OK");
			m_ComboBox.AddString ("��");
		}
#ifdef LKAY_VERSION
		else if(nIndex==10)
		{
			m_ComboBox.AddString ("OK");
			m_ComboBox.AddString ("�");
		}
		else if(nIndex==12)
		{
			m_ComboBox.AddString ("OK");
			m_ComboBox.AddString ("δת");
		}
		else if(nIndex==14)
		{
			m_ComboBox.AddString ("OK");
			m_ComboBox.AddString ("δȡ");
		}
#else
		else if(nIndex==10)
		{
			m_ComboBox.AddString ("OK");
			m_ComboBox.AddString ("δȡ");
		}
#endif
		m_ComboBox.SetCurSel (m_ComboBox.FindString (0, strItem));
		m_ComboBox.SetCtrlData(MAKEWPARAM(nIndex,nItem));	
	}
	else
		m_ComboBox.ShowWindow(SW_HIDE);
}

#ifdef VC60
void CComboBoxListCtrl::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
{
	if(wParam == TRUE)
	{
		CString strText(_T(""));
		int pos=m_ComboBox.GetCurSel ();

		if(pos!=-1)
		{
			m_ComboBox.GetLBText (pos, strText);
			DWORD dwData = m_ComboBox.GetCtrlData();
			int nItem= dwData>>16;
			int nIndex = dwData&0x0000ffff;
			CListCtrl::SetItemText(nItem,nIndex,strText);

			//printf("Jeff:%d,%d,%s\n",nItem,nIndex,strText);
			strText.TrimLeft(" ");
			strText.TrimRight(" ");

			if(nIndex==4 && strText.IsEmpty())
			{
				SetItemText(nItem,6,"");
				SetItemText(nItem,5,"");	// Jeff.add ��ѡ��ؿ�,���������;
			}


			if(nIndex==4)	// ����;
			{
				//printf("Jeff:nIndex=4,%s\n",m_sparray->ElementAt(nItem).ElementAt(4));
				//if(strText!=m_sparray->ElementAt(nItem).ElementAt(4))	// Jeff.delete 4:dindansp.status1;
				if( strText != "")
					SetItemText(nItem,5,g_date+"/"+g_user.name);
				else
					SetItemText(nItem,5,"");	// Jeff.add ��ѡ��ؿ�,���������;
			}
			else if(nIndex==6)
			{
				//		MessageBox(strText);
				if(strText.Find("����")!=-1 && m_bNeedSel)
				{
					m_bNeedSel=0;
					//SetItemText(nItem,7,g_date+"/"+g_user.name);
					CChooseReason dlg;
					if(dlg.DoModal ()==IDOK)
					{
						SetItemText( nItem, 6,"����/("+dlg.sRet+")");
						m_ComboBox.SetCurSel (-1);

					}
					SetItemText(nItem,7,g_date+"/"+g_user.name);
				}
				else if( strText == "")	// Jeff.add.��ѡ��ؿ�,���������;
				{
					SetItemText(nItem,7,"");
				}
			}
			else if(nIndex==8)
			{
				//printf("Jeff:nIndex=8,%s\n",m_sparray->ElementAt(nItem).ElementAt(6));
				//if(strText!=m_sparray->ElementAt (nItem).ElementAt (6))		// Jeff.delete 6:dindansp.autoid;
				if( strText == "OK")
					SetItemText(nItem,9,g_date+"/"+g_user.name);
				else// Jeff.add.��ѡ��ؿ�,���������;
					SetItemText(nItem,9,"");
			}
#ifdef LKAY_VERSION
			else if(nIndex==10)
			{
				if(strText!=m_sparray->ElementAt (nItem).ElementAt (23))
					SetItemText(nItem,11,g_date+"/"+g_user.name);
				else // Jeff.add.��ѡ��ؿ�,���������;
					SetItemText(nItem,11,"");
			}
			else if(nIndex==12)
			{
				if(strText!=m_sparray->ElementAt (nItem).ElementAt (24))
					SetItemText(nItem,13,g_date+"/"+g_user.name);
				else // Jeff.add.��ѡ��ؿ�,���������;
					SetItemText(nItem,13,"");
			}
			else if(nIndex==14)
			{
				if(strText!=m_sparray->ElementAt (nItem).ElementAt (7))
					SetItemText(nItem,15,g_date+"/"+g_user.name);
				else // Jeff.add.��ѡ��ؿ�,���������;
					SetItemText(nItem,15,"");
			}
#else
			else if(nIndex==10)
			{
				//printf("Jeff:nIndex=10,%s\n",m_sparray->ElementAt(nItem).ElementAt(7));
				//if(strText!=m_sparray->ElementAt(nItem).ElementAt(7))	// Jeff.delete 7:dindansp.spid;
				if( strText == "OK")
					SetItemText(nItem,11,g_date+"/"+g_user.name);
				else // Jeff.add.��ѡ��ؿ�,���������;
					SetItemText(nItem,11,"");
			}
#endif
		}
	}
	else
	{

	}

	if(lParam == FALSE)
		m_ComboBox.ShowWindow(SW_HIDE);
}
#else
LRESULT CComboBoxListCtrl::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
{
	if(wParam == TRUE)
	{
		CString strText(_T(""));
		int pos=m_ComboBox.GetCurSel ();

		if(pos!=-1)
		{
			m_ComboBox.GetLBText (pos, strText);
			DWORD dwData = m_ComboBox.GetCtrlData();
			int nItem= dwData>>16;
			int nIndex = dwData&0x0000ffff;
			CListCtrl::SetItemText(nItem,nIndex,strText);

			//printf("Jeff:%d,%d,%s\n",nItem,nIndex,strText);
			strText.TrimLeft(" ");
			strText.TrimRight(" ");

			if(nIndex==4 && strText.IsEmpty())
			{
				SetItemText(nItem,6,"");
				SetItemText(nItem,5,"");	// Jeff.add ��ѡ��ؿ�,���������;
			}


			if(nIndex==4)	// ����;
			{
				//printf("Jeff:nIndex=4,%s\n",m_sparray->ElementAt(nItem).ElementAt(4));
				//if(strText!=m_sparray->ElementAt(nItem).ElementAt(4))	// Jeff.delete 4:dindansp.status1;
				if( strText != "")
					SetItemText(nItem,5,g_date+"/"+g_user.name);
				else
					SetItemText(nItem,5,"");	// Jeff.add ��ѡ��ؿ�,���������;
			}
			else if(nIndex==6)
			{
				//		MessageBox(strText);
				if(strText.Find("����")!=-1 && m_bNeedSel)
				{
					m_bNeedSel=0;
					//SetItemText(nItem,7,g_date+"/"+g_user.name);
					CChooseReason dlg;
					if(dlg.DoModal ()==IDOK)
					{
						SetItemText( nItem, 6,"����/("+dlg.sRet+")");
						m_ComboBox.SetCurSel (-1);

					}
					SetItemText(nItem,7,g_date+"/"+g_user.name);
				}
				else if( strText == "")	// Jeff.add.��ѡ��ؿ�,���������;
				{
					SetItemText(nItem,7,"");
				}
			}
			else if(nIndex==8)
			{
				//printf("Jeff:nIndex=8,%s\n",m_sparray->ElementAt(nItem).ElementAt(6));
				//if(strText!=m_sparray->ElementAt (nItem).ElementAt (6))		// Jeff.delete 6:dindansp.autoid;
				if( strText == "OK")
					SetItemText(nItem,9,g_date+"/"+g_user.name);
				else// Jeff.add.��ѡ��ؿ�,���������;
					SetItemText(nItem,9,"");
			}
#ifdef LKAY_VERSION
			else if(nIndex==10)
			{
				if(strText!=m_sparray->ElementAt (nItem).ElementAt (23))
					SetItemText(nItem,11,g_date+"/"+g_user.name);
				else // Jeff.add.��ѡ��ؿ�,���������;
					SetItemText(nItem,11,"");
			}
			else if(nIndex==12)
			{
				if(strText!=m_sparray->ElementAt (nItem).ElementAt (24))
					SetItemText(nItem,13,g_date+"/"+g_user.name);
				else // Jeff.add.��ѡ��ؿ�,���������;
					SetItemText(nItem,13,"");
			}
			else if(nIndex==14)
			{
				if(strText!=m_sparray->ElementAt (nItem).ElementAt (7))
					SetItemText(nItem,15,g_date+"/"+g_user.name);
				else // Jeff.add.��ѡ��ؿ�,���������;
					SetItemText(nItem,15,"");
			}
#else
			else if(nIndex==10)
			{
				//printf("Jeff:nIndex=10,%s\n",m_sparray->ElementAt(nItem).ElementAt(7));
				//if(strText!=m_sparray->ElementAt(nItem).ElementAt(7))	// Jeff.delete 7:dindansp.spid;
				if( strText == "OK")
					SetItemText(nItem,11,g_date+"/"+g_user.name);
				else // Jeff.add.��ѡ��ؿ�,���������;
					SetItemText(nItem,11,"");
			}
#endif
		}
	}
	else
	{

	}

	if(lParam == FALSE)
		m_ComboBox.ShowWindow(SW_HIDE);

	return 0;
}
#endif

void CComboBoxListCtrl::OnParentNotify(UINT message, LPARAM lParam) 
{
	CListCtrl::OnParentNotify(message, lParam);
	//////////////////////////////////////////////////////////////////////////
	CHeaderCtrl* pHeaderCtrl = CListCtrl::GetHeaderCtrl();
	if(pHeaderCtrl == NULL)
		return;
	CRect rcHeader;
	pHeaderCtrl->GetWindowRect(rcHeader);
	ScreenToClient(rcHeader);
	//The x coordinate is in the low-order word and the y coordinate is in the high-order word.
	CPoint pt;
	pt.x = GET_X_LPARAM(lParam);
	pt.y = GET_Y_LPARAM(lParam);
	if(rcHeader.PtInRect(pt) && message == WM_LBUTTONDOWN)
	{
		if(m_ComboBox.m_hWnd != NULL)
		{
			DWORD dwStyle = m_ComboBox.GetStyle();
			if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
			{
				m_ComboBox.ShowWindow(SW_HIDE);
			}
		}	
	}	
}
BOOL CComboBoxListCtrl::PreTranslateMessage(MSG* pMsg) 
{
	if(0)//pMsg->message == WM_KEYDOWN)
	{
		if(pMsg->wParam == VK_TAB && m_ComboBox.m_hWnd!= NULL)
		{
			DWORD dwStyle = m_ComboBox.GetStyle();
			if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
			{
				OnComboBoxEnd(TRUE,TRUE);
				CRect rcCtrl;		 
				int nItem;
				int nSub;
				if(FALSE == Key_Ctrl(nItem,nSub))
					Key_Shift(nItem,nSub);
				CListCtrl::GetSubItemRect(nItem,nSub,LVIR_LABEL,rcCtrl); 
				CPoint pt(rcCtrl.left+1,rcCtrl.top+1);
				OnLButtonDblClk(0,pt);
				POSITION pos = CListCtrl::GetFirstSelectedItemPosition();
				if (pos == NULL)
				{
				}
				else
				{
					while (pos)
					{
						int ntpItem = CListCtrl::GetNextSelectedItem(pos);
						CListCtrl::SetItemState(ntpItem,0,LVIS_SELECTED);
					}
				}
				CListCtrl::SetItemState(nItem,  LVIS_SELECTED,  LVIS_SELECTED);
				return TRUE;
			}
		}
	}

	return CListCtrl::PreTranslateMessage(pMsg);
}

BOOL CComboBoxListCtrl::Key_Shift(int& nItem,int& nSub)
{
	int nItemCount = CListCtrl::GetItemCount();
	DWORD dwData = m_ComboBox.GetCtrlData();
	nItem= dwData>>16;
	nSub = dwData&0x0000ffff;

	CHeaderCtrl* pHeader = CListCtrl::GetHeaderCtrl();
	if(pHeader == NULL)
		return FALSE;

	short sRet = GetKeyState(VK_SHIFT);
	int nSubcCount = pHeader->GetItemCount();
	sRet = sRet >>15;
	if(sRet == 0)
	{
		nSub += 1;
		if(nSub >= nSubcCount)
		{
			if(nItem == nItemCount-1)
			{
				nItem = 0;
				nSub  = 0;
			}
			else
			{
				nSub = 0;
				nItem += 1;
			}
		}
		if(nItem >= nItemCount)
			nItem = nItemCount-1;
		return FALSE;
	}
	else
	{
		nSub -= 1;
		if(nSub < 0)
		{

			nSub = nSubcCount -1;
			nItem --;
		}

		if(nItem < 0)
			nItem = nItemCount-1;
		return TRUE;

	}
	return FALSE;
}
BOOL CComboBoxListCtrl::Key_Ctrl(int& nItem,int &nSub)
{
	short sRet = GetKeyState(VK_CONTROL);
	DWORD dwData = m_ComboBox.GetCtrlData();
	nItem= dwData>>16;
	nSub = dwData&0x0000ffff;

	sRet = sRet >>15;
	int nItemCount = CListCtrl::GetItemCount();
	if(sRet == 0)
	{

	}
	else
	{
		nItem = nItem >=nItemCount-1? 0:nItem+=1;
		return TRUE;
	}

	return FALSE;
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

CListCtrlComboBox::CListCtrlComboBox()
{
}

CListCtrlComboBox::~CListCtrlComboBox()
{
}


BEGIN_MESSAGE_MAP(CListCtrlComboBox, CComboBox)
	//{{AFX_MSG_MAP(CListCtrlComboBox)
	ON_WM_KILLFOCUS()
	ON_WM_SETFOCUS()
	ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseup)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CListCtrlComboBox message handlers
void CListCtrlComboBox::SetCtrlData(DWORD dwData)
{
	m_dwData = dwData;
}

DWORD CListCtrlComboBox::GetCtrlData()
{
	return m_dwData;
}

void CListCtrlComboBox::OnKillFocus(CWnd* pNewWnd) 
{
	CComboBox::OnKillFocus(pNewWnd);
	CWnd* pParent = this->GetParent();
	::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
}

BOOL CListCtrlComboBox::PreTranslateMessage(MSG* pMsg) 
{

	if(pMsg->message == WM_KEYDOWN)
	{
		if(pMsg->wParam == VK_RETURN)
		{
			CWnd* pParent = this->GetParent();
			m_bExchange = TRUE;
			::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
		}
		else if(pMsg->wParam == VK_ESCAPE)
		{
			CWnd* pParent = this->GetParent();
			m_bExchange = FALSE;
			::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
		}
	}

	return CComboBox::PreTranslateMessage(pMsg);
}

void CListCtrlComboBox::OnSetFocus(CWnd* pOldWnd) 
{
	CComboBox::OnSetFocus(pOldWnd);
	m_bExchange = TRUE;	
}

void CComboBoxListCtrl::InitStyle()
{
	SetFont (&g_listctrlfont);
	SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP);
}

void CListCtrlComboBox::OnCloseup() 
{
	// TODO: Add your control notification handler code here
	CWnd* pParent = this->GetParent();
	::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);	
}

#undef SubclassWindow   
void CComboBoxListCtrl::PreSubclassWindow()
{
	// the list control must have the report style.
	CListCtrl::PreSubclassWindow();
	m_ctlHeader.SubclassWindow ( GetHeaderCtrl()->GetSafeHwnd());
}