UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2024 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "image.h" 30 31 #include "container.h" 32 #include "menu.h" 33 #include "../common/context.h" 34 #include "../common/object.h" 35 36 37 UIWIDGET ui_imageviewer_create(UiObject *obj, UiImageViewerArgs args) { 38 UiObject *current = uic_current_obj(obj); 39 40 GtkWidget *scrolledwindow = SCROLLEDWINDOW_NEW(); 41 #if GTK_CHECK_VERSION(4, 0, 0) 42 GtkWidget *image = gtk_picture_new(); 43 #else 44 GtkWidget *image = gtk_image_new(); 45 #endif 46 47 ui_set_name_and_style(image, args.name, args.style_class); 48 49 #if GTK_MAJOR_VERSION < 4 50 GtkWidget *eventbox = gtk_event_box_new(); 51 SCROLLEDWINDOW_SET_CHILD(scrolledwindow, eventbox); 52 gtk_container_add(GTK_CONTAINER(eventbox), image); 53 #else 54 SCROLLEDWINDOW_SET_CHILD(scrolledwindow, image); 55 GtkWidget *eventbox = image; 56 #endif 57 58 UI_APPLY_LAYOUT1(current, args); 59 current->container->add(current->container, scrolledwindow, TRUE); 60 61 UiVar *var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_GENERIC); 62 if(var) { 63 UiGeneric *value = var->value; 64 value->get = ui_imageviewer_get; 65 value->get_type = ui_imageviewer_get_type; 66 value->set = ui_imageviewer_set; 67 value->obj = image; 68 if(value->value && value->type && !strcmp(value->type, UI_IMAGE_OBJECT_TYPE)) { 69 GdkPixbuf *pixbuf = value->value; 70 value->value = NULL; 71 ui_imageviewer_set(value, pixbuf, UI_IMAGE_OBJECT_TYPE); 72 } 73 } 74 75 if(args.contextmenu) { 76 UIMENU menu = ui_contextmenu_create(args.contextmenu, obj, eventbox); 77 ui_widget_set_contextmenu(eventbox, menu); 78 } 79 80 return scrolledwindow; 81 } 82 83 void* ui_imageviewer_get(UiGeneric *g) { 84 return g->value; 85 } 86 87 const char* ui_imageviewer_get_type(UiGeneric *g) { 88 89 } 90 91 int ui_imageviewer_set(UiGeneric *g, void *value, const char *type) { 92 if(!type || strcmp(type, UI_IMAGE_OBJECT_TYPE)) { 93 return 1; 94 } 95 96 // TODO: do we need to free the previous value here? 97 98 g->value = value; 99 g->type = type; 100 GdkPixbuf *pixbuf = value; 101 102 if(pixbuf) { 103 int width = gdk_pixbuf_get_width(pixbuf); 104 int height = gdk_pixbuf_get_height(pixbuf); 105 106 #if GTK_CHECK_VERSION(4, 0, 0) 107 GdkTexture *texture = gdk_texture_new_for_pixbuf(pixbuf); 108 gtk_picture_set_paintable(GTK_PICTURE(g->obj), GDK_PAINTABLE(texture)); 109 #else 110 gtk_image_set_from_pixbuf(GTK_IMAGE(g->obj), pixbuf); 111 #endif 112 gtk_widget_set_size_request(g->obj, width, height); 113 } 114 115 116 return 0; 117 } 118 119 120 121 int ui_image_load_file(UiGeneric *obj, const char *path) { 122 GError *error = NULL; 123 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error); 124 if(!pixbuf) { 125 return 1; 126 } 127 128 if(obj->set) { 129 obj->set(obj, pixbuf, UI_IMAGE_OBJECT_TYPE); 130 } else { 131 obj->value = pixbuf; 132 } 133 134 return 0; 135 } 136