add minimally working image viewer (GTK) newapi

Sun, 13 Oct 2024 21:28:09 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 13 Oct 2024 21:28:09 +0200
branch
newapi
changeset 339
b68b5f984074
parent 338
6d827f85e0f5
child 340
2dd42bd4fe5d

add minimally working image viewer (GTK)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/button.c file | annotate | diff | comparison | revisions
ui/gtk/icon.h file | annotate | diff | comparison | revisions
ui/gtk/image.c file | annotate | diff | comparison | revisions
ui/gtk/image.h file | annotate | diff | comparison | revisions
ui/gtk/list.c file | annotate | diff | comparison | revisions
ui/gtk/objs.mk file | annotate | diff | comparison | revisions
ui/gtk/toolbar.c file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
ui/ui/image.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Oct 13 19:41:12 2024 +0200
+++ b/application/main.c	Sun Oct 13 21:28:09 2024 +0200
@@ -43,6 +43,7 @@
     UiList *menulist;
     UiInteger *radio;
     UiInteger *tabview;
+    UiGeneric *image;
 } MyDocument;
 
 MyDocument *doc1;
@@ -56,14 +57,16 @@
 
 void action_file_selected(UiEvent *event, void *userdata) {
     UiFileList *files = event->eventdata;
+    MyDocument *doc = event->document;
     printf("files: %d\n", (int)files->nfiles);
     if(files->nfiles > 0) {
         printf("selected file: %s\n", files->files[0]);
+        ui_image_load_file(doc->image, files->files[0]);
     }
 }
 
 void action_button(UiEvent *event, void *userdata) {
-    ui_savefiledialog(event->obj, "myfile.txt", action_file_selected, NULL);
+    ui_openfiledialog(event->obj, UI_FILEDIALOG_SELECT_SINGLE, action_file_selected, NULL);
 }
 
 void action_switch(UiEvent *event, void *userdata) {
@@ -106,6 +109,7 @@
     ui_list_append(doc->list, "test3");
     doc->radio = ui_int_new(docctx, "radio");
     doc->tabview = ui_int_new(docctx, "tabview");
+    doc->image = ui_generic_new(docctx, "image");
     //doc->text = ui_text_new(docctx, "text");
     return doc;
 }
@@ -225,6 +229,10 @@
         ui_tab(obj, "Tab 4") {
             ui_textarea(obj, .varname = "text");
         }
+        ui_tab(obj, "Tab 5") {
+            ui_button(obj, .label = "Test Button", .icon = "application-x-generic", .onclick = action_button);
+            ui_imageviewer(obj, .varname = "image");
+        }
     }
     
     /*
--- a/ui/gtk/button.c	Sun Oct 13 19:41:12 2024 +0200
+++ b/ui/gtk/button.c	Sun Oct 13 21:28:09 2024 +0200
@@ -401,8 +401,8 @@
                 event);
     }
     
-    UiContainer *ct = uic_get_current_container(obj);
-    ct->add(ct, rbutton, FALSE);
+    UI_APPLY_LAYOUT1(current, args);
+    current->container->add(current->container, rbutton, FALSE);
     
     return rbutton;
 }
--- a/ui/gtk/icon.h	Sun Oct 13 19:41:12 2024 +0200
+++ b/ui/gtk/icon.h	Sun Oct 13 21:28:09 2024 +0200
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2017 Olaf Wintermann. All rights reserved.
+ * Copyright 2024 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:
@@ -29,7 +29,7 @@
 #ifndef ICON_H
 #define	ICON_H
 
-#include "../ui/icon.h"
+#include "../ui/icons.h"
 
 #ifdef	__cplusplus
 extern "C" {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/image.c	Sun Oct 13 21:28:09 2024 +0200
@@ -0,0 +1,118 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2024 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 "image.h"
+
+#include "container.h"
+#include "../common/context.h"
+#include "../common/object.h"
+
+
+UIWIDGET ui_imageviewer_create(UiObject *obj, UiImageViewerArgs args) {
+    UiObject *current = uic_current_obj(obj);
+    
+    GtkWidget *scrolledwindow = SCROLLEDWINDOW_NEW();
+    GtkWidget *image = gtk_image_new();
+    
+#if GTK_MAJOR_VERSION < 4
+    GtkWidget *eventbox = gtk_event_box_new();
+    SCROLLEDWINDOW_SET_CHILD(scrolledwindow, event_box);
+    gtk_container_add(GTK_CONTAINER(eventbox), image);
+#else
+    SCROLLEDWINDOW_SET_CHILD(scrolledwindow, image);
+#endif
+    
+    UI_APPLY_LAYOUT1(current, args);
+    current->container->add(current->container, scrolledwindow, TRUE);
+    
+    UiVar *var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_GENERIC);
+    if(var) {
+        UiGeneric *value = var->value;
+        value->get = ui_imageviewer_get;
+        value->get_type = ui_imageviewer_get_type;
+        value->set = ui_imageviewer_set;
+        value->obj = image;
+        if(value->value && value->type && !strcmp(value->type, UI_IMAGE_OBJECT_TYPE)) {
+            GdkPixbuf *pixbuf = value->value;
+            value->value = NULL;
+            ui_imageviewer_set(value, pixbuf, UI_IMAGE_OBJECT_TYPE);
+        }
+    }
+    
+    return scrolledwindow;
+}
+
+void* ui_imageviewer_get(UiGeneric *g) {
+    
+}
+
+const char* ui_imageviewer_get_type(UiGeneric *g) {
+    
+}
+
+int ui_imageviewer_set(UiGeneric *g, void *value, const char *type) {
+    if(!type || strcmp(type, UI_IMAGE_OBJECT_TYPE)) {
+        return 1;
+    }
+    
+    // TODO: do we need to free the previous value here?
+    
+    g->value = value;
+    g->type = type;
+    GdkPixbuf *pixbuf = value;
+    
+    if(pixbuf) {
+#if GTK_CHECK_VERSION(4, 12, 0)
+        GdkTexture *texture = gdk_texture_new_for_pixbuf(pixbuf);
+        gtk_image_set_from_paintable(GTK_IMAGE(g->obj), GDK_PAINTABLE(texture));
+#else
+        gtk_image_set_from_pixbuf(GTK_IMAGE(g->obj), pixbuf);
+#endif
+    }
+
+    
+    return 0;
+}
+
+
+
+int ui_image_load_file(UiGeneric *obj, const char *path) {
+    GError *error = NULL;
+    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error);
+    if(!pixbuf) {
+        return 1;
+    }
+    
+    if(obj->set) {
+        obj->set(obj, pixbuf, UI_IMAGE_OBJECT_TYPE);
+    } else {
+        obj->value = pixbuf;
+    }
+    
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/image.h	Sun Oct 13 21:28:09 2024 +0200
@@ -0,0 +1,49 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2024 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 IMAGE_H
+#define IMAGE_H
+
+#include "../ui/image.h"
+#include "toolkit.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+void* ui_imageviewer_get(UiGeneric *g);
+const char* ui_imageviewer_get_type(UiGeneric *g);
+int ui_imageviewer_set(UiGeneric *g, void *value, const char *type);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* IMAGE_H */
+
--- a/ui/gtk/list.c	Sun Oct 13 19:41:12 2024 +0200
+++ b/ui/gtk/list.c	Sun Oct 13 21:28:09 2024 +0200
@@ -36,7 +36,7 @@
 #include "container.h"
 
 #include "list.h"
-#include "image.h"
+#include "icon.h"
 
 
 void* ui_strmodel_getvalue(void *elm, int column) {
--- a/ui/gtk/objs.mk	Sun Oct 13 19:41:12 2024 +0200
+++ b/ui/gtk/objs.mk	Sun Oct 13 21:28:09 2024 +0200
@@ -40,6 +40,7 @@
 GTKOBJ += text.o
 GTKOBJ += list.o
 GTKOBJ += image.o
+GTKOBJ += icon.o
 GTKOBJ += graphics.o
 GTKOBJ += range.o
 GTKOBJ += entry.o
--- a/ui/gtk/toolbar.c	Sun Oct 13 19:41:12 2024 +0200
+++ b/ui/gtk/toolbar.c	Sun Oct 13 21:28:09 2024 +0200
@@ -33,7 +33,7 @@
 #include "toolbar.h"
 #include "menu.h"
 #include "button.h"
-#include "image.h"
+#include "icon.h"
 #include "list.h"
 #include <cx/mempool.h>
 #include <cx/hash_map.h>
--- a/ui/gtk/toolkit.c	Sun Oct 13 19:41:12 2024 +0200
+++ b/ui/gtk/toolkit.c	Sun Oct 13 21:28:09 2024 +0200
@@ -33,7 +33,7 @@
 
 #include "toolkit.h"
 #include "toolbar.h"
-#include "image.h"
+#include "icon.h"
 #include "../common/document.h"
 #include "../common/properties.h"
 #include "../common/menu.h"
--- a/ui/ui/image.h	Sun Oct 13 19:41:12 2024 +0200
+++ b/ui/ui/image.h	Sun Oct 13 21:28:09 2024 +0200
@@ -35,6 +35,8 @@
 extern "C" {
 #endif
 
+#define UI_IMAGE_OBJECT_TYPE "image"
+    
 typedef struct UiImageViewerArgs {
     UiTri fill;
     UiBool hexpand;
@@ -45,6 +47,7 @@
     const char *style_class;
 
     UiBool scrollarea;
+    UiBool autoscale;
     UiGeneric *value;
     const char *varname;
 } UiImageViewerArgs;
@@ -53,6 +56,8 @@
     
 UIEXPORT UIWIDGET ui_imageviewer_create(UiObject *obj, UiImageViewerArgs args);
 
+int ui_image_load_file(UiGeneric *obj, const char *path);
+
 #ifdef __cplusplus
 }
 #endif
--- a/ui/ui/toolkit.h	Sun Oct 13 19:41:12 2024 +0200
+++ b/ui/ui/toolkit.h	Sun Oct 13 21:28:09 2024 +0200
@@ -329,7 +329,7 @@
 struct UiGeneric {
     void* (*get)(UiGeneric*);
     const char* (*get_type)(UiGeneric*);
-    void (*set)(UiGeneric*, void *, const char *type);
+    int (*set)(UiGeneric*, void *, const char *type);
     void *obj;
     
     void *value;

mercurial