/* TextEditor::replaceAll * Replaces all occurrences of [find] in the text with [replace]. * Returns the number of occurrences replaced *******************************************************************/ int TextEditor::replaceAll(string find, string replace, int flags) { // Check search string if (find.IsEmpty()) return false; // Start at beginning SetSelection(0, 0); // Loop of death int replaced = 0; while (true) { SearchAnchor(); int found = SearchNext(flags, find); if (found < 0) break; // No matches, finished else { // Replace text & increment counter Replace(found, found + find.length(), replace); replaced++; // Continue from end of replaced text SetSelection(found + find.length(), found + find.length()); } } // Return number of instances replaced return replaced; }
/* TextEditor::findPrev * Finds the previous occurrence of the [find] after the caret * position, selects it and scrolls to it if needed. Returns false * if the [find] was invalid or no match was found, true otherwise *******************************************************************/ bool TextEditor::findPrev(string find, int flags) { // Check search string if (find.IsEmpty()) return false; // Get current selection int sel_start = GetSelectionStart(); int sel_end = GetSelectionEnd(); // Search back from the start of the current selection SetSelection(sel_start, sel_start); SearchAnchor(); int found = SearchPrev(flags, find); if (found < 0) { // Not found, loop back to end SetSelection(GetTextLength() - 1, GetTextLength() - 1); SearchAnchor(); found = SearchPrev(flags, find); if (found < 0) { // No match found in entire text, reset selection SetSelection(sel_start, sel_end); return false; } } // Set caret to the end of the matching text // (it defaults to the start for some dumb reason) // and scroll to the selection SetSelection(found, found + find.length()); EnsureCaretVisible(); return true; }
bool Edit::FindText(int &found_start, int &found_end, bool find_next) { const int flags = m_FindData.GetFlags(); const bool find_down = (flags & wxFR_DOWN) ? true : false; const wxString find_string = m_FindData.GetFindString(); found_start = found_end = -1; if (find_next) { GotoPos(find_down ? GetCurrentPos() : GetCurrentPos() - find_string.size()); } SearchAnchor(); // search up/down found_start = find_down ? SearchNext(flags, find_string) : SearchPrev(flags, find_string); // found if (found_start > -1) { found_end = found_start + find_string.size(); GotoPos(found_start); return true; } return false; }