# HG changeset patch # User Olaf Wintermann # Date 1708107450 -3600 # Node ID 3756725aeaf4be156d3760a0137bd8b2bd09bce1 # Parent 24d9a92fd04838ef21115be6729139d2d9c75928 implement new icon API (GTK) diff -r 24d9a92fd048 -r 3756725aeaf4 application/main.c --- a/application/main.c Fri Feb 16 19:00:53 2024 +0100 +++ b/application/main.c Fri Feb 16 19:17:30 2024 +0100 @@ -67,7 +67,7 @@ UiObject *obj = ui_window("Test", NULL); - ui_button(obj, .label = "Test Button", .icon = "folder-documents"); + ui_button(obj, .label = "Test Button", .icon = "application-x-generic"); ui_togglebutton(obj, .label = "Toggle"); ui_checkbox(obj, .label = "Checkbox"); diff -r 24d9a92fd048 -r 3756725aeaf4 ui/gtk/image.c --- a/ui/gtk/image.c Fri Feb 16 19:00:53 2024 +0100 +++ b/ui/gtk/image.c Fri Feb 16 19:17:30 2024 +0100 @@ -59,7 +59,7 @@ } } -// **** new functions **** +// **** deprecated2**** static UiIcon* get_icon(const char *name, int size, int scale) { #ifdef UI_SUPPORTS_SCALE @@ -70,25 +70,52 @@ if(info) { UiIcon *icon = malloc(sizeof(UiIcon)); icon->info = info; + icon->pixbuf = NULL; return icon; } return NULL; } -/* -UiIcon* ui_icon(const char *name, int size) { +UiIcon* ui_icon(const char* name, size_t size) { return get_icon(name, size, ui_get_scalefactor()); } -*/ + +UiIcon* ui_imageicon(const char* file) { + GError *error = NULL; + GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(file, &error); + if(!pixbuf) { + fprintf(stderr, "UiError: Cannot load image: %s\n", file); + return NULL; + } + + UiIcon *icon = malloc(sizeof(UiIcon)); + icon->info = NULL; + icon->pixbuf = pixbuf; + return icon; +} + +void ui_icon_free(UiIcon* icon) { + if(icon->info) { + g_object_unref(icon->info); + } + if(icon->pixbuf) { + g_object_unref(icon->pixbuf); + } + free(icon); +} + +UiIcon* ui_foldericon(size_t size) { + return ui_icon("folder", size); +} + +UiIcon* ui_fileicon(size_t size) { + return ui_icon("application-x-generic", size); +} 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; diff -r 24d9a92fd048 -r 3756725aeaf4 ui/gtk/image.h --- a/ui/gtk/image.h Fri Feb 16 19:00:53 2024 +0100 +++ b/ui/gtk/image.h Fri Feb 16 19:17:30 2024 +0100 @@ -42,6 +42,7 @@ struct UiIcon { GtkIconInfo *info; + GdkPixbuf *pixbuf; }; struct UiImage { diff -r 24d9a92fd048 -r 3756725aeaf4 ui/ui/toolkit.h --- a/ui/ui/toolkit.h Fri Feb 16 19:00:53 2024 +0100 +++ b/ui/ui/toolkit.h Fri Feb 16 19:17:30 2024 +0100 @@ -499,6 +499,7 @@ UIEXPORT void ui_listselection_free(UiListSelection selection); UIEXPORT UiIcon* ui_icon(const char* name, size_t size); +UIEXPORT UiIcon* ui_icon_unscaled(const char *name, int size); UIEXPORT UiIcon* ui_imageicon(const char* file); UIEXPORT void ui_icon_free(UiIcon* icon);