[gtk-rs] get and set text for GTK3 textview

Hi,
Im trying to find out how to both get and set text in a textview. Im a beginner with both rust and gtk-rs. im using gtk3 bc my distro doesnt have a gtk4 pkg yet.
I want to get/set the text from a textview using functions, for use by other parts of an app.
Ive seen from GtkEntry, that it uses .text() and .set_text(), but textview is different.
Can anyone help, point out examples, or docs that explain how to do it?

Ive looked thru the docs and examples for gtk-rs but all i can find is demo examples that print the text from inside the click-connect. I havent been able work out the steps to get from that, to the functions i want.
I read that i need to get the textbuffer that the textview displays, and use that to get and set the text - but i cant find a single example that shows how to actually do it.
with some guessing i got as far as textview.buffer().text() but the red squiggle says trait bounds are not satisfied, and now im stuck. and i havent started on setting yet.
i expect the gtk-rs docs to provide such examples, but i cant find them.
Thanks for any help.

You can not expect to have examples for all the various language bindings, as the community is tiny and it is hard to find someone who is willing to pay for it. Well Rust has at least Mozilla. And my feeling is, that the Rust GTK bindings may be the best of all the various GTK bindings.

For your task, I would recommend first learning Rust well, and then look for C examples in the GTK repository at gitlab – when the API docs do not give enough info. For the textview, you have to use the textbuffer, and call setText() on it to set a actual text. For Nim we have a tiny textview example at GTK4 for Graphical User Interfaces. You may find similar examples for some other languages. But GtkTextView and GtkSourceView is really not that easy – I wrote a simple Nim text editor six years ago with the low level Nim bindings, and I have since then never managed to convert that one to a new version using the gintro bindings.

Actually, for the GtkTextView Gnome provides a detailed C (and Python) tutorial, see Loading Content From A File.

I started myself with the old version: Building applications: GTK+ 3 Reference Manual many years ago.

gtk.jl provides a nice example of documentation for gtk bindings:
https://juliagraphics.github.io/Gtk.jl/latest/manual/textwidgets/

im hoping for something like this for gtk-rs.
it provides simple examples of how to use the gtk bindings, and makes using gtk.jl so easy.

doesnt gtk-rs have equivalent documentation?
isnt it the case that the people produsing gtk-rs, also design the syntax to use it?
how can i find out that syntax if the designers dont publish it?

Check gtk3-rs/main.rs at master · gtk-rs/gtk3-rs · GitHub . It’s reading a file as string and setting the contents as text to a TextView. You can get the content of the TextView via the contained TextBuffer like you did, but without more details (e.g. your code or the actual compiler error) it’s hard to say what went wrong in your case.

Where did you check for examples? Also, as most code and documentation out there is for C, you can also search for the C function names in the gtk-rs documentation: e.g. gtk_text_buffer_set_text

1 Like

Hi @sdroege,
thanks for your reply.
Yeah, ive seen many C related examples but they werent very helpful for me, ive never learned any C.
i previously looked at the gtk-rs examples, a while ago. now looking at your linked example again, that bit about handling the option was what i needed, thanks for that.

so now i have success in my testing with:
textview.buffer().unwrap().set_text(“display this”);

but i havent yet worked out get_text() / text().

when i:
textview.buffer().text();
the linter says the method exists, but:
note: the following trait bounds were not satisfied:
std::option::Option<gtk::TextBuffer>: glib::IsA<gtk::atk::Text>
which is required by std::option::Option<gtk::TextBuffer>: gtk::prelude::TextExt
`std::option::Optiongtk::TextBuffer:

and thats beyond what i know right now.

Please check the docs. TextBufferExt in gtk::prelude - Rust

The text function takes two TextIter which defines the start/end of the text you want to retrieve from the buffer. If you want to get all the text, you can get the start/end TextIter using bounds method TextBufferExt in gtk::prelude - Rust

let buffer = textview.buffer().unwrap();
let (start, end) = buffer.bounds();
let text = buffer.text(start, end, true);
2 Likes

Also TextViewExt::buffer() returns an Option<TextBuffer>. You need to get the text buffer out of the Option before calling any TextBuffer APIs on it.

1 Like

Thanks @bilelmoussaoui :slight_smile:
thats exactly what i needed.
i just had to add .unwrap() to the end, because text() returns an Option.

let buffer = textview.buffer().unwrap();
let (start, end) = buffer.bounds();
let text = buffer.text(&start, &end, true).unwrap();

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.