Page 1 of 1

wxTextCtrl and SetSelection

Posted: Wed Oct 03, 2007 5:24 am
by normunds
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());

        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_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;

Posted: Wed Oct 03, 2007 2:09 pm
by protocol
You can check out my project: QuRegExmm, it seems to be doing what you are trying to accomplish.

best regards.

Posted: Thu Oct 04, 2007 7:52 am
by normunds
Solution was to set wxTE_RICH2 to wxTextCtrl attributes.