From: Olaf Wintermann Date: Sat, 5 Apr 2025 19:10:24 +0000 (+0200) Subject: move text embedded image creation to separate file X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=dd37ce8908a60ce22f5f9c0b137c1f0e983ae93b;p=note.git move text embedded image creation to separate file --- diff --git a/application/gtk-image.c b/application/gtk-image.c new file mode 100644 index 0000000..94cba30 --- /dev/null +++ b/application/gtk-image.c @@ -0,0 +1,58 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2025 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "gtk-image.h" + + +#if GTK_MAJOR_VERSION >= 4 + +GtkWidget* embedded_image_create(GdkPixbuf *pix) { + GdkTexture *texture = gdk_texture_new_for_pixbuf(pixbuf); + GtkWidget *image = gtk_image_new_from_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); + + return image; +} + +#else + +GtkWidget* embedded_image_create(GdkPixbuf *pix) { + GtkWidget *image = gtk_image_new_from_pixbuf(pix); + + int width = gdk_pixbuf_get_width(pix); + int height = gdk_pixbuf_get_height(pix); + gtk_widget_set_size_request(image, width, height); + + return image; +} + + +#endif \ No newline at end of file diff --git a/application/gtk-image.h b/application/gtk-image.h new file mode 100644 index 0000000..af6cb07 --- /dev/null +++ b/application/gtk-image.h @@ -0,0 +1,39 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2025 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef GTK_IMAGE_H +#define GTK_IMAGE_H + +#include "application.h" +#include "note.h" +#include "attachment.h" + +GtkWidget* embedded_image_create(GdkPixbuf *pix); + +#endif /* GTK_IMAGE_H */ + diff --git a/application/gtk-text.c b/application/gtk-text.c index 91d2c9e..43f755c 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -27,6 +27,7 @@ */ #include "gtk-text.h" +#include "gtk-image.h" #include "editor.h" #include "note.h" #include "attachment.h" @@ -289,13 +290,7 @@ static void editor_attach_image(NoteEditor *editor, GdkPixbuf *pixbuf, char *att gtk_text_buffer_get_iter_at_mark(buffer, &iter, cursor); GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(buffer, &iter); - GdkTexture *texture = gdk_texture_new_for_pixbuf(pixbuf); - GtkWidget *image = gtk_image_new_from_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); - + GtkWidget *image = embedded_image_create(pixbuf); gtk_text_view_add_child_at_anchor(textview, image, anchor); // remember widget and store a reference in the textbuffer diff --git a/configure b/configure index 32aae07..3238d8c 100755 --- a/configure +++ b/configure @@ -967,6 +967,7 @@ checkopt_toolkit_gtk4() TOOLKIT = gtk GTKOBJ = draw_cairo.o APP_PLATFORM_SRC = gtk-text.c +APP_PLATFORM_SRC += gtk-image.c __EOF__ return 0 } @@ -987,6 +988,7 @@ TOOLKIT = gtk GTKOBJ = draw_cairo.o GTKOBJ = draw_cairo.o APP_PLATFORM_SRC = gtk-text.c +APP_PLATFORM_SRC += gtk-image.c __EOF__ return 0 } diff --git a/make/project.xml b/make/project.xml index 2977368..0efbc90 100644 --- a/make/project.xml +++ b/make/project.xml @@ -163,6 +163,7 @@ TOOLKIT = gtk GTKOBJ = draw_cairo.o APP_PLATFORM_SRC = gtk-text.c + APP_PLATFORM_SRC += gtk-image.c gtk3,webkit2gtk4 @@ -170,6 +171,7 @@ GTKOBJ = draw_cairo.o GTKOBJ = draw_cairo.o APP_PLATFORM_SRC = gtk-text.c + APP_PLATFORM_SRC += gtk-image.c cocoa