| |
1 /* |
| |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| |
3 * |
| |
4 * Copyright 2025 Olaf Wintermann. All rights reserved. |
| |
5 * |
| |
6 * Redistribution and use in source and binary forms, with or without |
| |
7 * modification, are permitted provided that the following conditions are met: |
| |
8 * |
| |
9 * 1. Redistributions of source code must retain the above copyright |
| |
10 * notice, this list of conditions and the following disclaimer. |
| |
11 * |
| |
12 * 2. Redistributions in binary form must reproduce the above copyright |
| |
13 * notice, this list of conditions and the following disclaimer in the |
| |
14 * documentation and/or other materials provided with the distribution. |
| |
15 * |
| |
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| |
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| |
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| |
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| |
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| |
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| |
26 * POSSIBILITY OF SUCH DAMAGE. |
| |
27 */ |
| |
28 |
| |
29 #include "button.h" |
| |
30 #include <cx/buffer.h> |
| |
31 #include <cx/printf.h> |
| |
32 |
| |
33 #include "args.h" |
| |
34 #include "container.h" |
| |
35 |
| |
36 cxmutstr ui_button_args_to_string(UiContext *ctx, UiButtonArgs *args) { |
| |
37 CxBuffer buf; |
| |
38 cxBufferInit(&buf, NULL, 256, ctx->allocator, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS); |
| |
39 |
| |
40 cxBufferPutString(&buf, "{"); |
| |
41 |
| |
42 UI_STANDARD_ARGS(&buf, args); |
| |
43 UI_LABEL_ARGS(&buf, args); |
| |
44 // TODO: labeltype, states |
| |
45 |
| |
46 if(buf.size > 0 && buf.space[buf.size-1] == ',') { |
| |
47 buf.space[buf.size-1] = ' '; |
| |
48 } |
| |
49 cxBufferPutString(&buf, "}"); |
| |
50 return cx_mutstrn(buf.space, buf.size); |
| |
51 } |
| |
52 |
| |
53 cxmutstr ui_toggle_args_to_string(UiContext *ctx, UiToggleArgs *args) { |
| |
54 |
| |
55 } |
| |
56 |
| |
57 |
| |
58 UIWIDGET ui_button_create(UiObject *obj, UiButtonArgs *args) { |
| |
59 UiCallbackWidget *widget = cxZalloc(obj->ctx->allocator, sizeof(UiCallbackWidget)); |
| |
60 widget->widget.obj = obj->widget->obj; |
| |
61 widget->widget.args = ui_button_args_to_string(obj->ctx, args); |
| |
62 widget->widget.serialize = (ui_serialize_func)ui_button_serialize; |
| |
63 widget->callback = args->onclick; |
| |
64 widget->userdata = args->onclickdata; |
| |
65 ui_reg_widget((UiWidget*)widget); |
| |
66 |
| |
67 UiWidget *parent = obj->container_end->container; |
| |
68 cxListAdd(parent->children, widget); |
| |
69 |
| |
70 return (UiWidget*)widget; |
| |
71 } |
| |
72 |
| |
73 cxmutstr ui_button_serialize(UiCallbackWidget *w) { |
| |
74 CxBuffer buf; |
| |
75 cxBufferInit(&buf, NULL, 1024, NULL, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS); |
| |
76 |
| |
77 cxBufferPutString(&buf, "{\"type\":\"button\", \"obj\":\""); |
| |
78 cxBufferPutString(&buf, w->widget.obj->id.ptr); |
| |
79 cxBufferPutString(&buf, "\", \"id\":\""); |
| |
80 cxBufferPutString(&buf, w->widget.id.ptr); |
| |
81 cxBufferPutString(&buf, "\", \"args\":"); |
| |
82 cxBufferPutString(&buf, w->widget.args.ptr); |
| |
83 cxBufferPutString(&buf, "}\n"); |
| |
84 |
| |
85 return cx_mutstrn(buf.space, buf.size); |
| |
86 } |
| |
87 |
| |
88 static UIWIDGET togglebutton_create(UiObject *obj, const char *type, UiToggleArgs *args) { |
| |
89 return NULL; |
| |
90 } |
| |
91 |
| |
92 UIWIDGET ui_togglebutton_create(UiObject *obj, UiToggleArgs *args) { |
| |
93 return togglebutton_create(obj, "togglebutton", args); |
| |
94 } |
| |
95 |
| |
96 UIWIDGET ui_checkbox_create(UiObject *obj, UiToggleArgs *args) { |
| |
97 return togglebutton_create(obj, "checkbox", args); |
| |
98 } |
| |
99 |
| |
100 UIWIDGET ui_switch_create(UiObject *obj, UiToggleArgs *args) { |
| |
101 return togglebutton_create(obj, "switch", args); |
| |
102 } |
| |
103 |
| |
104 UIWIDGET ui_radiobutton_create(UiObject *obj, UiToggleArgs *args) { |
| |
105 return togglebutton_create(obj, "radiobutton", args); |
| |
106 } |