ui/gtk/image.c

branch
newapi
changeset 339
b68b5f984074
equal deleted inserted replaced
338:6d827f85e0f5 339:b68b5f984074
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 "../common/context.h"
33 #include "../common/object.h"
34
35
36 UIWIDGET ui_imageviewer_create(UiObject *obj, UiImageViewerArgs args) {
37 UiObject *current = uic_current_obj(obj);
38
39 GtkWidget *scrolledwindow = SCROLLEDWINDOW_NEW();
40 GtkWidget *image = gtk_image_new();
41
42 #if GTK_MAJOR_VERSION < 4
43 GtkWidget *eventbox = gtk_event_box_new();
44 SCROLLEDWINDOW_SET_CHILD(scrolledwindow, event_box);
45 gtk_container_add(GTK_CONTAINER(eventbox), image);
46 #else
47 SCROLLEDWINDOW_SET_CHILD(scrolledwindow, image);
48 #endif
49
50 UI_APPLY_LAYOUT1(current, args);
51 current->container->add(current->container, scrolledwindow, TRUE);
52
53 UiVar *var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_GENERIC);
54 if(var) {
55 UiGeneric *value = var->value;
56 value->get = ui_imageviewer_get;
57 value->get_type = ui_imageviewer_get_type;
58 value->set = ui_imageviewer_set;
59 value->obj = image;
60 if(value->value && value->type && !strcmp(value->type, UI_IMAGE_OBJECT_TYPE)) {
61 GdkPixbuf *pixbuf = value->value;
62 value->value = NULL;
63 ui_imageviewer_set(value, pixbuf, UI_IMAGE_OBJECT_TYPE);
64 }
65 }
66
67 return scrolledwindow;
68 }
69
70 void* ui_imageviewer_get(UiGeneric *g) {
71
72 }
73
74 const char* ui_imageviewer_get_type(UiGeneric *g) {
75
76 }
77
78 int ui_imageviewer_set(UiGeneric *g, void *value, const char *type) {
79 if(!type || strcmp(type, UI_IMAGE_OBJECT_TYPE)) {
80 return 1;
81 }
82
83 // TODO: do we need to free the previous value here?
84
85 g->value = value;
86 g->type = type;
87 GdkPixbuf *pixbuf = value;
88
89 if(pixbuf) {
90 #if GTK_CHECK_VERSION(4, 12, 0)
91 GdkTexture *texture = gdk_texture_new_for_pixbuf(pixbuf);
92 gtk_image_set_from_paintable(GTK_IMAGE(g->obj), GDK_PAINTABLE(texture));
93 #else
94 gtk_image_set_from_pixbuf(GTK_IMAGE(g->obj), pixbuf);
95 #endif
96 }
97
98
99 return 0;
100 }
101
102
103
104 int ui_image_load_file(UiGeneric *obj, const char *path) {
105 GError *error = NULL;
106 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error);
107 if(!pixbuf) {
108 return 1;
109 }
110
111 if(obj->set) {
112 obj->set(obj, pixbuf, UI_IMAGE_OBJECT_TYPE);
113 } else {
114 obj->value = pixbuf;
115 }
116
117 return 0;
118 }

mercurial