Serialize TextChildAnchors of a TextBuffer with GTK4

I wonder if it is somehow possible to implement some way of serializing widgets that are inserted into a GtkTextBuffer, so that they can be copy and pasted with the typical primary/clipboard clipboards (and potentially DnD, now that they share common implementations).
Unlike widgets, pictures can actually be copied application internally. Copying them to an external application (that would require actually serializing the pictures) doesn’t work either.

(can be replicated with gtk4-demo’s “Multiple Views”)

GTK used to have the methods gtk_text_buffer_register_serialize_format and gtk_text_buffer_register_deserialize_format that allowed serializing the buffer for clipboard transfer, but they aren’t available for GTK4 anymore and the migration docs don’t tell me more than

Note: Support for rich text serialization across different processes for GtkTextBuffer is not available any more.

without telling me how to work around it.

I also don’t understand why serialization support has been removed from GtkTextBuffer, apart from the GTK team to have less code to adapt and to maintain.

I guess it would need to be re-implemented, either externally or adding it back to GTK itself.

BTW, if it was just to have less code for the GTK team to maintain, in my opinion it’s a symptom that GTK has become too big, and finer-grained modules would be a better way of organizing code (even if at the end the modules are aggregated and statically linked to a broader gtk platform library).

Because, sometimes maintainers don’t scale (it’s a recurrent topic for Linux kernel development).

Or, conversely, have a workflow for GTK development that would be inspired by the Linux kernel. So have more sub-maintainers, and coarse-grained merge requests, with a merge window. The coarse-grained reviews would be less on the details, and more on the overall design/architecture.

Edit: if this would be written on a mailing list, I would have changed the subject of the email, but on Discourse, unable to fork the conversation I suppose.

Serialization now happens using content providers, which makes it a lot easier to add your own than it had been previously with GTK 3.

You can call gdk_content_register_serializer() and gdk_content_register_deserializer() for your own custom implementations (which could easily include imaging data, should you prefer). You would want to register the type with GTK_TYPE_TEXT_BUFFER as the GType and things should just start working with your text view.

I imagine that having a builtin RTF serializer for GTK would be useful, so long as it implements those interfaces.

this works in theory, but not here. It fails with widgets, gtk creates a new buffer (a Gtk.TextBuffer, instead a copy of itself [relevant when subclassed]) and writes the selected area of the buffer into the copy with gtk_text_buffer_insert_range (See create_clipboard_contents_buffer in gtktextbuffer.c). This however means, that any widgets are dropped from the copy.

Also registering the type against GTK_TYPE_TEXT_BUFFER is very bad in my case, since this is a library. That’d mean that once called, every Gtk.TextBuffer (subclass) that is not mine will be broken. I haven’t tested it yet, but I assume another is will be the deserializer function, that will need a pointer to the Gtk.TextView to insert the widgets into the buffer, which would make it impossible to use more than one TextView/Buffer.

Indeed. I’d suggest using your own subclass of GtkTextBuffer and a serializer you control and a GtkTextView subclass you control. You can deserialize into the new buffer with markup to be processed later, and then expand that when attaching the buffer to a view so you can anchor new image widgets.

That might work. However I still don’t know how to address my inability to serialize widgets with that method, since they aren’t copied.

You cannot serialise generic widgets, because widgets have no idea how to serialise themselves.

If you want to serialise a GtkTextBuffer with widgets inside it, you have to know what kind of widgets you added and you have to know how to represent them. GTK3 also does not deal with widgets inside a text buffer, only pixbufs.

Yes, I’m aware of that.

But I was talking about “specific widgets”. For saving files, I have a “Serializable” interface implemented the widgets I want to have serialized, which provides a method to serialize the widgets as a markdown image string.
The buffer “serialize” method just goes over the buffer until it hits such a widget, inserts the text before that, then serializes the widget and does the same til the buffer ends. (MdNotebook/mdnotebookview.c at master - MdNotebook -

And at least for now, I’d like to reuse that for copy/paste as much as possible, without reimplementing the entire copy/cut/paste virtual-methods of the view.

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