diff -r 853685152c1d -r dd0ae1c62a72 ui/gtk/image.c --- a/ui/gtk/image.c Sun Nov 12 12:03:50 2017 +0100 +++ b/ui/gtk/image.c Thu Nov 16 12:04:10 2017 +0100 @@ -31,43 +31,106 @@ #include #include "../../ucx/map.h" +#include "toolkit.h" #include "image.h" #include "../common/properties.h" - static UcxMap *image_map; -void ui_image_init() { +static GtkIconTheme *icon_theme; + +void ui_image_init(void) { image_map = ucx_map_new(8); + + icon_theme = gtk_icon_theme_get_default(); +} + +// **** deprecated functions **** + +GdkPixbuf* ui_get_image(char *name) { + UiImage *img = ucx_map_cstr_get(image_map, name); + if(img) { + return img->pixbuf; + } else { + //ui_add_image(name, name); + //return ucx_map_cstr_get(image_map, name); + // TODO + return NULL; + } +} + +// **** new functions **** + +static UiIcon* get_icon(const char *name, int size, int scale) { +#ifdef UI_SUPPORTS_SCALE + GtkIconInfo *info = gtk_icon_theme_lookup_icon_for_scale(icon_theme, name, size, scale, 0); +#else + GtkIconInfo *info = gtk_icon_theme_lookup_icon(icon_theme, name, size, 0); +#endif + if(info) { + UiIcon *icon = malloc(sizeof(UiIcon)); + icon->info = info; + return icon; + } + return NULL; +} + +UiIcon* ui_icon(const char *name, int size) { + return get_icon(name, size, ui_get_scalefactor()); } -void ui_add_image(char *imgname, char *filename) { - // create file path +UiIcon* ui_icon_unscaled(const char *name, int size) { + return get_icon(name, size, 1); +} + +void ui_free_icon(UiIcon *icon) { + g_object_unref(icon->info); + free(icon); +} + +UiImage* ui_icon_image(UiIcon *icon) { + GError *error = NULL; + GdkPixbuf *pixbuf = gtk_icon_info_load_icon(icon->info, &error); + if(pixbuf) { + UiImage *img = malloc(sizeof(UiImage)); + img->pixbuf = pixbuf; + return img; + } + return NULL; +} + +UiImage* ui_image(const char *filename) { + return ui_named_image(filename, NULL); +} + +UiImage* ui_named_image(const char *filename, const char *name) { char *path = uic_get_image_path(filename); if(!path) { - // TODO: error fprintf(stderr, "UiError: pixmaps directory not set\n"); - return; + return NULL; + } + UiImage *img = ui_load_image_from_path(path, name); + free(path); + return img; +} + +UiImage* ui_load_image_from_path(const char *path, const char *name) { + GError *error = NULL; + GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error); + if(!pixbuf) { + fprintf(stderr, "UiError: Cannot load image: %s\n", path); + return NULL; } - GError *error = NULL; - GdkPixbuf *image = gdk_pixbuf_new_from_file(path, &error); - if(!image) { - // TODO: error - fprintf(stderr, "UiError: Cannot load image: %s\n", path); - } else { - ucx_map_cstr_put(image_map, imgname, image); + UiImage *img = malloc(sizeof(UiImage)); + img->pixbuf = pixbuf; + if(name) { + ucx_map_cstr_put(image_map, name, img); } - - free(path); + return img; } -GdkPixbuf* ui_get_image(char *name) { - GdkPixbuf *pixbuf = ucx_map_cstr_get(image_map, name); - if(pixbuf) { - return pixbuf; - } else { - ui_add_image(name, name); - return ucx_map_cstr_get(image_map, name); - } +void ui_free_image(UiImage *img) { + g_object_unref(img->pixbuf); + free(img); }