How do I cut, copy, paste and select all in a Text

2019-04-10 15:43发布

问题:

I'm trying to cut, copy, paste and select all with the TextView Gtk control. Why the TextView control? Cause I can't seem to get the bloody TextEditor control to go multiline!

Anyway... How do I:

Cut text from the TextView control?

Copy text from the TextView control?

Paste text into the TextView control?

Select All text in the TextView control?


Here's what I've tried: 1 hour of googling.

and this:

TextView tv = ...;
TextIter start, end;
if (tv.Buffer.GetSelectionBounds(start, end)) {
  String selected = tv.Buffer.GetText(start, end);
  Clipboard clipboard = tv.GetClipboard(Gdk.Selection.Clipboard);
  clipboard.Text = selected;
}

from: https://stackoverflow.com/questions/26308501/gtk-textview-copy-and-paste - but that obviously doesn't work (hence my question).

I've also found this: http://docs.go-mono.com/?link=T%3aGtk.TextView The Mono GTK C# docs. There is so much stuff that just seems to be non-existent.

回答1:

Basically you should work with Underlying TextBuffer from your TextView.

Selection

To Cut, Copy and Paste, First we should select the part we intended to Copy (or check and see if the buffer already has some selection or not), to select a part we should get an Iterator of type TextIter from buffer, here is how we can do it:

Here is an example for SelectAll:

var start = textview.Buffer.GetIterAtOffset (0);

var end = textview.Buffer.GetIterAtOffset (0);
end.ForwardToEnd ();

textview.Buffer.SelectRange (start, end);

Here is an example is for selecting range [2,4] from text:

var start = textview.Buffer.GetIterAtOffset (0);
start.ForwardChars (2);

var end = textview.Buffer.GetIterAtOffset (0);
end.ForwardChars (4);

textview.Buffer.SelectRange (start, end);

TextIter have extensive methods for range selection for example ForwardChars() has a twin method BackwardChars().

To check if our TextBuffer has any selection we should use HasSelection Property:

var hasSelection = textview.Buffer.HasSelection;

Working with Clipboard

Now that we have a selected text we can simply use it with Clipboard actions.

Here is a sample for Cutting selected range [2,4]:

 var clipboard = textview.GetClipboard (Gdk.Selection.Clipboard);

var start = textview.Buffer.GetIterAtOffset (0);
start.ForwardChars (2);

var end = textview.Buffer.GetIterAtOffset (0);
end.ForwardChars (4);

textview.Buffer.SelectRange (start, end);

textview.Buffer.CutClipboard (clipboard, true);

Copying is very similar to Cutting we should only replace CutClipboard with CopyClipboard :

Here is a sample for Copying selected range [2,4]:

 var clipboard = textview.GetClipboard (Gdk.Selection.Clipboard);

var start = textview.Buffer.GetIterAtOffset (0);
start.ForwardChars (2);

var end = textview.Buffer.GetIterAtOffset (0);
end.ForwardChars (4);

textview.Buffer.SelectRange (start, end);

textview.Buffer.CopyClipboard (clipboard, true);

and finally Pasting something from clipboard is very similar to Cutting/Copying

Here is an example of Pasting some text from Clipboard to location 0:

var pasteLocation=textview.Buffer.GetIterAtOffset (0);
textview.Buffer.SelectRange (pasteLocation, pasteLocation);

textview.Buffer.PasteClipboard (clipboard);

Final Example:

As a final example, we set text to 123456 and then cut 34 from it and paste it at the beginning, the final text should be like 341256 :

void TextViewSample ()
{
    textview.Buffer.Text = "123456";
    var clipboard = textview.GetClipboard (Gdk.Selection.Clipboard);
    var start = textview.Buffer.GetIterAtOffset (0);
    start.ForwardChars (2);
    var end = textview.Buffer.GetIterAtOffset (0);
    end.ForwardChars (4);
    textview.Buffer.SelectRange (start, end);
    var hasSelection = textview.Buffer.HasSelection;
    textview.Buffer.CutClipboard (clipboard, true);
    var pasteLocation = textview.Buffer.GetIterAtOffset (0);
    textview.Buffer.SelectRange (pasteLocation, pasteLocation);
    textview.Buffer.PasteClipboard (clipboard);
} 


回答2:

you can use some buttons for do this!actually button1(copy),button2(paste) and etc then you can read user input (TextField**.Text**) and then put it in a array after that you can show your array for output(paste) and also for copy/paste selected parts of inputs(not all),you can store indexes(from which index until end index are selected) and read/write data between that indexes(selected index) and put user input in array and Enjoy it! i'm not a c# Programmer,but i try to make you understand what you should to do.i hope so good luck