//////////////////////////////////////////////////////////////////////
//
// Разработчик: Алексей Фёдоров aka АЛьФ, mailto: alf@dorex.ru
//
//////////////////////////////////////////////////////////////////////
//
// MyComboBox.cpp : implementation file
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "addin.h"
#include "MyComboBox.h"

extern CBkEndUI * pBkEndUI;

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

/////////////////////////////////////////////////////////////////////////////
// CMyComboBox

CMyComboBox::CMyComboBox()
{
	m_LastString = "";
}

CMyComboBox::~CMyComboBox()
{
}


BEGIN_MESSAGE_MAP(CMyComboBox, CComboBox)
	//{{AFX_MSG_MAP(CMyComboBox)
	ON_CONTROL_REFLECT(CBN_EDITCHANGE, OnEditchange)
	ON_CONTROL_REFLECT(CBN_SELCHANGE, OnSelchange)
	ON_CONTROL_REFLECT(CBN_KILLFOCUS, OnKillfocus)
	ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyComboBox message handlers

void CMyComboBox::OnEditchange() 
{
	CString st = "";
	GetWindowText(st);

	if(st == "")
	{
		m_CurSel = 0;
		SetCurSel(0);
		GetLBText(0,m_LastString);
		return;
	}
	if(st == m_LastString.Left(st.GetLength()))
	{
		st = st.Left(st.GetLength() - 1);
	}
	m_LastString = st;
	if(st == "")
	{
		m_CurSel = 0;
		SetCurSel(0);
		GetLBText(0,m_LastString);
		return;
	}
	CString stLB = "";
	CString stF = st;
	stF.MakeUpper();
	int n = GetCount();
	while(st != "")
	{
		for(int i = 0; i < n; i++)
		{
			GetLBText(i,stLB);
			stLB.MakeUpper();
			if(stF == stLB.Left(stF.GetLength()))
			{
				SetCurSel(i);
				m_CurSel = i;
				m_LastString = st;
				SetEditSel(stF.GetLength(),-1);
				return;
			}
		}
		st = st.Left(st.GetLength() - 1);
		stF = stF.Left(stF.GetLength() - 1);
	}
	m_CurSel = 0;
	SetCurSel(0);
	GetLBText(0,m_LastString);
	return;
}

void CMyComboBox::OnSelchange() 
{
	int n = GetCurSel();
	m_CurSel = n;
	GetLBText(n,m_LastString);
}

CEdit* CMyComboBox::GetEditCtrl()
{
	return (CEdit*)GetWindow(GW_CHILD);
}

void CMyComboBox::OnKillfocus() 
{
	SetCurSel(m_CurSel);
}

void CMyComboBox::OnEditUpdate()
{
	int n = GetCurSel();
	m_CurSel = n;
	GetLBText(n,m_LastString);
}