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
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : findinfilesdlg.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 "search_thread.h"
#include <wx/tokenzr.h>
#include <wx/fontmap.h>
#include "event_notifier.h"
#include "dirpicker.h"
#include "manager.h"
#include "frame.h"
#include "macros.h"
#include "findinfilesdlg.h"
#include "findresultstab.h"
#include "replaceinfilespanel.h"
#include "windowattrmanager.h"
#include <algorithm>
FindInFilesDialog::FindInFilesDialog(wxWindow* parent, const wxString &dataName)
: FindInFilesDialogBase(parent, wxID_ANY)
{
m_data.SetName(dataName);
// Store the find-in-files data
clConfig::Get().ReadItem( &m_data );
wxArrayString choices;
size_t count = m_data.GetSearchPaths().GetCount();
for (size_t i = 0; i < count; ++i) {
choices.Add(m_data.GetSearchPaths().Item(i));
}
// add the default search paths
if(choices.Index(wxGetTranslation(SEARCH_IN_WORKSPACE)) == wxNOT_FOUND)
choices.Add(wxGetTranslation(SEARCH_IN_WORKSPACE));
if(choices.Index(wxGetTranslation(SEARCH_IN_PROJECT)) == wxNOT_FOUND)
choices.Add(wxGetTranslation(SEARCH_IN_PROJECT));
if(choices.Index(wxGetTranslation(SEARCH_IN_CURR_FILE_PROJECT)) == wxNOT_FOUND)
choices.Add(wxGetTranslation(SEARCH_IN_CURR_FILE_PROJECT));
if(choices.Index(wxGetTranslation(SEARCH_IN_CURRENT_FILE)) == wxNOT_FOUND)
choices.Add(wxGetTranslation(SEARCH_IN_CURRENT_FILE));
if(choices.Index(wxGetTranslation(SEARCH_IN_OPEN_FILES)) == wxNOT_FOUND)
choices.Add(wxGetTranslation(SEARCH_IN_OPEN_FILES));
int initial = m_data.GetSearchScope();
if ((initial == wxNOT_FOUND) || ((size_t)initial >= count)) {
initial = 0;
}
m_dirPicker->SetValues(choices, initial);
// Search for
m_findString->Clear();
m_findString->Append(m_data.GetFindStringArr());
m_findString->SetValue(m_data.GetFindString());
m_fileTypes->SetSelection(0);
m_matchCase->SetValue(m_data.GetFlags() & wxFRD_MATCHCASE);
m_matchWholeWord->SetValue(m_data.GetFlags() & wxFRD_MATCHWHOLEWORD);
m_regualrExpression->SetValue(m_data.GetFlags() & wxFRD_REGULAREXPRESSION);
m_printScope->SetValue(m_data.GetFlags() & wxFRD_DISPLAYSCOPE);
m_checkBoxSaveFilesBeforeSearching->SetValue(m_data.GetFlags() & wxFRD_SAVE_BEFORE_SEARCH);
m_checkBoxSeparateTab->SetValue(m_data.GetFlags() & wxFRD_SEPARATETAB_DISPLAY);
m_checkBoxSkipMatchesFoundInComments->SetValue(m_data.GetFlags() & wxFRD_SKIP_COMMENTS);
m_checkBoxSkipMatchesFoundInStrings->SetValue(m_data.GetFlags() & wxFRD_SKIP_STRINGS);
m_checkBoxHighlighStringComments->SetValue(m_data.GetFlags() & wxFRD_COLOUR_COMMENTS);
// Set encoding
wxArrayString astrEncodings;
wxFontEncoding fontEnc;
int selection(0);
size_t iEncCnt = wxFontMapper::GetSupportedEncodingsCount();
for (size_t i = 0; i < iEncCnt; i++) {
fontEnc = wxFontMapper::GetEncoding(i);
if (wxFONTENCODING_SYSTEM == fontEnc) { // skip system, it is changed to UTF-8 in optionsconfig
continue;
}
wxString encodingName = wxFontMapper::GetEncodingName(fontEnc);
size_t pos = astrEncodings.Add(encodingName);
if(m_data.GetEncoding() == encodingName) {
selection = static_cast<int>(pos);
}
}
m_choiceEncoding->Append(astrEncodings);
if(m_choiceEncoding->IsEmpty() == false)
m_choiceEncoding->SetSelection(selection);
// Set the file mask
DoSetFileMask();
GetSizer()->Fit(this);
WindowAttrManager::Load(this, "FindInFilesDialog", NULL);
Centre();
}
FindInFilesDialog::~FindInFilesDialog()
{
// Update the data
m_data.SetFlags( GetSearchFlags() );
m_data.SetFindString( m_findString->GetValue() );
m_data.SetEncoding ( m_choiceEncoding->GetStringSelection() );
m_data.SetSearchScope(m_dirPicker->GetCurrentSelection());
wxString value = m_fileTypes->GetValue();
value.Trim().Trim(false);
wxArrayString masks = m_fileTypes->GetStrings();
if ( masks.Index(value) == wxNOT_FOUND ) {
masks.Insert(value, 0);
}
m_data.SetSelectedMask( value );
m_data.SetFileMask( masks );
m_data.SetSearchPaths( m_dirPicker->GetValues() );
clConfig::Get().WriteItem( &m_data );
WindowAttrManager::Save(this, "FindInFilesDialog", NULL);
}
void FindInFilesDialog::SetRootDir(const wxString &rootDir)
{
m_dirPicker->SetPath(rootDir);
}
void FindInFilesDialog::DoSetFileMask()
{
// First send an event to the plugins asking for an additional file mask
clCommandEvent getFileMaskEvent(wxEVT_CMD_GET_FIND_IN_FILES_MASK, GetId());
getFileMaskEvent.SetEventObject(this);
EventNotifier::Get()->ProcessEvent(getFileMaskEvent);
// Get the output
wxArrayString fileTypes = m_data.GetFileMask();
wxArrayString pluginsMask = getFileMaskEvent.GetStrings();
// sort and merge arrays
fileTypes.Sort();
pluginsMask.Sort();
wxArrayString mergedArr;
std::merge(fileTypes.begin(), fileTypes.end(), pluginsMask.begin(), pluginsMask.end(), std::back_inserter( mergedArr ) );
wxArrayString::iterator iter = std::unique(mergedArr.begin(), mergedArr.end());
// remove the non unique parts
mergedArr.resize( std::distance(mergedArr.begin(), iter) );
// Create a single mask array
m_fileTypes->Clear();
if( !mergedArr.IsEmpty() ) {
m_fileTypes->Append(mergedArr);
int where = m_fileTypes->FindString(m_data.GetSelectedMask());
if(where == wxNOT_FOUND) {
where = 0;
}
m_fileTypes->SetSelection( where );
}
}
void FindInFilesDialog::DoSearchReplace()
{
SearchData data = DoGetSearchData();
data.SetOwner(clMainFrame::Get()->GetOutputPane()->GetReplaceResultsTab());
DoSaveOpenFiles();
SearchThreadST::Get()->PerformSearch(data);
Close();
}
void FindInFilesDialog::DoSearch()
{
SearchData data = DoGetSearchData();
data.SetOwner(clMainFrame::Get()->GetOutputPane()->GetFindResultsTab());
// check to see if we require to save the files
DoSaveOpenFiles();
SearchThreadST::Get()->PerformSearch(data);
Close();
}
SearchData FindInFilesDialog::DoGetSearchData()
{
SearchData data;
wxString findStr(m_data.GetFindString());
if (m_findString->GetValue().IsEmpty() == false) {
findStr = m_findString->GetValue();
}
data.SetFindString (findStr);
m_data.SetFlags( GetSearchFlags() );
size_t flags = m_data.GetFlags();
// If the 'Skip comments' is ON, remove the
// 'colour comments' flag
if(flags & wxFRD_SKIP_COMMENTS) {
flags &= ~wxFRD_COLOUR_COMMENTS;
}
data.SetMatchCase ((flags & wxFRD_MATCHCASE) != 0);
data.SetMatchWholeWord ((flags & wxFRD_MATCHWHOLEWORD) != 0);
data.SetRegularExpression((flags & wxFRD_REGULAREXPRESSION) != 0);
data.SetDisplayScope ((flags & wxFRD_DISPLAYSCOPE) != 0);
data.SetEncoding (m_choiceEncoding->GetStringSelection());
data.SetSkipComments (flags & wxFRD_SKIP_COMMENTS);
data.SetSkipStrings (flags & wxFRD_SKIP_STRINGS);
data.SetColourComments ( flags & wxFRD_COLOUR_COMMENTS );
wxArrayString rootDirs;
for (size_t i = 0; i < m_listPaths->GetCount(); ++i) {
rootDirs.push_back(m_listPaths->GetString(i));
}
if (rootDirs.IsEmpty()) {
wxString dir = m_dirPicker->GetPath();
if (dir.IsEmpty() == false) {
rootDirs.push_back(dir);
}
}
data.SetRootDirs(rootDirs);
wxArrayString files;
for (size_t i = 0; i < rootDirs.GetCount(); ++i) {
const wxString& rootDir = rootDirs.Item(i);
// Check both translations and otherwise: the history may contain either
if ((rootDir == wxGetTranslation(SEARCH_IN_WORKSPACE)) || (rootDir == SEARCH_IN_WORKSPACE)) {
ManagerST::Get()->GetWorkspaceFiles(files);
} else if ((rootDir == wxGetTranslation(SEARCH_IN_PROJECT)) || (rootDir == SEARCH_IN_PROJECT)) {
ManagerST::Get()->GetActiveProjectFiles(files);
} else if ((rootDir == wxGetTranslation(SEARCH_IN_CURR_FILE_PROJECT)) || (rootDir == SEARCH_IN_CURR_FILE_PROJECT)) {
ManagerST::Get()->GetActiveFileProjectFiles(files);
} else if ((rootDir == wxGetTranslation(SEARCH_IN_CURRENT_FILE)) || (rootDir == SEARCH_IN_CURRENT_FILE)) {
LEditor *editor = clMainFrame::Get()->GetMainBook()->GetActiveEditor();
if(editor) {
files.Add(editor->GetFileName().GetFullPath());
}
} else if ((rootDir == wxGetTranslation(SEARCH_IN_OPEN_FILES)) || (rootDir == SEARCH_IN_OPEN_FILES)) {
std::vector<LEditor*> editors;
clMainFrame::Get()->GetMainBook()->GetAllEditors(editors, MainBook::kGetAll_IncludeDetached );
for (size_t n=0; n < editors.size(); ++n) {
LEditor* editor = dynamic_cast<LEditor*>(*(editors.begin()+n));
if (editor) {
files.Add(editor->GetFileName().GetFullPath());
}
}
}
}
data.SetFiles(files);
data.UseNewTab(m_checkBoxSeparateTab->IsChecked());
data.SetExtensions(m_fileTypes->GetValue());
return data;
}
void FindInFilesDialog::OnClose(wxCloseEvent &e)
{
Destroy();
}
void FindInFilesDialog::OnAddPath( wxCommandEvent& event )
{
wxString path = m_dirPicker->GetPath();
if (m_listPaths->FindString(path) == wxNOT_FOUND) {
m_listPaths->Append(path);
}
}
void FindInFilesDialog::OnRemovePath( wxCommandEvent& event )
{
int sel = m_listPaths->GetSelection();
if (sel != wxNOT_FOUND) {
m_listPaths->Delete(sel);
}
}
void FindInFilesDialog::OnClearPaths( wxCommandEvent& event )
{
m_listPaths->Clear();
}
bool FindInFilesDialog::Show()
{
bool res = IsShown() || wxDialog::Show();
if (res) {
// update the combobox
m_findString->Clear();
m_findString->Append(m_data.GetFindStringArr());
DoSetFileMask();
m_findString->SetValue(m_data.GetFindString());
LEditor *editor = clMainFrame::Get()->GetMainBook()->GetActiveEditor();
if (editor) {
//if we have an open editor, and a selected text, make this text the search string
wxString selText = editor->GetSelectedText();
if (!selText.IsEmpty()) {
m_findString->SetValue(selText);
}
}
m_findString->SetSelection(-1, -1); // select all
m_findString->SetFocus();
}
return res;
}
void FindInFilesDialog::DoSaveSearchPaths()
{
wxArrayString paths = m_dirPicker->GetValues();
m_data.SetSearchPaths(paths);
}
void FindInFilesDialog::DoSaveOpenFiles()
{
if (m_checkBoxSaveFilesBeforeSearching->IsChecked()) {
clMainFrame::Get()->GetMainBook()->SaveAll(false, false);
}
}
void FindInFilesDialog::OnClearPathsUI(wxUpdateUIEvent& event)
{
event.Enable(m_listPaths->IsEmpty() == false);
}
void FindInFilesDialog::OnRemovePathUI(wxUpdateUIEvent& event)
{
event.Enable(m_listPaths->GetSelection() != wxNOT_FOUND);
}
void FindInFilesDialog::OnFindWhatUI(wxUpdateUIEvent& event)
{
event.Enable(m_findString->GetValue().IsEmpty() == false);
}
void FindInFilesDialog::OnUseDiffColourForCommentsUI(wxUpdateUIEvent& event)
{
event.Enable(m_checkBoxSkipMatchesFoundInComments->IsChecked() == false);
}
void FindInFilesDialog::OnFind(wxCommandEvent& event)
{
wxUnusedVar( event );
DoSearch();
}
void FindInFilesDialog::OnReplace(wxCommandEvent& event)
{
wxUnusedVar( event );
DoSearchReplace();
}
void FindInFilesDialog::OnStop(wxCommandEvent& event)
{
SearchThreadST::Get()->StopSearch();
}
void FindInFilesDialog::OnButtonClose(wxCommandEvent& event)
{
Destroy();
}
size_t FindInFilesDialog::GetSearchFlags()
{
size_t flags = 0;
if ( m_matchCase->IsChecked() ) flags |= wxFRD_MATCHCASE;
if ( m_matchWholeWord->IsChecked()) flags |= wxFRD_MATCHWHOLEWORD;
if ( m_regualrExpression->IsChecked()) flags |= wxFRD_REGULAREXPRESSION;
if ( m_printScope->IsChecked()) flags |= wxFRD_DISPLAYSCOPE;
if ( m_checkBoxSeparateTab->IsChecked() ) flags |= wxFRD_SEPARATETAB_DISPLAY;
if ( m_checkBoxSaveFilesBeforeSearching->IsChecked() ) flags |= wxFRD_SAVE_BEFORE_SEARCH;
if ( m_checkBoxSkipMatchesFoundInComments->IsChecked() ) flags |= wxFRD_SKIP_COMMENTS;
if ( m_checkBoxSkipMatchesFoundInStrings->IsChecked() ) flags |= wxFRD_SKIP_STRINGS;
if ( m_checkBoxHighlighStringComments->IsChecked() ) flags |= wxFRD_COLOUR_COMMENTS;
return flags;
}
|