GtkPicture not being displayed inside GtkTextView in GTK4

I am trying to draw a picture inside a text view in GTK4.
I want it to be its full size (natural size), but not larger.
It should be allowed to shrink if the window is too small to fit the whole image.

This code unfortunately always tries to draw the image as small as it’s allowed to, if gtk_widget_set_size_request is not called it will in fact not even be drawn.

GtkWidget *picture;
picture = gtk_picture_new_for_paintable (GDK_PAINTABLE (texture));
if (picture == NULL) {
    g_printerr ("Failed to load image: %s\n", full_image_path);
    return;
}
/* this sets the minimum size, if not set the image will not be displayed at all for some reason */
gtk_widget_set_size_request (picture, 20, 20);

gtk_text_view_add_child_at_anchor (r->ctx->text_view, picture, anchor);
g_print ("Adding child at %s\n", full_image_path);

/* Debug prints */
int width = gtk_widget_get_width (picture);
int height = gtk_widget_get_height (picture);
g_print ("%dx%d\n", width, height);
/* Outputs "0x0" */

/* Debug prints about the image */
int min_w, min_h;
int nat_w, nat_h;
int bmin_w, bmin_h;
int bnat_w, bnat_h;
gtk_widget_measure (picture,
                GTK_ORIENTATION_HORIZONTAL,
                -1,
                &min_w,
                &nat_w,
                &bmin_w,
                &bnat_w);
gtk_widget_measure (picture,
                GTK_ORIENTATION_VERTICAL,
                -1,
                &min_h,
                &nat_h,
                &bmin_h,
                &bnat_h);
g_print("min_w: %d, nat_w: %d, bmin_w: %d, bnat_w: %d\n", min_w, nat_w, bmin_w, bnat_w);
g_print("min_h: %d, nat_h: %d, bmin_h: %d, bnat_h: %d\n", min_h, nat_h, bmin_h, bnat_h);
/*
min_w: 10, nat_w: 780, bmin_w: -1, bnat_w: -1
min_h: 10, nat_h: 1052, bmin_h: -1, bnat_h: -1
*/

I think I might be having the same issue as the following post, but for me it’s not an acceptable workaround in my case to set a fixed size of the image.

Anyone got any clue what might be going on here?

That is not something that GtkTextView supports. As you’ve found out, it always allocates children their min size.

Oh, that’s a shame.

I guess one option to work around it would be to set the minimum size myself to the natural size or smaller when the size of the GtkTextView changes to fake that behaviour?

I guess one option to work around it would be to set the minimum size myself to the natural size or smaller when the size of the GtkTextView changes to fake that behaviour?

That is tricky because you are going to introduce loops by changing child sizing in response to the textview changing (its own size).

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