From 971380f8dba8b35107ccddf16003de8b9dff8b74 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Thu, 3 Apr 2025 21:33:35 +0200 Subject: [PATCH] refactor gtk text EmbeddedWidget to store a reference to the attachment --- application/gtk-text.c | 7 +++---- application/gtk-text.h | 2 -- ui/gtk/image.c | 13 ++++++++++++- ui/ui/image.h | 10 ++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/application/gtk-text.c b/application/gtk-text.c index 8d9777b..588773b 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -265,7 +265,8 @@ static gboolean editor_attach_file(NoteEditor *editor, const char *path) { } void md_serialize_image(EmbeddedWidget *em, CxBuffer *out) { - cx_bprintf(out, "![image](%s)", em->name); + Attachment *attachment = em->data1; + cx_bprintf(out, "![image](%s)", attachment->name); } static void editor_attach_image(NoteEditor *editor, GdkPixbuf *pixbuf, char *attachment_path) { @@ -302,9 +303,7 @@ static void editor_attach_image(NoteEditor *editor, GdkPixbuf *pixbuf, char *att EmbeddedWidget *em = malloc(sizeof(EmbeddedWidget)); em->widget = image; em->anchor = anchor; - //em->name = attachment_path ? strdup(util_resource_name(attachment_path)) : NULL; - //em->path = attachment_path; - em->data1 = pixbuf; + em->data1 = attachment; em->data2 = NULL; em->serialize = md_serialize_image; g_object_ref(image); diff --git a/application/gtk-text.h b/application/gtk-text.h index c9f9bc2..60de7bf 100644 --- a/application/gtk-text.h +++ b/application/gtk-text.h @@ -59,8 +59,6 @@ typedef struct EmbeddedWidget EmbeddedWidget; struct EmbeddedWidget { GtkWidget *widget; GtkTextChildAnchor *anchor; - char *name; - char *path; void *data1; void *data2; void (*serialize)(EmbeddedWidget *e, CxBuffer *out); diff --git a/ui/gtk/image.c b/ui/gtk/image.c index 04cb75c..6a49665 100644 --- a/ui/gtk/image.c +++ b/ui/gtk/image.c @@ -125,6 +125,7 @@ UIWIDGET ui_imageviewer_create(UiObject *obj, UiImageViewerArgs args) { GdkPixbuf *pixbuf = value->value; value->value = NULL; ui_imageviewer_set(value, pixbuf, UI_IMAGE_OBJECT_TYPE); + g_object_unref(pixbuf); } } @@ -299,6 +300,7 @@ int ui_imageviewer_set(UiGeneric *g, void *value, const char *type) { } GdkPixbuf *pixbuf = value; + g_object_ref(pixbuf); UiImageViewer *imgviewer = g->obj; g->value = pixbuf; @@ -331,13 +333,22 @@ int ui_image_load_file(UiGeneric *obj, const char *path) { if(obj->set) { obj->set(obj, pixbuf, UI_IMAGE_OBJECT_TYPE); + g_object_unref(pixbuf); } else { obj->value = pixbuf; } - + return 0; } +void ui_image_ref(UIIMAGE img) { + g_object_ref(img); +} + +void ui_image_unref(UIIMAGE img) { + g_object_unref(img); +} + #if GTK_MAJOR_VERSION >= 4 gboolean ui_imageviewer_scroll( diff --git a/ui/ui/image.h b/ui/ui/image.h index b0a9e91..0d53d8f 100644 --- a/ui/ui/image.h +++ b/ui/ui/image.h @@ -37,6 +37,13 @@ extern "C" { #define UI_IMAGE_OBJECT_TYPE "image" +#ifdef UI_GTK +#define UIIMAGE GdkPixbuf* +#else +#define UIIMAGE void* +#endif + + typedef struct UiImageViewerArgs { UiTri fill; UiBool hexpand; @@ -79,6 +86,9 @@ UIEXPORT UIWIDGET ui_imageviewer_set_useradjustable(UIWIDGET w, UiBool set); UIEXPORT int ui_image_load_file(UiGeneric *obj, const char *path); +UIEXPORT void ui_image_ref(UIIMAGE img); +UIEXPORT void ui_image_unref(UIIMAGE img); + #ifdef __cplusplus } #endif -- 2.43.5