wxWidgets의 wxTextCtrl를 가지고 왔을때, std::stringstream으로 해서 받아와서 그런지. find에서 줄 바꿈을 제대로 처리 못하는 문제점이 있다.

 그래서 줄 바꿈이 있을시 검색하는 기능으로 수정했다.

 

 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 
// 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();
 
        // \r\n 기준으로 검색된 위치를 찾습니다.
        long lineStart = textToSearch.rfind('\n', foundPos);
        if (lineStart != wxString::npos)
        {
            long lineEnd = textToSearch.find('\n', foundPos);
            if (lineEnd == wxString::npos)
                lineEnd = textToSearch.length();
 
            // 검색된 텍스트가 보이도록 스크롤합니다.
            m_textControl->ShowPosition(lineStart);
 
            // 검색된 텍스트를 선택합니다.
            m_textControl->SetSelection(lineStart, lineEnd+1);
        }
        else
        {
            // \n이 없는 경우 해당 위치로 이동하고 텍스트를 선택합니다.
            m_textControl->SetSelection(foundPos, foundPos + lengths);
            m_textControl->ShowPosition(foundPos);
        }
 
        m_textControl->SetFocus();
    }
    else
    {
        wxMessageBox("Search string not found.""Find", wxOK | wxICON_INFORMATION);
    }
}
 
cs


 이렇게 하면, 줄 바꿈이 있어도 제대로된 값을 검색할 수 있다.

 여기서 lineEnd+1이 붙은 것은 줄바꿈은 \r\n이라는 형태로 행이 바뀌고, 다시 초기 부분으로 바뀌는 부분이 있어서 +1을 하게 되었다.

 buffer의 길이에서 이런 개행 부분을 제대로 처리하는 변수로 바꾸면 문제가 없을 것으로 보인다.

Posted by JunkMam
,