wxTextCtrl and SetSelection Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
normunds
Knows some wx things
Knows some wx things
Posts: 31
Joined: Mon Sep 03, 2007 8:14 am
Contact:

wxTextCtrl and SetSelection

Post by normunds » 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;
    }
}

protocol
Moderator
Moderator
Posts: 680
Joined: Wed Jan 18, 2006 6:13 pm
Location: Dallas, TX
Contact:

Post by protocol » Wed Oct 03, 2007 2:09 pm

You can check out my project: QuRegExmm, it seems to be doing what you are trying to accomplish.
http://forums.wxwidgets.org/viewtopic.php?t=14317
http://quregexmm.sourceforge.net

best regards.
/* UIKit && wxWidgets 2.8 && Cocoa && .Net */
QuRegExmm
wxPCRE & ObjPCRE - Regex It!

normunds
Knows some wx things
Knows some wx things
Posts: 31
Joined: Mon Sep 03, 2007 8:14 am
Contact:

Post by normunds » Thu Oct 04, 2007 7:52 am

Solution was to set wxTE_RICH2 to wxTextCtrl attributes.

Post Reply