Respond to changes in a tagged GtkTextBuffer

What is the proper way to handle changes to a GtkTextBuffer that is tagged.

Until now, I just connected the “changed” signal and used _get_text, but with tags, which in my case are clickable URLs, the text I get is (obviously) bare without any tags.

The only way I see at the moment is to add marks to all the tags so that I can identify where the tagged pieces were and used TagTable to recreate the tagged GtkTextBuffer?

I guess it’s just coding but it seems like I am missing something. Going over the bridge for water. A lot of work for adding or removing a single character.

Can you explain further what you are trying to achieve?

I have an editable text buffer with tags that are eventually handled as links, eventually leading to calls to uri_launcher. This works fine (thanks to hypertext.c in gtk4-demo).

When this buffer is changed, those tags and other link-rekaled functionality is lost, or at least no longer connected the current content, because the current handler calls get_text to get the new content which is no longer tagged.

I create the text buffer from XML which, if there is a <a href= and markup creates the tags. Eventually, I need to recreate this text structure because it is going back to disk but that is another story.

Right now I think I’ll mark the text with the (named) tags and recover the tags and iters in a gtk_tags_table_foreach(), but it’s seems like a lot of “work” for each single character that will result I a call to the handler. I just hoped there is some built-in solution that handles this that I am just not aware of.

I really apologize, but I still don’t understand. What do you mean by “When this buffer is changed”? Because the behavior of tags that I know of is to remain active and working until the text they affect is deleted. It doesn’t matter if text is removed or changed, as long as there is at least one character of originally tagged text the tag will work.

Now, if you are talking about saving the file to disk, then the tags will not actually be saved, the text is saved as pure text. To maintain tags and other elements it will be necessary to serialize them and the text and then save this as a structured file of some type (XML for example).

I went through this problem myself.

That is an interesting reply indeed. So my error is probably caused by using gtk_text_buffer_get_text in the callback. I shouldn’t mess with the buffer until I want to replace its content entirely. Thank you. That was the answer I was looking for.

“When the buffer is changed” is just me saying that the “changed” signal is connected to the buffer and each time I type a letter, the callback get called. If this is indeed the problem, it might solve my problem with typing in <a tags. Right now, they enter only through the XML file parsing but I would like to be able to enter them manually into the text buffer.

Thanks for taking the time answer. Much appreciated.

1 Like

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