ui/gtk/image.c

Thu, 21 Nov 2024 12:04:53 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 21 Nov 2024 12:04:53 +0100
branch
newapi
changeset 388
473c03f85197
parent 381
b47bda6666ce
child 389
d15eca5fd8b3
permissions
-rw-r--r--

add menu builder API

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

mercurial