ui/gtk/image.c

Wed, 13 Nov 2024 22:05:52 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 13 Nov 2024 22:05:52 +0100
changeset 81
42f66ebb9881
parent 51
e324291ca9f8
permissions
-rw-r--r--

change binary name to idav

/*
 * 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();
#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
    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) {
    return g->value;
}

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) {
        int width = gdk_pixbuf_get_width(pixbuf);
        int height = gdk_pixbuf_get_height(pixbuf);
        
#if GTK_CHECK_VERSION(4, 0, 0)
        GdkTexture *texture = gdk_texture_new_for_pixbuf(pixbuf);
        gtk_picture_set_paintable(GTK_PICTURE(g->obj), GDK_PAINTABLE(texture));
#else
        gtk_image_set_from_pixbuf(GTK_IMAGE(g->obj), pixbuf);
#endif
        gtk_widget_set_size_request(g->obj, width, height);
    }

    
    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;
}

mercurial