/* TextEditor::findNext * Finds the next 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::findNext(string find) { // Check search string if (find.IsEmpty()) return false; // Setup target range SetTargetEnd(GetTextLength()); SetTargetStart(GetSelectionEnd()); // Search within current target range if (SearchInTarget(find) < 0) { // None found, search again from start SetTargetStart(0); SetTargetEnd(GetTextLength()); if (SearchInTarget(find) < 0) { // No matches found in entire text return false; } } // Select matched text SetSelection(GetTargetStart(), GetTargetEnd()); // Scroll to selection EnsureCaretVisible(); return true; }
/* 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) { // Check search string if (find.IsEmpty()) return false; // Init search target to entire text SetTargetStart(0); SetTargetEnd(GetTextLength()); // Loop of death int replaced = 0; while (1) { if (SearchInTarget(find) < 0) break; // No matches, finished else { // Replace text & increment counter ReplaceTarget(replace); replaced++; // Continue search from end of replaced text to end of text SetTargetStart(GetTargetEnd()); SetTargetEnd(GetTextLength()); } } // Return number of instances replaced return replaced; }
/* TextEditor::replaceCurrent * Replaces the currently selected occurrence of [find] with * [replace], then selects and scrolls to the next occurrence of * [find] in the text. Returns false if [find] is invalid or the * current selection does not match it, true otherwise *******************************************************************/ bool TextEditor::replaceCurrent(string find, string replace) { // Check search string if (find.IsEmpty()) return false; // Check that we've done a find previously // (by searching for the find string within the current selection) if (GetSelectedText().Length() != find.Length()) return false; SetTargetStart(GetSelectionStart()); SetTargetEnd(GetSelectionEnd()); if (SearchInTarget(find) < 0) return false; // Do the replace ReplaceTarget(replace); // Update selection SetSelection(GetTargetStart(), GetTargetEnd()); // Do find next findNext(find); return true; }
/* TextEditor::onUpdateUI * Called when anything is modified in the text editor (cursor * position, styling, text, etc) *******************************************************************/ void TextEditor::onUpdateUI(wxStyledTextEvent& e) { // Check for brace match if (txed_brace_match) checkBraceMatch(); // If a calltip is open, update it if (call_tip->IsShown()) updateCalltip(); // Do word matching if appropriate if (txed_match_cursor_word && language) { int word_start = WordStartPosition(GetCurrentPos(), true); int word_end = WordEndPosition(GetCurrentPos(), true); string current_word = GetTextRange(word_start, word_end); if (!current_word.IsEmpty() && HasFocus()) { if (current_word != prev_word_match) { prev_word_match = current_word; SetIndicatorCurrent(8); IndicatorClearRange(0, GetTextLength()); SetTargetStart(0); SetTargetEnd(GetTextLength()); SetSearchFlags(0); while (SearchInTarget(current_word) != -1) { IndicatorFillRange(GetTargetStart(), GetTargetEnd() - GetTargetStart()); SetTargetStart(GetTargetEnd()); SetTargetEnd(GetTextLength()); } } } else { SetIndicatorCurrent(8); IndicatorClearRange(0, GetTextLength()); prev_word_match = ""; } } // Hilight current line MarkerDeleteAll(1); MarkerDeleteAll(2); if (txed_hilight_current_line > 0 && HasFocus()) { int line = LineFromPosition(GetCurrentPos()); MarkerAdd(line, 1); if (txed_hilight_current_line > 1) MarkerAdd(line, 2); } e.Skip(); }
void Edit::OnFindReplaceDialog(wxFindDialogEvent& event) { const wxEventType type = event.GetEventType(); const wxString find_string = m_FindData.GetFindString(); int found_start, found_end; if (type == wxEVT_FIND || type == wxEVT_FIND_NEXT) { // search if (FindText(found_start, found_end, type == wxEVT_FIND_NEXT)) { SetSelection(found_start, found_end); } else { wxMessageDialog dialog(this, wxT("Cannot find the text \"" + find_string + "\" from current position"), wxT("Find")); dialog.ShowModal(); } } else if (type == wxEVT_FIND_REPLACE) { ReplaceText(find_string); } else if (type == wxEVT_FIND_REPLACE_ALL) { const wxString replace_string = m_FindData.GetReplaceString(); int start_index = 0; bool found = true; int found_count = 0; do { // set search area SetTargetStart(start_index); SetTargetEnd(GetLastPosition()); // search and replace found_start = SearchInTarget(find_string); if (found_start > -1) { found_end = found_start + find_string.size(); SetTargetStart(found_start); SetTargetEnd(found_end); ReplaceTarget(replace_string); start_index = found_start + replace_string.size(); found_count++; } else { found = false; } } while (found); const wxString message = wxString::Format(wxT("%d occurrence(s) of \"%s\" were replaced with \"%s\"."), found_count, find_string, replace_string); wxMessageDialog replace_dialog(this, message, wxT("Replaced Text")); replace_dialog.ShowModal(); } }