From dd622efeb2c55cf13976af9fefd652a737c1cac3 Mon Sep 17 00:00:00 2001 From: Tobias Predel Date: Sat, 2 Dec 2023 10:15:46 +0100 Subject: [PATCH] Implement backwards search --- src/cell.h | 4 ++-- src/document.h | 14 +++++++++----- src/grid.h | 8 ++++++-- src/main.cpp | 1 + src/myframe.h | 5 +++-- src/system.h | 2 +- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/cell.h b/src/cell.h index ab8d5e27..cc8c4a6f 100755 --- a/src/cell.h +++ b/src/cell.h @@ -410,7 +410,7 @@ struct Cell { } } - Cell *FindNextSearchMatch(wxString &search, Cell *best, Cell *selected, bool &lastwasselected) { + Cell *FindNextSearchMatch(wxString &search, Cell *best, Cell *selected, bool &lastwasselected, bool &reverse) { if (sys->casesensitivesearch) { if (text.t.Find(search) >= 0) { if (lastwasselected) best = this; @@ -423,7 +423,7 @@ struct Cell { } } if (selected == this) lastwasselected = true; - if (grid) best = grid->FindNextSearchMatch(search, best, selected, lastwasselected); + if (grid) best = grid->FindNextSearchMatch(search, best, selected, lastwasselected, reverse); return best; } diff --git a/src/document.h b/src/document.h index fd950473..cbb2e21c 100755 --- a/src/document.h +++ b/src/document.h @@ -1179,7 +1179,11 @@ struct Document { } case A_SEARCHNEXT: { - return SearchNext(dc, false, true); + return SearchNext(dc, false, true, false); + } + + case A_SEARCHPREV: { + return SearchNext(dc, false, true, true); } case A_CASESENSITIVESEARCH: { @@ -1190,7 +1194,7 @@ struct Document { sys->frame->filter->GetValue() : sys->frame->filter->GetValue().Lower(); sys->frame->SetSearchTextBoxBackgroundColour(false); - this->SearchNext(dc, false, false); + this->SearchNext(dc, false, false, false); this->Refresh(); return nullptr; } @@ -1230,7 +1234,7 @@ struct Document { } else { loopallcellssel(c, true) if (c->text.IsInSearch()) c->AddUndo(this); selected.g->ReplaceStr(this, replaces, lreplaces, selected); - if (k == A_REPLACEONCEJ) return SearchNext(dc, false, true); + if (k == A_REPLACEONCEJ) return SearchNext(dc, false, true, false); } return _(L"Text has been replaced."); } @@ -2023,12 +2027,12 @@ struct Document { } } - const wxChar *SearchNext(wxDC &dc, bool focusmatch, bool jump) { + const wxChar *SearchNext(wxDC &dc, bool focusmatch, bool jump, bool reverse) { if (!sys->searchstring.Len()) return _(L"No search string."); bool lastsel = true; if (!rootgrid) return nullptr; //fix crash when opening new doc Cell *next = - rootgrid->FindNextSearchMatch(sys->searchstring, nullptr, selected.GetCell(), lastsel); + rootgrid->FindNextSearchMatch(sys->searchstring, nullptr, selected.GetCell(), lastsel, reverse); sys->frame->SetSearchTextBoxBackgroundColour(next); if (!next) return _(L"No matches for search."); if (!jump) return nullptr; diff --git a/src/grid.h b/src/grid.h index 986aec50..c8327d29 100755 --- a/src/grid.h +++ b/src/grid.h @@ -312,8 +312,12 @@ struct Grid { return best; } - Cell *FindNextSearchMatch(wxString &search, Cell *best, Cell *selected, bool &lastwasselected) { - foreachcell(c) best = c->FindNextSearchMatch(search, best, selected, lastwasselected); + Cell *FindNextSearchMatch(wxString &search, Cell *best, Cell *selected, bool &lastwasselected, bool &reverse) { + if (reverse) { + foreachcellrev(c) best = c->FindNextSearchMatch(search, best, selected, lastwasselected, reverse); + } else { + foreachcell(c) best = c->FindNextSearchMatch(search, best, selected, lastwasselected, reverse); + } return best; } diff --git a/src/main.cpp b/src/main.cpp index 71d997c2..049bf9be 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -152,6 +152,7 @@ enum { A_LINKIMG, A_LINKIMGREV, A_SEARCHNEXT, + A_SEARCHPREV, A_CUSTCOL, A_COLCELL, A_SORT, diff --git a/src/myframe.h b/src/myframe.h index 6e2742b9..8daef4a8 100755 --- a/src/myframe.h +++ b/src/myframe.h @@ -448,6 +448,7 @@ struct MyFrame : wxFrame { semenu->AppendCheckItem(A_CASESENSITIVESEARCH, _(L"Case-sensitive search")); semenu->Check(A_CASESENSITIVESEARCH, sys->casesensitivesearch); MyAppend(semenu, A_SEARCHNEXT, _(L"&Go To Next Search Result\tF3")); + MyAppend(semenu, A_SEARCHPREV, _(L"Go To &Previous Search Result\tSHIFT+F3")); MyAppend(semenu, A_REPLACEF, _(L"&Replace\tCTRL+h")); MyAppend(semenu, A_REPLACEONCE, _(L"Replace in Current &Selection\tCTRL+k")); MyAppend(semenu, A_REPLACEONCEJ, _(L"Replace in Current Selection && &Jump Next\tCTRL+j")); @@ -478,7 +479,7 @@ struct MyFrame : wxFrame { // xgettext:no-c-format MyAppend(filtermenu, A_FILTERL, _(L"Show 1% less than the last filter")); MyAppend(filtermenu, A_FILTERBYCELLBG, _(L"Filter by the same cell color")); - MyAppend(filtermenu, A_FILTERMATCHNEXT, _(L"Go to next filter match\tSHIFT+F3")); + MyAppend(filtermenu, A_FILTERMATCHNEXT, _(L"Go to next filter match\tCTRL+F3")); wxMenu *viewmenu = new wxMenu(); MyAppend(viewmenu, A_ZOOMIN, _(L"Zoom &In (CTRL+mousewheel)\tCTRL+PGUP")); @@ -1066,7 +1067,7 @@ struct MyFrame : wxFrame { Document *doc = GetCurTab()->doc; TSCanvas *sw = GetCurTab(); wxClientDC dc(sw); - doc->SearchNext(dc, false, false); + doc->SearchNext(dc, false, false, false); if (doc->searchfilter) { doc->SetSearchFilter(sys->searchstring.Len() != 0); } else diff --git a/src/system.h b/src/system.h index a95d11ee..b2c1c983 100755 --- a/src/system.h +++ b/src/system.h @@ -158,7 +158,7 @@ struct System { newdoc->sw->SetFocus(); newdoc->UpdateFileName(); wxClientDC dc(newdoc->sw); - newdoc->SearchNext(dc, false, false); + newdoc->SearchNext(dc, false, false, false); } void Init(const wxString &filename) {