File: findreplacedlg.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (614 lines) | stat: -rw-r--r-- 22,217 bytes parent folder | download | duplicates (2)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : findreplacedlg.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "findreplacedlg.h"
#include <wx/msgdlg.h>
#include "editor_config.h"
#include <wx/gbsizer.h>
#include <wx/textctrl.h>
#include <wx/checkbox.h>
#include <wx/combobox.h>
#include <wx/button.h>
#include <wx/stattext.h>
#include "macros.h"
#include <algorithm>
#include "windowattrmanager.h"

DEFINE_EVENT_TYPE(wxEVT_FRD_FIND_NEXT)
DEFINE_EVENT_TYPE(wxEVT_FRD_CLOSE)
DEFINE_EVENT_TYPE(wxEVT_FRD_REPLACE)
DEFINE_EVENT_TYPE(wxEVT_FRD_REPLACEALL)
DEFINE_EVENT_TYPE(wxEVT_FRD_BOOKMARKALL)
DEFINE_EVENT_TYPE(wxEVT_FRD_CLEARBOOKMARKS)

BEGIN_EVENT_TABLE(FindReplaceDialog, wxDialog)
EVT_CLOSE(FindReplaceDialog::OnClose)
EVT_CHAR_HOOK(FindReplaceDialog::OnKeyDown)
EVT_COMMAND(wxID_ANY, wxEVT_FRD_FIND_NEXT, FindReplaceDialog::OnFindEvent)
END_EVENT_TABLE()

#define VALIDATE_FINDWHAT()                          \
    {                                                \
        if(m_findString->GetValue().IsEmpty()) {     \
            wxMessageBox(_("'Find What' is empty")); \
            return;                                  \
        }                                            \
    }

FindReplaceDialog::FindReplaceDialog()
    : wxDialog()
    , m_owner(NULL)
    , m_kind(FIND_DLG)
{
}

FindReplaceDialog::FindReplaceDialog(wxWindow* parent,
                                     const FindReplaceData& data,
                                     wxWindowID id,
                                     const wxString& caption,
                                     const wxPoint& pos,
                                     const wxSize& size,
                                     long style)
{
    Create(parent, data, id, caption, pos, size, style | wxWANTS_CHARS);
}

bool FindReplaceDialog::Create(wxWindow* parent,
                               const FindReplaceData& data,
                               wxWindowID id,
                               const wxString& caption,
                               const wxPoint& pos,
                               const wxSize& size,
                               long style)
{
    m_kind = FIND_DLG;
    if(!wxDialog::Create(parent, id, caption, pos, size, style)) return false;

    m_data = data;
    m_owner = NULL;

    CreateGUIControls();
    ConnectEvents();
    
    SetName("FindAndReplaceDialog");
    WindowAttrManager::Load(this);
    GetSizer()->Fit(this);
    SetMinClientSize(GetSize());
    m_findString->SetFocus();
    CentreOnParent();
    return true;
}

FindReplaceDialog::~FindReplaceDialog() {}

void FindReplaceDialog::CreateGUIControls()
{
    wxBoxSizer* hMainSzier = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* btnSizer = new wxBoxSizer(wxVERTICAL);
    gbSizer = new wxGridBagSizer();
    SetSizer(hMainSzier);

    hMainSzier->Add(gbSizer, 1, wxEXPAND | wxALL, 5);
    hMainSzier->Add(btnSizer, 0, wxALL, 5);

    wxStaticText* itemStaticText;
    itemStaticText =
        new wxStaticText(this, wxID_STATIC, wxT("Find What:"), wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
    gbSizer->Add(itemStaticText, wxGBPosition(0, 0), wxDefaultSpan, wxALL, 5);

    m_findString = new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, -1));
    gbSizer->Add(m_findString, wxGBPosition(0, 1), wxDefaultSpan, wxALL | wxEXPAND, 5);

    m_replaceWithLabel =
        new wxStaticText(this, wxID_STATIC, wxT("Replace With:"), wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
    gbSizer->Add(m_replaceWithLabel, wxGBPosition(1, 0), wxDefaultSpan, wxALL, 5);

    m_replaceString = new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, -1));
    gbSizer->Add(m_replaceString, wxGBPosition(1, 1), wxDefaultSpan, wxALL | wxEXPAND, 5);

    sz = new wxStaticBoxSizer(wxVERTICAL, this, wxT("Options"));
    gbSizer->Add(sz, wxGBPosition(2, 0), wxGBSpan(1, 2), wxALL | wxEXPAND, 5);

    gbSizer->AddGrowableCol(1);

    m_matchCase = new wxCheckBox(this, wxID_ANY, wxT("&Match case"));
    sz->Add(m_matchCase, 1, wxALL | wxEXPAND, 5);

    m_matchWholeWord = new wxCheckBox(this, wxID_ANY, wxT("Match &whole word"));
    sz->Add(m_matchWholeWord, 1, wxALL | wxEXPAND, 5);

    m_regualrExpression = new wxCheckBox(this, wxID_ANY, wxT("Regular &expression"));
    sz->Add(m_regualrExpression, 1, wxALL | wxEXPAND, 5);

    m_searchUp = new wxCheckBox(this, wxID_ANY, wxT("Search &up"));
    sz->Add(m_searchUp, 1, wxALL | wxEXPAND, 5);

    m_selectionOnly = new wxCheckBox(this, wxID_ANY, wxT("Search / Replace in selec&ted text"));
    sz->Add(m_selectionOnly, 1, wxALL | wxEXPAND, 5);

    // Add the buttons

    m_find = new wxButton(this, wxID_ANY, wxT("&Find Next"));
    btnSizer->Add(m_find, 1, wxALL | wxEXPAND, 5);

    m_replace = new wxButton(this, wxID_ANY, wxT("&Replace"));
    btnSizer->Add(m_replace, 1, wxALL | wxEXPAND, 5);

    m_replaceAll = new wxButton(this, wxID_ANY, wxT("Replace &All"));
    btnSizer->Add(m_replaceAll, 1, wxALL | wxEXPAND, 5);

    m_markAll = new wxButton(this, wxID_ANY, wxT("&Bookmark All"));
    btnSizer->Add(m_markAll, 1, wxALL | wxEXPAND, 5);

    m_clearBookmarks = new wxButton(this, wxID_ANY, wxT("&Clear Bookmarks"));
    btnSizer->Add(m_clearBookmarks, 1, wxALL | wxEXPAND, 5);

    m_cancel = new wxButton(this, wxID_ANY, wxT("Close"));
    btnSizer->Add(m_cancel, 1, wxALL | wxEXPAND, 5);

    m_replacementsMsg = new wxStaticText(this, wxID_ANY, wxEmptyString);
    btnSizer->Add(m_replacementsMsg, 1, wxALL | wxEXPAND, 5);
    // gbSizer->Add(m_replacementsMsg, wxGBPosition(3, 0), wxGBSpan(1, 2), wxALL | wxEXPAND, 5);
    SetReplacementsMessage(frd_dontshowzeros);

    // set values
    SetFindReplaceData(m_data, true);
}

void FindReplaceDialog::OnFindEvent(wxCommandEvent& event) { SetFindReplaceData(GetData(), false); }

void FindReplaceDialog::SetFindReplaceData(FindReplaceData& data, bool focus)
{
    m_findString->Freeze();

    m_findString->Clear();
    m_findString->Append(data.GetFindStringArr());
    m_findString->SetValue(data.GetFindString());

    m_findString->Thaw();

    m_replaceString->Freeze();

    m_replaceString->Clear();
    m_replaceString->Append(data.GetReplaceStringArr());
    m_replaceString->SetValue(data.GetReplaceString());

    m_replaceString->Thaw();

    m_matchCase->SetValue(data.GetFlags() & wxFRD_MATCHCASE ? true : false);
    m_matchWholeWord->SetValue(data.GetFlags() & wxFRD_MATCHWHOLEWORD ? true : false);
    m_regualrExpression->SetValue(data.GetFlags() & wxFRD_REGULAREXPRESSION ? true : false);
    m_searchUp->SetValue(data.GetFlags() & wxFRD_SEARCHUP ? true : false);
    m_selectionOnly->SetValue(data.GetFlags() & wxFRD_SELECTIONONLY ? true : false);

    // set the focus to the find string text control
    if(focus) {
        m_findString->SetFocus();
        m_findString->SelectAll();
    }
}

void FindReplaceDialog::OnClick(wxCommandEvent& event)
{
    wxObject* btnClicked = event.GetEventObject();
    size_t flags = m_data.GetFlags();
    m_data.SetFindString(m_findString->GetValue());
    m_data.SetReplaceString(m_replaceString->GetValue());

    // disable the 'Find/Replace' buttons when the 'Selection only' is enabled
    if(m_selectionOnly->IsChecked()) {
        m_find->Enable(false);
        m_replace->Enable(false);
    } else {
        m_find->Enable(true);
        m_replace->Enable(true);
    }

    if(btnClicked == m_find) {
        VALIDATE_FINDWHAT();
        SendEvent(wxEVT_FRD_FIND_NEXT);
    } else if(btnClicked == m_replace) {
        VALIDATE_FINDWHAT();
        SendEvent(wxEVT_FRD_REPLACE);
    } else if(btnClicked == m_replaceAll) {
        VALIDATE_FINDWHAT();
        SendEvent(wxEVT_FRD_REPLACEALL);
    } else if(btnClicked == m_markAll) {
        VALIDATE_FINDWHAT();
        SendEvent(wxEVT_FRD_BOOKMARKALL);
    } else if(btnClicked == m_clearBookmarks) {
        SendEvent(wxEVT_FRD_CLEARBOOKMARKS);
    } else if(btnClicked == m_cancel) {
        // Fire a close event
        SendEvent(wxEVT_FRD_CLOSE);
        // Hide the dialog
        Hide();
        // Make sure the Search in Selected Text flag is clear, otherwise we can't Find Next
        flags &= ~(wxFRD_SELECTIONONLY);
    } else if(btnClicked == m_matchCase) {
        if(m_matchCase->IsChecked()) {
            flags |= wxFRD_MATCHCASE;
        } else {
            flags &= ~(wxFRD_MATCHCASE);
        }
    } else if(btnClicked == m_matchWholeWord) {
        if(m_matchWholeWord->IsChecked()) {
            flags |= wxFRD_MATCHWHOLEWORD;
        } else {
            flags &= ~(wxFRD_MATCHWHOLEWORD);
        }
    } else if(btnClicked == m_regualrExpression) {
        if(m_regualrExpression->IsChecked()) {
            flags |= wxFRD_REGULAREXPRESSION;
        } else {
            flags &= ~(wxFRD_REGULAREXPRESSION);
        }
    } else if(btnClicked == m_searchUp) {
        if(m_searchUp->IsChecked()) {
            flags |= wxFRD_SEARCHUP;
        } else {
            flags &= ~(wxFRD_SEARCHUP);
        }
    } else if(btnClicked == m_selectionOnly) {
        if(m_selectionOnly->IsChecked()) {
            flags |= wxFRD_SELECTIONONLY;
        } else {
            flags &= ~(wxFRD_SELECTIONONLY);
        }
    }

    // Set the updated flags, unless it was ReplaceAll which does this itself
    if(btnClicked != m_replaceAll) {
        m_data.SetFlags(flags);
    }

// update the data of the find/replace dialog, in particular,
// update the history of the Find What / replace with controls
#if defined(__WXGTK__) && wxVERSION_NUMBER >= 2900
    // But if it's a findNext or a Replace, do it by posting an event,
    // otherwise strange duplications happen (because scintilla steals the primary selection?)
    wxCommandEvent e(wxEVT_FRD_FIND_NEXT); // Arbitrary choice of event-type
    wxPostEvent(this, e);
#else
    SetFindReplaceData(m_data, false);
#endif
}

void FindReplaceDialog::OnClose(wxCloseEvent& event)
{
    wxUnusedVar(event);

    // Fire a close event
    SendEvent(wxEVT_FRD_CLOSE);
    Hide();

    // Make sure the Search in Selected Text flag is clear, otherwise we can't Find Next
    size_t flags = m_data.GetFlags();
    flags &= ~(wxFRD_SELECTIONONLY);
    m_data.SetFlags(flags);
}

void FindReplaceDialog::OnKeyDown(wxKeyEvent& event)
{
    if(event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER) {
        // start the search
        size_t flags = m_data.GetFlags();
        m_data.SetFindString(m_findString->GetValue());
        m_data.SetReplaceString(m_replaceString->GetValue());
        SendEvent(wxEVT_FRD_FIND_NEXT);
        // Set the updated flags
        m_data.SetFlags(flags);
        event.Skip(false);
        return;
    } // if(event.GetKeyCode() == WXK_RETURN)

    if(event.GetKeyCode() == WXK_ESCAPE) {
        // hide the find/replace dialog
        if(IsShown()) {
            Hide();
            // Make sure the Search in Selected Text flag is clear, otherwise we can't Find Next
            ResetSelectionOnlyFlag();
            // For similar reasons, ensure the Find and Replace buttons are enabled
            m_find->Enable(true);
            m_replace->Enable(true);
            event.Skip(false);
            return;
        }
    }
    event.Skip();
}

void FindReplaceDialog::ConnectEvents()
{
    // Connect buttons
    m_find->Connect(
        wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(FindReplaceDialog::OnClick), NULL, this);
    m_replace->Connect(
        wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(FindReplaceDialog::OnClick), NULL, this);
    m_replaceAll->Connect(
        wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(FindReplaceDialog::OnClick), NULL, this);
    m_markAll->Connect(
        wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(FindReplaceDialog::OnClick), NULL, this);
    m_cancel->Connect(
        wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(FindReplaceDialog::OnClick), NULL, this);
    m_clearBookmarks->Connect(
        wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(FindReplaceDialog::OnClick), NULL, this);

    // connect options
    m_matchCase->Connect(
        wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(FindReplaceDialog::OnClick), NULL, this);
    m_matchWholeWord->Connect(
        wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(FindReplaceDialog::OnClick), NULL, this);
    m_regualrExpression->Connect(
        wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(FindReplaceDialog::OnClick), NULL, this);
    m_searchUp->Connect(
        wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(FindReplaceDialog::OnClick), NULL, this);
    m_selectionOnly->Connect(
        wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(FindReplaceDialog::OnClick), NULL, this);
    m_find->Connect(
        wxID_ANY, wxEVT_UPDATE_UI, wxUpdateUIEventHandler(FindReplaceDialog::OnSelectionOnlyUI), NULL, this);
    m_replace->Connect(
        wxID_ANY, wxEVT_UPDATE_UI, wxUpdateUIEventHandler(FindReplaceDialog::OnSelectionOnlyUI), NULL, this);
}

void FindReplaceDialog::SendEvent(wxEventType type)
{
    wxCommandEvent event(type, GetId());
    event.SetEventObject(this);

    if(GetEventOwner() == NULL) {
        GetEventHandler()->ProcessEvent(event);
    } else {
        // If an event owner was provided, pass it the event
        GetEventOwner()->ProcessEvent(event);
    }
}

bool FindReplaceDialog::Show(int kind)
{
    if(IsShown()) {
        if(m_kind == kind) {
            return true;
        }
        // change the dialog
        ShowReplaceControls(true);
        return true;
    }

    if(kind == FIND_DLG) {
        ShowReplaceControls(false);
    } else {
        ShowReplaceControls(true);
    }

    // call Show() here
    bool res = wxDialog::Show();

    // and now call the focus methods
    SetFindReplaceData(m_data, true);
    m_findString->SetSelection(-1, -1); // select all
    m_findString->SetFocus();
    return res;
}

void FindReplaceDialog::ShowReplaceControls(bool show)
{
    // detach the find string & its label from the gridbag sizer
    bool isFindDlg(false);
    isFindDlg = gbSizer->GetItemPosition(sz) == wxGBPosition(1, 0);
    if(show == false) {
        // is this dialog is already a 'Find' dialog?
        if(isFindDlg) {
            return;
        }

        // remove 'Replace' dialog items
        gbSizer->Detach(m_replaceWithLabel);
        gbSizer->Detach(m_replaceString);

        // reposition the options static sizer
        gbSizer->Detach(sz);
        gbSizer->Add(sz, wxGBPosition(1, 0), wxGBSpan(1, 2), wxALL | wxEXPAND, 5);

    } else {
        // is this dialog is already a 'Replace' dialog?
        if(!isFindDlg) {
            return;
        }

        // remmove the 'Options' item frmo pos 1,0
        gbSizer->Detach(sz);
        gbSizer->Add(m_replaceWithLabel, wxGBPosition(1, 0), wxDefaultSpan, wxALL | wxEXPAND, 5);
        gbSizer->Add(m_replaceString, wxGBPosition(1, 1), wxDefaultSpan, wxALL | wxEXPAND, 5);
        gbSizer->Add(sz, wxGBPosition(2, 0), wxGBSpan(1, 2), wxALL | wxEXPAND, 5);
    }

    wxString label = show ? wxT("Replace") : wxT("Find");
    m_replace->Show(show);
    m_replaceAll->Show(show);
    m_replaceString->Show(show);
    m_replacementsMsg->Show(show);
    m_replaceWithLabel->Show(show);
    m_selectionOnly->Show(show);

    SetLabel(label);
    this->Fit();
    GetSizer()->Layout();
}

void FindReplaceDialog::SetReplacementsMessage(enum frd_showzero showzero /*= frd_showzeros*/)
{
    if(showzero == frd_dontshowzeros &&
       GetReplacedCount() == 0) { // Don't show a count of 0 before any replacement has started
        m_replacementsMsg->SetLabel(wxString::Format(wxT("Replacements:")));
    } else {
        m_replacementsMsg->SetLabel(wxString::Format(wxT("Replacements: %u"), GetReplacedCount()));
    }
}

void FindReplaceDialog::ResetSelectionOnlyFlag()
{
    size_t flags = GetData().GetFlags();
    GetData().SetFlags(flags & ~(wxFRD_SELECTIONONLY));
}

void FindReplaceDialog::OnSelectionOnlyUI(wxUpdateUIEvent& event)
{
    event.Enable(m_selectionOnly->IsChecked() == false);
}

//---------------------------------------------------------------
// FindReplaceData
//---------------------------------------------------------------

void FindReplaceData::SetReplaceString(const wxString& str)
{

    int where = m_replaceString.Index(str);
    if(where != wxNOT_FOUND) {
        m_replaceString.RemoveAt(where);
    }
    m_replaceString.Insert(str, 0);

    long max_value = clConfig::Get().Read(kConfigMaxItemsInFindReplaceDialog, 15);
    TruncateArray(m_replaceString, (size_t)max_value);
}

void FindReplaceData::SetFindString(const wxString& str)
{
    int where = m_findString.Index(str);
    if(where != wxNOT_FOUND) {
        m_findString.RemoveAt(where);
    }
    m_findString.Insert(str, 0);

    long max_value = clConfig::Get().Read(kConfigMaxItemsInFindReplaceDialog, 15);
    TruncateArray(m_findString, (size_t)max_value);
}

wxString FindReplaceData::GetReplaceString() const
{
    if(m_replaceString.IsEmpty()) {
        return wxEmptyString;
    } else {
        return m_replaceString.Item(0);
    }
}

wxString FindReplaceData::GetFindString() const
{
    if(m_findString.IsEmpty()) {
        return wxEmptyString;

    } else {
        return m_findString.Item(0);
    }
}

void FindReplaceData::TruncateArray(wxArrayString& arr, size_t maxSize)
{
    while(arr.GetCount() > maxSize && arr.GetCount() > 0) {
        arr.RemoveAt(arr.GetCount() - 1);
    }
}
void FindReplaceData::FromJSON(const JSONElement& json)
{
    m_findString = json.namedObject("m_findString").toArrayString();
    m_replaceString = json.namedObject("m_replaceString").toArrayString();
    m_flags = json.namedObject("m_flags").toSize_t(m_flags);
    
    if(json.hasNamedObject("m_lookIn")) {
        m_searchPaths = json.namedObject("m_lookIn").toArrayString();
    } else {
        m_searchPaths.Add(SEARCH_IN_WORKSPACE);
    }
    
    m_encoding = json.namedObject("m_encoding").toString(m_encoding);
    m_fileMask = json.namedObject("m_fileMask").toArrayString();
    m_selectedMask = json.namedObject("m_selectedMask").toString(m_selectedMask);

    long max_value = clConfig::Get().Read(kConfigMaxItemsInFindReplaceDialog, 15);

    TruncateArray(m_searchPaths, (size_t)max_value);
    TruncateArray(m_replaceString, (size_t)max_value);
    TruncateArray(m_findString, (size_t)max_value);

    if(m_fileMask.IsEmpty()) {
        m_fileMask.Add("*.c;*.cpp;*.cxx;*.cc;*.h;*.hpp;*.inc;*.mm;*.m;*.xrc");
        m_selectedMask = m_fileMask.Item(0);
    }
}

JSONElement FindReplaceData::ToJSON() const
{
    JSONElement element = JSONElement::createObject(GetName());
    element.addProperty("m_findString", m_findString);
    element.addProperty("m_replaceString", m_replaceString);
    element.addProperty("m_flags", m_flags);
    element.addProperty("m_lookIn", m_searchPaths);
    element.addProperty("m_encoding", m_encoding);
    element.addProperty("m_fileMask", m_fileMask);
    element.addProperty("m_selectedMask", m_selectedMask);
    return element;
}

wxArrayString FindReplaceData::GetFindStringArr() const
{
    wxArrayString findArr;
    for(size_t i = 0; i < m_findString.GetCount(); ++i) {
        if((findArr.Index(m_findString.Item(i)) == wxNOT_FOUND) && !m_findString.Item(i).IsEmpty()) {
            findArr.Add(m_findString.Item(i));
        }
    }
    return findArr;
}

wxArrayString FindReplaceData::GetReplaceStringArr() const
{
    wxArrayString replaceArr;
    for(size_t i = 0; i < m_replaceString.GetCount(); ++i) {
        if((replaceArr.Index(m_replaceString.Item(i)) == wxNOT_FOUND) && !m_replaceString.Item(i).IsEmpty()) {
            replaceArr.Add(m_replaceString.Item(i));
        }
    }
    return replaceArr;
}

FindReplaceData::FindReplaceData()
    : clConfigItem("FindReplaceData")
    , m_flags(wxFRD_SEPARATETAB_DISPLAY | wxFRD_MATCHCASE | wxFRD_MATCHWHOLEWORD)
    , m_selectedMask("*.c;*.cpp;*.cxx;*.cc;*.h;*.hpp;*.inc;*.mm;*.m;*.xrc") // Default file mask
{
    m_searchPaths.Add(SEARCH_IN_WORKSPACE);
    m_fileMask.Add("*.c;*.cpp;*.cxx;*.cc;*.h;*.hpp;*.inc;*.mm;*.m;*.xrc");
}

void FindReplaceData::SetSearchPaths(const wxArrayString& searchPaths)
{
    // filter duplicate items
    wxStringSet_t paths;
    paths.insert(searchPaths.begin(), searchPaths.end());
    m_searchPaths.clear();
    std::for_each(paths.begin(), paths.end(), [&](const wxString& path) { m_searchPaths.Add(path); });
}