wxTextCtrl and SetSelection
Posted: Wed Oct 03, 2007 5:24 am
Is there any solution to make cross platform searching in wxTextCtrl and highlighting founded text area? There is code founded in Internet, but it is not working on Windows because of endline differences between platforms
Code: Select all
void MyClass::OnFindDlgFind(wxFindDialogEvent& event)
{
// build the regex we're looking for
wxString strFind = event.GetFindString();
const int flagsFind = event.GetFlags();
if (strFind != m_strFind || flagsFind != m_flagsFind)
{
// need to (re)compile because either the text or the flags changed
int flagsRE = wxRE_EXTENDED | wxRE_NEWLINE;
if (!(flagsFind & wxFR_MATCHCASE))
flagsRE |= wxRE_ICASE;
if (flagsFind & wxFR_WHOLEWORD)
strFind = wxString(_T("[[:<:]]")) + strFind + _T("[[:>:]]");
if (!m_regexFind.Compile(strFind, flagsRE))
{
wxLogError(_("Invalid regular expression \"%s\"."), strFind.c_str());
return;
}
m_strFind = strFind;
m_flagsFind = flagsFind;
}
//else: ok, continue with the old regex
// start searching for it
const wxString text = tcText->GetValue();
if (!m_regexFind.Matches(text.c_str() + m_posFind))
{
// reset the selection but use m_posFind and not 0 to prevent the control
// from scrolling back to the origin if it had been scrolled down
textcontrol->SetSelection(m_posFind, m_posFind);
m_strFind.clear();
m_posFind = 0;
wxLogError(m_posFind ? _("No more matches") : _("No match"));
}
else // highlight the found text
{
size_t pos, len;
m_regexFind.GetMatch(&pos, &len);
// note that pos is relative to the start of search, i.e. m_posFind
m_posFind += pos;
textcontrol->SetSelection(m_posFind, m_posFind + len);
m_posFind += len;
}
}