diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:27:42 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:27:42 +0200 |
commit | b916407a2a06f8452e82b74dc44c54acbcc572b0 (patch) | |
tree | 46358e0bb035fca0f42edb4b5b8aa5f1613814af /ui/search.cpp | |
parent | 5.20 (diff) | |
download | FreeFileSync-b916407a2a06f8452e82b74dc44c54acbcc572b0.tar.gz FreeFileSync-b916407a2a06f8452e82b74dc44c54acbcc572b0.tar.bz2 FreeFileSync-b916407a2a06f8452e82b74dc44c54acbcc572b0.zip |
5.21
Diffstat (limited to 'ui/search.cpp')
-rw-r--r-- | ui/search.cpp | 180 |
1 files changed, 34 insertions, 146 deletions
diff --git a/ui/search.cpp b/ui/search.cpp index 4c9c5366..c834b934 100644 --- a/ui/search.cpp +++ b/ui/search.cpp @@ -5,82 +5,19 @@ // ************************************************************************** #include "search.h" -#include "gui_generated.h" -#include <wx/msgdlg.h> -#include <wx/utils.h> -#include <utility> #include <zen/string_tools.h> -#include <wx+/mouse_move_dlg.h> -using namespace zen; - - -class SearchDlg : public SearchDialogGenerated -{ -public: - SearchDlg(wxWindow* parent, wxString& searchText, bool& respectCase); - - enum ReturnCodes - { - BUTTON_CANCEL, - BUTTON_OKAY - }; - -private: - void OnClose (wxCloseEvent& event) { EndModal(BUTTON_CANCEL); } - void OnCancel(wxCommandEvent& event) { EndModal(BUTTON_CANCEL); } - void OnFindNext(wxCommandEvent& event); - void OnText(wxCommandEvent& event); - - wxString& searchText_; - bool& respectCase_; -}; - - -SearchDlg::SearchDlg(wxWindow* parent, wxString& searchText, bool& respectCase) : - SearchDialogGenerated(parent), - searchText_(searchText), - respectCase_(respectCase) -{ -#ifdef ZEN_WIN - new zen::MouseMoveWindow(*this); //allow moving main dialog by clicking (nearly) anywhere...; ownership passed to "this" -#endif - - m_checkBoxMatchCase->SetValue(respectCase_); - m_textCtrlSearchTxt->SetValue(searchText_); - - m_textCtrlSearchTxt->SetFocus(); -} - - -void SearchDlg::OnFindNext(wxCommandEvent& event) -{ - respectCase_ = m_checkBoxMatchCase->GetValue(); - searchText_ = m_textCtrlSearchTxt->GetValue(); - EndModal(BUTTON_OKAY); -} +using namespace zen; -void SearchDlg::OnText(wxCommandEvent& event) +namespace { - if (m_textCtrlSearchTxt->GetValue().Trim().IsEmpty()) - m_buttonFindNext->Disable(); - else - m_buttonFindNext->Enable(); - - event.Skip(); -} -//########################################################################################### - -template <bool respectCase> -class FindInText; - template <bool respectCase> -class FindInText +class ContainsMatch { public: - FindInText(const wxString& textToFind) : textToFind_(textToFind) {} - bool found(const wxString& phrase) const { return contains(phrase, textToFind_); } + ContainsMatch(const wxString& textToFind) : textToFind_(textToFind) {} + bool operator()(const wxString& phrase) const { return contains(phrase, textToFind_); } private: wxString textToFind_; @@ -88,11 +25,11 @@ private: template <> -class FindInText<false> +class ContainsMatch<false> { public: - FindInText(const wxString& textToFind) : textToFind_(textToFind) { textToFind_.MakeUpper(); } - bool found(wxString&& phrase) const + ContainsMatch(const wxString& textToFind) : textToFind_(textToFind) { textToFind_.MakeUpper(); } + bool operator()(wxString&& phrase) const { //wxWidgets::MakeUpper() is inefficient! But performance is not THAT important for this high-level search functionality phrase.MakeUpper(); @@ -111,102 +48,53 @@ ptrdiff_t findRow(const Grid& grid, //return -1 if no matching row found size_t rowFirst, //specify area to search: size_t rowLast) // [rowFirst, rowLast) { - auto prov = grid.getDataProvider(); - std::vector<Grid::ColumnAttribute> colAttr = grid.getColumnConfig(); - vector_remove_if(colAttr, [](const Grid::ColumnAttribute& ca) { return !ca.visible_; }); - if (!colAttr.empty() && prov) + if (auto prov = grid.getDataProvider()) { - const FindInText<respectCase> searchTxt(searchString); + std::vector<Grid::ColumnAttribute> colAttr = grid.getColumnConfig(); + vector_remove_if(colAttr, [](const Grid::ColumnAttribute& ca) { return !ca.visible_; }); + if (!colAttr.empty()) + { + const ContainsMatch<respectCase> containsMatch(searchString); - for (size_t row = rowFirst; row < rowLast; ++row) - for (auto iterCol = colAttr.begin(); iterCol != colAttr.end(); ++iterCol) - if (searchTxt.found(prov->getValue(row, iterCol->type_))) - return row; + for (size_t row = rowFirst; row < rowLast; ++row) + for (auto iterCol = colAttr.begin(); iterCol != colAttr.end(); ++iterCol) + if (containsMatch(prov->getValue(row, iterCol->type_))) + return row; + } } return -1; } - - -//syntactic sugar... -ptrdiff_t findRow(Grid& grid, - bool respectCase, - const wxString& searchString, - size_t rowFirst, //specify area to search: - size_t rowLast) // [rowFirst, rowLast) -{ - return respectCase ? - findRow<true>( grid, searchString, rowFirst, rowLast) : - findRow<false>(grid, searchString, rowFirst, rowLast); } -wxString lastSearchString; //this variable really is conceptionally global... - - -void executeSearch(bool forceShowDialog, - bool& respectCase, - wxWindow* parent, - Grid* gridL, Grid* gridR) +std::pair<const Grid*, ptrdiff_t> zen::findGridMatch(const Grid& grid1, const Grid& grid2, const wxString& searchString, bool respectCase) { - bool searchDialogWasShown = false; - - if (forceShowDialog || lastSearchString.IsEmpty()) - { - SearchDlg searchDlg(parent, lastSearchString, respectCase); //wxWidgets deletion handling -> deleted by parentWindow - if (static_cast<SearchDlg::ReturnCodes>(searchDlg.ShowModal()) != SearchDlg::BUTTON_OKAY) - return; - - searchDialogWasShown = true; - } - - if (wxWindow::FindFocus() == &gridR->getMainWin()) - std::swap(gridL, gridR); //select side to start with + const size_t rowCountL = grid1.getRowCount(); + const size_t rowCountR = grid2.getRowCount(); + auto cursorPos = grid1.getGridCursor(); //(row, component pos) - const size_t rowCountL = gridL->getRowCount(); - const size_t rowCountR = gridR->getRowCount(); - auto cursorPos = gridL->getGridCursor(); //(row, component pos) + std::pair<const Grid*, ptrdiff_t> result(nullptr, -1); size_t cursorRowL = cursorPos.first; if (cursorRowL >= rowCountL) cursorRowL = 0; { - wxBusyCursor showHourGlass; - - auto finishSearch = [&](Grid& grid, size_t rowFirst, size_t rowLast) -> bool + auto finishSearch = [&](const Grid& grid, size_t rowFirst, size_t rowLast) -> bool { - const ptrdiff_t targetRow = findRow(grid, respectCase, lastSearchString, rowFirst, rowLast); + const ptrdiff_t targetRow = respectCase ? + findRow<true>( grid, searchString, rowFirst, rowLast) : + findRow<false>(grid, searchString, rowFirst, rowLast); if (targetRow >= 0) { - //gridOther.clearSelection(); -> not needed other grids are automatically cleared after selection - grid.setGridCursor(targetRow); - grid.SetFocus(); + result = std::make_pair(&grid, targetRow); return true; } return false; }; - if (finishSearch(*gridL, cursorRowL + 1, rowCountL) || - finishSearch(*gridR, 0, rowCountR) || - finishSearch(*gridL, 0, cursorRowL + 1)) - return; + if (!finishSearch(grid1, cursorRowL + 1, rowCountL)) + if (!finishSearch(grid2, 0, rowCountR)) + finishSearch(grid1, 0, cursorRowL + 1); } - - wxMessageBox(replaceCpy(_("Cannot find %x"), L"%x", L"\"" + lastSearchString + L"\"", false), _("Find"), wxOK, parent); - - //show search dialog again - if (searchDialogWasShown) - executeSearch(true, respectCase, parent, gridL, gridR); -} -//########################################################################################### - - -void zen::startFind(wxWindow* parent, Grid& gridL, Grid& gridR, bool& respectCase) //Strg + F -{ - executeSearch(true, respectCase, parent, &gridL, &gridR); -} - - -void zen::findNext(wxWindow* parent, Grid& gridL, Grid& gridR, bool& respectCase) //F3 -{ - executeSearch(false, respectCase, parent, &gridL, &gridR); -} + return result; +}
\ No newline at end of file |