summaryrefslogtreecommitdiff
path: root/ui/search.cpp
blob: 494be60820951fba7b0383cba6f663e82842d7c7 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************
//
#include "search.h"
#include "guiGenerated.h"
#include <wx/msgdlg.h>
#include <wx/utils.h>


class SearchDlg : public SearchDialogGenerated
{
public:
    SearchDlg(wxWindow& parentWindow, wxString& searchText, bool& respectCase);

    enum ReturnCodes
    {
        BUTTON_OKAY = 1 //mustn't be 0
    };

private:
    void OnClose(wxCloseEvent& event);
    void OnCancel(wxCommandEvent& event);
    void OnFindNext(wxCommandEvent& event);
    void OnText(wxCommandEvent& event);

    wxString& searchText_;
    bool& respectCase_;
};


SearchDlg::SearchDlg(wxWindow& parentWindow, wxString& searchText, bool& respectCase) :
    SearchDialogGenerated(&parentWindow),
    searchText_(searchText),
    respectCase_(respectCase)
{
    m_checkBoxMatchCase->SetValue(respectCase_);
    m_textCtrlSearchTxt->SetValue(searchText_);

    CentreOnParent(); //this requires a parent window!
}


void SearchDlg::OnClose(wxCloseEvent& event)
{
    EndModal(0);
}


void SearchDlg::OnCancel(wxCommandEvent& event)
{
    EndModal(0);
}


void SearchDlg::OnFindNext(wxCommandEvent& event)
{
    respectCase_ = m_checkBoxMatchCase->GetValue();
    searchText_  = m_textCtrlSearchTxt->GetValue();
    EndModal(BUTTON_OKAY);
}


void SearchDlg::OnText(wxCommandEvent& event)
{
    if (m_textCtrlSearchTxt->GetValue().Trim().IsEmpty())
        m_buttonFindNext->Disable();
    else
        m_buttonFindNext->Enable();

    event.Skip();
}
//###########################################################################################


template <bool respectCase>
class FindInText
{
public:
    FindInText(const wxString& textToFind);
    bool found(const wxString& phrase) const;

private:
    wxString textToFind_;
};


template <>
FindInText<true>::FindInText(const wxString& textToFind) :
    textToFind_(textToFind) {}


template <>
inline
bool FindInText<true>::found(const wxString& phrase) const
{
    return phrase.Find(textToFind_) != wxNOT_FOUND;
}


template <>
FindInText<false>::FindInText(const wxString& textToFind) :
    textToFind_(textToFind)
{
    textToFind_.MakeUpper();
}


template <>
inline
bool FindInText<false>::found(const wxString& phrase) const
{
    wxString phraseTmp = phrase; //wxWidgets::MakeUpper() is inefficient!
    phraseTmp.MakeUpper();       //But performance is not THAT important for this high-level search functionality
    return phraseTmp.Find(textToFind_) != wxNOT_FOUND;
}
//###########################################################################################


template <bool respectCase>
std::pair<int, int> searchGrid(const wxGrid& grid,
                               const wxString& searchString,
                               bool fromBeginToCursor, //specify area to search
                               bool afterCursorToEnd)  //
{
    const int rowCount    = const_cast<wxGrid&>(grid).GetNumberRows();
    const int columnCount = const_cast<wxGrid&>(grid).GetNumberCols();

    //consistency checks on ints: wxGrid uses ints, so we have to use them, too
    if (rowCount <= 0 || columnCount <= 0)
        return std::make_pair(-1, -1);

    int cursorRow    = const_cast<wxGrid&>(grid).GetGridCursorRow();
    int cursorColumn = const_cast<wxGrid&>(grid).GetGridCursorCol();

    if (    cursorRow    < 0         ||
            cursorRow    >= rowCount ||
            cursorColumn < 0         ||
            cursorColumn >= columnCount)
    {
        //cursor not on valid position...
        cursorRow    = 0;
        cursorColumn = 0;
    }

    const FindInText<respectCase> searchTxt(searchString);

    if (fromBeginToCursor)
    {
        for (int row = 0; row < cursorRow; ++row)
            for (int col = 0; col < columnCount; ++col)
                if (searchTxt.found(const_cast<wxGrid&>(grid).GetCellValue(row, col)))
                    return std::make_pair(row, col);

        for (int col = 0; col <= cursorColumn; ++col)
            if (searchTxt.found(const_cast<wxGrid&>(grid).GetCellValue(cursorRow, col)))
                return std::make_pair(cursorRow, col);
    }

    if (afterCursorToEnd)
    {
        //begin search after cursor cell...
        for (int col = cursorColumn + 1; col < columnCount; ++col)
            if (searchTxt.found(const_cast<wxGrid&>(grid).GetCellValue(cursorRow, col)))
                return std::make_pair(cursorRow, col);

        for (int row = cursorRow + 1; row < rowCount; ++row)
            for (int col = 0; col < columnCount; ++col)
                if (searchTxt.found(const_cast<wxGrid&>(grid).GetCellValue(row, col)))
                    return std::make_pair(row, col);
    }

    return std::make_pair(-1, -1);
}


//syntactic sugar...
std::pair<int, int> searchGrid(const wxGrid& grid,
                               bool respectCase,
                               const wxString& searchString,
                               bool fromBeginToCursor, //specify area to search
                               bool afterCursorToEnd)  //
{
    return respectCase ?
           searchGrid<true>( grid, searchString, fromBeginToCursor, afterCursorToEnd) :
           searchGrid<false>(grid, searchString, fromBeginToCursor, afterCursorToEnd);
}


wxString lastSearchString; //this variable really is conceptionally global...


void executeSearch(bool forceShowDialog,
                   bool& respectCase,
                   wxWindow& parentWindow,
                   wxGrid& leftGrid,
                   wxGrid& rightGrid)
{
    if (forceShowDialog || lastSearchString.IsEmpty())
    {
        SearchDlg* searchDlg = new SearchDlg(parentWindow, lastSearchString, respectCase); //wxWidgets deletion handling -> deleted by parentWindow
        if (static_cast<SearchDlg::ReturnCodes>(searchDlg->ShowModal()) != SearchDlg::BUTTON_OKAY)
            return;
    }

    wxGrid* targetGrid = NULL;     //filled if match is found
    std::pair<int, int> targetPos; //
    {
        wxBusyCursor showHourGlass;

        const bool startLeft = wxWindow::FindFocus() != rightGrid.GetGridWindow();
        wxGrid& firstGrid  = startLeft ? leftGrid  : rightGrid;
        wxGrid& secondGrid = startLeft ? rightGrid : leftGrid;

        //begin with first grid after cursor
        targetGrid = &firstGrid;
        targetPos  = searchGrid(firstGrid, respectCase, lastSearchString, false, true);
        if (targetPos.first == -1)
        {
            //scan second grid completely
            targetGrid = &secondGrid;
            targetPos  = searchGrid(secondGrid, respectCase, lastSearchString, true, true);

            //scan first grid up to cursor
            if (targetPos.first == -1)
            {
                targetGrid = &firstGrid;
                targetPos  = searchGrid(firstGrid, respectCase, lastSearchString, true, false);
            }
        }
    }

    if (targetPos.first != -1 && targetPos.second != -1) //new position found
    {
        targetGrid->SetFocus();
        targetGrid->SetGridCursor(  targetPos.first, targetPos.second);
        targetGrid->SelectRow(      targetPos.first);
        targetGrid->MakeCellVisible(targetPos.first, targetPos.second);
    }
    else
    {
        wxString messageNotFound = _("Cannot find %x");
        messageNotFound.Replace(wxT("%x"), wxString(wxT("\"")) + lastSearchString + wxT("\""), false);
        wxMessageBox(messageNotFound, _("Find"), wxOK);

        //show search dialog again
        executeSearch(true, respectCase, parentWindow, leftGrid, rightGrid);
    }
}
//###########################################################################################


void FreeFileSync::startFind(wxWindow& parentWindow, wxGrid& leftGrid, wxGrid& rightGrid, bool& respectCase) //Strg + F
{
    executeSearch(true, respectCase, parentWindow, leftGrid, rightGrid);
}


void FreeFileSync::findNext(wxWindow& parentWindow, wxGrid& leftGrid, wxGrid& rightGrid, bool& respectCase)  //F3
{
    executeSearch(false, respectCase, parentWindow, leftGrid, rightGrid);
}
bgstack15