public static bool ReplaceSelection(this Gtk.TextBuffer buffer, string newText, bool keepSelection = true) { bool retval = false; TextIter start; TextIter end; if (buffer.GetSelectionBounds(out start, out end)) { TextMark startMark = buffer.CreateMark("start", start, false); TextMark endMark = buffer.CreateMark("end", end, true); buffer.DeleteSelection(true, true); TextIter start2 = buffer.GetIterAtMark(startMark); buffer.Insert(ref start2, newText); if (keepSelection) { TextIter start3 = buffer.GetIterAtMark(startMark); TextIter end3 = buffer.GetIterAtMark(endMark); buffer.SelectRange(start3, end3); } retval = true; } return(retval); }
public static string GetSelectedText(this Gtk.TextBuffer buffer, out TextIter start, out TextIter end) { string retval = string.Empty; if (buffer.GetSelectionBounds(out start, out end)) { retval = buffer.GetText(start, end, true); } return(retval); }
static void CopyBufferToClipboard (TextBuffer buf) { //get current cursor state TextIter s, e; TextIter cursorIter = TextIter.Zero; var hadSel = buf.GetSelectionBounds (out s, out e); if (!hadSel) { cursorIter = buf.GetIterAtOffset (buf.CursorPosition); } //copy text to clipboard, let the buffer handle the details buf.SelectRange (buf.StartIter, buf.EndIter); Clipboard clipboard = Clipboard.Get (Mono.TextEditor.ClipboardActions.CopyOperation.CLIPBOARD_ATOM); buf.CopyClipboard (clipboard); //restore cursor state if (hadSel) { buf.SelectRange (s, e); } else { buf.PlaceCursor (cursorIter); } }