| 31 #include <cx/printf.h> |
31 #include <cx/printf.h> |
| 32 |
32 |
| 33 #include "args.h" |
33 #include "args.h" |
| 34 #include "container.h" |
34 #include "container.h" |
| 35 #include "widget.h" |
35 #include "widget.h" |
| |
36 #include "var.h" |
| 36 |
37 |
| 37 cxmutstr ui_button_args_to_string(UiContext *ctx, UiButtonArgs *args) { |
38 cxmutstr ui_button_args_to_string(UiContext *ctx, UiButtonArgs *args) { |
| 38 CxBuffer buf; |
39 CxBuffer buf; |
| 39 cxBufferInit(&buf, NULL, 256, ctx->allocator, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS); |
40 cxBufferInit(&buf, NULL, 256, ctx->allocator, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS); |
| 40 |
41 |
| 49 } |
50 } |
| 50 cxBufferPutString(&buf, "}"); |
51 cxBufferPutString(&buf, "}"); |
| 51 return cx_mutstrn(buf.space, buf.size); |
52 return cx_mutstrn(buf.space, buf.size); |
| 52 } |
53 } |
| 53 |
54 |
| 54 cxmutstr ui_toggle_args_to_string(UiContext *ctx, UiToggleArgs *args) { |
55 cxmutstr ui_toggle_args_to_string(UiContext *ctx, UiToggleArgs *args, cxmutstr value) { |
| |
56 CxBuffer buf; |
| |
57 cxBufferInit(&buf, NULL, 256, ctx->allocator, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS); |
| 55 |
58 |
| |
59 cxBufferPutString(&buf, "{"); |
| |
60 |
| |
61 UI_STANDARD_ARGS(&buf, args); |
| |
62 UI_LABEL_ARGS(&buf, args); |
| |
63 if(value.ptr) ui_argstr_add_str(&buf, "value", value.ptr); |
| |
64 // TODO: labeltype, states |
| |
65 |
| |
66 if(buf.size > 0 && buf.space[buf.size-1] == ',') { |
| |
67 buf.space[buf.size-1] = ' '; |
| |
68 } |
| |
69 cxBufferPutString(&buf, "}"); |
| |
70 return cx_mutstrn(buf.space, buf.size); |
| 56 } |
71 } |
| 57 |
72 |
| 58 |
73 |
| 59 UIWIDGET ui_button_create(UiObject *obj, UiButtonArgs *args) { |
74 UIWIDGET ui_button_create(UiObject *obj, UiButtonArgs *args) { |
| 60 UiCallbackWidget *widget = cxZalloc(obj->ctx->allocator, sizeof(UiCallbackWidget)); |
75 UiCallbackWidget *widget = cxZalloc(obj->ctx->allocator, sizeof(UiCallbackWidget)); |
| 78 |
93 |
| 79 cxBufferPutString(&buf, "{"); |
94 cxBufferPutString(&buf, "{"); |
| 80 ui_serialize_type_obj_id(&w->widget, &buf); |
95 ui_serialize_type_obj_id(&w->widget, &buf); |
| 81 cxBufferPutString(&buf, ",\"args\":"); |
96 cxBufferPutString(&buf, ",\"args\":"); |
| 82 cxBufferPutString(&buf, w->widget.args.ptr); |
97 cxBufferPutString(&buf, w->widget.args.ptr); |
| |
98 if(w->widget.var_id.ptr) { |
| |
99 cxBufferPutString(&buf, ",\"value\":\""); |
| |
100 cxBufferPutString(&buf, w->widget.var_id.ptr); |
| |
101 cxBufferPutString(&buf, "\""); |
| |
102 } |
| 83 cxBufferPutString(&buf, "}\n"); |
103 cxBufferPutString(&buf, "}\n"); |
| 84 |
104 |
| 85 return cx_mutstrn(buf.space, buf.size); |
105 return cx_mutstrn(buf.space, buf.size); |
| 86 } |
106 } |
| 87 |
107 |
| 88 static UIWIDGET togglebutton_create(UiObject *obj, const char *type, UiToggleArgs *args) { |
108 static UIWIDGET togglebutton_create(UiObject *obj, const char *type, UiToggleArgs *args) { |
| 89 return NULL; |
109 cxmutstr var_id = cx_mutstrn(NULL, 0); |
| |
110 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_INTEGER); |
| |
111 if(var) { |
| |
112 UiInteger *i = var->value; |
| |
113 if(!i->obj) { |
| |
114 ui_server_bind_int(obj->ctx, i); |
| |
115 } |
| |
116 var_id = cx_mutstr(i->obj); |
| |
117 } |
| |
118 |
| |
119 UiCallbackWidget *widget = cxZalloc(obj->ctx->allocator, sizeof(UiCallbackWidget)); |
| |
120 widget->widget.obj = obj->widget->obj; |
| |
121 widget->widget.type = cx_str(type); |
| |
122 widget->widget.args = ui_toggle_args_to_string(obj->ctx, args, var_id); |
| |
123 widget->widget.var_id = var_id; |
| |
124 widget->widget.var_type = UI_VAR_INTEGER; |
| |
125 widget->widget.serialize = (ui_serialize_func)ui_button_serialize; |
| |
126 widget->callback = args->onchange; |
| |
127 widget->userdata = args->onchangedata; |
| |
128 ui_reg_widget((UiWidget*)widget); |
| |
129 |
| |
130 UiWidget *parent = obj->container_end->container; |
| |
131 cxListAdd(parent->children, widget); |
| |
132 |
| |
133 return (UiWidget*)widget; |
| 90 } |
134 } |
| 91 |
135 |
| 92 UIWIDGET ui_togglebutton_create(UiObject *obj, UiToggleArgs *args) { |
136 UIWIDGET ui_togglebutton_create(UiObject *obj, UiToggleArgs *args) { |
| 93 return togglebutton_create(obj, "togglebutton", args); |
137 return togglebutton_create(obj, "togglebutton", args); |
| 94 } |
138 } |