void CodeEdit::CommentSelection(const wxString& comment) { if (GetSelectionStart() < GetSelectionEnd()) { int startLine = LineFromPosition(GetSelectionStart()); int endLine = LineFromPosition(GetSelectionEnd()); // Find the minimum indentation level for all of the selected lines. int minIndentation = INT_MAX; for (int i = startLine; i <= endLine; ++i) { wxString lineText = GetLine(i); int firstNonWhitespace = GetFirstNonWhitespace(lineText); if (firstNonWhitespace != -1) { minIndentation = wxMin(firstNonWhitespace, minIndentation); } } // Insert the comment string on each non-blank line. wxString result; for (int i = startLine; i <= endLine; ++i) { wxString lineText = GetLine(i); if (GetFirstNonWhitespace(lineText) != -1) { lineText.insert(minIndentation, comment); } result += lineText; } SetTargetStart(PositionFromLine(startLine)); SetTargetEnd(PositionFromLine(endLine + 1)); ReplaceTarget(result); // Select the replaced text. SetSelectionStart(GetTargetStart()); SetSelectionEnd(GetTargetEnd() - 1); } }
/* 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; }
void fbtTextFile::cutEvent(wxCommandEvent& evt) { if (GetReadOnly() || GetSelectionEnd() - GetSelectionStart() <= 0) return; Cut(); }
int SearchableEditor::replaceAll() { int caret = GetSelectionStart(); // remember position, so we return to some sensible place at end SetSelectionStart(0); // start from beginning of file SetSelectionEnd(0); findFlagsM.remove(se::FROM_TOP); bool alert = findFlagsM.has(se::ALERT); if (alert) findFlagsM.remove(se::ALERT); // turn flag off temporary (we'll popup a different message) bool wrap = findFlagsM.has(se::WRAP); if (wrap) findFlagsM.remove(se::WRAP); // turn it off to prevent endless loop (if replace text contains search text) int cnt = 0; while (replace(true)) cnt++; if (alert) { wxMessageBox(wxString::Format(_("%d replacements were made."), cnt), _("Replace"), wxICON_INFORMATION|wxOK); findFlagsM.add(se::ALERT); // turn it back on } if (wrap) // turn it back on findFlagsM.add(se::WRAP); SetSelectionStart(caret); SetSelectionEnd(caret); return cnt; }
void Edit::ReplaceText(const wxString &find_string) { int found_start, found_end; const int select_start = GetSelectionStart(); const int select_end = GetSelectionEnd(); // replace & search if (select_start > -1 && select_end > select_start) { const wxString select_string = GetSelectedText(); if (select_string == find_string) { ReplaceSelection(m_FindData.GetReplaceString()); // search if (FindText(found_start, found_end, false)) { SetSelection(found_start, found_end); } else { wxMessageDialog dialog(this, wxT("Cannot find the text \"" + find_string + "\" from current position"), wxT("Find")); dialog.ShowModal(); } } } // search else if (FindText(found_start, found_end, false)) { SetSelection(found_start, found_end); } }
///////////////////////////////////// // @mfunc Search backward for a given string and select it if found. You may use regular expressions on the text. // @rvalue bool | true if text is ffound else false // bool CScintillaWnd::SearchBackward(LPSTR szText) { //@parm text to search if (szText == NULL) { return false; } long lPos = GetCurrentPosition(); long lMinSel = GetSelectionStart(); TextToFind tf; tf.lpstrText = szText; if (lMinSel >= 0) { tf.chrg.cpMin = lMinSel-1; } else { tf.chrg.cpMin = lPos-1; } tf.chrg.cpMax = 0; lPos = SendMessage(SCI_FINDTEXT, m_nSearchflags, (ULONG_PTR)&tf); if (lPos >= 0) { SetFocus(); GotoPosition(lPos); SendMessage(SCI_SETSEL, tf.chrgText.cpMin, tf.chrgText.cpMax); SendMessage(SCI_FINDTEXT, m_nSearchflags, (ULONG_PTR)&tf); return true; } return false; }
void StyledTextBox::RemoveSelectedText() { if (!HasSelectedText()) { return; } auto text = GetStdText(); auto eraseStart = begin(text) + GetSelectionStart(); auto eraseEnd = eraseStart + (GetSelectionEnd() - GetSelectionStart()); text.erase(eraseStart, eraseEnd); SetCursorPosition(GetSelectionStart()); SetText(text); ClearSelection(); }
NS_IMETHODIMP HTMLTextAreaElement::GetSelectionStart(int32_t *aSelectionStart) { NS_ENSURE_ARG_POINTER(aSelectionStart); ErrorResult error; *aSelectionStart = GetSelectionStart(error); return error.ErrorCode(); }
bool SearchableEditor::replace(bool force) { int start = GetSelectionStart(); int end = GetSelectionEnd(); if (start == end || FindText(start, end, findTextM, findFlagsM.asStc()) == -1) { if (!find(false)) return false; if (!force) return false; } // we have selection here start = GetSelectionStart(); ReplaceSelection(replaceTextM); SetSelectionEnd(start + replaceTextM.Length()); // position at end of replaced text find(false); return true; }
int SearchableEditor::replaceInSelection() { findFlagsM.remove(se::FROM_TOP); // turn flag off bool alert = findFlagsM.has(se::ALERT); if (alert) findFlagsM.remove(se::ALERT); // turn flag off temporary bool wrap = findFlagsM.has(se::WRAP); if (wrap) findFlagsM.remove(se::WRAP); // turn it off to prevent endless loop (if replace text contains search text) int cnt = 0; int selstart = GetSelectionStart(); int selend = GetSelectionEnd(); SetSelectionEnd(selstart); // start replace from here while (find(false)) { int start = GetSelectionStart(); int end = GetSelectionEnd(); if (end <= selend) // in range { selend += replaceTextM.Length() - (end-start); // expand/contract range ReplaceSelection(replaceTextM); SetSelectionEnd(start + replaceTextM.Length()); // move to appropriate position for next find() cnt++; } else break; } if (alert) { wxMessageBox(wxString::Format(_("%d replacements were made."), cnt), _("Replace"), wxICON_INFORMATION|wxOK); findFlagsM.add(se::ALERT); // turn it back on } if (wrap) // turn it back on findFlagsM.add(se::WRAP); SetSelectionStart(selstart); SetSelectionEnd(selend); return cnt; }
void CodeEdit::UncommentSelection(const wxString& commentString) { if (GetSelectionStart() < GetSelectionEnd()) { int startLine = LineFromPosition(GetSelectionStart()); int endLine = LineFromPosition(GetSelectionEnd()); wxString result; for (int i = startLine; i <= endLine; ++i) { wxString lineText = GetLine(i); unsigned int c = GetFirstNonWhitespace(lineText); if (c != -1 && lineText.compare(c, commentString.Length(), commentString) == 0) { lineText.erase(c, commentString.Length()); } result += lineText; } SetTargetStart(PositionFromLine(startLine)); SetTargetEnd(PositionFromLine(endLine + 1)); ReplaceTarget(result); // Select the replaced text. SetSelectionStart(GetTargetStart()); SetSelectionEnd(GetTargetEnd() - 1); } }
boost::python::tuple ScintillaWrapper::getUserCharSelection() { int start = GetSelectionStart(); int end = GetSelectionEnd(); if (start == end) { start = 0; end = GetLength(); } return boost::python::make_tuple(start, end); }
NS_IMETHODIMP HTMLTextAreaElement::GetSelectionStart(int32_t *aSelectionStart) { NS_ENSURE_ARG_POINTER(aSelectionStart); ErrorResult error; Nullable<uint32_t> selStart(GetSelectionStart(error)); if (error.Failed()) { return error.StealNSResult(); } *aSelectionStart = int32_t(selStart.Value()); return error.StealNSResult(); }
///////////////////////////////////// // @mfunc Get selected string // @rvalue CString | string with currentliy selected text // CString CScintillaWnd::GetSelectedText(void) { long lLen = (GetSelectionEnd() - GetSelectionStart()) + 1; if (lLen > 0) { TCHAR *p = new TCHAR[lLen+1]; if (p != NULL) { *p = '\0'; SendMessage(SCI_GETSELTEXT, 0, (ULONG_PTR)p); CString strReturn = p; delete [] p; return strReturn; } } return _T(""); }
/** * Set the selection anchor to <code>start</code> and the selection edge * to <code>stop</code>. */ void Selection::SetSelection( /* [in] */ ISpannable* text, /* [in] */ Int32 start, /* [in] */ Int32 stop) { Int32 ostart = GetSelectionStart(text); Int32 oend = GetSelectionEnd(text); if (ostart != start || oend != stop) { text->SetSpan( SELECTION_START, start, start, ISpanned::SPAN_POINT_POINT | ISpanned::SPAN_INTERMEDIATE); text->SetSpan( SELECTION_END, stop, stop, ISpanned::SPAN_POINT_POINT); } }
/** * Move the cursor to the buffer offset physically below the current * offset, or return FALSE if the cursor is already on the bottom line. */ Boolean Selection::MoveDown( /* [in] */ ISpannable* text, /* [in] */ ILayout* layout) { Int32 start = GetSelectionStart(text); Int32 end = GetSelectionEnd(text); if (start != end) { Int32 min = Elastos::Core::Math::Min(start, end); Int32 max = Elastos::Core::Math::Max(start, end); SetSelection(text, max); Int32 len; if (min == 0 && max == (text->GetLength(&len), len)) { return FALSE; } return TRUE; } else { Int32 line; layout->GetLineForOffset(end, &line); Int32 lineCount; layout->GetLineCount(&lineCount); if (line < lineCount - 1) { Int32 move; Int32 direction1, direction2; layout->GetParagraphDirection(line, &direction1); layout->GetParagraphDirection(line + 1, &direction2); if (direction1 ==direction2) { Float h; layout->GetPrimaryHorizontal(end, &h); layout->GetOffsetForHorizontal(line + 1, h, &move); } else { layout->GetLineStart(line + 1, &move); } SetSelection(text, move); return TRUE; } } return FALSE; }
boost::python::tuple ScintillaWrapper::getUserLineSelection() { int start = GetSelectionStart(); int end = GetSelectionEnd(); if (start == end) { start = 0; end = GetLineCount() - 1; } else { start = LineFromPosition(start); end = LineFromPosition(end); } return boost::python::make_tuple(start, end); }
bool wxSTEditorShell::CheckReadOnly(bool set) { bool make_ro = !CaretOnPromptLine(STE_CARET_MOVE_NONE); if (!make_ro) { // also check selection and make ro so they can't cut text not on last line int prompt_line = GetPromptLine(); make_ro |= ((LineFromPosition(GetSelectionStart()) < prompt_line) || (LineFromPosition(GetSelectionEnd()) < prompt_line)); } if (set && (make_ro != GetReadOnly())) SetReadOnly(make_ro); return make_ro; }
void Edit::OnFindReplace(wxCommandEvent &event) { const wxString find_string = m_FindData.GetFindString(); int found_start, found_end; if (event.GetId() == myID_FINDNEXT) { // search if (FindText(found_start, found_end, true)) { SetSelection(found_start, found_end); } else { wxMessageDialog dialog(this, wxT("Cannot find the text \"" + find_string + "\" from current position"), wxT("Find")); dialog.ShowModal(); } } if (event.GetId() == myID_REPLACENEXT) { ReplaceText(find_string); } else { if (m_findReplace) { delete m_findReplace; m_findReplace = NULL; } const int select_start = GetSelectionStart(); const int select_end = GetSelectionEnd(); wxString find_text; if (select_start > -1 && select_end > select_start) { find_text = GetSelectedText(); } if (event.GetId() == myID_DLG_FIND_TEXT) { if (find_text.size() > 0) { m_FindData.SetFindString(find_text); } m_findReplace = new wxFindReplaceDialog(this, &m_FindData, wxT("Find")); } else { if (find_text.size() > 0) { m_FindData.SetFindString(find_text); } m_findReplace = new wxFindReplaceDialog(this, &m_FindData, wxT("Find & Replace"), wxFR_REPLACEDIALOG); } m_findReplace->Show(); } }
bool CodeEdit::GetHoverText(int position, wxString& result) { int selectionStart = GetSelectionStart(); int selectionEnd = GetSelectionEnd(); if (position >= selectionStart && position < selectionEnd) { // We're mousing over the selected text. result = GetSelectedText(); return true; } // We don't use the : character as a joiner since we don't // want to evaulate with that. return GetTokenFromPosition(position, ".", result); }
/** * Move the cursor to the buffer offset physically to the right of * the current offset, or return FALSE if the cursor is already at * at the right edge of the line and there is not another line * to move it to. */ Boolean Selection::MoveRight( /* [in] */ ISpannable* text, /* [in] */ ILayout* layout) { Int32 start = GetSelectionStart(text); Int32 end = GetSelectionEnd(text); if (start != end) { SetSelection(text, ChooseHorizontal(layout, 1, start, end)); return TRUE; } else { Int32 to; layout->GetOffsetToRightOf(end, &to); if (to != end) { SetSelection(text, to); return TRUE; } } return FALSE; }
/* 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; }
void DisassemblyTextCtrl::OnGPM(wxMouseEvent& event) { if(platform::gtk == false) // only if GPM is not already implemented by the OS { int pos = PositionFromPoint(wxPoint(event.GetX(), event.GetY())); if(pos == wxSCI_INVALID_POSITION) return; int start = GetSelectionStart(); int end = GetSelectionEnd(); const wxString s = GetSelectedText(); if(pos < GetCurrentPos()) { start += s.length(); end += s.length(); } InsertText(pos, s); SetSelectionVoid(start, end); } } // end of OnGPM
void Edit::OnEditCopy (wxCommandEvent &WXUNUSED(event)) { if (GetSelectionEnd()-GetSelectionStart() <= 0) return; Copy (); }
void Edit::OnEditCut (wxCommandEvent &WXUNUSED(event)) { if (GetReadOnly() || (GetSelectionEnd()-GetSelectionStart() <= 0)) return; Cut (); }
///////////////////////////////////// // @mfunc Replace a text in a selection or in the complete file multiple times // @rvalue int | number of replacements // int CScintillaWnd::ReplaceAll(LPCSTR szFind, LPCSTR szReplace, bool bUseSelection) { int nCount = 0; // different branches for replace in selection or total file if (bUseSelection) { // get starting selection range long lLen = 0; long lStart = GetSelectionStart(); long lEnd = GetSelectionEnd(); // set target to selection SendMessage(SCI_SETTARGETSTART, lStart); SendMessage(SCI_SETTARGETEND, lEnd); // try to find text in target for the first time long lPos = SendMessage(SCI_SEARCHINTARGET, strlen(szFind), (ULONG_PTR)szFind); // loop over selection until end of selection reached - moving the target start each time while (lPos < lEnd && lPos >= 0) { if (m_nSearchflags & SCFIND_REGEXP) {// check for regular expression flag lLen = SendMessage(SCI_REPLACETARGETRE, strlen(szReplace), (ULONG_PTR)szReplace); } else { lLen = SendMessage(SCI_REPLACETARGET, strlen(szReplace), (ULONG_PTR)szReplace); } // the end of the selection was changed - recalc the end lEnd = GetSelectionEnd(); // move start of target behind last change and end of target to new end of selection SendMessage(SCI_SETTARGETSTART, lPos+lLen); SendMessage(SCI_SETTARGETEND, lEnd); // find again - if nothing found loop exits lPos = SendMessage(SCI_SEARCHINTARGET, strlen(szFind), (ULONG_PTR)szFind); nCount++; } } else { // start with first and last char in buffer long lLen = 0; long lStart = 0; long lEnd = SendMessage(SCI_GETTEXTLENGTH, 0, 0); // set target to selection SendMessage(SCI_SETTARGETSTART, lStart, 0); SendMessage(SCI_SETTARGETEND, lEnd, 0); // try to find text in target for the first time long lPos = SendMessage(SCI_SEARCHINTARGET, strlen(szFind), (ULONG_PTR)szFind); // loop over selection until end of selection reached - moving the target start each time while (lPos < lEnd && lPos >= 0) { if (m_nSearchflags & SCFIND_REGEXP) { // check for regular expression flag lLen = SendMessage(SCI_REPLACETARGETRE, strlen(szReplace), (ULONG_PTR)szReplace); } else { lLen = SendMessage(SCI_REPLACETARGET, strlen(szReplace), (ULONG_PTR)szReplace); } // the end of the selection was changed - recalc the end lEnd = SendMessage(SCI_GETTEXTLENGTH, 0, 0);; // move start of target behind last change and end of target to new end of buffer SendMessage(SCI_SETTARGETSTART, lPos+lLen); SendMessage(SCI_SETTARGETEND, lEnd); // find again - if nothing found loop exits lPos = SendMessage(SCI_SEARCHINTARGET, strlen(szFind), (ULONG_PTR)szFind); nCount++; } } return nCount; }
bool ctlSQLBox::BlockComment(bool uncomment) { wxString lineEnd; switch (GetEOLMode()) { case wxSTC_EOL_LF: lineEnd = wxT("\n"); break; case wxSTC_EOL_CRLF: lineEnd = wxT("\r\n"); break; case wxSTC_EOL_CR: lineEnd = wxT("\r"); break; } // Save the start position const wxString comment = wxT("-- "); int start = GetSelectionStart(); if (!GetSelectedText().IsEmpty()) { wxString selection = GetSelectedText(); if (!uncomment) { selection.Replace(lineEnd, lineEnd + comment); selection.Prepend(comment); if (selection.EndsWith(comment)) selection = selection.Left(selection.Length() - comment.Length()); } else { selection.Replace(lineEnd + comment, lineEnd); if (selection.StartsWith(comment)) selection = selection.Right(selection.Length() - comment.Length()); } ReplaceSelection(selection); SetSelection(start, start + selection.Length()); } else { // No text selection - (un)comment the current line int column = GetColumn(start); int curLineNum = GetCurrentLine(); int pos = PositionFromLine(curLineNum); if (!uncomment) { InsertText(pos, comment); } else { wxString t = GetTextRange(pos, pos + comment.Length()); if (t == comment) { // The line starts with a comment, so we remove it SetTargetStart(pos); SetTargetEnd(pos + comment.Length()); ReplaceTarget(wxT("")); } else { // The line doesn't start with a comment, do nothing return false; } } if (GetLineCount() > curLineNum) { wxString nextLine = GetLine(curLineNum + 1); if (nextLine.EndsWith(lineEnd)) nextLine = nextLine.Left(nextLine.Length() - lineEnd.Length()); int nextColumn = (nextLine.Length() < (unsigned int)column ? nextLine.Length() : column); GotoPos(PositionFromLine(curLineNum + 1) + nextColumn); } } return true; }
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; }
void FB_STC::OnCopy ( wxCommandEvent& event ) { if (GetSelectionEnd()-GetSelectionStart() <= 0) return; Copy (); }
void FB_STC::OnCut ( wxCommandEvent& event ) { if (GetReadOnly() || (GetSelectionEnd()-GetSelectionStart() <= 0)) return; Cut (); }