add label style option (GTK) newapi

Sun, 27 Oct 2024 08:25:15 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 27 Oct 2024 08:25:15 +0100
branch
newapi
changeset 352
1b4aae4e8432
parent 351
63db7e35e2e9
child 353
ae999e3346a9

add label style option (GTK)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/display.c file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
ui/ui/display.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Fri Oct 25 15:44:39 2024 +0200
+++ b/application/main.c	Sun Oct 27 08:25:15 2024 +0100
@@ -250,7 +250,7 @@
                 ui_newline(obj);
 
                 ui_label(obj, .label = "Label Col 1", .align = UI_ALIGN_LEFT);
-                ui_label(obj, .label = "Label Col 2", .align = UI_ALIGN_RIGHT);
+                ui_label(obj, .label = "Label Col 2", .type = UI_LABEL_STYLE_HEADING, .align = UI_ALIGN_RIGHT);
                 ui_newline(obj);
 
                 ui_spinner(obj, .step = 5);
--- a/ui/gtk/display.c	Fri Oct 25 15:44:39 2024 +0200
+++ b/ui/gtk/display.c	Sun Oct 27 08:25:15 2024 +0100
@@ -35,6 +35,8 @@
 #include "../common/object.h"
 #include "../ui/display.h"
 
+#include <cx/printf.h>
+
 static void set_alignment(GtkWidget *widget, float xalign, float yalign) {
 #if GTK_MAJOR_VERSION >= 4 || (GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 16)
     gtk_label_set_xalign(GTK_LABEL(widget), xalign);
@@ -47,7 +49,37 @@
 UIWIDGET ui_label_create(UiObject *obj, UiLabelArgs args) {
     UiObject* current = uic_current_obj(obj);
     
+    const char *css_class = NULL;
+    char *markup = NULL;
+#if GTK_MAJOR_VERSION < 3
+    switch(args.type && args.label) {
+        case UI_LABEL_STYLE_DEFAULT: break;
+        case UI_LABEL_STYLE_HEADING: {
+            cxmutstr m = cx_asprintf("<b>%s</b>", args.label);
+            markup = m.ptr;
+            args.label = NULL;
+        }
+    }
+#else
+    switch(args.type && args.label) {
+        case UI_LABEL_STYLE_DEFAULT: break;
+        case UI_LABEL_STYLE_HEADING: {
+            css_class = "ui_label_heading";
+            break;
+        }
+    }
+#endif
+    
     GtkWidget *widget = gtk_label_new(args.label);
+    if(markup) {
+        gtk_label_set_markup(GTK_LABEL(widget), markup);
+        free(markup);
+    }
+    
+    if(css_class) {
+        WIDGET_ADD_CSS_CLASS(widget, css_class);
+    }
+    
     switch(args.align) {
         case UI_ALIGN_DEFAULT: break;
         case UI_ALIGN_LEFT: set_alignment(widget, 0, .5); break;
@@ -55,6 +87,7 @@
         case UI_ALIGN_CENTER: break; // TODO
     }
     
+
     UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_STRING);
     if(var) {
         UiString* value = (UiString*)var->value;
--- a/ui/gtk/toolkit.c	Fri Oct 25 15:44:39 2024 +0200
+++ b/ui/gtk/toolkit.c	Sun Oct 27 08:25:15 2024 +0100
@@ -351,6 +351,9 @@
 ".ui_test {\n"
 "  background-color: red;\n"
 "}\n"
+".ui_label_heading {\n"
+"  font-weight: bold;\n"
+"}\n"
 ;
 
 #elif GTK_MAJOR_VERSION == 3
--- a/ui/ui/display.h	Fri Oct 25 15:44:39 2024 +0200
+++ b/ui/ui/display.h	Sun Oct 27 08:25:15 2024 +0100
@@ -48,6 +48,13 @@
 
 typedef enum UiAlignment UiAlignment;
 
+enum UiLabelStyle {
+    UI_LABEL_STYLE_DEFAULT = 0,
+    UI_LABEL_STYLE_HEADING
+};
+
+typedef enum UiLabelStyle UiLabelStyle;
+
 typedef struct UiLabelArgs {
     UiTri fill;
     UiBool hexpand;
@@ -57,6 +64,7 @@
 
     const char* label;
     UiAlignment align;
+    UiLabelType type;
     UiString* value;
     const char* varname;
 } UiLabelArgs;

mercurial