// ComboBoxEx.cpp : implementation file
//
#include "stdafx.h"
#include "ComboBoxEx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CComboBoxEx
CComboBoxEx::CComboBoxEx()
{
m_bEditChanged=FALSE;
m_bDoComplete = FALSE;
m_bAutoComplete = FALSE;
}
CComboBoxEx::~CComboBoxEx()
{
}
BEGIN_MESSAGE_MAP(CComboBoxEx, CComboBox)
//{{AFX_MSG_MAP(CComboBoxEx)
ON_CONTROL_REFLECT_EX(CBN_EDITCHANGE, OnEditchange)
ON_CONTROL_REFLECT_EX(CBN_SELCHANGE, OnSelchange)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CComboBoxEx message handlers
void CComboBoxEx::LoadState(LPCSTR szRegSubKey, UINT nMaxItems)
{
CString s,s2;
UINT cnt = AfxGetApp()->GetProfileInt(szRegSubKey, "Count", 0);
for (UINT i=0; i < cnt && i < nMaxItems; i++)
{
s2.Format("Item_%d", i);
s = AfxGetApp()->GetProfileString(szRegSubKey, s2);
if (i==0)
SetWindowText(s);
if (FindStringExact(-1, s) == -1
&& !s.IsEmpty())
AddString(s);
}
}
void CComboBoxEx::SaveState(LPCSTR szRegSubKey, UINT nMaxItems)
{
CString strItem,s,s2;
int i,idx,cnt = GetCount();
GetWindowText(strItem);
if (!strItem.IsEmpty())
{
AfxGetApp()->WriteProfileString(szRegSubKey, "Item_0", strItem);
idx=1;
}
else
idx=0;
for (i=0; i < cnt && idx < (int)nMaxItems; i++)
{
GetLBText(i, s);
if (s != strItem
&& !s.IsEmpty())
{
s2.Format("Item_%d", idx);
AfxGetApp()->WriteProfileString(szRegSubKey, s2, s);
idx++;
}
}
AfxGetApp()->WriteProfileInt(szRegSubKey, "Count", idx);
}
BOOL CComboBoxEx::OnEditchange()
{
m_bEditChanged=TRUE;
int length = GetWindowTextLength();
// bail if not auto completing or no text
if (!m_bDoComplete
|| length <= 0)
return FALSE;
// Get the text in the edit box
CString s;
GetWindowText(s);
// get the current selection
DWORD sel = GetEditSel();
WORD start=LOWORD(sel), end=HIWORD(sel);
// look for the string that is prefixed by the typed text
int idx = FindString(-1, s);
if (idx != CB_ERR)
{
// set the new string
CString strNew;
GetLBText(idx, strNew);
SetWindowText(strNew);
// get the caret back in the right spot
if (sel != CB_ERR)
SetEditSel(start, end);
}
// select the text after our typing
if (sel == CB_ERR
|| end >= length)
SetEditSel(length, -1);
// restore the selection
else
SetEditSel(start, end);
return FALSE;
}
BOOL CComboBoxEx::OnSelchange()
{
m_bEditChanged=FALSE;
return FALSE;
}
BOOL CComboBoxEx::PreTranslateMessage(MSG* pMsg)
{
if (m_bAutoComplete
&& pMsg->message == WM_KEYDOWN)
{
m_bDoComplete = TRUE;
int nVirtKey = (int) pMsg->wParam;
if (nVirtKey == VK_DELETE || nVirtKey == VK_BACK)
m_bDoComplete = FALSE;
}
return CComboBox::PreTranslateMessage(pMsg);
}