]> uap-core.de Git - note.git/commitdiff
refactor gtk text EmbeddedWidget to store a reference to the attachment
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 3 Apr 2025 19:33:35 +0000 (21:33 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 3 Apr 2025 19:33:35 +0000 (21:33 +0200)
application/gtk-text.c
application/gtk-text.h
ui/gtk/image.c
ui/ui/image.h

index 8d9777bade374f55f0d98eaeaa588f9fa785a984..588773bd819489ccdf3f189f64f9757a48c17871 100644 (file)
@@ -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);
index c9f9bc2d26cd6bd7c29633f59f59779d9d166a20..60de7bfcc1a416de7399ff26b8b6ac720401c93b 100644 (file)
@@ -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);
index 04cb75c4818e374cc1614c0d6b47dc90ab6eca8f..6a49665c801b3b35fa7e4a699131e3352efae288 100644 (file)
@@ -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(
index b0a9e91de256d8758abcf54954175d1cdfe9f932..0d53d8f464a6576d9c81194b83c942ef62a3c472 100644 (file)
@@ -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