#include "AutoCompleteComboBox.h"

CAutoCompleteComboBox::CAutoCompleteComboBox(wxWindow* parent,
                                             wxWindowID id,
                                             const wxString& value,
                                             const wxPoint& pos,
                                             const wxSize& size,
                                             const wxSortedArrayString& choices,
                                             long style,
                                             const wxValidator& validator,
                                             const wxString& name)
: wxComboBox(parent, id, value, pos, size, choices, style, validator, name),
  m_Choices(choices), m_ValueSelected(false)
{
    Connect(id, wxEVT_COMMAND_TEXT_UPDATED,
            wxCommandEventHandler(CAutoCompleteComboBox::SearchForString) );
    Connect(id, wxEVT_COMMAND_COMBOBOX_SELECTED,
            wxCommandEventHandler(CAutoCompleteComboBox::GetSelection) );
}

CAutoCompleteComboBox::~CAutoCompleteComboBox()
{

}

void CAutoCompleteComboBox::SearchForString(wxCommandEvent &event)
{

    if (m_ValueSelected)
    {
        m_ValueSelected = false;
        return;
    }

    const size_t NumItems = m_Choices.GetCount();
    const long StrLen = GetInsertionPoint();
    wxString ValueToFind = GetValue().Left(StrLen);
    wxString TempStr;

    for (size_t x = 0; x != NumItems; x++)
    {
        TempStr = m_Choices[x].Left(StrLen);
        if (TempStr.IsSameAs(ValueToFind, false) )
        {
            ChangeValue(m_Choices[x] );
            SetSelection(StrLen, -1);
            return;
        }
    }

}

void CAutoCompleteComboBox::GetSelection(wxCommandEvent &event)
{
    m_ValueSelected = true;
}


