add first code for binding demo

Thu, 23 Oct 2025 21:22:05 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 23 Oct 2025 21:22:05 +0200
changeset 872
1e34a27e016d
parent 871
483191323952
child 873
78853bfc47cf

add first code for binding demo

application/Makefile file | annotate | diff | comparison | revisions
application/demo_bindings.c file | annotate | diff | comparison | revisions
application/demo_bindings.h file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
--- a/application/Makefile	Tue Oct 21 20:26:05 2025 +0200
+++ b/application/Makefile	Thu Oct 23 21:22:05 2025 +0200
@@ -31,18 +31,21 @@
 
 CFLAGS += -I../ui/ -I../ucx
 
-SRC = main.c
-
-OBJ = $(SRC:%.c=../build/application/%$(OBJ_EXT))
+APP_BIN_OBJ = ../build/application/main$(OBJ_EXT)
+DEMO_BINDINGS_OBJ = ../build/application/demo_bindings$(OBJ_EXT)
 
 APP_BIN = ../build/$(BUILD_BIN_DIR)/mk12$(APP_EXT)
+DEMO_BINDINGS = ../build/$(BUILD_BIN_DIR)/demo_bindings$(APP_EXT)
 
-all: $(APP_BIN)
+all: $(APP_BIN) $(DEMO_BINDINGS)
 
 include $(SYS_MAKEFILE)
 
-$(APP_BIN): $(OBJ) $(RES_FILE) $(BUILD_ROOT)/build/$(BUILD_LIB_DIR)/$(LIB_PREFIX)uitk$(LIB_EXT)
-	$(LD) -o $(APP_BIN) $(OBJ) $(RES_FILE) $(BUILD_ROOT)/build/$(BUILD_LIB_DIR)/$(LIB_PREFIX)uitk$(LIB_EXT) $(BUILD_ROOT)/build/$(BUILD_LIB_DIR)/$(LIB_PREFIX)ucx$(LIB_EXT) $(LDFLAGS) $(TK_LDFLAGS)
+$(APP_BIN): $(APP_BIN_OBJ) $(RES_FILE) $(BUILD_ROOT)/build/$(BUILD_LIB_DIR)/$(LIB_PREFIX)uitk$(LIB_EXT)
+	$(LD) -o $(APP_BIN) $(APP_BIN_OBJ) $(RES_FILE) $(BUILD_ROOT)/build/$(BUILD_LIB_DIR)/$(LIB_PREFIX)uitk$(LIB_EXT) $(BUILD_ROOT)/build/$(BUILD_LIB_DIR)/$(LIB_PREFIX)ucx$(LIB_EXT) $(LDFLAGS) $(TK_LDFLAGS)
+
+$(DEMO_BINDINGS): $(DEMO_BINDINGS_OBJ) $(RES_FILE) $(BUILD_ROOT)/build/$(BUILD_LIB_DIR)/$(LIB_PREFIX)uitk$(LIB_EXT)
+	$(LD) -o $(DEMO_BINDINGS) $(DEMO_BINDINGS_OBJ) $(RES_FILE) $(BUILD_ROOT)/build/$(BUILD_LIB_DIR)/$(LIB_PREFIX)uitk$(LIB_EXT) $(BUILD_ROOT)/build/$(BUILD_LIB_DIR)/$(LIB_PREFIX)ucx$(LIB_EXT) $(LDFLAGS) $(TK_LDFLAGS)
 
 ../build/application/%$(OBJ_EXT): %.c
 	$(CC) $(CFLAGS) $(TK_CFLAGS) -o $@ -c $<
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/application/demo_bindings.c	Thu Oct 23 21:22:05 2025 +0200
@@ -0,0 +1,130 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2017 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_bindings.h"
+
+
+
+Document* document_create(int id) {
+    Document* doc = ui_document_new(sizeof(Document));
+    UiContext *ctx = ui_document_context(doc);
+    
+    char buf[32];
+    snprintf(buf, 32, "%d", id);
+    
+    doc->id = ui_string_new(ctx, "id");
+    doc->name = ui_string_new(ctx, "name");
+    
+    doc->input_name = ui_string_new(ctx, "input_name");
+    
+    ui_set(doc->id, buf);
+    
+    return doc;
+}
+
+void update_name(UiEvent *event, void *userdata) {
+    Document *doc = event->document;
+    if(!doc) {
+        return;
+    }
+    char *s = ui_get(doc->input_name);
+    ui_set(doc->name, s);
+}
+
+void switch_document(UiEvent *event, void *userdata) {
+    MainWindow *wdata = event->window;
+    if(event->document) {
+        ui_detach_document(event->obj->ctx, event->document);
+    }
+    Document *doc = ui_list_get(wdata->doclist, event->intval);
+    if(doc) {
+        ui_attach_document(event->obj->ctx, doc);
+    }
+}
+
+void* doclist_get_value(void *elm, int col) {
+    Document *doc = elm;
+    return ui_get(doc->id);
+}
+
+void application_startup(UiEvent *event, void *data) {
+    UiObject *obj = ui_simple_window("Bindings Demo", NULL);
+    MainWindow *wdata = ui_malloc(obj->ctx, sizeof(MainWindow));
+    obj->window = wdata;
+    
+    wdata->doclist = ui_list_new(obj->ctx, "doclist");
+    
+    ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10, .fill = TRUE) {
+        ui_rlabel(obj, .label = "Document:");
+        ui_llabel(obj, .varname = "id");
+        ui_newline(obj);
+        
+        ui_rlabel(obj, .label = "Name:");
+        ui_llabel(obj, .varname = "name");
+        ui_newline(obj);
+        
+        ui_combobox(obj, .varname = "doclist", .colspan = 2, .onactivate = switch_document, .getvalue = doclist_get_value);
+        ui_newline(obj);
+        
+        ui_frame(obj, .label = "Document", .colspan = 2, .fill = TRUE) {
+            ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10) {
+                ui_rlabel(obj, .label = "Name:", .vfill = TRUE);
+                ui_textfield(obj, .varname = "input_name", .onchange = update_name);
+            }
+        }
+    }
+    
+    for(int i=0;i<10;i++) {
+        Document *doc = document_create(i);
+        ui_list_append(wdata->doclist, doc);
+    }
+    ui_list_update(wdata->doclist);
+    Document *doc = ui_list_get(wdata->doclist, 0);
+    ui_attach_document(obj->ctx, doc);
+    
+    
+    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) {
+    return 0;
+}
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/application/demo_bindings.h	Thu Oct 23 21:22:05 2025 +0200
@@ -0,0 +1,40 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2017 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 <ui/ui.h>
+
+typedef struct MainWindow {
+    UiList *doclist;
+} MainWindow;
+
+typedef struct Document {
+    UiString *id;
+    UiString *name;
+    
+    UiString *input_name;
+} Document;
--- a/ui/gtk/toolkit.c	Tue Oct 21 20:26:05 2025 +0200
+++ b/ui/gtk/toolkit.c	Thu Oct 23 21:22:05 2025 +0200
@@ -40,7 +40,6 @@
 #include "../common/toolbar.h"
 #include "../common/threadpool.h"
 
-#include <cx/utils.h>
 #include <cx/string.h>
 #include <cx/printf.h>
 

mercurial