Sun, 07 Dec 2025 19:20:48 +0100
implement radiobutton (Client)
/* * 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" #include <stdio.h> #include <stdlib.h> #include <string.h> #if !defined(UI_WIN32) 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"); doc->subdocuments = ui_list_new(ctx, "subdocuments"); ui_set(doc->id, buf); // create some sub documents for(int i=0;i<3;i++) { SubDocument *sub = ui_document_new(sizeof(SubDocument)); UiContext *subctx = ui_document_context(sub); sub->id = ui_string_new(subctx, "sub_id"); sub->name = ui_string_new(subctx, "sub_name"); sub->input_name = ui_string_new(subctx, "sub_input_name"); snprintf(buf, 32, "%d.%d", id, i); ui_set(sub->id, buf); ui_list_append(doc->subdocuments, sub); } 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); } printf("selection: %d\n", event->intval); Document *doc = ui_list_get(wdata->doclist, event->intval); if(doc) { char *s1 = ui_get(doc->id); char *s2 = ui_get(doc->name); printf("doc %s - %s\n", s1, s2); ui_attach_document(event->obj->ctx, doc); } } void switch_subdocument(UiEvent *event, void *userdata) { MainWindow *wdata = event->window; Document *doc = event->document; UiContext *ctx = ui_document_context(doc); if(doc->current_sub) { ui_detach_document(ctx, doc->current_sub); doc->current_sub = NULL; } SubDocument *sub = ui_list_get(doc->subdocuments, event->intval); if(sub) { char *s1 = ui_get(sub->id); char *s2 = ui_get(sub->name); printf("sub %s - %s\n", s1, s2); ui_attach_document(ctx, sub); doc->current_sub = sub; } } void* doclist_get_value(void *elm, int col) { Document *doc = elm; return ui_get(doc->id); } void* sublist_get_value(void *elm, int col) { SubDocument *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", .hfill = TRUE, .hexpand = TRUE, .vfill = TRUE); ui_newline(obj); ui_rlabel(obj, .label = "Name:"); ui_llabel(obj, .varname = "name", .hfill = TRUE, .hexpand = TRUE, .vfill = TRUE); ui_newline(obj); ui_rlabel(obj, .label = "Sub Document:"); ui_llabel(obj, .varname = "sub_id", .hfill = TRUE, .hexpand = TRUE, .vfill = TRUE); ui_newline(obj); ui_rlabel(obj, .label = "Name:"); ui_llabel(obj, .varname = "sub_name", .hfill = TRUE, .hexpand = TRUE, .vfill = TRUE); ui_newline(obj); ui_dropdown(obj, .varname = "doclist", .colspan = 2, .onactivate = switch_document, .getvalue = doclist_get_value, .colspan = 2, .hfill = TRUE); ui_newline(obj); ui_frame(obj, .label = "Document", .colspan = 2, .fill = TRUE, .subcontainer = UI_CONTAINER_GRID, .columnspacing = 10, .rowspacing = 10, .padding = 10) { ui_rlabel(obj, .label = "Name:", .vfill = TRUE); ui_textfield(obj, .varname = "input_name", .onchange = update_name); ui_newline(obj); ui_separator(obj, .colspan = 2, .hfill = TRUE); ui_newline(obj); ui_dropdown(obj, .varname = "subdocuments", .getvalue = sublist_get_value, .onactivate = switch_subdocument, .colspan = 2, .hfill = TRUE); } } for(int i=0;i<10;i++) { Document *doc = document_create(i); ui_list_append(wdata->doclist, doc); } ui_list_update(wdata->doclist); ui_list_setselection(wdata->doclist, 0); Document *doc = ui_list_get(wdata->doclist, 0); ui_attach_document(obj->ctx, doc); ui_list_setselection(wdata->doclist, 0); ui_show(obj); } #endif #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