ui/gtk/image.c

changeset 51
e324291ca9f8
parent 50
9c25e2616bfa
child 81
42f66ebb9881
equal deleted inserted replaced
50:9c25e2616bfa 51:e324291ca9f8
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2017 Olaf Wintermann. All rights reserved. 4 * Copyright 2024 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include <stdio.h> 29 #include "image.h"
30 #include <stdlib.h>
31 #include <string.h>
32 #include <cx/map.h>
33 30
34 #include "toolkit.h" 31 #include "container.h"
35 #include "image.h" 32 #include "../common/context.h"
36 #include "../common/properties.h" 33 #include "../common/object.h"
37 34
38 static CxMap *image_map;
39 35
40 static GtkIconTheme *icon_theme; 36 UIWIDGET ui_imageviewer_create(UiObject *obj, UiImageViewerArgs args) {
41 37 UiObject *current = uic_current_obj(obj);
42 #if GTK_MAJOR_VERSION >= 4 38
43 #define ICONTHEME_GET_DEFAULT() gtk_icon_theme_get_for_display(gdk_display_get_default()) 39 GtkWidget *scrolledwindow = SCROLLEDWINDOW_NEW();
40 #if GTK_CHECK_VERSION(4, 0, 0)
41 GtkWidget *image = gtk_picture_new();
44 #else 42 #else
45 #define ICONTHEME_GET_DEFAULT() gtk_icon_theme_get_default() 43 GtkWidget *image = gtk_image_new();
46 #endif 44 #endif
47
48 void ui_image_init(void) {
49 image_map = cxHashMapCreateSimple(CX_STORE_POINTERS);
50 45
51 icon_theme = ICONTHEME_GET_DEFAULT(); 46 ui_set_name_and_style(image, args.name, args.style_class);
47
48 #if GTK_MAJOR_VERSION < 4
49 GtkWidget *eventbox = gtk_event_box_new();
50 SCROLLEDWINDOW_SET_CHILD(scrolledwindow, eventbox);
51 gtk_container_add(GTK_CONTAINER(eventbox), image);
52 #else
53 SCROLLEDWINDOW_SET_CHILD(scrolledwindow, image);
54 #endif
55
56 UI_APPLY_LAYOUT1(current, args);
57 current->container->add(current->container, scrolledwindow, TRUE);
58
59 UiVar *var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_GENERIC);
60 if(var) {
61 UiGeneric *value = var->value;
62 value->get = ui_imageviewer_get;
63 value->get_type = ui_imageviewer_get_type;
64 value->set = ui_imageviewer_set;
65 value->obj = image;
66 if(value->value && value->type && !strcmp(value->type, UI_IMAGE_OBJECT_TYPE)) {
67 GdkPixbuf *pixbuf = value->value;
68 value->value = NULL;
69 ui_imageviewer_set(value, pixbuf, UI_IMAGE_OBJECT_TYPE);
70 }
71 }
72
73 return scrolledwindow;
52 } 74 }
53 75
54 // **** deprecated functions **** 76 void* ui_imageviewer_get(UiGeneric *g) {
55 77 return g->value;
56 GdkPixbuf* ui_get_image(const char *name) {
57 UiImage *img = cxMapGet(image_map, name);
58 if(img) {
59 return img->pixbuf;
60 } else {
61 //ui_add_image(name, name);
62 //return ucx_map_cstr_get(image_map, name);
63 // TODO
64 return NULL;
65 }
66 } 78 }
67 79
68 // **** deprecated2**** 80 const char* ui_imageviewer_get_type(UiGeneric *g) {
69 81
70 static UiIcon* get_icon(const char *name, int size, int scale) {
71 #if GTK_MAJOR_VERSION >= 4
72 GtkIconPaintable *info = gtk_icon_theme_lookup_icon(icon_theme, name, NULL, size, scale, GTK_TEXT_DIR_LTR, GTK_ICON_LOOKUP_FORCE_REGULAR);
73 #elif defined(UI_SUPPORTS_SCALE)
74 GtkIconInfo *info = gtk_icon_theme_lookup_icon_for_scale(icon_theme, name, size, scale, 0);
75 #else
76 GtkIconInfo *info = gtk_icon_theme_lookup_icon(icon_theme, name, size, 0);
77 #endif
78 if(info) {
79 UiIcon *icon = malloc(sizeof(UiIcon));
80 icon->info = info;
81 icon->pixbuf = NULL;
82 return icon;
83 }
84 return NULL;
85 } 82 }
86 83
87 UiIcon* ui_icon(const char* name, size_t size) { 84 int ui_imageviewer_set(UiGeneric *g, void *value, const char *type) {
88 return get_icon(name, size, ui_get_scalefactor()); 85 if(!type || strcmp(type, UI_IMAGE_OBJECT_TYPE)) {
86 return 1;
87 }
88
89 // TODO: do we need to free the previous value here?
90
91 g->value = value;
92 g->type = type;
93 GdkPixbuf *pixbuf = value;
94
95 if(pixbuf) {
96 int width, height;
97 #if GTK_CHECK_VERSION(4, 12, 0)
98 GdkTexture *texture = gdk_texture_new_for_pixbuf(pixbuf);
99 gtk_picture_set_paintable(GTK_PICTURE(g->obj), GDK_PAINTABLE(texture));
100 width = gdk_texture_get_width(texture);
101 height = gdk_texture_get_height(texture);
102 #else
103 gtk_image_set_from_pixbuf(GTK_IMAGE(g->obj), pixbuf);
104 #endif
105 gtk_widget_set_size_request(g->obj, width, height);
106 }
107
108
109 return 0;
89 } 110 }
90 111
91 UiIcon* ui_imageicon(const char* file) {
92 GError *error = NULL;
93 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(file, &error);
94 if(!pixbuf) {
95 fprintf(stderr, "UiError: Cannot load image: %s\n", file);
96 return NULL;
97 }
98
99 UiIcon *icon = malloc(sizeof(UiIcon));
100 icon->info = NULL;
101 icon->pixbuf = pixbuf;
102 return icon;
103 }
104 112
105 void ui_icon_free(UiIcon* icon) {
106 if(icon->info) {
107 g_object_unref(icon->info);
108 }
109 if(icon->pixbuf) {
110 g_object_unref(icon->pixbuf);
111 }
112 free(icon);
113 }
114 113
115 UiIcon* ui_foldericon(size_t size) { 114 int ui_image_load_file(UiGeneric *obj, const char *path) {
116 return ui_icon("folder", size);
117 }
118
119 UiIcon* ui_fileicon(size_t size) {
120 return ui_icon("file", size);
121 }
122
123 UiIcon* ui_icon_unscaled(const char *name, int size) {
124 return get_icon(name, size, 1);
125 }
126
127 #if GTK_MAJOR_VERSION >= 4
128 GdkPixbuf* ui_icon_pixbuf(UiIcon *icon) {
129 if(!icon->pixbuf) {
130 GFile *file = gtk_icon_paintable_get_file(icon->info);
131 GError *error = NULL;
132 icon->pixbuf = gdk_pixbuf_new_from_file(g_file_get_path(file), &error);
133 }
134 return icon->pixbuf;
135 }
136 #else
137 GdkPixbuf* ui_icon_pixbuf(UiIcon *icon) {
138 if(!icon->pixbuf) {
139 GError *error = NULL;
140 icon->pixbuf = gtk_icon_info_load_icon(icon->info, &error);
141 }
142 return icon->pixbuf;
143 }
144 #endif
145
146 /*
147 UiImage* ui_icon_image(UiIcon *icon) {
148 GError *error = NULL;
149 GdkPixbuf *pixbuf = gtk_icon_info_load_icon(icon->info, &error);
150 if(pixbuf) {
151 UiImage *img = malloc(sizeof(UiImage));
152 img->pixbuf = pixbuf;
153 return img;
154 }
155 return NULL;
156 }
157 */
158
159 /*
160 UiImage* ui_image(const char *filename) {
161 return ui_named_image(filename, NULL);
162 }
163
164 UiImage* ui_named_image(const char *filename, const char *name) {
165 char *path = uic_get_image_path(filename);
166 if(!path) {
167 fprintf(stderr, "UiError: pixmaps directory not set\n");
168 return NULL;
169 }
170 UiImage *img = ui_load_image_from_path(path, name);
171 free(path);
172 return img;
173 }
174
175 UiImage* ui_load_image_from_path(const char *path, const char *name) {
176 GError *error = NULL; 115 GError *error = NULL;
177 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error); 116 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error);
178 if(!pixbuf) { 117 if(!pixbuf) {
179 fprintf(stderr, "UiError: Cannot load image: %s\n", path); 118 return 1;
180 return NULL;
181 } 119 }
182 120
183 UiImage *img = malloc(sizeof(UiImage)); 121 if(obj->set) {
184 img->pixbuf = pixbuf; 122 obj->set(obj, pixbuf, UI_IMAGE_OBJECT_TYPE);
185 if(name) { 123 } else {
186 cxMapPut(image_map, name, img); 124 obj->value = pixbuf;
187 } 125 }
188 return img; 126
127 return 0;
189 } 128 }
190 */
191
192 void ui_free_image(UiImage *img) {
193 g_object_unref(img->pixbuf);
194 free(img);
195 }

mercurial