Fri, 16 Feb 2024 19:17:30 +0100
implement new icon API (GTK)
application/main.c | file | annotate | diff | comparison | revisions | |
ui/gtk/image.c | file | annotate | diff | comparison | revisions | |
ui/gtk/image.h | file | annotate | diff | comparison | revisions | |
ui/ui/toolkit.h | file | annotate | diff | comparison | revisions |
--- 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");
--- 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;
--- 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 {
--- 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);