wxWidgets에서 검색 기능을 구현했는데.

 제대로 안되는 문제점이 있는걸 확인된다.

 커서 위치를 갱신해주지 않고, 그냥 해당 페이지 위치로 이동하는 문제점이 있는데.

 이걸 해결해주기 위해서 다음과 같이 수정한다.

 

 wxMain.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
// FindDialog를 다음 문자을 검색했을때 처리하는 이벤트
void MyFrame::OnFindNext(wxFindDialogEvent& event) {
    wxString searchString = event.GetFindString();
    int flags = event.GetFlags();
 
    // 검색 시작 위치를 결정합니다.
    long startPos = m_textControl->GetInsertionPoint();
    long textLength = m_textControl->GetValue().length();
 
    // 대소문자 구분 여부를 확인합니다.
    wxString textToSearch = m_textControl->GetValue();
    if (!(flags & wxFR_MATCHCASE))
    {
        textToSearch.MakeLower();
        searchString.MakeLower();
    }
 
    // 검색을 시작합니다.
    long foundPos = -1;
    if (flags & wxFR_DOWN) // "다음 찾기"인 경우
    {
        foundPos = textToSearch.find(searchString, startPos);
        // 검색된 위치가 현재 커서 위치보다 앞인 경우 다음 위치부터 다시 검색
        if (foundPos == startPos) {
            foundPos = textToSearch.find(searchString, startPos + 1);
        };
    }
    else // "이전 찾기"인 경우 (옵션에 따라)
    {
        wxString textToStartPos = textToSearch.Mid(0, startPos);
        foundPos = textToStartPos.rfind(searchString);
    }
 
    if (foundPos != wxString::npos)
    {
        int lengths = searchString.length();
 
        m_textControl->ShowPosition(foundPos);
        m_textControl->SetSelection(foundPos, foundPos + lengths);
 
        m_textControl->SetFocus();
    }
    else
    {
        wxMessageBox("Search string not found.""Find", wxOK | wxICON_INFORMATION);
    }
}
cs

 

이렇게 하면, 커서 위치가 이동하고, 선택하며, 해당 단어까지 선택하는 기능이 완성하게 된다.

'm_textControl->ShowPosition(foundPos);'는 검색이 된 단어의 위치로 이동하게 되는 것이고, 

'm_textControl->SetSelection(foundPos, foundPos + lengths);'는 해당 단어를 선택하는 기능을 처리하는 것이다.

'm_textControl->SetFocus();'는 단어를 표시하기 위해서 적용하는 것으로 m_textControl에 검색했을때, 처리가 가능해진다.

Posted by JunkMam
,