Skip to content

Commit

Permalink
Implement wxTextCtrl::SearchText() in wxQt
Browse files Browse the repository at this point in the history
  • Loading branch information
ali kettab authored and vadz committed Jan 8, 2025
1 parent d8ba9ce commit 5319941
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/wx/qt/textctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class WXDLLIMPEXP_CORE wxTextCtrl : public wxTextCtrlBase

virtual void SetMaxLength(unsigned long len) override;

virtual wxTextSearchResult SearchText(const wxTextSearch& search) const override;

protected:
virtual wxSize DoGetBestSize() const override;

Expand Down
49 changes: 49 additions & 0 deletions src/qt/textctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class wxQtEdit
virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, long *pos) const = 0;
virtual void WriteText( const wxString &text ) = 0;
virtual void SetMaxLength(unsigned long len) = 0;
virtual wxTextSearchResult SearchText(const wxTextSearch& search) const = 0;
virtual void MarkDirty() = 0;
virtual void DiscardEdits() = 0;
virtual void blockSignals(bool block) = 0;
Expand Down Expand Up @@ -291,6 +292,44 @@ class wxQtMultiLineEdit : public wxQtEdit
wxMISSING_IMPLEMENTATION("not implemented for multiline control");
}

virtual wxTextSearchResult SearchText(const wxTextSearch& search) const override
{
// set up the flags
QTextDocument::FindFlags options;

if ( search.m_direction == wxTextSearch::Direction::Up )
{
options |= QTextDocument::FindBackward;
}
if ( search.m_wholeWord )
{
options |= QTextDocument::FindWholeWords;
}
if ( search.m_matchCase )
{
options |= QTextDocument::FindCaseSensitively;
}

const auto searchValue = wxQtConvertString(search.m_searchValue);
const auto startPos = search.m_startingPosition != -1
? search.m_startingPosition // user-provided start
: (search.m_direction == wxTextSearch::Direction::Down) ?
// if going down, then start from 0; otherwise, start from end
0 : m_edit->document()->characterCount()-1;

const auto cursor = m_edit->document()->find(searchValue, startPos, options);

wxTextSearchResult result;

if ( !cursor.isNull() )
{
result.m_start = cursor.selectionStart();
result.m_end = cursor.selectionEnd();
}

return result;
}

virtual void MarkDirty() override
{
return m_edit->setWindowModified( true );
Expand Down Expand Up @@ -475,6 +514,11 @@ class wxQtSingleLineEdit : public wxQtEdit
m_edit->setMaxLength(len);
}

virtual wxTextSearchResult SearchText(const wxTextSearch& WXUNUSED(search)) const override
{
return {};
}

virtual void MarkDirty() override
{
return m_edit->setModified( true );
Expand Down Expand Up @@ -903,4 +947,9 @@ void wxTextCtrl::DoSetValue( const wxString &text, int flags )
}
}

wxTextSearchResult wxTextCtrl::SearchText(const wxTextSearch& search) const
{
return m_qtEdit->SearchText(search);
}

#endif // wxUSE_TEXTCTRL
2 changes: 1 addition & 1 deletion tests/controls/textctrltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ TEST_CASE("wxTextCtrl::Get/SetRTFValue", "[wxTextCtrl][rtf]")
}
#endif

#if defined(__WXMSW__) || defined(__WXGTK3__)
#if defined(__WXMSW__) || defined(__WXGTK3__) || defined(__WXQT__)
TEST_CASE("wxTextCtrl::SearchText", "[wxTextCtrl][search]")
{
wxWindow* const parent = wxTheApp->GetTopWindow();
Expand Down

0 comments on commit 5319941

Please sign in to comment.