application/demo_states.c

changeset 1201
fd7dc0716ab6
equal deleted inserted replaced
1200:abb4d3851061 1201:fd7dc0716ab6
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2026 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 "demo_states.h"
30
31 #include <inttypes.h>
32
33 static void *doc1;
34 static void *doc2;
35
36 static void doc1_attachment(UiEvent *event, void *userdata) {
37 if(event->intval) {
38 printf("attach document 1\n");
39 ui_attach_document(event->obj->ctx, doc1);
40 } else {
41 printf("detach document 1\n");
42 ui_detach_document(event->obj->ctx, doc1);
43 }
44 }
45
46 static void doc2_attachment(UiEvent *event, void *userdata) {
47 UiContext *ctx = ui_document_context(doc1);
48 if(event->intval) {
49 printf("attach document 2 to document 1\n");
50 ui_attach_document(ctx, doc2);
51 } else {
52 printf("detach document 2 from document 1\n");
53 ui_detach_document(ctx, doc2);
54 }
55 }
56
57 static void doc_enable_state1(UiEvent *event, void *doc) {
58 UiContext *ctx = ui_document_context(doc);
59 if(event->intval) {
60 ui_set_state(ctx, 1);
61 } else {
62 ui_unset_state(ctx, 1);
63 }
64 }
65
66 static void doc_enable_state2(UiEvent *event, void *doc) {
67 UiContext *ctx = ui_document_context(doc);
68 if(event->intval) {
69 ui_set_state(ctx, 2);
70 } else {
71 ui_unset_state(ctx, 2);
72 }
73 }
74
75 static void doc_enable_state3(UiEvent *event, void *doc) {
76 UiContext *ctx = ui_document_context(doc);
77 if(event->intval) {
78 ui_set_state(ctx, 3);
79 } else {
80 ui_unset_state(ctx, 3);
81 }
82 }
83
84 static void doc_enable_state4(UiEvent *event, void *doc) {
85 UiContext *ctx = ui_document_context(doc);
86 if(event->intval) {
87 ui_set_state(ctx, 4);
88 } else {
89 ui_unset_state(ctx, 4);
90 }
91 }
92
93 static void application_startup(UiEvent *event, void *userdata) {
94 UiObject *obj = ui_window("States Demo");
95
96 doc1 = ui_document_new(8);
97 doc2 = ui_document_new(8);
98
99 ui_hbox(obj, .margin = 10, .spacing = 8) {
100 ui_button(obj, .label = "State 1", .states = UI_STATES(1));
101 ui_button(obj, .label = "State 2", .states = UI_STATES(2));
102 ui_button(obj, .label = "State 3", .states = UI_STATES(3));
103 ui_button(obj, .label = "State 4", .states = UI_STATES(4));
104 ui_button(obj, .label = "State 1,2", .states = UI_STATES(1, 2));
105 ui_button(obj, .label = "State 1,2,3", .states = UI_STATES(1, 2, 3));
106 ui_button(obj, .label = "State 1,2,3,4", .states = UI_STATES(1, 2, 3, 4));
107 }
108
109 ui_frame(obj, .label = "Window", .fill = TRUE, .margin = 10, .subcontainer = UI_CONTAINER_VBOX) {
110 ui_hbox(obj, .margin = 10, .spacing = 8) {
111 ui_togglebutton(obj, .label = "Enable 1", .enable_state = 1);
112 ui_togglebutton(obj, .label = "Enable 2", .enable_state = 2);
113 ui_togglebutton(obj, .label = "Enable 3", .enable_state = 3);
114 ui_togglebutton(obj, .label = "Enable 4", .enable_state = 4);
115 }
116
117 ui_frame(obj, .label = "Doc 1", .fill = TRUE, .margin = 10, .padding = 10, .spacing = 10, .subcontainer = UI_CONTAINER_VBOX) {
118 ui_togglebutton(obj, .label = "Attach", .onchange = doc1_attachment);
119
120 ui_hbox(obj, .margin = 10, .spacing = 8) {
121 ui_togglebutton(obj, .label = "Enable 1", .onchange = doc_enable_state1, .onchangedata = doc1);
122 ui_togglebutton(obj, .label = "Enable 2", .onchange = doc_enable_state2, .onchangedata = doc1);
123 ui_togglebutton(obj, .label = "Enable 3", .onchange = doc_enable_state3, .onchangedata = doc1);
124 ui_togglebutton(obj, .label = "Enable 4", .onchange = doc_enable_state4, .onchangedata = doc1);
125 }
126
127 ui_frame(obj, .label = "Doc 2", .fill = TRUE, .margin = 10, .padding = 10, .spacing = 10, .subcontainer = UI_CONTAINER_VBOX) {
128 ui_togglebutton(obj, .label = "Attach", .onchange = doc2_attachment);
129
130 ui_hbox(obj, .margin = 10, .spacing = 8) {
131 ui_togglebutton(obj, .label = "Enable 1", .onchange = doc_enable_state1, .onchangedata = doc2);
132 ui_togglebutton(obj, .label = "Enable 2", .onchange = doc_enable_state2, .onchangedata = doc2);
133 ui_togglebutton(obj, .label = "Enable 3", .onchange = doc_enable_state3, .onchangedata = doc2);
134 ui_togglebutton(obj, .label = "Enable 4", .onchange = doc_enable_state4, .onchangedata = doc2);
135 }
136 }
137 }
138 }
139
140 ui_show(obj);
141 }
142
143
144 #ifndef UI_WIN32
145
146 int main(int argc, char **argv) {
147 ui_init(NULL, argc, argv);
148 ui_onstartup(application_startup, NULL);
149 ui_main();
150 return 0;
151 }
152
153 #else
154
155 int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
156 //ui_init(NULL, argc, argv);
157 //ui_onstartup(application_startup, NULL);
158 //ui_main();
159 return 0;
160 }
161
162 #endif

mercurial