From 07c774c4d45e1670ad2359716104715e8d65ca51 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 20 Apr 2025 16:35:01 +0200 Subject: [PATCH] use GtkPicture instead of GtkImage for embedded images (GTK4) --- application/gtk-image.c | 22 +++++++++++++++++----- application/gtk-image.h | 2 ++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/application/gtk-image.c b/application/gtk-image.c index aea45da..fec2cd2 100644 --- a/application/gtk-image.c +++ b/application/gtk-image.c @@ -32,14 +32,26 @@ #if GTK_MAJOR_VERSION >= 4 GtkWidget* embedded_image_create(GdkPixbuf *pix) { + GtkWidget *grid = gtk_grid_new(); + gtk_widget_add_css_class(grid, "ui_test"); // TODO: replace with new class + GdkTexture *texture = gdk_texture_new_for_pixbuf(pix); - GtkWidget *image = gtk_image_new_from_paintable(GDK_PAINTABLE(texture)); + GtkWidget *picture = gtk_picture_new_for_paintable(GDK_PAINTABLE(texture)); - int width = gdk_texture_get_width(texture); - int height = gdk_texture_get_height(texture); - gtk_widget_set_size_request(image, width, height); + double width = gdk_texture_get_width(texture); + double height = gdk_texture_get_height(texture); - return image; + if(width > EDITOR_IMAGE_MAX_WIDTH) { + height = height * (EDITOR_IMAGE_MAX_WIDTH / width); + width = EDITOR_IMAGE_MAX_WIDTH; + } + + gtk_widget_set_size_request(picture, width, height); + + gtk_grid_attach(GTK_GRID(grid), picture, 0, 0, 1, 1); + + + return grid; } #else diff --git a/application/gtk-image.h b/application/gtk-image.h index af6cb07..a710d75 100644 --- a/application/gtk-image.h +++ b/application/gtk-image.h @@ -33,6 +33,8 @@ #include "note.h" #include "attachment.h" +#define EDITOR_IMAGE_MAX_WIDTH 600 + GtkWidget* embedded_image_create(GdkPixbuf *pix); #endif /* GTK_IMAGE_H */ -- 2.43.5