implement button icon parameter (GTK) newapi

Thu, 15 Feb 2024 21:04:21 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 15 Feb 2024 21:04:21 +0100
branch
newapi
changeset 259
8cce275d2847
parent 258
a252f68c665a
child 260
eebb0626d020

implement button icon parameter (GTK)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/button.c file | annotate | diff | comparison | revisions
--- a/application/main.c	Wed Feb 14 22:00:41 2024 +0100
+++ b/application/main.c	Thu Feb 15 21:04:21 2024 +0100
@@ -67,7 +67,7 @@
     
     UiObject *obj = ui_window("Test", NULL);
     
-    ui_button(obj, .label = "Test Button");
+    ui_button(obj, .label = "Test Button", .icon = "folder-documents");
     ui_togglebutton(obj, .label = "Toggle");
     ui_checkbox(obj, .label = "Checkbox");
     
--- a/ui/gtk/button.c	Wed Feb 14 22:00:41 2024 +0100
+++ b/ui/gtk/button.c	Thu Feb 15 21:04:21 2024 +0100
@@ -35,9 +35,33 @@
 #include "../common/context.h"
 #include "../common/object.h"
 
+static void button_set_icon(GtkWidget *button, const char *icon) {
+    if(!icon) {
+        return;
+    }
+    
+#ifdef UI_GTK4
+    gtk_button_set_icon_name(GTK_BUTTON(button), args.icon);
+#else
+#if GTK_CHECK_VERSION(2, 6, 0)
+    GtkWidget *image = gtk_image_new_from_icon_name(icon, GTK_ICON_SIZE_BUTTON);
+    if(image) {
+        gtk_button_set_image(GTK_BUTTON(button), image);
+    }
+#else
+    // TODO
+#endif
+#endif
+}
+
 UIWIDGET ui_button_create(UiObject *obj, UiButtonArgs args) {
     UiObject* current = uic_current_obj(obj);
-    GtkWidget *button = gtk_button_new_with_label(args.label);
+    GtkWidget *button = gtk_button_new();
+    if(args.label) {
+        gtk_button_set_label(GTK_BUTTON(button), args.label);
+    }
+    button_set_icon(button, args.icon);
+
     
     if(args.onclick) {
         UiEventData *event = malloc(sizeof(UiEventData));
@@ -105,6 +129,7 @@
     if(args.label) {
         gtk_button_set_label(GTK_BUTTON(widget), args.label);
     }
+    button_set_icon(widget, args.icon);
     
     UiVar* var = NULL;
     if (args.value) {

mercurial