Using TextView anchors for Picture widgets and limiting their size to the view

I’m working on a simple Markdown Browser that I’m porting to D using giD. The intent is to use it as help navigator for markdown formatted content which can be easily integrated into Gtk4 giD applications.

It is based on TextView and uses Picture objects anchored to the TextBuffer in order to fit the images to the view if they exceed the current allocated width.

One common issue I’ve had with Gtk4, which I’ve seen many others mention, is that there doesn’t appear to be an easy way to signal when a Widget’s allocation changes when creating composite widgets, such as in language bindings which lack the ability to create native GObject classes.

As you can see here, I ended up using the “page-size” property of the horizontal Adjustment for the ScrolledWindow that the TextView is in, in order to know when the width changes. While this does work, it seems like a bit of a hack and I wanted to see if anyone had a better idea of how to handle this.

I understand that containers are often sized based on their contents. But in the case of overlay widgets, like with TextView anchors, this doesn’t seem to apply. In fact it seems as if anchored widgets aren’t allocated any area by default and the requested width/height must be specified. It would be nice if there was a clean way to be notified when the container changes, in order to possibly adjust such contents so they don’t flow outside of the allocated area. Even just having a read only allocatedWidth and allocatedHeight property would have made this possible. Though I’m beginning to understand that this was done somewhat intentionally to prevent abuse, there seem to me to be legitimate reasons to want to resize children based on their parent and to be able to do this in a way which doesn’t require creating a native GObject Widget class.

I started looking at ConstraintLayout, which seemed like perhaps a great way to connect the width of the TextView to Picture widgets inside of it. The more I researched layout managers though, the more it sounded like something for use with native Widgets only. Maybe I’m wrong?

Any ideas or tips to help me better understand Gtk4 in this regard would be appreciated. I am entertaining the idea of adding support to giD for creating D classes which are registered as native C classes at some point. Which makes me wonder what other language bindings have achieved this. Technically this should be possible now with giD and the C API functions/types which the bindings are based on, but that kind of decreases the convenience of using D.

Hi,

I suggest to create your own widget class that inherits from GtkTextView, then override the Gtk.Widget.measure vfunc to resize the picture to the received width then chain-up the resize to the parent textview class.

That said, because the textview is scrollable there may be extra complexity to consider…