implement toggle button (Client)

Thu, 04 Dec 2025 19:04:08 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 04 Dec 2025 19:04:08 +0100
changeset 948
94bc57d63128
parent 947
a335a12936ce
child 949
ef8f13c8c08f

implement toggle button (Client)

client/args.c file | annotate | diff | comparison | revisions
client/args.h file | annotate | diff | comparison | revisions
client/uiclient.c file | annotate | diff | comparison | revisions
client/uiclient.h file | annotate | diff | comparison | revisions
--- a/client/args.c	Wed Dec 03 18:45:05 2025 +0100
+++ b/client/args.c	Thu Dec 04 19:04:08 2025 +0100
@@ -232,3 +232,40 @@
     
     return args;
 }
+
+UiToggleArgs* json2toggle_args(const CxJsonValue *value) {
+    UiToggleArgs *args = ui_toggle_args_new();
+    if(value->type != CX_JSON_OBJECT) {
+        return args;
+    }
+    
+    init_common_args(value, args, &button_args);
+    init_groups(value, args, (argfunc_set_intarray)ui_button_args_set_groups);
+    
+    CxJsonValue *val = cxJsonObjGet(value, "label");
+    if(val && val->type == CX_JSON_STRING) {
+        ui_toggle_args_set_label(args, val->value.string.ptr);
+    }
+    
+    val = cxJsonObjGet(value, "icon");
+    if(val && val->type == CX_JSON_STRING) {
+        ui_toggle_args_set_icon(args, val->value.string.ptr);
+    }
+    
+    val = cxJsonObjGet(value, "tooltip");
+    if(val && val->type == CX_JSON_STRING) {
+        ui_toggle_args_set_tooltip(args, val->value.string.ptr);
+    }
+    
+    val = cxJsonObjGet(value, "labeltype");
+    if(val && val->type == CX_JSON_INTEGER) {
+        ui_toggle_args_set_labeltype(args, (int)val->value.integer);
+    }
+    
+    val = cxJsonObjGet(value, "enable_group");
+    if(val && val->type == CX_JSON_INTEGER) {
+        ui_toggle_args_set_enablegroup(args, (int)val->value.integer);
+    }
+    
+    return args;
+}
--- a/client/args.h	Wed Dec 03 18:45:05 2025 +0100
+++ b/client/args.h	Thu Dec 04 19:04:08 2025 +0100
@@ -64,6 +64,8 @@
 
 UiButtonArgs* json2button_args(const CxJsonValue *value);
 
+UiToggleArgs* json2toggle_args(const CxJsonValue *value);
+
 
 #ifdef __cplusplus
 }
--- a/client/uiclient.c	Wed Dec 03 18:45:05 2025 +0100
+++ b/client/uiclient.c	Thu Dec 04 19:04:08 2025 +0100
@@ -50,6 +50,7 @@
     cxMapPut(msg_types, "grid", msg_vbox);
     cxMapPut(msg_types, "end", msg_end);
     cxMapPut(msg_types, "button", msg_button);
+    cxMapPut(msg_types, "toggle", msg_togglebutton);
     
     objects = cxHashMapCreateSimple(CX_STORE_POINTERS);
 }
@@ -252,3 +253,47 @@
     
     return 0;
 }
+
+int msg_togglebutton(UiObject *parent, const CxJsonValue *value) {
+    CxJsonValue *args_value = cxJsonObjGet(value, "args");
+    cxmutstr id = jsonobj_getstring(value, "id");
+    if(!id.ptr) {
+        return 1;
+    }
+    UiObject *obj = get_msg_obj(parent, value);
+    if(!obj) {
+        return 1;
+    }
+    
+    CxJsonValue *button_type = cxJsonObjGet(value, "button_type");
+    if(!button_type || button_type->type != CX_JSON_INTEGER) {
+        return 1;
+    }
+    
+    CxJsonValue *val = cxJsonObjGet(value, "value");
+    UiInteger *i = NULL;
+    if(val && val->type == CX_JSON_STRING) {
+        i = ui_int_new(obj->ctx, val->value.string.ptr);
+    }
+    
+    UiToggleArgs *args = json2toggle_args(args_value);
+    UIWIDGET w;
+    switch(button_type->value.integer) {
+        default: {
+            w = ui_togglebutton_create(obj, args);
+            break;
+        }
+        case 1: {
+            w = ui_checkbox_create(obj, args);
+            break;
+        }
+        case 2: {
+            w = ui_switch_create(obj, args);
+            break;
+        }
+    }
+    ui_toggle_args_free(args);
+    client_reg_widget(obj, id, w);
+    
+    return 0;
+}
--- a/client/uiclient.h	Wed Dec 03 18:45:05 2025 +0100
+++ b/client/uiclient.h	Thu Dec 04 19:04:08 2025 +0100
@@ -71,6 +71,8 @@
 
 int msg_button(UiObject *parent, const CxJsonValue *value);
 
+int msg_togglebutton(UiObject *parent, const CxJsonValue *value);
+
 
 
 

mercurial