Code::Blocks  SVN r11506
findreplacedlg.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 11349 $
6  * $Id: findreplacedlg.cpp 11349 2018-03-27 21:59:55Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/findreplacedlg.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 
12 #include "findreplacedlg.h"
13 
14 #include <algorithm>
15 
16 #ifndef CB_PRECOMP
17  #include <wx/button.h>
18  #include <wx/checkbox.h>
19  #include <wx/choice.h>
20  #include <wx/combobox.h>
21  #include <wx/intl.h>
22  #include <wx/notebook.h>
23  #include <wx/radiobox.h>
24  #include <wx/sizer.h>
25  #include <wx/textctrl.h>
26  #include <wx/xrc/xmlres.h>
27 
28  #include "cbproject.h"
29  #include "configmanager.h"
30  #include "editormanager.h"
31  #include "globals.h"
32  #include "projectmanager.h"
33 #endif
34 
36 
37 #define CONF_GROUP _T("/replace_options")
38 
39 const int maxTargetCount = 100;
40 
41 //On wxGTK changing the focus of widgets inside the notebook page change event doesn't work
42 //so we create this custom event (and associated handler) to do the focus change after
43 //the notebook page change is complete
44 DEFINE_EVENT_TYPE(wxDEFERRED_FOCUS_EVENT)
45 
46 BEGIN_EVENT_TABLE(FindReplaceDlg, wxScrollingDialog)
47  EVT_ACTIVATE( FindReplaceDlg::OnActivate)
48  EVT_CHECKBOX(XRCID("chkRegEx1"), FindReplaceDlg::OnRegEx)
49 
50  // Special events for Find/Replace
51  EVT_CHECKBOX(XRCID("chkMultiLine1"), FindReplaceDlg::OnMultiChange)
52  EVT_CHECKBOX(XRCID("chkMultiLine2"), FindReplaceDlg::OnMultiChange)
53  EVT_CHECKBOX(XRCID("chkLimitTo1"), FindReplaceDlg::OnLimitToChange)
54  EVT_CHECKBOX(XRCID("chkLimitTo2"), FindReplaceDlg::OnLimitToChange)
55  EVT_RADIOBOX(XRCID("rbScope2"), FindReplaceDlg::OnScopeChange)
56  EVT_BUTTON(XRCID("btnBrowsePath"), FindReplaceDlg::OnBrowsePath)
57  EVT_BUTTON(XRCID("btnSelectTarget"), FindReplaceDlg::OnSelectTarget)
58  EVT_CHOICE(XRCID("chProject"), FindReplaceDlg::OnSearchProject)
59 
60  EVT_COMMAND(wxID_ANY, wxDEFERRED_FOCUS_EVENT, FindReplaceDlg::OnDeferredFocus)
61 END_EVENT_TABLE()
62 
63 FindReplaceDlg::FindReplaceDlg(wxWindow* parent, const wxString& initial, bool hasSelection,
64  bool findMode, bool findReplaceInFilesOnly, bool findReplaceInFilesActive)
65  : FindReplaceBase(parent, initial, hasSelection),
66  m_findReplaceInFilesActive(findReplaceInFilesActive),
67  m_findMode(findMode)
68 {
69  wxXmlResource::Get()->LoadObject(this, parent, _T("dlgFindReplace"),_T("wxScrollingDialog"));
70  ConfigManager* cfg = Manager::Get()->GetConfigManager(_T("editor"));
71 
72 
73  // load last searches
74  FillComboWithLastValues(XRCCTRL(*this, "cmbFind1", wxComboBox), CONF_GROUP _T("/last"));
75  FillComboWithLastValues(XRCCTRL(*this, "cmbReplace1", wxComboBox), CONF_GROUP _T("/lastReplace"));
76  FillComboWithLastValues(XRCCTRL(*this, "cmbFind2", wxComboBox), CONF_GROUP _T("/last"));
77  FillComboWithLastValues(XRCCTRL(*this, "cmbReplace2", wxComboBox), CONF_GROUP _T("/lastReplace"));
78 
79  // load last multiline searches
80  XRCCTRL(*this, "txtMultiLineFind1", wxTextCtrl)->SetValue(cfg->Read(CONF_GROUP _T("/lastMultiLineFind"), _T("")));
81  XRCCTRL(*this, "txtMultiLineReplace1", wxTextCtrl)->SetValue(cfg->Read(CONF_GROUP _T("/lastMultiLineReplace"), _T("")));
82  XRCCTRL(*this, "txtMultiLineFind2", wxTextCtrl)->SetValue(cfg->Read(CONF_GROUP _T("/lastMultiLineFind"), _T("")));
83  XRCCTRL(*this, "txtMultiLineReplace2", wxTextCtrl)->SetValue(cfg->Read(CONF_GROUP _T("/lastMultiLineReplace"), _T("")));
84 
85  // replace options
86  XRCCTRL(*this, "cmbFind1", wxComboBox)->SetValue(initial);
87 
88  bool flgWholeWord = cfg->ReadBool(CONF_GROUP _T("/match_word"), false);
89  bool flgStartWord = cfg->ReadBool(CONF_GROUP _T("/start_word"), false);
90  bool flgStartFile = cfg->ReadBool(CONF_GROUP _T("/start_file"), false);
91  XRCCTRL(*this, "chkLimitTo1", wxCheckBox)->SetValue(flgWholeWord || flgStartWord || flgStartFile);
92  XRCCTRL(*this, "rbLimitTo1", wxRadioBox)->Enable(flgWholeWord || flgStartWord || flgStartFile);
93  XRCCTRL(*this, "rbLimitTo1", wxRadioBox)->SetSelection(flgStartFile ? 2 : (flgStartWord ? 1 : 0));
94 
95  XRCCTRL(*this, "chkMatchCase1", wxCheckBox)->SetValue(cfg->ReadBool(CONF_GROUP _T("/match_case"), false));
96  XRCCTRL(*this, "chkRegEx1", wxCheckBox)->SetValue(cfg->ReadBool(CONF_GROUP _T("/regex"), false));
97  XRCCTRL(*this, "rbDirection", wxRadioBox)->SetSelection(cfg->ReadInt(CONF_GROUP _T("/direction"), 1));
98  XRCCTRL(*this, "rbDirection", wxRadioBox)->Enable(!XRCCTRL(*this, "chkRegEx1", wxCheckBox)->GetValue()); // if regex, only forward searches
99  XRCCTRL(*this, "rbOrigin", wxRadioBox)->SetSelection(cfg->ReadInt(CONF_GROUP _T("/origin"), 0));
100  XRCCTRL(*this, "rbScope1", wxRadioBox)->SetSelection(hasSelection);
101  // special key, uses same config for both find & replace options
102  XRCCTRL(*this, "chkAutoWrapSearch", wxCheckBox)->SetValue(cfg->ReadBool(_T("/find_options/auto_wrap_search"), true));
103 
104  // replace in files options
105  flgWholeWord = cfg->ReadBool(CONF_GROUP _T("/match_word2"), false);
106  flgStartWord = cfg->ReadBool(CONF_GROUP _T("/start_word2"), false);
107  flgStartFile = cfg->ReadBool(CONF_GROUP _T("/start_file2"), false);
108  XRCCTRL(*this, "chkLimitTo2", wxCheckBox)->SetValue(flgWholeWord || flgStartWord || flgStartFile);
109  XRCCTRL(*this, "rbLimitTo2", wxRadioBox)->Enable(flgWholeWord || flgStartWord || flgStartFile);
110  XRCCTRL(*this, "rbLimitTo2", wxRadioBox)->SetSelection(flgStartFile ? 2 : (flgStartWord ? 1 : 0));
111 
112  XRCCTRL(*this, "cmbFind2", wxComboBox)->SetValue(initial);
113  XRCCTRL(*this, "chkMatchCase2", wxCheckBox)->SetValue(cfg->ReadBool(CONF_GROUP _T("/match_case2"), false));
114  XRCCTRL(*this, "chkRegEx2", wxCheckBox)->SetValue(cfg->ReadBool(CONF_GROUP _T("/regex2"), false));
115  XRCCTRL(*this, "rbScope2", wxRadioBox)->SetSelection(cfg->ReadInt(CONF_GROUP _T("/scope2"), 0));
116 
117  XRCCTRL(*this, "chkMultiLine1", wxCheckBox)->SetValue(false);
118  XRCCTRL(*this, "chkFixEOLs1", wxCheckBox)->SetValue(cfg->ReadBool(CONF_GROUP _T("/fix_eols1"), false));
119  XRCCTRL(*this, "chkFixEOLs1", wxCheckBox)->Enable(XRCCTRL(*this, "chkMultiLine1", wxCheckBox)->GetValue());
120 
121  XRCCTRL(*this, "chkMultiLine2", wxCheckBox)->SetValue(false);
122  XRCCTRL(*this, "chkFixEOLs2", wxCheckBox)->SetValue(cfg->ReadBool(CONF_GROUP _T("/fix_eols2"), false));
123  XRCCTRL(*this, "chkFixEOLs2", wxCheckBox)->Enable(XRCCTRL(*this, "chkMultiLine2", wxCheckBox)->GetValue());
124  XRCCTRL(*this, "chkDelOldSearchRes2", wxCheckBox)->SetValue(cfg->ReadBool(CONF_GROUP _T("/delete_old_searches2"), true));
125 
126  wxSize szReplaceMulti = XRCCTRL(*this, "nbReplaceMulti", wxPanel)->GetEffectiveMinSize();
127  XRCCTRL(*this, "nbReplaceSingle", wxPanel)->SetMinSize(szReplaceMulti);
128  XRCCTRL(*this, "nbReplaceMulti", wxPanel)->SetMinSize(szReplaceMulti);
129 
130  wxSize szReplaceInFilesMulti = XRCCTRL(*this, "nbReplaceInFilesMulti", wxPanel)->GetEffectiveMinSize();
131  XRCCTRL(*this, "nbReplaceInFilesSingle", wxPanel)->SetMinSize(szReplaceInFilesMulti);
132  XRCCTRL(*this, "nbReplaceInFilesMulti", wxPanel)->SetMinSize(szReplaceInFilesMulti);
133 
134  wxSize szFindMulti = XRCCTRL(*this, "nbFindMulti", wxPanel)->GetEffectiveMinSize();
135  XRCCTRL(*this, "nbFindSingle", wxPanel)->SetMinSize(szFindMulti);
136  XRCCTRL(*this, "nbFindMulti", wxPanel)->SetMinSize(szFindMulti);
137 
138  wxSize szFindInFilesMulti = XRCCTRL(*this, "nbFindInFilesMulti", wxPanel)->GetEffectiveMinSize();
139  XRCCTRL(*this, "nbFindInFilesSingle", wxPanel)->SetMinSize(szFindInFilesMulti);
140  XRCCTRL(*this, "nbFindInFilesMulti", wxPanel)->SetMinSize(szFindInFilesMulti);
141 
142  wxSize szSearchPath = XRCCTRL(*this, "pnSearchPath", wxPanel)->GetEffectiveMinSize();
143  XRCCTRL(*this, "pnSearchProject", wxPanel)->SetMinSize(szSearchPath);
144  XRCCTRL(*this, "pnSearchPath", wxPanel)->SetMinSize(szSearchPath);
145 
147  ProjectsArray *pa = pm->GetProjects();
148  cbProject *active_project = Manager::Get()->GetProjectManager()->GetActiveProject();
149 
150  // load search path options
151  XRCCTRL(*this, "txtSearchPath", wxTextCtrl)->SetValue(cfg->Read(CONF_GROUP _T("/search_path"),
152  (active_project ? active_project->GetBasePath() : wxT(""))));
153  wxComboBox* cmbSearchMask = XRCCTRL(*this, "cmbSearchMask", wxComboBox);
154  if (cfg->Exists(CONF_GROUP _T("/search_mask")))
155  {
156  // Migrate from previous config setting of "search_mask" string (since it used to be a textbox)
157  // to new config setting of "search_masks" array for the combobox
158  cmbSearchMask->Append(cfg->Read(CONF_GROUP _T("/search_mask")));
159  cfg->UnSet(CONF_GROUP _T("/search_mask"));
160  }
161  else
162  FillComboWithLastValues(cmbSearchMask, CONF_GROUP _T("/search_masks"));
163 
164  if (cmbSearchMask->GetCount() > 0)
165  XRCCTRL(*this, "cmbSearchMask", wxComboBox)->SetSelection(0);
166 
167  XRCCTRL(*this, "chkSearchRecursively", wxCheckBox)->SetValue(cfg->ReadBool(CONF_GROUP _T("/search_recursive"), false));
168  XRCCTRL(*this, "chkSearchHidden", wxCheckBox)->SetValue(cfg->ReadBool(CONF_GROUP _T("/search_hidden"), false));
169 
170  wxChoice *chProject = XRCCTRL(*this, "chProject", wxChoice);
171  wxChoice *chTarget = XRCCTRL(*this, "chTarget", wxChoice);
172  for(unsigned int i=0;i<pa->size();++i)
173  {
174  chProject->AppendString((*pa)[i]->GetTitle());
175  if ((*pa)[i] == active_project)
176  {
177  chProject->SetSelection(i);
178  chTarget->Clear();
179  chTarget->AppendString(_("All project files"));
180 
181  const bool selectScopeAll = cfg->ReadBool(CONF_GROUP _T("/target_scope_all"), true);
182 
183  const int targetCount = active_project->GetBuildTargetsCount();
184  if (targetCount < maxTargetCount)
185  {
186  wxArrayString targetNames;
187  for(int j=0;j<targetCount && j<maxTargetCount;++j)
188  targetNames.push_back(active_project->GetBuildTarget(j)->GetTitle());
189  chTarget->Append(targetNames);
190  const int targIdx = chTarget->FindString(active_project->GetActiveBuildTarget(), true);
191  chTarget->SetSelection((selectScopeAll || targIdx < 0) ? 0 : targIdx );
192  }
193  else
194  {
195  if (selectScopeAll)
196  chTarget->SetSelection(0);
197  else
198  {
199  chTarget->Append(active_project->GetActiveBuildTarget());
200  chTarget->SetSelection(1);
201  }
202  chTarget->Enable(false);
203  }
204  }
205  }
206 
207  wxRadioBox* rbScope = XRCCTRL(*this, "rbScope2", wxRadioBox);
209  bool filesOpen = false;
210  for (int i = 0; i < edMgr->GetEditorsCount(); ++i)
211  {
212  if (edMgr->GetBuiltinEditor(i))
213  {
214  filesOpen = true;
215  break;
216  }
217  }
218  if (!filesOpen)
219  {
220  if (rbScope->GetSelection() == 0)
221  rbScope->SetSelection(1);
222  rbScope->Enable(0, false);
223  }
224 
225  if (pa->IsEmpty())
226  {
227  if (rbScope->GetSelection() == 1 || rbScope->GetSelection() == 2)
228  {
229  if (rbScope->IsItemEnabled(0))
230  rbScope->SetSelection(0);
231  else
232  rbScope->SetSelection(3);
233  }
234  rbScope->Enable(1, false);
235  rbScope->Enable(2, false);
236  }
237 
238  switch (rbScope->GetSelection())
239  {
240  case 1:
241  XRCCTRL(*this, "pnSearchPath", wxPanel)->Hide();
242  XRCCTRL(*this, "pnSearchPath", wxPanel)->Disable();
243  XRCCTRL(*this, "pnSearchProject", wxPanel)->Show();
244  break;
245  case 3:
246  XRCCTRL(*this, "pnSearchPath", wxPanel)->Show();
247  XRCCTRL(*this, "pnSearchPath", wxPanel)->Enable();
248  XRCCTRL(*this, "pnSearchProject", wxPanel)->Hide();
249  break;
250  default:
251  XRCCTRL(*this, "pnSearchPath", wxPanel)->Show();
252  XRCCTRL(*this, "pnSearchPath", wxPanel)->Disable();
253  XRCCTRL(*this, "pnSearchProject", wxPanel)->Hide();
254  break;
255  }
256  (XRCCTRL(*this, "nbReplace", wxNotebook)->GetPage(1))->Layout();
257 
258  if (findMode)
259  {
260  SetTitle(_T("Find"));
261  XRCCTRL(*this, "nbReplaceSingle", wxPanel)->Hide();
262  XRCCTRL(*this, "nbReplaceInFilesSingle", wxPanel)->Hide();
263  XRCCTRL(*this, "nbReplace", wxNotebook)->SetPageText(0,_T("Find"));
264  XRCCTRL(*this, "nbReplace", wxNotebook)->SetPageText(1,_T("Find in files"));
265  XRCCTRL(*this, "wxID_OK", wxButton)->SetLabel(_T("&Find"));
266  XRCCTRL(*this, "chkFixEOLs1", wxCheckBox)->Hide();
267  XRCCTRL(*this, "chkFixEOLs2", wxCheckBox)->Hide();
268  XRCCTRL(*this, "chkDelOldSearchRes2", wxCheckBox)->Show();
269  }
270 
271  m_findPage = nullptr;
272  if (findReplaceInFilesOnly)
273  {
274  // Remove, but don't destroy the Find/Replace page until this dialog is destroyed.
275  XRCCTRL(*this, "nbReplace", wxNotebook)->SetSelection(1);
276  m_findPage = (XRCCTRL(*this, "nbReplace", wxNotebook)->GetPage(0)); // no active editor, so only replace-in-files
277  (XRCCTRL(*this, "nbReplace", wxNotebook)->RemovePage(0)); // no active editor, so only replace-in-files
278  XRCCTRL(*this, "cmbFind2", wxComboBox)->SetFocus();
279  m_findReplaceInFilesActive = true;
280  }
281  else if (m_findReplaceInFilesActive)
282  {
283  XRCCTRL(*this, "nbReplace", wxNotebook)->SetSelection(1); // Search->Replace in Files was selected
284  XRCCTRL(*this, "cmbFind2", wxComboBox)->SetFocus();
285  }
286  else
287  XRCCTRL(*this, "cmbFind1", wxComboBox)->SetFocus();
288 
289  GetSizer()->SetSizeHints(this);
290 
291  // NOTE (jens#1#): Dynamically connect these events, to avoid asserts in debug-mode
292  Connect(XRCID("nbReplace"), wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, wxNotebookEventHandler(FindReplaceDlg::OnReplaceChange));
293 }
294 
296 {
297  ConfigManager* cfg = Manager::Get()->GetConfigManager(_T("editor"));
298 
299  // save last searches (up to 10)
300  if ( IsFindInFiles() )
301  {
302  SaveComboValues(XRCCTRL(*this, "cmbFind2", wxComboBox), CONF_GROUP _T("/last"));
303  SaveComboValues(XRCCTRL(*this, "cmbReplace2", wxComboBox), CONF_GROUP _T("/lastReplace"));
304 
305  // Save last multi-line search & replace
306  cfg->Write(CONF_GROUP _T("/lastMultiLineFind"), XRCCTRL(*this, "txtMultiLineFind2", wxTextCtrl)->GetValue());
307  cfg->Write(CONF_GROUP _T("/lastMultiLineReplace"), XRCCTRL(*this, "txtMultiLineReplace2", wxTextCtrl)->GetValue());
308  }
309  else
310  {
311  SaveComboValues(XRCCTRL(*this, "cmbFind1", wxComboBox), CONF_GROUP _T("/last"));
312  SaveComboValues(XRCCTRL(*this, "cmbReplace1", wxComboBox), CONF_GROUP _T("/lastReplace"));
313 
314  // Save last multi-line search & replace
315  cfg->Write(CONF_GROUP _T("/lastMultiLineFind"), XRCCTRL(*this, "txtMultiLineFind1", wxTextCtrl)->GetValue());
316  cfg->Write(CONF_GROUP _T("/lastMultiLineReplace"), XRCCTRL(*this, "txtMultiLineReplace1", wxTextCtrl)->GetValue());
317  }
318 
320  {
321  // find(replace) options
322  cfg->Write(CONF_GROUP _T("/fix_eols1"), XRCCTRL(*this, "chkFixEOLs1", wxCheckBox)->GetValue());
323  cfg->Write(CONF_GROUP _T("/match_case"), XRCCTRL(*this, "chkMatchCase1", wxCheckBox)->GetValue());
324  cfg->Write(CONF_GROUP _T("/regex"), XRCCTRL(*this, "chkRegEx1", wxCheckBox)->GetValue());
325  cfg->Write(CONF_GROUP _T("/direction"), XRCCTRL(*this, "rbDirection", wxRadioBox)->GetSelection());
326  cfg->Write(CONF_GROUP _T("/origin"), XRCCTRL(*this, "rbOrigin", wxRadioBox)->GetSelection());
327 
328  bool flgLimitTo = XRCCTRL(*this, "chkLimitTo1", wxCheckBox)->GetValue();
329  int valLimitTo = XRCCTRL(*this, "rbLimitTo1", wxRadioBox)->GetSelection();
330 
331  cfg->Write(CONF_GROUP _T("/match_word"), flgLimitTo && valLimitTo == 0);
332  cfg->Write(CONF_GROUP _T("/start_word"), flgLimitTo && valLimitTo == 1);
333  cfg->Write(CONF_GROUP _T("/start_file"), flgLimitTo && valLimitTo == 2);
334 
335  // special key, uses same config for both find & replace options
336  cfg->Write(_T("/find_options/auto_wrap_search"), XRCCTRL(*this, "chkAutoWrapSearch", wxCheckBox)->GetValue());
337  }
338 
339  // find(replace) in files options
340  bool flgLimitTo = XRCCTRL(*this, "chkLimitTo2", wxCheckBox)->GetValue();
341  int valLimitTo = XRCCTRL(*this, "rbLimitTo2", wxRadioBox)->GetSelection();
342 
343  cfg->Write(CONF_GROUP _T("/match_word2"), flgLimitTo && valLimitTo == 0);
344  cfg->Write(CONF_GROUP _T("/start_word2"), flgLimitTo && valLimitTo == 1);
345  cfg->Write(CONF_GROUP _T("/start_file2"), flgLimitTo && valLimitTo == 2);
346 
347  cfg->Write(CONF_GROUP _T("/fix_eols2"), XRCCTRL(*this, "chkFixEOLs2", wxCheckBox)->GetValue());
348  cfg->Write(CONF_GROUP _T("/match_case2"), XRCCTRL(*this, "chkMatchCase2", wxCheckBox)->GetValue());
349  cfg->Write(CONF_GROUP _T("/regex2"), XRCCTRL(*this, "chkRegEx2", wxCheckBox)->GetValue());
350  cfg->Write(CONF_GROUP _T("/scope2"), XRCCTRL(*this, "rbScope2", wxRadioBox)->GetSelection());
351  cfg->Write(CONF_GROUP _T("/delete_old_searches2"), XRCCTRL(*this, "chkDelOldSearchRes2", wxCheckBox)->GetValue());
352 
353  cfg->Write(CONF_GROUP _T("/search_path"), XRCCTRL(*this, "txtSearchPath", wxTextCtrl)->GetValue());
354  SaveComboValues(XRCCTRL(*this, "cmbSearchMask", wxComboBox), CONF_GROUP _T("/search_masks"));
355  cfg->Write(CONF_GROUP _T("/search_recursive"), XRCCTRL(*this, "chkSearchRecursively", wxCheckBox)->GetValue());
356  cfg->Write(CONF_GROUP _T("/search_hidden"), XRCCTRL(*this, "chkSearchHidden", wxCheckBox)->GetValue());
357  cfg->Write(CONF_GROUP _T("/target_scope_all"),(XRCCTRL(*this, "chTarget", wxChoice)->GetSelection() == 0));
358 
359  if (m_findPage!=nullptr)
360  m_findPage->Destroy();
361 
362  Disconnect(XRCID("nbReplace"), wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, wxNotebookEventHandler(FindReplaceDlg::OnReplaceChange));
363 }
364 
366 {
367  if ( IsMultiLine() )
368  {
369  if ( IsFindInFiles() )
370  return XRCCTRL(*this, "txtMultiLineFind2", wxTextCtrl)->GetValue();
371  return XRCCTRL(*this, "txtMultiLineFind1", wxTextCtrl)->GetValue();
372  }
373  if ( IsFindInFiles() )
374  return XRCCTRL(*this, "cmbFind2", wxComboBox)->GetValue();
375 
376  return XRCCTRL(*this, "cmbFind1", wxComboBox)->GetValue();
377 }
378 
380 {
381  if ( IsMultiLine() )
382  {
383  wxString tmpString;
384 
385  if ( IsFindInFiles() )
386  return XRCCTRL(*this, "txtMultiLineReplace2", wxTextCtrl)->GetValue();
387  return XRCCTRL(*this, "txtMultiLineReplace1", wxTextCtrl)->GetValue();
388  }
389  if ( IsFindInFiles() )
390  return XRCCTRL(*this, "cmbReplace2", wxComboBox)->GetValue();
391 
392  return XRCCTRL(*this, "cmbReplace1", wxComboBox)->GetValue();
393 }
394 
396 {
398 }
399 
401 {
402  if ( IsFindInFiles() )
403  return XRCCTRL(*this, "chkDelOldSearchRes2", wxCheckBox)->GetValue();
404 
405  return true; // checkbox doesn't exist in Find dialog
406 }
407 
409 {
410  return true; // checkbox doesn't exist
411 }
412 
414 {
415  if ( IsFindInFiles() )
416  {
417  bool flgLimitTo = XRCCTRL(*this, "chkLimitTo2", wxCheckBox)->GetValue();
418  return flgLimitTo && XRCCTRL(*this, "rbLimitTo2", wxRadioBox)->GetSelection() == 0;
419  }
420 
421  bool flgLimitTo = XRCCTRL(*this, "chkLimitTo1", wxCheckBox)->GetValue();
422  return flgLimitTo && XRCCTRL(*this, "rbLimitTo1", wxRadioBox)->GetSelection() == 0;
423 }
424 
426 {
427  if ( IsFindInFiles() )
428  {
429  bool flgLimitTo = XRCCTRL(*this, "chkLimitTo2", wxCheckBox)->GetValue();
430  return flgLimitTo && XRCCTRL(*this, "rbLimitTo2", wxRadioBox)->GetSelection() == 1;
431  }
432  bool flgLimitTo = XRCCTRL(*this, "chkLimitTo1", wxCheckBox)->GetValue();
433  return flgLimitTo && XRCCTRL(*this, "rbLimitTo1", wxRadioBox)->GetSelection() == 1;
434 }
435 
437 {
438  if ( IsFindInFiles() )
439  return XRCCTRL(*this, "chkMatchCase2", wxCheckBox)->GetValue();
440  return XRCCTRL(*this, "chkMatchCase1", wxCheckBox)->GetValue();
441 }
442 
444 {
445  if ( IsFindInFiles() )
446  return XRCCTRL(*this, "chkRegEx2", wxCheckBox)->GetValue();
447  return XRCCTRL(*this, "chkRegEx1", wxCheckBox)->GetValue();
448 }
449 
451 {
452  if ( IsFindInFiles() )
453  return false; // not for replace in files
454  return XRCCTRL(*this, "chkAutoWrapSearch", wxCheckBox)->GetValue();
455 }
456 
458 {
459  return false; // not for replace
460 }
461 
463 {
464  if ( IsFindInFiles() )
465  {
466  bool flgLimitTo = XRCCTRL(*this, "chkLimitTo2", wxCheckBox)->GetValue();
467  return flgLimitTo && XRCCTRL(*this, "rbLimitTo2", wxRadioBox)->GetSelection() == 2;
468  }
469  bool flgLimitTo = XRCCTRL(*this, "chkLimitTo1", wxCheckBox)->GetValue();
470  return flgLimitTo && XRCCTRL(*this, "rbLimitTo1", wxRadioBox)->GetSelection() == 2;
471 }
472 
474 {
475  if ( IsFindInFiles() )
476  return XRCCTRL(*this, "chkMultiLine2", wxCheckBox)->GetValue();
477  return XRCCTRL(*this, "chkMultiLine1", wxCheckBox)->GetValue();
478 }
479 
481 {
482  if ( IsFindInFiles() )
483  return XRCCTRL(*this, "chkFixEOLs2", wxCheckBox)->GetValue();
484  return XRCCTRL(*this, "chkFixEOLs1", wxCheckBox)->GetValue();
485 }
486 
488 {
489  if ( IsFindInFiles() )
490  return 1;
491  return XRCCTRL(*this, "rbDirection", wxRadioBox)->GetSelection();
492 }
493 
495 {
496  if ( IsFindInFiles() )
497  return 1;
498  return XRCCTRL(*this, "rbOrigin", wxRadioBox)->GetSelection();
499 }
500 
502 {
503  if ( IsFindInFiles() )
504  return XRCCTRL(*this, "rbScope2", wxRadioBox)->GetSelection();
505  return XRCCTRL(*this, "rbScope1", wxRadioBox)->GetSelection();
506 }
507 
509 {
510  return XRCCTRL(*this, "chkSearchRecursively", wxCheckBox)->IsChecked();
511 }
512 
514 {
515  return XRCCTRL(*this, "chkSearchHidden", wxCheckBox)->IsChecked();
516 }
517 
519 {
520  return XRCCTRL(*this, "txtSearchPath", wxTextCtrl)->GetValue();
521 }
522 
524 {
525  return XRCCTRL(*this, "cmbSearchMask", wxComboBox)->GetValue();
526 }
527 
529 {
530  return XRCCTRL(*this, "chProject", wxChoice)->GetSelection();
531 }
532 
534 {
535  cbProject* activeProject = Manager::Get()->GetProjectManager()->GetActiveProject();
536  if (!activeProject)
537  return -1;
538 
539  wxChoice *control = XRCCTRL(*this, "chTarget", wxChoice);
540 
541  const wxString selectedString = control->GetString(control->GetSelection());
542 
543  const int targetCount = activeProject->GetBuildTargetsCount();
544  for(int ii = 0; ii < targetCount; ++ii)
545  {
546  if (activeProject->GetBuildTarget(ii)->GetTitle() == selectedString)
547  return ii;
548  }
549 
550  return -1;
551 }
552 
554 {
555  if ( IsFindInFiles() )
556  return XRCCTRL(*this, "chkMultiLine2", wxCheckBox)->GetValue();
557  return XRCCTRL(*this, "chkMultiLine1", wxCheckBox)->GetValue();
558 }
559 
560 // events
561 
563 {
564  wxRadioBox* rbScope = XRCCTRL(*this, "rbScope2", wxRadioBox);
565  switch (rbScope->GetSelection())
566  {
567  case 1:
568  XRCCTRL(*this, "pnSearchPath", wxPanel)->Hide();
569  XRCCTRL(*this, "pnSearchPath", wxPanel)->Disable();
570  XRCCTRL(*this, "pnSearchProject", wxPanel)->Show();
571  break;
572  case 3:
573  XRCCTRL(*this, "pnSearchPath", wxPanel)->Show();
574  XRCCTRL(*this, "pnSearchPath", wxPanel)->Enable();
575  XRCCTRL(*this, "pnSearchProject", wxPanel)->Hide();
576  break;
577  default:
578  XRCCTRL(*this, "pnSearchPath", wxPanel)->Show();
579  XRCCTRL(*this, "pnSearchPath", wxPanel)->Disable();
580  XRCCTRL(*this, "pnSearchProject", wxPanel)->Hide();
581  break;
582  }
583  if (m_findPage==nullptr)
584  (XRCCTRL(*this, "nbReplace", wxNotebook)->GetPage(1))->Layout();
585  else
586  (XRCCTRL(*this, "nbReplace", wxNotebook)->GetPage(0))->Layout();
587 }
588 
590 {
591  const wxString txtSearchPath = XRCCTRL(*this, "txtSearchPath", wxTextCtrl)->GetValue();
592  const wxString dir = ChooseDirectory(nullptr, _("Select search path"), txtSearchPath);
593  if (!dir.IsEmpty())
594  XRCCTRL(*this, "txtSearchPath", wxTextCtrl)->SetValue(dir);
595 }
596 
598 {
599  wxChoice *chProject = XRCCTRL(*this, "chProject", wxChoice);
600  wxChoice *chTarget = XRCCTRL(*this, "chTarget", wxChoice);
601  int i=chProject->GetSelection();
602  if (i<0)
603  return;
604  cbProject *active_project=(*Manager::Get()->GetProjectManager()->GetProjects())[i];
605  const bool targAll = (chTarget->GetSelection() == 0);
606  chTarget->Clear();
607  chTarget->AppendString(_("All project files"));
608  for(int j=0;j<active_project->GetBuildTargetsCount();++j)
609  chTarget->AppendString(active_project->GetBuildTarget(j)->GetTitle());
610  const int targIdx = chTarget->FindString(active_project->GetActiveBuildTarget(), true);
611  chTarget->SetSelection(targAll || targIdx < 0 ? 0 : targIdx);
612 }
613 
614 void FindReplaceDlg::OnReplaceChange(wxNotebookEvent& event)
615 {
616  // Replace / in files triggered
617  wxComboBox* cmbFind1 = XRCCTRL(*this, "cmbFind1", wxComboBox);
618  wxComboBox* cmbFind2 = XRCCTRL(*this, "cmbFind2", wxComboBox);
619  wxTextCtrl* txtFind1 = XRCCTRL(*this, "txtMultiLineFind1", wxTextCtrl);
620  wxTextCtrl* txtFind2 = XRCCTRL(*this, "txtMultiLineFind2", wxTextCtrl);
621  wxComboBox* cmbReplace1 = XRCCTRL(*this, "cmbReplace1", wxComboBox);
622  wxComboBox* cmbReplace2 = XRCCTRL(*this, "cmbReplace2", wxComboBox);
623 
624  if (txtFind1 && txtFind2 && cmbFind1 && cmbFind2 && cmbReplace1 && cmbReplace2)
625  {
626  if (event.GetSelection() == 0)
627  {
628  // Switched from "Replace in files" to "Replace"
629  txtFind1->SetValue(txtFind2->GetValue());
630  cmbFind1->SetValue(cmbFind2->GetValue());
631  cmbReplace1->SetValue(cmbReplace2->GetValue());
633  }
634  else if (event.GetSelection() == 1)
635  {
636  // Switched from "Replace" to "Replace in files"
637  txtFind2->SetValue(txtFind1->GetValue());
638  cmbFind2->SetValue(cmbFind1->GetValue());
639  cmbReplace2->SetValue(cmbReplace1->GetValue());
640  cmbFind1->SetFocus();
642  }
643  }
644 
645  wxCommandEvent e(wxDEFERRED_FOCUS_EVENT,wxID_ANY);
646  AddPendingEvent(e);
647  event.Skip();
648 }
649 
651 {
652  if ( IsMultiLine() )
653  {
654  wxTextCtrl* tc = ( IsFindInFiles() ? XRCCTRL(*this, "txtMultiLineFind2", wxTextCtrl)
655  : XRCCTRL(*this, "txtMultiLineFind1", wxTextCtrl) );
656  if (tc) tc->SetFocus();
657  }
658  else
659  {
660  wxComboBox* cb = ( IsFindInFiles() ? XRCCTRL(*this, "cmbFind2", wxComboBox)
661  : XRCCTRL(*this, "cmbFind1", wxComboBox) );
662  if (cb) cb->SetFocus();
663  }
664 }
665 
667 {
668  XRCCTRL(*this, "rbDirection", wxRadioBox)->Enable(!XRCCTRL(*this, "chkRegEx1", wxCheckBox)->GetValue());
669 }
670 
672 {
673  if ( IsMultiLine() )
674  {
675  wxTextCtrl* tcp = ( IsFindInFiles() ? XRCCTRL(*this, "txtMultiLineFind2", wxTextCtrl)
676  : XRCCTRL(*this, "txtMultiLineFind1", wxTextCtrl) );
677  if (tcp) tcp->SetFocus();
678  }
679  else
680  {
681  wxComboBox* cbp = ( IsFindInFiles() ? XRCCTRL(*this, "cmbFind2", wxComboBox)
682  : XRCCTRL(*this, "cmbFind1", wxComboBox) );
683  if (cbp) cbp->SetFocus();
684  }
685  event.Skip();
686 }
687 
688 // special events for Replace
689 
691 {
692  // Multi-Line replacements triggered
693  wxComboBox* cmbFind1 = XRCCTRL(*this, "cmbFind1", wxComboBox);
694  wxComboBox* cmbFind2 = XRCCTRL(*this, "cmbFind2", wxComboBox);
695  wxTextCtrl* txtFind1 = XRCCTRL(*this, "txtMultiLineFind1", wxTextCtrl);
696  wxTextCtrl* txtFind2 = XRCCTRL(*this, "txtMultiLineFind2", wxTextCtrl);
697  wxCheckBox* chkMultiLine1 = XRCCTRL(*this, "chkMultiLine1", wxCheckBox);
698  wxCheckBox* chkMultiLine2 = XRCCTRL(*this, "chkMultiLine2", wxCheckBox);
699  wxCheckBox* chkFixEOLs1 = XRCCTRL(*this, "chkFixEOLs1", wxCheckBox);
700  wxCheckBox* chkFixEOLs2 = XRCCTRL(*this, "chkFixEOLs2", wxCheckBox);
701 
702  bool enabledMultiLine = false;
703  wxWindow* ctrlToFocus = nullptr;
704  if (event.GetId() == XRCID("chkMultiLine1"))
705  {
706  enabledMultiLine = chkMultiLine1->GetValue();
707  if (chkMultiLine2) chkMultiLine2->SetValue(enabledMultiLine);
708  ctrlToFocus = enabledMultiLine ? dynamic_cast<wxWindow*>(txtFind1) : dynamic_cast<wxWindow*>(cmbFind1);
709  }
710  else if (event.GetId() == XRCID("chkMultiLine2"))
711  {
712  enabledMultiLine = chkMultiLine2->GetValue();
713  if (chkMultiLine1) chkMultiLine1->SetValue(enabledMultiLine);
714  ctrlToFocus = enabledMultiLine ? dynamic_cast<wxWindow*>(txtFind2) : dynamic_cast<wxWindow*>(cmbFind2);
715  }
716  else
717  return;
718 
719  XRCCTRL(*this, "nbFindSingle", wxPanel)->Show(!enabledMultiLine);
720  XRCCTRL(*this, "nbFindInFilesSingle", wxPanel)->Show(!enabledMultiLine);
721  XRCCTRL(*this, "nbFindMulti", wxPanel)->Show(enabledMultiLine);
722  XRCCTRL(*this, "nbFindInFilesMulti", wxPanel)->Show(enabledMultiLine);
723 
724  if (!m_findMode)
725  {
726  XRCCTRL(*this, "nbReplaceSingle", wxPanel)->Show(!enabledMultiLine);
727  XRCCTRL(*this, "nbReplaceInFilesSingle", wxPanel)->Show(!enabledMultiLine);
728  XRCCTRL(*this, "nbReplaceMulti", wxPanel)->Show(enabledMultiLine);
729  XRCCTRL(*this, "nbReplaceInFilesMulti", wxPanel)->Show(enabledMultiLine);
730  }
731 
732  if (chkFixEOLs1) chkFixEOLs1->Enable(enabledMultiLine);
733  if (chkFixEOLs2) chkFixEOLs2->Enable(enabledMultiLine);
734  if (ctrlToFocus) ctrlToFocus->SetFocus();
735 
736  //After hiding/showing panels, redo the layout in the notebook pages
737  (XRCCTRL(*this, "nbReplace", wxNotebook)->GetPage(0))->Layout();
738  if (m_findPage==nullptr)
739  (XRCCTRL(*this, "nbReplace", wxNotebook)->GetPage(1))->Layout();
740 
741  Refresh();
742  event.Skip();
743 }
744 
746 {
747  if (event.GetId() == XRCID("chkLimitTo1"))
748  XRCCTRL(*this, "rbLimitTo1", wxRadioBox)->Enable(XRCCTRL(*this, "chkLimitTo1", wxCheckBox)->GetValue());
749  else
750  XRCCTRL(*this, "rbLimitTo2", wxRadioBox)->Enable(XRCCTRL(*this, "chkLimitTo2", wxCheckBox)->GetValue());
751 }
752 
753 // special methods for Replace
754 
756 {
757  wxArrayString values;
758  Manager::Get()->GetConfigManager(_T("editor"))->Read(configKey, &values);
759 
760  combo->Append(values);
761 }
762 
764 {
765  // there should be only a maximum of entries in the config to limit data use
766  // so we define a reasonable maximal of entries to be taken from
767  // the combobox to be written into the config
768  static const unsigned int max_value = 10u;
769 
770  wxArrayString values;
771 
772  values.Add(combo->GetValue());
773 
774  const unsigned int item_count = std::min(combo->GetCount(), max_value);
775  for (unsigned int i = 0; i < item_count; ++i)
776  {
777  const wxString item = combo->GetString(i);
778 
779  if ( item.IsEmpty() )
780  continue;
781 
782  if ( values.Index(item) == wxNOT_FOUND )
783  values.Add(item);
784  }
785 
786  Manager::Get()->GetConfigManager(_T("editor"))->Write(configKey, values);
787 }
788 
790 {
791  cbProject* activeProject = Manager::Get()->GetProjectManager()->GetActiveProject();
792  if (!activeProject)
793  return;
794 
795  wxArrayString targetNames;
796  const wxString strAllProjectFiles = _("All project files");
797  targetNames.push_back(strAllProjectFiles);
798 
799  const int targetCount = activeProject->GetBuildTargetsCount();
800  for(int ii = 0; ii < targetCount; ++ii)
801  targetNames.push_back(activeProject->GetBuildTarget(ii)->GetTitle());
802 
803  IncrementalSelectArrayIterator iterator(targetNames);
804  IncrementalSelectDialog dlg(this, &iterator, _("Select target..."), _("Choose target:"));
805  if (dlg.ShowModal() == wxID_OK)
806  {
807  wxChoice *chTarget = XRCCTRL(*this, "chTarget", wxChoice);
808 
809  if (targetCount < maxTargetCount)
810  chTarget->SetSelection(dlg.GetSelection());
811  else
812  {
813  chTarget->Clear();
814  const int selection = dlg.GetSelection();
815  if (selection == 0)
816  {
817  chTarget->Append(targetNames[0]);
818  chTarget->SetSelection(0);
819  }
820  else
821  {
822  chTarget->Append(strAllProjectFiles);
823  chTarget->Append(targetNames[selection]);
824  chTarget->SetSelection(1);
825  }
826  }
827  }
828 }
bool IsMultiLine() const
void OnActivate(wxActivateEvent &event)
int GetOrigin() const override
bool GetAutoWrapSearch() const override
bool GetMultiLine() const override
ConfigManager * GetConfigManager(const wxString &name_space) const
Definition: manager.cpp:474
int ReadInt(const wxString &name, int defaultVal=0)
static Manager * Get()
Use Manager::Get() to get a pointer to its instance Manager::Get() is guaranteed to never return an i...
Definition: manager.cpp:182
void OnLimitToChange(wxCommandEvent &event)
void OnBrowsePath(wxCommandEvent &event)
void SaveComboValues(wxComboBox *combo, const wxString &configKey)
virtual int GetSelection() const
virtual wxString GetString(unsigned int n) const
bool GetMatchWord() const override
bool GetStartWord() const override
bool ReadBool(const wxString &name, bool defaultVal=false)
DLLIMPORT wxString ChooseDirectory(wxWindow *parent, const wxString &message=_("Select directory"), const wxString &initialPath=_T(""), const wxString &basePath=_T(""), bool askToMakeRelative=false, bool showCreateDirButton=false)
Definition: globals.cpp:639
int Index(const wxString &sz, bool bCase=true, bool bFromEnd=false) const
bool GetStartFile() const override
#define _T(string)
virtual bool IsItemEnabled(unsigned int n) const
bool GetFixEOLs() const override
wxString GetReplaceString() const override
bool GetRecursive() const override
void OnSelectTarget(wxCommandEvent &event)
#define wxT(string)
#define wxNOT_FOUND
bool GetRegEx() const override
bool GetDeleteOldSearches() const override
EditorManager * GetEditorManager() const
Definition: manager.cpp:434
void OnMultiChange(wxCommandEvent &event)
void OnScopeChange(wxCommandEvent &event)
void UnSet(const wxString &name)
virtual void SetSelection(int n)
ProjectManager * GetProjectManager() const
Functions returning pointers to the respective sub-manager instances.
Definition: manager.cpp:429
void Write(const wxString &name, const wxString &value, bool ignoreEmpty=false)
wxWindow * m_findPage
Represents a Code::Blocks project.
Definition: cbproject.h:96
const wxString & GetActiveBuildTarget() const
Definition: cbproject.cpp:1373
#define CONF_GROUP
bool GetFindUsesSelectedText() const override
virtual bool Enable(unsigned int n, bool enable=true)
virtual const wxString & GetTitle() const
Read the target&#39;s title.
bool Exists(const wxString &name)
void OnReplaceChange(wxNotebookEvent &event)
cbProject * GetActiveProject()
Retrieve the active project.
wxString Read(const wxString &key, const wxString &defaultVal=wxEmptyString)
virtual wxString GetBasePath() const
Read the target&#39;s base path, e.g. if GetFilename() returns "/usr/local/bin/xxx", base path will retur...
bool m_findReplaceInFilesActive
wxString GetSearchMask() const override
virtual int ShowModal()
wxString GetSearchPath() const override
const wxString & _(const wxString &string)
cbEditor * GetBuiltinEditor(EditorBase *eb)
int GetBuildTargetsCount()
Definition: cbproject.h:200
ProjectBuildTarget * GetBuildTarget(int index)
Access a build target.
Definition: cbproject.cpp:1392
void OnSearchProject(wxCommandEvent &event)
bool GetMatchCase() const override
bool IsEmpty() const
void OnDeferredFocus(wxCommandEvent &event)
The entry point singleton for working with projects.
bool IsFindInFiles() const override
~FindReplaceDlg() override
wxString GetFindString() const override
void OnRegEx(wxCommandEvent &event)
int GetTarget() const override
size_t Add(const wxString &str, size_t copies=1)
Simple iterator that uses wxArrayString as data source.
static wxXmlResource * Get()
Simple incremental select dialog that shows a single column and doesn&#39;t have much ui elements...
ProjectsArray * GetProjects()
Retrieve an array of all the opened projects.
const int maxTargetCount
int GetScope() const override
int GetDirection() const override
virtual bool GetValue() const
bool GetSortSearchResult() const override
int GetProject() const override
void FillComboWithLastValues(wxComboBox *combo, const wxString &configKey)
virtual void SetSelection(int n)
bool GetHidden() const override
virtual void SetValue(bool state)
virtual int GetSelection() const
wxObject * LoadObject(wxWindow *parent, const wxString &name, const wxString &classname)
virtual int FindString(const wxString &s, bool bCase=false) const