ui/gtk/image.c

changeset 51
e324291ca9f8
parent 50
9c25e2616bfa
--- a/ui/gtk/image.c	Sun Oct 06 18:43:06 2024 +0200
+++ b/ui/gtk/image.c	Sun Oct 20 21:24:13 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:
@@ -26,170 +26,103 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <cx/map.h>
-
-#include "toolkit.h"
 #include "image.h"
-#include "../common/properties.h"
-
-static CxMap *image_map;
-
-static GtkIconTheme *icon_theme;
 
-#if GTK_MAJOR_VERSION >= 4
-#define ICONTHEME_GET_DEFAULT() gtk_icon_theme_get_for_display(gdk_display_get_default())
-#else
-#define ICONTHEME_GET_DEFAULT() gtk_icon_theme_get_default()
-#endif
+#include "container.h"
+#include "../common/context.h"
+#include "../common/object.h"
 
-void ui_image_init(void) {
-    image_map = cxHashMapCreateSimple(CX_STORE_POINTERS);
-    
-    icon_theme = ICONTHEME_GET_DEFAULT();
-}
-
-// **** deprecated functions ****
 
-GdkPixbuf* ui_get_image(const char *name) {
-    UiImage *img = cxMapGet(image_map, name);
-    if(img) {
-        return img->pixbuf;
-    } else {
-        //ui_add_image(name, name);
-        //return ucx_map_cstr_get(image_map, name);
-        // TODO
-        return NULL;
-    }
-}
-
-// **** deprecated2****
-
-static UiIcon* get_icon(const char *name, int size, int scale) {
-#if GTK_MAJOR_VERSION >= 4
-    GtkIconPaintable *info = gtk_icon_theme_lookup_icon(icon_theme, name, NULL, size, scale, GTK_TEXT_DIR_LTR, GTK_ICON_LOOKUP_FORCE_REGULAR);
-#elif defined(UI_SUPPORTS_SCALE)
-    GtkIconInfo *info = gtk_icon_theme_lookup_icon_for_scale(icon_theme, name, size, scale, 0);
+UIWIDGET ui_imageviewer_create(UiObject *obj, UiImageViewerArgs args) {
+    UiObject *current = uic_current_obj(obj);
+    
+    GtkWidget *scrolledwindow = SCROLLEDWINDOW_NEW();
+#if GTK_CHECK_VERSION(4, 0, 0)
+    GtkWidget *image = gtk_picture_new();
+#else
+    GtkWidget *image = gtk_image_new();
+#endif
+    
+    ui_set_name_and_style(image, args.name, args.style_class);
+    
+#if GTK_MAJOR_VERSION < 4
+    GtkWidget *eventbox = gtk_event_box_new();
+    SCROLLEDWINDOW_SET_CHILD(scrolledwindow, eventbox);
+    gtk_container_add(GTK_CONTAINER(eventbox), image);
 #else
-    GtkIconInfo *info = gtk_icon_theme_lookup_icon(icon_theme, name, size, 0);
+    SCROLLEDWINDOW_SET_CHILD(scrolledwindow, image);
 #endif
-    if(info) {
-        UiIcon *icon = malloc(sizeof(UiIcon));
-        icon->info = info;
-        icon->pixbuf = NULL;
-        return icon;
-    }
-    return NULL;
-}
-
-UiIcon* ui_icon(const char* name, size_t size) {
-    return get_icon(name, size, ui_get_scalefactor());
-}
-
-UiIcon* ui_imageicon(const char* file) {
-    GError *error = NULL;
-    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(file, &error);
-    if(!pixbuf) {
-        fprintf(stderr, "UiError: Cannot load image: %s\n", file);
-        return NULL;
+    
+    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);
+        }
     }
     
-    UiIcon *icon = malloc(sizeof(UiIcon));
-    icon->info = NULL;
-    icon->pixbuf = pixbuf;
-    return icon;
+    return scrolledwindow;
 }
 
-void ui_icon_free(UiIcon* icon) {
-    if(icon->info) {
-        g_object_unref(icon->info);
-    }
-    if(icon->pixbuf) {
-        g_object_unref(icon->pixbuf);
-    }
-    free(icon);
+void* ui_imageviewer_get(UiGeneric *g) {
+    return g->value;
 }
 
-UiIcon* ui_foldericon(size_t size) {
-    return ui_icon("folder", size);
-}
-
-UiIcon* ui_fileicon(size_t size) {
-    return ui_icon("file", size);
-}
-
-UiIcon* ui_icon_unscaled(const char *name, int size) {
-    return get_icon(name, size, 1);
+const char* ui_imageviewer_get_type(UiGeneric *g) {
+    
 }
 
-#if GTK_MAJOR_VERSION >= 4
-GdkPixbuf* ui_icon_pixbuf(UiIcon *icon) {
-    if(!icon->pixbuf) {
-        GFile *file = gtk_icon_paintable_get_file(icon->info);
-        GError *error = NULL;
-        icon->pixbuf = gdk_pixbuf_new_from_file(g_file_get_path(file), &error);
+int ui_imageviewer_set(UiGeneric *g, void *value, const char *type) {
+    if(!type || strcmp(type, UI_IMAGE_OBJECT_TYPE)) {
+        return 1;
     }
-    return icon->pixbuf;
-}
+    
+    // TODO: do we need to free the previous value here?
+    
+    g->value = value;
+    g->type = type;
+    GdkPixbuf *pixbuf = value;
+    
+    if(pixbuf) {
+        int width, height;
+#if GTK_CHECK_VERSION(4, 12, 0)
+        GdkTexture *texture = gdk_texture_new_for_pixbuf(pixbuf);
+        gtk_picture_set_paintable(GTK_PICTURE(g->obj), GDK_PAINTABLE(texture));
+        width = gdk_texture_get_width(texture);
+        height = gdk_texture_get_height(texture);
 #else
-GdkPixbuf* ui_icon_pixbuf(UiIcon *icon) {
-    if(!icon->pixbuf) {
-        GError *error = NULL;
-        icon->pixbuf = gtk_icon_info_load_icon(icon->info, &error);
-    }
-    return icon->pixbuf;
-}
+        gtk_image_set_from_pixbuf(GTK_IMAGE(g->obj), pixbuf);
 #endif
+        gtk_widget_set_size_request(g->obj, width, height);
+    }
 
-/*
-UiImage* ui_icon_image(UiIcon *icon) {
-    GError *error = NULL;
-    GdkPixbuf *pixbuf = gtk_icon_info_load_icon(icon->info, &error);
-    if(pixbuf) {
-        UiImage *img = malloc(sizeof(UiImage));
-        img->pixbuf = pixbuf;
-        return img;
-    }
-    return NULL;
-}
-*/
-
-/*
-UiImage* ui_image(const char *filename) {
-    return ui_named_image(filename, NULL);
+    
+    return 0;
 }
 
-UiImage* ui_named_image(const char *filename, const char *name) {
-    char *path =  uic_get_image_path(filename);
-    if(!path) {
-        fprintf(stderr, "UiError: pixmaps directory not set\n");
-        return NULL;
-    }
-    UiImage *img = ui_load_image_from_path(path, name);
-    free(path);
-    return img;
-}
+
 
-UiImage* ui_load_image_from_path(const char *path, const char *name) {
+int ui_image_load_file(UiGeneric *obj, const char *path) {
     GError *error = NULL;
     GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error);
     if(!pixbuf) {
-        fprintf(stderr, "UiError: Cannot load image: %s\n", path);
-        return NULL;
+        return 1;
     }
     
-    UiImage *img = malloc(sizeof(UiImage));
-    img->pixbuf = pixbuf;
-    if(name) {
-        cxMapPut(image_map, name, img);
+    if(obj->set) {
+        obj->set(obj, pixbuf, UI_IMAGE_OBJECT_TYPE);
+    } else {
+        obj->value = pixbuf;
     }
-    return img;
+    
+    return 0;
 }
-*/
-
-void ui_free_image(UiImage *img) {
-    g_object_unref(img->pixbuf);
-    free(img);
-}

mercurial