application/demo_states.c

changeset 1201
fd7dc0716ab6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/application/demo_states.c	Mon Jun 15 21:13:05 2026 +0200
@@ -0,0 +1,162 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2026 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "demo_states.h"
+
+#include <inttypes.h>
+
+static void *doc1;
+static void *doc2;
+
+static void doc1_attachment(UiEvent *event, void *userdata) {
+    if(event->intval) {
+        printf("attach document 1\n");
+        ui_attach_document(event->obj->ctx, doc1);
+    } else {
+        printf("detach document 1\n");
+        ui_detach_document(event->obj->ctx, doc1);
+    }
+}
+
+static void doc2_attachment(UiEvent *event, void *userdata) {
+    UiContext *ctx = ui_document_context(doc1);
+    if(event->intval) {
+        printf("attach document 2 to document 1\n");
+        ui_attach_document(ctx, doc2);
+    } else {
+        printf("detach document 2 from document 1\n");
+        ui_detach_document(ctx, doc2);
+    }
+}
+
+static void doc_enable_state1(UiEvent *event, void *doc) {
+    UiContext *ctx = ui_document_context(doc);
+    if(event->intval) {
+        ui_set_state(ctx, 1);
+    } else {
+        ui_unset_state(ctx, 1);
+    }
+}
+
+static void doc_enable_state2(UiEvent *event, void *doc) {
+    UiContext *ctx = ui_document_context(doc);
+    if(event->intval) {
+        ui_set_state(ctx, 2);
+    } else {
+        ui_unset_state(ctx, 2);
+    }
+}
+
+static void doc_enable_state3(UiEvent *event, void *doc) {
+    UiContext *ctx = ui_document_context(doc);
+    if(event->intval) {
+        ui_set_state(ctx, 3);
+    } else {
+        ui_unset_state(ctx, 3);
+    }
+}
+
+static void doc_enable_state4(UiEvent *event, void *doc) {
+    UiContext *ctx = ui_document_context(doc);
+    if(event->intval) {
+        ui_set_state(ctx, 4);
+    } else {
+        ui_unset_state(ctx, 4);
+    }
+}
+
+static void application_startup(UiEvent *event, void *userdata) {
+    UiObject *obj = ui_window("States Demo");
+    
+    doc1 = ui_document_new(8);
+    doc2 = ui_document_new(8);
+    
+    ui_hbox(obj, .margin = 10, .spacing = 8) {
+        ui_button(obj, .label = "State 1", .states = UI_STATES(1));
+        ui_button(obj, .label = "State 2", .states = UI_STATES(2));
+        ui_button(obj, .label = "State 3", .states = UI_STATES(3));
+        ui_button(obj, .label = "State 4", .states = UI_STATES(4));
+        ui_button(obj, .label = "State 1,2", .states = UI_STATES(1, 2));
+        ui_button(obj, .label = "State 1,2,3", .states = UI_STATES(1, 2, 3));
+        ui_button(obj, .label = "State 1,2,3,4", .states = UI_STATES(1, 2, 3, 4));
+    }
+    
+    ui_frame(obj, .label = "Window", .fill = TRUE, .margin = 10, .subcontainer = UI_CONTAINER_VBOX) {
+        ui_hbox(obj, .margin = 10, .spacing = 8) {
+            ui_togglebutton(obj, .label = "Enable 1", .enable_state = 1);
+            ui_togglebutton(obj, .label = "Enable 2", .enable_state = 2);
+            ui_togglebutton(obj, .label = "Enable 3", .enable_state = 3);
+            ui_togglebutton(obj, .label = "Enable 4", .enable_state = 4);
+        }
+        
+        ui_frame(obj, .label = "Doc 1", .fill = TRUE, .margin = 10, .padding = 10, .spacing = 10, .subcontainer = UI_CONTAINER_VBOX) {
+            ui_togglebutton(obj, .label = "Attach", .onchange = doc1_attachment);
+            
+            ui_hbox(obj, .margin = 10, .spacing = 8) {
+                ui_togglebutton(obj, .label = "Enable 1", .onchange = doc_enable_state1, .onchangedata = doc1);
+                ui_togglebutton(obj, .label = "Enable 2", .onchange = doc_enable_state2, .onchangedata = doc1);
+                ui_togglebutton(obj, .label = "Enable 3", .onchange = doc_enable_state3, .onchangedata = doc1);
+                ui_togglebutton(obj, .label = "Enable 4", .onchange = doc_enable_state4, .onchangedata = doc1);
+            }
+            
+            ui_frame(obj, .label = "Doc 2", .fill = TRUE, .margin = 10, .padding = 10, .spacing = 10, .subcontainer = UI_CONTAINER_VBOX) {
+                ui_togglebutton(obj, .label = "Attach", .onchange = doc2_attachment);
+            
+                ui_hbox(obj, .margin = 10, .spacing = 8) {
+                    ui_togglebutton(obj, .label = "Enable 1", .onchange = doc_enable_state1, .onchangedata = doc2);
+                    ui_togglebutton(obj, .label = "Enable 2", .onchange = doc_enable_state2, .onchangedata = doc2);
+                    ui_togglebutton(obj, .label = "Enable 3", .onchange = doc_enable_state3, .onchangedata = doc2);
+                    ui_togglebutton(obj, .label = "Enable 4", .onchange = doc_enable_state4, .onchangedata = doc2);
+                }
+            }
+        }
+    }
+    
+    ui_show(obj);
+}
+
+
+#ifndef UI_WIN32
+
+int main(int argc, char **argv) {
+    ui_init(NULL, argc, argv);
+    ui_onstartup(application_startup, NULL);
+    ui_main();
+    return 0;
+}
+
+#else
+
+int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
+    //ui_init(NULL, argc, argv);
+    //ui_onstartup(application_startup, NULL);
+    //ui_main();
+    return 0;
+}
+
+#endif

mercurial