Search code examples
gtksignalseditoredit

How to Properly Detect User Edits Properly with GTK Text Buffers?


In an attempt to implement a text editor with simple highlighting functionalities using the text view widget in GTK, I find myself in need of pinning down -- or at least narrowing down -- the section of the text whose highlight needs to be adjusted, instead of having to re-highlight everything each time I modify something.

To this end, I would hope to be able to obtain enough information, upon every user edit, to determine: (1) in the case of insertion of some text (whether it be the insertion of a single character from a keyboard press or a bunch of text from pasting), the position at which the insertion happens and what the inserted content is; (2) in the case of deletion, the range of text being deleted; (3) and in the case where a new piece of text has replaced an old range, which is a deletion followed immediately by an insertion, everything in both case (1) and (2).

Now, I have just come to understand that GTK uses events to handle user inputs, but the official documentation of gtk seems to be littered with "no description available". In the documentation I do come across certain signals, scattered in TextView and TextBuffer, whose names seem relevant, but none of them provide enough information, they seem to be able to just inform you of the occurrence of an edit but hardly anything more. Is there any feasible way to obtain information of the sort said in the preceding paragraph with GTK Text View or GTK Text Buffer, or GTK in general?

Btw I am just beginning to delve into the C programming language and GTK (installed on my pc is gtk4), any form of help is appreciated!


Solution

  • It's been a while.

    For insertion: https://docs.gtk.org/gtk3/vfunc.TextBuffer.insert_text.html

    For deletion: https://docs.gtk.org/gtk3/vfunc.TextBuffer.delete_range.html

    These signals should offer the information sufficient to know everything about an edit.