/* 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::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; }
/* TextEditor::jumpToLine * Prompts the user for a line number and moves the cursor to the end * of the entered line *******************************************************************/ void TextEditor::jumpToLine() { int numlines = GetNumberOfLines(); // Prompt for line number long line = wxGetNumberFromUser( "Enter a line number to jump to", S_FMT("Line number (1-%d):", numlines), "Jump To Line", GetCurrentLine() + 1, 1, numlines, this); if (line >= 1) { // Move to line int pos = GetLineEndPosition(line - 1); SetCurrentPos(pos); SetSelection(pos, pos); EnsureCaretVisible(); SetFocus(); } }
bool ctlSQLBox::DoFind(const wxString &find, const wxString &replace, bool doReplace, bool wholeWord, bool matchCase, bool useRegexps, bool startAtTop, bool reverse) { int flags = 0; int startPos = GetSelectionStart(); int endPos = GetTextLength(); // Setup flags if (wholeWord) flags |= wxSTC_FIND_WHOLEWORD; if (matchCase) flags |= wxSTC_FIND_MATCHCASE; // Replace the current selection, if there is one and it matches the find param. wxString current = GetSelectedText(); if (doReplace) { if (useRegexps) { CharacterRange cr = RegexFindText(GetSelectionStart(), GetSelectionEnd(), find); if (GetSelectionStart() == cr.cpMin && GetSelectionEnd() == cr.cpMax) { if (cr.cpMin == cr.cpMax) // Must be finding a special char, such as $ (line end) { InsertText(cr.cpMax, replace); SetSelection(cr.cpMax, cr.cpMax + replace.Length()); SetCurrentPos(cr.cpMax + replace.Length()); // Stop if we've got to the end. This is important for the $ // case where it'll just keep finding the end of the line!! if ((int)(cr.cpMin + replace.Length()) == GetLength()) return false; } else { ReplaceSelection(replace); SetSelection(startPos, startPos + replace.Length()); SetCurrentPos(startPos + replace.Length()); } } } else if ((matchCase && current == find) || (!matchCase && current.Upper() == find.Upper())) { ReplaceSelection(replace); if (!reverse) { SetSelection(startPos, startPos + replace.Length()); SetCurrentPos(startPos + replace.Length()); } else { SetSelection(startPos + replace.Length(), startPos); SetCurrentPos(startPos); } } } //////////////////////////////////////////////////////////////////////// // Figure out the starting position for the next search //////////////////////////////////////////////////////////////////////// if (startAtTop) { startPos = 0; endPos = GetTextLength(); } else { if (reverse) { endPos = 0; startPos = GetCurrentPos(); } else { endPos = GetTextLength(); startPos = GetCurrentPos(); } } size_t selStart = 0, selEnd = 0; if (useRegexps) { CharacterRange cr = RegexFindText(startPos, endPos, find); selStart = cr.cpMin; selEnd = cr.cpMax; } else { selStart = FindText(startPos, endPos, find, flags); selEnd = selStart + find.Length(); } if (selStart >= 0 && selStart != (size_t)(-1)) { if (reverse) { SetCurrentPos(selStart); SetSelection(selEnd, selStart); } else { SetCurrentPos(selEnd); SetSelection(selStart, selEnd); } EnsureCaretVisible(); return true; } else return false; }